1 // dynobj.cc -- dynamic object support for gold
9 #include "parameters.h"
18 // Return the string to use in a DT_NEEDED entry.
21 Dynobj::soname() const
23 if (!this->soname_
.empty())
24 return this->soname_
.c_str();
25 return this->name().c_str();
28 // Class Sized_dynobj.
30 template<int size
, bool big_endian
>
31 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
32 const std::string
& name
,
33 Input_file
* input_file
,
35 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
36 : Dynobj(name
, input_file
, offset
),
43 template<int size
, bool big_endian
>
45 Sized_dynobj
<size
, big_endian
>::setup(
46 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
48 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
49 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
50 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
52 const unsigned int shnum
= this->elf_file_
.shnum();
53 this->set_shnum(shnum
);
56 // Find the SHT_DYNSYM section and the various version sections, and
57 // the dynamic section, given the section headers.
59 template<int size
, bool big_endian
>
61 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
62 const unsigned char* pshdrs
,
63 unsigned int* pdynsym_shndx
,
64 unsigned int* pversym_shndx
,
65 unsigned int* pverdef_shndx
,
66 unsigned int* pverneed_shndx
,
67 unsigned int* pdynamic_shndx
)
72 *pverneed_shndx
= -1U;
73 *pdynamic_shndx
= -1U;
75 const unsigned int shnum
= this->shnum();
76 const unsigned char* p
= pshdrs
;
77 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
79 typename
This::Shdr
shdr(p
);
82 switch (shdr
.get_sh_type())
84 case elfcpp::SHT_DYNSYM
:
87 case elfcpp::SHT_GNU_versym
:
90 case elfcpp::SHT_GNU_verdef
:
93 case elfcpp::SHT_GNU_verneed
:
96 case elfcpp::SHT_DYNAMIC
:
110 _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
111 program_name
, this->name().c_str(), shdr
.get_sh_type(),
120 // Read the contents of section SHNDX. PSHDRS points to the section
121 // headers. TYPE is the expected section type. LINK is the expected
122 // section link. Store the data in *VIEW and *VIEW_SIZE. The
123 // section's sh_info field is stored in *VIEW_INFO.
125 template<int size
, bool big_endian
>
127 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
128 const unsigned char* pshdrs
,
134 unsigned int* view_info
)
144 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
146 gold_assert(shdr
.get_sh_type() == type
);
148 if (shdr
.get_sh_link() != link
)
151 _("%s: %s: unexpected link in section %u header: %u != %u\n"),
152 program_name
, this->name().c_str(), shndx
,
153 shdr
.get_sh_link(), link
);
157 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size());
158 *view_size
= shdr
.get_sh_size();
159 *view_info
= shdr
.get_sh_info();
162 // Set the soname field if this shared object has a DT_SONAME tag.
163 // PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
164 // index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
165 // STRTAB_SIZE are the section index and contents of a string table
166 // which may be the one associated with the SHT_DYNAMIC section.
168 template<int size
, bool big_endian
>
170 Sized_dynobj
<size
, big_endian
>::set_soname(const unsigned char* pshdrs
,
171 unsigned int dynamic_shndx
,
172 unsigned int strtab_shndx
,
173 const unsigned char* strtabu
,
176 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
177 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
179 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
180 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
183 const unsigned int link
= dynamicshdr
.get_sh_link();
184 if (link
!= strtab_shndx
)
186 if (link
>= this->shnum())
189 _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
190 program_name
, this->name().c_str(),
191 dynamic_shndx
, link
);
195 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
196 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
199 _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
200 program_name
, this->name().c_str(),
201 dynamic_shndx
, link
);
205 strtab_size
= strtabshdr
.get_sh_size();
206 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
);
209 for (const unsigned char* p
= pdynamic
;
210 p
< pdynamic
+ dynamic_size
;
213 typename
This::Dyn
dyn(p
);
215 if (dyn
.get_d_tag() == elfcpp::DT_SONAME
)
217 off_t val
= dyn
.get_d_val();
218 if (val
>= strtab_size
)
221 _("%s: %s: DT_SONAME value out of range: "
223 program_name
, this->name().c_str(),
224 static_cast<long long>(val
),
225 static_cast<long long>(strtab_size
));
229 const char* strtab
= reinterpret_cast<const char*>(strtabu
);
230 this->set_soname_string(strtab
+ val
);
234 if (dyn
.get_d_tag() == elfcpp::DT_NULL
)
238 fprintf(stderr
, _("%s: %s: missing DT_NULL in dynamic segment\n"),
239 program_name
, this->name().c_str());
243 // Read the symbols and sections from a dynamic object. We read the
244 // dynamic symbols, not the normal symbols.
246 template<int size
, bool big_endian
>
248 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
250 this->read_section_data(&this->elf_file_
, sd
);
252 const unsigned char* const pshdrs
= sd
->section_headers
->data();
254 unsigned int dynsym_shndx
;
255 unsigned int versym_shndx
;
256 unsigned int verdef_shndx
;
257 unsigned int verneed_shndx
;
258 unsigned int dynamic_shndx
;
259 this->find_dynsym_sections(pshdrs
, &dynsym_shndx
, &versym_shndx
,
260 &verdef_shndx
, &verneed_shndx
, &dynamic_shndx
);
262 unsigned int strtab_shndx
= -1U;
264 if (dynsym_shndx
== -1U)
267 sd
->symbols_size
= 0;
268 sd
->symbol_names
= NULL
;
269 sd
->symbol_names_size
= 0;
273 // Get the dynamic symbols.
274 typename
This::Shdr
dynsymshdr(pshdrs
+ dynsym_shndx
* This::shdr_size
);
275 gold_assert(dynsymshdr
.get_sh_type() == elfcpp::SHT_DYNSYM
);
277 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
278 dynsymshdr
.get_sh_size());
279 sd
->symbols_size
= dynsymshdr
.get_sh_size();
281 // Get the symbol names.
282 strtab_shndx
= dynsymshdr
.get_sh_link();
283 if (strtab_shndx
>= this->shnum())
286 _("%s: %s: invalid dynamic symbol table name index: %u\n"),
287 program_name
, this->name().c_str(), strtab_shndx
);
290 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
291 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
294 _("%s: %s: dynamic symbol table name section "
295 "has wrong type: %u\n"),
296 program_name
, this->name().c_str(),
297 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
301 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
302 strtabshdr
.get_sh_size());
303 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
305 // Get the version information.
308 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
309 dynsym_shndx
, &sd
->versym
, &sd
->versym_size
,
312 // We require that the version definition and need section link
313 // to the same string table as the dynamic symbol table. This
314 // is not a technical requirement, but it always happens in
315 // practice. We could change this if necessary.
317 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
318 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
321 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
322 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
326 // Read the SHT_DYNAMIC section to find whether this shared object
327 // has a DT_SONAME tag. This doesn't really have anything to do
328 // with reading the symbols, but this is a convenient place to do
330 if (dynamic_shndx
!= -1U)
331 this->set_soname(pshdrs
, dynamic_shndx
, strtab_shndx
,
332 (sd
->symbol_names
== NULL
334 : sd
->symbol_names
->data()),
335 sd
->symbol_names_size
);
338 // Lay out the input sections for a dynamic object. We don't want to
339 // include sections from a dynamic object, so all that we actually do
340 // here is check for .gnu.warning sections.
342 template<int size
, bool big_endian
>
344 Sized_dynobj
<size
, big_endian
>::do_layout(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 if (ndx
>= version_map
->size())
396 version_map
->resize(ndx
+ 1);
397 if ((*version_map
)[ndx
] != NULL
)
399 fprintf(stderr
, _("%s: %s: duplicate definition for version %u\n"),
400 program_name
, this->name().c_str(), ndx
);
403 (*version_map
)[ndx
] = name
;
406 // Add mappings for the version definitions to VERSION_MAP.
408 template<int size
, bool big_endian
>
410 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
411 Read_symbols_data
* sd
,
412 Version_map
* version_map
) const
414 if (sd
->verdef
== NULL
)
417 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
418 off_t names_size
= sd
->symbol_names_size
;
420 const unsigned char* pverdef
= sd
->verdef
->data();
421 off_t verdef_size
= sd
->verdef_size
;
422 const unsigned int count
= sd
->verdef_info
;
424 const unsigned char* p
= pverdef
;
425 for (unsigned int i
= 0; i
< count
; ++i
)
427 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
429 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
431 fprintf(stderr
, _("%s: %s: unexpected verdef version %u\n"),
432 program_name
, this->name().c_str(), verdef
.get_vd_version());
436 const unsigned int vd_ndx
= verdef
.get_vd_ndx();
438 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
441 // The first Verdaux holds the name of this version. Subsequent
442 // ones are versions that this one depends upon, which we don't
444 const unsigned int vd_cnt
= verdef
.get_vd_cnt();
447 fprintf(stderr
, _("%s: %s: verdef vd_cnt field too small: %u\n"),
448 program_name
, this->name().c_str(), vd_cnt
);
452 const unsigned int vd_aux
= verdef
.get_vd_aux();
453 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
456 _("%s: %s: verdef vd_aux field out of range: %u\n"),
457 program_name
, this->name().c_str(), vd_aux
);
461 const unsigned char* pvda
= p
+ vd_aux
;
462 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
464 const unsigned int vda_name
= verdaux
.get_vda_name();
465 if (vda_name
>= names_size
)
468 _("%s: %s: verdaux vda_name field out of range: %u\n"),
469 program_name
, this->name().c_str(), vda_name
);
473 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
475 const unsigned int vd_next
= verdef
.get_vd_next();
476 if ((p
- pverdef
) + vd_next
>= verdef_size
)
479 _("%s: %s: verdef vd_next field out of range: %u\n"),
480 program_name
, this->name().c_str(), vd_next
);
488 // Add mappings for the required versions to VERSION_MAP.
490 template<int size
, bool big_endian
>
492 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
493 Read_symbols_data
* sd
,
494 Version_map
* version_map
) const
496 if (sd
->verneed
== NULL
)
499 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
500 off_t names_size
= sd
->symbol_names_size
;
502 const unsigned char* pverneed
= sd
->verneed
->data();
503 const off_t verneed_size
= sd
->verneed_size
;
504 const unsigned int count
= sd
->verneed_info
;
506 const unsigned char* p
= pverneed
;
507 for (unsigned int i
= 0; i
< count
; ++i
)
509 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
511 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
513 fprintf(stderr
, _("%s: %s: unexpected verneed version %u\n"),
514 program_name
, this->name().c_str(),
515 verneed
.get_vn_version());
519 const unsigned int vn_aux
= verneed
.get_vn_aux();
521 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
524 _("%s: %s: verneed vn_aux field out of range: %u\n"),
525 program_name
, this->name().c_str(), vn_aux
);
529 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
530 const unsigned char* pvna
= p
+ vn_aux
;
531 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
533 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
535 const unsigned int vna_name
= vernaux
.get_vna_name();
536 if (vna_name
>= names_size
)
539 _("%s: %s: vernaux vna_name field "
540 "out of range: %u\n"),
541 program_name
, this->name().c_str(), vna_name
);
545 this->set_version_map(version_map
, vernaux
.get_vna_other(),
548 const unsigned int vna_next
= vernaux
.get_vna_next();
549 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
552 _("%s: %s: verneed vna_next field "
553 "out of range: %u\n"),
554 program_name
, this->name().c_str(), vna_next
);
561 const unsigned int vn_next
= verneed
.get_vn_next();
562 if ((p
- pverneed
) + vn_next
>= verneed_size
)
565 _("%s: %s: verneed vn_next field out of range: %u\n"),
566 program_name
, this->name().c_str(), vn_next
);
574 // Create a vector mapping version numbers to version strings.
576 template<int size
, bool big_endian
>
578 Sized_dynobj
<size
, big_endian
>::make_version_map(
579 Read_symbols_data
* sd
,
580 Version_map
* version_map
) const
582 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
585 // A guess at the maximum version number we will see. If this is
586 // wrong we will be less efficient but still correct.
587 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
589 this->make_verdef_map(sd
, version_map
);
590 this->make_verneed_map(sd
, version_map
);
593 // Add the dynamic symbols to the symbol table.
595 template<int size
, bool big_endian
>
597 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
598 Read_symbols_data
* sd
)
600 if (sd
->symbols
== NULL
)
602 gold_assert(sd
->symbol_names
== NULL
);
603 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
604 && sd
->verneed
== NULL
);
608 const int sym_size
= This::sym_size
;
609 const size_t symcount
= sd
->symbols_size
/ sym_size
;
610 if (symcount
* sym_size
!= sd
->symbols_size
)
613 _("%s: %s: size of dynamic symbols is not "
614 "multiple of symbol size\n"),
615 program_name
, this->name().c_str());
619 Version_map version_map
;
620 this->make_version_map(sd
, &version_map
);
622 const char* sym_names
=
623 reinterpret_cast<const char*>(sd
->symbol_names
->data());
624 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
625 sym_names
, sd
->symbol_names_size
,
628 : sd
->versym
->data()),
634 delete sd
->symbol_names
;
635 sd
->symbol_names
= NULL
;
636 if (sd
->versym
!= NULL
)
641 if (sd
->verdef
!= NULL
)
646 if (sd
->verneed
!= NULL
)
653 // Given a vector of hash codes, compute the number of hash buckets to
657 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
658 bool for_gnu_hash_table
)
660 // FIXME: Implement optional hash table optimization.
662 // Array used to determine the number of hash table buckets to use
663 // based on the number of symbols there are. If there are fewer
664 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
665 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
666 // use more than 32771 buckets. This is straight from the old GNU
668 static const unsigned int buckets
[] =
670 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
673 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
675 unsigned int symcount
= hashcodes
.size();
676 unsigned int ret
= 1;
677 for (int i
= 0; i
< buckets_count
; ++i
)
679 if (symcount
< buckets
[i
])
684 if (for_gnu_hash_table
&& ret
< 2)
690 // The standard ELF hash function. This hash function must not
691 // change, as the dynamic linker uses it also.
694 Dynobj::elf_hash(const char* name
)
696 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
699 while ((c
= *nameu
++) != '\0')
702 uint32_t g
= h
& 0xf0000000;
706 // The ELF ABI says h &= ~g, but using xor is equivalent in
707 // this case (since g was set from h) and may save one
715 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
716 // DYNSYMS is a vector with all the global dynamic symbols.
717 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
721 Dynobj::create_elf_hash_table(const Target
* target
,
722 const std::vector
<Symbol
*>& dynsyms
,
723 unsigned int local_dynsym_count
,
724 unsigned char** pphash
,
725 unsigned int* phashlen
)
727 unsigned int dynsym_count
= dynsyms
.size();
729 // Get the hash values for all the symbols.
730 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
731 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
732 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
734 const unsigned int bucketcount
=
735 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
737 std::vector
<uint32_t> bucket(bucketcount
);
738 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
740 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
742 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
743 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
744 chain
[dynsym_index
] = bucket
[bucketpos
];
745 bucket
[bucketpos
] = dynsym_index
;
748 unsigned int hashlen
= ((2
753 unsigned char* phash
= new unsigned char[hashlen
];
755 if (target
->is_big_endian())
756 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
, hashlen
);
758 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
, hashlen
);
764 // Fill in an ELF hash table.
766 template<bool big_endian
>
768 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
769 const std::vector
<uint32_t>& chain
,
770 unsigned char* phash
,
771 unsigned int hashlen
)
773 unsigned char* p
= phash
;
775 const unsigned int bucketcount
= bucket
.size();
776 const unsigned int chaincount
= chain
.size();
778 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
780 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
783 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
785 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
789 for (unsigned int i
= 0; i
< chaincount
; ++i
)
791 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
795 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
798 // The hash function used for the GNU hash table. This hash function
799 // must not change, as the dynamic linker uses it also.
802 Dynobj::gnu_hash(const char* name
)
804 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
807 while ((c
= *nameu
++) != '\0')
808 h
= (h
<< 5) + h
+ c
;
812 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
813 // tables are an extension to ELF which are recognized by the GNU
814 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
815 // TARGET is the target. DYNSYMS is a vector with all the global
816 // symbols which will be going into the dynamic symbol table.
817 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
821 Dynobj::create_gnu_hash_table(const Target
* target
,
822 const std::vector
<Symbol
*>& dynsyms
,
823 unsigned int local_dynsym_count
,
824 unsigned char** pphash
,
825 unsigned int* phashlen
)
827 const unsigned int count
= dynsyms
.size();
829 // Sort the dynamic symbols into two vectors. Symbols which we do
830 // not want to put into the hash table we store into
831 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
832 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
833 // and records the hash codes.
835 std::vector
<Symbol
*> unhashed_dynsyms
;
836 unhashed_dynsyms
.reserve(count
);
838 std::vector
<Symbol
*> hashed_dynsyms
;
839 hashed_dynsyms
.reserve(count
);
841 std::vector
<uint32_t> dynsym_hashvals
;
842 dynsym_hashvals
.reserve(count
);
844 for (unsigned int i
= 0; i
< count
; ++i
)
846 Symbol
* sym
= dynsyms
[i
];
848 // FIXME: Should put on unhashed_dynsyms if the symbol is
850 if (sym
->is_undefined())
851 unhashed_dynsyms
.push_back(sym
);
854 hashed_dynsyms
.push_back(sym
);
855 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
859 // Put the unhashed symbols at the start of the global portion of
860 // the dynamic symbol table.
861 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
862 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
863 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
865 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
866 ++unhashed_dynsym_index
;
869 // For the actual data generation we call out to a templatized
871 int size
= target
->get_size();
872 bool big_endian
= target
->is_big_endian();
876 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
878 unhashed_dynsym_index
,
882 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
884 unhashed_dynsym_index
,
891 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
893 unhashed_dynsym_index
,
897 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
899 unhashed_dynsym_index
,
907 // Create the actual data for a GNU hash table. This is just a copy
908 // of the code from the old GNU linker.
910 template<int size
, bool big_endian
>
912 Dynobj::sized_create_gnu_hash_table(
913 const std::vector
<Symbol
*>& hashed_dynsyms
,
914 const std::vector
<uint32_t>& dynsym_hashvals
,
915 unsigned int unhashed_dynsym_count
,
916 unsigned char** pphash
,
917 unsigned int* phashlen
)
919 if (hashed_dynsyms
.empty())
921 // Special case for the empty hash table.
922 unsigned int hashlen
= 5 * 4 + size
/ 8;
923 unsigned char* phash
= new unsigned char[hashlen
];
925 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
926 // Symbol index above unhashed symbols.
927 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
928 // One word for bitmask.
929 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
930 // Only bloom filter.
931 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
933 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
934 // No hashes in only bucket.
935 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
943 const unsigned int bucketcount
=
944 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
946 const unsigned int nsyms
= hashed_dynsyms
.size();
948 uint32_t maskbitslog2
= 1;
949 uint32_t x
= nsyms
>> 1;
955 if (maskbitslog2
< 3)
957 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
967 if (maskbitslog2
== 5)
971 uint32_t mask
= (1U << shift1
) - 1U;
972 uint32_t shift2
= maskbitslog2
;
973 uint32_t maskbits
= 1U << maskbitslog2
;
974 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
976 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
977 std::vector
<Word
> bitmask(maskwords
);
978 std::vector
<uint32_t> counts(bucketcount
);
979 std::vector
<uint32_t> indx(bucketcount
);
980 uint32_t symindx
= unhashed_dynsym_count
;
982 // Count the number of times each hash bucket is used.
983 for (unsigned int i
= 0; i
< nsyms
; ++i
)
984 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
986 unsigned int cnt
= symindx
;
987 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
993 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
994 hashlen
+= maskbits
/ 8;
995 unsigned char* phash
= new unsigned char[hashlen
];
997 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
998 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
999 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1000 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1002 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1003 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1006 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1008 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1012 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1014 Symbol
* sym
= hashed_dynsyms
[i
];
1015 uint32_t hashval
= dynsym_hashvals
[i
];
1017 unsigned int bucket
= hashval
% bucketcount
;
1018 unsigned int val
= ((hashval
>> shift1
)
1019 & ((maskbits
>> shift1
) - 1));
1020 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1021 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1022 val
= hashval
& ~ 1U;
1023 if (counts
[bucket
] == 1)
1025 // Last element terminates the chain.
1028 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1032 sym
->set_dynsym_index(indx
[bucket
]);
1037 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1039 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1043 *phashlen
= hashlen
;
1049 // Write this definition to a buffer for the output section.
1051 template<int size
, bool big_endian
>
1053 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
1054 ACCEPT_SIZE_ENDIAN
) const
1056 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1057 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1059 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1060 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1061 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1062 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0));
1063 vd
.set_vd_ndx(this->index());
1064 vd
.set_vd_cnt(1 + this->deps_
.size());
1065 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1066 vd
.set_vd_aux(verdef_size
);
1067 vd
.set_vd_next(is_last
1069 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1072 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1073 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1074 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1077 Deps::const_iterator p
;
1079 for (p
= this->deps_
.begin(), i
= 0;
1080 p
!= this->deps_
.end();
1083 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1084 vda
.set_vda_name(dynpool
->get_offset(*p
));
1085 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1096 for (Need_versions::iterator p
= this->need_versions_
.begin();
1097 p
!= this->need_versions_
.end();
1102 // Add a new version to this file reference.
1105 Verneed::add_name(const char* name
)
1107 Verneed_version
* vv
= new Verneed_version(name
);
1108 this->need_versions_
.push_back(vv
);
1112 // Set the version indexes starting at INDEX.
1115 Verneed::finalize(unsigned int index
)
1117 for (Need_versions::iterator p
= this->need_versions_
.begin();
1118 p
!= this->need_versions_
.end();
1121 (*p
)->set_index(index
);
1127 // Write this list of referenced versions to a buffer for the output
1130 template<int size
, bool big_endian
>
1132 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1133 unsigned char* pb ACCEPT_SIZE_ENDIAN
) const
1135 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1136 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1138 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1139 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1140 vn
.set_vn_cnt(this->need_versions_
.size());
1141 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1142 vn
.set_vn_aux(verneed_size
);
1143 vn
.set_vn_next(is_last
1145 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1148 Need_versions::const_iterator p
;
1150 for (p
= this->need_versions_
.begin(), i
= 0;
1151 p
!= this->need_versions_
.end();
1154 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1155 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1156 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1157 vna
.set_vna_flags(0);
1158 vna
.set_vna_other((*p
)->index());
1159 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1160 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1169 // Versions methods.
1171 Versions::~Versions()
1173 for (Defs::iterator p
= this->defs_
.begin();
1174 p
!= this->defs_
.end();
1178 for (Needs::iterator p
= this->needs_
.begin();
1179 p
!= this->needs_
.end();
1184 // Record version information for a symbol going into the dynamic
1188 Versions::record_version(const General_options
* options
,
1189 Stringpool
* dynpool
, const Symbol
* sym
)
1191 gold_assert(!this->is_finalized_
);
1192 gold_assert(sym
->version() != NULL
);
1194 Stringpool::Key version_key
;
1195 const char* version
= dynpool
->add(sym
->version(), &version_key
);
1197 if (!sym
->is_from_dynobj())
1199 if (parameters
->output_is_shared())
1200 this->add_def(options
, sym
, version
, version_key
);
1204 // This is a version reference.
1206 Object
* object
= sym
->object();
1207 gold_assert(object
->is_dynamic());
1208 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1210 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1214 // We've found a symbol SYM defined in version VERSION.
1217 Versions::add_def(const General_options
* options
, const Symbol
* sym
,
1218 const char* version
, Stringpool::Key version_key
)
1220 Key
k(version_key
, 0);
1221 Version_base
* const vbnull
= NULL
;
1222 std::pair
<Version_table::iterator
, bool> ins
=
1223 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1227 // We already have an entry for this version.
1228 Version_base
* vb
= ins
.first
->second
;
1230 // We have now seen a symbol in this version, so it is not
1234 // FIXME: When we support version scripts, we will need to
1235 // check whether this symbol should be forced local.
1239 // If we are creating a shared object, it is an error to
1240 // find a definition of a symbol with a version which is not
1241 // in the version script.
1242 if (parameters
->output_is_shared())
1244 fprintf(stderr
, _("%s: symbol %s has undefined version %s\n"),
1245 program_name
, sym
->name(), version
);
1249 // If this is the first version we are defining, first define
1250 // the base version. FIXME: Should use soname here when
1251 // creating a shared object.
1252 Verdef
* vdbase
= new Verdef(options
->output_file_name(), true, false,
1254 this->defs_
.push_back(vdbase
);
1256 // When creating a regular executable, automatically define
1258 Verdef
* vd
= new Verdef(version
, false, false, false);
1259 this->defs_
.push_back(vd
);
1260 ins
.first
->second
= vd
;
1264 // Add a reference to version NAME in file FILENAME.
1267 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1268 Stringpool::Key name_key
)
1270 Stringpool::Key filename_key
;
1271 filename
= dynpool
->add(filename
, &filename_key
);
1273 Key
k(name_key
, filename_key
);
1274 Version_base
* const vbnull
= NULL
;
1275 std::pair
<Version_table::iterator
, bool> ins
=
1276 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1280 // We already have an entry for this filename/version.
1284 // See whether we already have this filename. We don't expect many
1285 // version references, so we just do a linear search. This could be
1286 // replaced by a hash table.
1288 for (Needs::iterator p
= this->needs_
.begin();
1289 p
!= this->needs_
.end();
1292 if ((*p
)->filename() == filename
)
1301 // We have a new filename.
1302 vn
= new Verneed(filename
);
1303 this->needs_
.push_back(vn
);
1306 ins
.first
->second
= vn
->add_name(name
);
1309 // Set the version indexes. Create a new dynamic version symbol for
1310 // each new version definition.
1313 Versions::finalize(const Target
* target
, Symbol_table
* symtab
,
1314 unsigned int dynsym_index
, std::vector
<Symbol
*>* syms
)
1316 gold_assert(!this->is_finalized_
);
1318 unsigned int vi
= 1;
1320 for (Defs::iterator p
= this->defs_
.begin();
1321 p
!= this->defs_
.end();
1324 (*p
)->set_index(vi
);
1327 // Create a version symbol if necessary.
1328 if (!(*p
)->is_symbol_created())
1330 Symbol
* vsym
= symtab
->define_as_constant(target
, (*p
)->name(),
1334 elfcpp::STV_DEFAULT
, 0,
1336 vsym
->set_needs_dynsym_entry();
1337 vsym
->set_dynsym_index(dynsym_index
);
1339 syms
->push_back(vsym
);
1340 // The name is already in the dynamic pool.
1344 // Index 1 is used for global symbols.
1347 gold_assert(this->defs_
.empty());
1351 for (Needs::iterator p
= this->needs_
.begin();
1352 p
!= this->needs_
.end();
1354 vi
= (*p
)->finalize(vi
);
1356 this->is_finalized_
= true;
1358 return dynsym_index
;
1361 // Return the version index to use for a symbol. This does two hash
1362 // table lookups: one in DYNPOOL and one in this->version_table_.
1363 // Another approach alternative would be store a pointer in SYM, which
1364 // would increase the size of the symbol table. Or perhaps we could
1365 // use a hash table from dynamic symbol pointer values to Version_base
1369 Versions::version_index(const Stringpool
* dynpool
, const Symbol
* sym
) const
1371 Stringpool::Key version_key
;
1372 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1373 gold_assert(version
!= NULL
);
1376 if (!sym
->is_from_dynobj())
1378 if (!parameters
->output_is_shared())
1379 return elfcpp::VER_NDX_GLOBAL
;
1380 k
= Key(version_key
, 0);
1384 Object
* object
= sym
->object();
1385 gold_assert(object
->is_dynamic());
1386 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1388 Stringpool::Key filename_key
;
1389 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1390 gold_assert(filename
!= NULL
);
1392 k
= Key(version_key
, filename_key
);
1395 Version_table::const_iterator p
= this->version_table_
.find(k
);
1396 gold_assert(p
!= this->version_table_
.end());
1398 return p
->second
->index();
1401 // Return an allocated buffer holding the contents of the symbol
1404 template<int size
, bool big_endian
>
1406 Versions::symbol_section_contents(const Stringpool
* dynpool
,
1407 unsigned int local_symcount
,
1408 const std::vector
<Symbol
*>& syms
,
1411 ACCEPT_SIZE_ENDIAN
) const
1413 gold_assert(this->is_finalized_
);
1415 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1416 unsigned char* pbuf
= new unsigned char[sz
];
1418 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1419 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1420 elfcpp::VER_NDX_LOCAL
);
1422 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1426 unsigned int version_index
;
1427 const char* version
= (*p
)->version();
1428 if (version
== NULL
)
1429 version_index
= elfcpp::VER_NDX_GLOBAL
;
1431 version_index
= this->version_index(dynpool
, *p
);
1432 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1440 // Return an allocated buffer holding the contents of the version
1441 // definition section.
1443 template<int size
, bool big_endian
>
1445 Versions::def_section_contents(const Stringpool
* dynpool
,
1446 unsigned char** pp
, unsigned int* psize
,
1447 unsigned int* pentries
1448 ACCEPT_SIZE_ENDIAN
) const
1450 gold_assert(this->is_finalized_
);
1451 gold_assert(!this->defs_
.empty());
1453 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1454 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1456 unsigned int sz
= 0;
1457 for (Defs::const_iterator p
= this->defs_
.begin();
1458 p
!= this->defs_
.end();
1461 sz
+= verdef_size
+ verdaux_size
;
1462 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1465 unsigned char* pbuf
= new unsigned char[sz
];
1467 unsigned char* pb
= pbuf
;
1468 Defs::const_iterator p
;
1470 for (p
= this->defs_
.begin(), i
= 0;
1471 p
!= this->defs_
.end();
1473 pb
= (*p
)->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1474 dynpool
, i
+ 1 >= this->defs_
.size(), pb
1475 SELECT_SIZE_ENDIAN(size
, big_endian
));
1477 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1481 *pentries
= this->defs_
.size();
1484 // Return an allocated buffer holding the contents of the version
1485 // reference section.
1487 template<int size
, bool big_endian
>
1489 Versions::need_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->needs_
.empty());
1497 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1498 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1500 unsigned int sz
= 0;
1501 for (Needs::const_iterator p
= this->needs_
.begin();
1502 p
!= this->needs_
.end();
1506 sz
+= (*p
)->count_versions() * vernaux_size
;
1509 unsigned char* pbuf
= new unsigned char[sz
];
1511 unsigned char* pb
= pbuf
;
1512 Needs::const_iterator p
;
1514 for (p
= this->needs_
.begin(), i
= 0;
1515 p
!= this->needs_
.end();
1517 pb
= (*p
)->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1518 dynpool
, i
+ 1 >= this->needs_
.size(), pb
1519 SELECT_SIZE_ENDIAN(size
, big_endian
));
1521 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1525 *pentries
= this->needs_
.size();
1528 // Instantiate the templates we need. We could use the configure
1529 // script to restrict this to only the ones for implemented targets.
1531 #ifdef HAVE_TARGET_32_LITTLE
1533 class Sized_dynobj
<32, false>;
1536 #ifdef HAVE_TARGET_32_BIG
1538 class Sized_dynobj
<32, true>;
1541 #ifdef HAVE_TARGET_64_LITTLE
1543 class Sized_dynobj
<64, false>;
1546 #ifdef HAVE_TARGET_64_BIG
1548 class Sized_dynobj
<64, true>;
1551 #ifdef HAVE_TARGET_32_LITTLE
1554 Versions::symbol_section_contents
<32, false>(
1557 const std::vector
<Symbol
*>&,
1560 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1563 #ifdef HAVE_TARGET_32_BIG
1566 Versions::symbol_section_contents
<32, true>(
1569 const std::vector
<Symbol
*>&,
1572 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1575 #ifdef HAVE_TARGET_64_LITTLE
1578 Versions::symbol_section_contents
<64, false>(
1581 const std::vector
<Symbol
*>&,
1584 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1587 #ifdef HAVE_TARGET_64_BIG
1590 Versions::symbol_section_contents
<64, true>(
1593 const std::vector
<Symbol
*>&,
1596 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1599 #ifdef HAVE_TARGET_32_LITTLE
1602 Versions::def_section_contents
<32, false>(
1607 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1610 #ifdef HAVE_TARGET_32_BIG
1613 Versions::def_section_contents
<32, true>(
1618 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1621 #ifdef HAVE_TARGET_64_LITTLE
1624 Versions::def_section_contents
<64, false>(
1629 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1632 #ifdef HAVE_TARGET_64_BIG
1635 Versions::def_section_contents
<64, true>(
1640 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1643 #ifdef HAVE_TARGET_32_LITTLE
1646 Versions::need_section_contents
<32, false>(
1651 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1654 #ifdef HAVE_TARGET_32_BIG
1657 Versions::need_section_contents
<32, true>(
1662 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1665 #ifdef HAVE_TARGET_64_LITTLE
1668 Versions::need_section_contents
<64, false>(
1673 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1676 #ifdef HAVE_TARGET_64_BIG
1679 Versions::need_section_contents
<64, true>(
1684 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1687 } // End namespace gold.