1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
39 // Sets up the default soname_ to use, in the (rare) cases we never
40 // see a DT_SONAME entry.
42 Dynobj::Dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
)
43 : Object(name
, input_file
, true, offset
),
45 unknown_needed_(UNKNOWN_NEEDED_UNSET
)
47 // This will be overridden by a DT_SONAME entry, hopefully. But if
48 // we never see a DT_SONAME entry, our rule is to use the dynamic
49 // object's filename. The only exception is when the dynamic object
50 // is part of an archive (so the filename is the archive's
51 // filename). In that case, we use just the dynobj's name-in-archive.
52 this->soname_
= this->input_file()->found_name();
53 if (this->offset() != 0)
55 std::string::size_type open_paren
= this->name().find('(');
56 std::string::size_type close_paren
= this->name().find(')');
57 if (open_paren
!= std::string::npos
&& close_paren
!= std::string::npos
)
59 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
60 this->soname_
= this->name().substr(open_paren
+ 1,
61 close_paren
- (open_paren
+ 1));
66 // Class Sized_dynobj.
68 template<int size
, bool big_endian
>
69 Sized_dynobj
<size
, big_endian
>::Sized_dynobj(
70 const std::string
& name
,
71 Input_file
* input_file
,
73 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
74 : Dynobj(name
, input_file
, offset
),
75 elf_file_(this, ehdr
),
84 template<int size
, bool big_endian
>
86 Sized_dynobj
<size
, big_endian
>::setup()
88 const unsigned int shnum
= this->elf_file_
.shnum();
89 this->set_shnum(shnum
);
92 // Find the SHT_DYNSYM section and the various version sections, and
93 // the dynamic section, given the section headers.
95 template<int size
, bool big_endian
>
97 Sized_dynobj
<size
, big_endian
>::find_dynsym_sections(
98 const unsigned char* pshdrs
,
99 unsigned int* pversym_shndx
,
100 unsigned int* pverdef_shndx
,
101 unsigned int* pverneed_shndx
,
102 unsigned int* pdynamic_shndx
)
104 *pversym_shndx
= -1U;
105 *pverdef_shndx
= -1U;
106 *pverneed_shndx
= -1U;
107 *pdynamic_shndx
= -1U;
109 unsigned int xindex_shndx
= 0;
110 unsigned int xindex_link
= 0;
111 const unsigned int shnum
= this->shnum();
112 const unsigned char* p
= pshdrs
;
113 for (unsigned int i
= 0; i
< shnum
; ++i
, p
+= This::shdr_size
)
115 typename
This::Shdr
shdr(p
);
118 switch (shdr
.get_sh_type())
120 case elfcpp::SHT_DYNSYM
:
121 this->dynsym_shndx_
= i
;
122 if (xindex_shndx
> 0 && xindex_link
== i
)
124 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
125 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
127 this->set_xindex(xindex
);
131 case elfcpp::SHT_GNU_versym
:
134 case elfcpp::SHT_GNU_verdef
:
137 case elfcpp::SHT_GNU_verneed
:
140 case elfcpp::SHT_DYNAMIC
:
143 case elfcpp::SHT_SYMTAB_SHNDX
:
145 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
146 if (xindex_link
== this->dynsym_shndx_
)
148 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
149 xindex
->read_symtab_xindex
<size
, big_endian
>(this, xindex_shndx
,
151 this->set_xindex(xindex
);
164 this->error(_("unexpected duplicate type %u section: %u, %u"),
165 shdr
.get_sh_type(), *pi
, i
);
171 // Read the contents of section SHNDX. PSHDRS points to the section
172 // headers. TYPE is the expected section type. LINK is the expected
173 // section link. Store the data in *VIEW and *VIEW_SIZE. The
174 // section's sh_info field is stored in *VIEW_INFO.
176 template<int size
, bool big_endian
>
178 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
179 const unsigned char* pshdrs
,
184 section_size_type
* view_size
,
185 unsigned int* view_info
)
195 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
197 gold_assert(shdr
.get_sh_type() == type
);
199 if (this->adjust_shndx(shdr
.get_sh_link()) != link
)
200 this->error(_("unexpected link in section %u header: %u != %u"),
201 shndx
, this->adjust_shndx(shdr
.get_sh_link()), link
);
203 *view
= this->get_lasting_view(shdr
.get_sh_offset(), shdr
.get_sh_size(),
205 *view_size
= convert_to_section_size_type(shdr
.get_sh_size());
206 *view_info
= shdr
.get_sh_info();
209 // Read the dynamic tags. Set the soname field if this shared object
210 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
211 // the section headers. DYNAMIC_SHNDX is the section index of the
212 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
213 // section index and contents of a string table which may be the one
214 // associated with the SHT_DYNAMIC section.
216 template<int size
, bool big_endian
>
218 Sized_dynobj
<size
, big_endian
>::read_dynamic(const unsigned char* pshdrs
,
219 unsigned int dynamic_shndx
,
220 unsigned int strtab_shndx
,
221 const unsigned char* strtabu
,
224 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
225 gold_assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
227 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
228 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
229 dynamic_size
, true, false);
231 const unsigned int link
= this->adjust_shndx(dynamicshdr
.get_sh_link());
232 if (link
!= strtab_shndx
)
234 if (link
>= this->shnum())
236 this->error(_("DYNAMIC section %u link out of range: %u"),
237 dynamic_shndx
, link
);
241 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
242 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
244 this->error(_("DYNAMIC section %u link %u is not a strtab"),
245 dynamic_shndx
, link
);
249 strtab_size
= strtabshdr
.get_sh_size();
250 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
, false,
254 const char* const strtab
= reinterpret_cast<const char*>(strtabu
);
256 for (const unsigned char* p
= pdynamic
;
257 p
< pdynamic
+ dynamic_size
;
260 typename
This::Dyn
dyn(p
);
262 switch (dyn
.get_d_tag())
264 case elfcpp::DT_NULL
:
265 // We should always see DT_NULL at the end of the dynamic
269 case elfcpp::DT_SONAME
:
271 off_t val
= dyn
.get_d_val();
272 if (val
>= strtab_size
)
273 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
274 static_cast<long long>(val
),
275 static_cast<long long>(strtab_size
));
277 this->set_soname_string(strtab
+ val
);
281 case elfcpp::DT_NEEDED
:
283 off_t val
= dyn
.get_d_val();
284 if (val
>= strtab_size
)
285 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
286 static_cast<long long>(val
),
287 static_cast<long long>(strtab_size
));
289 this->add_needed(strtab
+ val
);
298 this->error(_("missing DT_NULL in dynamic segment"));
301 // Read the symbols and sections from a dynamic object. We read the
302 // dynamic symbols, not the normal symbols.
304 template<int size
, bool big_endian
>
306 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
308 this->read_section_data(&this->elf_file_
, sd
);
310 const unsigned char* const pshdrs
= sd
->section_headers
->data();
312 unsigned int versym_shndx
;
313 unsigned int verdef_shndx
;
314 unsigned int verneed_shndx
;
315 unsigned int dynamic_shndx
;
316 this->find_dynsym_sections(pshdrs
, &versym_shndx
, &verdef_shndx
,
317 &verneed_shndx
, &dynamic_shndx
);
319 unsigned int strtab_shndx
= -1U;
322 sd
->symbols_size
= 0;
323 sd
->external_symbols_offset
= 0;
324 sd
->symbol_names
= NULL
;
325 sd
->symbol_names_size
= 0;
332 sd
->verneed_size
= 0;
333 sd
->verneed_info
= 0;
335 if (this->dynsym_shndx_
!= -1U)
337 // Get the dynamic symbols.
338 typename
This::Shdr
dynsymshdr(pshdrs
339 + this->dynsym_shndx_
* This::shdr_size
);
340 gold_assert(dynsymshdr
.get_sh_type() == elfcpp::SHT_DYNSYM
);
342 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
343 dynsymshdr
.get_sh_size(), true,
346 convert_to_section_size_type(dynsymshdr
.get_sh_size());
348 // Get the symbol names.
349 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
350 if (strtab_shndx
>= this->shnum())
352 this->error(_("invalid dynamic symbol table name index: %u"),
356 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
357 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
359 this->error(_("dynamic symbol table name section "
360 "has wrong type: %u"),
361 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
365 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
366 strtabshdr
.get_sh_size(),
368 sd
->symbol_names_size
=
369 convert_to_section_size_type(strtabshdr
.get_sh_size());
371 // Get the version information.
374 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
376 &sd
->versym
, &sd
->versym_size
, &dummy
);
378 // We require that the version definition and need section link
379 // to the same string table as the dynamic symbol table. This
380 // is not a technical requirement, but it always happens in
381 // practice. We could change this if necessary.
383 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
384 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
387 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
388 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
392 // Read the SHT_DYNAMIC section to find whether this shared object
393 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
394 // doesn't really have anything to do with reading the symbols, but
395 // this is a convenient place to do it.
396 if (dynamic_shndx
!= -1U)
397 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
398 (sd
->symbol_names
== NULL
400 : sd
->symbol_names
->data()),
401 sd
->symbol_names_size
);
404 // Return the Xindex structure to use for object with lots of
407 template<int size
, bool big_endian
>
409 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
411 gold_assert(this->dynsym_shndx_
!= -1U);
412 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
413 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
417 // Lay out the input sections for a dynamic object. We don't want to
418 // include sections from a dynamic object, so all that we actually do
419 // here is check for .gnu.warning and .note.GNU-split-stack sections.
421 template<int size
, bool big_endian
>
423 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
425 Read_symbols_data
* sd
)
427 const unsigned int shnum
= this->shnum();
431 // Get the section headers.
432 const unsigned char* pshdrs
= sd
->section_headers
->data();
434 // Get the section names.
435 const unsigned char* pnamesu
= sd
->section_names
->data();
436 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
438 // Skip the first, dummy, section.
439 pshdrs
+= This::shdr_size
;
440 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
442 typename
This::Shdr
shdr(pshdrs
);
444 if (shdr
.get_sh_name() >= sd
->section_names_size
)
446 this->error(_("bad section name offset for section %u: %lu"),
447 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
451 const char* name
= pnames
+ shdr
.get_sh_name();
453 this->handle_gnu_warning_section(name
, i
, symtab
);
454 this->handle_split_stack_section(name
);
457 delete sd
->section_headers
;
458 sd
->section_headers
= NULL
;
459 delete sd
->section_names
;
460 sd
->section_names
= NULL
;
463 // Add an entry to the vector mapping version numbers to version
466 template<int size
, bool big_endian
>
468 Sized_dynobj
<size
, big_endian
>::set_version_map(
469 Version_map
* version_map
,
471 const char* name
) const
473 if (ndx
>= version_map
->size())
474 version_map
->resize(ndx
+ 1);
475 if ((*version_map
)[ndx
] != NULL
)
476 this->error(_("duplicate definition for version %u"), ndx
);
477 (*version_map
)[ndx
] = name
;
480 // Add mappings for the version definitions to VERSION_MAP.
482 template<int size
, bool big_endian
>
484 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
485 Read_symbols_data
* sd
,
486 Version_map
* version_map
) const
488 if (sd
->verdef
== NULL
)
491 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
492 section_size_type names_size
= sd
->symbol_names_size
;
494 const unsigned char* pverdef
= sd
->verdef
->data();
495 section_size_type verdef_size
= sd
->verdef_size
;
496 const unsigned int count
= sd
->verdef_info
;
498 const unsigned char* p
= pverdef
;
499 for (unsigned int i
= 0; i
< count
; ++i
)
501 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
503 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
505 this->error(_("unexpected verdef version %u"),
506 verdef
.get_vd_version());
510 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
512 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
515 // The first Verdaux holds the name of this version. Subsequent
516 // ones are versions that this one depends upon, which we don't
518 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
521 this->error(_("verdef vd_cnt field too small: %u"),
522 static_cast<unsigned int>(vd_cnt
));
526 const section_size_type vd_aux
= verdef
.get_vd_aux();
527 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
529 this->error(_("verdef vd_aux field out of range: %u"),
530 static_cast<unsigned int>(vd_aux
));
534 const unsigned char* pvda
= p
+ vd_aux
;
535 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
537 const section_size_type vda_name
= verdaux
.get_vda_name();
538 if (vda_name
>= names_size
)
540 this->error(_("verdaux vda_name field out of range: %u"),
541 static_cast<unsigned int>(vda_name
));
545 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
547 const section_size_type vd_next
= verdef
.get_vd_next();
548 if ((p
- pverdef
) + vd_next
>= verdef_size
)
550 this->error(_("verdef vd_next field out of range: %u"),
551 static_cast<unsigned int>(vd_next
));
559 // Add mappings for the required versions to VERSION_MAP.
561 template<int size
, bool big_endian
>
563 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
564 Read_symbols_data
* sd
,
565 Version_map
* version_map
) const
567 if (sd
->verneed
== NULL
)
570 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
571 section_size_type names_size
= sd
->symbol_names_size
;
573 const unsigned char* pverneed
= sd
->verneed
->data();
574 const section_size_type verneed_size
= sd
->verneed_size
;
575 const unsigned int count
= sd
->verneed_info
;
577 const unsigned char* p
= pverneed
;
578 for (unsigned int i
= 0; i
< count
; ++i
)
580 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
582 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
584 this->error(_("unexpected verneed version %u"),
585 verneed
.get_vn_version());
589 const section_size_type vn_aux
= verneed
.get_vn_aux();
591 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
593 this->error(_("verneed vn_aux field out of range: %u"),
594 static_cast<unsigned int>(vn_aux
));
598 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
599 const unsigned char* pvna
= p
+ vn_aux
;
600 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
602 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
604 const unsigned int vna_name
= vernaux
.get_vna_name();
605 if (vna_name
>= names_size
)
607 this->error(_("vernaux vna_name field out of range: %u"),
608 static_cast<unsigned int>(vna_name
));
612 this->set_version_map(version_map
, vernaux
.get_vna_other(),
615 const section_size_type vna_next
= vernaux
.get_vna_next();
616 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
618 this->error(_("verneed vna_next field out of range: %u"),
619 static_cast<unsigned int>(vna_next
));
626 const section_size_type vn_next
= verneed
.get_vn_next();
627 if ((p
- pverneed
) + vn_next
>= verneed_size
)
629 this->error(_("verneed vn_next field out of range: %u"),
630 static_cast<unsigned int>(vn_next
));
638 // Create a vector mapping version numbers to version strings.
640 template<int size
, bool big_endian
>
642 Sized_dynobj
<size
, big_endian
>::make_version_map(
643 Read_symbols_data
* sd
,
644 Version_map
* version_map
) const
646 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
649 // A guess at the maximum version number we will see. If this is
650 // wrong we will be less efficient but still correct.
651 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
653 this->make_verdef_map(sd
, version_map
);
654 this->make_verneed_map(sd
, version_map
);
657 // Add the dynamic symbols to the symbol table.
659 template<int size
, bool big_endian
>
661 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
662 Read_symbols_data
* sd
,
665 if (sd
->symbols
== NULL
)
667 gold_assert(sd
->symbol_names
== NULL
);
668 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
669 && sd
->verneed
== NULL
);
673 const int sym_size
= This::sym_size
;
674 const size_t symcount
= sd
->symbols_size
/ sym_size
;
675 gold_assert(sd
->external_symbols_offset
== 0);
676 if (symcount
* sym_size
!= sd
->symbols_size
)
678 this->error(_("size of dynamic symbols is not multiple of symbol size"));
682 Version_map version_map
;
683 this->make_version_map(sd
, &version_map
);
685 // If printing symbol counts, we want to track symbols.
687 if (parameters
->options().user_set_print_symbol_counts())
689 this->symbols_
= new Symbols();
690 this->symbols_
->resize(symcount
);
693 const char* sym_names
=
694 reinterpret_cast<const char*>(sd
->symbol_names
->data());
695 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
696 sym_names
, sd
->symbol_names_size
,
699 : sd
->versym
->data()),
703 &this->defined_count_
);
707 delete sd
->symbol_names
;
708 sd
->symbol_names
= NULL
;
709 if (sd
->versym
!= NULL
)
714 if (sd
->verdef
!= NULL
)
719 if (sd
->verneed
!= NULL
)
725 // This is normally the last time we will read any data from this
727 this->clear_view_cache_marks();
730 // Get symbol counts.
732 template<int size
, bool big_endian
>
734 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
739 *defined
= this->defined_count_
;
741 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
742 p
!= this->symbols_
->end();
745 && (*p
)->source() == Symbol::FROM_OBJECT
746 && (*p
)->object() == this
747 && (*p
)->is_defined()
748 && (*p
)->dynsym_index() != -1U)
753 // Given a vector of hash codes, compute the number of hash buckets to
757 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
758 bool for_gnu_hash_table
)
760 // FIXME: Implement optional hash table optimization.
762 // Array used to determine the number of hash table buckets to use
763 // based on the number of symbols there are. If there are fewer
764 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
765 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
766 // use more than 262147 buckets. This is straight from the old GNU
768 static const unsigned int buckets
[] =
770 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
771 16411, 32771, 65537, 131101, 262147
773 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
775 unsigned int symcount
= hashcodes
.size();
776 unsigned int ret
= 1;
777 const double full_fraction
778 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
779 for (int i
= 0; i
< buckets_count
; ++i
)
781 if (symcount
< buckets
[i
] * full_fraction
)
786 if (for_gnu_hash_table
&& ret
< 2)
792 // The standard ELF hash function. This hash function must not
793 // change, as the dynamic linker uses it also.
796 Dynobj::elf_hash(const char* name
)
798 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
801 while ((c
= *nameu
++) != '\0')
804 uint32_t g
= h
& 0xf0000000;
808 // The ELF ABI says h &= ~g, but using xor is equivalent in
809 // this case (since g was set from h) and may save one
817 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
818 // DYNSYMS is a vector with all the global dynamic symbols.
819 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
823 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
824 unsigned int local_dynsym_count
,
825 unsigned char** pphash
,
826 unsigned int* phashlen
)
828 unsigned int dynsym_count
= dynsyms
.size();
830 // Get the hash values for all the symbols.
831 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
832 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
833 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
835 const unsigned int bucketcount
=
836 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
838 std::vector
<uint32_t> bucket(bucketcount
);
839 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
841 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
843 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
844 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
845 chain
[dynsym_index
] = bucket
[bucketpos
];
846 bucket
[bucketpos
] = dynsym_index
;
849 unsigned int hashlen
= ((2
854 unsigned char* phash
= new unsigned char[hashlen
];
856 if (parameters
->target().is_big_endian())
858 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
859 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
867 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
868 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
879 // Fill in an ELF hash table.
881 template<bool big_endian
>
883 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
884 const std::vector
<uint32_t>& chain
,
885 unsigned char* phash
,
886 unsigned int hashlen
)
888 unsigned char* p
= phash
;
890 const unsigned int bucketcount
= bucket
.size();
891 const unsigned int chaincount
= chain
.size();
893 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
895 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
898 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
900 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
904 for (unsigned int i
= 0; i
< chaincount
; ++i
)
906 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
910 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
913 // The hash function used for the GNU hash table. This hash function
914 // must not change, as the dynamic linker uses it also.
917 Dynobj::gnu_hash(const char* name
)
919 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
922 while ((c
= *nameu
++) != '\0')
923 h
= (h
<< 5) + h
+ c
;
927 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
928 // tables are an extension to ELF which are recognized by the GNU
929 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
930 // TARGET is the target. DYNSYMS is a vector with all the global
931 // symbols which will be going into the dynamic symbol table.
932 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
936 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
937 unsigned int local_dynsym_count
,
938 unsigned char** pphash
,
939 unsigned int* phashlen
)
941 const unsigned int count
= dynsyms
.size();
943 // Sort the dynamic symbols into two vectors. Symbols which we do
944 // not want to put into the hash table we store into
945 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
946 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
947 // and records the hash codes.
949 std::vector
<Symbol
*> unhashed_dynsyms
;
950 unhashed_dynsyms
.reserve(count
);
952 std::vector
<Symbol
*> hashed_dynsyms
;
953 hashed_dynsyms
.reserve(count
);
955 std::vector
<uint32_t> dynsym_hashvals
;
956 dynsym_hashvals
.reserve(count
);
958 for (unsigned int i
= 0; i
< count
; ++i
)
960 Symbol
* sym
= dynsyms
[i
];
962 if (!sym
->needs_dynsym_value()
963 && (sym
->is_undefined()
964 || sym
->is_from_dynobj()
965 || sym
->is_forced_local()))
966 unhashed_dynsyms
.push_back(sym
);
969 hashed_dynsyms
.push_back(sym
);
970 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
974 // Put the unhashed symbols at the start of the global portion of
975 // the dynamic symbol table.
976 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
977 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
978 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
980 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
981 ++unhashed_dynsym_index
;
984 // For the actual data generation we call out to a templatized
986 int size
= parameters
->target().get_size();
987 bool big_endian
= parameters
->target().is_big_endian();
992 #ifdef HAVE_TARGET_32_BIG
993 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
995 unhashed_dynsym_index
,
1004 #ifdef HAVE_TARGET_32_LITTLE
1005 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
1007 unhashed_dynsym_index
,
1015 else if (size
== 64)
1019 #ifdef HAVE_TARGET_64_BIG
1020 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1022 unhashed_dynsym_index
,
1031 #ifdef HAVE_TARGET_64_LITTLE
1032 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1034 unhashed_dynsym_index
,
1046 // Create the actual data for a GNU hash table. This is just a copy
1047 // of the code from the old GNU linker.
1049 template<int size
, bool big_endian
>
1051 Dynobj::sized_create_gnu_hash_table(
1052 const std::vector
<Symbol
*>& hashed_dynsyms
,
1053 const std::vector
<uint32_t>& dynsym_hashvals
,
1054 unsigned int unhashed_dynsym_count
,
1055 unsigned char** pphash
,
1056 unsigned int* phashlen
)
1058 if (hashed_dynsyms
.empty())
1060 // Special case for the empty hash table.
1061 unsigned int hashlen
= 5 * 4 + size
/ 8;
1062 unsigned char* phash
= new unsigned char[hashlen
];
1063 // One empty bucket.
1064 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1065 // Symbol index above unhashed symbols.
1066 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1067 // One word for bitmask.
1068 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1069 // Only bloom filter.
1070 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1072 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1073 // No hashes in only bucket.
1074 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1076 *phashlen
= hashlen
;
1082 const unsigned int bucketcount
=
1083 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1085 const unsigned int nsyms
= hashed_dynsyms
.size();
1087 uint32_t maskbitslog2
= 1;
1088 uint32_t x
= nsyms
>> 1;
1094 if (maskbitslog2
< 3)
1096 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1106 if (maskbitslog2
== 5)
1110 uint32_t mask
= (1U << shift1
) - 1U;
1111 uint32_t shift2
= maskbitslog2
;
1112 uint32_t maskbits
= 1U << maskbitslog2
;
1113 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1115 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1116 std::vector
<Word
> bitmask(maskwords
);
1117 std::vector
<uint32_t> counts(bucketcount
);
1118 std::vector
<uint32_t> indx(bucketcount
);
1119 uint32_t symindx
= unhashed_dynsym_count
;
1121 // Count the number of times each hash bucket is used.
1122 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1123 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1125 unsigned int cnt
= symindx
;
1126 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1132 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1133 hashlen
+= maskbits
/ 8;
1134 unsigned char* phash
= new unsigned char[hashlen
];
1136 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1137 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1138 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1139 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1141 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1142 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1145 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1147 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1151 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1153 Symbol
* sym
= hashed_dynsyms
[i
];
1154 uint32_t hashval
= dynsym_hashvals
[i
];
1156 unsigned int bucket
= hashval
% bucketcount
;
1157 unsigned int val
= ((hashval
>> shift1
)
1158 & ((maskbits
>> shift1
) - 1));
1159 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1160 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1161 val
= hashval
& ~ 1U;
1162 if (counts
[bucket
] == 1)
1164 // Last element terminates the chain.
1167 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1171 sym
->set_dynsym_index(indx
[bucket
]);
1176 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1178 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1182 *phashlen
= hashlen
;
1188 // Write this definition to a buffer for the output section.
1190 template<int size
, bool big_endian
>
1192 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1194 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1195 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1197 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1198 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1199 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1200 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0));
1201 vd
.set_vd_ndx(this->index());
1202 vd
.set_vd_cnt(1 + this->deps_
.size());
1203 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1204 vd
.set_vd_aux(verdef_size
);
1205 vd
.set_vd_next(is_last
1207 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1210 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1211 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1212 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1215 Deps::const_iterator p
;
1217 for (p
= this->deps_
.begin(), i
= 0;
1218 p
!= this->deps_
.end();
1221 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1222 vda
.set_vda_name(dynpool
->get_offset(*p
));
1223 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1234 for (Need_versions::iterator p
= this->need_versions_
.begin();
1235 p
!= this->need_versions_
.end();
1240 // Add a new version to this file reference.
1243 Verneed::add_name(const char* name
)
1245 Verneed_version
* vv
= new Verneed_version(name
);
1246 this->need_versions_
.push_back(vv
);
1250 // Set the version indexes starting at INDEX.
1253 Verneed::finalize(unsigned int index
)
1255 for (Need_versions::iterator p
= this->need_versions_
.begin();
1256 p
!= this->need_versions_
.end();
1259 (*p
)->set_index(index
);
1265 // Write this list of referenced versions to a buffer for the output
1268 template<int size
, bool big_endian
>
1270 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1271 unsigned char* pb
) const
1273 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1274 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1276 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1277 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1278 vn
.set_vn_cnt(this->need_versions_
.size());
1279 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1280 vn
.set_vn_aux(verneed_size
);
1281 vn
.set_vn_next(is_last
1283 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1286 Need_versions::const_iterator p
;
1288 for (p
= this->need_versions_
.begin(), i
= 0;
1289 p
!= this->need_versions_
.end();
1292 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1293 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1294 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1295 vna
.set_vna_flags(0);
1296 vna
.set_vna_other((*p
)->index());
1297 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1298 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1307 // Versions methods.
1309 Versions::Versions(const Version_script_info
& version_script
,
1310 Stringpool
* dynpool
)
1311 : defs_(), needs_(), version_table_(),
1312 is_finalized_(false), version_script_(version_script
),
1313 needs_base_version_(parameters
->options().shared())
1315 if (!this->version_script_
.empty())
1317 // Parse the version script, and insert each declared version into
1318 // defs_ and version_table_.
1319 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1321 if (this->needs_base_version_
&& !versions
.empty())
1322 this->define_base_version(dynpool
);
1324 for (size_t k
= 0; k
< versions
.size(); ++k
)
1326 Stringpool::Key version_key
;
1327 const char* version
= dynpool
->add(versions
[k
].c_str(),
1328 true, &version_key
);
1329 Verdef
* const vd
= new Verdef(
1331 this->version_script_
.get_dependencies(version
),
1332 false, false, false);
1333 this->defs_
.push_back(vd
);
1334 Key
key(version_key
, 0);
1335 this->version_table_
.insert(std::make_pair(key
, vd
));
1340 Versions::~Versions()
1342 for (Defs::iterator p
= this->defs_
.begin();
1343 p
!= this->defs_
.end();
1347 for (Needs::iterator p
= this->needs_
.begin();
1348 p
!= this->needs_
.end();
1353 // Define the base version of a shared library. The base version definition
1354 // must be the first entry in defs_. We insert it lazily so that defs_ is
1355 // empty if no symbol versioning is used. Then layout can just drop the
1356 // version sections.
1359 Versions::define_base_version(Stringpool
* dynpool
)
1361 // If we do any versioning at all, we always need a base version, so
1362 // define that first. Nothing explicitly declares itself as part of base,
1363 // so it doesn't need to be in version_table_.
1364 gold_assert(this->defs_
.empty());
1365 const char* name
= parameters
->options().soname();
1367 name
= parameters
->options().output_file_name();
1368 name
= dynpool
->add(name
, false, NULL
);
1369 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1371 this->defs_
.push_back(vdbase
);
1372 this->needs_base_version_
= false;
1375 // Return the dynamic object which a symbol refers to.
1378 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1379 const Symbol
* sym
) const
1381 if (sym
->is_copied_from_dynobj())
1382 return symtab
->get_copy_source(sym
);
1385 Object
* object
= sym
->object();
1386 gold_assert(object
->is_dynamic());
1387 return static_cast<Dynobj
*>(object
);
1391 // Record version information for a symbol going into the dynamic
1395 Versions::record_version(const Symbol_table
* symtab
,
1396 Stringpool
* dynpool
, const Symbol
* sym
)
1398 gold_assert(!this->is_finalized_
);
1399 gold_assert(sym
->version() != NULL
);
1401 Stringpool::Key version_key
;
1402 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1404 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1406 if (parameters
->options().shared())
1407 this->add_def(sym
, version
, version_key
);
1411 // This is a version reference.
1412 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1413 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1417 // We've found a symbol SYM defined in version VERSION.
1420 Versions::add_def(const Symbol
* sym
, const char* version
,
1421 Stringpool::Key version_key
)
1423 Key
k(version_key
, 0);
1424 Version_base
* const vbnull
= NULL
;
1425 std::pair
<Version_table::iterator
, bool> ins
=
1426 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1430 // We already have an entry for this version.
1431 Version_base
* vb
= ins
.first
->second
;
1433 // We have now seen a symbol in this version, so it is not
1435 gold_assert(vb
!= NULL
);
1440 // If we are creating a shared object, it is an error to
1441 // find a definition of a symbol with a version which is not
1442 // in the version script.
1443 if (parameters
->options().shared())
1444 gold_error(_("symbol %s has undefined version %s"),
1445 sym
->demangled_name().c_str(), version
);
1447 // We only insert a base version for shared library.
1448 gold_assert(!this->needs_base_version_
);
1450 // When creating a regular executable, automatically define
1452 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1453 false, false, false);
1454 this->defs_
.push_back(vd
);
1455 ins
.first
->second
= vd
;
1459 // Add a reference to version NAME in file FILENAME.
1462 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1463 Stringpool::Key name_key
)
1465 Stringpool::Key filename_key
;
1466 filename
= dynpool
->add(filename
, true, &filename_key
);
1468 Key
k(name_key
, filename_key
);
1469 Version_base
* const vbnull
= NULL
;
1470 std::pair
<Version_table::iterator
, bool> ins
=
1471 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1475 // We already have an entry for this filename/version.
1479 // See whether we already have this filename. We don't expect many
1480 // version references, so we just do a linear search. This could be
1481 // replaced by a hash table.
1483 for (Needs::iterator p
= this->needs_
.begin();
1484 p
!= this->needs_
.end();
1487 if ((*p
)->filename() == filename
)
1496 // Create base version definition lazily for shared library.
1497 if (this->needs_base_version_
)
1498 this->define_base_version(dynpool
);
1500 // We have a new filename.
1501 vn
= new Verneed(filename
);
1502 this->needs_
.push_back(vn
);
1505 ins
.first
->second
= vn
->add_name(name
);
1508 // Set the version indexes. Create a new dynamic version symbol for
1509 // each new version definition.
1512 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1513 std::vector
<Symbol
*>* syms
)
1515 gold_assert(!this->is_finalized_
);
1517 unsigned int vi
= 1;
1519 for (Defs::iterator p
= this->defs_
.begin();
1520 p
!= this->defs_
.end();
1523 (*p
)->set_index(vi
);
1526 // Create a version symbol if necessary.
1527 if (!(*p
)->is_symbol_created())
1529 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1531 Symbol_table::PREDEFINED
,
1535 elfcpp::STV_DEFAULT
, 0,
1537 vsym
->set_needs_dynsym_entry();
1538 vsym
->set_dynsym_index(dynsym_index
);
1540 syms
->push_back(vsym
);
1541 // The name is already in the dynamic pool.
1545 // Index 1 is used for global symbols.
1548 gold_assert(this->defs_
.empty());
1552 for (Needs::iterator p
= this->needs_
.begin();
1553 p
!= this->needs_
.end();
1555 vi
= (*p
)->finalize(vi
);
1557 this->is_finalized_
= true;
1559 return dynsym_index
;
1562 // Return the version index to use for a symbol. This does two hash
1563 // table lookups: one in DYNPOOL and one in this->version_table_.
1564 // Another approach alternative would be store a pointer in SYM, which
1565 // would increase the size of the symbol table. Or perhaps we could
1566 // use a hash table from dynamic symbol pointer values to Version_base
1570 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1571 const Symbol
* sym
) const
1573 Stringpool::Key version_key
;
1574 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1575 gold_assert(version
!= NULL
);
1578 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1580 if (!parameters
->options().shared())
1581 return elfcpp::VER_NDX_GLOBAL
;
1582 k
= Key(version_key
, 0);
1586 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1588 Stringpool::Key filename_key
;
1589 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1590 gold_assert(filename
!= NULL
);
1592 k
= Key(version_key
, filename_key
);
1595 Version_table::const_iterator p
= this->version_table_
.find(k
);
1596 gold_assert(p
!= this->version_table_
.end());
1598 return p
->second
->index();
1601 // Return an allocated buffer holding the contents of the symbol
1604 template<int size
, bool big_endian
>
1606 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1607 const Stringpool
* dynpool
,
1608 unsigned int local_symcount
,
1609 const std::vector
<Symbol
*>& syms
,
1611 unsigned int* psize
) const
1613 gold_assert(this->is_finalized_
);
1615 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1616 unsigned char* pbuf
= new unsigned char[sz
];
1618 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1619 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1620 elfcpp::VER_NDX_LOCAL
);
1622 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1626 unsigned int version_index
;
1627 const char* version
= (*p
)->version();
1628 if (version
== NULL
)
1629 version_index
= elfcpp::VER_NDX_GLOBAL
;
1631 version_index
= this->version_index(symtab
, dynpool
, *p
);
1632 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1634 if ((*p
)->version() != NULL
&& !(*p
)->is_default())
1635 version_index
|= elfcpp::VERSYM_HIDDEN
;
1636 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1644 // Return an allocated buffer holding the contents of the version
1645 // definition section.
1647 template<int size
, bool big_endian
>
1649 Versions::def_section_contents(const Stringpool
* dynpool
,
1650 unsigned char** pp
, unsigned int* psize
,
1651 unsigned int* pentries
) const
1653 gold_assert(this->is_finalized_
);
1654 gold_assert(!this->defs_
.empty());
1656 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1657 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1659 unsigned int sz
= 0;
1660 for (Defs::const_iterator p
= this->defs_
.begin();
1661 p
!= this->defs_
.end();
1664 sz
+= verdef_size
+ verdaux_size
;
1665 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1668 unsigned char* pbuf
= new unsigned char[sz
];
1670 unsigned char* pb
= pbuf
;
1671 Defs::const_iterator p
;
1673 for (p
= this->defs_
.begin(), i
= 0;
1674 p
!= this->defs_
.end();
1676 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1677 i
+ 1 >= this->defs_
.size(),
1680 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1684 *pentries
= this->defs_
.size();
1687 // Return an allocated buffer holding the contents of the version
1688 // reference section.
1690 template<int size
, bool big_endian
>
1692 Versions::need_section_contents(const Stringpool
* dynpool
,
1693 unsigned char** pp
, unsigned int *psize
,
1694 unsigned int *pentries
) const
1696 gold_assert(this->is_finalized_
);
1697 gold_assert(!this->needs_
.empty());
1699 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1700 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1702 unsigned int sz
= 0;
1703 for (Needs::const_iterator p
= this->needs_
.begin();
1704 p
!= this->needs_
.end();
1708 sz
+= (*p
)->count_versions() * vernaux_size
;
1711 unsigned char* pbuf
= new unsigned char[sz
];
1713 unsigned char* pb
= pbuf
;
1714 Needs::const_iterator p
;
1716 for (p
= this->needs_
.begin(), i
= 0;
1717 p
!= this->needs_
.end();
1719 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1720 i
+ 1 >= this->needs_
.size(),
1723 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1727 *pentries
= this->needs_
.size();
1730 // Instantiate the templates we need. We could use the configure
1731 // script to restrict this to only the ones for implemented targets.
1733 #ifdef HAVE_TARGET_32_LITTLE
1735 class Sized_dynobj
<32, false>;
1738 #ifdef HAVE_TARGET_32_BIG
1740 class Sized_dynobj
<32, true>;
1743 #ifdef HAVE_TARGET_64_LITTLE
1745 class Sized_dynobj
<64, false>;
1748 #ifdef HAVE_TARGET_64_BIG
1750 class Sized_dynobj
<64, true>;
1753 #ifdef HAVE_TARGET_32_LITTLE
1756 Versions::symbol_section_contents
<32, false>(
1757 const Symbol_table
*,
1760 const std::vector
<Symbol
*>&,
1762 unsigned int*) const;
1765 #ifdef HAVE_TARGET_32_BIG
1768 Versions::symbol_section_contents
<32, true>(
1769 const Symbol_table
*,
1772 const std::vector
<Symbol
*>&,
1774 unsigned int*) const;
1777 #ifdef HAVE_TARGET_64_LITTLE
1780 Versions::symbol_section_contents
<64, false>(
1781 const Symbol_table
*,
1784 const std::vector
<Symbol
*>&,
1786 unsigned int*) const;
1789 #ifdef HAVE_TARGET_64_BIG
1792 Versions::symbol_section_contents
<64, true>(
1793 const Symbol_table
*,
1796 const std::vector
<Symbol
*>&,
1798 unsigned int*) const;
1801 #ifdef HAVE_TARGET_32_LITTLE
1804 Versions::def_section_contents
<32, false>(
1808 unsigned int*) const;
1811 #ifdef HAVE_TARGET_32_BIG
1814 Versions::def_section_contents
<32, true>(
1818 unsigned int*) const;
1821 #ifdef HAVE_TARGET_64_LITTLE
1824 Versions::def_section_contents
<64, false>(
1828 unsigned int*) const;
1831 #ifdef HAVE_TARGET_64_BIG
1834 Versions::def_section_contents
<64, true>(
1838 unsigned int*) const;
1841 #ifdef HAVE_TARGET_32_LITTLE
1844 Versions::need_section_contents
<32, false>(
1848 unsigned int*) const;
1851 #ifdef HAVE_TARGET_32_BIG
1854 Versions::need_section_contents
<32, true>(
1858 unsigned int*) const;
1861 #ifdef HAVE_TARGET_64_LITTLE
1864 Versions::need_section_contents
<64, false>(
1868 unsigned int*) const;
1871 #ifdef HAVE_TARGET_64_BIG
1874 Versions::need_section_contents
<64, true>(
1878 unsigned int*) const;
1881 } // End namespace gold.