1 // dynobj.cc -- dynamic object support for gold
17 // Return the string to use in a DT_NEEDED entry.
20 Dynobj::soname() const
22 if (!this->soname_
.empty())
23 return this->soname_
.c_str();
24 return this->name().c_str();
27 // Class Sized_dynobj.
29 template<int size
, bool big_endian
>
30 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
31 const std::string
& name
,
32 Input_file
* input_file
,
34 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
35 : Dynobj(name
, input_file
, offset
),
42 template<int size
, bool big_endian
>
44 Sized_dynobj
<size
, big_endian
>::setup(
45 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
47 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
48 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
49 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
51 const unsigned int shnum
= this->elf_file_
.shnum();
52 this->set_shnum(shnum
);
55 // Find the SHT_DYNSYM section and the various version sections, and
56 // the dynamic section, given the section headers.
58 template<int size
, bool big_endian
>
60 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
61 const unsigned char* pshdrs
,
62 unsigned int* pdynsym_shndx
,
63 unsigned int* pversym_shndx
,
64 unsigned int* pverdef_shndx
,
65 unsigned int* pverneed_shndx
,
66 unsigned int* pdynamic_shndx
)
71 *pverneed_shndx
= -1U;
72 *pdynamic_shndx
= -1U;
74 const unsigned int shnum
= this->shnum();
75 const unsigned char* p
= pshdrs
;
76 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
78 typename
This::Shdr
shdr(p
);
81 switch (shdr
.get_sh_type())
83 case elfcpp::SHT_DYNSYM
:
86 case elfcpp::SHT_GNU_versym
:
89 case elfcpp::SHT_GNU_verdef
:
92 case elfcpp::SHT_GNU_verneed
:
95 case elfcpp::SHT_DYNAMIC
:
109 _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
110 program_name
, this->name().c_str(), shdr
.get_sh_type(),
119 // Read the contents of section SHNDX. PSHDRS points to the section
120 // headers. TYPE is the expected section type. LINK is the expected
121 // section link. Store the data in *VIEW and *VIEW_SIZE. The
122 // section's sh_info field is stored in *VIEW_INFO.
124 template<int size
, bool big_endian
>
126 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
127 const unsigned char* pshdrs
,
133 unsigned int* view_info
)
143 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
145 gold_assert(shdr
.get_sh_type() == type
);
147 if (shdr
.get_sh_link() != link
)
150 _("%s: %s: unexpected link in section %u header: %u != %u\n"),
151 program_name
, this->name().c_str(), shndx
,
152 shdr
.get_sh_link(), link
);
156 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size());
157 *view_size
= shdr
.get_sh_size();
158 *view_info
= shdr
.get_sh_info();
161 // Set the soname field if this shared object has a DT_SONAME tag.
162 // PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
163 // index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
164 // STRTAB_SIZE are the section index and contents of a string table
165 // which may be the one associated with the SHT_DYNAMIC section.
167 template<int size
, bool big_endian
>
169 Sized_dynobj
<size
, big_endian
>::set_soname(const unsigned char* pshdrs
,
170 unsigned int dynamic_shndx
,
171 unsigned int strtab_shndx
,
172 const unsigned char* strtabu
,
175 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
176 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
178 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
179 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
182 const unsigned int link
= dynamicshdr
.get_sh_link();
183 if (link
!= strtab_shndx
)
185 if (link
>= this->shnum())
188 _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
189 program_name
, this->name().c_str(),
190 dynamic_shndx
, link
);
194 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
195 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
198 _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
199 program_name
, this->name().c_str(),
200 dynamic_shndx
, link
);
204 strtab_size
= strtabshdr
.get_sh_size();
205 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
);
208 for (const unsigned char* p
= pdynamic
;
209 p
< pdynamic
+ dynamic_size
;
212 typename
This::Dyn
dyn(p
);
214 if (dyn
.get_d_tag() == elfcpp::DT_SONAME
)
216 off_t val
= dyn
.get_d_val();
217 if (val
>= strtab_size
)
220 _("%s: %s: DT_SONAME value out of range: "
222 program_name
, this->name().c_str(),
223 static_cast<long long>(val
),
224 static_cast<long long>(strtab_size
));
228 const char* strtab
= reinterpret_cast<const char*>(strtabu
);
229 this->set_soname_string(strtab
+ val
);
233 if (dyn
.get_d_tag() == elfcpp::DT_NULL
)
237 fprintf(stderr
, _("%s: %s: missing DT_NULL in dynamic segment\n"),
238 program_name
, this->name().c_str());
242 // Read the symbols and sections from a dynamic object. We read the
243 // dynamic symbols, not the normal symbols.
245 template<int size
, bool big_endian
>
247 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
249 this->read_section_data(&this->elf_file_
, sd
);
251 const unsigned char* const pshdrs
= sd
->section_headers
->data();
253 unsigned int dynsym_shndx
;
254 unsigned int versym_shndx
;
255 unsigned int verdef_shndx
;
256 unsigned int verneed_shndx
;
257 unsigned int dynamic_shndx
;
258 this->find_dynsym_sections(pshdrs
, &dynsym_shndx
, &versym_shndx
,
259 &verdef_shndx
, &verneed_shndx
, &dynamic_shndx
);
261 unsigned int strtab_shndx
= -1U;
263 if (dynsym_shndx
== -1U)
266 sd
->symbols_size
= 0;
267 sd
->symbol_names
= NULL
;
268 sd
->symbol_names_size
= 0;
272 // Get the dynamic symbols.
273 typename
This::Shdr
dynsymshdr(pshdrs
+ dynsym_shndx
* This::shdr_size
);
274 gold_assert(dynsymshdr
.get_sh_type() == elfcpp::SHT_DYNSYM
);
276 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
277 dynsymshdr
.get_sh_size());
278 sd
->symbols_size
= dynsymshdr
.get_sh_size();
280 // Get the symbol names.
281 strtab_shndx
= dynsymshdr
.get_sh_link();
282 if (strtab_shndx
>= this->shnum())
285 _("%s: %s: invalid dynamic symbol table name index: %u\n"),
286 program_name
, this->name().c_str(), strtab_shndx
);
289 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
290 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
293 _("%s: %s: dynamic symbol table name section "
294 "has wrong type: %u\n"),
295 program_name
, this->name().c_str(),
296 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
300 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
301 strtabshdr
.get_sh_size());
302 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
304 // Get the version information.
307 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
308 dynsym_shndx
, &sd
->versym
, &sd
->versym_size
,
311 // We require that the version definition and need section link
312 // to the same string table as the dynamic symbol table. This
313 // is not a technical requirement, but it always happens in
314 // practice. We could change this if necessary.
316 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
317 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
320 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
321 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
325 // Read the SHT_DYNAMIC section to find whether this shared object
326 // has a DT_SONAME tag. This doesn't really have anything to do
327 // with reading the symbols, but this is a convenient place to do
329 if (dynamic_shndx
!= -1U)
330 this->set_soname(pshdrs
, dynamic_shndx
, strtab_shndx
,
331 (sd
->symbol_names
== NULL
333 : sd
->symbol_names
->data()),
334 sd
->symbol_names_size
);
337 // Lay out the input sections for a dynamic object. We don't want to
338 // include sections from a dynamic object, so all that we actually do
339 // here is check for .gnu.warning sections.
341 template<int size
, bool big_endian
>
343 Sized_dynobj
<size
, big_endian
>::do_layout(const General_options
&,
344 Symbol_table
* symtab
,
346 Read_symbols_data
* sd
)
348 const unsigned int shnum
= this->shnum();
352 // Get the section headers.
353 const unsigned char* pshdrs
= sd
->section_headers
->data();
355 // Get the section names.
356 const unsigned char* pnamesu
= sd
->section_names
->data();
357 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
359 // Skip the first, dummy, section.
360 pshdrs
+= This::shdr_size
;
361 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
363 typename
This::Shdr
shdr(pshdrs
);
365 if (shdr
.get_sh_name() >= sd
->section_names_size
)
368 _("%s: %s: bad section name offset for section %u: %lu\n"),
369 program_name
, this->name().c_str(), i
,
370 static_cast<unsigned long>(shdr
.get_sh_name()));
374 const char* name
= pnames
+ shdr
.get_sh_name();
376 this->handle_gnu_warning_section(name
, i
, symtab
);
379 delete sd
->section_headers
;
380 sd
->section_headers
= NULL
;
381 delete sd
->section_names
;
382 sd
->section_names
= NULL
;
385 // Add an entry to the vector mapping version numbers to version
388 template<int size
, bool big_endian
>
390 Sized_dynobj
<size
, big_endian
>::set_version_map(
391 Version_map
* version_map
,
393 const char* name
) const
395 gold_assert(ndx
< version_map
->size());
396 if ((*version_map
)[ndx
] != NULL
)
398 fprintf(stderr
, _("%s: %s: duplicate definition for version %u\n"),
399 program_name
, this->name().c_str(), ndx
);
402 (*version_map
)[ndx
] = name
;
405 // Create a vector mapping version numbers to version strings.
407 template<int size
, bool big_endian
>
409 Sized_dynobj
<size
, big_endian
>::make_version_map(
410 Read_symbols_data
* sd
,
411 Version_map
* version_map
) const
413 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
416 // First find the largest version index.
417 unsigned int maxver
= 0;
419 if (sd
->verdef
!= NULL
)
421 const unsigned char* pverdef
= sd
->verdef
->data();
422 off_t verdef_size
= sd
->verdef_size
;
423 const unsigned int count
= sd
->verdef_info
;
425 const unsigned char* p
= pverdef
;
426 for (unsigned int i
= 0; i
< count
; ++i
)
428 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
430 const unsigned int vd_ndx
= verdef
.get_vd_ndx();
432 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
438 const unsigned int vd_next
= verdef
.get_vd_next();
439 if ((p
- pverdef
) + vd_next
>= verdef_size
)
442 _("%s: %s: verdef vd_next field out of range: %u\n"),
443 program_name
, this->name().c_str(), vd_next
);
451 if (sd
->verneed
!= NULL
)
453 const unsigned char* pverneed
= sd
->verneed
->data();
454 off_t verneed_size
= sd
->verneed_size
;
455 const unsigned int count
= sd
->verneed_info
;
457 const unsigned char* p
= pverneed
;
458 for (unsigned int i
= 0; i
< count
; ++i
)
460 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
462 const unsigned int vn_aux
= verneed
.get_vn_aux();
463 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
466 _("%s: %s: verneed vn_aux field out of range: %u\n"),
467 program_name
, this->name().c_str(), vn_aux
);
471 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
472 const unsigned char* pvna
= p
+ vn_aux
;
473 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
475 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
477 const unsigned int vna_other
= vernaux
.get_vna_other();
478 if (vna_other
> maxver
)
481 const unsigned int vna_next
= vernaux
.get_vna_next();
482 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
485 _("%s: %s: verneed vna_next field "
486 "out of range: %u\n"),
487 program_name
, this->name().c_str(), vna_next
);
494 const unsigned int vn_next
= verneed
.get_vn_next();
495 if ((p
- pverneed
) + vn_next
>= verneed_size
)
498 _("%s: %s: verneed vn_next field out of range: %u\n"),
499 program_name
, this->name().c_str(), vn_next
);
507 // Now MAXVER is the largest version index we have seen.
509 version_map
->resize(maxver
+ 1);
511 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
512 off_t names_size
= sd
->symbol_names_size
;
514 if (sd
->verdef
!= NULL
)
516 const unsigned char* pverdef
= sd
->verdef
->data();
517 off_t verdef_size
= sd
->verdef_size
;
518 const unsigned int count
= sd
->verdef_info
;
520 const unsigned char* p
= pverdef
;
521 for (unsigned int i
= 0; i
< count
; ++i
)
523 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
525 const unsigned int vd_cnt
= verdef
.get_vd_cnt();
528 fprintf(stderr
, _("%s: %s: verdef vd_cnt field too small: %u\n"),
529 program_name
, this->name().c_str(), vd_cnt
);
533 const unsigned int vd_aux
= verdef
.get_vd_aux();
534 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
537 _("%s: %s: verdef vd_aux field out of range: %u\n"),
538 program_name
, this->name().c_str(), vd_aux
);
542 const unsigned char* pvda
= p
+ vd_aux
;
543 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
545 const unsigned int vda_name
= verdaux
.get_vda_name();
546 if (vda_name
>= names_size
)
549 _("%s: %s: verdaux vda_name field out of range: %u\n"),
550 program_name
, this->name().c_str(), vda_name
);
554 this->set_version_map(version_map
, verdef
.get_vd_ndx(),
557 const unsigned int vd_next
= verdef
.get_vd_next();
558 if ((p
- pverdef
) + vd_next
>= verdef_size
)
561 _("%s: %s: verdef vd_next field out of range: %u\n"),
562 program_name
, this->name().c_str(), vd_next
);
570 if (sd
->verneed
!= NULL
)
572 const unsigned char* pverneed
= sd
->verneed
->data();
573 const unsigned int count
= sd
->verneed_info
;
575 const unsigned char* p
= pverneed
;
576 for (unsigned int i
= 0; i
< count
; ++i
)
578 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
580 const unsigned int vn_aux
= verneed
.get_vn_aux();
581 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
582 const unsigned char* pvna
= p
+ vn_aux
;
583 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
585 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
587 const unsigned int vna_name
= vernaux
.get_vna_name();
588 if (vna_name
>= names_size
)
591 _("%s: %s: vernaux vna_name field "
592 "out of range: %u\n"),
593 program_name
, this->name().c_str(), vna_name
);
597 this->set_version_map(version_map
, vernaux
.get_vna_other(),
600 pvna
+= vernaux
.get_vna_next();
603 p
+= verneed
.get_vn_next();
608 // Add the dynamic symbols to the symbol table.
610 template<int size
, bool big_endian
>
612 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
613 Read_symbols_data
* sd
)
615 if (sd
->symbols
== NULL
)
617 gold_assert(sd
->symbol_names
== NULL
);
618 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
619 && sd
->verneed
== NULL
);
623 const int sym_size
= This::sym_size
;
624 const size_t symcount
= sd
->symbols_size
/ sym_size
;
625 if (symcount
* sym_size
!= sd
->symbols_size
)
628 _("%s: %s: size of dynamic symbols is not "
629 "multiple of symbol size\n"),
630 program_name
, this->name().c_str());
634 Version_map version_map
;
635 this->make_version_map(sd
, &version_map
);
637 const char* sym_names
=
638 reinterpret_cast<const char*>(sd
->symbol_names
->data());
639 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
640 sym_names
, sd
->symbol_names_size
,
643 : sd
->versym
->data()),
649 delete sd
->symbol_names
;
650 sd
->symbol_names
= NULL
;
651 if (sd
->versym
!= NULL
)
656 if (sd
->verdef
!= NULL
)
661 if (sd
->verneed
!= NULL
)
668 // Given a vector of hash codes, compute the number of hash buckets to
672 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
673 bool for_gnu_hash_table
)
675 // FIXME: Implement optional hash table optimization.
677 // Array used to determine the number of hash table buckets to use
678 // based on the number of symbols there are. If there are fewer
679 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
680 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
681 // use more than 32771 buckets. This is straight from the old GNU
683 static const unsigned int buckets
[] =
685 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
688 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
690 unsigned int symcount
= hashcodes
.size();
691 unsigned int ret
= 1;
692 for (int i
= 0; i
< buckets_count
; ++i
)
694 if (symcount
< buckets
[i
])
699 if (for_gnu_hash_table
&& ret
< 2)
705 // The standard ELF hash function. This hash function must not
706 // change, as the dynamic linker uses it also.
709 Dynobj::elf_hash(const char* name
)
711 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
714 while ((c
= *nameu
++) != '\0')
717 uint32_t g
= h
& 0xf0000000;
721 // The ELF ABI says h &= ~g, but using xor is equivalent in
722 // this case (since g was set from h) and may save one
730 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
731 // DYNSYMS is a vector with all the global dynamic symbols.
732 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
736 Dynobj::create_elf_hash_table(const Target
* target
,
737 const std::vector
<Symbol
*>& dynsyms
,
738 unsigned int local_dynsym_count
,
739 unsigned char** pphash
,
740 unsigned int* phashlen
)
742 unsigned int dynsym_count
= dynsyms
.size();
744 // Get the hash values for all the symbols.
745 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
746 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
747 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
749 const unsigned int bucketcount
=
750 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
752 std::vector
<uint32_t> bucket(bucketcount
);
753 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
755 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
757 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
758 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
759 chain
[dynsym_index
] = bucket
[bucketpos
];
760 bucket
[bucketpos
] = dynsym_index
;
763 unsigned int hashlen
= ((2
768 unsigned char* phash
= new unsigned char[hashlen
];
770 if (target
->is_big_endian())
771 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
, hashlen
);
773 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
, hashlen
);
779 // Fill in an ELF hash table.
781 template<bool big_endian
>
783 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
784 const std::vector
<uint32_t>& chain
,
785 unsigned char* phash
,
786 unsigned int hashlen
)
788 unsigned char* p
= phash
;
790 const unsigned int bucketcount
= bucket
.size();
791 const unsigned int chaincount
= chain
.size();
793 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
795 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
798 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
800 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
804 for (unsigned int i
= 0; i
< chaincount
; ++i
)
806 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
810 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
813 // The hash function used for the GNU hash table. This hash function
814 // must not change, as the dynamic linker uses it also.
817 Dynobj::gnu_hash(const char* name
)
819 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
822 while ((c
= *nameu
++) != '\0')
823 h
= (h
<< 5) + h
+ c
;
827 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
828 // tables are an extension to ELF which are recognized by the GNU
829 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
830 // TARGET is the target. DYNSYMS is a vector with all the global
831 // symbols which will be going into the dynamic symbol table.
832 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
836 Dynobj::create_gnu_hash_table(const Target
* target
,
837 const std::vector
<Symbol
*>& dynsyms
,
838 unsigned int local_dynsym_count
,
839 unsigned char** pphash
,
840 unsigned int* phashlen
)
842 const unsigned int count
= dynsyms
.size();
844 // Sort the dynamic symbols into two vectors. Symbols which we do
845 // not want to put into the hash table we store into
846 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
847 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
848 // and records the hash codes.
850 std::vector
<Symbol
*> unhashed_dynsyms
;
851 unhashed_dynsyms
.reserve(count
);
853 std::vector
<Symbol
*> hashed_dynsyms
;
854 hashed_dynsyms
.reserve(count
);
856 std::vector
<uint32_t> dynsym_hashvals
;
857 dynsym_hashvals
.reserve(count
);
859 for (unsigned int i
= 0; i
< count
; ++i
)
861 Symbol
* sym
= dynsyms
[i
];
863 // FIXME: Should put on unhashed_dynsyms if the symbol is
865 if (sym
->is_undefined())
866 unhashed_dynsyms
.push_back(sym
);
869 hashed_dynsyms
.push_back(sym
);
870 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
874 // Put the unhashed symbols at the start of the global portion of
875 // the dynamic symbol table.
876 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
877 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
878 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
880 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
881 ++unhashed_dynsym_index
;
884 // For the actual data generation we call out to a templatized
886 int size
= target
->get_size();
887 bool big_endian
= target
->is_big_endian();
891 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
893 unhashed_dynsym_index
,
897 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
899 unhashed_dynsym_index
,
906 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
908 unhashed_dynsym_index
,
912 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
914 unhashed_dynsym_index
,
922 // Create the actual data for a GNU hash table. This is just a copy
923 // of the code from the old GNU linker.
925 template<int size
, bool big_endian
>
927 Dynobj::sized_create_gnu_hash_table(
928 const std::vector
<Symbol
*>& hashed_dynsyms
,
929 const std::vector
<uint32_t>& dynsym_hashvals
,
930 unsigned int unhashed_dynsym_count
,
931 unsigned char** pphash
,
932 unsigned int* phashlen
)
934 if (hashed_dynsyms
.empty())
936 // Special case for the empty hash table.
937 unsigned int hashlen
= 5 * 4 + size
/ 8;
938 unsigned char* phash
= new unsigned char[hashlen
];
940 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
941 // Symbol index above unhashed symbols.
942 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
943 // One word for bitmask.
944 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
945 // Only bloom filter.
946 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
948 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
949 // No hashes in only bucket.
950 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
958 const unsigned int bucketcount
=
959 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
961 const unsigned int nsyms
= hashed_dynsyms
.size();
963 uint32_t maskbitslog2
= 1;
964 uint32_t x
= nsyms
>> 1;
970 if (maskbitslog2
< 3)
972 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
982 if (maskbitslog2
== 5)
986 uint32_t mask
= (1U << shift1
) - 1U;
987 uint32_t shift2
= maskbitslog2
;
988 uint32_t maskbits
= 1U << maskbitslog2
;
989 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
991 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
992 std::vector
<Word
> bitmask(maskwords
);
993 std::vector
<uint32_t> counts(bucketcount
);
994 std::vector
<uint32_t> indx(bucketcount
);
995 uint32_t symindx
= unhashed_dynsym_count
;
997 // Count the number of times each hash bucket is used.
998 for (unsigned int i
= 0; i
< nsyms
; ++i
)
999 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1001 unsigned int cnt
= symindx
;
1002 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1008 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1009 hashlen
+= maskbits
/ 8;
1010 unsigned char* phash
= new unsigned char[hashlen
];
1012 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1013 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1014 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1015 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1017 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1018 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1021 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1023 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1027 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1029 Symbol
* sym
= hashed_dynsyms
[i
];
1030 uint32_t hashval
= dynsym_hashvals
[i
];
1032 unsigned int bucket
= hashval
% bucketcount
;
1033 unsigned int val
= ((hashval
>> shift1
)
1034 & ((maskbits
>> shift1
) - 1));
1035 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1036 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1037 val
= hashval
& ~ 1U;
1038 if (counts
[bucket
] == 1)
1040 // Last element terminates the chain.
1043 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1047 sym
->set_dynsym_index(indx
[bucket
]);
1052 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1054 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1058 *phashlen
= hashlen
;
1062 // Instantiate the templates we need. We could use the configure
1063 // script to restrict this to only the ones for implemented targets.
1066 class Sized_dynobj
<32, false>;
1069 class Sized_dynobj
<32, true>;
1072 class Sized_dynobj
<64, false>;
1075 class Sized_dynobj
<64, true>;
1077 } // End namespace gold.