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;
327 if (this->dynsym_shndx_
!= -1U)
329 // Get the dynamic symbols.
330 typename
This::Shdr
dynsymshdr(pshdrs
331 + this->dynsym_shndx_
* This::shdr_size
);
332 gold_assert(dynsymshdr
.get_sh_type() == elfcpp::SHT_DYNSYM
);
334 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
335 dynsymshdr
.get_sh_size(), true,
338 convert_to_section_size_type(dynsymshdr
.get_sh_size());
340 // Get the symbol names.
341 strtab_shndx
= this->adjust_shndx(dynsymshdr
.get_sh_link());
342 if (strtab_shndx
>= this->shnum())
344 this->error(_("invalid dynamic symbol table name index: %u"),
348 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
349 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
351 this->error(_("dynamic symbol table name section "
352 "has wrong type: %u"),
353 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
357 sd
->symbol_names
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
358 strtabshdr
.get_sh_size(),
360 sd
->symbol_names_size
=
361 convert_to_section_size_type(strtabshdr
.get_sh_size());
363 // Get the version information.
366 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
368 &sd
->versym
, &sd
->versym_size
, &dummy
);
370 // We require that the version definition and need section link
371 // to the same string table as the dynamic symbol table. This
372 // is not a technical requirement, but it always happens in
373 // practice. We could change this if necessary.
375 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
376 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
379 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
380 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
384 // Read the SHT_DYNAMIC section to find whether this shared object
385 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
386 // doesn't really have anything to do with reading the symbols, but
387 // this is a convenient place to do it.
388 if (dynamic_shndx
!= -1U)
389 this->read_dynamic(pshdrs
, dynamic_shndx
, strtab_shndx
,
390 (sd
->symbol_names
== NULL
392 : sd
->symbol_names
->data()),
393 sd
->symbol_names_size
);
396 // Return the Xindex structure to use for object with lots of
399 template<int size
, bool big_endian
>
401 Sized_dynobj
<size
, big_endian
>::do_initialize_xindex()
403 gold_assert(this->dynsym_shndx_
!= -1U);
404 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
405 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->dynsym_shndx_
);
409 // Lay out the input sections for a dynamic object. We don't want to
410 // include sections from a dynamic object, so all that we actually do
411 // here is check for .gnu.warning sections.
413 template<int size
, bool big_endian
>
415 Sized_dynobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
417 Read_symbols_data
* sd
)
419 const unsigned int shnum
= this->shnum();
423 // Get the section headers.
424 const unsigned char* pshdrs
= sd
->section_headers
->data();
426 // Get the section names.
427 const unsigned char* pnamesu
= sd
->section_names
->data();
428 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
430 // Skip the first, dummy, section.
431 pshdrs
+= This::shdr_size
;
432 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
434 typename
This::Shdr
shdr(pshdrs
);
436 if (shdr
.get_sh_name() >= sd
->section_names_size
)
438 this->error(_("bad section name offset for section %u: %lu"),
439 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
443 const char* name
= pnames
+ shdr
.get_sh_name();
445 this->handle_gnu_warning_section(name
, i
, symtab
);
448 delete sd
->section_headers
;
449 sd
->section_headers
= NULL
;
450 delete sd
->section_names
;
451 sd
->section_names
= NULL
;
454 // Add an entry to the vector mapping version numbers to version
457 template<int size
, bool big_endian
>
459 Sized_dynobj
<size
, big_endian
>::set_version_map(
460 Version_map
* version_map
,
462 const char* name
) const
464 if (ndx
>= version_map
->size())
465 version_map
->resize(ndx
+ 1);
466 if ((*version_map
)[ndx
] != NULL
)
467 this->error(_("duplicate definition for version %u"), ndx
);
468 (*version_map
)[ndx
] = name
;
471 // Add mappings for the version definitions to VERSION_MAP.
473 template<int size
, bool big_endian
>
475 Sized_dynobj
<size
, big_endian
>::make_verdef_map(
476 Read_symbols_data
* sd
,
477 Version_map
* version_map
) const
479 if (sd
->verdef
== NULL
)
482 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
483 section_size_type names_size
= sd
->symbol_names_size
;
485 const unsigned char* pverdef
= sd
->verdef
->data();
486 section_size_type verdef_size
= sd
->verdef_size
;
487 const unsigned int count
= sd
->verdef_info
;
489 const unsigned char* p
= pverdef
;
490 for (unsigned int i
= 0; i
< count
; ++i
)
492 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
494 if (verdef
.get_vd_version() != elfcpp::VER_DEF_CURRENT
)
496 this->error(_("unexpected verdef version %u"),
497 verdef
.get_vd_version());
501 const section_size_type vd_ndx
= verdef
.get_vd_ndx();
503 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
506 // The first Verdaux holds the name of this version. Subsequent
507 // ones are versions that this one depends upon, which we don't
509 const section_size_type vd_cnt
= verdef
.get_vd_cnt();
512 this->error(_("verdef vd_cnt field too small: %u"),
513 static_cast<unsigned int>(vd_cnt
));
517 const section_size_type vd_aux
= verdef
.get_vd_aux();
518 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
520 this->error(_("verdef vd_aux field out of range: %u"),
521 static_cast<unsigned int>(vd_aux
));
525 const unsigned char* pvda
= p
+ vd_aux
;
526 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
528 const section_size_type vda_name
= verdaux
.get_vda_name();
529 if (vda_name
>= names_size
)
531 this->error(_("verdaux vda_name field out of range: %u"),
532 static_cast<unsigned int>(vda_name
));
536 this->set_version_map(version_map
, vd_ndx
, names
+ vda_name
);
538 const section_size_type vd_next
= verdef
.get_vd_next();
539 if ((p
- pverdef
) + vd_next
>= verdef_size
)
541 this->error(_("verdef vd_next field out of range: %u"),
542 static_cast<unsigned int>(vd_next
));
550 // Add mappings for the required versions to VERSION_MAP.
552 template<int size
, bool big_endian
>
554 Sized_dynobj
<size
, big_endian
>::make_verneed_map(
555 Read_symbols_data
* sd
,
556 Version_map
* version_map
) const
558 if (sd
->verneed
== NULL
)
561 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
562 section_size_type names_size
= sd
->symbol_names_size
;
564 const unsigned char* pverneed
= sd
->verneed
->data();
565 const section_size_type verneed_size
= sd
->verneed_size
;
566 const unsigned int count
= sd
->verneed_info
;
568 const unsigned char* p
= pverneed
;
569 for (unsigned int i
= 0; i
< count
; ++i
)
571 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
573 if (verneed
.get_vn_version() != elfcpp::VER_NEED_CURRENT
)
575 this->error(_("unexpected verneed version %u"),
576 verneed
.get_vn_version());
580 const section_size_type vn_aux
= verneed
.get_vn_aux();
582 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
584 this->error(_("verneed vn_aux field out of range: %u"),
585 static_cast<unsigned int>(vn_aux
));
589 const unsigned int vn_cnt
= verneed
.get_vn_cnt();
590 const unsigned char* pvna
= p
+ vn_aux
;
591 for (unsigned int j
= 0; j
< vn_cnt
; ++j
)
593 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
595 const unsigned int vna_name
= vernaux
.get_vna_name();
596 if (vna_name
>= names_size
)
598 this->error(_("vernaux vna_name field out of range: %u"),
599 static_cast<unsigned int>(vna_name
));
603 this->set_version_map(version_map
, vernaux
.get_vna_other(),
606 const section_size_type vna_next
= vernaux
.get_vna_next();
607 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
609 this->error(_("verneed vna_next field out of range: %u"),
610 static_cast<unsigned int>(vna_next
));
617 const section_size_type vn_next
= verneed
.get_vn_next();
618 if ((p
- pverneed
) + vn_next
>= verneed_size
)
620 this->error(_("verneed vn_next field out of range: %u"),
621 static_cast<unsigned int>(vn_next
));
629 // Create a vector mapping version numbers to version strings.
631 template<int size
, bool big_endian
>
633 Sized_dynobj
<size
, big_endian
>::make_version_map(
634 Read_symbols_data
* sd
,
635 Version_map
* version_map
) const
637 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
640 // A guess at the maximum version number we will see. If this is
641 // wrong we will be less efficient but still correct.
642 version_map
->reserve(sd
->verdef_info
+ sd
->verneed_info
* 10);
644 this->make_verdef_map(sd
, version_map
);
645 this->make_verneed_map(sd
, version_map
);
648 // Add the dynamic symbols to the symbol table.
650 template<int size
, bool big_endian
>
652 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
653 Read_symbols_data
* sd
,
656 if (sd
->symbols
== NULL
)
658 gold_assert(sd
->symbol_names
== NULL
);
659 gold_assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
660 && sd
->verneed
== NULL
);
664 const int sym_size
= This::sym_size
;
665 const size_t symcount
= sd
->symbols_size
/ sym_size
;
666 gold_assert(sd
->external_symbols_offset
== 0);
667 if (symcount
* sym_size
!= sd
->symbols_size
)
669 this->error(_("size of dynamic symbols is not multiple of symbol size"));
673 Version_map version_map
;
674 this->make_version_map(sd
, &version_map
);
676 // If printing symbol counts, we want to track symbols.
678 if (parameters
->options().user_set_print_symbol_counts())
680 this->symbols_
= new Symbols();
681 this->symbols_
->resize(symcount
);
684 const char* sym_names
=
685 reinterpret_cast<const char*>(sd
->symbol_names
->data());
686 symtab
->add_from_dynobj(this, sd
->symbols
->data(), symcount
,
687 sym_names
, sd
->symbol_names_size
,
690 : sd
->versym
->data()),
694 &this->defined_count_
);
698 delete sd
->symbol_names
;
699 sd
->symbol_names
= NULL
;
700 if (sd
->versym
!= NULL
)
705 if (sd
->verdef
!= NULL
)
710 if (sd
->verneed
!= NULL
)
716 // This is normally the last time we will read any data from this
718 this->clear_view_cache_marks();
721 // Get symbol counts.
723 template<int size
, bool big_endian
>
725 Sized_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
730 *defined
= this->defined_count_
;
732 for (typename
Symbols::const_iterator p
= this->symbols_
->begin();
733 p
!= this->symbols_
->end();
736 && (*p
)->source() == Symbol::FROM_OBJECT
737 && (*p
)->object() == this
738 && (*p
)->is_defined()
739 && (*p
)->dynsym_index() != -1U)
744 // Given a vector of hash codes, compute the number of hash buckets to
748 Dynobj::compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
749 bool for_gnu_hash_table
)
751 // FIXME: Implement optional hash table optimization.
753 // Array used to determine the number of hash table buckets to use
754 // based on the number of symbols there are. If there are fewer
755 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
756 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
757 // use more than 262147 buckets. This is straight from the old GNU
759 static const unsigned int buckets
[] =
761 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
762 16411, 32771, 65537, 131101, 262147
764 const int buckets_count
= sizeof buckets
/ sizeof buckets
[0];
766 unsigned int symcount
= hashcodes
.size();
767 unsigned int ret
= 1;
768 const double full_fraction
769 = 1.0 - parameters
->options().hash_bucket_empty_fraction();
770 for (int i
= 0; i
< buckets_count
; ++i
)
772 if (symcount
< buckets
[i
] * full_fraction
)
777 if (for_gnu_hash_table
&& ret
< 2)
783 // The standard ELF hash function. This hash function must not
784 // change, as the dynamic linker uses it also.
787 Dynobj::elf_hash(const char* name
)
789 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
792 while ((c
= *nameu
++) != '\0')
795 uint32_t g
= h
& 0xf0000000;
799 // The ELF ABI says h &= ~g, but using xor is equivalent in
800 // this case (since g was set from h) and may save one
808 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
809 // DYNSYMS is a vector with all the global dynamic symbols.
810 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
814 Dynobj::create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
815 unsigned int local_dynsym_count
,
816 unsigned char** pphash
,
817 unsigned int* phashlen
)
819 unsigned int dynsym_count
= dynsyms
.size();
821 // Get the hash values for all the symbols.
822 std::vector
<uint32_t> dynsym_hashvals(dynsym_count
);
823 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
824 dynsym_hashvals
[i
] = Dynobj::elf_hash(dynsyms
[i
]->name());
826 const unsigned int bucketcount
=
827 Dynobj::compute_bucket_count(dynsym_hashvals
, false);
829 std::vector
<uint32_t> bucket(bucketcount
);
830 std::vector
<uint32_t> chain(local_dynsym_count
+ dynsym_count
);
832 for (unsigned int i
= 0; i
< dynsym_count
; ++i
)
834 unsigned int dynsym_index
= dynsyms
[i
]->dynsym_index();
835 unsigned int bucketpos
= dynsym_hashvals
[i
] % bucketcount
;
836 chain
[dynsym_index
] = bucket
[bucketpos
];
837 bucket
[bucketpos
] = dynsym_index
;
840 unsigned int hashlen
= ((2
845 unsigned char* phash
= new unsigned char[hashlen
];
847 if (parameters
->target().is_big_endian())
849 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
850 Dynobj::sized_create_elf_hash_table
<true>(bucket
, chain
, phash
,
858 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
859 Dynobj::sized_create_elf_hash_table
<false>(bucket
, chain
, phash
,
870 // Fill in an ELF hash table.
872 template<bool big_endian
>
874 Dynobj::sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
875 const std::vector
<uint32_t>& chain
,
876 unsigned char* phash
,
877 unsigned int hashlen
)
879 unsigned char* p
= phash
;
881 const unsigned int bucketcount
= bucket
.size();
882 const unsigned int chaincount
= chain
.size();
884 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucketcount
);
886 elfcpp::Swap
<32, big_endian
>::writeval(p
, chaincount
);
889 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
891 elfcpp::Swap
<32, big_endian
>::writeval(p
, bucket
[i
]);
895 for (unsigned int i
= 0; i
< chaincount
; ++i
)
897 elfcpp::Swap
<32, big_endian
>::writeval(p
, chain
[i
]);
901 gold_assert(static_cast<unsigned int>(p
- phash
) == hashlen
);
904 // The hash function used for the GNU hash table. This hash function
905 // must not change, as the dynamic linker uses it also.
908 Dynobj::gnu_hash(const char* name
)
910 const unsigned char* nameu
= reinterpret_cast<const unsigned char*>(name
);
913 while ((c
= *nameu
++) != '\0')
914 h
= (h
<< 5) + h
+ c
;
918 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
919 // tables are an extension to ELF which are recognized by the GNU
920 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
921 // TARGET is the target. DYNSYMS is a vector with all the global
922 // symbols which will be going into the dynamic symbol table.
923 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
927 Dynobj::create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
928 unsigned int local_dynsym_count
,
929 unsigned char** pphash
,
930 unsigned int* phashlen
)
932 const unsigned int count
= dynsyms
.size();
934 // Sort the dynamic symbols into two vectors. Symbols which we do
935 // not want to put into the hash table we store into
936 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
937 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
938 // and records the hash codes.
940 std::vector
<Symbol
*> unhashed_dynsyms
;
941 unhashed_dynsyms
.reserve(count
);
943 std::vector
<Symbol
*> hashed_dynsyms
;
944 hashed_dynsyms
.reserve(count
);
946 std::vector
<uint32_t> dynsym_hashvals
;
947 dynsym_hashvals
.reserve(count
);
949 for (unsigned int i
= 0; i
< count
; ++i
)
951 Symbol
* sym
= dynsyms
[i
];
953 // FIXME: Should put on unhashed_dynsyms if the symbol is
955 if (sym
->is_undefined())
956 unhashed_dynsyms
.push_back(sym
);
959 hashed_dynsyms
.push_back(sym
);
960 dynsym_hashvals
.push_back(Dynobj::gnu_hash(sym
->name()));
964 // Put the unhashed symbols at the start of the global portion of
965 // the dynamic symbol table.
966 const unsigned int unhashed_count
= unhashed_dynsyms
.size();
967 unsigned int unhashed_dynsym_index
= local_dynsym_count
;
968 for (unsigned int i
= 0; i
< unhashed_count
; ++i
)
970 unhashed_dynsyms
[i
]->set_dynsym_index(unhashed_dynsym_index
);
971 ++unhashed_dynsym_index
;
974 // For the actual data generation we call out to a templatized
976 int size
= parameters
->target().get_size();
977 bool big_endian
= parameters
->target().is_big_endian();
982 #ifdef HAVE_TARGET_32_BIG
983 Dynobj::sized_create_gnu_hash_table
<32, true>(hashed_dynsyms
,
985 unhashed_dynsym_index
,
994 #ifdef HAVE_TARGET_32_LITTLE
995 Dynobj::sized_create_gnu_hash_table
<32, false>(hashed_dynsyms
,
997 unhashed_dynsym_index
,
1005 else if (size
== 64)
1009 #ifdef HAVE_TARGET_64_BIG
1010 Dynobj::sized_create_gnu_hash_table
<64, true>(hashed_dynsyms
,
1012 unhashed_dynsym_index
,
1021 #ifdef HAVE_TARGET_64_LITTLE
1022 Dynobj::sized_create_gnu_hash_table
<64, false>(hashed_dynsyms
,
1024 unhashed_dynsym_index
,
1036 // Create the actual data for a GNU hash table. This is just a copy
1037 // of the code from the old GNU linker.
1039 template<int size
, bool big_endian
>
1041 Dynobj::sized_create_gnu_hash_table(
1042 const std::vector
<Symbol
*>& hashed_dynsyms
,
1043 const std::vector
<uint32_t>& dynsym_hashvals
,
1044 unsigned int unhashed_dynsym_count
,
1045 unsigned char** pphash
,
1046 unsigned int* phashlen
)
1048 if (hashed_dynsyms
.empty())
1050 // Special case for the empty hash table.
1051 unsigned int hashlen
= 5 * 4 + size
/ 8;
1052 unsigned char* phash
= new unsigned char[hashlen
];
1053 // One empty bucket.
1054 elfcpp::Swap
<32, big_endian
>::writeval(phash
, 1);
1055 // Symbol index above unhashed symbols.
1056 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, unhashed_dynsym_count
);
1057 // One word for bitmask.
1058 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, 1);
1059 // Only bloom filter.
1060 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, 0);
1062 elfcpp::Swap
<size
, big_endian
>::writeval(phash
+ 16, 0);
1063 // No hashes in only bucket.
1064 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 16 + size
/ 8, 0);
1066 *phashlen
= hashlen
;
1072 const unsigned int bucketcount
=
1073 Dynobj::compute_bucket_count(dynsym_hashvals
, true);
1075 const unsigned int nsyms
= hashed_dynsyms
.size();
1077 uint32_t maskbitslog2
= 1;
1078 uint32_t x
= nsyms
>> 1;
1084 if (maskbitslog2
< 3)
1086 else if (((1U << (maskbitslog2
- 2)) & nsyms
) != 0)
1096 if (maskbitslog2
== 5)
1100 uint32_t mask
= (1U << shift1
) - 1U;
1101 uint32_t shift2
= maskbitslog2
;
1102 uint32_t maskbits
= 1U << maskbitslog2
;
1103 uint32_t maskwords
= 1U << (maskbitslog2
- shift1
);
1105 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Word
;
1106 std::vector
<Word
> bitmask(maskwords
);
1107 std::vector
<uint32_t> counts(bucketcount
);
1108 std::vector
<uint32_t> indx(bucketcount
);
1109 uint32_t symindx
= unhashed_dynsym_count
;
1111 // Count the number of times each hash bucket is used.
1112 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1113 ++counts
[dynsym_hashvals
[i
] % bucketcount
];
1115 unsigned int cnt
= symindx
;
1116 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1122 unsigned int hashlen
= (4 + bucketcount
+ nsyms
) * 4;
1123 hashlen
+= maskbits
/ 8;
1124 unsigned char* phash
= new unsigned char[hashlen
];
1126 elfcpp::Swap
<32, big_endian
>::writeval(phash
, bucketcount
);
1127 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 4, symindx
);
1128 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 8, maskwords
);
1129 elfcpp::Swap
<32, big_endian
>::writeval(phash
+ 12, shift2
);
1131 unsigned char* p
= phash
+ 16 + maskbits
/ 8;
1132 for (unsigned int i
= 0; i
< bucketcount
; ++i
)
1135 elfcpp::Swap
<32, big_endian
>::writeval(p
, 0);
1137 elfcpp::Swap
<32, big_endian
>::writeval(p
, indx
[i
]);
1141 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1143 Symbol
* sym
= hashed_dynsyms
[i
];
1144 uint32_t hashval
= dynsym_hashvals
[i
];
1146 unsigned int bucket
= hashval
% bucketcount
;
1147 unsigned int val
= ((hashval
>> shift1
)
1148 & ((maskbits
>> shift1
) - 1));
1149 bitmask
[val
] |= (static_cast<Word
>(1U)) << (hashval
& mask
);
1150 bitmask
[val
] |= (static_cast<Word
>(1U)) << ((hashval
>> shift2
) & mask
);
1151 val
= hashval
& ~ 1U;
1152 if (counts
[bucket
] == 1)
1154 // Last element terminates the chain.
1157 elfcpp::Swap
<32, big_endian
>::writeval(p
+ (indx
[bucket
] - symindx
) * 4,
1161 sym
->set_dynsym_index(indx
[bucket
]);
1166 for (unsigned int i
= 0; i
< maskwords
; ++i
)
1168 elfcpp::Swap
<size
, big_endian
>::writeval(p
, bitmask
[i
]);
1172 *phashlen
= hashlen
;
1178 // Write this definition to a buffer for the output section.
1180 template<int size
, bool big_endian
>
1182 Verdef::write(const Stringpool
* dynpool
, bool is_last
, unsigned char* pb
) const
1184 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1185 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1187 elfcpp::Verdef_write
<size
, big_endian
> vd(pb
);
1188 vd
.set_vd_version(elfcpp::VER_DEF_CURRENT
);
1189 vd
.set_vd_flags((this->is_base_
? elfcpp::VER_FLG_BASE
: 0)
1190 | (this->is_weak_
? elfcpp::VER_FLG_WEAK
: 0));
1191 vd
.set_vd_ndx(this->index());
1192 vd
.set_vd_cnt(1 + this->deps_
.size());
1193 vd
.set_vd_hash(Dynobj::elf_hash(this->name()));
1194 vd
.set_vd_aux(verdef_size
);
1195 vd
.set_vd_next(is_last
1197 : verdef_size
+ (1 + this->deps_
.size()) * verdaux_size
);
1200 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1201 vda
.set_vda_name(dynpool
->get_offset(this->name()));
1202 vda
.set_vda_next(this->deps_
.empty() ? 0 : verdaux_size
);
1205 Deps::const_iterator p
;
1207 for (p
= this->deps_
.begin(), i
= 0;
1208 p
!= this->deps_
.end();
1211 elfcpp::Verdaux_write
<size
, big_endian
> vda(pb
);
1212 vda
.set_vda_name(dynpool
->get_offset(*p
));
1213 vda
.set_vda_next(i
+ 1 >= this->deps_
.size() ? 0 : verdaux_size
);
1224 for (Need_versions::iterator p
= this->need_versions_
.begin();
1225 p
!= this->need_versions_
.end();
1230 // Add a new version to this file reference.
1233 Verneed::add_name(const char* name
)
1235 Verneed_version
* vv
= new Verneed_version(name
);
1236 this->need_versions_
.push_back(vv
);
1240 // Set the version indexes starting at INDEX.
1243 Verneed::finalize(unsigned int index
)
1245 for (Need_versions::iterator p
= this->need_versions_
.begin();
1246 p
!= this->need_versions_
.end();
1249 (*p
)->set_index(index
);
1255 // Write this list of referenced versions to a buffer for the output
1258 template<int size
, bool big_endian
>
1260 Verneed::write(const Stringpool
* dynpool
, bool is_last
,
1261 unsigned char* pb
) const
1263 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1264 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1266 elfcpp::Verneed_write
<size
, big_endian
> vn(pb
);
1267 vn
.set_vn_version(elfcpp::VER_NEED_CURRENT
);
1268 vn
.set_vn_cnt(this->need_versions_
.size());
1269 vn
.set_vn_file(dynpool
->get_offset(this->filename()));
1270 vn
.set_vn_aux(verneed_size
);
1271 vn
.set_vn_next(is_last
1273 : verneed_size
+ this->need_versions_
.size() * vernaux_size
);
1276 Need_versions::const_iterator p
;
1278 for (p
= this->need_versions_
.begin(), i
= 0;
1279 p
!= this->need_versions_
.end();
1282 elfcpp::Vernaux_write
<size
, big_endian
> vna(pb
);
1283 vna
.set_vna_hash(Dynobj::elf_hash((*p
)->version()));
1284 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1285 vna
.set_vna_flags(0);
1286 vna
.set_vna_other((*p
)->index());
1287 vna
.set_vna_name(dynpool
->get_offset((*p
)->version()));
1288 vna
.set_vna_next(i
+ 1 >= this->need_versions_
.size()
1297 // Versions methods.
1299 Versions::Versions(const Version_script_info
& version_script
,
1300 Stringpool
* dynpool
)
1301 : defs_(), needs_(), version_table_(),
1302 is_finalized_(false), version_script_(version_script
)
1304 // We always need a base version, so define that first. Nothing
1305 // explicitly declares itself as part of base, so it doesn't need to
1306 // be in version_table_.
1307 if (parameters
->options().shared())
1309 const char* name
= parameters
->options().soname();
1311 name
= parameters
->options().output_file_name();
1312 name
= dynpool
->add(name
, false, NULL
);
1313 Verdef
* vdbase
= new Verdef(name
, std::vector
<std::string
>(),
1315 this->defs_
.push_back(vdbase
);
1318 if (!this->version_script_
.empty())
1320 // Parse the version script, and insert each declared version into
1321 // defs_ and version_table_.
1322 std::vector
<std::string
> versions
= this->version_script_
.get_versions();
1323 for (size_t k
= 0; k
< versions
.size(); ++k
)
1325 Stringpool::Key version_key
;
1326 const char* version
= dynpool
->add(versions
[k
].c_str(),
1327 true, &version_key
);
1328 Verdef
* const vd
= new Verdef(
1330 this->version_script_
.get_dependencies(version
),
1331 false, false, false);
1332 this->defs_
.push_back(vd
);
1333 Key
key(version_key
, 0);
1334 this->version_table_
.insert(std::make_pair(key
, vd
));
1339 Versions::~Versions()
1341 for (Defs::iterator p
= this->defs_
.begin();
1342 p
!= this->defs_
.end();
1346 for (Needs::iterator p
= this->needs_
.begin();
1347 p
!= this->needs_
.end();
1352 // Return the dynamic object which a symbol refers to.
1355 Versions::get_dynobj_for_sym(const Symbol_table
* symtab
,
1356 const Symbol
* sym
) const
1358 if (sym
->is_copied_from_dynobj())
1359 return symtab
->get_copy_source(sym
);
1362 Object
* object
= sym
->object();
1363 gold_assert(object
->is_dynamic());
1364 return static_cast<Dynobj
*>(object
);
1368 // Record version information for a symbol going into the dynamic
1372 Versions::record_version(const Symbol_table
* symtab
,
1373 Stringpool
* dynpool
, const Symbol
* sym
)
1375 gold_assert(!this->is_finalized_
);
1376 gold_assert(sym
->version() != NULL
);
1378 Stringpool::Key version_key
;
1379 const char* version
= dynpool
->add(sym
->version(), false, &version_key
);
1381 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1383 if (parameters
->options().shared())
1384 this->add_def(sym
, version
, version_key
);
1388 // This is a version reference.
1389 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1390 this->add_need(dynpool
, dynobj
->soname(), version
, version_key
);
1394 // We've found a symbol SYM defined in version VERSION.
1397 Versions::add_def(const Symbol
* sym
, const char* version
,
1398 Stringpool::Key version_key
)
1400 Key
k(version_key
, 0);
1401 Version_base
* const vbnull
= NULL
;
1402 std::pair
<Version_table::iterator
, bool> ins
=
1403 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1407 // We already have an entry for this version.
1408 Version_base
* vb
= ins
.first
->second
;
1410 // We have now seen a symbol in this version, so it is not
1412 gold_assert(vb
!= NULL
);
1417 // If we are creating a shared object, it is an error to
1418 // find a definition of a symbol with a version which is not
1419 // in the version script.
1420 if (parameters
->options().shared())
1421 gold_error(_("symbol %s has undefined version %s"),
1422 sym
->demangled_name().c_str(), version
);
1424 // When creating a regular executable, automatically define
1426 Verdef
* vd
= new Verdef(version
, std::vector
<std::string
>(),
1427 false, false, false);
1428 this->defs_
.push_back(vd
);
1429 ins
.first
->second
= vd
;
1433 // Add a reference to version NAME in file FILENAME.
1436 Versions::add_need(Stringpool
* dynpool
, const char* filename
, const char* name
,
1437 Stringpool::Key name_key
)
1439 Stringpool::Key filename_key
;
1440 filename
= dynpool
->add(filename
, true, &filename_key
);
1442 Key
k(name_key
, filename_key
);
1443 Version_base
* const vbnull
= NULL
;
1444 std::pair
<Version_table::iterator
, bool> ins
=
1445 this->version_table_
.insert(std::make_pair(k
, vbnull
));
1449 // We already have an entry for this filename/version.
1453 // See whether we already have this filename. We don't expect many
1454 // version references, so we just do a linear search. This could be
1455 // replaced by a hash table.
1457 for (Needs::iterator p
= this->needs_
.begin();
1458 p
!= this->needs_
.end();
1461 if ((*p
)->filename() == filename
)
1470 // We have a new filename.
1471 vn
= new Verneed(filename
);
1472 this->needs_
.push_back(vn
);
1475 ins
.first
->second
= vn
->add_name(name
);
1478 // Set the version indexes. Create a new dynamic version symbol for
1479 // each new version definition.
1482 Versions::finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
1483 std::vector
<Symbol
*>* syms
)
1485 gold_assert(!this->is_finalized_
);
1487 unsigned int vi
= 1;
1489 for (Defs::iterator p
= this->defs_
.begin();
1490 p
!= this->defs_
.end();
1493 (*p
)->set_index(vi
);
1496 // Create a version symbol if necessary.
1497 if (!(*p
)->is_symbol_created())
1499 Symbol
* vsym
= symtab
->define_as_constant((*p
)->name(),
1503 elfcpp::STV_DEFAULT
, 0,
1505 vsym
->set_needs_dynsym_entry();
1506 vsym
->set_dynsym_index(dynsym_index
);
1508 syms
->push_back(vsym
);
1509 // The name is already in the dynamic pool.
1513 // Index 1 is used for global symbols.
1516 gold_assert(this->defs_
.empty());
1520 for (Needs::iterator p
= this->needs_
.begin();
1521 p
!= this->needs_
.end();
1523 vi
= (*p
)->finalize(vi
);
1525 this->is_finalized_
= true;
1527 return dynsym_index
;
1530 // Return the version index to use for a symbol. This does two hash
1531 // table lookups: one in DYNPOOL and one in this->version_table_.
1532 // Another approach alternative would be store a pointer in SYM, which
1533 // would increase the size of the symbol table. Or perhaps we could
1534 // use a hash table from dynamic symbol pointer values to Version_base
1538 Versions::version_index(const Symbol_table
* symtab
, const Stringpool
* dynpool
,
1539 const Symbol
* sym
) const
1541 Stringpool::Key version_key
;
1542 const char* version
= dynpool
->find(sym
->version(), &version_key
);
1543 gold_assert(version
!= NULL
);
1546 if (!sym
->is_from_dynobj() && !sym
->is_copied_from_dynobj())
1548 if (!parameters
->options().shared())
1549 return elfcpp::VER_NDX_GLOBAL
;
1550 k
= Key(version_key
, 0);
1554 Dynobj
* dynobj
= this->get_dynobj_for_sym(symtab
, sym
);
1556 Stringpool::Key filename_key
;
1557 const char* filename
= dynpool
->find(dynobj
->soname(), &filename_key
);
1558 gold_assert(filename
!= NULL
);
1560 k
= Key(version_key
, filename_key
);
1563 Version_table::const_iterator p
= this->version_table_
.find(k
);
1564 gold_assert(p
!= this->version_table_
.end());
1566 return p
->second
->index();
1569 // Return an allocated buffer holding the contents of the symbol
1572 template<int size
, bool big_endian
>
1574 Versions::symbol_section_contents(const Symbol_table
* symtab
,
1575 const Stringpool
* dynpool
,
1576 unsigned int local_symcount
,
1577 const std::vector
<Symbol
*>& syms
,
1579 unsigned int* psize
) const
1581 gold_assert(this->is_finalized_
);
1583 unsigned int sz
= (local_symcount
+ syms
.size()) * 2;
1584 unsigned char* pbuf
= new unsigned char[sz
];
1586 for (unsigned int i
= 0; i
< local_symcount
; ++i
)
1587 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ i
* 2,
1588 elfcpp::VER_NDX_LOCAL
);
1590 for (std::vector
<Symbol
*>::const_iterator p
= syms
.begin();
1594 unsigned int version_index
;
1595 const char* version
= (*p
)->version();
1596 if (version
== NULL
)
1597 version_index
= elfcpp::VER_NDX_GLOBAL
;
1599 version_index
= this->version_index(symtab
, dynpool
, *p
);
1600 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1602 if ((*p
)->version() != NULL
&& !(*p
)->is_default())
1603 version_index
|= elfcpp::VERSYM_HIDDEN
;
1604 elfcpp::Swap
<16, big_endian
>::writeval(pbuf
+ (*p
)->dynsym_index() * 2,
1612 // Return an allocated buffer holding the contents of the version
1613 // definition section.
1615 template<int size
, bool big_endian
>
1617 Versions::def_section_contents(const Stringpool
* dynpool
,
1618 unsigned char** pp
, unsigned int* psize
,
1619 unsigned int* pentries
) const
1621 gold_assert(this->is_finalized_
);
1622 gold_assert(!this->defs_
.empty());
1624 const int verdef_size
= elfcpp::Elf_sizes
<size
>::verdef_size
;
1625 const int verdaux_size
= elfcpp::Elf_sizes
<size
>::verdaux_size
;
1627 unsigned int sz
= 0;
1628 for (Defs::const_iterator p
= this->defs_
.begin();
1629 p
!= this->defs_
.end();
1632 sz
+= verdef_size
+ verdaux_size
;
1633 sz
+= (*p
)->count_dependencies() * verdaux_size
;
1636 unsigned char* pbuf
= new unsigned char[sz
];
1638 unsigned char* pb
= pbuf
;
1639 Defs::const_iterator p
;
1641 for (p
= this->defs_
.begin(), i
= 0;
1642 p
!= this->defs_
.end();
1644 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1645 i
+ 1 >= this->defs_
.size(),
1648 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1652 *pentries
= this->defs_
.size();
1655 // Return an allocated buffer holding the contents of the version
1656 // reference section.
1658 template<int size
, bool big_endian
>
1660 Versions::need_section_contents(const Stringpool
* dynpool
,
1661 unsigned char** pp
, unsigned int *psize
,
1662 unsigned int *pentries
) const
1664 gold_assert(this->is_finalized_
);
1665 gold_assert(!this->needs_
.empty());
1667 const int verneed_size
= elfcpp::Elf_sizes
<size
>::verneed_size
;
1668 const int vernaux_size
= elfcpp::Elf_sizes
<size
>::vernaux_size
;
1670 unsigned int sz
= 0;
1671 for (Needs::const_iterator p
= this->needs_
.begin();
1672 p
!= this->needs_
.end();
1676 sz
+= (*p
)->count_versions() * vernaux_size
;
1679 unsigned char* pbuf
= new unsigned char[sz
];
1681 unsigned char* pb
= pbuf
;
1682 Needs::const_iterator p
;
1684 for (p
= this->needs_
.begin(), i
= 0;
1685 p
!= this->needs_
.end();
1687 pb
= (*p
)->write
<size
, big_endian
>(dynpool
,
1688 i
+ 1 >= this->needs_
.size(),
1691 gold_assert(static_cast<unsigned int>(pb
- pbuf
) == sz
);
1695 *pentries
= this->needs_
.size();
1698 // Instantiate the templates we need. We could use the configure
1699 // script to restrict this to only the ones for implemented targets.
1701 #ifdef HAVE_TARGET_32_LITTLE
1703 class Sized_dynobj
<32, false>;
1706 #ifdef HAVE_TARGET_32_BIG
1708 class Sized_dynobj
<32, true>;
1711 #ifdef HAVE_TARGET_64_LITTLE
1713 class Sized_dynobj
<64, false>;
1716 #ifdef HAVE_TARGET_64_BIG
1718 class Sized_dynobj
<64, true>;
1721 #ifdef HAVE_TARGET_32_LITTLE
1724 Versions::symbol_section_contents
<32, false>(
1725 const Symbol_table
*,
1728 const std::vector
<Symbol
*>&,
1730 unsigned int*) const;
1733 #ifdef HAVE_TARGET_32_BIG
1736 Versions::symbol_section_contents
<32, true>(
1737 const Symbol_table
*,
1740 const std::vector
<Symbol
*>&,
1742 unsigned int*) const;
1745 #ifdef HAVE_TARGET_64_LITTLE
1748 Versions::symbol_section_contents
<64, false>(
1749 const Symbol_table
*,
1752 const std::vector
<Symbol
*>&,
1754 unsigned int*) const;
1757 #ifdef HAVE_TARGET_64_BIG
1760 Versions::symbol_section_contents
<64, true>(
1761 const Symbol_table
*,
1764 const std::vector
<Symbol
*>&,
1766 unsigned int*) const;
1769 #ifdef HAVE_TARGET_32_LITTLE
1772 Versions::def_section_contents
<32, false>(
1776 unsigned int*) const;
1779 #ifdef HAVE_TARGET_32_BIG
1782 Versions::def_section_contents
<32, true>(
1786 unsigned int*) const;
1789 #ifdef HAVE_TARGET_64_LITTLE
1792 Versions::def_section_contents
<64, false>(
1796 unsigned int*) const;
1799 #ifdef HAVE_TARGET_64_BIG
1802 Versions::def_section_contents
<64, true>(
1806 unsigned int*) const;
1809 #ifdef HAVE_TARGET_32_LITTLE
1812 Versions::need_section_contents
<32, false>(
1816 unsigned int*) const;
1819 #ifdef HAVE_TARGET_32_BIG
1822 Versions::need_section_contents
<32, true>(
1826 unsigned int*) const;
1829 #ifdef HAVE_TARGET_64_LITTLE
1832 Versions::need_section_contents
<64, false>(
1836 unsigned int*) const;
1839 #ifdef HAVE_TARGET_64_BIG
1842 Versions::need_section_contents
<64, true>(
1846 unsigned int*) const;
1849 } // End namespace gold.