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 if (input_file
== NULL
)
56 this->soname_
= input_file
->found_name();
57 if (this->offset() != 0)
59 std::string::size_type open_paren
= this->name().find('(');
60 std::string::size_type close_paren
= this->name().find(')');
61 if (open_paren
!= std::string::npos
62 && close_paren
!= std::string::npos
)
64 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
66 this->soname_
= this->name().substr(open_paren
,
67 close_paren
- open_paren
);
73 // Class Sized_dynobj.
75 template<int size
, bool big_endian
>
76 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
77 const std::string
& name
,
78 Input_file
* input_file
,
80 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
81 : Dynobj(name
, input_file
, offset
),
82 elf_file_(this, ehdr
),
91 template<int size
, bool big_endian
>
93 Sized_dynobj
<size
, big_endian
>::setup()
95 const unsigned int shnum
= this->elf_file_
.shnum();
96 this->set_shnum(shnum
);
99 // Find the SHT_DYNSYM section and the various version sections, and
100 // the dynamic section, given the section headers.
102 template<int size
, bool big_endian
>
104 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
105 const unsigned char* pshdrs
,
106 unsigned int* pversym_shndx
,
107 unsigned int* pverdef_shndx
,
108 unsigned int* pverneed_shndx
,
109 unsigned int* pdynamic_shndx
)
111 *pversym_shndx
= -1U;
112 *pverdef_shndx
= -1U;
113 *pverneed_shndx
= -1U;
114 *pdynamic_shndx
= -1U;
116 unsigned int symtab_shndx
= 0;
117 unsigned int xindex_shndx
= 0;
118 unsigned int xindex_link
= 0;
119 const unsigned int shnum
= this->shnum();
120 const unsigned char* p
= pshdrs
;
121 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
123 typename
This::Shdr
shdr(p
);
126 switch (shdr
.get_sh_type())
128 case elfcpp::SHT_DYNSYM
:
129 this->dynsym_shndx_
= i
;
130 if (xindex_shndx
> 0 && xindex_link
== i
)
132 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
133 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
135 this->set_xindex(xindex
);
139 case elfcpp::SHT_SYMTAB
:
143 case elfcpp::SHT_GNU_versym
:
146 case elfcpp::SHT_GNU_verdef
:
149 case elfcpp::SHT_GNU_verneed
:
152 case elfcpp::SHT_DYNAMIC
:
155 case elfcpp::SHT_SYMTAB_SHNDX
:
157 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
158 if (xindex_link
== this->dynsym_shndx_
)
160 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
161 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
163 this->set_xindex(xindex
);
176 this->error(_("unexpected duplicate type %u section: %u, %u"),
177 shdr
.get_sh_type(), *pi
, i
);
182 // If there is no dynamic symbol table, use the normal symbol table.
183 // On some SVR4 systems, a shared library is stored in an archive.
184 // The version stored in the archive only has a normal symbol table.
185 // It has an SONAME entry which points to another copy in the file
186 // system which has a dynamic symbol table as usual. This is way of
187 // addressing the issues which glibc addresses using GROUP with
189 if (this->dynsym_shndx_
== -1U && symtab_shndx
!= 0)
191 this->dynsym_shndx_
= symtab_shndx
;
192 if (xindex_shndx
> 0 && xindex_link
== symtab_shndx
)
194 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
195 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
197 this->set_xindex(xindex
);
202 // Read the contents of section SHNDX. PSHDRS points to the section
203 // headers. TYPE is the expected section type. LINK is the expected
204 // section link. Store the data in *VIEW and *VIEW_SIZE. The
205 // section's sh_info field is stored in *VIEW_INFO.
207 template<int size
, bool big_endian
>
209 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
210 const unsigned char* pshdrs
,
215 section_size_type
* view_size
,
216 unsigned int* view_info
)
226 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
228 gold_assert(shdr
.get_sh_type() == type
);
230 if (this->adjust_shndx(shdr
.get_sh_link()) != link
)
231 this->error(_("unexpected link in section %u header: %u != %u"),
232 shndx
, this->adjust_shndx(shdr
.get_sh_link()), link
);
234 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size(),
236 *view_size
= convert_to_section_size_type(shdr
.get_sh_size());
237 *view_info
= shdr
.get_sh_info();
240 // Read the dynamic tags. Set the soname field if this shared object
241 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
242 // the section headers. DYNAMIC_SHNDX is the section index of the
243 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
244 // section index and contents of a string table which may be the one
245 // associated with the SHT_DYNAMIC section.
247 template<int size
, bool big_endian
>
249 Sized_dynobj
<size
, big_endian
>::read_dynamic(const unsigned char* pshdrs
,
250 unsigned int dynamic_shndx
,
251 unsigned int strtab_shndx
,
252 const unsigned char* strtabu
,
255 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
256 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
258 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
259 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
260 dynamic_size
, true, false);
262 const unsigned int link
= this->adjust_shndx(dynamicshdr
.get_sh_link());
263 if (link
!= strtab_shndx
)
265 if (link
>= this->shnum())
267 this->error(_("DYNAMIC section %u link out of range: %u"),
268 dynamic_shndx
, link
);
272 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
273 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
275 this->error(_("DYNAMIC section %u link %u is not a strtab"),
276 dynamic_shndx
, link
);
280 strtab_size
= strtabshdr
.get_sh_size();
281 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
, false,
285 const char* const strtab
= reinterpret_cast<const char*>(strtabu
);
287 for (const unsigned char* p
= pdynamic
;
288 p
< pdynamic
+ dynamic_size
;
291 typename
This::Dyn
dyn(p
);
293 switch (dyn
.get_d_tag())
295 case elfcpp::DT_NULL
:
296 // We should always see DT_NULL at the end of the dynamic
300 case elfcpp::DT_SONAME
:
302 off_t val
= dyn
.get_d_val();
303 if (val
>= strtab_size
)
304 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
305 static_cast<long long>(val
),
306 static_cast<long long>(strtab_size
));
308 this->set_soname_string(strtab
+ val
);
312 case elfcpp::DT_NEEDED
:
314 off_t val
= dyn
.get_d_val();
315 if (val
>= strtab_size
)
316 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
317 static_cast<long long>(val
),
318 static_cast<long long>(strtab_size
));
320 this->add_needed(strtab
+ val
);
329 this->error(_("missing DT_NULL in dynamic segment"));
332 // Read the symbols and sections from a dynamic object. We read the
333 // dynamic symbols, not the normal symbols.
335 template<int size
, bool big_endian
>
337 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
339 this->read_section_data(&this->elf_file_
, sd
);
341 const unsigned char* const pshdrs
= sd
->section_headers
->data();
343 unsigned int versym_shndx
;
344 unsigned int verdef_shndx
;
345 unsigned int verneed_shndx
;
346 unsigned int dynamic_shndx
;
347 this->find_dynsym_sections(pshdrs
, &versym_shndx
, &verdef_shndx
,
348 &verneed_shndx
, &dynamic_shndx
);
350 unsigned int strtab_shndx
= -1U;
353 sd
->symbols_size
= 0;
354 sd
->external_symbols_offset
= 0;
355 sd
->symbol_names
= NULL
;
356 sd
->symbol_names_size
= 0;
363 sd
->verneed_size
= 0;
364 sd
->verneed_info
= 0;
366 if (this->dynsym_shndx_
!= -1U)
368 // Get the dynamic symbols.
369 typename
This::Shdr
dynsymshdr(pshdrs
370 + this->dynsym_shndx_
* This::shdr_size
);
372 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
373 dynsymshdr
.get_sh_size(), true,
376 convert_to_section_size_type(dynsymshdr
.get_sh_size());
378 // Get the symbol names.
379 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
380 if (strtab_shndx
>= this->shnum())
382 this->error(_("invalid dynamic symbol table name index: %u"),
386 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
387 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
389 this->error(_("dynamic symbol table name section "
390 "has wrong type: %u"),
391 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
395 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
396 strtabshdr
.get_sh_size(),
398 sd
->symbol_names_size
=
399 convert_to_section_size_type(strtabshdr
.get_sh_size());
401 // Get the version information.
404 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
406 &sd
->versym
, &sd
->versym_size
, &dummy
);
408 // We require that the version definition and need section link
409 // to the same string table as the dynamic symbol table. This
410 // is not a technical requirement, but it always happens in
411 // practice. We could change this if necessary.
413 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
414 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
417 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
418 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
422 // Read the SHT_DYNAMIC section to find whether this shared object
423 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
424 // doesn't really have anything to do with reading the symbols, but
425 // this is a convenient place to do it.
426 if (dynamic_shndx
!= -1U)
427 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
428 (sd
->symbol_names
== NULL
430 : sd
->symbol_names
->data()),
431 sd
->symbol_names_size
);
434 // Return the Xindex structure to use for object with lots of
437 template<int size
, bool big_endian
>
439 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
441 gold_assert(this->dynsym_shndx_
!= -1U);
442 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
443 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
447 // Lay out the input sections for a dynamic object. We don't want to
448 // include sections from a dynamic object, so all that we actually do
449 // here is check for .gnu.warning and .note.GNU-split-stack sections.
451 template<int size
, bool big_endian
>
453 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
455 Read_symbols_data
* sd
)
457 const unsigned int shnum
= this->shnum();
461 // Get the section headers.
462 const unsigned char* pshdrs
= sd
->section_headers
->data();
464 // Get the section names.
465 const unsigned char* pnamesu
= sd
->section_names
->data();
466 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
468 // Skip the first, dummy, section.
469 pshdrs
+= This::shdr_size
;
470 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
472 typename
This::Shdr
shdr(pshdrs
);
474 if (shdr
.get_sh_name() >= sd
->section_names_size
)
476 this->error(_("bad section name offset for section %u: %lu"),
477 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
481 const char* name
= pnames
+ shdr
.get_sh_name();
483 this->handle_gnu_warning_section(name
, i
, symtab
);
484 this->handle_split_stack_section(name
);
487 delete sd
->section_headers
;
488 sd
->section_headers
= NULL
;
489 delete sd
->section_names
;
490 sd
->section_names
= NULL
;
493 // Add an entry to the vector mapping version numbers to version
496 template<int size
, bool big_endian
>
498 Sized_dynobj
<size
, big_endian
>::set_version_map(
499 Version_map
* version_map
,
501 const char* name
) const
503 if (ndx
>= version_map
->size())
504 version_map
->resize(ndx
+ 1);
505 if ((*version_map
)[ndx
] != NULL
)
506 this->error(_("duplicate definition for version %u"), ndx
);
507 (*version_map
)[ndx
] = name
;
510 // Add mappings for the version definitions to VERSION_MAP.
512 template<int size
, bool big_endian
>
514 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
515 Read_symbols_data
* sd
,
516 Version_map
* version_map
) const
518 if (sd
->verdef
== NULL
)
521 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
522 section_size_type names_size
= sd
->symbol_names_size
;
524 const unsigned char* pverdef
= sd
->verdef
->data();
525 section_size_type verdef_size
= sd
->verdef_size
;
526 const unsigned int count
= sd
->verdef_info
;
528 const unsigned char* p
= pverdef
;
529 for (unsigned int i
= 0; i
< count
; ++i
)
531 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
533 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
535 this->error(_("unexpected verdef version %u"),
536 verdef
.get_vd_version());
540 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
542 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
545 // The first Verdaux holds the name of this version. Subsequent
546 // ones are versions that this one depends upon, which we don't
548 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
551 this->error(_("verdef vd_cnt field too small: %u"),
552 static_cast<unsigned int>(vd_cnt
));
556 const section_size_type vd_aux
= verdef
.get_vd_aux();
557 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
559 this->error(_("verdef vd_aux field out of range: %u"),
560 static_cast<unsigned int>(vd_aux
));
564 const unsigned char* pvda
= p
+ vd_aux
;
565 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
567 const section_size_type vda_name
= verdaux
.get_vda_name();
568 if (vda_name
>= names_size
)
570 this->error(_("verdaux vda_name field out of range: %u"),
571 static_cast<unsigned int>(vda_name
));
575 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
577 const section_size_type vd_next
= verdef
.get_vd_next();
578 if ((p
- pverdef
) + vd_next
>= verdef_size
)
580 this->error(_("verdef vd_next field out of range: %u"),
581 static_cast<unsigned int>(vd_next
));
589 // Add mappings for the required versions to VERSION_MAP.
591 template<int size
, bool big_endian
>
593 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
594 Read_symbols_data
* sd
,
595 Version_map
* version_map
) const
597 if (sd
->verneed
== NULL
)
600 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
601 section_size_type names_size
= sd
->symbol_names_size
;
603 const unsigned char* pverneed
= sd
->verneed
->data();
604 const section_size_type verneed_size
= sd
->verneed_size
;
605 const unsigned int count
= sd
->verneed_info
;
607 const unsigned char* p
= pverneed
;
608 for (unsigned int i
= 0; i
< count
; ++i
)
610 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
612 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
614 this->error(_("unexpected verneed version %u"),
615 verneed
.get_vn_version());
619 const section_size_type vn_aux
= verneed
.get_vn_aux();
621 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
623 this->error(_("verneed vn_aux field out of range: %u"),
624 static_cast<unsigned int>(vn_aux
));
628 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
629 const unsigned char* pvna
= p
+ vn_aux
;
630 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
632 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
634 const unsigned int vna_name
= vernaux
.get_vna_name();
635 if (vna_name
>= names_size
)
637 this->error(_("vernaux vna_name field out of range: %u"),
638 static_cast<unsigned int>(vna_name
));
642 this->set_version_map(version_map
, vernaux
.get_vna_other(),
645 const section_size_type vna_next
= vernaux
.get_vna_next();
646 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
648 this->error(_("verneed vna_next field out of range: %u"),
649 static_cast<unsigned int>(vna_next
));
656 const section_size_type vn_next
= verneed
.get_vn_next();
657 if ((p
- pverneed
) + vn_next
>= verneed_size
)
659 this->error(_("verneed vn_next field out of range: %u"),
660 static_cast<unsigned int>(vn_next
));
668 // Create a vector mapping version numbers to version strings.
670 template<int size
, bool big_endian
>
672 Sized_dynobj
<size
, big_endian
>::make_version_map(
673 Read_symbols_data
* sd
,
674 Version_map
* version_map
) const
676 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
679 // A guess at the maximum version number we will see. If this is
680 // wrong we will be less efficient but still correct.
681 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
683 this->make_verdef_map(sd
, version_map
);
684 this->make_verneed_map(sd
, version_map
);
687 // Add the dynamic symbols to the symbol table.
689 template<int size
, bool big_endian
>
691 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
692 Read_symbols_data
* sd
,
695 if (sd
->symbols
== NULL
)
697 gold_assert(sd
->symbol_names
== NULL
);
698 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
699 && sd
->verneed
== NULL
);
703 const int sym_size
= This::sym_size
;
704 const size_t symcount
= sd
->symbols_size
/ sym_size
;
705 gold_assert(sd
->external_symbols_offset
== 0);
706 if (symcount
* sym_size
!= sd
->symbols_size
)
708 this->error(_("size of dynamic symbols is not multiple of symbol size"));
712 Version_map version_map
;
713 this->make_version_map(sd
, &version_map
);
715 // If printing symbol counts or a cross reference table or
716 // preparing for an incremental link, we want to track symbols.
717 if (parameters
->options().user_set_print_symbol_counts()
718 || parameters
->options().cref()
719 || parameters
->incremental())
721 this->symbols_
= new Symbols();
722 this->symbols_
->resize(symcount
);
725 const char* sym_names
=
726 reinterpret_cast<const char*>(sd
->symbol_names
->data());
727 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
728 sym_names
, sd
->symbol_names_size
,
731 : sd
->versym
->data()),
735 &this->defined_count_
);
739 delete sd
->symbol_names
;
740 sd
->symbol_names
= NULL
;
741 if (sd
->versym
!= NULL
)
746 if (sd
->verdef
!= NULL
)
751 if (sd
->verneed
!= NULL
)
757 // This is normally the last time we will read any data from this
759 this->clear_view_cache_marks();
762 template<int size
, bool big_endian
>
763 Archive::Should_include
764 Sized_dynobj
<size
, big_endian
>::do_should_include_member(Symbol_table
*,
769 return Archive::SHOULD_INCLUDE_YES
;
772 // Iterate over global symbols, calling a visitor class V for each.
774 template<int size
, bool big_endian
>
776 Sized_dynobj
<size
, big_endian
>::do_for_all_global_symbols(
777 Read_symbols_data
* sd
,
778 Library_base::Symbol_visitor_base
* v
)
780 const char* sym_names
=
781 reinterpret_cast<const char*>(sd
->symbol_names
->data());
782 const unsigned char* syms
=
783 sd
->symbols
->data() + sd
->external_symbols_offset
;
784 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
785 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
787 const unsigned char* p
= syms
;
789 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
791 elfcpp::Sym
<size
, big_endian
> sym(p
);
792 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
793 && sym
.get_st_bind() != elfcpp::STB_LOCAL
)
794 v
->visit(sym_names
+ sym
.get_st_name());
798 // Iterate over local symbols, calling a visitor class V for each GOT offset
799 // associated with a local symbol.
801 template<int size
, bool big_endian
>
803 Sized_dynobj
<size
, big_endian
>::do_for_all_local_got_entries(
804 Got_offset_list::Visitor
*) const
808 // Get symbol counts.
810 template<int size
, bool big_endian
>
812 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
817 *defined
= this->defined_count_
;
819 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
820 p
!= this->symbols_
->end();
823 && (*p
)->source() == Symbol::FROM_OBJECT
824 && (*p
)->object() == this
825 && (*p
)->is_defined()
826 && (*p
)->dynsym_index() != -1U)
831 // Given a vector of hash codes, compute the number of hash buckets to
835 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
836 bool for_gnu_hash_table
)
838 // FIXME: Implement optional hash table optimization.
840 // Array used to determine the number of hash table buckets to use
841 // based on the number of symbols there are. If there are fewer
842 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
843 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
844 // use more than 262147 buckets. This is straight from the old GNU
846 static const unsigned int buckets
[] =
848 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
849 16411, 32771, 65537, 131101, 262147
851 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
853 unsigned int symcount
= hashcodes
.size();
854 unsigned int ret
= 1;
855 const double full_fraction
856 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
857 for (int i
= 0; i
< buckets_count
; ++i
)
859 if (symcount
< buckets
[i
] * full_fraction
)
864 if (for_gnu_hash_table
&& ret
< 2)
870 // The standard ELF hash function. This hash function must not
871 // change, as the dynamic linker uses it also.
874 Dynobj::elf_hash(const char* name
)
876 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
879 while ((c
= *nameu
++) != '\0')
882 uint32_t g
= h
& 0xf0000000;
886 // The ELF ABI says h &= ~g, but using xor is equivalent in
887 // this case (since g was set from h) and may save one
895 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
896 // DYNSYMS is a vector with all the global dynamic symbols.
897 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
901 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
902 unsigned int local_dynsym_count
,
903 unsigned char** pphash
,
904 unsigned int* phashlen
)
906 unsigned int dynsym_count
= dynsyms
.size();
908 // Get the hash values for all the symbols.
909 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
910 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
911 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
913 const unsigned int bucketcount
=
914 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
916 std::vector
<uint32_t> bucket(bucketcount
);
917 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
919 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
921 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
922 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
923 chain
[dynsym_index
] = bucket
[bucketpos
];
924 bucket
[bucketpos
] = dynsym_index
;
927 unsigned int hashlen
= ((2
932 unsigned char* phash
= new unsigned char[hashlen
];
934 if (parameters
->target().is_big_endian())
936 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
937 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
945 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
946 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
957 // Fill in an ELF hash table.
959 template<bool big_endian
>
961 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
962 const std::vector
<uint32_t>& chain
,
963 unsigned char* phash
,
964 unsigned int hashlen
)
966 unsigned char* p
= phash
;
968 const unsigned int bucketcount
= bucket
.size();
969 const unsigned int chaincount
= chain
.size();
971 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
973 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
976 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
978 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
982 for (unsigned int i
= 0; i
< chaincount
; ++i
)
984 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
988 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
991 // The hash function used for the GNU hash table. This hash function
992 // must not change, as the dynamic linker uses it also.
995 Dynobj::gnu_hash(const char* name
)
997 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
1000 while ((c
= *nameu
++) != '\0')
1001 h
= (h
<< 5) + h
+ c
;
1005 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
1006 // tables are an extension to ELF which are recognized by the GNU
1007 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
1008 // TARGET is the target. DYNSYMS is a vector with all the global
1009 // symbols which will be going into the dynamic symbol table.
1010 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
1014 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
1015 unsigned int local_dynsym_count
,
1016 unsigned char** pphash
,
1017 unsigned int* phashlen
)
1019 const unsigned int count
= dynsyms
.size();
1021 // Sort the dynamic symbols into two vectors. Symbols which we do
1022 // not want to put into the hash table we store into
1023 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
1024 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
1025 // and records the hash codes.
1027 std::vector
<Symbol
*> unhashed_dynsyms
;
1028 unhashed_dynsyms
.reserve(count
);
1030 std::vector
<Symbol
*> hashed_dynsyms
;
1031 hashed_dynsyms
.reserve(count
);
1033 std::vector
<uint32_t> dynsym_hashvals
;
1034 dynsym_hashvals
.reserve(count
);
1036 for (unsigned int i
= 0; i
< count
; ++i
)
1038 Symbol
* sym
= dynsyms
[i
];
1040 if (!sym
->needs_dynsym_value()
1041 && (sym
->is_undefined()
1042 || sym
->is_from_dynobj()
1043 || sym
->is_forced_local()))
1044 unhashed_dynsyms
.push_back(sym
);
1047 hashed_dynsyms
.push_back(sym
);
1048 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
1052 // Put the unhashed symbols at the start of the global portion of
1053 // the dynamic symbol table.
1054 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
1055 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
1056 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
1058 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
1059 ++unhashed_dynsym_index
;
1062 // For the actual data generation we call out to a templatized
1064 int size
= parameters
->target().get_size();
1065 bool big_endian
= parameters
->target().is_big_endian();
1070 #ifdef HAVE_TARGET_32_BIG
1071 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
1073 unhashed_dynsym_index
,
1082 #ifdef HAVE_TARGET_32_LITTLE
1083 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
1085 unhashed_dynsym_index
,
1093 else if (size
== 64)
1097 #ifdef HAVE_TARGET_64_BIG
1098 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1100 unhashed_dynsym_index
,
1109 #ifdef HAVE_TARGET_64_LITTLE
1110 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1112 unhashed_dynsym_index
,
1124 // Create the actual data for a GNU hash table. This is just a copy
1125 // of the code from the old GNU linker.
1127 template<int size
, bool big_endian
>
1129 Dynobj::sized_create_gnu_hash_table(
1130 const std::vector
<Symbol
*>& hashed_dynsyms
,
1131 const std::vector
<uint32_t>& dynsym_hashvals
,
1132 unsigned int unhashed_dynsym_count
,
1133 unsigned char** pphash
,
1134 unsigned int* phashlen
)
1136 if (hashed_dynsyms
.empty())
1138 // Special case for the empty hash table.
1139 unsigned int hashlen
= 5 * 4 + size
/ 8;
1140 unsigned char* phash
= new unsigned char[hashlen
];
1141 // One empty bucket.
1142 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1143 // Symbol index above unhashed symbols.
1144 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1145 // One word for bitmask.
1146 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1147 // Only bloom filter.
1148 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1150 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1151 // No hashes in only bucket.
1152 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1154 *phashlen
= hashlen
;
1160 const unsigned int bucketcount
=
1161 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1163 const unsigned int nsyms
= hashed_dynsyms
.size();
1165 uint32_t maskbitslog2
= 1;
1166 uint32_t x
= nsyms
>> 1;
1172 if (maskbitslog2
< 3)
1174 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1184 if (maskbitslog2
== 5)
1188 uint32_t mask
= (1U << shift1
) - 1U;
1189 uint32_t shift2
= maskbitslog2
;
1190 uint32_t maskbits
= 1U << maskbitslog2
;
1191 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1193 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1194 std::vector
<Word
> bitmask(maskwords
);
1195 std::vector
<uint32_t> counts(bucketcount
);
1196 std::vector
<uint32_t> indx(bucketcount
);
1197 uint32_t symindx
= unhashed_dynsym_count
;
1199 // Count the number of times each hash bucket is used.
1200 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1201 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1203 unsigned int cnt
= symindx
;
1204 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1210 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1211 hashlen
+= maskbits
/ 8;
1212 unsigned char* phash
= new unsigned char[hashlen
];
1214 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1215 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1216 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1217 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1219 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1220 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1223 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1225 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1229 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1231 Symbol
* sym
= hashed_dynsyms
[i
];
1232 uint32_t hashval
= dynsym_hashvals
[i
];
1234 unsigned int bucket
= hashval
% bucketcount
;
1235 unsigned int val
= ((hashval
>> shift1
)
1236 & ((maskbits
>> shift1
) - 1));
1237 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1238 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1239 val
= hashval
& ~ 1U;
1240 if (counts
[bucket
] == 1)
1242 // Last element terminates the chain.
1245 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1249 sym
->set_dynsym_index(indx
[bucket
]);
1254 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1256 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1260 *phashlen
= hashlen
;
1266 // Write this definition to a buffer for the output section.
1268 template<int size
, bool big_endian
>
1270 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1272 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1273 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1275 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1276 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1277 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1278 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0)
1279 | (this->is_info_
? elfcpp::VER_FLG_INFO
: 0));
1280 vd
.set_vd_ndx(this->index());
1281 vd
.set_vd_cnt(1 + this->deps_
.size());
1282 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1283 vd
.set_vd_aux(verdef_size
);
1284 vd
.set_vd_next(is_last
1286 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1289 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1290 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1291 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1294 Deps::const_iterator p
;
1296 for (p
= this->deps_
.begin(), i
= 0;
1297 p
!= this->deps_
.end();
1300 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1301 vda
.set_vda_name(dynpool
->get_offset(*p
));
1302 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1313 for (Need_versions::iterator p
= this->need_versions_
.begin();
1314 p
!= this->need_versions_
.end();
1319 // Add a new version to this file reference.
1322 Verneed::add_name(const char* name
)
1324 Verneed_version
* vv
= new Verneed_version(name
);
1325 this->need_versions_
.push_back(vv
);
1329 // Set the version indexes starting at INDEX.
1332 Verneed::finalize(unsigned int index
)
1334 for (Need_versions::iterator p
= this->need_versions_
.begin();
1335 p
!= this->need_versions_
.end();
1338 (*p
)->set_index(index
);
1344 // Write this list of referenced versions to a buffer for the output
1347 template<int size
, bool big_endian
>
1349 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1350 unsigned char* pb
) const
1352 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1353 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1355 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1356 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1357 vn
.set_vn_cnt(this->need_versions_
.size());
1358 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1359 vn
.set_vn_aux(verneed_size
);
1360 vn
.set_vn_next(is_last
1362 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1365 Need_versions::const_iterator p
;
1367 for (p
= this->need_versions_
.begin(), i
= 0;
1368 p
!= this->need_versions_
.end();
1371 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1372 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1373 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1374 vna
.set_vna_flags(0);
1375 vna
.set_vna_other((*p
)->index());
1376 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1377 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1386 // Versions methods.
1388 Versions::Versions(const Version_script_info
& version_script
,
1389 Stringpool
* dynpool
)
1390 : defs_(), needs_(), version_table_(),
1391 is_finalized_(false), version_script_(version_script
),
1392 needs_base_version_(parameters
->options().shared())
1394 if (!this->version_script_
.empty())
1396 // Parse the version script, and insert each declared version into
1397 // defs_ and version_table_.
1398 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1400 if (this->needs_base_version_
&& !versions
.empty())
1401 this->define_base_version(dynpool
);
1403 for (size_t k
= 0; k
< versions
.size(); ++k
)
1405 Stringpool::Key version_key
;
1406 const char* version
= dynpool
->add(versions
[k
].c_str(),
1407 true, &version_key
);
1408 Verdef
* const vd
= new Verdef(
1410 this->version_script_
.get_dependencies(version
),
1411 false, false, false, false);
1412 this->defs_
.push_back(vd
);
1413 Key
key(version_key
, 0);
1414 this->version_table_
.insert(std::make_pair(key
, vd
));
1419 Versions::~Versions()
1421 for (Defs::iterator p
= this->defs_
.begin();
1422 p
!= this->defs_
.end();
1426 for (Needs::iterator p
= this->needs_
.begin();
1427 p
!= this->needs_
.end();
1432 // Define the base version of a shared library. The base version definition
1433 // must be the first entry in defs_. We insert it lazily so that defs_ is
1434 // empty if no symbol versioning is used. Then layout can just drop the
1435 // version sections.
1438 Versions::define_base_version(Stringpool
* dynpool
)
1440 // If we do any versioning at all, we always need a base version, so
1441 // define that first. Nothing explicitly declares itself as part of base,
1442 // so it doesn't need to be in version_table_.
1443 gold_assert(this->defs_
.empty());
1444 const char* name
= parameters
->options().soname();
1446 name
= parameters
->options().output_file_name();
1447 name
= dynpool
->add(name
, false, NULL
);
1448 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1449 true, false, false, true);
1450 this->defs_
.push_back(vdbase
);
1451 this->needs_base_version_
= false;
1454 // Return the dynamic object which a symbol refers to.
1457 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1458 const Symbol
* sym
) const
1460 if (sym
->is_copied_from_dynobj())
1461 return symtab
->get_copy_source(sym
);
1464 Object
* object
= sym
->object();
1465 gold_assert(object
->is_dynamic());
1466 return static_cast<Dynobj
*>(object
);
1470 // Record version information for a symbol going into the dynamic
1474 Versions::record_version(const Symbol_table
* symtab
,
1475 Stringpool
* dynpool
, const Symbol
* sym
)
1477 gold_assert(!this->is_finalized_
);
1478 gold_assert(sym
->version() != NULL
);
1480 Stringpool::Key version_key
;
1481 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1483 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1485 if (parameters
->options().shared())
1486 this->add_def(sym
, version
, version_key
);
1490 // This is a version reference.
1491 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1492 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1496 // We've found a symbol SYM defined in version VERSION.
1499 Versions::add_def(const Symbol
* sym
, const char* version
,
1500 Stringpool::Key version_key
)
1502 Key
k(version_key
, 0);
1503 Version_base
* const vbnull
= NULL
;
1504 std::pair
<Version_table::iterator
, bool> ins
=
1505 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1509 // We already have an entry for this version.
1510 Version_base
* vb
= ins
.first
->second
;
1512 // We have now seen a symbol in this version, so it is not
1514 gold_assert(vb
!= NULL
);
1519 // If we are creating a shared object, it is an error to
1520 // find a definition of a symbol with a version which is not
1521 // in the version script.
1522 if (parameters
->options().shared())
1523 gold_error(_("symbol %s has undefined version %s"),
1524 sym
->demangled_name().c_str(), version
);
1526 // We only insert a base version for shared library.
1527 gold_assert(!this->needs_base_version_
);
1529 // When creating a regular executable, automatically define
1531 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1532 false, false, false, false);
1533 this->defs_
.push_back(vd
);
1534 ins
.first
->second
= vd
;
1538 // Add a reference to version NAME in file FILENAME.
1541 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1542 Stringpool::Key name_key
)
1544 Stringpool::Key filename_key
;
1545 filename
= dynpool
->add(filename
, true, &filename_key
);
1547 Key
k(name_key
, filename_key
);
1548 Version_base
* const vbnull
= NULL
;
1549 std::pair
<Version_table::iterator
, bool> ins
=
1550 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1554 // We already have an entry for this filename/version.
1558 // See whether we already have this filename. We don't expect many
1559 // version references, so we just do a linear search. This could be
1560 // replaced by a hash table.
1562 for (Needs::iterator p
= this->needs_
.begin();
1563 p
!= this->needs_
.end();
1566 if ((*p
)->filename() == filename
)
1575 // Create base version definition lazily for shared library.
1576 if (this->needs_base_version_
)
1577 this->define_base_version(dynpool
);
1579 // We have a new filename.
1580 vn
= new Verneed(filename
);
1581 this->needs_
.push_back(vn
);
1584 ins
.first
->second
= vn
->add_name(name
);
1587 // Set the version indexes. Create a new dynamic version symbol for
1588 // each new version definition.
1591 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1592 std::vector
<Symbol
*>* syms
)
1594 gold_assert(!this->is_finalized_
);
1596 unsigned int vi
= 1;
1598 for (Defs::iterator p
= this->defs_
.begin();
1599 p
!= this->defs_
.end();
1602 (*p
)->set_index(vi
);
1605 // Create a version symbol if necessary.
1606 if (!(*p
)->is_symbol_created())
1608 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1610 Symbol_table::PREDEFINED
,
1614 elfcpp::STV_DEFAULT
, 0,
1616 vsym
->set_needs_dynsym_entry();
1617 vsym
->set_dynsym_index(dynsym_index
);
1618 vsym
->set_is_default();
1620 syms
->push_back(vsym
);
1621 // The name is already in the dynamic pool.
1625 // Index 1 is used for global symbols.
1628 gold_assert(this->defs_
.empty());
1632 for (Needs::iterator p
= this->needs_
.begin();
1633 p
!= this->needs_
.end();
1635 vi
= (*p
)->finalize(vi
);
1637 this->is_finalized_
= true;
1639 return dynsym_index
;
1642 // Return the version index to use for a symbol. This does two hash
1643 // table lookups: one in DYNPOOL and one in this->version_table_.
1644 // Another approach alternative would be store a pointer in SYM, which
1645 // would increase the size of the symbol table. Or perhaps we could
1646 // use a hash table from dynamic symbol pointer values to Version_base
1650 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1651 const Symbol
* sym
) const
1653 Stringpool::Key version_key
;
1654 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1655 gold_assert(version
!= NULL
);
1658 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1660 if (!parameters
->options().shared())
1661 return elfcpp::VER_NDX_GLOBAL
;
1662 k
= Key(version_key
, 0);
1666 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1668 Stringpool::Key filename_key
;
1669 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1670 gold_assert(filename
!= NULL
);
1672 k
= Key(version_key
, filename_key
);
1675 Version_table::const_iterator p
= this->version_table_
.find(k
);
1676 gold_assert(p
!= this->version_table_
.end());
1678 return p
->second
->index();
1681 // Return an allocated buffer holding the contents of the symbol
1684 template<int size
, bool big_endian
>
1686 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1687 const Stringpool
* dynpool
,
1688 unsigned int local_symcount
,
1689 const std::vector
<Symbol
*>& syms
,
1691 unsigned int* psize
) const
1693 gold_assert(this->is_finalized_
);
1695 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1696 unsigned char* pbuf
= new unsigned char[sz
];
1698 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1699 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1700 elfcpp::VER_NDX_LOCAL
);
1702 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1706 unsigned int version_index
;
1707 const char* version
= (*p
)->version();
1708 if (version
!= NULL
)
1709 version_index
= this->version_index(symtab
, dynpool
, *p
);
1712 if ((*p
)->is_defined() && !(*p
)->is_from_dynobj())
1713 version_index
= elfcpp::VER_NDX_GLOBAL
;
1715 version_index
= elfcpp::VER_NDX_LOCAL
;
1717 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1719 if ((*p
)->version() != NULL
&& !(*p
)->is_default())
1720 version_index
|= elfcpp::VERSYM_HIDDEN
;
1721 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1729 // Return an allocated buffer holding the contents of the version
1730 // definition section.
1732 template<int size
, bool big_endian
>
1734 Versions::def_section_contents(const Stringpool
* dynpool
,
1735 unsigned char** pp
, unsigned int* psize
,
1736 unsigned int* pentries
) const
1738 gold_assert(this->is_finalized_
);
1739 gold_assert(!this->defs_
.empty());
1741 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1742 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1744 unsigned int sz
= 0;
1745 for (Defs::const_iterator p
= this->defs_
.begin();
1746 p
!= this->defs_
.end();
1749 sz
+= verdef_size
+ verdaux_size
;
1750 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1753 unsigned char* pbuf
= new unsigned char[sz
];
1755 unsigned char* pb
= pbuf
;
1756 Defs::const_iterator p
;
1758 for (p
= this->defs_
.begin(), i
= 0;
1759 p
!= this->defs_
.end();
1761 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1762 i
+ 1 >= this->defs_
.size(),
1765 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1769 *pentries
= this->defs_
.size();
1772 // Return an allocated buffer holding the contents of the version
1773 // reference section.
1775 template<int size
, bool big_endian
>
1777 Versions::need_section_contents(const Stringpool
* dynpool
,
1778 unsigned char** pp
, unsigned int* psize
,
1779 unsigned int* pentries
) const
1781 gold_assert(this->is_finalized_
);
1782 gold_assert(!this->needs_
.empty());
1784 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1785 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1787 unsigned int sz
= 0;
1788 for (Needs::const_iterator p
= this->needs_
.begin();
1789 p
!= this->needs_
.end();
1793 sz
+= (*p
)->count_versions() * vernaux_size
;
1796 unsigned char* pbuf
= new unsigned char[sz
];
1798 unsigned char* pb
= pbuf
;
1799 Needs::const_iterator p
;
1801 for (p
= this->needs_
.begin(), i
= 0;
1802 p
!= this->needs_
.end();
1804 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1805 i
+ 1 >= this->needs_
.size(),
1808 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1812 *pentries
= this->needs_
.size();
1815 // Instantiate the templates we need. We could use the configure
1816 // script to restrict this to only the ones for implemented targets.
1818 #ifdef HAVE_TARGET_32_LITTLE
1820 class Sized_dynobj
<32, false>;
1823 #ifdef HAVE_TARGET_32_BIG
1825 class Sized_dynobj
<32, true>;
1828 #ifdef HAVE_TARGET_64_LITTLE
1830 class Sized_dynobj
<64, false>;
1833 #ifdef HAVE_TARGET_64_BIG
1835 class Sized_dynobj
<64, true>;
1838 #ifdef HAVE_TARGET_32_LITTLE
1841 Versions::symbol_section_contents
<32, false>(
1842 const Symbol_table
*,
1845 const std::vector
<Symbol
*>&,
1847 unsigned int*) const;
1850 #ifdef HAVE_TARGET_32_BIG
1853 Versions::symbol_section_contents
<32, true>(
1854 const Symbol_table
*,
1857 const std::vector
<Symbol
*>&,
1859 unsigned int*) const;
1862 #ifdef HAVE_TARGET_64_LITTLE
1865 Versions::symbol_section_contents
<64, false>(
1866 const Symbol_table
*,
1869 const std::vector
<Symbol
*>&,
1871 unsigned int*) const;
1874 #ifdef HAVE_TARGET_64_BIG
1877 Versions::symbol_section_contents
<64, true>(
1878 const Symbol_table
*,
1881 const std::vector
<Symbol
*>&,
1883 unsigned int*) const;
1886 #ifdef HAVE_TARGET_32_LITTLE
1889 Versions::def_section_contents
<32, false>(
1893 unsigned int*) const;
1896 #ifdef HAVE_TARGET_32_BIG
1899 Versions::def_section_contents
<32, true>(
1903 unsigned int*) const;
1906 #ifdef HAVE_TARGET_64_LITTLE
1909 Versions::def_section_contents
<64, false>(
1913 unsigned int*) const;
1916 #ifdef HAVE_TARGET_64_BIG
1919 Versions::def_section_contents
<64, true>(
1923 unsigned int*) const;
1926 #ifdef HAVE_TARGET_32_LITTLE
1929 Versions::need_section_contents
<32, false>(
1933 unsigned int*) const;
1936 #ifdef HAVE_TARGET_32_BIG
1939 Versions::need_section_contents
<32, true>(
1943 unsigned int*) const;
1946 #ifdef HAVE_TARGET_64_LITTLE
1949 Versions::need_section_contents
<64, false>(
1953 unsigned int*) const;
1956 #ifdef HAVE_TARGET_64_BIG
1959 Versions::need_section_contents
<64, true>(
1963 unsigned int*) const;
1966 } // End namespace gold.