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
:
136 case elfcpp::SHT_GNU_versym
:
139 case elfcpp::SHT_GNU_verdef
:
142 case elfcpp::SHT_GNU_verneed
:
145 case elfcpp::SHT_DYNAMIC
:
148 case elfcpp::SHT_SYMTAB_SHNDX
:
150 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
151 if (xindex_link
== this->dynsym_shndx_
)
153 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
154 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
156 this->set_xindex(xindex
);
169 this->error(_("unexpected duplicate type %u section: %u, %u"),
170 shdr
.get_sh_type(), *pi
, i
);
175 // If there is no dynamic symbol table, use the normal symbol table.
176 // On some SVR4 systems, a shared library is stored in an archive.
177 // The version stored in the archive only has a normal symbol table.
178 // It has an SONAME entry which points to another copy in the file
179 // system which has a dynamic symbol table as usual. This is way of
180 // addressing the issues which glibc addresses using GROUP with
182 if (this->dynsym_shndx_
== -1U && symtab_shndx
!= 0)
184 this->dynsym_shndx_
= symtab_shndx
;
185 if (xindex_shndx
> 0 && xindex_link
== symtab_shndx
)
187 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
188 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
190 this->set_xindex(xindex
);
195 // Read the contents of section SHNDX. PSHDRS points to the section
196 // headers. TYPE is the expected section type. LINK is the expected
197 // section link. Store the data in *VIEW and *VIEW_SIZE. The
198 // section's sh_info field is stored in *VIEW_INFO.
200 template<int size
, bool big_endian
>
202 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
203 const unsigned char* pshdrs
,
208 section_size_type
* view_size
,
209 unsigned int* view_info
)
219 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
221 gold_assert(shdr
.get_sh_type() == type
);
223 if (this->adjust_shndx(shdr
.get_sh_link()) != link
)
224 this->error(_("unexpected link in section %u header: %u != %u"),
225 shndx
, this->adjust_shndx(shdr
.get_sh_link()), link
);
227 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size(),
229 *view_size
= convert_to_section_size_type(shdr
.get_sh_size());
230 *view_info
= shdr
.get_sh_info();
233 // Read the dynamic tags. Set the soname field if this shared object
234 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
235 // the section headers. DYNAMIC_SHNDX is the section index of the
236 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
237 // section index and contents of a string table which may be the one
238 // associated with the SHT_DYNAMIC section.
240 template<int size
, bool big_endian
>
242 Sized_dynobj
<size
, big_endian
>::read_dynamic(const unsigned char* pshdrs
,
243 unsigned int dynamic_shndx
,
244 unsigned int strtab_shndx
,
245 const unsigned char* strtabu
,
248 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
249 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
251 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
252 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
253 dynamic_size
, true, false);
255 const unsigned int link
= this->adjust_shndx(dynamicshdr
.get_sh_link());
256 if (link
!= strtab_shndx
)
258 if (link
>= this->shnum())
260 this->error(_("DYNAMIC section %u link out of range: %u"),
261 dynamic_shndx
, link
);
265 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
266 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
268 this->error(_("DYNAMIC section %u link %u is not a strtab"),
269 dynamic_shndx
, link
);
273 strtab_size
= strtabshdr
.get_sh_size();
274 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
, false,
278 const char* const strtab
= reinterpret_cast<const char*>(strtabu
);
280 for (const unsigned char* p
= pdynamic
;
281 p
< pdynamic
+ dynamic_size
;
284 typename
This::Dyn
dyn(p
);
286 switch (dyn
.get_d_tag())
288 case elfcpp::DT_NULL
:
289 // We should always see DT_NULL at the end of the dynamic
293 case elfcpp::DT_SONAME
:
295 off_t val
= dyn
.get_d_val();
296 if (val
>= strtab_size
)
297 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
298 static_cast<long long>(val
),
299 static_cast<long long>(strtab_size
));
301 this->set_soname_string(strtab
+ val
);
305 case elfcpp::DT_NEEDED
:
307 off_t val
= dyn
.get_d_val();
308 if (val
>= strtab_size
)
309 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
310 static_cast<long long>(val
),
311 static_cast<long long>(strtab_size
));
313 this->add_needed(strtab
+ val
);
322 this->error(_("missing DT_NULL in dynamic segment"));
325 // Read the symbols and sections from a dynamic object. We read the
326 // dynamic symbols, not the normal symbols.
328 template<int size
, bool big_endian
>
330 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
332 this->read_section_data(&this->elf_file_
, sd
);
334 const unsigned char* const pshdrs
= sd
->section_headers
->data();
336 unsigned int versym_shndx
;
337 unsigned int verdef_shndx
;
338 unsigned int verneed_shndx
;
339 unsigned int dynamic_shndx
;
340 this->find_dynsym_sections(pshdrs
, &versym_shndx
, &verdef_shndx
,
341 &verneed_shndx
, &dynamic_shndx
);
343 unsigned int strtab_shndx
= -1U;
346 sd
->symbols_size
= 0;
347 sd
->external_symbols_offset
= 0;
348 sd
->symbol_names
= NULL
;
349 sd
->symbol_names_size
= 0;
356 sd
->verneed_size
= 0;
357 sd
->verneed_info
= 0;
359 if (this->dynsym_shndx_
!= -1U)
361 // Get the dynamic symbols.
362 typename
This::Shdr
dynsymshdr(pshdrs
363 + this->dynsym_shndx_
* This::shdr_size
);
365 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
366 dynsymshdr
.get_sh_size(), true,
369 convert_to_section_size_type(dynsymshdr
.get_sh_size());
371 // Get the symbol names.
372 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
373 if (strtab_shndx
>= this->shnum())
375 this->error(_("invalid dynamic symbol table name index: %u"),
379 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
380 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
382 this->error(_("dynamic symbol table name section "
383 "has wrong type: %u"),
384 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
388 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
389 strtabshdr
.get_sh_size(),
391 sd
->symbol_names_size
=
392 convert_to_section_size_type(strtabshdr
.get_sh_size());
394 // Get the version information.
397 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
399 &sd
->versym
, &sd
->versym_size
, &dummy
);
401 // We require that the version definition and need section link
402 // to the same string table as the dynamic symbol table. This
403 // is not a technical requirement, but it always happens in
404 // practice. We could change this if necessary.
406 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
407 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
410 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
411 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
415 // Read the SHT_DYNAMIC section to find whether this shared object
416 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
417 // doesn't really have anything to do with reading the symbols, but
418 // this is a convenient place to do it.
419 if (dynamic_shndx
!= -1U)
420 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
421 (sd
->symbol_names
== NULL
423 : sd
->symbol_names
->data()),
424 sd
->symbol_names_size
);
427 // Return the Xindex structure to use for object with lots of
430 template<int size
, bool big_endian
>
432 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
434 gold_assert(this->dynsym_shndx_
!= -1U);
435 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
436 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
440 // Lay out the input sections for a dynamic object. We don't want to
441 // include sections from a dynamic object, so all that we actually do
442 // here is check for .gnu.warning and .note.GNU-split-stack sections.
444 template<int size
, bool big_endian
>
446 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
448 Read_symbols_data
* sd
)
450 const unsigned int shnum
= this->shnum();
454 // Get the section headers.
455 const unsigned char* pshdrs
= sd
->section_headers
->data();
457 // Get the section names.
458 const unsigned char* pnamesu
= sd
->section_names
->data();
459 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
461 // Skip the first, dummy, section.
462 pshdrs
+= This::shdr_size
;
463 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
465 typename
This::Shdr
shdr(pshdrs
);
467 if (shdr
.get_sh_name() >= sd
->section_names_size
)
469 this->error(_("bad section name offset for section %u: %lu"),
470 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
474 const char* name
= pnames
+ shdr
.get_sh_name();
476 this->handle_gnu_warning_section(name
, i
, symtab
);
477 this->handle_split_stack_section(name
);
480 delete sd
->section_headers
;
481 sd
->section_headers
= NULL
;
482 delete sd
->section_names
;
483 sd
->section_names
= NULL
;
486 // Add an entry to the vector mapping version numbers to version
489 template<int size
, bool big_endian
>
491 Sized_dynobj
<size
, big_endian
>::set_version_map(
492 Version_map
* version_map
,
494 const char* name
) const
496 if (ndx
>= version_map
->size())
497 version_map
->resize(ndx
+ 1);
498 if ((*version_map
)[ndx
] != NULL
)
499 this->error(_("duplicate definition for version %u"), ndx
);
500 (*version_map
)[ndx
] = name
;
503 // Add mappings for the version definitions to VERSION_MAP.
505 template<int size
, bool big_endian
>
507 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
508 Read_symbols_data
* sd
,
509 Version_map
* version_map
) const
511 if (sd
->verdef
== NULL
)
514 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
515 section_size_type names_size
= sd
->symbol_names_size
;
517 const unsigned char* pverdef
= sd
->verdef
->data();
518 section_size_type verdef_size
= sd
->verdef_size
;
519 const unsigned int count
= sd
->verdef_info
;
521 const unsigned char* p
= pverdef
;
522 for (unsigned int i
= 0; i
< count
; ++i
)
524 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
526 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
528 this->error(_("unexpected verdef version %u"),
529 verdef
.get_vd_version());
533 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
535 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
538 // The first Verdaux holds the name of this version. Subsequent
539 // ones are versions that this one depends upon, which we don't
541 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
544 this->error(_("verdef vd_cnt field too small: %u"),
545 static_cast<unsigned int>(vd_cnt
));
549 const section_size_type vd_aux
= verdef
.get_vd_aux();
550 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
552 this->error(_("verdef vd_aux field out of range: %u"),
553 static_cast<unsigned int>(vd_aux
));
557 const unsigned char* pvda
= p
+ vd_aux
;
558 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
560 const section_size_type vda_name
= verdaux
.get_vda_name();
561 if (vda_name
>= names_size
)
563 this->error(_("verdaux vda_name field out of range: %u"),
564 static_cast<unsigned int>(vda_name
));
568 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
570 const section_size_type vd_next
= verdef
.get_vd_next();
571 if ((p
- pverdef
) + vd_next
>= verdef_size
)
573 this->error(_("verdef vd_next field out of range: %u"),
574 static_cast<unsigned int>(vd_next
));
582 // Add mappings for the required versions to VERSION_MAP.
584 template<int size
, bool big_endian
>
586 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
587 Read_symbols_data
* sd
,
588 Version_map
* version_map
) const
590 if (sd
->verneed
== NULL
)
593 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
594 section_size_type names_size
= sd
->symbol_names_size
;
596 const unsigned char* pverneed
= sd
->verneed
->data();
597 const section_size_type verneed_size
= sd
->verneed_size
;
598 const unsigned int count
= sd
->verneed_info
;
600 const unsigned char* p
= pverneed
;
601 for (unsigned int i
= 0; i
< count
; ++i
)
603 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
605 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
607 this->error(_("unexpected verneed version %u"),
608 verneed
.get_vn_version());
612 const section_size_type vn_aux
= verneed
.get_vn_aux();
614 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
616 this->error(_("verneed vn_aux field out of range: %u"),
617 static_cast<unsigned int>(vn_aux
));
621 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
622 const unsigned char* pvna
= p
+ vn_aux
;
623 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
625 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
627 const unsigned int vna_name
= vernaux
.get_vna_name();
628 if (vna_name
>= names_size
)
630 this->error(_("vernaux vna_name field out of range: %u"),
631 static_cast<unsigned int>(vna_name
));
635 this->set_version_map(version_map
, vernaux
.get_vna_other(),
638 const section_size_type vna_next
= vernaux
.get_vna_next();
639 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
641 this->error(_("verneed vna_next field out of range: %u"),
642 static_cast<unsigned int>(vna_next
));
649 const section_size_type vn_next
= verneed
.get_vn_next();
650 if ((p
- pverneed
) + vn_next
>= verneed_size
)
652 this->error(_("verneed vn_next field out of range: %u"),
653 static_cast<unsigned int>(vn_next
));
661 // Create a vector mapping version numbers to version strings.
663 template<int size
, bool big_endian
>
665 Sized_dynobj
<size
, big_endian
>::make_version_map(
666 Read_symbols_data
* sd
,
667 Version_map
* version_map
) const
669 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
672 // A guess at the maximum version number we will see. If this is
673 // wrong we will be less efficient but still correct.
674 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
676 this->make_verdef_map(sd
, version_map
);
677 this->make_verneed_map(sd
, version_map
);
680 // Add the dynamic symbols to the symbol table.
682 template<int size
, bool big_endian
>
684 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
685 Read_symbols_data
* sd
,
688 if (sd
->symbols
== NULL
)
690 gold_assert(sd
->symbol_names
== NULL
);
691 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
692 && sd
->verneed
== NULL
);
696 const int sym_size
= This::sym_size
;
697 const size_t symcount
= sd
->symbols_size
/ sym_size
;
698 gold_assert(sd
->external_symbols_offset
== 0);
699 if (symcount
* sym_size
!= sd
->symbols_size
)
701 this->error(_("size of dynamic symbols is not multiple of symbol size"));
705 Version_map version_map
;
706 this->make_version_map(sd
, &version_map
);
708 // If printing symbol counts or a cross reference table, we want to
710 if (parameters
->options().user_set_print_symbol_counts()
711 || parameters
->options().cref())
713 this->symbols_
= new Symbols();
714 this->symbols_
->resize(symcount
);
717 const char* sym_names
=
718 reinterpret_cast<const char*>(sd
->symbol_names
->data());
719 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
720 sym_names
, sd
->symbol_names_size
,
723 : sd
->versym
->data()),
727 &this->defined_count_
);
731 delete sd
->symbol_names
;
732 sd
->symbol_names
= NULL
;
733 if (sd
->versym
!= NULL
)
738 if (sd
->verdef
!= NULL
)
743 if (sd
->verneed
!= NULL
)
749 // This is normally the last time we will read any data from this
751 this->clear_view_cache_marks();
754 template<int size
, bool big_endian
>
755 Archive::Should_include
756 Sized_dynobj
<size
, big_endian
>::do_should_include_member(Symbol_table
*,
761 return Archive::SHOULD_INCLUDE_YES
;
764 // Iterate over global symbols, calling a visitor class V for each.
766 template<int size
, bool big_endian
>
768 Sized_dynobj
<size
, big_endian
>::do_for_all_global_symbols(
769 Read_symbols_data
* sd
,
770 Library_base::Symbol_visitor_base
* v
)
772 const char* sym_names
=
773 reinterpret_cast<const char*>(sd
->symbol_names
->data());
774 const unsigned char* syms
=
775 sd
->symbols
->data() + sd
->external_symbols_offset
;
776 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
777 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
779 const unsigned char* p
= syms
;
781 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
783 elfcpp::Sym
<size
, big_endian
> sym(p
);
784 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
785 && sym
.get_st_bind() != elfcpp::STB_LOCAL
)
786 v
->visit(sym_names
+ sym
.get_st_name());
790 // Get symbol counts.
792 template<int size
, bool big_endian
>
794 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
799 *defined
= this->defined_count_
;
801 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
802 p
!= this->symbols_
->end();
805 && (*p
)->source() == Symbol::FROM_OBJECT
806 && (*p
)->object() == this
807 && (*p
)->is_defined()
808 && (*p
)->dynsym_index() != -1U)
813 // Given a vector of hash codes, compute the number of hash buckets to
817 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
818 bool for_gnu_hash_table
)
820 // FIXME: Implement optional hash table optimization.
822 // Array used to determine the number of hash table buckets to use
823 // based on the number of symbols there are. If there are fewer
824 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
825 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
826 // use more than 262147 buckets. This is straight from the old GNU
828 static const unsigned int buckets
[] =
830 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
831 16411, 32771, 65537, 131101, 262147
833 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
835 unsigned int symcount
= hashcodes
.size();
836 unsigned int ret
= 1;
837 const double full_fraction
838 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
839 for (int i
= 0; i
< buckets_count
; ++i
)
841 if (symcount
< buckets
[i
] * full_fraction
)
846 if (for_gnu_hash_table
&& ret
< 2)
852 // The standard ELF hash function. This hash function must not
853 // change, as the dynamic linker uses it also.
856 Dynobj::elf_hash(const char* name
)
858 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
861 while ((c
= *nameu
++) != '\0')
864 uint32_t g
= h
& 0xf0000000;
868 // The ELF ABI says h &= ~g, but using xor is equivalent in
869 // this case (since g was set from h) and may save one
877 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
878 // DYNSYMS is a vector with all the global dynamic symbols.
879 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
883 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
884 unsigned int local_dynsym_count
,
885 unsigned char** pphash
,
886 unsigned int* phashlen
)
888 unsigned int dynsym_count
= dynsyms
.size();
890 // Get the hash values for all the symbols.
891 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
892 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
893 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
895 const unsigned int bucketcount
=
896 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
898 std::vector
<uint32_t> bucket(bucketcount
);
899 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
901 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
903 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
904 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
905 chain
[dynsym_index
] = bucket
[bucketpos
];
906 bucket
[bucketpos
] = dynsym_index
;
909 unsigned int hashlen
= ((2
914 unsigned char* phash
= new unsigned char[hashlen
];
916 if (parameters
->target().is_big_endian())
918 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
919 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
927 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
928 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
939 // Fill in an ELF hash table.
941 template<bool big_endian
>
943 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
944 const std::vector
<uint32_t>& chain
,
945 unsigned char* phash
,
946 unsigned int hashlen
)
948 unsigned char* p
= phash
;
950 const unsigned int bucketcount
= bucket
.size();
951 const unsigned int chaincount
= chain
.size();
953 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
955 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
958 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
960 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
964 for (unsigned int i
= 0; i
< chaincount
; ++i
)
966 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
970 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
973 // The hash function used for the GNU hash table. This hash function
974 // must not change, as the dynamic linker uses it also.
977 Dynobj::gnu_hash(const char* name
)
979 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
982 while ((c
= *nameu
++) != '\0')
983 h
= (h
<< 5) + h
+ c
;
987 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
988 // tables are an extension to ELF which are recognized by the GNU
989 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
990 // TARGET is the target. DYNSYMS is a vector with all the global
991 // symbols which will be going into the dynamic symbol table.
992 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
996 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
997 unsigned int local_dynsym_count
,
998 unsigned char** pphash
,
999 unsigned int* phashlen
)
1001 const unsigned int count
= dynsyms
.size();
1003 // Sort the dynamic symbols into two vectors. Symbols which we do
1004 // not want to put into the hash table we store into
1005 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
1006 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
1007 // and records the hash codes.
1009 std::vector
<Symbol
*> unhashed_dynsyms
;
1010 unhashed_dynsyms
.reserve(count
);
1012 std::vector
<Symbol
*> hashed_dynsyms
;
1013 hashed_dynsyms
.reserve(count
);
1015 std::vector
<uint32_t> dynsym_hashvals
;
1016 dynsym_hashvals
.reserve(count
);
1018 for (unsigned int i
= 0; i
< count
; ++i
)
1020 Symbol
* sym
= dynsyms
[i
];
1022 if (!sym
->needs_dynsym_value()
1023 && (sym
->is_undefined()
1024 || sym
->is_from_dynobj()
1025 || sym
->is_forced_local()))
1026 unhashed_dynsyms
.push_back(sym
);
1029 hashed_dynsyms
.push_back(sym
);
1030 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
1034 // Put the unhashed symbols at the start of the global portion of
1035 // the dynamic symbol table.
1036 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
1037 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
1038 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
1040 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
1041 ++unhashed_dynsym_index
;
1044 // For the actual data generation we call out to a templatized
1046 int size
= parameters
->target().get_size();
1047 bool big_endian
= parameters
->target().is_big_endian();
1052 #ifdef HAVE_TARGET_32_BIG
1053 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
1055 unhashed_dynsym_index
,
1064 #ifdef HAVE_TARGET_32_LITTLE
1065 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
1067 unhashed_dynsym_index
,
1075 else if (size
== 64)
1079 #ifdef HAVE_TARGET_64_BIG
1080 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1082 unhashed_dynsym_index
,
1091 #ifdef HAVE_TARGET_64_LITTLE
1092 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1094 unhashed_dynsym_index
,
1106 // Create the actual data for a GNU hash table. This is just a copy
1107 // of the code from the old GNU linker.
1109 template<int size
, bool big_endian
>
1111 Dynobj::sized_create_gnu_hash_table(
1112 const std::vector
<Symbol
*>& hashed_dynsyms
,
1113 const std::vector
<uint32_t>& dynsym_hashvals
,
1114 unsigned int unhashed_dynsym_count
,
1115 unsigned char** pphash
,
1116 unsigned int* phashlen
)
1118 if (hashed_dynsyms
.empty())
1120 // Special case for the empty hash table.
1121 unsigned int hashlen
= 5 * 4 + size
/ 8;
1122 unsigned char* phash
= new unsigned char[hashlen
];
1123 // One empty bucket.
1124 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1125 // Symbol index above unhashed symbols.
1126 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1127 // One word for bitmask.
1128 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1129 // Only bloom filter.
1130 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1132 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1133 // No hashes in only bucket.
1134 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1136 *phashlen
= hashlen
;
1142 const unsigned int bucketcount
=
1143 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1145 const unsigned int nsyms
= hashed_dynsyms
.size();
1147 uint32_t maskbitslog2
= 1;
1148 uint32_t x
= nsyms
>> 1;
1154 if (maskbitslog2
< 3)
1156 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1166 if (maskbitslog2
== 5)
1170 uint32_t mask
= (1U << shift1
) - 1U;
1171 uint32_t shift2
= maskbitslog2
;
1172 uint32_t maskbits
= 1U << maskbitslog2
;
1173 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1175 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1176 std::vector
<Word
> bitmask(maskwords
);
1177 std::vector
<uint32_t> counts(bucketcount
);
1178 std::vector
<uint32_t> indx(bucketcount
);
1179 uint32_t symindx
= unhashed_dynsym_count
;
1181 // Count the number of times each hash bucket is used.
1182 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1183 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1185 unsigned int cnt
= symindx
;
1186 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1192 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1193 hashlen
+= maskbits
/ 8;
1194 unsigned char* phash
= new unsigned char[hashlen
];
1196 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1197 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1198 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1199 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1201 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1202 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1205 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1207 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1211 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1213 Symbol
* sym
= hashed_dynsyms
[i
];
1214 uint32_t hashval
= dynsym_hashvals
[i
];
1216 unsigned int bucket
= hashval
% bucketcount
;
1217 unsigned int val
= ((hashval
>> shift1
)
1218 & ((maskbits
>> shift1
) - 1));
1219 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1220 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1221 val
= hashval
& ~ 1U;
1222 if (counts
[bucket
] == 1)
1224 // Last element terminates the chain.
1227 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1231 sym
->set_dynsym_index(indx
[bucket
]);
1236 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1238 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1242 *phashlen
= hashlen
;
1248 // Write this definition to a buffer for the output section.
1250 template<int size
, bool big_endian
>
1252 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1254 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1255 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1257 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1258 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1259 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1260 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0)
1261 | (this->is_info_
? elfcpp::VER_FLG_INFO
: 0));
1262 vd
.set_vd_ndx(this->index());
1263 vd
.set_vd_cnt(1 + this->deps_
.size());
1264 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1265 vd
.set_vd_aux(verdef_size
);
1266 vd
.set_vd_next(is_last
1268 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1271 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1272 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1273 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1276 Deps::const_iterator p
;
1278 for (p
= this->deps_
.begin(), i
= 0;
1279 p
!= this->deps_
.end();
1282 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1283 vda
.set_vda_name(dynpool
->get_offset(*p
));
1284 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1295 for (Need_versions::iterator p
= this->need_versions_
.begin();
1296 p
!= this->need_versions_
.end();
1301 // Add a new version to this file reference.
1304 Verneed::add_name(const char* name
)
1306 Verneed_version
* vv
= new Verneed_version(name
);
1307 this->need_versions_
.push_back(vv
);
1311 // Set the version indexes starting at INDEX.
1314 Verneed::finalize(unsigned int index
)
1316 for (Need_versions::iterator p
= this->need_versions_
.begin();
1317 p
!= this->need_versions_
.end();
1320 (*p
)->set_index(index
);
1326 // Write this list of referenced versions to a buffer for the output
1329 template<int size
, bool big_endian
>
1331 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1332 unsigned char* pb
) const
1334 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1335 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1337 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1338 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1339 vn
.set_vn_cnt(this->need_versions_
.size());
1340 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1341 vn
.set_vn_aux(verneed_size
);
1342 vn
.set_vn_next(is_last
1344 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1347 Need_versions::const_iterator p
;
1349 for (p
= this->need_versions_
.begin(), i
= 0;
1350 p
!= this->need_versions_
.end();
1353 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1354 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1355 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1356 vna
.set_vna_flags(0);
1357 vna
.set_vna_other((*p
)->index());
1358 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1359 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1368 // Versions methods.
1370 Versions::Versions(const Version_script_info
& version_script
,
1371 Stringpool
* dynpool
)
1372 : defs_(), needs_(), version_table_(),
1373 is_finalized_(false), version_script_(version_script
),
1374 needs_base_version_(parameters
->options().shared())
1376 if (!this->version_script_
.empty())
1378 // Parse the version script, and insert each declared version into
1379 // defs_ and version_table_.
1380 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1382 if (this->needs_base_version_
&& !versions
.empty())
1383 this->define_base_version(dynpool
);
1385 for (size_t k
= 0; k
< versions
.size(); ++k
)
1387 Stringpool::Key version_key
;
1388 const char* version
= dynpool
->add(versions
[k
].c_str(),
1389 true, &version_key
);
1390 Verdef
* const vd
= new Verdef(
1392 this->version_script_
.get_dependencies(version
),
1393 false, false, false, false);
1394 this->defs_
.push_back(vd
);
1395 Key
key(version_key
, 0);
1396 this->version_table_
.insert(std::make_pair(key
, vd
));
1401 Versions::~Versions()
1403 for (Defs::iterator p
= this->defs_
.begin();
1404 p
!= this->defs_
.end();
1408 for (Needs::iterator p
= this->needs_
.begin();
1409 p
!= this->needs_
.end();
1414 // Define the base version of a shared library. The base version definition
1415 // must be the first entry in defs_. We insert it lazily so that defs_ is
1416 // empty if no symbol versioning is used. Then layout can just drop the
1417 // version sections.
1420 Versions::define_base_version(Stringpool
* dynpool
)
1422 // If we do any versioning at all, we always need a base version, so
1423 // define that first. Nothing explicitly declares itself as part of base,
1424 // so it doesn't need to be in version_table_.
1425 gold_assert(this->defs_
.empty());
1426 const char* name
= parameters
->options().soname();
1428 name
= parameters
->options().output_file_name();
1429 name
= dynpool
->add(name
, false, NULL
);
1430 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1431 true, false, false, true);
1432 this->defs_
.push_back(vdbase
);
1433 this->needs_base_version_
= false;
1436 // Return the dynamic object which a symbol refers to.
1439 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1440 const Symbol
* sym
) const
1442 if (sym
->is_copied_from_dynobj())
1443 return symtab
->get_copy_source(sym
);
1446 Object
* object
= sym
->object();
1447 gold_assert(object
->is_dynamic());
1448 return static_cast<Dynobj
*>(object
);
1452 // Record version information for a symbol going into the dynamic
1456 Versions::record_version(const Symbol_table
* symtab
,
1457 Stringpool
* dynpool
, const Symbol
* sym
)
1459 gold_assert(!this->is_finalized_
);
1460 gold_assert(sym
->version() != NULL
);
1462 Stringpool::Key version_key
;
1463 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1465 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1467 if (parameters
->options().shared())
1468 this->add_def(sym
, version
, version_key
);
1472 // This is a version reference.
1473 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1474 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1478 // We've found a symbol SYM defined in version VERSION.
1481 Versions::add_def(const Symbol
* sym
, const char* version
,
1482 Stringpool::Key version_key
)
1484 Key
k(version_key
, 0);
1485 Version_base
* const vbnull
= NULL
;
1486 std::pair
<Version_table::iterator
, bool> ins
=
1487 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1491 // We already have an entry for this version.
1492 Version_base
* vb
= ins
.first
->second
;
1494 // We have now seen a symbol in this version, so it is not
1496 gold_assert(vb
!= NULL
);
1501 // If we are creating a shared object, it is an error to
1502 // find a definition of a symbol with a version which is not
1503 // in the version script.
1504 if (parameters
->options().shared())
1505 gold_error(_("symbol %s has undefined version %s"),
1506 sym
->demangled_name().c_str(), version
);
1508 // We only insert a base version for shared library.
1509 gold_assert(!this->needs_base_version_
);
1511 // When creating a regular executable, automatically define
1513 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1514 false, false, false, false);
1515 this->defs_
.push_back(vd
);
1516 ins
.first
->second
= vd
;
1520 // Add a reference to version NAME in file FILENAME.
1523 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1524 Stringpool::Key name_key
)
1526 Stringpool::Key filename_key
;
1527 filename
= dynpool
->add(filename
, true, &filename_key
);
1529 Key
k(name_key
, filename_key
);
1530 Version_base
* const vbnull
= NULL
;
1531 std::pair
<Version_table::iterator
, bool> ins
=
1532 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1536 // We already have an entry for this filename/version.
1540 // See whether we already have this filename. We don't expect many
1541 // version references, so we just do a linear search. This could be
1542 // replaced by a hash table.
1544 for (Needs::iterator p
= this->needs_
.begin();
1545 p
!= this->needs_
.end();
1548 if ((*p
)->filename() == filename
)
1557 // Create base version definition lazily for shared library.
1558 if (this->needs_base_version_
)
1559 this->define_base_version(dynpool
);
1561 // We have a new filename.
1562 vn
= new Verneed(filename
);
1563 this->needs_
.push_back(vn
);
1566 ins
.first
->second
= vn
->add_name(name
);
1569 // Set the version indexes. Create a new dynamic version symbol for
1570 // each new version definition.
1573 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1574 std::vector
<Symbol
*>* syms
)
1576 gold_assert(!this->is_finalized_
);
1578 unsigned int vi
= 1;
1580 for (Defs::iterator p
= this->defs_
.begin();
1581 p
!= this->defs_
.end();
1584 (*p
)->set_index(vi
);
1587 // Create a version symbol if necessary.
1588 if (!(*p
)->is_symbol_created())
1590 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1592 Symbol_table::PREDEFINED
,
1596 elfcpp::STV_DEFAULT
, 0,
1598 vsym
->set_needs_dynsym_entry();
1599 vsym
->set_dynsym_index(dynsym_index
);
1600 vsym
->set_is_default();
1602 syms
->push_back(vsym
);
1603 // The name is already in the dynamic pool.
1607 // Index 1 is used for global symbols.
1610 gold_assert(this->defs_
.empty());
1614 for (Needs::iterator p
= this->needs_
.begin();
1615 p
!= this->needs_
.end();
1617 vi
= (*p
)->finalize(vi
);
1619 this->is_finalized_
= true;
1621 return dynsym_index
;
1624 // Return the version index to use for a symbol. This does two hash
1625 // table lookups: one in DYNPOOL and one in this->version_table_.
1626 // Another approach alternative would be store a pointer in SYM, which
1627 // would increase the size of the symbol table. Or perhaps we could
1628 // use a hash table from dynamic symbol pointer values to Version_base
1632 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1633 const Symbol
* sym
) const
1635 Stringpool::Key version_key
;
1636 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1637 gold_assert(version
!= NULL
);
1640 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1642 if (!parameters
->options().shared())
1643 return elfcpp::VER_NDX_GLOBAL
;
1644 k
= Key(version_key
, 0);
1648 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1650 Stringpool::Key filename_key
;
1651 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1652 gold_assert(filename
!= NULL
);
1654 k
= Key(version_key
, filename_key
);
1657 Version_table::const_iterator p
= this->version_table_
.find(k
);
1658 gold_assert(p
!= this->version_table_
.end());
1660 return p
->second
->index();
1663 // Return an allocated buffer holding the contents of the symbol
1666 template<int size
, bool big_endian
>
1668 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1669 const Stringpool
* dynpool
,
1670 unsigned int local_symcount
,
1671 const std::vector
<Symbol
*>& syms
,
1673 unsigned int* psize
) const
1675 gold_assert(this->is_finalized_
);
1677 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1678 unsigned char* pbuf
= new unsigned char[sz
];
1680 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1681 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1682 elfcpp::VER_NDX_LOCAL
);
1684 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1688 unsigned int version_index
;
1689 const char* version
= (*p
)->version();
1690 if (version
!= NULL
)
1691 version_index
= this->version_index(symtab
, dynpool
, *p
);
1694 if ((*p
)->is_defined() && !(*p
)->is_from_dynobj())
1695 version_index
= elfcpp::VER_NDX_GLOBAL
;
1697 version_index
= elfcpp::VER_NDX_LOCAL
;
1699 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1701 if ((*p
)->version() != NULL
&& !(*p
)->is_default())
1702 version_index
|= elfcpp::VERSYM_HIDDEN
;
1703 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1711 // Return an allocated buffer holding the contents of the version
1712 // definition section.
1714 template<int size
, bool big_endian
>
1716 Versions::def_section_contents(const Stringpool
* dynpool
,
1717 unsigned char** pp
, unsigned int* psize
,
1718 unsigned int* pentries
) const
1720 gold_assert(this->is_finalized_
);
1721 gold_assert(!this->defs_
.empty());
1723 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1724 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1726 unsigned int sz
= 0;
1727 for (Defs::const_iterator p
= this->defs_
.begin();
1728 p
!= this->defs_
.end();
1731 sz
+= verdef_size
+ verdaux_size
;
1732 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1735 unsigned char* pbuf
= new unsigned char[sz
];
1737 unsigned char* pb
= pbuf
;
1738 Defs::const_iterator p
;
1740 for (p
= this->defs_
.begin(), i
= 0;
1741 p
!= this->defs_
.end();
1743 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1744 i
+ 1 >= this->defs_
.size(),
1747 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1751 *pentries
= this->defs_
.size();
1754 // Return an allocated buffer holding the contents of the version
1755 // reference section.
1757 template<int size
, bool big_endian
>
1759 Versions::need_section_contents(const Stringpool
* dynpool
,
1760 unsigned char** pp
, unsigned int* psize
,
1761 unsigned int* pentries
) const
1763 gold_assert(this->is_finalized_
);
1764 gold_assert(!this->needs_
.empty());
1766 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1767 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1769 unsigned int sz
= 0;
1770 for (Needs::const_iterator p
= this->needs_
.begin();
1771 p
!= this->needs_
.end();
1775 sz
+= (*p
)->count_versions() * vernaux_size
;
1778 unsigned char* pbuf
= new unsigned char[sz
];
1780 unsigned char* pb
= pbuf
;
1781 Needs::const_iterator p
;
1783 for (p
= this->needs_
.begin(), i
= 0;
1784 p
!= this->needs_
.end();
1786 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1787 i
+ 1 >= this->needs_
.size(),
1790 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1794 *pentries
= this->needs_
.size();
1797 // Instantiate the templates we need. We could use the configure
1798 // script to restrict this to only the ones for implemented targets.
1800 #ifdef HAVE_TARGET_32_LITTLE
1802 class Sized_dynobj
<32, false>;
1805 #ifdef HAVE_TARGET_32_BIG
1807 class Sized_dynobj
<32, true>;
1810 #ifdef HAVE_TARGET_64_LITTLE
1812 class Sized_dynobj
<64, false>;
1815 #ifdef HAVE_TARGET_64_BIG
1817 class Sized_dynobj
<64, true>;
1820 #ifdef HAVE_TARGET_32_LITTLE
1823 Versions::symbol_section_contents
<32, false>(
1824 const Symbol_table
*,
1827 const std::vector
<Symbol
*>&,
1829 unsigned int*) const;
1832 #ifdef HAVE_TARGET_32_BIG
1835 Versions::symbol_section_contents
<32, true>(
1836 const Symbol_table
*,
1839 const std::vector
<Symbol
*>&,
1841 unsigned int*) const;
1844 #ifdef HAVE_TARGET_64_LITTLE
1847 Versions::symbol_section_contents
<64, false>(
1848 const Symbol_table
*,
1851 const std::vector
<Symbol
*>&,
1853 unsigned int*) const;
1856 #ifdef HAVE_TARGET_64_BIG
1859 Versions::symbol_section_contents
<64, true>(
1860 const Symbol_table
*,
1863 const std::vector
<Symbol
*>&,
1865 unsigned int*) const;
1868 #ifdef HAVE_TARGET_32_LITTLE
1871 Versions::def_section_contents
<32, false>(
1875 unsigned int*) const;
1878 #ifdef HAVE_TARGET_32_BIG
1881 Versions::def_section_contents
<32, true>(
1885 unsigned int*) const;
1888 #ifdef HAVE_TARGET_64_LITTLE
1891 Versions::def_section_contents
<64, false>(
1895 unsigned int*) const;
1898 #ifdef HAVE_TARGET_64_BIG
1901 Versions::def_section_contents
<64, true>(
1905 unsigned int*) const;
1908 #ifdef HAVE_TARGET_32_LITTLE
1911 Versions::need_section_contents
<32, false>(
1915 unsigned int*) const;
1918 #ifdef HAVE_TARGET_32_BIG
1921 Versions::need_section_contents
<32, true>(
1925 unsigned int*) const;
1928 #ifdef HAVE_TARGET_64_LITTLE
1931 Versions::need_section_contents
<64, false>(
1935 unsigned int*) const;
1938 #ifdef HAVE_TARGET_64_BIG
1941 Versions::need_section_contents
<64, true>(
1945 unsigned int*) const;
1948 } // End namespace gold.