1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
39 // Sets up the default soname_ to use, in the (rare) cases we never
40 // see a DT_SONAME entry.
42 Dynobj::Dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
)
43 : Object(name
, input_file
, true, offset
),
45 unknown_needed_(UNKNOWN_NEEDED_UNSET
)
47 // This will be overridden by a DT_SONAME entry, hopefully. But if
48 // we never see a DT_SONAME entry, our rule is to use the dynamic
49 // object's filename. The only exception is when the dynamic object
50 // is part of an archive (so the filename is the archive's
51 // filename). In that case, we use just the dynobj's name-in-archive.
52 this->soname_
= this->input_file()->found_name();
53 if (this->offset() != 0)
55 std::string::size_type open_paren
= this->name().find('(');
56 std::string::size_type close_paren
= this->name().find(')');
57 if (open_paren
!= std::string::npos
&& close_paren
!= std::string::npos
)
59 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
60 this->soname_
= this->name().substr(open_paren
+ 1,
61 close_paren
- (open_paren
+ 1));
66 // Class Sized_dynobj.
68 template<int size
, bool big_endian
>
69 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
70 const std::string
& name
,
71 Input_file
* input_file
,
73 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
74 : Dynobj(name
, input_file
, offset
),
75 elf_file_(this, ehdr
),
84 template<int size
, bool big_endian
>
86 Sized_dynobj
<size
, big_endian
>::setup()
88 const unsigned int shnum
= this->elf_file_
.shnum();
89 this->set_shnum(shnum
);
92 // Find the SHT_DYNSYM section and the various version sections, and
93 // the dynamic section, given the section headers.
95 template<int size
, bool big_endian
>
97 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
98 const unsigned char* pshdrs
,
99 unsigned int* pversym_shndx
,
100 unsigned int* pverdef_shndx
,
101 unsigned int* pverneed_shndx
,
102 unsigned int* pdynamic_shndx
)
104 *pversym_shndx
= -1U;
105 *pverdef_shndx
= -1U;
106 *pverneed_shndx
= -1U;
107 *pdynamic_shndx
= -1U;
109 unsigned int symtab_shndx
= 0;
110 unsigned int xindex_shndx
= 0;
111 unsigned int xindex_link
= 0;
112 const unsigned int shnum
= this->shnum();
113 const unsigned char* p
= pshdrs
;
114 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
116 typename
This::Shdr
shdr(p
);
119 switch (shdr
.get_sh_type())
121 case elfcpp::SHT_DYNSYM
:
122 this->dynsym_shndx_
= i
;
123 if (xindex_shndx
> 0 && xindex_link
== i
)
125 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
126 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
128 this->set_xindex(xindex
);
132 case elfcpp::SHT_SYMTAB
:
135 case elfcpp::SHT_GNU_versym
:
138 case elfcpp::SHT_GNU_verdef
:
141 case elfcpp::SHT_GNU_verneed
:
144 case elfcpp::SHT_DYNAMIC
:
147 case elfcpp::SHT_SYMTAB_SHNDX
:
149 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
150 if (xindex_link
== this->dynsym_shndx_
)
152 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
153 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
155 this->set_xindex(xindex
);
168 this->error(_("unexpected duplicate type %u section: %u, %u"),
169 shdr
.get_sh_type(), *pi
, i
);
174 // If there is no dynamic symbol table, use the normal symbol table.
175 // On some SVR4 systems, a shared library is stored in an archive.
176 // The version stored in the archive only has a normal symbol table.
177 // It has an SONAME entry which points to another copy in the file
178 // system which has a dynamic symbol table as usual. This is way of
179 // addressing the issues which glibc addresses using GROUP with
181 if (this->dynsym_shndx_
== -1U && symtab_shndx
!= 0)
183 this->dynsym_shndx_
= symtab_shndx
;
184 if (xindex_shndx
> 0 && xindex_link
== symtab_shndx
)
186 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
187 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
189 this->set_xindex(xindex
);
194 // Read the contents of section SHNDX. PSHDRS points to the section
195 // headers. TYPE is the expected section type. LINK is the expected
196 // section link. Store the data in *VIEW and *VIEW_SIZE. The
197 // section's sh_info field is stored in *VIEW_INFO.
199 template<int size
, bool big_endian
>
201 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
202 const unsigned char* pshdrs
,
207 section_size_type
* view_size
,
208 unsigned int* view_info
)
218 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
220 gold_assert(shdr
.get_sh_type() == type
);
222 if (this->adjust_shndx(shdr
.get_sh_link()) != link
)
223 this->error(_("unexpected link in section %u header: %u != %u"),
224 shndx
, this->adjust_shndx(shdr
.get_sh_link()), link
);
226 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size(),
228 *view_size
= convert_to_section_size_type(shdr
.get_sh_size());
229 *view_info
= shdr
.get_sh_info();
232 // Read the dynamic tags. Set the soname field if this shared object
233 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
234 // the section headers. DYNAMIC_SHNDX is the section index of the
235 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
236 // section index and contents of a string table which may be the one
237 // associated with the SHT_DYNAMIC section.
239 template<int size
, bool big_endian
>
241 Sized_dynobj
<size
, big_endian
>::read_dynamic(const unsigned char* pshdrs
,
242 unsigned int dynamic_shndx
,
243 unsigned int strtab_shndx
,
244 const unsigned char* strtabu
,
247 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
248 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
250 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
251 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
252 dynamic_size
, true, false);
254 const unsigned int link
= this->adjust_shndx(dynamicshdr
.get_sh_link());
255 if (link
!= strtab_shndx
)
257 if (link
>= this->shnum())
259 this->error(_("DYNAMIC section %u link out of range: %u"),
260 dynamic_shndx
, link
);
264 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
265 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
267 this->error(_("DYNAMIC section %u link %u is not a strtab"),
268 dynamic_shndx
, link
);
272 strtab_size
= strtabshdr
.get_sh_size();
273 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
, false,
277 const char* const strtab
= reinterpret_cast<const char*>(strtabu
);
279 for (const unsigned char* p
= pdynamic
;
280 p
< pdynamic
+ dynamic_size
;
283 typename
This::Dyn
dyn(p
);
285 switch (dyn
.get_d_tag())
287 case elfcpp::DT_NULL
:
288 // We should always see DT_NULL at the end of the dynamic
292 case elfcpp::DT_SONAME
:
294 off_t val
= dyn
.get_d_val();
295 if (val
>= strtab_size
)
296 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
297 static_cast<long long>(val
),
298 static_cast<long long>(strtab_size
));
300 this->set_soname_string(strtab
+ val
);
304 case elfcpp::DT_NEEDED
:
306 off_t val
= dyn
.get_d_val();
307 if (val
>= strtab_size
)
308 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
309 static_cast<long long>(val
),
310 static_cast<long long>(strtab_size
));
312 this->add_needed(strtab
+ val
);
321 this->error(_("missing DT_NULL in dynamic segment"));
324 // Read the symbols and sections from a dynamic object. We read the
325 // dynamic symbols, not the normal symbols.
327 template<int size
, bool big_endian
>
329 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
331 this->read_section_data(&this->elf_file_
, sd
);
333 const unsigned char* const pshdrs
= sd
->section_headers
->data();
335 unsigned int versym_shndx
;
336 unsigned int verdef_shndx
;
337 unsigned int verneed_shndx
;
338 unsigned int dynamic_shndx
;
339 this->find_dynsym_sections(pshdrs
, &versym_shndx
, &verdef_shndx
,
340 &verneed_shndx
, &dynamic_shndx
);
342 unsigned int strtab_shndx
= -1U;
345 sd
->symbols_size
= 0;
346 sd
->external_symbols_offset
= 0;
347 sd
->symbol_names
= NULL
;
348 sd
->symbol_names_size
= 0;
355 sd
->verneed_size
= 0;
356 sd
->verneed_info
= 0;
358 if (this->dynsym_shndx_
!= -1U)
360 // Get the dynamic symbols.
361 typename
This::Shdr
dynsymshdr(pshdrs
362 + this->dynsym_shndx_
* This::shdr_size
);
364 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
365 dynsymshdr
.get_sh_size(), true,
368 convert_to_section_size_type(dynsymshdr
.get_sh_size());
370 // Get the symbol names.
371 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
372 if (strtab_shndx
>= this->shnum())
374 this->error(_("invalid dynamic symbol table name index: %u"),
378 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
379 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
381 this->error(_("dynamic symbol table name section "
382 "has wrong type: %u"),
383 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
387 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
388 strtabshdr
.get_sh_size(),
390 sd
->symbol_names_size
=
391 convert_to_section_size_type(strtabshdr
.get_sh_size());
393 // Get the version information.
396 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
398 &sd
->versym
, &sd
->versym_size
, &dummy
);
400 // We require that the version definition and need section link
401 // to the same string table as the dynamic symbol table. This
402 // is not a technical requirement, but it always happens in
403 // practice. We could change this if necessary.
405 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
406 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
409 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
410 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
414 // Read the SHT_DYNAMIC section to find whether this shared object
415 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
416 // doesn't really have anything to do with reading the symbols, but
417 // this is a convenient place to do it.
418 if (dynamic_shndx
!= -1U)
419 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
420 (sd
->symbol_names
== NULL
422 : sd
->symbol_names
->data()),
423 sd
->symbol_names_size
);
426 // Return the Xindex structure to use for object with lots of
429 template<int size
, bool big_endian
>
431 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
433 gold_assert(this->dynsym_shndx_
!= -1U);
434 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
435 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
439 // Lay out the input sections for a dynamic object. We don't want to
440 // include sections from a dynamic object, so all that we actually do
441 // here is check for .gnu.warning and .note.GNU-split-stack sections.
443 template<int size
, bool big_endian
>
445 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
447 Read_symbols_data
* sd
)
449 const unsigned int shnum
= this->shnum();
453 // Get the section headers.
454 const unsigned char* pshdrs
= sd
->section_headers
->data();
456 // Get the section names.
457 const unsigned char* pnamesu
= sd
->section_names
->data();
458 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
460 // Skip the first, dummy, section.
461 pshdrs
+= This::shdr_size
;
462 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
464 typename
This::Shdr
shdr(pshdrs
);
466 if (shdr
.get_sh_name() >= sd
->section_names_size
)
468 this->error(_("bad section name offset for section %u: %lu"),
469 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
473 const char* name
= pnames
+ shdr
.get_sh_name();
475 this->handle_gnu_warning_section(name
, i
, symtab
);
476 this->handle_split_stack_section(name
);
479 delete sd
->section_headers
;
480 sd
->section_headers
= NULL
;
481 delete sd
->section_names
;
482 sd
->section_names
= NULL
;
485 // Add an entry to the vector mapping version numbers to version
488 template<int size
, bool big_endian
>
490 Sized_dynobj
<size
, big_endian
>::set_version_map(
491 Version_map
* version_map
,
493 const char* name
) const
495 if (ndx
>= version_map
->size())
496 version_map
->resize(ndx
+ 1);
497 if ((*version_map
)[ndx
] != NULL
)
498 this->error(_("duplicate definition for version %u"), ndx
);
499 (*version_map
)[ndx
] = name
;
502 // Add mappings for the version definitions to VERSION_MAP.
504 template<int size
, bool big_endian
>
506 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
507 Read_symbols_data
* sd
,
508 Version_map
* version_map
) const
510 if (sd
->verdef
== NULL
)
513 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
514 section_size_type names_size
= sd
->symbol_names_size
;
516 const unsigned char* pverdef
= sd
->verdef
->data();
517 section_size_type 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 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
527 this->error(_("unexpected verdef version %u"),
528 verdef
.get_vd_version());
532 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
534 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
537 // The first Verdaux holds the name of this version. Subsequent
538 // ones are versions that this one depends upon, which we don't
540 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
543 this->error(_("verdef vd_cnt field too small: %u"),
544 static_cast<unsigned int>(vd_cnt
));
548 const section_size_type vd_aux
= verdef
.get_vd_aux();
549 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
551 this->error(_("verdef vd_aux field out of range: %u"),
552 static_cast<unsigned int>(vd_aux
));
556 const unsigned char* pvda
= p
+ vd_aux
;
557 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
559 const section_size_type vda_name
= verdaux
.get_vda_name();
560 if (vda_name
>= names_size
)
562 this->error(_("verdaux vda_name field out of range: %u"),
563 static_cast<unsigned int>(vda_name
));
567 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
569 const section_size_type vd_next
= verdef
.get_vd_next();
570 if ((p
- pverdef
) + vd_next
>= verdef_size
)
572 this->error(_("verdef vd_next field out of range: %u"),
573 static_cast<unsigned int>(vd_next
));
581 // Add mappings for the required versions to VERSION_MAP.
583 template<int size
, bool big_endian
>
585 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
586 Read_symbols_data
* sd
,
587 Version_map
* version_map
) const
589 if (sd
->verneed
== NULL
)
592 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
593 section_size_type names_size
= sd
->symbol_names_size
;
595 const unsigned char* pverneed
= sd
->verneed
->data();
596 const section_size_type verneed_size
= sd
->verneed_size
;
597 const unsigned int count
= sd
->verneed_info
;
599 const unsigned char* p
= pverneed
;
600 for (unsigned int i
= 0; i
< count
; ++i
)
602 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
604 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
606 this->error(_("unexpected verneed version %u"),
607 verneed
.get_vn_version());
611 const section_size_type vn_aux
= verneed
.get_vn_aux();
613 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
615 this->error(_("verneed vn_aux field out of range: %u"),
616 static_cast<unsigned int>(vn_aux
));
620 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
621 const unsigned char* pvna
= p
+ vn_aux
;
622 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
624 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
626 const unsigned int vna_name
= vernaux
.get_vna_name();
627 if (vna_name
>= names_size
)
629 this->error(_("vernaux vna_name field out of range: %u"),
630 static_cast<unsigned int>(vna_name
));
634 this->set_version_map(version_map
, vernaux
.get_vna_other(),
637 const section_size_type vna_next
= vernaux
.get_vna_next();
638 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
640 this->error(_("verneed vna_next field out of range: %u"),
641 static_cast<unsigned int>(vna_next
));
648 const section_size_type vn_next
= verneed
.get_vn_next();
649 if ((p
- pverneed
) + vn_next
>= verneed_size
)
651 this->error(_("verneed vn_next field out of range: %u"),
652 static_cast<unsigned int>(vn_next
));
660 // Create a vector mapping version numbers to version strings.
662 template<int size
, bool big_endian
>
664 Sized_dynobj
<size
, big_endian
>::make_version_map(
665 Read_symbols_data
* sd
,
666 Version_map
* version_map
) const
668 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
671 // A guess at the maximum version number we will see. If this is
672 // wrong we will be less efficient but still correct.
673 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
675 this->make_verdef_map(sd
, version_map
);
676 this->make_verneed_map(sd
, version_map
);
679 // Add the dynamic symbols to the symbol table.
681 template<int size
, bool big_endian
>
683 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
684 Read_symbols_data
* sd
,
687 if (sd
->symbols
== NULL
)
689 gold_assert(sd
->symbol_names
== NULL
);
690 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
691 && sd
->verneed
== NULL
);
695 const int sym_size
= This::sym_size
;
696 const size_t symcount
= sd
->symbols_size
/ sym_size
;
697 gold_assert(sd
->external_symbols_offset
== 0);
698 if (symcount
* sym_size
!= sd
->symbols_size
)
700 this->error(_("size of dynamic symbols is not multiple of symbol size"));
704 Version_map version_map
;
705 this->make_version_map(sd
, &version_map
);
707 // If printing symbol counts or a cross reference table, we want to
709 if (parameters
->options().user_set_print_symbol_counts()
710 || parameters
->options().cref())
712 this->symbols_
= new Symbols();
713 this->symbols_
->resize(symcount
);
716 const char* sym_names
=
717 reinterpret_cast<const char*>(sd
->symbol_names
->data());
718 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
719 sym_names
, sd
->symbol_names_size
,
722 : sd
->versym
->data()),
726 &this->defined_count_
);
730 delete sd
->symbol_names
;
731 sd
->symbol_names
= NULL
;
732 if (sd
->versym
!= NULL
)
737 if (sd
->verdef
!= NULL
)
742 if (sd
->verneed
!= NULL
)
748 // This is normally the last time we will read any data from this
750 this->clear_view_cache_marks();
753 // Get symbol counts.
755 template<int size
, bool big_endian
>
757 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
762 *defined
= this->defined_count_
;
764 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
765 p
!= this->symbols_
->end();
768 && (*p
)->source() == Symbol::FROM_OBJECT
769 && (*p
)->object() == this
770 && (*p
)->is_defined()
771 && (*p
)->dynsym_index() != -1U)
776 // Given a vector of hash codes, compute the number of hash buckets to
780 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
781 bool for_gnu_hash_table
)
783 // FIXME: Implement optional hash table optimization.
785 // Array used to determine the number of hash table buckets to use
786 // based on the number of symbols there are. If there are fewer
787 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
788 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
789 // use more than 262147 buckets. This is straight from the old GNU
791 static const unsigned int buckets
[] =
793 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
794 16411, 32771, 65537, 131101, 262147
796 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
798 unsigned int symcount
= hashcodes
.size();
799 unsigned int ret
= 1;
800 const double full_fraction
801 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
802 for (int i
= 0; i
< buckets_count
; ++i
)
804 if (symcount
< buckets
[i
] * full_fraction
)
809 if (for_gnu_hash_table
&& ret
< 2)
815 // The standard ELF hash function. This hash function must not
816 // change, as the dynamic linker uses it also.
819 Dynobj::elf_hash(const char* name
)
821 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
824 while ((c
= *nameu
++) != '\0')
827 uint32_t g
= h
& 0xf0000000;
831 // The ELF ABI says h &= ~g, but using xor is equivalent in
832 // this case (since g was set from h) and may save one
840 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
841 // DYNSYMS is a vector with all the global dynamic symbols.
842 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
846 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
847 unsigned int local_dynsym_count
,
848 unsigned char** pphash
,
849 unsigned int* phashlen
)
851 unsigned int dynsym_count
= dynsyms
.size();
853 // Get the hash values for all the symbols.
854 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
855 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
856 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
858 const unsigned int bucketcount
=
859 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
861 std::vector
<uint32_t> bucket(bucketcount
);
862 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
864 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
866 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
867 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
868 chain
[dynsym_index
] = bucket
[bucketpos
];
869 bucket
[bucketpos
] = dynsym_index
;
872 unsigned int hashlen
= ((2
877 unsigned char* phash
= new unsigned char[hashlen
];
879 if (parameters
->target().is_big_endian())
881 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
882 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
890 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
891 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
902 // Fill in an ELF hash table.
904 template<bool big_endian
>
906 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
907 const std::vector
<uint32_t>& chain
,
908 unsigned char* phash
,
909 unsigned int hashlen
)
911 unsigned char* p
= phash
;
913 const unsigned int bucketcount
= bucket
.size();
914 const unsigned int chaincount
= chain
.size();
916 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
918 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
921 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
923 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
927 for (unsigned int i
= 0; i
< chaincount
; ++i
)
929 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
933 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
936 // The hash function used for the GNU hash table. This hash function
937 // must not change, as the dynamic linker uses it also.
940 Dynobj::gnu_hash(const char* name
)
942 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
945 while ((c
= *nameu
++) != '\0')
946 h
= (h
<< 5) + h
+ c
;
950 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
951 // tables are an extension to ELF which are recognized by the GNU
952 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
953 // TARGET is the target. DYNSYMS is a vector with all the global
954 // symbols which will be going into the dynamic symbol table.
955 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
959 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
960 unsigned int local_dynsym_count
,
961 unsigned char** pphash
,
962 unsigned int* phashlen
)
964 const unsigned int count
= dynsyms
.size();
966 // Sort the dynamic symbols into two vectors. Symbols which we do
967 // not want to put into the hash table we store into
968 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
969 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
970 // and records the hash codes.
972 std::vector
<Symbol
*> unhashed_dynsyms
;
973 unhashed_dynsyms
.reserve(count
);
975 std::vector
<Symbol
*> hashed_dynsyms
;
976 hashed_dynsyms
.reserve(count
);
978 std::vector
<uint32_t> dynsym_hashvals
;
979 dynsym_hashvals
.reserve(count
);
981 for (unsigned int i
= 0; i
< count
; ++i
)
983 Symbol
* sym
= dynsyms
[i
];
985 if (!sym
->needs_dynsym_value()
986 && (sym
->is_undefined()
987 || sym
->is_from_dynobj()
988 || sym
->is_forced_local()))
989 unhashed_dynsyms
.push_back(sym
);
992 hashed_dynsyms
.push_back(sym
);
993 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
997 // Put the unhashed symbols at the start of the global portion of
998 // the dynamic symbol table.
999 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
1000 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
1001 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
1003 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
1004 ++unhashed_dynsym_index
;
1007 // For the actual data generation we call out to a templatized
1009 int size
= parameters
->target().get_size();
1010 bool big_endian
= parameters
->target().is_big_endian();
1015 #ifdef HAVE_TARGET_32_BIG
1016 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
1018 unhashed_dynsym_index
,
1027 #ifdef HAVE_TARGET_32_LITTLE
1028 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
1030 unhashed_dynsym_index
,
1038 else if (size
== 64)
1042 #ifdef HAVE_TARGET_64_BIG
1043 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1045 unhashed_dynsym_index
,
1054 #ifdef HAVE_TARGET_64_LITTLE
1055 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1057 unhashed_dynsym_index
,
1069 // Create the actual data for a GNU hash table. This is just a copy
1070 // of the code from the old GNU linker.
1072 template<int size
, bool big_endian
>
1074 Dynobj::sized_create_gnu_hash_table(
1075 const std::vector
<Symbol
*>& hashed_dynsyms
,
1076 const std::vector
<uint32_t>& dynsym_hashvals
,
1077 unsigned int unhashed_dynsym_count
,
1078 unsigned char** pphash
,
1079 unsigned int* phashlen
)
1081 if (hashed_dynsyms
.empty())
1083 // Special case for the empty hash table.
1084 unsigned int hashlen
= 5 * 4 + size
/ 8;
1085 unsigned char* phash
= new unsigned char[hashlen
];
1086 // One empty bucket.
1087 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1088 // Symbol index above unhashed symbols.
1089 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1090 // One word for bitmask.
1091 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1092 // Only bloom filter.
1093 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1095 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1096 // No hashes in only bucket.
1097 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1099 *phashlen
= hashlen
;
1105 const unsigned int bucketcount
=
1106 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1108 const unsigned int nsyms
= hashed_dynsyms
.size();
1110 uint32_t maskbitslog2
= 1;
1111 uint32_t x
= nsyms
>> 1;
1117 if (maskbitslog2
< 3)
1119 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1129 if (maskbitslog2
== 5)
1133 uint32_t mask
= (1U << shift1
) - 1U;
1134 uint32_t shift2
= maskbitslog2
;
1135 uint32_t maskbits
= 1U << maskbitslog2
;
1136 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1138 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1139 std::vector
<Word
> bitmask(maskwords
);
1140 std::vector
<uint32_t> counts(bucketcount
);
1141 std::vector
<uint32_t> indx(bucketcount
);
1142 uint32_t symindx
= unhashed_dynsym_count
;
1144 // Count the number of times each hash bucket is used.
1145 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1146 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1148 unsigned int cnt
= symindx
;
1149 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1155 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1156 hashlen
+= maskbits
/ 8;
1157 unsigned char* phash
= new unsigned char[hashlen
];
1159 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1160 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1161 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1162 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1164 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1165 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1168 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1170 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1174 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1176 Symbol
* sym
= hashed_dynsyms
[i
];
1177 uint32_t hashval
= dynsym_hashvals
[i
];
1179 unsigned int bucket
= hashval
% bucketcount
;
1180 unsigned int val
= ((hashval
>> shift1
)
1181 & ((maskbits
>> shift1
) - 1));
1182 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1183 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1184 val
= hashval
& ~ 1U;
1185 if (counts
[bucket
] == 1)
1187 // Last element terminates the chain.
1190 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1194 sym
->set_dynsym_index(indx
[bucket
]);
1199 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1201 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1205 *phashlen
= hashlen
;
1211 // Write this definition to a buffer for the output section.
1213 template<int size
, bool big_endian
>
1215 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1217 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1218 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1220 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1221 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1222 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1223 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0));
1224 vd
.set_vd_ndx(this->index());
1225 vd
.set_vd_cnt(1 + this->deps_
.size());
1226 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1227 vd
.set_vd_aux(verdef_size
);
1228 vd
.set_vd_next(is_last
1230 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1233 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1234 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1235 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1238 Deps::const_iterator p
;
1240 for (p
= this->deps_
.begin(), i
= 0;
1241 p
!= this->deps_
.end();
1244 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1245 vda
.set_vda_name(dynpool
->get_offset(*p
));
1246 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1257 for (Need_versions::iterator p
= this->need_versions_
.begin();
1258 p
!= this->need_versions_
.end();
1263 // Add a new version to this file reference.
1266 Verneed::add_name(const char* name
)
1268 Verneed_version
* vv
= new Verneed_version(name
);
1269 this->need_versions_
.push_back(vv
);
1273 // Set the version indexes starting at INDEX.
1276 Verneed::finalize(unsigned int index
)
1278 for (Need_versions::iterator p
= this->need_versions_
.begin();
1279 p
!= this->need_versions_
.end();
1282 (*p
)->set_index(index
);
1288 // Write this list of referenced versions to a buffer for the output
1291 template<int size
, bool big_endian
>
1293 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1294 unsigned char* pb
) const
1296 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1297 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1299 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1300 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1301 vn
.set_vn_cnt(this->need_versions_
.size());
1302 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1303 vn
.set_vn_aux(verneed_size
);
1304 vn
.set_vn_next(is_last
1306 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1309 Need_versions::const_iterator p
;
1311 for (p
= this->need_versions_
.begin(), i
= 0;
1312 p
!= this->need_versions_
.end();
1315 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1316 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1317 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1318 vna
.set_vna_flags(0);
1319 vna
.set_vna_other((*p
)->index());
1320 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1321 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1330 // Versions methods.
1332 Versions::Versions(const Version_script_info
& version_script
,
1333 Stringpool
* dynpool
)
1334 : defs_(), needs_(), version_table_(),
1335 is_finalized_(false), version_script_(version_script
),
1336 needs_base_version_(parameters
->options().shared())
1338 if (!this->version_script_
.empty())
1340 // Parse the version script, and insert each declared version into
1341 // defs_ and version_table_.
1342 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1344 if (this->needs_base_version_
&& !versions
.empty())
1345 this->define_base_version(dynpool
);
1347 for (size_t k
= 0; k
< versions
.size(); ++k
)
1349 Stringpool::Key version_key
;
1350 const char* version
= dynpool
->add(versions
[k
].c_str(),
1351 true, &version_key
);
1352 Verdef
* const vd
= new Verdef(
1354 this->version_script_
.get_dependencies(version
),
1355 false, false, false);
1356 this->defs_
.push_back(vd
);
1357 Key
key(version_key
, 0);
1358 this->version_table_
.insert(std::make_pair(key
, vd
));
1363 Versions::~Versions()
1365 for (Defs::iterator p
= this->defs_
.begin();
1366 p
!= this->defs_
.end();
1370 for (Needs::iterator p
= this->needs_
.begin();
1371 p
!= this->needs_
.end();
1376 // Define the base version of a shared library. The base version definition
1377 // must be the first entry in defs_. We insert it lazily so that defs_ is
1378 // empty if no symbol versioning is used. Then layout can just drop the
1379 // version sections.
1382 Versions::define_base_version(Stringpool
* dynpool
)
1384 // If we do any versioning at all, we always need a base version, so
1385 // define that first. Nothing explicitly declares itself as part of base,
1386 // so it doesn't need to be in version_table_.
1387 gold_assert(this->defs_
.empty());
1388 const char* name
= parameters
->options().soname();
1390 name
= parameters
->options().output_file_name();
1391 name
= dynpool
->add(name
, false, NULL
);
1392 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1394 this->defs_
.push_back(vdbase
);
1395 this->needs_base_version_
= false;
1398 // Return the dynamic object which a symbol refers to.
1401 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1402 const Symbol
* sym
) const
1404 if (sym
->is_copied_from_dynobj())
1405 return symtab
->get_copy_source(sym
);
1408 Object
* object
= sym
->object();
1409 gold_assert(object
->is_dynamic());
1410 return static_cast<Dynobj
*>(object
);
1414 // Record version information for a symbol going into the dynamic
1418 Versions::record_version(const Symbol_table
* symtab
,
1419 Stringpool
* dynpool
, const Symbol
* sym
)
1421 gold_assert(!this->is_finalized_
);
1422 gold_assert(sym
->version() != NULL
);
1424 Stringpool::Key version_key
;
1425 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1427 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1429 if (parameters
->options().shared())
1430 this->add_def(sym
, version
, version_key
);
1434 // This is a version reference.
1435 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1436 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1440 // We've found a symbol SYM defined in version VERSION.
1443 Versions::add_def(const Symbol
* sym
, const char* version
,
1444 Stringpool::Key version_key
)
1446 Key
k(version_key
, 0);
1447 Version_base
* const vbnull
= NULL
;
1448 std::pair
<Version_table::iterator
, bool> ins
=
1449 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1453 // We already have an entry for this version.
1454 Version_base
* vb
= ins
.first
->second
;
1456 // We have now seen a symbol in this version, so it is not
1458 gold_assert(vb
!= NULL
);
1463 // If we are creating a shared object, it is an error to
1464 // find a definition of a symbol with a version which is not
1465 // in the version script.
1466 if (parameters
->options().shared())
1467 gold_error(_("symbol %s has undefined version %s"),
1468 sym
->demangled_name().c_str(), version
);
1470 // We only insert a base version for shared library.
1471 gold_assert(!this->needs_base_version_
);
1473 // When creating a regular executable, automatically define
1475 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1476 false, false, false);
1477 this->defs_
.push_back(vd
);
1478 ins
.first
->second
= vd
;
1482 // Add a reference to version NAME in file FILENAME.
1485 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1486 Stringpool::Key name_key
)
1488 Stringpool::Key filename_key
;
1489 filename
= dynpool
->add(filename
, true, &filename_key
);
1491 Key
k(name_key
, filename_key
);
1492 Version_base
* const vbnull
= NULL
;
1493 std::pair
<Version_table::iterator
, bool> ins
=
1494 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1498 // We already have an entry for this filename/version.
1502 // See whether we already have this filename. We don't expect many
1503 // version references, so we just do a linear search. This could be
1504 // replaced by a hash table.
1506 for (Needs::iterator p
= this->needs_
.begin();
1507 p
!= this->needs_
.end();
1510 if ((*p
)->filename() == filename
)
1519 // Create base version definition lazily for shared library.
1520 if (this->needs_base_version_
)
1521 this->define_base_version(dynpool
);
1523 // We have a new filename.
1524 vn
= new Verneed(filename
);
1525 this->needs_
.push_back(vn
);
1528 ins
.first
->second
= vn
->add_name(name
);
1531 // Set the version indexes. Create a new dynamic version symbol for
1532 // each new version definition.
1535 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1536 std::vector
<Symbol
*>* syms
)
1538 gold_assert(!this->is_finalized_
);
1540 unsigned int vi
= 1;
1542 for (Defs::iterator p
= this->defs_
.begin();
1543 p
!= this->defs_
.end();
1546 (*p
)->set_index(vi
);
1549 // Create a version symbol if necessary.
1550 if (!(*p
)->is_symbol_created())
1552 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1554 Symbol_table::PREDEFINED
,
1558 elfcpp::STV_DEFAULT
, 0,
1560 vsym
->set_needs_dynsym_entry();
1561 vsym
->set_dynsym_index(dynsym_index
);
1563 syms
->push_back(vsym
);
1564 // The name is already in the dynamic pool.
1568 // Index 1 is used for global symbols.
1571 gold_assert(this->defs_
.empty());
1575 for (Needs::iterator p
= this->needs_
.begin();
1576 p
!= this->needs_
.end();
1578 vi
= (*p
)->finalize(vi
);
1580 this->is_finalized_
= true;
1582 return dynsym_index
;
1585 // Return the version index to use for a symbol. This does two hash
1586 // table lookups: one in DYNPOOL and one in this->version_table_.
1587 // Another approach alternative would be store a pointer in SYM, which
1588 // would increase the size of the symbol table. Or perhaps we could
1589 // use a hash table from dynamic symbol pointer values to Version_base
1593 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1594 const Symbol
* sym
) const
1596 Stringpool::Key version_key
;
1597 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1598 gold_assert(version
!= NULL
);
1601 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1603 if (!parameters
->options().shared())
1604 return elfcpp::VER_NDX_GLOBAL
;
1605 k
= Key(version_key
, 0);
1609 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1611 Stringpool::Key filename_key
;
1612 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1613 gold_assert(filename
!= NULL
);
1615 k
= Key(version_key
, filename_key
);
1618 Version_table::const_iterator p
= this->version_table_
.find(k
);
1619 gold_assert(p
!= this->version_table_
.end());
1621 return p
->second
->index();
1624 // Return an allocated buffer holding the contents of the symbol
1627 template<int size
, bool big_endian
>
1629 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1630 const Stringpool
* dynpool
,
1631 unsigned int local_symcount
,
1632 const std::vector
<Symbol
*>& syms
,
1634 unsigned int* psize
) const
1636 gold_assert(this->is_finalized_
);
1638 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1639 unsigned char* pbuf
= new unsigned char[sz
];
1641 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1642 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1643 elfcpp::VER_NDX_LOCAL
);
1645 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1649 unsigned int version_index
;
1650 const char* version
= (*p
)->version();
1651 if (version
== NULL
)
1652 version_index
= elfcpp::VER_NDX_GLOBAL
;
1654 version_index
= this->version_index(symtab
, dynpool
, *p
);
1655 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1657 if ((*p
)->version() != NULL
&& !(*p
)->is_default())
1658 version_index
|= elfcpp::VERSYM_HIDDEN
;
1659 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1667 // Return an allocated buffer holding the contents of the version
1668 // definition section.
1670 template<int size
, bool big_endian
>
1672 Versions::def_section_contents(const Stringpool
* dynpool
,
1673 unsigned char** pp
, unsigned int* psize
,
1674 unsigned int* pentries
) const
1676 gold_assert(this->is_finalized_
);
1677 gold_assert(!this->defs_
.empty());
1679 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1680 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1682 unsigned int sz
= 0;
1683 for (Defs::const_iterator p
= this->defs_
.begin();
1684 p
!= this->defs_
.end();
1687 sz
+= verdef_size
+ verdaux_size
;
1688 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1691 unsigned char* pbuf
= new unsigned char[sz
];
1693 unsigned char* pb
= pbuf
;
1694 Defs::const_iterator p
;
1696 for (p
= this->defs_
.begin(), i
= 0;
1697 p
!= this->defs_
.end();
1699 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1700 i
+ 1 >= this->defs_
.size(),
1703 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1707 *pentries
= this->defs_
.size();
1710 // Return an allocated buffer holding the contents of the version
1711 // reference section.
1713 template<int size
, bool big_endian
>
1715 Versions::need_section_contents(const Stringpool
* dynpool
,
1716 unsigned char** pp
, unsigned int *psize
,
1717 unsigned int *pentries
) const
1719 gold_assert(this->is_finalized_
);
1720 gold_assert(!this->needs_
.empty());
1722 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1723 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1725 unsigned int sz
= 0;
1726 for (Needs::const_iterator p
= this->needs_
.begin();
1727 p
!= this->needs_
.end();
1731 sz
+= (*p
)->count_versions() * vernaux_size
;
1734 unsigned char* pbuf
= new unsigned char[sz
];
1736 unsigned char* pb
= pbuf
;
1737 Needs::const_iterator p
;
1739 for (p
= this->needs_
.begin(), i
= 0;
1740 p
!= this->needs_
.end();
1742 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1743 i
+ 1 >= this->needs_
.size(),
1746 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1750 *pentries
= this->needs_
.size();
1753 // Instantiate the templates we need. We could use the configure
1754 // script to restrict this to only the ones for implemented targets.
1756 #ifdef HAVE_TARGET_32_LITTLE
1758 class Sized_dynobj
<32, false>;
1761 #ifdef HAVE_TARGET_32_BIG
1763 class Sized_dynobj
<32, true>;
1766 #ifdef HAVE_TARGET_64_LITTLE
1768 class Sized_dynobj
<64, false>;
1771 #ifdef HAVE_TARGET_64_BIG
1773 class Sized_dynobj
<64, true>;
1776 #ifdef HAVE_TARGET_32_LITTLE
1779 Versions::symbol_section_contents
<32, false>(
1780 const Symbol_table
*,
1783 const std::vector
<Symbol
*>&,
1785 unsigned int*) const;
1788 #ifdef HAVE_TARGET_32_BIG
1791 Versions::symbol_section_contents
<32, true>(
1792 const Symbol_table
*,
1795 const std::vector
<Symbol
*>&,
1797 unsigned int*) const;
1800 #ifdef HAVE_TARGET_64_LITTLE
1803 Versions::symbol_section_contents
<64, false>(
1804 const Symbol_table
*,
1807 const std::vector
<Symbol
*>&,
1809 unsigned int*) const;
1812 #ifdef HAVE_TARGET_64_BIG
1815 Versions::symbol_section_contents
<64, true>(
1816 const Symbol_table
*,
1819 const std::vector
<Symbol
*>&,
1821 unsigned int*) const;
1824 #ifdef HAVE_TARGET_32_LITTLE
1827 Versions::def_section_contents
<32, false>(
1831 unsigned int*) const;
1834 #ifdef HAVE_TARGET_32_BIG
1837 Versions::def_section_contents
<32, true>(
1841 unsigned int*) const;
1844 #ifdef HAVE_TARGET_64_LITTLE
1847 Versions::def_section_contents
<64, false>(
1851 unsigned int*) const;
1854 #ifdef HAVE_TARGET_64_BIG
1857 Versions::def_section_contents
<64, true>(
1861 unsigned int*) const;
1864 #ifdef HAVE_TARGET_32_LITTLE
1867 Versions::need_section_contents
<32, false>(
1871 unsigned int*) const;
1874 #ifdef HAVE_TARGET_32_BIG
1877 Versions::need_section_contents
<32, true>(
1881 unsigned int*) const;
1884 #ifdef HAVE_TARGET_64_LITTLE
1887 Versions::need_section_contents
<64, false>(
1891 unsigned int*) const;
1894 #ifdef HAVE_TARGET_64_BIG
1897 Versions::need_section_contents
<64, true>(
1901 unsigned int*) const;
1904 } // End namespace gold.