1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007, 2008 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"
39 // Sets up the default soname_ to use, in the (rare) cases we never
40 // see a DT_SONAME entry.
42 Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
43 : Object(name, input_file, true, offset),
45 unknown_needed_(UNKNOWN_NEEDED_UNSET)
47 // This will be overridden by a DT_SONAME entry, hopefully. But if
48 // we never see a DT_SONAME entry, our rule is to use the dynamic
49 // object's filename. The only exception is when the dynamic object
50 // is part of an archive (so the filename is the archive's
51 // filename). In that case, we use just the dynobj's name-in-archive.
52 this->soname_ = this->input_file()->found_name();
53 if (this->offset() != 0)
55 std::string::size_type open_paren = this->name().find('(');
56 std::string::size_type close_paren = this->name().find(')');
57 if (open_paren != std::string::npos && close_paren != std::string::npos)
59 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
60 this->soname_ = this->name().substr(open_paren + 1,
61 close_paren - (open_paren + 1));
66 // Class Sized_dynobj.
68 template<int size, bool big_endian>
69 Sized_dynobj<size, big_endian>::Sized_dynobj(
70 const std::string& name,
71 Input_file* input_file,
73 const elfcpp::Ehdr<size, big_endian>& ehdr)
74 : Dynobj(name, input_file, offset),
81 template<int size, bool big_endian>
83 Sized_dynobj<size, big_endian>::setup(
84 const elfcpp::Ehdr<size, big_endian>& ehdr)
86 this->set_target(ehdr.get_e_machine(), size, big_endian,
87 ehdr.get_e_ident()[elfcpp::EI_OSABI],
88 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
90 const unsigned int shnum = this->elf_file_.shnum();
91 this->set_shnum(shnum);
94 // Find the SHT_DYNSYM section and the various version sections, and
95 // the dynamic section, given the section headers.
97 template<int size, bool big_endian>
99 Sized_dynobj<size, big_endian>::find_dynsym_sections(
100 const unsigned char* pshdrs,
101 unsigned int* pdynsym_shndx,
102 unsigned int* pversym_shndx,
103 unsigned int* pverdef_shndx,
104 unsigned int* pverneed_shndx,
105 unsigned int* pdynamic_shndx)
107 *pdynsym_shndx = -1U;
108 *pversym_shndx = -1U;
109 *pverdef_shndx = -1U;
110 *pverneed_shndx = -1U;
111 *pdynamic_shndx = -1U;
113 const unsigned int shnum = this->shnum();
114 const unsigned char* p = pshdrs;
115 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
117 typename This::Shdr shdr(p);
120 switch (shdr.get_sh_type())
122 case elfcpp::SHT_DYNSYM:
125 case elfcpp::SHT_GNU_versym:
128 case elfcpp::SHT_GNU_verdef:
131 case elfcpp::SHT_GNU_verneed:
134 case elfcpp::SHT_DYNAMIC:
146 this->error(_("unexpected duplicate type %u section: %u, %u"),
147 shdr.get_sh_type(), *pi, i);
153 // Read the contents of section SHNDX. PSHDRS points to the section
154 // headers. TYPE is the expected section type. LINK is the expected
155 // section link. Store the data in *VIEW and *VIEW_SIZE. The
156 // section's sh_info field is stored in *VIEW_INFO.
158 template<int size, bool big_endian>
160 Sized_dynobj<size, big_endian>::read_dynsym_section(
161 const unsigned char* pshdrs,
166 section_size_type* view_size,
167 unsigned int* view_info)
177 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
179 gold_assert(shdr.get_sh_type() == type);
181 if (shdr.get_sh_link() != link)
182 this->error(_("unexpected link in section %u header: %u != %u"),
183 shndx, shdr.get_sh_link(), link);
185 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
187 *view_size = convert_to_section_size_type(shdr.get_sh_size());
188 *view_info = shdr.get_sh_info();
191 // Read the dynamic tags. Set the soname field if this shared object
192 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
193 // the section headers. DYNAMIC_SHNDX is the section index of the
194 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
195 // section index and contents of a string table which may be the one
196 // associated with the SHT_DYNAMIC section.
198 template<int size, bool big_endian>
200 Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
201 unsigned int dynamic_shndx,
202 unsigned int strtab_shndx,
203 const unsigned char* strtabu,
206 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
207 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
209 const off_t dynamic_size = dynamicshdr.get_sh_size();
210 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
211 dynamic_size, true, false);
213 const unsigned int link = dynamicshdr.get_sh_link();
214 if (link != strtab_shndx)
216 if (link >= this->shnum())
218 this->error(_("DYNAMIC section %u link out of range: %u"),
219 dynamic_shndx, link);
223 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
224 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
226 this->error(_("DYNAMIC section %u link %u is not a strtab"),
227 dynamic_shndx, link);
231 strtab_size = strtabshdr.get_sh_size();
232 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false,
236 const char* const strtab = reinterpret_cast<const char*>(strtabu);
238 for (const unsigned char* p = pdynamic;
239 p < pdynamic + dynamic_size;
242 typename This::Dyn dyn(p);
244 switch (dyn.get_d_tag())
246 case elfcpp::DT_NULL:
247 // We should always see DT_NULL at the end of the dynamic
251 case elfcpp::DT_SONAME:
253 off_t val = dyn.get_d_val();
254 if (val >= strtab_size)
255 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
256 static_cast<long long>(val),
257 static_cast<long long>(strtab_size));
259 this->set_soname_string(strtab + val);
263 case elfcpp::DT_NEEDED:
265 off_t val = dyn.get_d_val();
266 if (val >= strtab_size)
267 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
268 static_cast<long long>(val),
269 static_cast<long long>(strtab_size));
271 this->add_needed(strtab + val);
280 this->error(_("missing DT_NULL in dynamic segment"));
283 // Read the symbols and sections from a dynamic object. We read the
284 // dynamic symbols, not the normal symbols.
286 template<int size, bool big_endian>
288 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
290 this->read_section_data(&this->elf_file_, sd);
292 const unsigned char* const pshdrs = sd->section_headers->data();
294 unsigned int dynsym_shndx;
295 unsigned int versym_shndx;
296 unsigned int verdef_shndx;
297 unsigned int verneed_shndx;
298 unsigned int dynamic_shndx;
299 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
300 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
302 unsigned int strtab_shndx = -1U;
305 sd->symbols_size = 0;
306 sd->external_symbols_offset = 0;
307 sd->symbol_names = NULL;
308 sd->symbol_names_size = 0;
310 if (dynsym_shndx != -1U)
312 // Get the dynamic symbols.
313 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
314 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
316 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
317 dynsymshdr.get_sh_size(), true,
320 convert_to_section_size_type(dynsymshdr.get_sh_size());
322 // Get the symbol names.
323 strtab_shndx = dynsymshdr.get_sh_link();
324 if (strtab_shndx >= this->shnum())
326 this->error(_("invalid dynamic symbol table name index: %u"),
330 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
331 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
333 this->error(_("dynamic symbol table name section "
334 "has wrong type: %u"),
335 static_cast<unsigned int>(strtabshdr.get_sh_type()));
339 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
340 strtabshdr.get_sh_size(),
342 sd->symbol_names_size =
343 convert_to_section_size_type(strtabshdr.get_sh_size());
345 // Get the version information.
348 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
349 dynsym_shndx, &sd->versym, &sd->versym_size,
352 // We require that the version definition and need section link
353 // to the same string table as the dynamic symbol table. This
354 // is not a technical requirement, but it always happens in
355 // practice. We could change this if necessary.
357 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
358 strtab_shndx, &sd->verdef, &sd->verdef_size,
361 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
362 strtab_shndx, &sd->verneed, &sd->verneed_size,
366 // Read the SHT_DYNAMIC section to find whether this shared object
367 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
368 // doesn't really have anything to do with reading the symbols, but
369 // this is a convenient place to do it.
370 if (dynamic_shndx != -1U)
371 this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
372 (sd->symbol_names == NULL
374 : sd->symbol_names->data()),
375 sd->symbol_names_size);
378 // Lay out the input sections for a dynamic object. We don't want to
379 // include sections from a dynamic object, so all that we actually do
380 // here is check for .gnu.warning sections.
382 template<int size, bool big_endian>
384 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
386 Read_symbols_data* sd)
388 const unsigned int shnum = this->shnum();
392 // Get the section headers.
393 const unsigned char* pshdrs = sd->section_headers->data();
395 // Get the section names.
396 const unsigned char* pnamesu = sd->section_names->data();
397 const char* pnames = reinterpret_cast<const char*>(pnamesu);
399 // Skip the first, dummy, section.
400 pshdrs += This::shdr_size;
401 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
403 typename This::Shdr shdr(pshdrs);
405 if (shdr.get_sh_name() >= sd->section_names_size)
407 this->error(_("bad section name offset for section %u: %lu"),
408 i, static_cast<unsigned long>(shdr.get_sh_name()));
412 const char* name = pnames + shdr.get_sh_name();
414 this->handle_gnu_warning_section(name, i, symtab);
417 delete sd->section_headers;
418 sd->section_headers = NULL;
419 delete sd->section_names;
420 sd->section_names = NULL;
423 // Add an entry to the vector mapping version numbers to version
426 template<int size, bool big_endian>
428 Sized_dynobj<size, big_endian>::set_version_map(
429 Version_map* version_map,
431 const char* name) const
433 if (ndx >= version_map->size())
434 version_map->resize(ndx + 1);
435 if ((*version_map)[ndx] != NULL)
436 this->error(_("duplicate definition for version %u"), ndx);
437 (*version_map)[ndx] = name;
440 // Add mappings for the version definitions to VERSION_MAP.
442 template<int size, bool big_endian>
444 Sized_dynobj<size, big_endian>::make_verdef_map(
445 Read_symbols_data* sd,
446 Version_map* version_map) const
448 if (sd->verdef == NULL)
451 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
452 section_size_type names_size = sd->symbol_names_size;
454 const unsigned char* pverdef = sd->verdef->data();
455 section_size_type verdef_size = sd->verdef_size;
456 const unsigned int count = sd->verdef_info;
458 const unsigned char* p = pverdef;
459 for (unsigned int i = 0; i < count; ++i)
461 elfcpp::Verdef<size, big_endian> verdef(p);
463 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
465 this->error(_("unexpected verdef version %u"),
466 verdef.get_vd_version());
470 const section_size_type vd_ndx = verdef.get_vd_ndx();
472 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
475 // The first Verdaux holds the name of this version. Subsequent
476 // ones are versions that this one depends upon, which we don't
478 const section_size_type vd_cnt = verdef.get_vd_cnt();
481 this->error(_("verdef vd_cnt field too small: %u"),
482 static_cast<unsigned int>(vd_cnt));
486 const section_size_type vd_aux = verdef.get_vd_aux();
487 if ((p - pverdef) + vd_aux >= verdef_size)
489 this->error(_("verdef vd_aux field out of range: %u"),
490 static_cast<unsigned int>(vd_aux));
494 const unsigned char* pvda = p + vd_aux;
495 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
497 const section_size_type vda_name = verdaux.get_vda_name();
498 if (vda_name >= names_size)
500 this->error(_("verdaux vda_name field out of range: %u"),
501 static_cast<unsigned int>(vda_name));
505 this->set_version_map(version_map, vd_ndx, names + vda_name);
507 const section_size_type vd_next = verdef.get_vd_next();
508 if ((p - pverdef) + vd_next >= verdef_size)
510 this->error(_("verdef vd_next field out of range: %u"),
511 static_cast<unsigned int>(vd_next));
519 // Add mappings for the required versions to VERSION_MAP.
521 template<int size, bool big_endian>
523 Sized_dynobj<size, big_endian>::make_verneed_map(
524 Read_symbols_data* sd,
525 Version_map* version_map) const
527 if (sd->verneed == NULL)
530 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
531 section_size_type names_size = sd->symbol_names_size;
533 const unsigned char* pverneed = sd->verneed->data();
534 const section_size_type verneed_size = sd->verneed_size;
535 const unsigned int count = sd->verneed_info;
537 const unsigned char* p = pverneed;
538 for (unsigned int i = 0; i < count; ++i)
540 elfcpp::Verneed<size, big_endian> verneed(p);
542 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
544 this->error(_("unexpected verneed version %u"),
545 verneed.get_vn_version());
549 const section_size_type vn_aux = verneed.get_vn_aux();
551 if ((p - pverneed) + vn_aux >= verneed_size)
553 this->error(_("verneed vn_aux field out of range: %u"),
554 static_cast<unsigned int>(vn_aux));
558 const unsigned int vn_cnt = verneed.get_vn_cnt();
559 const unsigned char* pvna = p + vn_aux;
560 for (unsigned int j = 0; j < vn_cnt; ++j)
562 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
564 const unsigned int vna_name = vernaux.get_vna_name();
565 if (vna_name >= names_size)
567 this->error(_("vernaux vna_name field out of range: %u"),
568 static_cast<unsigned int>(vna_name));
572 this->set_version_map(version_map, vernaux.get_vna_other(),
575 const section_size_type vna_next = vernaux.get_vna_next();
576 if ((pvna - pverneed) + vna_next >= verneed_size)
578 this->error(_("verneed vna_next field out of range: %u"),
579 static_cast<unsigned int>(vna_next));
586 const section_size_type vn_next = verneed.get_vn_next();
587 if ((p - pverneed) + vn_next >= verneed_size)
589 this->error(_("verneed vn_next field out of range: %u"),
590 static_cast<unsigned int>(vn_next));
598 // Create a vector mapping version numbers to version strings.
600 template<int size, bool big_endian>
602 Sized_dynobj<size, big_endian>::make_version_map(
603 Read_symbols_data* sd,
604 Version_map* version_map) const
606 if (sd->verdef == NULL && sd->verneed == NULL)
609 // A guess at the maximum version number we will see. If this is
610 // wrong we will be less efficient but still correct.
611 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
613 this->make_verdef_map(sd, version_map);
614 this->make_verneed_map(sd, version_map);
617 // Add the dynamic symbols to the symbol table.
619 template<int size, bool big_endian>
621 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
622 Read_symbols_data* sd)
624 if (sd->symbols == NULL)
626 gold_assert(sd->symbol_names == NULL);
627 gold_assert(sd->versym == NULL && sd->verdef == NULL
628 && sd->verneed == NULL);
632 const int sym_size = This::sym_size;
633 const size_t symcount = sd->symbols_size / sym_size;
634 gold_assert(sd->external_symbols_offset == 0);
635 if (symcount * sym_size != sd->symbols_size)
637 this->error(_("size of dynamic symbols is not multiple of symbol size"));
641 Version_map version_map;
642 this->make_version_map(sd, &version_map);
644 const char* sym_names =
645 reinterpret_cast<const char*>(sd->symbol_names->data());
646 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
647 sym_names, sd->symbol_names_size,
650 : sd->versym->data()),
656 delete sd->symbol_names;
657 sd->symbol_names = NULL;
658 if (sd->versym != NULL)
663 if (sd->verdef != NULL)
668 if (sd->verneed != NULL)
674 // This is normally the last time we will read any data from this
676 this->clear_view_cache_marks();
679 // Given a vector of hash codes, compute the number of hash buckets to
683 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
684 bool for_gnu_hash_table)
686 // FIXME: Implement optional hash table optimization.
688 // Array used to determine the number of hash table buckets to use
689 // based on the number of symbols there are. If there are fewer
690 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
691 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
692 // use more than 262147 buckets. This is straight from the old GNU
694 static const unsigned int buckets[] =
696 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
697 16411, 32771, 65537, 131101, 262147
699 const int buckets_count = sizeof buckets / sizeof buckets[0];
701 unsigned int symcount = hashcodes.size();
702 unsigned int ret = 1;
703 const double full_fraction
704 = 1.0 - parameters->options().hash_bucket_empty_fraction();
705 for (int i = 0; i < buckets_count; ++i)
707 if (symcount < buckets[i] * full_fraction)
712 if (for_gnu_hash_table && ret < 2)
718 // The standard ELF hash function. This hash function must not
719 // change, as the dynamic linker uses it also.
722 Dynobj::elf_hash(const char* name)
724 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
727 while ((c = *nameu++) != '\0')
730 uint32_t g = h & 0xf0000000;
734 // The ELF ABI says h &= ~g, but using xor is equivalent in
735 // this case (since g was set from h) and may save one
743 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
744 // DYNSYMS is a vector with all the global dynamic symbols.
745 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
749 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
750 unsigned int local_dynsym_count,
751 unsigned char** pphash,
752 unsigned int* phashlen)
754 unsigned int dynsym_count = dynsyms.size();
756 // Get the hash values for all the symbols.
757 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
758 for (unsigned int i = 0; i < dynsym_count; ++i)
759 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
761 const unsigned int bucketcount =
762 Dynobj::compute_bucket_count(dynsym_hashvals, false);
764 std::vector<uint32_t> bucket(bucketcount);
765 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
767 for (unsigned int i = 0; i < dynsym_count; ++i)
769 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
770 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
771 chain[dynsym_index] = bucket[bucketpos];
772 bucket[bucketpos] = dynsym_index;
775 unsigned int hashlen = ((2
780 unsigned char* phash = new unsigned char[hashlen];
782 if (parameters->target().is_big_endian())
784 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
785 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
793 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
794 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
805 // Fill in an ELF hash table.
807 template<bool big_endian>
809 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
810 const std::vector<uint32_t>& chain,
811 unsigned char* phash,
812 unsigned int hashlen)
814 unsigned char* p = phash;
816 const unsigned int bucketcount = bucket.size();
817 const unsigned int chaincount = chain.size();
819 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
821 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
824 for (unsigned int i = 0; i < bucketcount; ++i)
826 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
830 for (unsigned int i = 0; i < chaincount; ++i)
832 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
836 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
839 // The hash function used for the GNU hash table. This hash function
840 // must not change, as the dynamic linker uses it also.
843 Dynobj::gnu_hash(const char* name)
845 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
848 while ((c = *nameu++) != '\0')
849 h = (h << 5) + h + c;
853 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
854 // tables are an extension to ELF which are recognized by the GNU
855 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
856 // TARGET is the target. DYNSYMS is a vector with all the global
857 // symbols which will be going into the dynamic symbol table.
858 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
862 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
863 unsigned int local_dynsym_count,
864 unsigned char** pphash,
865 unsigned int* phashlen)
867 const unsigned int count = dynsyms.size();
869 // Sort the dynamic symbols into two vectors. Symbols which we do
870 // not want to put into the hash table we store into
871 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
872 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
873 // and records the hash codes.
875 std::vector<Symbol*> unhashed_dynsyms;
876 unhashed_dynsyms.reserve(count);
878 std::vector<Symbol*> hashed_dynsyms;
879 hashed_dynsyms.reserve(count);
881 std::vector<uint32_t> dynsym_hashvals;
882 dynsym_hashvals.reserve(count);
884 for (unsigned int i = 0; i < count; ++i)
886 Symbol* sym = dynsyms[i];
888 // FIXME: Should put on unhashed_dynsyms if the symbol is
890 if (sym->is_undefined())
891 unhashed_dynsyms.push_back(sym);
894 hashed_dynsyms.push_back(sym);
895 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
899 // Put the unhashed symbols at the start of the global portion of
900 // the dynamic symbol table.
901 const unsigned int unhashed_count = unhashed_dynsyms.size();
902 unsigned int unhashed_dynsym_index = local_dynsym_count;
903 for (unsigned int i = 0; i < unhashed_count; ++i)
905 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
906 ++unhashed_dynsym_index;
909 // For the actual data generation we call out to a templatized
911 int size = parameters->target().get_size();
912 bool big_endian = parameters->target().is_big_endian();
917 #ifdef HAVE_TARGET_32_BIG
918 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
920 unhashed_dynsym_index,
929 #ifdef HAVE_TARGET_32_LITTLE
930 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
932 unhashed_dynsym_index,
944 #ifdef HAVE_TARGET_64_BIG
945 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
947 unhashed_dynsym_index,
956 #ifdef HAVE_TARGET_64_LITTLE
957 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
959 unhashed_dynsym_index,
971 // Create the actual data for a GNU hash table. This is just a copy
972 // of the code from the old GNU linker.
974 template<int size, bool big_endian>
976 Dynobj::sized_create_gnu_hash_table(
977 const std::vector<Symbol*>& hashed_dynsyms,
978 const std::vector<uint32_t>& dynsym_hashvals,
979 unsigned int unhashed_dynsym_count,
980 unsigned char** pphash,
981 unsigned int* phashlen)
983 if (hashed_dynsyms.empty())
985 // Special case for the empty hash table.
986 unsigned int hashlen = 5 * 4 + size / 8;
987 unsigned char* phash = new unsigned char[hashlen];
989 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
990 // Symbol index above unhashed symbols.
991 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
992 // One word for bitmask.
993 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
994 // Only bloom filter.
995 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
997 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
998 // No hashes in only bucket.
999 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
1001 *phashlen = hashlen;
1007 const unsigned int bucketcount =
1008 Dynobj::compute_bucket_count(dynsym_hashvals, true);
1010 const unsigned int nsyms = hashed_dynsyms.size();
1012 uint32_t maskbitslog2 = 1;
1013 uint32_t x = nsyms >> 1;
1019 if (maskbitslog2 < 3)
1021 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1031 if (maskbitslog2 == 5)
1035 uint32_t mask = (1U << shift1) - 1U;
1036 uint32_t shift2 = maskbitslog2;
1037 uint32_t maskbits = 1U << maskbitslog2;
1038 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1040 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1041 std::vector<Word> bitmask(maskwords);
1042 std::vector<uint32_t> counts(bucketcount);
1043 std::vector<uint32_t> indx(bucketcount);
1044 uint32_t symindx = unhashed_dynsym_count;
1046 // Count the number of times each hash bucket is used.
1047 for (unsigned int i = 0; i < nsyms; ++i)
1048 ++counts[dynsym_hashvals[i] % bucketcount];
1050 unsigned int cnt = symindx;
1051 for (unsigned int i = 0; i < bucketcount; ++i)
1057 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1058 hashlen += maskbits / 8;
1059 unsigned char* phash = new unsigned char[hashlen];
1061 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1062 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1063 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1064 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1066 unsigned char* p = phash + 16 + maskbits / 8;
1067 for (unsigned int i = 0; i < bucketcount; ++i)
1070 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1072 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1076 for (unsigned int i = 0; i < nsyms; ++i)
1078 Symbol* sym = hashed_dynsyms[i];
1079 uint32_t hashval = dynsym_hashvals[i];
1081 unsigned int bucket = hashval % bucketcount;
1082 unsigned int val = ((hashval >> shift1)
1083 & ((maskbits >> shift1) - 1));
1084 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1085 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1086 val = hashval & ~ 1U;
1087 if (counts[bucket] == 1)
1089 // Last element terminates the chain.
1092 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1096 sym->set_dynsym_index(indx[bucket]);
1101 for (unsigned int i = 0; i < maskwords; ++i)
1103 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1107 *phashlen = hashlen;
1113 // Write this definition to a buffer for the output section.
1115 template<int size, bool big_endian>
1117 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const
1119 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1120 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1122 elfcpp::Verdef_write<size, big_endian> vd(pb);
1123 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1124 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1125 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1126 vd.set_vd_ndx(this->index());
1127 vd.set_vd_cnt(1 + this->deps_.size());
1128 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1129 vd.set_vd_aux(verdef_size);
1130 vd.set_vd_next(is_last
1132 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1135 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1136 vda.set_vda_name(dynpool->get_offset(this->name()));
1137 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1140 Deps::const_iterator p;
1142 for (p = this->deps_.begin(), i = 0;
1143 p != this->deps_.end();
1146 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1147 vda.set_vda_name(dynpool->get_offset(*p));
1148 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1159 for (Need_versions::iterator p = this->need_versions_.begin();
1160 p != this->need_versions_.end();
1165 // Add a new version to this file reference.
1168 Verneed::add_name(const char* name)
1170 Verneed_version* vv = new Verneed_version(name);
1171 this->need_versions_.push_back(vv);
1175 // Set the version indexes starting at INDEX.
1178 Verneed::finalize(unsigned int index)
1180 for (Need_versions::iterator p = this->need_versions_.begin();
1181 p != this->need_versions_.end();
1184 (*p)->set_index(index);
1190 // Write this list of referenced versions to a buffer for the output
1193 template<int size, bool big_endian>
1195 Verneed::write(const Stringpool* dynpool, bool is_last,
1196 unsigned char* pb) const
1198 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1199 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1201 elfcpp::Verneed_write<size, big_endian> vn(pb);
1202 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1203 vn.set_vn_cnt(this->need_versions_.size());
1204 vn.set_vn_file(dynpool->get_offset(this->filename()));
1205 vn.set_vn_aux(verneed_size);
1206 vn.set_vn_next(is_last
1208 : verneed_size + this->need_versions_.size() * vernaux_size);
1211 Need_versions::const_iterator p;
1213 for (p = this->need_versions_.begin(), i = 0;
1214 p != this->need_versions_.end();
1217 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1218 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1219 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1220 vna.set_vna_flags(0);
1221 vna.set_vna_other((*p)->index());
1222 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1223 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1232 // Versions methods.
1234 Versions::Versions(const Version_script_info& version_script,
1235 Stringpool* dynpool)
1236 : defs_(), needs_(), version_table_(),
1237 is_finalized_(false), version_script_(version_script)
1239 // We always need a base version, so define that first. Nothing
1240 // explicitly declares itself as part of base, so it doesn't need to
1241 // be in version_table_.
1242 // FIXME: Should use soname here when creating a shared object. Is
1243 // this fixme still valid? It looks like it's doing the right thing
1245 if (parameters->options().shared())
1247 const char* name = dynpool->add(parameters->options().output_file_name(),
1249 Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
1251 this->defs_.push_back(vdbase);
1254 if (!this->version_script_.empty())
1256 // Parse the version script, and insert each declared version into
1257 // defs_ and version_table_.
1258 std::vector<std::string> versions = this->version_script_.get_versions();
1259 for (size_t k = 0; k < versions.size(); ++k)
1261 Stringpool::Key version_key;
1262 const char* version = dynpool->add(versions[k].c_str(),
1263 true, &version_key);
1264 Verdef* const vd = new Verdef(
1266 this->version_script_.get_dependencies(version),
1267 false, false, false);
1268 this->defs_.push_back(vd);
1269 Key key(version_key, 0);
1270 this->version_table_.insert(std::make_pair(key, vd));
1275 Versions::~Versions()
1277 for (Defs::iterator p = this->defs_.begin();
1278 p != this->defs_.end();
1282 for (Needs::iterator p = this->needs_.begin();
1283 p != this->needs_.end();
1288 // Return the dynamic object which a symbol refers to.
1291 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1292 const Symbol* sym) const
1294 if (sym->is_copied_from_dynobj())
1295 return symtab->get_copy_source(sym);
1298 Object* object = sym->object();
1299 gold_assert(object->is_dynamic());
1300 return static_cast<Dynobj*>(object);
1304 // Record version information for a symbol going into the dynamic
1308 Versions::record_version(const Symbol_table* symtab,
1309 Stringpool* dynpool, const Symbol* sym)
1311 gold_assert(!this->is_finalized_);
1312 gold_assert(sym->version() != NULL);
1314 Stringpool::Key version_key;
1315 const char* version = dynpool->add(sym->version(), false, &version_key);
1317 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1319 if (parameters->options().shared())
1320 this->add_def(sym, version, version_key);
1324 // This is a version reference.
1325 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1326 this->add_need(dynpool, dynobj->soname(), version, version_key);
1330 // We've found a symbol SYM defined in version VERSION.
1333 Versions::add_def(const Symbol* sym, const char* version,
1334 Stringpool::Key version_key)
1336 Key k(version_key, 0);
1337 Version_base* const vbnull = NULL;
1338 std::pair<Version_table::iterator, bool> ins =
1339 this->version_table_.insert(std::make_pair(k, vbnull));
1343 // We already have an entry for this version.
1344 Version_base* vb = ins.first->second;
1346 // We have now seen a symbol in this version, so it is not
1348 gold_assert(vb != NULL);
1353 // If we are creating a shared object, it is an error to
1354 // find a definition of a symbol with a version which is not
1355 // in the version script.
1356 if (parameters->options().shared())
1358 gold_error(_("symbol %s has undefined version %s"),
1359 sym->demangled_name().c_str(), version);
1363 // When creating a regular executable, automatically define
1365 Verdef* vd = new Verdef(version, std::vector<std::string>(),
1366 false, false, false);
1367 this->defs_.push_back(vd);
1368 ins.first->second = vd;
1372 // Add a reference to version NAME in file FILENAME.
1375 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1376 Stringpool::Key name_key)
1378 Stringpool::Key filename_key;
1379 filename = dynpool->add(filename, true, &filename_key);
1381 Key k(name_key, filename_key);
1382 Version_base* const vbnull = NULL;
1383 std::pair<Version_table::iterator, bool> ins =
1384 this->version_table_.insert(std::make_pair(k, vbnull));
1388 // We already have an entry for this filename/version.
1392 // See whether we already have this filename. We don't expect many
1393 // version references, so we just do a linear search. This could be
1394 // replaced by a hash table.
1396 for (Needs::iterator p = this->needs_.begin();
1397 p != this->needs_.end();
1400 if ((*p)->filename() == filename)
1409 // We have a new filename.
1410 vn = new Verneed(filename);
1411 this->needs_.push_back(vn);
1414 ins.first->second = vn->add_name(name);
1417 // Set the version indexes. Create a new dynamic version symbol for
1418 // each new version definition.
1421 Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
1422 std::vector<Symbol*>* syms)
1424 gold_assert(!this->is_finalized_);
1426 unsigned int vi = 1;
1428 for (Defs::iterator p = this->defs_.begin();
1429 p != this->defs_.end();
1432 (*p)->set_index(vi);
1435 // Create a version symbol if necessary.
1436 if (!(*p)->is_symbol_created())
1438 Symbol* vsym = symtab->define_as_constant((*p)->name(),
1442 elfcpp::STV_DEFAULT, 0,
1444 vsym->set_needs_dynsym_entry();
1445 vsym->set_dynsym_index(dynsym_index);
1447 syms->push_back(vsym);
1448 // The name is already in the dynamic pool.
1452 // Index 1 is used for global symbols.
1455 gold_assert(this->defs_.empty());
1459 for (Needs::iterator p = this->needs_.begin();
1460 p != this->needs_.end();
1462 vi = (*p)->finalize(vi);
1464 this->is_finalized_ = true;
1466 return dynsym_index;
1469 // Return the version index to use for a symbol. This does two hash
1470 // table lookups: one in DYNPOOL and one in this->version_table_.
1471 // Another approach alternative would be store a pointer in SYM, which
1472 // would increase the size of the symbol table. Or perhaps we could
1473 // use a hash table from dynamic symbol pointer values to Version_base
1477 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1478 const Symbol* sym) const
1480 Stringpool::Key version_key;
1481 const char* version = dynpool->find(sym->version(), &version_key);
1482 gold_assert(version != NULL);
1485 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1487 if (!parameters->options().shared())
1488 return elfcpp::VER_NDX_GLOBAL;
1489 k = Key(version_key, 0);
1493 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1495 Stringpool::Key filename_key;
1496 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1497 gold_assert(filename != NULL);
1499 k = Key(version_key, filename_key);
1502 Version_table::const_iterator p = this->version_table_.find(k);
1503 gold_assert(p != this->version_table_.end());
1505 return p->second->index();
1508 // Return an allocated buffer holding the contents of the symbol
1511 template<int size, bool big_endian>
1513 Versions::symbol_section_contents(const Symbol_table* symtab,
1514 const Stringpool* dynpool,
1515 unsigned int local_symcount,
1516 const std::vector<Symbol*>& syms,
1518 unsigned int* psize) const
1520 gold_assert(this->is_finalized_);
1522 unsigned int sz = (local_symcount + syms.size()) * 2;
1523 unsigned char* pbuf = new unsigned char[sz];
1525 for (unsigned int i = 0; i < local_symcount; ++i)
1526 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1527 elfcpp::VER_NDX_LOCAL);
1529 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1533 unsigned int version_index;
1534 const char* version = (*p)->version();
1535 if (version == NULL)
1536 version_index = elfcpp::VER_NDX_GLOBAL;
1538 version_index = this->version_index(symtab, dynpool, *p);
1539 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1541 if ((*p)->version() != NULL && !(*p)->is_default())
1542 version_index |= elfcpp::VERSYM_HIDDEN;
1543 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1551 // Return an allocated buffer holding the contents of the version
1552 // definition section.
1554 template<int size, bool big_endian>
1556 Versions::def_section_contents(const Stringpool* dynpool,
1557 unsigned char** pp, unsigned int* psize,
1558 unsigned int* pentries) const
1560 gold_assert(this->is_finalized_);
1561 gold_assert(!this->defs_.empty());
1563 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1564 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1566 unsigned int sz = 0;
1567 for (Defs::const_iterator p = this->defs_.begin();
1568 p != this->defs_.end();
1571 sz += verdef_size + verdaux_size;
1572 sz += (*p)->count_dependencies() * verdaux_size;
1575 unsigned char* pbuf = new unsigned char[sz];
1577 unsigned char* pb = pbuf;
1578 Defs::const_iterator p;
1580 for (p = this->defs_.begin(), i = 0;
1581 p != this->defs_.end();
1583 pb = (*p)->write<size, big_endian>(dynpool,
1584 i + 1 >= this->defs_.size(),
1587 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1591 *pentries = this->defs_.size();
1594 // Return an allocated buffer holding the contents of the version
1595 // reference section.
1597 template<int size, bool big_endian>
1599 Versions::need_section_contents(const Stringpool* dynpool,
1600 unsigned char** pp, unsigned int *psize,
1601 unsigned int *pentries) const
1603 gold_assert(this->is_finalized_);
1604 gold_assert(!this->needs_.empty());
1606 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1607 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1609 unsigned int sz = 0;
1610 for (Needs::const_iterator p = this->needs_.begin();
1611 p != this->needs_.end();
1615 sz += (*p)->count_versions() * vernaux_size;
1618 unsigned char* pbuf = new unsigned char[sz];
1620 unsigned char* pb = pbuf;
1621 Needs::const_iterator p;
1623 for (p = this->needs_.begin(), i = 0;
1624 p != this->needs_.end();
1626 pb = (*p)->write<size, big_endian>(dynpool,
1627 i + 1 >= this->needs_.size(),
1630 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1634 *pentries = this->needs_.size();
1637 // Instantiate the templates we need. We could use the configure
1638 // script to restrict this to only the ones for implemented targets.
1640 #ifdef HAVE_TARGET_32_LITTLE
1642 class Sized_dynobj<32, false>;
1645 #ifdef HAVE_TARGET_32_BIG
1647 class Sized_dynobj<32, true>;
1650 #ifdef HAVE_TARGET_64_LITTLE
1652 class Sized_dynobj<64, false>;
1655 #ifdef HAVE_TARGET_64_BIG
1657 class Sized_dynobj<64, true>;
1660 #ifdef HAVE_TARGET_32_LITTLE
1663 Versions::symbol_section_contents<32, false>(
1664 const Symbol_table*,
1667 const std::vector<Symbol*>&,
1669 unsigned int*) const;
1672 #ifdef HAVE_TARGET_32_BIG
1675 Versions::symbol_section_contents<32, true>(
1676 const Symbol_table*,
1679 const std::vector<Symbol*>&,
1681 unsigned int*) const;
1684 #ifdef HAVE_TARGET_64_LITTLE
1687 Versions::symbol_section_contents<64, false>(
1688 const Symbol_table*,
1691 const std::vector<Symbol*>&,
1693 unsigned int*) const;
1696 #ifdef HAVE_TARGET_64_BIG
1699 Versions::symbol_section_contents<64, true>(
1700 const Symbol_table*,
1703 const std::vector<Symbol*>&,
1705 unsigned int*) const;
1708 #ifdef HAVE_TARGET_32_LITTLE
1711 Versions::def_section_contents<32, false>(
1715 unsigned int*) const;
1718 #ifdef HAVE_TARGET_32_BIG
1721 Versions::def_section_contents<32, true>(
1725 unsigned int*) const;
1728 #ifdef HAVE_TARGET_64_LITTLE
1731 Versions::def_section_contents<64, false>(
1735 unsigned int*) const;
1738 #ifdef HAVE_TARGET_64_BIG
1741 Versions::def_section_contents<64, true>(
1745 unsigned int*) const;
1748 #ifdef HAVE_TARGET_32_LITTLE
1751 Versions::need_section_contents<32, false>(
1755 unsigned int*) const;
1758 #ifdef HAVE_TARGET_32_BIG
1761 Versions::need_section_contents<32, true>(
1765 unsigned int*) const;
1768 #ifdef HAVE_TARGET_64_LITTLE
1771 Versions::need_section_contents<64, false>(
1775 unsigned int*) const;
1778 #ifdef HAVE_TARGET_64_BIG
1781 Versions::need_section_contents<64, true>(
1785 unsigned int*) const;
1788 } // End namespace gold.