1 // dynobj.cc -- dynamic object support for gold
3 // Copyright (C) 2006-2023 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->base_read_symbols(sd
);
342 // Read the symbols and sections from a dynamic object. We read the
343 // dynamic symbols, not the normal symbols. This is common code for
344 // all target-specific overrides of do_read_symbols().
346 template<int size
, bool big_endian
>
348 Sized_dynobj
<size
, big_endian
>::base_read_symbols(Read_symbols_data
* sd
)
350 this->read_section_data(&this->elf_file_
, sd
);
352 const unsigned char* const pshdrs
= sd
->section_headers
->data();
354 unsigned int versym_shndx
;
355 unsigned int verdef_shndx
;
356 unsigned int verneed_shndx
;
357 unsigned int dynamic_shndx
;
358 this->find_dynsym_sections(pshdrs
, &versym_shndx
, &verdef_shndx
,
359 &verneed_shndx
, &dynamic_shndx
);
361 unsigned int strtab_shndx
= -1U;
364 sd
->symbols_size
= 0;
365 sd
->external_symbols_offset
= 0;
366 sd
->symbol_names
= NULL
;
367 sd
->symbol_names_size
= 0;
374 sd
->verneed_size
= 0;
375 sd
->verneed_info
= 0;
377 const unsigned char* namesu
= sd
->section_names
->data();
378 const char* names
= reinterpret_cast<const char*>(namesu
);
379 if (memmem(names
, sd
->section_names_size
, ".zdebug_", 8) != NULL
)
381 Compressed_section_map
* compressed_sections
=
382 build_compressed_section_map
<size
, big_endian
>(
383 pshdrs
, this->shnum(), names
, sd
->section_names_size
, this, true);
384 if (compressed_sections
!= NULL
)
385 this->set_compressed_sections(compressed_sections
);
388 if (this->dynsym_shndx_
!= -1U)
390 // Get the dynamic symbols.
391 typename
This::Shdr
dynsymshdr(pshdrs
392 + this->dynsym_shndx_
* This::shdr_size
);
394 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
395 dynsymshdr
.get_sh_size(), true,
398 convert_to_section_size_type(dynsymshdr
.get_sh_size());
400 // Get the symbol names.
401 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
402 if (strtab_shndx
>= this->shnum())
404 this->error(_("invalid dynamic symbol table name index: %u"),
408 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
409 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
411 this->error(_("dynamic symbol table name section "
412 "has wrong type: %u"),
413 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
417 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
418 strtabshdr
.get_sh_size(),
420 sd
->symbol_names_size
=
421 convert_to_section_size_type(strtabshdr
.get_sh_size());
423 // Get the version information.
426 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
428 &sd
->versym
, &sd
->versym_size
, &dummy
);
430 // We require that the version definition and need section link
431 // to the same string table as the dynamic symbol table. This
432 // is not a technical requirement, but it always happens in
433 // practice. We could change this if necessary.
435 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
436 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
439 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
440 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
444 // Read the SHT_DYNAMIC section to find whether this shared object
445 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
446 // doesn't really have anything to do with reading the symbols, but
447 // this is a convenient place to do it.
448 if (dynamic_shndx
!= -1U)
449 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
450 (sd
->symbol_names
== NULL
452 : sd
->symbol_names
->data()),
453 sd
->symbol_names_size
);
456 // Return the Xindex structure to use for object with lots of
459 template<int size
, bool big_endian
>
461 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
463 gold_assert(this->dynsym_shndx_
!= -1U);
464 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
465 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
469 // Lay out the input sections for a dynamic object. We don't want to
470 // include sections from a dynamic object, so all that we actually do
471 // here is check for .gnu.warning and .note.GNU-split-stack sections.
473 template<int size
, bool big_endian
>
475 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
477 Read_symbols_data
* sd
)
479 const unsigned int shnum
= this->shnum();
483 // Get the section headers.
484 const unsigned char* pshdrs
= sd
->section_headers
->data();
486 // Get the section names.
487 const unsigned char* pnamesu
= sd
->section_names
->data();
488 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
490 // Skip the first, dummy, section.
491 pshdrs
+= This::shdr_size
;
492 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
494 typename
This::Shdr
shdr(pshdrs
);
496 if (shdr
.get_sh_name() >= sd
->section_names_size
)
498 this->error(_("bad section name offset for section %u: %lu"),
499 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
503 const char* name
= pnames
+ shdr
.get_sh_name();
505 this->handle_gnu_warning_section(name
, i
, symtab
);
506 this->handle_split_stack_section(name
);
509 delete sd
->section_headers
;
510 sd
->section_headers
= NULL
;
511 delete sd
->section_names
;
512 sd
->section_names
= NULL
;
515 // Add an entry to the vector mapping version numbers to version
518 template<int size
, bool big_endian
>
520 Sized_dynobj
<size
, big_endian
>::set_version_map(
521 Version_map
* version_map
,
523 const char* name
) const
525 if (ndx
>= version_map
->size())
526 version_map
->resize(ndx
+ 1);
527 if ((*version_map
)[ndx
] != NULL
)
528 this->error(_("duplicate definition for version %u"), ndx
);
529 (*version_map
)[ndx
] = name
;
532 // Add mappings for the version definitions to VERSION_MAP.
534 template<int size
, bool big_endian
>
536 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
537 Read_symbols_data
* sd
,
538 Version_map
* version_map
) const
540 if (sd
->verdef
== NULL
)
543 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
544 section_size_type names_size
= sd
->symbol_names_size
;
546 const unsigned char* pverdef
= sd
->verdef
->data();
547 section_size_type verdef_size
= sd
->verdef_size
;
548 const unsigned int count
= sd
->verdef_info
;
550 const unsigned char* p
= pverdef
;
551 for (unsigned int i
= 0; i
< count
; ++i
)
553 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
555 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
557 this->error(_("unexpected verdef version %u"),
558 verdef
.get_vd_version());
562 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
564 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
567 // The first Verdaux holds the name of this version. Subsequent
568 // ones are versions that this one depends upon, which we don't
570 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
573 this->error(_("verdef vd_cnt field too small: %u"),
574 static_cast<unsigned int>(vd_cnt
));
578 const section_size_type vd_aux
= verdef
.get_vd_aux();
579 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
581 this->error(_("verdef vd_aux field out of range: %u"),
582 static_cast<unsigned int>(vd_aux
));
586 const unsigned char* pvda
= p
+ vd_aux
;
587 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
589 const section_size_type vda_name
= verdaux
.get_vda_name();
590 if (vda_name
>= names_size
)
592 this->error(_("verdaux vda_name field out of range: %u"),
593 static_cast<unsigned int>(vda_name
));
597 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
599 const section_size_type vd_next
= verdef
.get_vd_next();
600 if ((p
- pverdef
) + vd_next
>= verdef_size
)
602 this->error(_("verdef vd_next field out of range: %u"),
603 static_cast<unsigned int>(vd_next
));
611 // Add mappings for the required versions to VERSION_MAP.
613 template<int size
, bool big_endian
>
615 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
616 Read_symbols_data
* sd
,
617 Version_map
* version_map
) const
619 if (sd
->verneed
== NULL
)
622 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
623 section_size_type names_size
= sd
->symbol_names_size
;
625 const unsigned char* pverneed
= sd
->verneed
->data();
626 const section_size_type verneed_size
= sd
->verneed_size
;
627 const unsigned int count
= sd
->verneed_info
;
629 const unsigned char* p
= pverneed
;
630 for (unsigned int i
= 0; i
< count
; ++i
)
632 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
634 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
636 this->error(_("unexpected verneed version %u"),
637 verneed
.get_vn_version());
641 const section_size_type vn_aux
= verneed
.get_vn_aux();
643 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
645 this->error(_("verneed vn_aux field out of range: %u"),
646 static_cast<unsigned int>(vn_aux
));
650 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
651 const unsigned char* pvna
= p
+ vn_aux
;
652 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
654 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
656 const unsigned int vna_name
= vernaux
.get_vna_name();
657 if (vna_name
>= names_size
)
659 this->error(_("vernaux vna_name field out of range: %u"),
660 static_cast<unsigned int>(vna_name
));
664 this->set_version_map(version_map
, vernaux
.get_vna_other(),
667 const section_size_type vna_next
= vernaux
.get_vna_next();
668 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
670 this->error(_("verneed vna_next field out of range: %u"),
671 static_cast<unsigned int>(vna_next
));
678 const section_size_type vn_next
= verneed
.get_vn_next();
679 if ((p
- pverneed
) + vn_next
>= verneed_size
)
681 this->error(_("verneed vn_next field out of range: %u"),
682 static_cast<unsigned int>(vn_next
));
690 // Create a vector mapping version numbers to version strings.
692 template<int size
, bool big_endian
>
694 Sized_dynobj
<size
, big_endian
>::make_version_map(
695 Read_symbols_data
* sd
,
696 Version_map
* version_map
) const
698 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
701 // A guess at the maximum version number we will see. If this is
702 // wrong we will be less efficient but still correct.
703 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
705 this->make_verdef_map(sd
, version_map
);
706 this->make_verneed_map(sd
, version_map
);
709 // Add the dynamic symbols to the symbol table.
711 template<int size
, bool big_endian
>
713 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
714 Read_symbols_data
* sd
,
717 if (sd
->symbols
== NULL
)
719 gold_assert(sd
->symbol_names
== NULL
);
720 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
721 && sd
->verneed
== NULL
);
725 const int sym_size
= This::sym_size
;
726 const size_t symcount
= sd
->symbols_size
/ sym_size
;
727 gold_assert(sd
->external_symbols_offset
== 0);
728 if (symcount
* sym_size
!= sd
->symbols_size
)
730 this->error(_("size of dynamic symbols is not multiple of symbol size"));
734 Version_map version_map
;
735 this->make_version_map(sd
, &version_map
);
737 // If printing symbol counts or a cross reference table or
738 // preparing for an incremental link, we want to track symbols.
739 if (parameters
->options().user_set_print_symbol_counts()
740 || parameters
->options().cref()
741 || parameters
->incremental())
743 this->symbols_
= new Symbols();
744 this->symbols_
->resize(symcount
);
747 const char* sym_names
=
748 reinterpret_cast<const char*>(sd
->symbol_names
->data());
749 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
750 sym_names
, sd
->symbol_names_size
,
753 : sd
->versym
->data()),
757 &this->defined_count_
);
761 delete sd
->symbol_names
;
762 sd
->symbol_names
= NULL
;
763 if (sd
->versym
!= NULL
)
768 if (sd
->verdef
!= NULL
)
773 if (sd
->verneed
!= NULL
)
779 // This is normally the last time we will read any data from this
781 this->clear_view_cache_marks();
784 template<int size
, bool big_endian
>
785 Archive::Should_include
786 Sized_dynobj
<size
, big_endian
>::do_should_include_member(Symbol_table
*,
791 return Archive::SHOULD_INCLUDE_YES
;
794 // Iterate over global symbols, calling a visitor class V for each.
796 template<int size
, bool big_endian
>
798 Sized_dynobj
<size
, big_endian
>::do_for_all_global_symbols(
799 Read_symbols_data
* sd
,
800 Library_base::Symbol_visitor_base
* v
)
802 const char* sym_names
=
803 reinterpret_cast<const char*>(sd
->symbol_names
->data());
804 const unsigned char* syms
=
805 sd
->symbols
->data() + sd
->external_symbols_offset
;
806 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
807 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
809 const unsigned char* p
= syms
;
811 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
813 elfcpp::Sym
<size
, big_endian
> sym(p
);
814 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
815 && sym
.get_st_bind() != elfcpp::STB_LOCAL
)
816 v
->visit(sym_names
+ sym
.get_st_name());
820 // Iterate over local symbols, calling a visitor class V for each GOT offset
821 // associated with a local symbol.
823 template<int size
, bool big_endian
>
825 Sized_dynobj
<size
, big_endian
>::do_for_all_local_got_entries(
826 Got_offset_list::Visitor
*) const
830 // Get symbol counts.
832 template<int size
, bool big_endian
>
834 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
839 *defined
= this->defined_count_
;
841 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
842 p
!= this->symbols_
->end();
845 && (*p
)->source() == Symbol::FROM_OBJECT
846 && (*p
)->object() == this
847 && (*p
)->is_defined()
848 && (*p
)->has_dynsym_index())
853 // Given a vector of hash codes, compute the number of hash buckets to
857 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
858 bool for_gnu_hash_table
)
860 // FIXME: Implement optional hash table optimization.
862 // Array used to determine the number of hash table buckets to use
863 // based on the number of symbols there are. If there are fewer
864 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
865 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
866 // use more than 262147 buckets. This is straight from the old GNU
868 static const unsigned int buckets
[] =
870 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
871 16411, 32771, 65537, 131101, 262147
873 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
875 unsigned int symcount
= hashcodes
.size();
876 unsigned int ret
= 1;
877 const double full_fraction
878 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
879 for (int i
= 0; i
< buckets_count
; ++i
)
881 if (symcount
< buckets
[i
] * full_fraction
)
886 if (for_gnu_hash_table
&& ret
< 2)
892 // The standard ELF hash function. This hash function must not
893 // change, as the dynamic linker uses it also.
896 Dynobj::elf_hash(const char* name
)
898 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
901 while ((c
= *nameu
++) != '\0')
904 uint32_t g
= h
& 0xf0000000;
908 // The ELF ABI says h &= ~g, but using xor is equivalent in
909 // this case (since g was set from h) and may save one
917 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
918 // DYNSYMS is a vector with all the global dynamic symbols.
919 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
923 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
924 unsigned int local_dynsym_count
,
925 unsigned char** pphash
,
926 unsigned int* phashlen
)
928 unsigned int dynsym_count
= dynsyms
.size();
930 // Get the hash values for all the symbols.
931 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
932 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
933 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
935 const unsigned int bucketcount
=
936 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
938 std::vector
<uint32_t> bucket(bucketcount
);
939 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
941 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
943 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
944 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
945 chain
[dynsym_index
] = bucket
[bucketpos
];
946 bucket
[bucketpos
] = dynsym_index
;
949 int size
= parameters
->target().hash_entry_size();
950 unsigned int hashlen
= ((2
955 unsigned char* phash
= new unsigned char[hashlen
];
957 bool big_endian
= parameters
->target().is_big_endian();
962 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
963 Dynobj::sized_create_elf_hash_table
<32, true>(bucket
, chain
, phash
,
971 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
972 Dynobj::sized_create_elf_hash_table
<32, false>(bucket
, chain
, phash
,
983 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
984 Dynobj::sized_create_elf_hash_table
<64, true>(bucket
, chain
, phash
,
992 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
993 Dynobj::sized_create_elf_hash_table
<64, false>(bucket
, chain
, phash
,
1004 *phashlen
= hashlen
;
1007 // Fill in an ELF hash table.
1009 template<int size
, bool big_endian
>
1011 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
1012 const std::vector
<uint32_t>& chain
,
1013 unsigned char* phash
,
1014 unsigned int hashlen
)
1016 unsigned char* p
= phash
;
1018 const unsigned int bucketcount
= bucket
.size();
1019 const unsigned int chaincount
= chain
.size();
1021 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bucketcount
);
1023 elfcpp::Swap
<size
, big_endian
>::writeval(p
, chaincount
);
1026 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1028 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bucket
[i
]);
1032 for (unsigned int i
= 0; i
< chaincount
; ++i
)
1034 elfcpp::Swap
<size
, big_endian
>::writeval(p
, chain
[i
]);
1038 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
1041 // The hash function used for the GNU hash table. This hash function
1042 // must not change, as the dynamic linker uses it also.
1045 Dynobj::gnu_hash(const char* name
)
1047 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
1050 while ((c
= *nameu
++) != '\0')
1051 h
= (h
<< 5) + h
+ c
;
1055 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
1056 // tables are an extension to ELF which are recognized by the GNU
1057 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
1058 // TARGET is the target. DYNSYMS is a vector with all the global
1059 // symbols which will be going into the dynamic symbol table.
1060 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
1064 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
1065 unsigned int local_dynsym_count
,
1066 unsigned char** pphash
,
1067 unsigned int* phashlen
)
1069 const unsigned int count
= dynsyms
.size();
1071 // Sort the dynamic symbols into two vectors. Symbols which we do
1072 // not want to put into the hash table we store into
1073 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
1074 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
1075 // and records the hash codes.
1077 std::vector
<Symbol
*> unhashed_dynsyms
;
1078 unhashed_dynsyms
.reserve(count
);
1080 std::vector
<Symbol
*> hashed_dynsyms
;
1081 hashed_dynsyms
.reserve(count
);
1083 std::vector
<uint32_t> dynsym_hashvals
;
1084 dynsym_hashvals
.reserve(count
);
1086 for (unsigned int i
= 0; i
< count
; ++i
)
1088 Symbol
* sym
= dynsyms
[i
];
1090 if (!sym
->needs_dynsym_value()
1091 && (sym
->is_undefined()
1092 || sym
->is_from_dynobj()
1093 || sym
->is_forced_local()))
1094 unhashed_dynsyms
.push_back(sym
);
1097 hashed_dynsyms
.push_back(sym
);
1098 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
1102 // Put the unhashed symbols at the start of the global portion of
1103 // the dynamic symbol table.
1104 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
1105 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
1106 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
1108 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
1109 ++unhashed_dynsym_index
;
1112 // For the actual data generation we call out to a templatized
1114 int size
= parameters
->target().get_size();
1115 bool big_endian
= parameters
->target().is_big_endian();
1120 #ifdef HAVE_TARGET_32_BIG
1121 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
1123 unhashed_dynsym_index
,
1132 #ifdef HAVE_TARGET_32_LITTLE
1133 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
1135 unhashed_dynsym_index
,
1143 else if (size
== 64)
1147 #ifdef HAVE_TARGET_64_BIG
1148 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1150 unhashed_dynsym_index
,
1159 #ifdef HAVE_TARGET_64_LITTLE
1160 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1162 unhashed_dynsym_index
,
1174 // Create the actual data for a GNU hash table. This is just a copy
1175 // of the code from the old GNU linker.
1177 template<int size
, bool big_endian
>
1179 Dynobj::sized_create_gnu_hash_table(
1180 const std::vector
<Symbol
*>& hashed_dynsyms
,
1181 const std::vector
<uint32_t>& dynsym_hashvals
,
1182 unsigned int unhashed_dynsym_count
,
1183 unsigned char** pphash
,
1184 unsigned int* phashlen
)
1186 if (hashed_dynsyms
.empty())
1188 // Special case for the empty hash table.
1189 unsigned int hashlen
= 5 * 4 + size
/ 8;
1190 unsigned char* phash
= new unsigned char[hashlen
];
1191 // One empty bucket.
1192 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1193 // Symbol index above unhashed symbols.
1194 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1195 // One word for bitmask.
1196 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1197 // Only bloom filter.
1198 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1200 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1201 // No hashes in only bucket.
1202 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1204 *phashlen
= hashlen
;
1210 const unsigned int bucketcount
=
1211 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1213 const unsigned int nsyms
= hashed_dynsyms
.size();
1215 uint32_t maskbitslog2
= 1;
1216 uint32_t x
= nsyms
>> 1;
1222 if (maskbitslog2
< 3)
1224 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1234 if (maskbitslog2
== 5)
1238 uint32_t mask
= (1U << shift1
) - 1U;
1239 uint32_t shift2
= maskbitslog2
;
1240 uint32_t maskbits
= 1U << maskbitslog2
;
1241 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1243 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1244 std::vector
<Word
> bitmask(maskwords
);
1245 std::vector
<uint32_t> counts(bucketcount
);
1246 std::vector
<uint32_t> indx(bucketcount
);
1247 uint32_t symindx
= unhashed_dynsym_count
;
1249 // Count the number of times each hash bucket is used.
1250 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1251 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1253 unsigned int cnt
= symindx
;
1254 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1260 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1261 hashlen
+= maskbits
/ 8;
1262 unsigned char* phash
= new unsigned char[hashlen
];
1264 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1265 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1266 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1267 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1269 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1270 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1273 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1275 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1279 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1281 Symbol
* sym
= hashed_dynsyms
[i
];
1282 uint32_t hashval
= dynsym_hashvals
[i
];
1284 unsigned int bucket
= hashval
% bucketcount
;
1285 unsigned int val
= ((hashval
>> shift1
)
1286 & ((maskbits
>> shift1
) - 1));
1287 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1288 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1289 val
= hashval
& ~ 1U;
1290 if (counts
[bucket
] == 1)
1292 // Last element terminates the chain.
1295 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1299 sym
->set_dynsym_index(indx
[bucket
]);
1304 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1306 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1310 *phashlen
= hashlen
;
1316 // Write this definition to a buffer for the output section.
1318 template<int size
, bool big_endian
>
1320 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1322 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1323 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1325 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1326 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1327 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1328 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0)
1329 | (this->is_info_
? elfcpp::VER_FLG_INFO
: 0));
1330 vd
.set_vd_ndx(this->index());
1331 vd
.set_vd_cnt(1 + this->deps_
.size());
1332 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1333 vd
.set_vd_aux(verdef_size
);
1334 vd
.set_vd_next(is_last
1336 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1339 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1340 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1341 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1344 Deps::const_iterator p
;
1346 for (p
= this->deps_
.begin(), i
= 0;
1347 p
!= this->deps_
.end();
1350 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1351 vda
.set_vda_name(dynpool
->get_offset(*p
));
1352 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1363 for (Need_versions::iterator p
= this->need_versions_
.begin();
1364 p
!= this->need_versions_
.end();
1369 // Add a new version to this file reference.
1372 Verneed::add_name(const char* name
)
1374 Verneed_version
* vv
= new Verneed_version(name
);
1375 this->need_versions_
.push_back(vv
);
1379 // Set the version indexes starting at INDEX.
1382 Verneed::finalize(unsigned int index
)
1384 for (Need_versions::iterator p
= this->need_versions_
.begin();
1385 p
!= this->need_versions_
.end();
1388 (*p
)->set_index(index
);
1394 // Write this list of referenced versions to a buffer for the output
1397 template<int size
, bool big_endian
>
1399 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1400 unsigned char* pb
) const
1402 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1403 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1405 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1406 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1407 vn
.set_vn_cnt(this->need_versions_
.size());
1408 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1409 vn
.set_vn_aux(verneed_size
);
1410 vn
.set_vn_next(is_last
1412 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1415 Need_versions::const_iterator p
;
1417 for (p
= this->need_versions_
.begin(), i
= 0;
1418 p
!= this->need_versions_
.end();
1421 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1422 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1423 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1424 vna
.set_vna_flags(0);
1425 vna
.set_vna_other((*p
)->index());
1426 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1427 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1436 // Versions methods.
1438 Versions::Versions(const Version_script_info
& version_script
,
1439 Stringpool
* dynpool
)
1440 : defs_(), needs_(), version_table_(),
1441 is_finalized_(false), version_script_(version_script
),
1442 needs_base_version_(true)
1444 if (!this->version_script_
.empty())
1446 // Parse the version script, and insert each declared version into
1447 // defs_ and version_table_.
1448 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1450 if (this->needs_base_version_
&& !versions
.empty())
1451 this->define_base_version(dynpool
);
1453 for (size_t k
= 0; k
< versions
.size(); ++k
)
1455 Stringpool::Key version_key
;
1456 const char* version
= dynpool
->add(versions
[k
].c_str(),
1457 true, &version_key
);
1458 Verdef
* const vd
= new Verdef(
1460 this->version_script_
.get_dependencies(version
),
1461 false, false, false, false);
1462 this->defs_
.push_back(vd
);
1463 Key
key(version_key
, 0);
1464 this->version_table_
.insert(std::make_pair(key
, vd
));
1469 Versions::~Versions()
1471 for (Defs::iterator p
= this->defs_
.begin();
1472 p
!= this->defs_
.end();
1476 for (Needs::iterator p
= this->needs_
.begin();
1477 p
!= this->needs_
.end();
1482 // Define the base version of a shared library. The base version definition
1483 // must be the first entry in defs_. We insert it lazily so that defs_ is
1484 // empty if no symbol versioning is used. Then layout can just drop the
1485 // version sections.
1488 Versions::define_base_version(Stringpool
* dynpool
)
1490 // If we do any versioning at all, we always need a base version, so
1491 // define that first. Nothing explicitly declares itself as part of base,
1492 // so it doesn't need to be in version_table_.
1493 gold_assert(this->defs_
.empty());
1494 const char* name
= parameters
->options().soname();
1496 name
= parameters
->options().output_file_name();
1497 name
= dynpool
->add(name
, false, NULL
);
1498 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1499 true, false, false, true);
1500 this->defs_
.push_back(vdbase
);
1501 this->needs_base_version_
= false;
1504 // Return the dynamic object which a symbol refers to.
1507 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1508 const Symbol
* sym
) const
1510 if (sym
->is_copied_from_dynobj())
1511 return symtab
->get_copy_source(sym
);
1514 Object
* object
= sym
->object();
1515 gold_assert(object
->is_dynamic());
1516 return static_cast<Dynobj
*>(object
);
1520 // Record version information for a symbol going into the dynamic
1524 Versions::record_version(const Symbol_table
* symtab
,
1525 Stringpool
* dynpool
, const Symbol
* sym
)
1527 gold_assert(!this->is_finalized_
);
1528 gold_assert(sym
->version() != NULL
);
1530 // A symbol defined as "sym@" is bound to an unspecified base version.
1531 if (sym
->version()[0] == '\0')
1534 Stringpool::Key version_key
;
1535 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1537 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1539 this->add_def(dynpool
, sym
, version
, version_key
);
1543 // This is a version reference.
1544 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1545 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1549 // We've found a symbol SYM defined in version VERSION.
1552 Versions::add_def(Stringpool
* dynpool
, const Symbol
* sym
, const char* version
,
1553 Stringpool::Key version_key
)
1555 Key
k(version_key
, 0);
1556 Version_base
* const vbnull
= NULL
;
1557 std::pair
<Version_table::iterator
, bool> ins
=
1558 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1562 // We already have an entry for this version.
1563 Version_base
* vb
= ins
.first
->second
;
1565 // We have now seen a symbol in this version, so it is not
1567 gold_assert(vb
!= NULL
);
1572 // If we are creating a shared object, it is an error to
1573 // find a definition of a symbol with a version which is not
1574 // in the version script.
1575 if (parameters
->options().shared())
1576 gold_error(_("symbol %s has undefined version %s"),
1577 sym
->demangled_name().c_str(), version
);
1579 // When creating a regular executable, automatically define
1581 if (this->needs_base_version_
)
1582 this->define_base_version(dynpool
);
1583 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1584 false, false, false, false);
1585 this->defs_
.push_back(vd
);
1586 ins
.first
->second
= vd
;
1590 // Add a reference to version NAME in file FILENAME.
1593 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1594 Stringpool::Key name_key
)
1596 Stringpool::Key filename_key
;
1597 filename
= dynpool
->add(filename
, true, &filename_key
);
1599 Key
k(name_key
, filename_key
);
1600 Version_base
* const vbnull
= NULL
;
1601 std::pair
<Version_table::iterator
, bool> ins
=
1602 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1606 // We already have an entry for this filename/version.
1610 // See whether we already have this filename. We don't expect many
1611 // version references, so we just do a linear search. This could be
1612 // replaced by a hash table.
1614 for (Needs::iterator p
= this->needs_
.begin();
1615 p
!= this->needs_
.end();
1618 if ((*p
)->filename() == filename
)
1627 // Create base version definition lazily for shared library.
1628 if (parameters
->options().shared() && this->needs_base_version_
)
1629 this->define_base_version(dynpool
);
1631 // We have a new filename.
1632 vn
= new Verneed(filename
);
1633 this->needs_
.push_back(vn
);
1636 ins
.first
->second
= vn
->add_name(name
);
1639 // Set the version indexes. Create a new dynamic version symbol for
1640 // each new version definition.
1643 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1644 std::vector
<Symbol
*>* syms
)
1646 gold_assert(!this->is_finalized_
);
1648 unsigned int vi
= 1;
1650 for (Defs::iterator p
= this->defs_
.begin();
1651 p
!= this->defs_
.end();
1654 (*p
)->set_index(vi
);
1657 // Create a version symbol if necessary.
1658 if (!(*p
)->is_symbol_created())
1660 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1662 Symbol_table::PREDEFINED
,
1666 elfcpp::STV_DEFAULT
, 0,
1668 vsym
->set_needs_dynsym_entry();
1669 vsym
->set_dynsym_index(dynsym_index
);
1670 vsym
->set_is_default();
1672 syms
->push_back(vsym
);
1673 // The name is already in the dynamic pool.
1677 // Index 1 is used for global symbols.
1680 gold_assert(this->defs_
.empty());
1684 for (Needs::iterator p
= this->needs_
.begin();
1685 p
!= this->needs_
.end();
1687 vi
= (*p
)->finalize(vi
);
1689 this->is_finalized_
= true;
1691 return dynsym_index
;
1694 // Return the version index to use for a symbol. This does two hash
1695 // table lookups: one in DYNPOOL and one in this->version_table_.
1696 // Another approach alternative would be store a pointer in SYM, which
1697 // would increase the size of the symbol table. Or perhaps we could
1698 // use a hash table from dynamic symbol pointer values to Version_base
1702 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1703 const Symbol
* sym
) const
1705 Stringpool::Key version_key
;
1706 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1707 gold_assert(version
!= NULL
);
1710 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1712 k
= Key(version_key
, 0);
1716 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1718 Stringpool::Key filename_key
;
1719 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1720 gold_assert(filename
!= NULL
);
1722 k
= Key(version_key
, filename_key
);
1725 Version_table::const_iterator p
= this->version_table_
.find(k
);
1726 gold_assert(p
!= this->version_table_
.end());
1728 return p
->second
->index();
1731 // Return an allocated buffer holding the contents of the symbol
1734 template<int size
, bool big_endian
>
1736 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1737 const Stringpool
* dynpool
,
1738 unsigned int local_symcount
,
1739 const std::vector
<Symbol
*>& syms
,
1741 unsigned int* psize
) const
1743 gold_assert(this->is_finalized_
);
1745 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1746 unsigned char* pbuf
= new unsigned char[sz
];
1748 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1749 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1750 elfcpp::VER_NDX_LOCAL
);
1752 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1756 unsigned int version_index
;
1757 const char* version
= (*p
)->version();
1758 if (version
== NULL
)
1760 if ((*p
)->is_defined() && !(*p
)->is_from_dynobj())
1761 version_index
= elfcpp::VER_NDX_GLOBAL
;
1763 version_index
= elfcpp::VER_NDX_LOCAL
;
1765 else if (version
[0] == '\0')
1766 version_index
= elfcpp::VER_NDX_GLOBAL
;
1768 version_index
= this->version_index(symtab
, dynpool
, *p
);
1769 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1771 if ((*p
)->version() != NULL
1772 && (*p
)->is_defined()
1773 && !(*p
)->is_default()
1774 && !(*p
)->from_dyn())
1775 version_index
|= elfcpp::VERSYM_HIDDEN
;
1776 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1784 // Return an allocated buffer holding the contents of the version
1785 // definition section.
1787 template<int size
, bool big_endian
>
1789 Versions::def_section_contents(const Stringpool
* dynpool
,
1790 unsigned char** pp
, unsigned int* psize
,
1791 unsigned int* pentries
) const
1793 gold_assert(this->is_finalized_
);
1794 gold_assert(!this->defs_
.empty());
1796 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1797 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1799 unsigned int sz
= 0;
1800 for (Defs::const_iterator p
= this->defs_
.begin();
1801 p
!= this->defs_
.end();
1804 sz
+= verdef_size
+ verdaux_size
;
1805 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1808 unsigned char* pbuf
= new unsigned char[sz
];
1810 unsigned char* pb
= pbuf
;
1811 Defs::const_iterator p
;
1813 for (p
= this->defs_
.begin(), i
= 0;
1814 p
!= this->defs_
.end();
1816 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1817 i
+ 1 >= this->defs_
.size(),
1820 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1824 *pentries
= this->defs_
.size();
1827 // Return an allocated buffer holding the contents of the version
1828 // reference section.
1830 template<int size
, bool big_endian
>
1832 Versions::need_section_contents(const Stringpool
* dynpool
,
1833 unsigned char** pp
, unsigned int* psize
,
1834 unsigned int* pentries
) const
1836 gold_assert(this->is_finalized_
);
1837 gold_assert(!this->needs_
.empty());
1839 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1840 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1842 unsigned int sz
= 0;
1843 for (Needs::const_iterator p
= this->needs_
.begin();
1844 p
!= this->needs_
.end();
1848 sz
+= (*p
)->count_versions() * vernaux_size
;
1851 unsigned char* pbuf
= new unsigned char[sz
];
1853 unsigned char* pb
= pbuf
;
1854 Needs::const_iterator p
;
1856 for (p
= this->needs_
.begin(), i
= 0;
1857 p
!= this->needs_
.end();
1859 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1860 i
+ 1 >= this->needs_
.size(),
1863 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1867 *pentries
= this->needs_
.size();
1870 // Instantiate the templates we need. We could use the configure
1871 // script to restrict this to only the ones for implemented targets.
1873 #ifdef HAVE_TARGET_32_LITTLE
1875 class Sized_dynobj
<32, false>;
1878 #ifdef HAVE_TARGET_32_BIG
1880 class Sized_dynobj
<32, true>;
1883 #ifdef HAVE_TARGET_64_LITTLE
1885 class Sized_dynobj
<64, false>;
1888 #ifdef HAVE_TARGET_64_BIG
1890 class Sized_dynobj
<64, true>;
1893 #ifdef HAVE_TARGET_32_LITTLE
1896 Versions::symbol_section_contents
<32, false>(
1897 const Symbol_table
*,
1900 const std::vector
<Symbol
*>&,
1902 unsigned int*) const;
1905 #ifdef HAVE_TARGET_32_BIG
1908 Versions::symbol_section_contents
<32, true>(
1909 const Symbol_table
*,
1912 const std::vector
<Symbol
*>&,
1914 unsigned int*) const;
1917 #ifdef HAVE_TARGET_64_LITTLE
1920 Versions::symbol_section_contents
<64, false>(
1921 const Symbol_table
*,
1924 const std::vector
<Symbol
*>&,
1926 unsigned int*) const;
1929 #ifdef HAVE_TARGET_64_BIG
1932 Versions::symbol_section_contents
<64, true>(
1933 const Symbol_table
*,
1936 const std::vector
<Symbol
*>&,
1938 unsigned int*) const;
1941 #ifdef HAVE_TARGET_32_LITTLE
1944 Versions::def_section_contents
<32, false>(
1948 unsigned int*) const;
1951 #ifdef HAVE_TARGET_32_BIG
1954 Versions::def_section_contents
<32, true>(
1958 unsigned int*) const;
1961 #ifdef HAVE_TARGET_64_LITTLE
1964 Versions::def_section_contents
<64, false>(
1968 unsigned int*) const;
1971 #ifdef HAVE_TARGET_64_BIG
1974 Versions::def_section_contents
<64, true>(
1978 unsigned int*) const;
1981 #ifdef HAVE_TARGET_32_LITTLE
1984 Versions::need_section_contents
<32, false>(
1988 unsigned int*) const;
1991 #ifdef HAVE_TARGET_32_BIG
1994 Versions::need_section_contents
<32, true>(
1998 unsigned int*) const;
2001 #ifdef HAVE_TARGET_64_LITTLE
2004 Versions::need_section_contents
<64, false>(
2008 unsigned int*) const;
2011 #ifdef HAVE_TARGET_64_BIG
2014 Versions::need_section_contents
<64, true>(
2018 unsigned int*) const;
2021 } // End namespace gold.