1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
38 // Return the string to use in a DT_NEEDED entry.
41 Dynobj::soname() const
43 if (!this->soname_
.empty())
44 return this->soname_
.c_str();
45 return this->input_file()->found_name().c_str();
48 // Class Sized_dynobj.
50 template<int size
, bool big_endian
>
51 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
52 const std::string
& name
,
53 Input_file
* input_file
,
55 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
56 : Dynobj(name
, input_file
, offset
),
63 template<int size
, bool big_endian
>
65 Sized_dynobj
<size
, big_endian
>::setup(
66 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
68 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
69 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
70 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
72 const unsigned int shnum
= this->elf_file_
.shnum();
73 this->set_shnum(shnum
);
76 // Find the SHT_DYNSYM section and the various version sections, and
77 // the dynamic section, given the section headers.
79 template<int size
, bool big_endian
>
81 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
82 const unsigned char* pshdrs
,
83 unsigned int* pdynsym_shndx
,
84 unsigned int* pversym_shndx
,
85 unsigned int* pverdef_shndx
,
86 unsigned int* pverneed_shndx
,
87 unsigned int* pdynamic_shndx
)
92 *pverneed_shndx
= -1U;
93 *pdynamic_shndx
= -1U;
95 const unsigned int shnum
= this->shnum();
96 const unsigned char* p
= pshdrs
;
97 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
99 typename
This::Shdr
shdr(p
);
102 switch (shdr
.get_sh_type())
104 case elfcpp::SHT_DYNSYM
:
107 case elfcpp::SHT_GNU_versym
:
110 case elfcpp::SHT_GNU_verdef
:
113 case elfcpp::SHT_GNU_verneed
:
116 case elfcpp::SHT_DYNAMIC
:
128 this->error(_("unexpected duplicate type %u section: %u, %u"),
129 shdr
.get_sh_type(), *pi
, i
);
135 // Read the contents of section SHNDX. PSHDRS points to the section
136 // headers. TYPE is the expected section type. LINK is the expected
137 // section link. Store the data in *VIEW and *VIEW_SIZE. The
138 // section's sh_info field is stored in *VIEW_INFO.
140 template<int size
, bool big_endian
>
142 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
143 const unsigned char* pshdrs
,
149 unsigned int* view_info
)
159 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
161 gold_assert(shdr
.get_sh_type() == type
);
163 if (shdr
.get_sh_link() != link
)
164 this->error(_("unexpected link in section %u header: %u != %u"),
165 shndx
, shdr
.get_sh_link(), link
);
167 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size(),
169 *view_size
= shdr
.get_sh_size();
170 *view_info
= shdr
.get_sh_info();
173 // Set the soname field if this shared object has a DT_SONAME tag.
174 // PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
175 // index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
176 // STRTAB_SIZE are the section index and contents of a string table
177 // which may be the one associated with the SHT_DYNAMIC section.
179 template<int size
, bool big_endian
>
181 Sized_dynobj
<size
, big_endian
>::set_soname(const unsigned char* pshdrs
,
182 unsigned int dynamic_shndx
,
183 unsigned int strtab_shndx
,
184 const unsigned char* strtabu
,
187 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
188 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
190 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
191 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
192 dynamic_size
, false);
194 const unsigned int link
= dynamicshdr
.get_sh_link();
195 if (link
!= strtab_shndx
)
197 if (link
>= this->shnum())
199 this->error(_("DYNAMIC section %u link out of range: %u"),
200 dynamic_shndx
, link
);
204 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
205 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
207 this->error(_("DYNAMIC section %u link %u is not a strtab"),
208 dynamic_shndx
, link
);
212 strtab_size
= strtabshdr
.get_sh_size();
213 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
, false);
216 for (const unsigned char* p
= pdynamic
;
217 p
< pdynamic
+ dynamic_size
;
220 typename
This::Dyn
dyn(p
);
222 if (dyn
.get_d_tag() == elfcpp::DT_SONAME
)
224 off_t val
= dyn
.get_d_val();
225 if (val
>= strtab_size
)
227 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
228 static_cast<long long>(val
),
229 static_cast<long long>(strtab_size
));
233 const char* strtab
= reinterpret_cast<const char*>(strtabu
);
234 this->set_soname_string(strtab
+ val
);
238 if (dyn
.get_d_tag() == elfcpp::DT_NULL
)
242 this->error(_("missing DT_NULL in dynamic segment"));
245 // Read the symbols and sections from a dynamic object. We read the
246 // dynamic symbols, not the normal symbols.
248 template<int size
, bool big_endian
>
250 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
252 this->read_section_data(&this->elf_file_
, sd
);
254 const unsigned char* const pshdrs
= sd
->section_headers
->data();
256 unsigned int dynsym_shndx
;
257 unsigned int versym_shndx
;
258 unsigned int verdef_shndx
;
259 unsigned int verneed_shndx
;
260 unsigned int dynamic_shndx
;
261 this->find_dynsym_sections(pshdrs
, &dynsym_shndx
, &versym_shndx
,
262 &verdef_shndx
, &verneed_shndx
, &dynamic_shndx
);
264 unsigned int strtab_shndx
= -1U;
267 sd
->symbols_size
= 0;
268 sd
->symbol_names
= NULL
;
269 sd
->symbol_names_size
= 0;
271 if (dynsym_shndx
!= -1U)
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(), false);
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())
285 this->error(_("invalid dynamic symbol table name index: %u"),
289 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
290 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
292 this->error(_("dynamic symbol table name section "
293 "has wrong type: %u"),
294 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
298 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
299 strtabshdr
.get_sh_size(),
301 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
303 // Get the version information.
306 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
307 dynsym_shndx
, &sd
->versym
, &sd
->versym_size
,
310 // We require that the version definition and need section link
311 // to the same string table as the dynamic symbol table. This
312 // is not a technical requirement, but it always happens in
313 // practice. We could change this if necessary.
315 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
316 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
319 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
320 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
324 // Read the SHT_DYNAMIC section to find whether this shared object
325 // has a DT_SONAME tag. This doesn't really have anything to do
326 // with reading the symbols, but this is a convenient place to do
328 if (dynamic_shndx
!= -1U)
329 this->set_soname(pshdrs
, dynamic_shndx
, strtab_shndx
,
330 (sd
->symbol_names
== NULL
332 : sd
->symbol_names
->data()),
333 sd
->symbol_names_size
);
336 // Lay out the input sections for a dynamic object. We don't want to
337 // include sections from a dynamic object, so all that we actually do
338 // here is check for .gnu.warning sections.
340 template<int size
, bool big_endian
>
342 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
344 Read_symbols_data
* sd
)
346 const unsigned int shnum
= this->shnum();
350 // Get the section headers.
351 const unsigned char* pshdrs
= sd
->section_headers
->data();
353 // Get the section names.
354 const unsigned char* pnamesu
= sd
->section_names
->data();
355 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
357 // Skip the first, dummy, section.
358 pshdrs
+= This::shdr_size
;
359 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
361 typename
This::Shdr
shdr(pshdrs
);
363 if (shdr
.get_sh_name() >= sd
->section_names_size
)
365 this->error(_("bad section name offset for section %u: %lu"),
366 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
370 const char* name
= pnames
+ shdr
.get_sh_name();
372 this->handle_gnu_warning_section(name
, i
, symtab
);
375 delete sd
->section_headers
;
376 sd
->section_headers
= NULL
;
377 delete sd
->section_names
;
378 sd
->section_names
= NULL
;
381 // Add an entry to the vector mapping version numbers to version
384 template<int size
, bool big_endian
>
386 Sized_dynobj
<size
, big_endian
>::set_version_map(
387 Version_map
* version_map
,
389 const char* name
) const
391 if (ndx
>= version_map
->size())
392 version_map
->resize(ndx
+ 1);
393 if ((*version_map
)[ndx
] != NULL
)
394 this->error(_("duplicate definition for version %u"), ndx
);
395 (*version_map
)[ndx
] = name
;
398 // Add mappings for the version definitions to VERSION_MAP.
400 template<int size
, bool big_endian
>
402 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
403 Read_symbols_data
* sd
,
404 Version_map
* version_map
) const
406 if (sd
->verdef
== NULL
)
409 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
410 off_t names_size
= sd
->symbol_names_size
;
412 const unsigned char* pverdef
= sd
->verdef
->data();
413 off_t verdef_size
= sd
->verdef_size
;
414 const unsigned int count
= sd
->verdef_info
;
416 const unsigned char* p
= pverdef
;
417 for (unsigned int i
= 0; i
< count
; ++i
)
419 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
421 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
423 this->error(_("unexpected verdef version %u"),
424 verdef
.get_vd_version());
428 const unsigned int vd_ndx
= verdef
.get_vd_ndx();
430 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
433 // The first Verdaux holds the name of this version. Subsequent
434 // ones are versions that this one depends upon, which we don't
436 const unsigned int vd_cnt
= verdef
.get_vd_cnt();
439 this->error(_("verdef vd_cnt field too small: %u"), vd_cnt
);
443 const unsigned int vd_aux
= verdef
.get_vd_aux();
444 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
446 this->error(_("verdef vd_aux field out of range: %u"), vd_aux
);
450 const unsigned char* pvda
= p
+ vd_aux
;
451 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
453 const unsigned int vda_name
= verdaux
.get_vda_name();
454 if (vda_name
>= names_size
)
456 this->error(_("verdaux vda_name field out of range: %u"), vda_name
);
460 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
462 const unsigned int vd_next
= verdef
.get_vd_next();
463 if ((p
- pverdef
) + vd_next
>= verdef_size
)
465 this->error(_("verdef vd_next field out of range: %u"), vd_next
);
473 // Add mappings for the required versions to VERSION_MAP.
475 template<int size
, bool big_endian
>
477 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
478 Read_symbols_data
* sd
,
479 Version_map
* version_map
) const
481 if (sd
->verneed
== NULL
)
484 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
485 off_t names_size
= sd
->symbol_names_size
;
487 const unsigned char* pverneed
= sd
->verneed
->data();
488 const off_t verneed_size
= sd
->verneed_size
;
489 const unsigned int count
= sd
->verneed_info
;
491 const unsigned char* p
= pverneed
;
492 for (unsigned int i
= 0; i
< count
; ++i
)
494 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
496 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
498 this->error(_("unexpected verneed version %u"),
499 verneed
.get_vn_version());
503 const unsigned int vn_aux
= verneed
.get_vn_aux();
505 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
507 this->error(_("verneed vn_aux field out of range: %u"), vn_aux
);
511 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
512 const unsigned char* pvna
= p
+ vn_aux
;
513 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
515 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
517 const unsigned int vna_name
= vernaux
.get_vna_name();
518 if (vna_name
>= names_size
)
520 this->error(_("vernaux vna_name field out of range: %u"),
525 this->set_version_map(version_map
, vernaux
.get_vna_other(),
528 const unsigned int vna_next
= vernaux
.get_vna_next();
529 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
531 this->error(_("verneed vna_next field out of range: %u"),
539 const unsigned int vn_next
= verneed
.get_vn_next();
540 if ((p
- pverneed
) + vn_next
>= verneed_size
)
542 this->error(_("verneed vn_next field out of range: %u"), vn_next
);
550 // Create a vector mapping version numbers to version strings.
552 template<int size
, bool big_endian
>
554 Sized_dynobj
<size
, big_endian
>::make_version_map(
555 Read_symbols_data
* sd
,
556 Version_map
* version_map
) const
558 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
561 // A guess at the maximum version number we will see. If this is
562 // wrong we will be less efficient but still correct.
563 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
565 this->make_verdef_map(sd
, version_map
);
566 this->make_verneed_map(sd
, version_map
);
569 // Add the dynamic symbols to the symbol table.
571 template<int size
, bool big_endian
>
573 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
574 Read_symbols_data
* sd
)
576 if (sd
->symbols
== NULL
)
578 gold_assert(sd
->symbol_names
== NULL
);
579 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
580 && sd
->verneed
== NULL
);
584 const int sym_size
= This::sym_size
;
585 const size_t symcount
= sd
->symbols_size
/ sym_size
;
586 if (static_cast<off_t
>(symcount
* sym_size
) != sd
->symbols_size
)
588 this->error(_("size of dynamic symbols is not multiple of symbol size"));
592 Version_map version_map
;
593 this->make_version_map(sd
, &version_map
);
595 const char* sym_names
=
596 reinterpret_cast<const char*>(sd
->symbol_names
->data());
597 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
598 sym_names
, sd
->symbol_names_size
,
601 : sd
->versym
->data()),
607 delete sd
->symbol_names
;
608 sd
->symbol_names
= NULL
;
609 if (sd
->versym
!= NULL
)
614 if (sd
->verdef
!= NULL
)
619 if (sd
->verneed
!= NULL
)
626 // Given a vector of hash codes, compute the number of hash buckets to
630 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
631 bool for_gnu_hash_table
)
633 // FIXME: Implement optional hash table optimization.
635 // Array used to determine the number of hash table buckets to use
636 // based on the number of symbols there are. If there are fewer
637 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
638 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
639 // use more than 32771 buckets. This is straight from the old GNU
641 static const unsigned int buckets
[] =
643 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
646 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
648 unsigned int symcount
= hashcodes
.size();
649 unsigned int ret
= 1;
650 for (int i
= 0; i
< buckets_count
; ++i
)
652 if (symcount
< buckets
[i
])
657 if (for_gnu_hash_table
&& ret
< 2)
663 // The standard ELF hash function. This hash function must not
664 // change, as the dynamic linker uses it also.
667 Dynobj::elf_hash(const char* name
)
669 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
672 while ((c
= *nameu
++) != '\0')
675 uint32_t g
= h
& 0xf0000000;
679 // The ELF ABI says h &= ~g, but using xor is equivalent in
680 // this case (since g was set from h) and may save one
688 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
689 // DYNSYMS is a vector with all the global dynamic symbols.
690 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
694 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
695 unsigned int local_dynsym_count
,
696 unsigned char** pphash
,
697 unsigned int* phashlen
)
699 unsigned int dynsym_count
= dynsyms
.size();
701 // Get the hash values for all the symbols.
702 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
703 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
704 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
706 const unsigned int bucketcount
=
707 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
709 std::vector
<uint32_t> bucket(bucketcount
);
710 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
712 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
714 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
715 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
716 chain
[dynsym_index
] = bucket
[bucketpos
];
717 bucket
[bucketpos
] = dynsym_index
;
720 unsigned int hashlen
= ((2
725 unsigned char* phash
= new unsigned char[hashlen
];
727 if (parameters
->is_big_endian())
729 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
730 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
738 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
739 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
750 // Fill in an ELF hash table.
752 template<bool big_endian
>
754 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
755 const std::vector
<uint32_t>& chain
,
756 unsigned char* phash
,
757 unsigned int hashlen
)
759 unsigned char* p
= phash
;
761 const unsigned int bucketcount
= bucket
.size();
762 const unsigned int chaincount
= chain
.size();
764 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
766 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
769 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
771 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
775 for (unsigned int i
= 0; i
< chaincount
; ++i
)
777 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
781 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
784 // The hash function used for the GNU hash table. This hash function
785 // must not change, as the dynamic linker uses it also.
788 Dynobj::gnu_hash(const char* name
)
790 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
793 while ((c
= *nameu
++) != '\0')
794 h
= (h
<< 5) + h
+ c
;
798 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
799 // tables are an extension to ELF which are recognized by the GNU
800 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
801 // TARGET is the target. DYNSYMS is a vector with all the global
802 // symbols which will be going into the dynamic symbol table.
803 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
807 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
808 unsigned int local_dynsym_count
,
809 unsigned char** pphash
,
810 unsigned int* phashlen
)
812 const unsigned int count
= dynsyms
.size();
814 // Sort the dynamic symbols into two vectors. Symbols which we do
815 // not want to put into the hash table we store into
816 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
817 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
818 // and records the hash codes.
820 std::vector
<Symbol
*> unhashed_dynsyms
;
821 unhashed_dynsyms
.reserve(count
);
823 std::vector
<Symbol
*> hashed_dynsyms
;
824 hashed_dynsyms
.reserve(count
);
826 std::vector
<uint32_t> dynsym_hashvals
;
827 dynsym_hashvals
.reserve(count
);
829 for (unsigned int i
= 0; i
< count
; ++i
)
831 Symbol
* sym
= dynsyms
[i
];
833 // FIXME: Should put on unhashed_dynsyms if the symbol is
835 if (sym
->is_undefined())
836 unhashed_dynsyms
.push_back(sym
);
839 hashed_dynsyms
.push_back(sym
);
840 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
844 // Put the unhashed symbols at the start of the global portion of
845 // the dynamic symbol table.
846 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
847 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
848 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
850 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
851 ++unhashed_dynsym_index
;
854 // For the actual data generation we call out to a templatized
856 int size
= parameters
->get_size();
857 bool big_endian
= parameters
->is_big_endian();
862 #ifdef HAVE_TARGET_32_BIG
863 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
865 unhashed_dynsym_index
,
874 #ifdef HAVE_TARGET_32_LITTLE
875 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
877 unhashed_dynsym_index
,
889 #ifdef HAVE_TARGET_64_BIG
890 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
892 unhashed_dynsym_index
,
901 #ifdef HAVE_TARGET_64_LITTLE
902 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
904 unhashed_dynsym_index
,
916 // Create the actual data for a GNU hash table. This is just a copy
917 // of the code from the old GNU linker.
919 template<int size
, bool big_endian
>
921 Dynobj::sized_create_gnu_hash_table(
922 const std::vector
<Symbol
*>& hashed_dynsyms
,
923 const std::vector
<uint32_t>& dynsym_hashvals
,
924 unsigned int unhashed_dynsym_count
,
925 unsigned char** pphash
,
926 unsigned int* phashlen
)
928 if (hashed_dynsyms
.empty())
930 // Special case for the empty hash table.
931 unsigned int hashlen
= 5 * 4 + size
/ 8;
932 unsigned char* phash
= new unsigned char[hashlen
];
934 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
935 // Symbol index above unhashed symbols.
936 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
937 // One word for bitmask.
938 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
939 // Only bloom filter.
940 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
942 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
943 // No hashes in only bucket.
944 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
952 const unsigned int bucketcount
=
953 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
955 const unsigned int nsyms
= hashed_dynsyms
.size();
957 uint32_t maskbitslog2
= 1;
958 uint32_t x
= nsyms
>> 1;
964 if (maskbitslog2
< 3)
966 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
976 if (maskbitslog2
== 5)
980 uint32_t mask
= (1U << shift1
) - 1U;
981 uint32_t shift2
= maskbitslog2
;
982 uint32_t maskbits
= 1U << maskbitslog2
;
983 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
985 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
986 std::vector
<Word
> bitmask(maskwords
);
987 std::vector
<uint32_t> counts(bucketcount
);
988 std::vector
<uint32_t> indx(bucketcount
);
989 uint32_t symindx
= unhashed_dynsym_count
;
991 // Count the number of times each hash bucket is used.
992 for (unsigned int i
= 0; i
< nsyms
; ++i
)
993 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
995 unsigned int cnt
= symindx
;
996 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1002 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1003 hashlen
+= maskbits
/ 8;
1004 unsigned char* phash
= new unsigned char[hashlen
];
1006 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1007 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1008 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1009 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1011 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1012 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1015 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1017 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1021 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1023 Symbol
* sym
= hashed_dynsyms
[i
];
1024 uint32_t hashval
= dynsym_hashvals
[i
];
1026 unsigned int bucket
= hashval
% bucketcount
;
1027 unsigned int val
= ((hashval
>> shift1
)
1028 & ((maskbits
>> shift1
) - 1));
1029 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1030 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1031 val
= hashval
& ~ 1U;
1032 if (counts
[bucket
] == 1)
1034 // Last element terminates the chain.
1037 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1041 sym
->set_dynsym_index(indx
[bucket
]);
1046 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1048 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1052 *phashlen
= hashlen
;
1058 // Write this definition to a buffer for the output section.
1060 template<int size
, bool big_endian
>
1062 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
1063 ACCEPT_SIZE_ENDIAN
) const
1065 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1066 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1068 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1069 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1070 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1071 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0));
1072 vd
.set_vd_ndx(this->index());
1073 vd
.set_vd_cnt(1 + this->deps_
.size());
1074 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1075 vd
.set_vd_aux(verdef_size
);
1076 vd
.set_vd_next(is_last
1078 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1081 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1082 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1083 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1086 Deps::const_iterator p
;
1088 for (p
= this->deps_
.begin(), i
= 0;
1089 p
!= this->deps_
.end();
1092 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1093 vda
.set_vda_name(dynpool
->get_offset(*p
));
1094 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1105 for (Need_versions::iterator p
= this->need_versions_
.begin();
1106 p
!= this->need_versions_
.end();
1111 // Add a new version to this file reference.
1114 Verneed::add_name(const char* name
)
1116 Verneed_version
* vv
= new Verneed_version(name
);
1117 this->need_versions_
.push_back(vv
);
1121 // Set the version indexes starting at INDEX.
1124 Verneed::finalize(unsigned int index
)
1126 for (Need_versions::iterator p
= this->need_versions_
.begin();
1127 p
!= this->need_versions_
.end();
1130 (*p
)->set_index(index
);
1136 // Write this list of referenced versions to a buffer for the output
1139 template<int size
, bool big_endian
>
1141 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1142 unsigned char* pb ACCEPT_SIZE_ENDIAN
) const
1144 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1145 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1147 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1148 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1149 vn
.set_vn_cnt(this->need_versions_
.size());
1150 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1151 vn
.set_vn_aux(verneed_size
);
1152 vn
.set_vn_next(is_last
1154 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1157 Need_versions::const_iterator p
;
1159 for (p
= this->need_versions_
.begin(), i
= 0;
1160 p
!= this->need_versions_
.end();
1163 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1164 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1165 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1166 vna
.set_vna_flags(0);
1167 vna
.set_vna_other((*p
)->index());
1168 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1169 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1178 // Versions methods.
1180 Versions::~Versions()
1182 for (Defs::iterator p
= this->defs_
.begin();
1183 p
!= this->defs_
.end();
1187 for (Needs::iterator p
= this->needs_
.begin();
1188 p
!= this->needs_
.end();
1193 // Record version information for a symbol going into the dynamic
1197 Versions::record_version(const General_options
* options
,
1198 Stringpool
* dynpool
, const Symbol
* sym
)
1200 gold_assert(!this->is_finalized_
);
1201 gold_assert(sym
->version() != NULL
);
1203 Stringpool::Key version_key
;
1204 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1206 if (!sym
->is_from_dynobj())
1208 if (parameters
->output_is_shared())
1209 this->add_def(options
, sym
, version
, version_key
);
1213 // This is a version reference.
1215 Object
* object
= sym
->object();
1216 gold_assert(object
->is_dynamic());
1217 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1219 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1223 // We've found a symbol SYM defined in version VERSION.
1226 Versions::add_def(const General_options
* options
, const Symbol
* sym
,
1227 const char* version
, Stringpool::Key version_key
)
1229 Key
k(version_key
, 0);
1230 Version_base
* const vbnull
= NULL
;
1231 std::pair
<Version_table::iterator
, bool> ins
=
1232 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1236 // We already have an entry for this version.
1237 Version_base
* vb
= ins
.first
->second
;
1239 // We have now seen a symbol in this version, so it is not
1243 // FIXME: When we support version scripts, we will need to
1244 // check whether this symbol should be forced local.
1248 // If we are creating a shared object, it is an error to
1249 // find a definition of a symbol with a version which is not
1250 // in the version script.
1251 if (parameters
->output_is_shared())
1253 gold_error(_("symbol %s has undefined version %s"),
1254 sym
->name(), version
);
1258 // If this is the first version we are defining, first define
1259 // the base version. FIXME: Should use soname here when
1260 // creating a shared object.
1261 Verdef
* vdbase
= new Verdef(options
->output_file_name(), true, false,
1263 this->defs_
.push_back(vdbase
);
1265 // When creating a regular executable, automatically define
1267 Verdef
* vd
= new Verdef(version
, false, false, false);
1268 this->defs_
.push_back(vd
);
1269 ins
.first
->second
= vd
;
1273 // Add a reference to version NAME in file FILENAME.
1276 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1277 Stringpool::Key name_key
)
1279 Stringpool::Key filename_key
;
1280 filename
= dynpool
->add(filename
, true, &filename_key
);
1282 Key
k(name_key
, filename_key
);
1283 Version_base
* const vbnull
= NULL
;
1284 std::pair
<Version_table::iterator
, bool> ins
=
1285 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1289 // We already have an entry for this filename/version.
1293 // See whether we already have this filename. We don't expect many
1294 // version references, so we just do a linear search. This could be
1295 // replaced by a hash table.
1297 for (Needs::iterator p
= this->needs_
.begin();
1298 p
!= this->needs_
.end();
1301 if ((*p
)->filename() == filename
)
1310 // We have a new filename.
1311 vn
= new Verneed(filename
);
1312 this->needs_
.push_back(vn
);
1315 ins
.first
->second
= vn
->add_name(name
);
1318 // Set the version indexes. Create a new dynamic version symbol for
1319 // each new version definition.
1322 Versions::finalize(const Target
* target
, Symbol_table
* symtab
,
1323 unsigned int dynsym_index
, std::vector
<Symbol
*>* syms
)
1325 gold_assert(!this->is_finalized_
);
1327 unsigned int vi
= 1;
1329 for (Defs::iterator p
= this->defs_
.begin();
1330 p
!= this->defs_
.end();
1333 (*p
)->set_index(vi
);
1336 // Create a version symbol if necessary.
1337 if (!(*p
)->is_symbol_created())
1339 Symbol
* vsym
= symtab
->define_as_constant(target
, (*p
)->name(),
1343 elfcpp::STV_DEFAULT
, 0,
1345 vsym
->set_needs_dynsym_entry();
1346 vsym
->set_dynsym_index(dynsym_index
);
1348 syms
->push_back(vsym
);
1349 // The name is already in the dynamic pool.
1353 // Index 1 is used for global symbols.
1356 gold_assert(this->defs_
.empty());
1360 for (Needs::iterator p
= this->needs_
.begin();
1361 p
!= this->needs_
.end();
1363 vi
= (*p
)->finalize(vi
);
1365 this->is_finalized_
= true;
1367 return dynsym_index
;
1370 // Return the version index to use for a symbol. This does two hash
1371 // table lookups: one in DYNPOOL and one in this->version_table_.
1372 // Another approach alternative would be store a pointer in SYM, which
1373 // would increase the size of the symbol table. Or perhaps we could
1374 // use a hash table from dynamic symbol pointer values to Version_base
1378 Versions::version_index(const Stringpool
* dynpool
, const Symbol
* sym
) const
1380 Stringpool::Key version_key
;
1381 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1382 gold_assert(version
!= NULL
);
1385 if (!sym
->is_from_dynobj())
1387 if (!parameters
->output_is_shared())
1388 return elfcpp::VER_NDX_GLOBAL
;
1389 k
= Key(version_key
, 0);
1393 Object
* object
= sym
->object();
1394 gold_assert(object
->is_dynamic());
1395 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1397 Stringpool::Key filename_key
;
1398 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1399 gold_assert(filename
!= NULL
);
1401 k
= Key(version_key
, filename_key
);
1404 Version_table::const_iterator p
= this->version_table_
.find(k
);
1405 gold_assert(p
!= this->version_table_
.end());
1407 return p
->second
->index();
1410 // Return an allocated buffer holding the contents of the symbol
1413 template<int size
, bool big_endian
>
1415 Versions::symbol_section_contents(const Stringpool
* dynpool
,
1416 unsigned int local_symcount
,
1417 const std::vector
<Symbol
*>& syms
,
1420 ACCEPT_SIZE_ENDIAN
) const
1422 gold_assert(this->is_finalized_
);
1424 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1425 unsigned char* pbuf
= new unsigned char[sz
];
1427 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1428 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1429 elfcpp::VER_NDX_LOCAL
);
1431 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1435 unsigned int version_index
;
1436 const char* version
= (*p
)->version();
1437 if (version
== NULL
)
1438 version_index
= elfcpp::VER_NDX_GLOBAL
;
1440 version_index
= this->version_index(dynpool
, *p
);
1441 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1449 // Return an allocated buffer holding the contents of the version
1450 // definition section.
1452 template<int size
, bool big_endian
>
1454 Versions::def_section_contents(const Stringpool
* dynpool
,
1455 unsigned char** pp
, unsigned int* psize
,
1456 unsigned int* pentries
1457 ACCEPT_SIZE_ENDIAN
) const
1459 gold_assert(this->is_finalized_
);
1460 gold_assert(!this->defs_
.empty());
1462 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1463 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1465 unsigned int sz
= 0;
1466 for (Defs::const_iterator p
= this->defs_
.begin();
1467 p
!= this->defs_
.end();
1470 sz
+= verdef_size
+ verdaux_size
;
1471 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1474 unsigned char* pbuf
= new unsigned char[sz
];
1476 unsigned char* pb
= pbuf
;
1477 Defs::const_iterator p
;
1479 for (p
= this->defs_
.begin(), i
= 0;
1480 p
!= this->defs_
.end();
1482 pb
= (*p
)->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1483 dynpool
, i
+ 1 >= this->defs_
.size(), pb
1484 SELECT_SIZE_ENDIAN(size
, big_endian
));
1486 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1490 *pentries
= this->defs_
.size();
1493 // Return an allocated buffer holding the contents of the version
1494 // reference section.
1496 template<int size
, bool big_endian
>
1498 Versions::need_section_contents(const Stringpool
* dynpool
,
1499 unsigned char** pp
, unsigned int *psize
,
1500 unsigned int *pentries
1501 ACCEPT_SIZE_ENDIAN
) const
1503 gold_assert(this->is_finalized_
);
1504 gold_assert(!this->needs_
.empty());
1506 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1507 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1509 unsigned int sz
= 0;
1510 for (Needs::const_iterator p
= this->needs_
.begin();
1511 p
!= this->needs_
.end();
1515 sz
+= (*p
)->count_versions() * vernaux_size
;
1518 unsigned char* pbuf
= new unsigned char[sz
];
1520 unsigned char* pb
= pbuf
;
1521 Needs::const_iterator p
;
1523 for (p
= this->needs_
.begin(), i
= 0;
1524 p
!= this->needs_
.end();
1526 pb
= (*p
)->write
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
)(
1527 dynpool
, i
+ 1 >= this->needs_
.size(), pb
1528 SELECT_SIZE_ENDIAN(size
, big_endian
));
1530 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1534 *pentries
= this->needs_
.size();
1537 // Instantiate the templates we need. We could use the configure
1538 // script to restrict this to only the ones for implemented targets.
1540 #ifdef HAVE_TARGET_32_LITTLE
1542 class Sized_dynobj
<32, false>;
1545 #ifdef HAVE_TARGET_32_BIG
1547 class Sized_dynobj
<32, true>;
1550 #ifdef HAVE_TARGET_64_LITTLE
1552 class Sized_dynobj
<64, false>;
1555 #ifdef HAVE_TARGET_64_BIG
1557 class Sized_dynobj
<64, true>;
1560 #ifdef HAVE_TARGET_32_LITTLE
1563 Versions::symbol_section_contents
<32, false>(
1566 const std::vector
<Symbol
*>&,
1569 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1572 #ifdef HAVE_TARGET_32_BIG
1575 Versions::symbol_section_contents
<32, true>(
1578 const std::vector
<Symbol
*>&,
1581 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1584 #ifdef HAVE_TARGET_64_LITTLE
1587 Versions::symbol_section_contents
<64, false>(
1590 const std::vector
<Symbol
*>&,
1593 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1596 #ifdef HAVE_TARGET_64_BIG
1599 Versions::symbol_section_contents
<64, true>(
1602 const std::vector
<Symbol
*>&,
1605 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1608 #ifdef HAVE_TARGET_32_LITTLE
1611 Versions::def_section_contents
<32, false>(
1616 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1619 #ifdef HAVE_TARGET_32_BIG
1622 Versions::def_section_contents
<32, true>(
1627 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1630 #ifdef HAVE_TARGET_64_LITTLE
1633 Versions::def_section_contents
<64, false>(
1638 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1641 #ifdef HAVE_TARGET_64_BIG
1644 Versions::def_section_contents
<64, true>(
1649 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1652 #ifdef HAVE_TARGET_32_LITTLE
1655 Versions::need_section_contents
<32, false>(
1660 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1663 #ifdef HAVE_TARGET_32_BIG
1666 Versions::need_section_contents
<32, true>(
1671 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1674 #ifdef HAVE_TARGET_64_LITTLE
1677 Versions::need_section_contents
<64, false>(
1682 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1685 #ifdef HAVE_TARGET_64_BIG
1688 Versions::need_section_contents
<64, true>(
1693 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1696 } // End namespace gold.