1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
38 // Sets up the default soname_ to use, in the (rare) cases we never
39 // see a DT_SONAME entry.
41 Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
42 : Object(name, input_file, true, offset)
44 // This will be overridden by a DT_SONAME entry, hopefully. But if
45 // we never see a DT_SONAME entry, our rule is to use the dynamic
46 // object's filename. The only exception is when the dynamic object
47 // is part of an archive (so the filename is the archive's
48 // filename). In that case, we use just the dynobj's name-in-archive.
49 this->soname_ = this->input_file()->found_name();
50 if (this->offset() != 0)
52 std::string::size_type open_paren = this->name().find('(');
53 std::string::size_type close_paren = this->name().find(')');
54 if (open_paren != std::string::npos && close_paren != std::string::npos)
56 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
57 this->soname_ = this->name().substr(open_paren + 1,
58 close_paren - (open_paren + 1));
63 // Return the string to use in a DT_NEEDED entry.
66 Dynobj::soname() const
68 return this->soname_.c_str();
71 // Class Sized_dynobj.
73 template<int size, bool big_endian>
74 Sized_dynobj<size, big_endian>::Sized_dynobj(
75 const std::string& name,
76 Input_file* input_file,
78 const elfcpp::Ehdr<size, big_endian>& ehdr)
79 : Dynobj(name, input_file, offset),
86 template<int size, bool big_endian>
88 Sized_dynobj<size, big_endian>::setup(
89 const elfcpp::Ehdr<size, big_endian>& ehdr)
91 this->set_target(ehdr.get_e_machine(), size, big_endian,
92 ehdr.get_e_ident()[elfcpp::EI_OSABI],
93 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
95 const unsigned int shnum = this->elf_file_.shnum();
96 this->set_shnum(shnum);
99 // Find the SHT_DYNSYM section and the various version sections, and
100 // the dynamic section, given the section headers.
102 template<int size, bool big_endian>
104 Sized_dynobj<size, big_endian>::find_dynsym_sections(
105 const unsigned char* pshdrs,
106 unsigned int* pdynsym_shndx,
107 unsigned int* pversym_shndx,
108 unsigned int* pverdef_shndx,
109 unsigned int* pverneed_shndx,
110 unsigned int* pdynamic_shndx)
112 *pdynsym_shndx = -1U;
113 *pversym_shndx = -1U;
114 *pverdef_shndx = -1U;
115 *pverneed_shndx = -1U;
116 *pdynamic_shndx = -1U;
118 const unsigned int shnum = this->shnum();
119 const unsigned char* p = pshdrs;
120 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
122 typename This::Shdr shdr(p);
125 switch (shdr.get_sh_type())
127 case elfcpp::SHT_DYNSYM:
130 case elfcpp::SHT_GNU_versym:
133 case elfcpp::SHT_GNU_verdef:
136 case elfcpp::SHT_GNU_verneed:
139 case elfcpp::SHT_DYNAMIC:
151 this->error(_("unexpected duplicate type %u section: %u, %u"),
152 shdr.get_sh_type(), *pi, i);
158 // Read the contents of section SHNDX. PSHDRS points to the section
159 // headers. TYPE is the expected section type. LINK is the expected
160 // section link. Store the data in *VIEW and *VIEW_SIZE. The
161 // section's sh_info field is stored in *VIEW_INFO.
163 template<int size, bool big_endian>
165 Sized_dynobj<size, big_endian>::read_dynsym_section(
166 const unsigned char* pshdrs,
172 unsigned int* view_info)
182 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
184 gold_assert(shdr.get_sh_type() == type);
186 if (shdr.get_sh_link() != link)
187 this->error(_("unexpected link in section %u header: %u != %u"),
188 shndx, shdr.get_sh_link(), link);
190 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
192 *view_size = shdr.get_sh_size();
193 *view_info = shdr.get_sh_info();
196 // Set the soname field if this shared object has a DT_SONAME tag.
197 // PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
198 // index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
199 // STRTAB_SIZE are the section index and contents of a string table
200 // which may be the one associated with the SHT_DYNAMIC section.
202 template<int size, bool big_endian>
204 Sized_dynobj<size, big_endian>::set_soname(const unsigned char* pshdrs,
205 unsigned int dynamic_shndx,
206 unsigned int strtab_shndx,
207 const unsigned char* strtabu,
210 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
211 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
213 const off_t dynamic_size = dynamicshdr.get_sh_size();
214 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
215 dynamic_size, false);
217 const unsigned int link = dynamicshdr.get_sh_link();
218 if (link != strtab_shndx)
220 if (link >= this->shnum())
222 this->error(_("DYNAMIC section %u link out of range: %u"),
223 dynamic_shndx, link);
227 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
228 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
230 this->error(_("DYNAMIC section %u link %u is not a strtab"),
231 dynamic_shndx, link);
235 strtab_size = strtabshdr.get_sh_size();
236 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false);
239 for (const unsigned char* p = pdynamic;
240 p < pdynamic + dynamic_size;
243 typename This::Dyn dyn(p);
245 if (dyn.get_d_tag() == elfcpp::DT_SONAME)
247 off_t val = dyn.get_d_val();
248 if (val >= strtab_size)
250 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
251 static_cast<long long>(val),
252 static_cast<long long>(strtab_size));
256 const char* strtab = reinterpret_cast<const char*>(strtabu);
257 this->set_soname_string(strtab + val);
261 if (dyn.get_d_tag() == elfcpp::DT_NULL)
265 this->error(_("missing DT_NULL in dynamic segment"));
268 // Read the symbols and sections from a dynamic object. We read the
269 // dynamic symbols, not the normal symbols.
271 template<int size, bool big_endian>
273 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
275 this->read_section_data(&this->elf_file_, sd);
277 const unsigned char* const pshdrs = sd->section_headers->data();
279 unsigned int dynsym_shndx;
280 unsigned int versym_shndx;
281 unsigned int verdef_shndx;
282 unsigned int verneed_shndx;
283 unsigned int dynamic_shndx;
284 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
285 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
287 unsigned int strtab_shndx = -1U;
290 sd->symbols_size = 0;
291 sd->symbol_names = NULL;
292 sd->symbol_names_size = 0;
294 if (dynsym_shndx != -1U)
296 // Get the dynamic symbols.
297 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
298 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
300 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
301 dynsymshdr.get_sh_size(), false);
302 sd->symbols_size = dynsymshdr.get_sh_size();
304 // Get the symbol names.
305 strtab_shndx = dynsymshdr.get_sh_link();
306 if (strtab_shndx >= this->shnum())
308 this->error(_("invalid dynamic symbol table name index: %u"),
312 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
313 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
315 this->error(_("dynamic symbol table name section "
316 "has wrong type: %u"),
317 static_cast<unsigned int>(strtabshdr.get_sh_type()));
321 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
322 strtabshdr.get_sh_size(),
324 sd->symbol_names_size = strtabshdr.get_sh_size();
326 // Get the version information.
329 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
330 dynsym_shndx, &sd->versym, &sd->versym_size,
333 // We require that the version definition and need section link
334 // to the same string table as the dynamic symbol table. This
335 // is not a technical requirement, but it always happens in
336 // practice. We could change this if necessary.
338 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
339 strtab_shndx, &sd->verdef, &sd->verdef_size,
342 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
343 strtab_shndx, &sd->verneed, &sd->verneed_size,
347 // Read the SHT_DYNAMIC section to find whether this shared object
348 // has a DT_SONAME tag. This doesn't really have anything to do
349 // with reading the symbols, but this is a convenient place to do
351 if (dynamic_shndx != -1U)
352 this->set_soname(pshdrs, dynamic_shndx, strtab_shndx,
353 (sd->symbol_names == NULL
355 : sd->symbol_names->data()),
356 sd->symbol_names_size);
359 // Lay out the input sections for a dynamic object. We don't want to
360 // include sections from a dynamic object, so all that we actually do
361 // here is check for .gnu.warning sections.
363 template<int size, bool big_endian>
365 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
367 Read_symbols_data* sd)
369 const unsigned int shnum = this->shnum();
373 // Get the section headers.
374 const unsigned char* pshdrs = sd->section_headers->data();
376 // Get the section names.
377 const unsigned char* pnamesu = sd->section_names->data();
378 const char* pnames = reinterpret_cast<const char*>(pnamesu);
380 // Skip the first, dummy, section.
381 pshdrs += This::shdr_size;
382 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
384 typename This::Shdr shdr(pshdrs);
386 if (shdr.get_sh_name() >= sd->section_names_size)
388 this->error(_("bad section name offset for section %u: %lu"),
389 i, static_cast<unsigned long>(shdr.get_sh_name()));
393 const char* name = pnames + shdr.get_sh_name();
395 this->handle_gnu_warning_section(name, i, symtab);
398 delete sd->section_headers;
399 sd->section_headers = NULL;
400 delete sd->section_names;
401 sd->section_names = NULL;
404 // Add an entry to the vector mapping version numbers to version
407 template<int size, bool big_endian>
409 Sized_dynobj<size, big_endian>::set_version_map(
410 Version_map* version_map,
412 const char* name) const
414 if (ndx >= version_map->size())
415 version_map->resize(ndx + 1);
416 if ((*version_map)[ndx] != NULL)
417 this->error(_("duplicate definition for version %u"), ndx);
418 (*version_map)[ndx] = name;
421 // Add mappings for the version definitions to VERSION_MAP.
423 template<int size, bool big_endian>
425 Sized_dynobj<size, big_endian>::make_verdef_map(
426 Read_symbols_data* sd,
427 Version_map* version_map) const
429 if (sd->verdef == NULL)
432 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
433 off_t names_size = sd->symbol_names_size;
435 const unsigned char* pverdef = sd->verdef->data();
436 off_t verdef_size = sd->verdef_size;
437 const unsigned int count = sd->verdef_info;
439 const unsigned char* p = pverdef;
440 for (unsigned int i = 0; i < count; ++i)
442 elfcpp::Verdef<size, big_endian> verdef(p);
444 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
446 this->error(_("unexpected verdef version %u"),
447 verdef.get_vd_version());
451 const unsigned int vd_ndx = verdef.get_vd_ndx();
453 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
456 // The first Verdaux holds the name of this version. Subsequent
457 // ones are versions that this one depends upon, which we don't
459 const unsigned int vd_cnt = verdef.get_vd_cnt();
462 this->error(_("verdef vd_cnt field too small: %u"), vd_cnt);
466 const unsigned int vd_aux = verdef.get_vd_aux();
467 if ((p - pverdef) + vd_aux >= verdef_size)
469 this->error(_("verdef vd_aux field out of range: %u"), vd_aux);
473 const unsigned char* pvda = p + vd_aux;
474 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
476 const unsigned int vda_name = verdaux.get_vda_name();
477 if (vda_name >= names_size)
479 this->error(_("verdaux vda_name field out of range: %u"), vda_name);
483 this->set_version_map(version_map, vd_ndx, names + vda_name);
485 const unsigned int vd_next = verdef.get_vd_next();
486 if ((p - pverdef) + vd_next >= verdef_size)
488 this->error(_("verdef vd_next field out of range: %u"), vd_next);
496 // Add mappings for the required versions to VERSION_MAP.
498 template<int size, bool big_endian>
500 Sized_dynobj<size, big_endian>::make_verneed_map(
501 Read_symbols_data* sd,
502 Version_map* version_map) const
504 if (sd->verneed == NULL)
507 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
508 off_t names_size = sd->symbol_names_size;
510 const unsigned char* pverneed = sd->verneed->data();
511 const off_t verneed_size = sd->verneed_size;
512 const unsigned int count = sd->verneed_info;
514 const unsigned char* p = pverneed;
515 for (unsigned int i = 0; i < count; ++i)
517 elfcpp::Verneed<size, big_endian> verneed(p);
519 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
521 this->error(_("unexpected verneed version %u"),
522 verneed.get_vn_version());
526 const unsigned int vn_aux = verneed.get_vn_aux();
528 if ((p - pverneed) + vn_aux >= verneed_size)
530 this->error(_("verneed vn_aux field out of range: %u"), vn_aux);
534 const unsigned int vn_cnt = verneed.get_vn_cnt();
535 const unsigned char* pvna = p + vn_aux;
536 for (unsigned int j = 0; j < vn_cnt; ++j)
538 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
540 const unsigned int vna_name = vernaux.get_vna_name();
541 if (vna_name >= names_size)
543 this->error(_("vernaux vna_name field out of range: %u"),
548 this->set_version_map(version_map, vernaux.get_vna_other(),
551 const unsigned int vna_next = vernaux.get_vna_next();
552 if ((pvna - pverneed) + vna_next >= verneed_size)
554 this->error(_("verneed vna_next field out of range: %u"),
562 const unsigned int vn_next = verneed.get_vn_next();
563 if ((p - pverneed) + vn_next >= verneed_size)
565 this->error(_("verneed vn_next field out of range: %u"), vn_next);
573 // Create a vector mapping version numbers to version strings.
575 template<int size, bool big_endian>
577 Sized_dynobj<size, big_endian>::make_version_map(
578 Read_symbols_data* sd,
579 Version_map* version_map) const
581 if (sd->verdef == NULL && sd->verneed == NULL)
584 // A guess at the maximum version number we will see. If this is
585 // wrong we will be less efficient but still correct.
586 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
588 this->make_verdef_map(sd, version_map);
589 this->make_verneed_map(sd, version_map);
592 // Add the dynamic symbols to the symbol table.
594 template<int size, bool big_endian>
596 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
597 Read_symbols_data* sd)
599 if (sd->symbols == NULL)
601 gold_assert(sd->symbol_names == NULL);
602 gold_assert(sd->versym == NULL && sd->verdef == NULL
603 && sd->verneed == NULL);
607 const int sym_size = This::sym_size;
608 const size_t symcount = sd->symbols_size / sym_size;
609 if (static_cast<off_t>(symcount * sym_size) != sd->symbols_size)
611 this->error(_("size of dynamic symbols is not multiple of symbol size"));
615 Version_map version_map;
616 this->make_version_map(sd, &version_map);
618 const char* sym_names =
619 reinterpret_cast<const char*>(sd->symbol_names->data());
620 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
621 sym_names, sd->symbol_names_size,
624 : sd->versym->data()),
630 delete sd->symbol_names;
631 sd->symbol_names = NULL;
632 if (sd->versym != NULL)
637 if (sd->verdef != NULL)
642 if (sd->verneed != NULL)
649 // Given a vector of hash codes, compute the number of hash buckets to
653 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
654 bool for_gnu_hash_table)
656 // FIXME: Implement optional hash table optimization.
658 // Array used to determine the number of hash table buckets to use
659 // based on the number of symbols there are. If there are fewer
660 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
661 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
662 // use more than 32771 buckets. This is straight from the old GNU
664 static const unsigned int buckets[] =
666 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
669 const int buckets_count = sizeof buckets / sizeof buckets[0];
671 unsigned int symcount = hashcodes.size();
672 unsigned int ret = 1;
673 for (int i = 0; i < buckets_count; ++i)
675 if (symcount < buckets[i])
680 if (for_gnu_hash_table && ret < 2)
686 // The standard ELF hash function. This hash function must not
687 // change, as the dynamic linker uses it also.
690 Dynobj::elf_hash(const char* name)
692 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
695 while ((c = *nameu++) != '\0')
698 uint32_t g = h & 0xf0000000;
702 // The ELF ABI says h &= ~g, but using xor is equivalent in
703 // this case (since g was set from h) and may save one
711 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
712 // DYNSYMS is a vector with all the global dynamic symbols.
713 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
717 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
718 unsigned int local_dynsym_count,
719 unsigned char** pphash,
720 unsigned int* phashlen)
722 unsigned int dynsym_count = dynsyms.size();
724 // Get the hash values for all the symbols.
725 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
726 for (unsigned int i = 0; i < dynsym_count; ++i)
727 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
729 const unsigned int bucketcount =
730 Dynobj::compute_bucket_count(dynsym_hashvals, false);
732 std::vector<uint32_t> bucket(bucketcount);
733 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
735 for (unsigned int i = 0; i < dynsym_count; ++i)
737 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
738 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
739 chain[dynsym_index] = bucket[bucketpos];
740 bucket[bucketpos] = dynsym_index;
743 unsigned int hashlen = ((2
748 unsigned char* phash = new unsigned char[hashlen];
750 if (parameters->is_big_endian())
752 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
753 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
761 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
762 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
773 // Fill in an ELF hash table.
775 template<bool big_endian>
777 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
778 const std::vector<uint32_t>& chain,
779 unsigned char* phash,
780 unsigned int hashlen)
782 unsigned char* p = phash;
784 const unsigned int bucketcount = bucket.size();
785 const unsigned int chaincount = chain.size();
787 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
789 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
792 for (unsigned int i = 0; i < bucketcount; ++i)
794 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
798 for (unsigned int i = 0; i < chaincount; ++i)
800 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
804 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
807 // The hash function used for the GNU hash table. This hash function
808 // must not change, as the dynamic linker uses it also.
811 Dynobj::gnu_hash(const char* name)
813 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
816 while ((c = *nameu++) != '\0')
817 h = (h << 5) + h + c;
821 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
822 // tables are an extension to ELF which are recognized by the GNU
823 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
824 // TARGET is the target. DYNSYMS is a vector with all the global
825 // symbols which will be going into the dynamic symbol table.
826 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
830 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
831 unsigned int local_dynsym_count,
832 unsigned char** pphash,
833 unsigned int* phashlen)
835 const unsigned int count = dynsyms.size();
837 // Sort the dynamic symbols into two vectors. Symbols which we do
838 // not want to put into the hash table we store into
839 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
840 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
841 // and records the hash codes.
843 std::vector<Symbol*> unhashed_dynsyms;
844 unhashed_dynsyms.reserve(count);
846 std::vector<Symbol*> hashed_dynsyms;
847 hashed_dynsyms.reserve(count);
849 std::vector<uint32_t> dynsym_hashvals;
850 dynsym_hashvals.reserve(count);
852 for (unsigned int i = 0; i < count; ++i)
854 Symbol* sym = dynsyms[i];
856 // FIXME: Should put on unhashed_dynsyms if the symbol is
858 if (sym->is_undefined())
859 unhashed_dynsyms.push_back(sym);
862 hashed_dynsyms.push_back(sym);
863 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
867 // Put the unhashed symbols at the start of the global portion of
868 // the dynamic symbol table.
869 const unsigned int unhashed_count = unhashed_dynsyms.size();
870 unsigned int unhashed_dynsym_index = local_dynsym_count;
871 for (unsigned int i = 0; i < unhashed_count; ++i)
873 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
874 ++unhashed_dynsym_index;
877 // For the actual data generation we call out to a templatized
879 int size = parameters->get_size();
880 bool big_endian = parameters->is_big_endian();
885 #ifdef HAVE_TARGET_32_BIG
886 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
888 unhashed_dynsym_index,
897 #ifdef HAVE_TARGET_32_LITTLE
898 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
900 unhashed_dynsym_index,
912 #ifdef HAVE_TARGET_64_BIG
913 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
915 unhashed_dynsym_index,
924 #ifdef HAVE_TARGET_64_LITTLE
925 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
927 unhashed_dynsym_index,
939 // Create the actual data for a GNU hash table. This is just a copy
940 // of the code from the old GNU linker.
942 template<int size, bool big_endian>
944 Dynobj::sized_create_gnu_hash_table(
945 const std::vector<Symbol*>& hashed_dynsyms,
946 const std::vector<uint32_t>& dynsym_hashvals,
947 unsigned int unhashed_dynsym_count,
948 unsigned char** pphash,
949 unsigned int* phashlen)
951 if (hashed_dynsyms.empty())
953 // Special case for the empty hash table.
954 unsigned int hashlen = 5 * 4 + size / 8;
955 unsigned char* phash = new unsigned char[hashlen];
957 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
958 // Symbol index above unhashed symbols.
959 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
960 // One word for bitmask.
961 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
962 // Only bloom filter.
963 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
965 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
966 // No hashes in only bucket.
967 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
975 const unsigned int bucketcount =
976 Dynobj::compute_bucket_count(dynsym_hashvals, true);
978 const unsigned int nsyms = hashed_dynsyms.size();
980 uint32_t maskbitslog2 = 1;
981 uint32_t x = nsyms >> 1;
987 if (maskbitslog2 < 3)
989 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
999 if (maskbitslog2 == 5)
1003 uint32_t mask = (1U << shift1) - 1U;
1004 uint32_t shift2 = maskbitslog2;
1005 uint32_t maskbits = 1U << maskbitslog2;
1006 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1008 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1009 std::vector<Word> bitmask(maskwords);
1010 std::vector<uint32_t> counts(bucketcount);
1011 std::vector<uint32_t> indx(bucketcount);
1012 uint32_t symindx = unhashed_dynsym_count;
1014 // Count the number of times each hash bucket is used.
1015 for (unsigned int i = 0; i < nsyms; ++i)
1016 ++counts[dynsym_hashvals[i] % bucketcount];
1018 unsigned int cnt = symindx;
1019 for (unsigned int i = 0; i < bucketcount; ++i)
1025 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1026 hashlen += maskbits / 8;
1027 unsigned char* phash = new unsigned char[hashlen];
1029 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1030 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1031 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1032 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1034 unsigned char* p = phash + 16 + maskbits / 8;
1035 for (unsigned int i = 0; i < bucketcount; ++i)
1038 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1040 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1044 for (unsigned int i = 0; i < nsyms; ++i)
1046 Symbol* sym = hashed_dynsyms[i];
1047 uint32_t hashval = dynsym_hashvals[i];
1049 unsigned int bucket = hashval % bucketcount;
1050 unsigned int val = ((hashval >> shift1)
1051 & ((maskbits >> shift1) - 1));
1052 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1053 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1054 val = hashval & ~ 1U;
1055 if (counts[bucket] == 1)
1057 // Last element terminates the chain.
1060 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1064 sym->set_dynsym_index(indx[bucket]);
1069 for (unsigned int i = 0; i < maskwords; ++i)
1071 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1075 *phashlen = hashlen;
1081 // Write this definition to a buffer for the output section.
1083 template<int size, bool big_endian>
1085 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb
1086 ACCEPT_SIZE_ENDIAN) const
1088 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1089 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1091 elfcpp::Verdef_write<size, big_endian> vd(pb);
1092 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1093 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1094 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1095 vd.set_vd_ndx(this->index());
1096 vd.set_vd_cnt(1 + this->deps_.size());
1097 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1098 vd.set_vd_aux(verdef_size);
1099 vd.set_vd_next(is_last
1101 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1104 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1105 vda.set_vda_name(dynpool->get_offset(this->name()));
1106 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1109 Deps::const_iterator p;
1111 for (p = this->deps_.begin(), i = 0;
1112 p != this->deps_.end();
1115 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1116 vda.set_vda_name(dynpool->get_offset(*p));
1117 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1128 for (Need_versions::iterator p = this->need_versions_.begin();
1129 p != this->need_versions_.end();
1134 // Add a new version to this file reference.
1137 Verneed::add_name(const char* name)
1139 Verneed_version* vv = new Verneed_version(name);
1140 this->need_versions_.push_back(vv);
1144 // Set the version indexes starting at INDEX.
1147 Verneed::finalize(unsigned int index)
1149 for (Need_versions::iterator p = this->need_versions_.begin();
1150 p != this->need_versions_.end();
1153 (*p)->set_index(index);
1159 // Write this list of referenced versions to a buffer for the output
1162 template<int size, bool big_endian>
1164 Verneed::write(const Stringpool* dynpool, bool is_last,
1165 unsigned char* pb ACCEPT_SIZE_ENDIAN) const
1167 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1168 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1170 elfcpp::Verneed_write<size, big_endian> vn(pb);
1171 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1172 vn.set_vn_cnt(this->need_versions_.size());
1173 vn.set_vn_file(dynpool->get_offset(this->filename()));
1174 vn.set_vn_aux(verneed_size);
1175 vn.set_vn_next(is_last
1177 : verneed_size + this->need_versions_.size() * vernaux_size);
1180 Need_versions::const_iterator p;
1182 for (p = this->need_versions_.begin(), i = 0;
1183 p != this->need_versions_.end();
1186 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1187 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1188 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1189 vna.set_vna_flags(0);
1190 vna.set_vna_other((*p)->index());
1191 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1192 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1201 // Versions methods.
1203 Versions::~Versions()
1205 for (Defs::iterator p = this->defs_.begin();
1206 p != this->defs_.end();
1210 for (Needs::iterator p = this->needs_.begin();
1211 p != this->needs_.end();
1216 // Return the dynamic object which a symbol refers to.
1219 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1220 const Symbol* sym) const
1222 if (sym->is_copied_from_dynobj())
1223 return symtab->get_copy_source(sym);
1226 Object* object = sym->object();
1227 gold_assert(object->is_dynamic());
1228 return static_cast<Dynobj*>(object);
1232 // Record version information for a symbol going into the dynamic
1236 Versions::record_version(const Symbol_table* symtab,
1237 Stringpool* dynpool, const Symbol* sym)
1239 gold_assert(!this->is_finalized_);
1240 gold_assert(sym->version() != NULL);
1242 Stringpool::Key version_key;
1243 const char* version = dynpool->add(sym->version(), false, &version_key);
1245 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1247 if (parameters->output_is_shared())
1248 this->add_def(sym, version, version_key);
1252 // This is a version reference.
1253 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1254 this->add_need(dynpool, dynobj->soname(), version, version_key);
1258 // We've found a symbol SYM defined in version VERSION.
1261 Versions::add_def(const Symbol* sym, const char* version,
1262 Stringpool::Key version_key)
1264 Key k(version_key, 0);
1265 Version_base* const vbnull = NULL;
1266 std::pair<Version_table::iterator, bool> ins =
1267 this->version_table_.insert(std::make_pair(k, vbnull));
1271 // We already have an entry for this version.
1272 Version_base* vb = ins.first->second;
1274 // We have now seen a symbol in this version, so it is not
1278 // FIXME: When we support version scripts, we will need to
1279 // check whether this symbol should be forced local.
1283 // If we are creating a shared object, it is an error to
1284 // find a definition of a symbol with a version which is not
1285 // in the version script.
1286 if (parameters->output_is_shared())
1288 gold_error(_("symbol %s has undefined version %s"),
1289 sym->name(), version);
1293 // If this is the first version we are defining, first define
1294 // the base version. FIXME: Should use soname here when
1295 // creating a shared object.
1296 Verdef* vdbase = new Verdef(parameters->output_file_name(), true, false,
1298 this->defs_.push_back(vdbase);
1300 // When creating a regular executable, automatically define
1302 Verdef* vd = new Verdef(version, false, false, false);
1303 this->defs_.push_back(vd);
1304 ins.first->second = vd;
1308 // Add a reference to version NAME in file FILENAME.
1311 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1312 Stringpool::Key name_key)
1314 Stringpool::Key filename_key;
1315 filename = dynpool->add(filename, true, &filename_key);
1317 Key k(name_key, filename_key);
1318 Version_base* const vbnull = NULL;
1319 std::pair<Version_table::iterator, bool> ins =
1320 this->version_table_.insert(std::make_pair(k, vbnull));
1324 // We already have an entry for this filename/version.
1328 // See whether we already have this filename. We don't expect many
1329 // version references, so we just do a linear search. This could be
1330 // replaced by a hash table.
1332 for (Needs::iterator p = this->needs_.begin();
1333 p != this->needs_.end();
1336 if ((*p)->filename() == filename)
1345 // We have a new filename.
1346 vn = new Verneed(filename);
1347 this->needs_.push_back(vn);
1350 ins.first->second = vn->add_name(name);
1353 // Set the version indexes. Create a new dynamic version symbol for
1354 // each new version definition.
1357 Versions::finalize(const Target* target, Symbol_table* symtab,
1358 unsigned int dynsym_index, std::vector<Symbol*>* syms)
1360 gold_assert(!this->is_finalized_);
1362 unsigned int vi = 1;
1364 for (Defs::iterator p = this->defs_.begin();
1365 p != this->defs_.end();
1368 (*p)->set_index(vi);
1371 // Create a version symbol if necessary.
1372 if (!(*p)->is_symbol_created())
1374 Symbol* vsym = symtab->define_as_constant(target, (*p)->name(),
1378 elfcpp::STV_DEFAULT, 0,
1380 vsym->set_needs_dynsym_entry();
1381 vsym->set_dynsym_index(dynsym_index);
1383 syms->push_back(vsym);
1384 // The name is already in the dynamic pool.
1388 // Index 1 is used for global symbols.
1391 gold_assert(this->defs_.empty());
1395 for (Needs::iterator p = this->needs_.begin();
1396 p != this->needs_.end();
1398 vi = (*p)->finalize(vi);
1400 this->is_finalized_ = true;
1402 return dynsym_index;
1405 // Return the version index to use for a symbol. This does two hash
1406 // table lookups: one in DYNPOOL and one in this->version_table_.
1407 // Another approach alternative would be store a pointer in SYM, which
1408 // would increase the size of the symbol table. Or perhaps we could
1409 // use a hash table from dynamic symbol pointer values to Version_base
1413 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1414 const Symbol* sym) const
1416 Stringpool::Key version_key;
1417 const char* version = dynpool->find(sym->version(), &version_key);
1418 gold_assert(version != NULL);
1421 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1423 if (!parameters->output_is_shared())
1424 return elfcpp::VER_NDX_GLOBAL;
1425 k = Key(version_key, 0);
1429 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1431 Stringpool::Key filename_key;
1432 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1433 gold_assert(filename != NULL);
1435 k = Key(version_key, filename_key);
1438 Version_table::const_iterator p = this->version_table_.find(k);
1439 gold_assert(p != this->version_table_.end());
1441 return p->second->index();
1444 // Return an allocated buffer holding the contents of the symbol
1447 template<int size, bool big_endian>
1449 Versions::symbol_section_contents(const Symbol_table* symtab,
1450 const Stringpool* dynpool,
1451 unsigned int local_symcount,
1452 const std::vector<Symbol*>& syms,
1455 ACCEPT_SIZE_ENDIAN) const
1457 gold_assert(this->is_finalized_);
1459 unsigned int sz = (local_symcount + syms.size()) * 2;
1460 unsigned char* pbuf = new unsigned char[sz];
1462 for (unsigned int i = 0; i < local_symcount; ++i)
1463 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1464 elfcpp::VER_NDX_LOCAL);
1466 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1470 unsigned int version_index;
1471 const char* version = (*p)->version();
1472 if (version == NULL)
1473 version_index = elfcpp::VER_NDX_GLOBAL;
1475 version_index = this->version_index(symtab, dynpool, *p);
1476 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1484 // Return an allocated buffer holding the contents of the version
1485 // definition section.
1487 template<int size, bool big_endian>
1489 Versions::def_section_contents(const Stringpool* dynpool,
1490 unsigned char** pp, unsigned int* psize,
1491 unsigned int* pentries
1492 ACCEPT_SIZE_ENDIAN) const
1494 gold_assert(this->is_finalized_);
1495 gold_assert(!this->defs_.empty());
1497 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1498 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1500 unsigned int sz = 0;
1501 for (Defs::const_iterator p = this->defs_.begin();
1502 p != this->defs_.end();
1505 sz += verdef_size + verdaux_size;
1506 sz += (*p)->count_dependencies() * verdaux_size;
1509 unsigned char* pbuf = new unsigned char[sz];
1511 unsigned char* pb = pbuf;
1512 Defs::const_iterator p;
1514 for (p = this->defs_.begin(), i = 0;
1515 p != this->defs_.end();
1517 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1518 dynpool, i + 1 >= this->defs_.size(), pb
1519 SELECT_SIZE_ENDIAN(size, big_endian));
1521 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1525 *pentries = this->defs_.size();
1528 // Return an allocated buffer holding the contents of the version
1529 // reference section.
1531 template<int size, bool big_endian>
1533 Versions::need_section_contents(const Stringpool* dynpool,
1534 unsigned char** pp, unsigned int *psize,
1535 unsigned int *pentries
1536 ACCEPT_SIZE_ENDIAN) const
1538 gold_assert(this->is_finalized_);
1539 gold_assert(!this->needs_.empty());
1541 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1542 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1544 unsigned int sz = 0;
1545 for (Needs::const_iterator p = this->needs_.begin();
1546 p != this->needs_.end();
1550 sz += (*p)->count_versions() * vernaux_size;
1553 unsigned char* pbuf = new unsigned char[sz];
1555 unsigned char* pb = pbuf;
1556 Needs::const_iterator p;
1558 for (p = this->needs_.begin(), i = 0;
1559 p != this->needs_.end();
1561 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1562 dynpool, i + 1 >= this->needs_.size(), pb
1563 SELECT_SIZE_ENDIAN(size, big_endian));
1565 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1569 *pentries = this->needs_.size();
1572 // Instantiate the templates we need. We could use the configure
1573 // script to restrict this to only the ones for implemented targets.
1575 #ifdef HAVE_TARGET_32_LITTLE
1577 class Sized_dynobj<32, false>;
1580 #ifdef HAVE_TARGET_32_BIG
1582 class Sized_dynobj<32, true>;
1585 #ifdef HAVE_TARGET_64_LITTLE
1587 class Sized_dynobj<64, false>;
1590 #ifdef HAVE_TARGET_64_BIG
1592 class Sized_dynobj<64, true>;
1595 #ifdef HAVE_TARGET_32_LITTLE
1598 Versions::symbol_section_contents<32, false>(
1599 const Symbol_table*,
1602 const std::vector<Symbol*>&,
1605 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1608 #ifdef HAVE_TARGET_32_BIG
1611 Versions::symbol_section_contents<32, true>(
1612 const Symbol_table*,
1615 const std::vector<Symbol*>&,
1618 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1621 #ifdef HAVE_TARGET_64_LITTLE
1624 Versions::symbol_section_contents<64, false>(
1625 const Symbol_table*,
1628 const std::vector<Symbol*>&,
1631 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1634 #ifdef HAVE_TARGET_64_BIG
1637 Versions::symbol_section_contents<64, true>(
1638 const Symbol_table*,
1641 const std::vector<Symbol*>&,
1644 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1647 #ifdef HAVE_TARGET_32_LITTLE
1650 Versions::def_section_contents<32, false>(
1655 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1658 #ifdef HAVE_TARGET_32_BIG
1661 Versions::def_section_contents<32, true>(
1666 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1669 #ifdef HAVE_TARGET_64_LITTLE
1672 Versions::def_section_contents<64, false>(
1677 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1680 #ifdef HAVE_TARGET_64_BIG
1683 Versions::def_section_contents<64, true>(
1688 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1691 #ifdef HAVE_TARGET_32_LITTLE
1694 Versions::need_section_contents<32, false>(
1699 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1702 #ifdef HAVE_TARGET_32_BIG
1705 Versions::need_section_contents<32, true>(
1710 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1713 #ifdef HAVE_TARGET_64_LITTLE
1716 Versions::need_section_contents<64, false>(
1721 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1724 #ifdef HAVE_TARGET_64_BIG
1727 Versions::need_section_contents<64, true>(
1732 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1735 } // End namespace gold.