From Craig Silverstein: Fix bug when reading large script files.
[binutils.git] / gold / dynobj.cc
blobb6255f8ec7ecc2ea3fd801e52d95845a3788e6fc
1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007 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.
23 #include "gold.h"
25 #include <vector>
26 #include <cstring>
28 #include "elfcpp.h"
29 #include "parameters.h"
30 #include "symtab.h"
31 #include "dynobj.h"
33 namespace gold
36 // Class Dynobj.
38 // Return the string to use in a DT_NEEDED entry.
40 const char*
41 Dynobj::soname() const
43 if (!this->soname_.empty())
44 return this->soname_.c_str();
45 return this->name().c_str();
48 // Class Sized_dynobj.
50 template<int size, bool big_endian>
51 Sized_dynobj<size, big_endian>::Sized_dynobj(
52 const std::string& name,
53 Input_file* input_file,
54 off_t offset,
55 const elfcpp::Ehdr<size, big_endian>& ehdr)
56 : Dynobj(name, input_file, offset),
57 elf_file_(this, ehdr)
61 // Set up the object.
63 template<int size, bool big_endian>
64 void
65 Sized_dynobj<size, big_endian>::setup(
66 const elfcpp::Ehdr<size, big_endian>& ehdr)
68 this->set_target(ehdr.get_e_machine(), size, big_endian,
69 ehdr.get_e_ident()[elfcpp::EI_OSABI],
70 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
72 const unsigned int shnum = this->elf_file_.shnum();
73 this->set_shnum(shnum);
76 // Find the SHT_DYNSYM section and the various version sections, and
77 // the dynamic section, given the section headers.
79 template<int size, bool big_endian>
80 void
81 Sized_dynobj<size, big_endian>::find_dynsym_sections(
82 const unsigned char* pshdrs,
83 unsigned int* pdynsym_shndx,
84 unsigned int* pversym_shndx,
85 unsigned int* pverdef_shndx,
86 unsigned int* pverneed_shndx,
87 unsigned int* pdynamic_shndx)
89 *pdynsym_shndx = -1U;
90 *pversym_shndx = -1U;
91 *pverdef_shndx = -1U;
92 *pverneed_shndx = -1U;
93 *pdynamic_shndx = -1U;
95 const unsigned int shnum = this->shnum();
96 const unsigned char* p = pshdrs;
97 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
99 typename This::Shdr shdr(p);
101 unsigned int* pi;
102 switch (shdr.get_sh_type())
104 case elfcpp::SHT_DYNSYM:
105 pi = pdynsym_shndx;
106 break;
107 case elfcpp::SHT_GNU_versym:
108 pi = pversym_shndx;
109 break;
110 case elfcpp::SHT_GNU_verdef:
111 pi = pverdef_shndx;
112 break;
113 case elfcpp::SHT_GNU_verneed:
114 pi = pverneed_shndx;
115 break;
116 case elfcpp::SHT_DYNAMIC:
117 pi = pdynamic_shndx;
118 break;
119 default:
120 pi = NULL;
121 break;
124 if (pi == NULL)
125 continue;
127 if (*pi != -1U)
129 fprintf(stderr,
130 _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
131 program_name, this->name().c_str(), shdr.get_sh_type(),
132 *pi, i);
133 gold_exit(false);
136 *pi = i;
140 // Read the contents of section SHNDX. PSHDRS points to the section
141 // headers. TYPE is the expected section type. LINK is the expected
142 // section link. Store the data in *VIEW and *VIEW_SIZE. The
143 // section's sh_info field is stored in *VIEW_INFO.
145 template<int size, bool big_endian>
146 void
147 Sized_dynobj<size, big_endian>::read_dynsym_section(
148 const unsigned char* pshdrs,
149 unsigned int shndx,
150 elfcpp::SHT type,
151 unsigned int link,
152 File_view** view,
153 off_t* view_size,
154 unsigned int* view_info)
156 if (shndx == -1U)
158 *view = NULL;
159 *view_size = 0;
160 *view_info = 0;
161 return;
164 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
166 gold_assert(shdr.get_sh_type() == type);
168 if (shdr.get_sh_link() != link)
170 fprintf(stderr,
171 _("%s: %s: unexpected link in section %u header: %u != %u\n"),
172 program_name, this->name().c_str(), shndx,
173 shdr.get_sh_link(), link);
174 gold_exit(false);
177 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size());
178 *view_size = shdr.get_sh_size();
179 *view_info = shdr.get_sh_info();
182 // Set the soname field if this shared object has a DT_SONAME tag.
183 // PSHDRS points to the section headers. DYNAMIC_SHNDX is the section
184 // index of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and
185 // STRTAB_SIZE are the section index and contents of a string table
186 // which may be the one associated with the SHT_DYNAMIC section.
188 template<int size, bool big_endian>
189 void
190 Sized_dynobj<size, big_endian>::set_soname(const unsigned char* pshdrs,
191 unsigned int dynamic_shndx,
192 unsigned int strtab_shndx,
193 const unsigned char* strtabu,
194 off_t strtab_size)
196 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
197 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
199 const off_t dynamic_size = dynamicshdr.get_sh_size();
200 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
201 dynamic_size);
203 const unsigned int link = dynamicshdr.get_sh_link();
204 if (link != strtab_shndx)
206 if (link >= this->shnum())
208 fprintf(stderr,
209 _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
210 program_name, this->name().c_str(),
211 dynamic_shndx, link);
212 gold_exit(false);
215 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
216 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
218 fprintf(stderr,
219 _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
220 program_name, this->name().c_str(),
221 dynamic_shndx, link);
222 gold_exit(false);
225 strtab_size = strtabshdr.get_sh_size();
226 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size);
229 for (const unsigned char* p = pdynamic;
230 p < pdynamic + dynamic_size;
231 p += This::dyn_size)
233 typename This::Dyn dyn(p);
235 if (dyn.get_d_tag() == elfcpp::DT_SONAME)
237 off_t val = dyn.get_d_val();
238 if (val >= strtab_size)
240 fprintf(stderr,
241 _("%s: %s: DT_SONAME value out of range: "
242 "%lld >= %lld\n"),
243 program_name, this->name().c_str(),
244 static_cast<long long>(val),
245 static_cast<long long>(strtab_size));
246 gold_exit(false);
249 const char* strtab = reinterpret_cast<const char*>(strtabu);
250 this->set_soname_string(strtab + val);
251 return;
254 if (dyn.get_d_tag() == elfcpp::DT_NULL)
255 return;
258 fprintf(stderr, _("%s: %s: missing DT_NULL in dynamic segment\n"),
259 program_name, this->name().c_str());
260 gold_exit(false);
263 // Read the symbols and sections from a dynamic object. We read the
264 // dynamic symbols, not the normal symbols.
266 template<int size, bool big_endian>
267 void
268 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
270 this->read_section_data(&this->elf_file_, sd);
272 const unsigned char* const pshdrs = sd->section_headers->data();
274 unsigned int dynsym_shndx;
275 unsigned int versym_shndx;
276 unsigned int verdef_shndx;
277 unsigned int verneed_shndx;
278 unsigned int dynamic_shndx;
279 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
280 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
282 unsigned int strtab_shndx = -1U;
284 if (dynsym_shndx == -1U)
286 sd->symbols = NULL;
287 sd->symbols_size = 0;
288 sd->symbol_names = NULL;
289 sd->symbol_names_size = 0;
291 else
293 // Get the dynamic symbols.
294 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
295 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
297 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
298 dynsymshdr.get_sh_size());
299 sd->symbols_size = dynsymshdr.get_sh_size();
301 // Get the symbol names.
302 strtab_shndx = dynsymshdr.get_sh_link();
303 if (strtab_shndx >= this->shnum())
305 fprintf(stderr,
306 _("%s: %s: invalid dynamic symbol table name index: %u\n"),
307 program_name, this->name().c_str(), strtab_shndx);
308 gold_exit(false);
310 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
311 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
313 fprintf(stderr,
314 _("%s: %s: dynamic symbol table name section "
315 "has wrong type: %u\n"),
316 program_name, this->name().c_str(),
317 static_cast<unsigned int>(strtabshdr.get_sh_type()));
318 gold_exit(false);
321 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
322 strtabshdr.get_sh_size());
323 sd->symbol_names_size = strtabshdr.get_sh_size();
325 // Get the version information.
327 unsigned int dummy;
328 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
329 dynsym_shndx, &sd->versym, &sd->versym_size,
330 &dummy);
332 // We require that the version definition and need section link
333 // to the same string table as the dynamic symbol table. This
334 // is not a technical requirement, but it always happens in
335 // practice. We could change this if necessary.
337 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
338 strtab_shndx, &sd->verdef, &sd->verdef_size,
339 &sd->verdef_info);
341 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
342 strtab_shndx, &sd->verneed, &sd->verneed_size,
343 &sd->verneed_info);
346 // Read the SHT_DYNAMIC section to find whether this shared object
347 // has a DT_SONAME tag. This doesn't really have anything to do
348 // with reading the symbols, but this is a convenient place to do
349 // it.
350 if (dynamic_shndx != -1U)
351 this->set_soname(pshdrs, dynamic_shndx, strtab_shndx,
352 (sd->symbol_names == NULL
353 ? NULL
354 : sd->symbol_names->data()),
355 sd->symbol_names_size);
358 // Lay out the input sections for a dynamic object. We don't want to
359 // include sections from a dynamic object, so all that we actually do
360 // here is check for .gnu.warning sections.
362 template<int size, bool big_endian>
363 void
364 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
365 Layout*,
366 Read_symbols_data* sd)
368 const unsigned int shnum = this->shnum();
369 if (shnum == 0)
370 return;
372 // Get the section headers.
373 const unsigned char* pshdrs = sd->section_headers->data();
375 // Get the section names.
376 const unsigned char* pnamesu = sd->section_names->data();
377 const char* pnames = reinterpret_cast<const char*>(pnamesu);
379 // Skip the first, dummy, section.
380 pshdrs += This::shdr_size;
381 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
383 typename This::Shdr shdr(pshdrs);
385 if (shdr.get_sh_name() >= sd->section_names_size)
387 fprintf(stderr,
388 _("%s: %s: bad section name offset for section %u: %lu\n"),
389 program_name, this->name().c_str(), i,
390 static_cast<unsigned long>(shdr.get_sh_name()));
391 gold_exit(false);
394 const char* name = pnames + shdr.get_sh_name();
396 this->handle_gnu_warning_section(name, i, symtab);
399 delete sd->section_headers;
400 sd->section_headers = NULL;
401 delete sd->section_names;
402 sd->section_names = NULL;
405 // Add an entry to the vector mapping version numbers to version
406 // strings.
408 template<int size, bool big_endian>
409 void
410 Sized_dynobj<size, big_endian>::set_version_map(
411 Version_map* version_map,
412 unsigned int ndx,
413 const char* name) const
415 if (ndx >= version_map->size())
416 version_map->resize(ndx + 1);
417 if ((*version_map)[ndx] != NULL)
419 fprintf(stderr, _("%s: %s: duplicate definition for version %u\n"),
420 program_name, this->name().c_str(), ndx);
421 gold_exit(false);
423 (*version_map)[ndx] = name;
426 // Add mappings for the version definitions to VERSION_MAP.
428 template<int size, bool big_endian>
429 void
430 Sized_dynobj<size, big_endian>::make_verdef_map(
431 Read_symbols_data* sd,
432 Version_map* version_map) const
434 if (sd->verdef == NULL)
435 return;
437 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
438 off_t names_size = sd->symbol_names_size;
440 const unsigned char* pverdef = sd->verdef->data();
441 off_t verdef_size = sd->verdef_size;
442 const unsigned int count = sd->verdef_info;
444 const unsigned char* p = pverdef;
445 for (unsigned int i = 0; i < count; ++i)
447 elfcpp::Verdef<size, big_endian> verdef(p);
449 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
451 fprintf(stderr, _("%s: %s: unexpected verdef version %u\n"),
452 program_name, this->name().c_str(), verdef.get_vd_version());
453 gold_exit(false);
456 const unsigned int vd_ndx = verdef.get_vd_ndx();
458 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
459 // sure why.
461 // The first Verdaux holds the name of this version. Subsequent
462 // ones are versions that this one depends upon, which we don't
463 // care about here.
464 const unsigned int vd_cnt = verdef.get_vd_cnt();
465 if (vd_cnt < 1)
467 fprintf(stderr, _("%s: %s: verdef vd_cnt field too small: %u\n"),
468 program_name, this->name().c_str(), vd_cnt);
469 gold_exit(false);
472 const unsigned int vd_aux = verdef.get_vd_aux();
473 if ((p - pverdef) + vd_aux >= verdef_size)
475 fprintf(stderr,
476 _("%s: %s: verdef vd_aux field out of range: %u\n"),
477 program_name, this->name().c_str(), vd_aux);
478 gold_exit(false);
481 const unsigned char* pvda = p + vd_aux;
482 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
484 const unsigned int vda_name = verdaux.get_vda_name();
485 if (vda_name >= names_size)
487 fprintf(stderr,
488 _("%s: %s: verdaux vda_name field out of range: %u\n"),
489 program_name, this->name().c_str(), vda_name);
490 gold_exit(false);
493 this->set_version_map(version_map, vd_ndx, names + vda_name);
495 const unsigned int vd_next = verdef.get_vd_next();
496 if ((p - pverdef) + vd_next >= verdef_size)
498 fprintf(stderr,
499 _("%s: %s: verdef vd_next field out of range: %u\n"),
500 program_name, this->name().c_str(), vd_next);
501 gold_exit(false);
504 p += vd_next;
508 // Add mappings for the required versions to VERSION_MAP.
510 template<int size, bool big_endian>
511 void
512 Sized_dynobj<size, big_endian>::make_verneed_map(
513 Read_symbols_data* sd,
514 Version_map* version_map) const
516 if (sd->verneed == NULL)
517 return;
519 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
520 off_t names_size = sd->symbol_names_size;
522 const unsigned char* pverneed = sd->verneed->data();
523 const off_t verneed_size = sd->verneed_size;
524 const unsigned int count = sd->verneed_info;
526 const unsigned char* p = pverneed;
527 for (unsigned int i = 0; i < count; ++i)
529 elfcpp::Verneed<size, big_endian> verneed(p);
531 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
533 fprintf(stderr, _("%s: %s: unexpected verneed version %u\n"),
534 program_name, this->name().c_str(),
535 verneed.get_vn_version());
536 gold_exit(false);
539 const unsigned int vn_aux = verneed.get_vn_aux();
541 if ((p - pverneed) + vn_aux >= verneed_size)
543 fprintf(stderr,
544 _("%s: %s: verneed vn_aux field out of range: %u\n"),
545 program_name, this->name().c_str(), vn_aux);
546 gold_exit(false);
549 const unsigned int vn_cnt = verneed.get_vn_cnt();
550 const unsigned char* pvna = p + vn_aux;
551 for (unsigned int j = 0; j < vn_cnt; ++j)
553 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
555 const unsigned int vna_name = vernaux.get_vna_name();
556 if (vna_name >= names_size)
558 fprintf(stderr,
559 _("%s: %s: vernaux vna_name field "
560 "out of range: %u\n"),
561 program_name, this->name().c_str(), vna_name);
562 gold_exit(false);
565 this->set_version_map(version_map, vernaux.get_vna_other(),
566 names + vna_name);
568 const unsigned int vna_next = vernaux.get_vna_next();
569 if ((pvna - pverneed) + vna_next >= verneed_size)
571 fprintf(stderr,
572 _("%s: %s: verneed vna_next field "
573 "out of range: %u\n"),
574 program_name, this->name().c_str(), vna_next);
575 gold_exit(false);
578 pvna += vna_next;
581 const unsigned int vn_next = verneed.get_vn_next();
582 if ((p - pverneed) + vn_next >= verneed_size)
584 fprintf(stderr,
585 _("%s: %s: verneed vn_next field out of range: %u\n"),
586 program_name, this->name().c_str(), vn_next);
587 gold_exit(false);
590 p += vn_next;
594 // Create a vector mapping version numbers to version strings.
596 template<int size, bool big_endian>
597 void
598 Sized_dynobj<size, big_endian>::make_version_map(
599 Read_symbols_data* sd,
600 Version_map* version_map) const
602 if (sd->verdef == NULL && sd->verneed == NULL)
603 return;
605 // A guess at the maximum version number we will see. If this is
606 // wrong we will be less efficient but still correct.
607 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
609 this->make_verdef_map(sd, version_map);
610 this->make_verneed_map(sd, version_map);
613 // Add the dynamic symbols to the symbol table.
615 template<int size, bool big_endian>
616 void
617 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
618 Read_symbols_data* sd)
620 if (sd->symbols == NULL)
622 gold_assert(sd->symbol_names == NULL);
623 gold_assert(sd->versym == NULL && sd->verdef == NULL
624 && sd->verneed == NULL);
625 return;
628 const int sym_size = This::sym_size;
629 const size_t symcount = sd->symbols_size / sym_size;
630 if (symcount * sym_size != sd->symbols_size)
632 fprintf(stderr,
633 _("%s: %s: size of dynamic symbols is not "
634 "multiple of symbol size\n"),
635 program_name, this->name().c_str());
636 gold_exit(false);
639 Version_map version_map;
640 this->make_version_map(sd, &version_map);
642 const char* sym_names =
643 reinterpret_cast<const char*>(sd->symbol_names->data());
644 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
645 sym_names, sd->symbol_names_size,
646 (sd->versym == NULL
647 ? NULL
648 : sd->versym->data()),
649 sd->versym_size,
650 &version_map);
652 delete sd->symbols;
653 sd->symbols = NULL;
654 delete sd->symbol_names;
655 sd->symbol_names = NULL;
656 if (sd->versym != NULL)
658 delete sd->versym;
659 sd->versym = NULL;
661 if (sd->verdef != NULL)
663 delete sd->verdef;
664 sd->verdef = NULL;
666 if (sd->verneed != NULL)
668 delete sd->verneed;
669 sd->verneed = NULL;
673 // Given a vector of hash codes, compute the number of hash buckets to
674 // use.
676 unsigned int
677 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
678 bool for_gnu_hash_table)
680 // FIXME: Implement optional hash table optimization.
682 // Array used to determine the number of hash table buckets to use
683 // based on the number of symbols there are. If there are fewer
684 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
685 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
686 // use more than 32771 buckets. This is straight from the old GNU
687 // linker.
688 static const unsigned int buckets[] =
690 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
691 16411, 32771
693 const int buckets_count = sizeof buckets / sizeof buckets[0];
695 unsigned int symcount = hashcodes.size();
696 unsigned int ret = 1;
697 for (int i = 0; i < buckets_count; ++i)
699 if (symcount < buckets[i])
700 break;
701 ret = buckets[i];
704 if (for_gnu_hash_table && ret < 2)
705 ret = 2;
707 return ret;
710 // The standard ELF hash function. This hash function must not
711 // change, as the dynamic linker uses it also.
713 uint32_t
714 Dynobj::elf_hash(const char* name)
716 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
717 uint32_t h = 0;
718 unsigned char c;
719 while ((c = *nameu++) != '\0')
721 h = (h << 4) + c;
722 uint32_t g = h & 0xf0000000;
723 if (g != 0)
725 h ^= g >> 24;
726 // The ELF ABI says h &= ~g, but using xor is equivalent in
727 // this case (since g was set from h) and may save one
728 // instruction.
729 h ^= g;
732 return h;
735 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
736 // DYNSYMS is a vector with all the global dynamic symbols.
737 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
738 // symbol table.
740 void
741 Dynobj::create_elf_hash_table(const Target* target,
742 const std::vector<Symbol*>& dynsyms,
743 unsigned int local_dynsym_count,
744 unsigned char** pphash,
745 unsigned int* phashlen)
747 unsigned int dynsym_count = dynsyms.size();
749 // Get the hash values for all the symbols.
750 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
751 for (unsigned int i = 0; i < dynsym_count; ++i)
752 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
754 const unsigned int bucketcount =
755 Dynobj::compute_bucket_count(dynsym_hashvals, false);
757 std::vector<uint32_t> bucket(bucketcount);
758 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
760 for (unsigned int i = 0; i < dynsym_count; ++i)
762 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
763 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
764 chain[dynsym_index] = bucket[bucketpos];
765 bucket[bucketpos] = dynsym_index;
768 unsigned int hashlen = ((2
769 + bucketcount
770 + local_dynsym_count
771 + dynsym_count)
772 * 4);
773 unsigned char* phash = new unsigned char[hashlen];
775 if (target->is_big_endian())
776 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash, hashlen);
777 else
778 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash, hashlen);
780 *pphash = phash;
781 *phashlen = hashlen;
784 // Fill in an ELF hash table.
786 template<bool big_endian>
787 void
788 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
789 const std::vector<uint32_t>& chain,
790 unsigned char* phash,
791 unsigned int hashlen)
793 unsigned char* p = phash;
795 const unsigned int bucketcount = bucket.size();
796 const unsigned int chaincount = chain.size();
798 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
799 p += 4;
800 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
801 p += 4;
803 for (unsigned int i = 0; i < bucketcount; ++i)
805 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
806 p += 4;
809 for (unsigned int i = 0; i < chaincount; ++i)
811 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
812 p += 4;
815 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
818 // The hash function used for the GNU hash table. This hash function
819 // must not change, as the dynamic linker uses it also.
821 uint32_t
822 Dynobj::gnu_hash(const char* name)
824 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
825 uint32_t h = 5381;
826 unsigned char c;
827 while ((c = *nameu++) != '\0')
828 h = (h << 5) + h + c;
829 return h;
832 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
833 // tables are an extension to ELF which are recognized by the GNU
834 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
835 // TARGET is the target. DYNSYMS is a vector with all the global
836 // symbols which will be going into the dynamic symbol table.
837 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
838 // symbol table.
840 void
841 Dynobj::create_gnu_hash_table(const Target* target,
842 const std::vector<Symbol*>& dynsyms,
843 unsigned int local_dynsym_count,
844 unsigned char** pphash,
845 unsigned int* phashlen)
847 const unsigned int count = dynsyms.size();
849 // Sort the dynamic symbols into two vectors. Symbols which we do
850 // not want to put into the hash table we store into
851 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
852 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
853 // and records the hash codes.
855 std::vector<Symbol*> unhashed_dynsyms;
856 unhashed_dynsyms.reserve(count);
858 std::vector<Symbol*> hashed_dynsyms;
859 hashed_dynsyms.reserve(count);
861 std::vector<uint32_t> dynsym_hashvals;
862 dynsym_hashvals.reserve(count);
864 for (unsigned int i = 0; i < count; ++i)
866 Symbol* sym = dynsyms[i];
868 // FIXME: Should put on unhashed_dynsyms if the symbol is
869 // hidden.
870 if (sym->is_undefined())
871 unhashed_dynsyms.push_back(sym);
872 else
874 hashed_dynsyms.push_back(sym);
875 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
879 // Put the unhashed symbols at the start of the global portion of
880 // the dynamic symbol table.
881 const unsigned int unhashed_count = unhashed_dynsyms.size();
882 unsigned int unhashed_dynsym_index = local_dynsym_count;
883 for (unsigned int i = 0; i < unhashed_count; ++i)
885 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
886 ++unhashed_dynsym_index;
889 // For the actual data generation we call out to a templatized
890 // function.
891 int size = target->get_size();
892 bool big_endian = target->is_big_endian();
893 if (size == 32)
895 if (big_endian)
896 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
897 dynsym_hashvals,
898 unhashed_dynsym_index,
899 pphash,
900 phashlen);
901 else
902 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
903 dynsym_hashvals,
904 unhashed_dynsym_index,
905 pphash,
906 phashlen);
908 else if (size == 64)
910 if (big_endian)
911 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
912 dynsym_hashvals,
913 unhashed_dynsym_index,
914 pphash,
915 phashlen);
916 else
917 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
918 dynsym_hashvals,
919 unhashed_dynsym_index,
920 pphash,
921 phashlen);
923 else
924 gold_unreachable();
927 // Create the actual data for a GNU hash table. This is just a copy
928 // of the code from the old GNU linker.
930 template<int size, bool big_endian>
931 void
932 Dynobj::sized_create_gnu_hash_table(
933 const std::vector<Symbol*>& hashed_dynsyms,
934 const std::vector<uint32_t>& dynsym_hashvals,
935 unsigned int unhashed_dynsym_count,
936 unsigned char** pphash,
937 unsigned int* phashlen)
939 if (hashed_dynsyms.empty())
941 // Special case for the empty hash table.
942 unsigned int hashlen = 5 * 4 + size / 8;
943 unsigned char* phash = new unsigned char[hashlen];
944 // One empty bucket.
945 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
946 // Symbol index above unhashed symbols.
947 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
948 // One word for bitmask.
949 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
950 // Only bloom filter.
951 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
952 // No valid hashes.
953 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
954 // No hashes in only bucket.
955 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
957 *phashlen = hashlen;
958 *pphash = phash;
960 return;
963 const unsigned int bucketcount =
964 Dynobj::compute_bucket_count(dynsym_hashvals, true);
966 const unsigned int nsyms = hashed_dynsyms.size();
968 uint32_t maskbitslog2 = 1;
969 uint32_t x = nsyms >> 1;
970 while (x != 0)
972 ++maskbitslog2;
973 x >>= 1;
975 if (maskbitslog2 < 3)
976 maskbitslog2 = 5;
977 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
978 maskbitslog2 += 3;
979 else
980 maskbitslog2 += 2;
982 uint32_t shift1;
983 if (size == 32)
984 shift1 = 5;
985 else
987 if (maskbitslog2 == 5)
988 maskbitslog2 = 6;
989 shift1 = 6;
991 uint32_t mask = (1U << shift1) - 1U;
992 uint32_t shift2 = maskbitslog2;
993 uint32_t maskbits = 1U << maskbitslog2;
994 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
996 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
997 std::vector<Word> bitmask(maskwords);
998 std::vector<uint32_t> counts(bucketcount);
999 std::vector<uint32_t> indx(bucketcount);
1000 uint32_t symindx = unhashed_dynsym_count;
1002 // Count the number of times each hash bucket is used.
1003 for (unsigned int i = 0; i < nsyms; ++i)
1004 ++counts[dynsym_hashvals[i] % bucketcount];
1006 unsigned int cnt = symindx;
1007 for (unsigned int i = 0; i < bucketcount; ++i)
1009 indx[i] = cnt;
1010 cnt += counts[i];
1013 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1014 hashlen += maskbits / 8;
1015 unsigned char* phash = new unsigned char[hashlen];
1017 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1018 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1019 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1020 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1022 unsigned char* p = phash + 16 + maskbits / 8;
1023 for (unsigned int i = 0; i < bucketcount; ++i)
1025 if (counts[i] == 0)
1026 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1027 else
1028 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1029 p += 4;
1032 for (unsigned int i = 0; i < nsyms; ++i)
1034 Symbol* sym = hashed_dynsyms[i];
1035 uint32_t hashval = dynsym_hashvals[i];
1037 unsigned int bucket = hashval % bucketcount;
1038 unsigned int val = ((hashval >> shift1)
1039 & ((maskbits >> shift1) - 1));
1040 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1041 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1042 val = hashval & ~ 1U;
1043 if (counts[bucket] == 1)
1045 // Last element terminates the chain.
1046 val |= 1;
1048 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1049 val);
1050 --counts[bucket];
1052 sym->set_dynsym_index(indx[bucket]);
1053 ++indx[bucket];
1056 p = phash + 16;
1057 for (unsigned int i = 0; i < maskwords; ++i)
1059 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1060 p += size / 8;
1063 *phashlen = hashlen;
1064 *pphash = phash;
1067 // Verdef methods.
1069 // Write this definition to a buffer for the output section.
1071 template<int size, bool big_endian>
1072 unsigned char*
1073 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb
1074 ACCEPT_SIZE_ENDIAN) const
1076 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1077 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1079 elfcpp::Verdef_write<size, big_endian> vd(pb);
1080 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1081 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1082 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1083 vd.set_vd_ndx(this->index());
1084 vd.set_vd_cnt(1 + this->deps_.size());
1085 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1086 vd.set_vd_aux(verdef_size);
1087 vd.set_vd_next(is_last
1089 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1090 pb += verdef_size;
1092 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1093 vda.set_vda_name(dynpool->get_offset(this->name()));
1094 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1095 pb += verdaux_size;
1097 Deps::const_iterator p;
1098 unsigned int i;
1099 for (p = this->deps_.begin(), i = 0;
1100 p != this->deps_.end();
1101 ++p, ++i)
1103 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1104 vda.set_vda_name(dynpool->get_offset(*p));
1105 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1106 pb += verdaux_size;
1109 return pb;
1112 // Verneed methods.
1114 Verneed::~Verneed()
1116 for (Need_versions::iterator p = this->need_versions_.begin();
1117 p != this->need_versions_.end();
1118 ++p)
1119 delete *p;
1122 // Add a new version to this file reference.
1124 Verneed_version*
1125 Verneed::add_name(const char* name)
1127 Verneed_version* vv = new Verneed_version(name);
1128 this->need_versions_.push_back(vv);
1129 return vv;
1132 // Set the version indexes starting at INDEX.
1134 unsigned int
1135 Verneed::finalize(unsigned int index)
1137 for (Need_versions::iterator p = this->need_versions_.begin();
1138 p != this->need_versions_.end();
1139 ++p)
1141 (*p)->set_index(index);
1142 ++index;
1144 return index;
1147 // Write this list of referenced versions to a buffer for the output
1148 // section.
1150 template<int size, bool big_endian>
1151 unsigned char*
1152 Verneed::write(const Stringpool* dynpool, bool is_last,
1153 unsigned char* pb ACCEPT_SIZE_ENDIAN) const
1155 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1156 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1158 elfcpp::Verneed_write<size, big_endian> vn(pb);
1159 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1160 vn.set_vn_cnt(this->need_versions_.size());
1161 vn.set_vn_file(dynpool->get_offset(this->filename()));
1162 vn.set_vn_aux(verneed_size);
1163 vn.set_vn_next(is_last
1165 : verneed_size + this->need_versions_.size() * vernaux_size);
1166 pb += verneed_size;
1168 Need_versions::const_iterator p;
1169 unsigned int i;
1170 for (p = this->need_versions_.begin(), i = 0;
1171 p != this->need_versions_.end();
1172 ++p, ++i)
1174 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1175 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1176 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1177 vna.set_vna_flags(0);
1178 vna.set_vna_other((*p)->index());
1179 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1180 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1182 : vernaux_size);
1183 pb += vernaux_size;
1186 return pb;
1189 // Versions methods.
1191 Versions::~Versions()
1193 for (Defs::iterator p = this->defs_.begin();
1194 p != this->defs_.end();
1195 ++p)
1196 delete *p;
1198 for (Needs::iterator p = this->needs_.begin();
1199 p != this->needs_.end();
1200 ++p)
1201 delete *p;
1204 // Record version information for a symbol going into the dynamic
1205 // symbol table.
1207 void
1208 Versions::record_version(const General_options* options,
1209 Stringpool* dynpool, const Symbol* sym)
1211 gold_assert(!this->is_finalized_);
1212 gold_assert(sym->version() != NULL);
1214 Stringpool::Key version_key;
1215 const char* version = dynpool->add(sym->version(), &version_key);
1217 if (!sym->is_from_dynobj())
1219 if (parameters->output_is_shared())
1220 this->add_def(options, sym, version, version_key);
1222 else
1224 // This is a version reference.
1226 Object* object = sym->object();
1227 gold_assert(object->is_dynamic());
1228 Dynobj* dynobj = static_cast<Dynobj*>(object);
1230 this->add_need(dynpool, dynobj->soname(), version, version_key);
1234 // We've found a symbol SYM defined in version VERSION.
1236 void
1237 Versions::add_def(const General_options* options, const Symbol* sym,
1238 const char* version, Stringpool::Key version_key)
1240 Key k(version_key, 0);
1241 Version_base* const vbnull = NULL;
1242 std::pair<Version_table::iterator, bool> ins =
1243 this->version_table_.insert(std::make_pair(k, vbnull));
1245 if (!ins.second)
1247 // We already have an entry for this version.
1248 Version_base* vb = ins.first->second;
1250 // We have now seen a symbol in this version, so it is not
1251 // weak.
1252 vb->clear_weak();
1254 // FIXME: When we support version scripts, we will need to
1255 // check whether this symbol should be forced local.
1257 else
1259 // If we are creating a shared object, it is an error to
1260 // find a definition of a symbol with a version which is not
1261 // in the version script.
1262 if (parameters->output_is_shared())
1264 fprintf(stderr, _("%s: symbol %s has undefined version %s\n"),
1265 program_name, sym->name(), version);
1266 gold_exit(false);
1269 // If this is the first version we are defining, first define
1270 // the base version. FIXME: Should use soname here when
1271 // creating a shared object.
1272 Verdef* vdbase = new Verdef(options->output_file_name(), true, false,
1273 true);
1274 this->defs_.push_back(vdbase);
1276 // When creating a regular executable, automatically define
1277 // a new version.
1278 Verdef* vd = new Verdef(version, false, false, false);
1279 this->defs_.push_back(vd);
1280 ins.first->second = vd;
1284 // Add a reference to version NAME in file FILENAME.
1286 void
1287 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1288 Stringpool::Key name_key)
1290 Stringpool::Key filename_key;
1291 filename = dynpool->add(filename, &filename_key);
1293 Key k(name_key, filename_key);
1294 Version_base* const vbnull = NULL;
1295 std::pair<Version_table::iterator, bool> ins =
1296 this->version_table_.insert(std::make_pair(k, vbnull));
1298 if (!ins.second)
1300 // We already have an entry for this filename/version.
1301 return;
1304 // See whether we already have this filename. We don't expect many
1305 // version references, so we just do a linear search. This could be
1306 // replaced by a hash table.
1307 Verneed* vn = NULL;
1308 for (Needs::iterator p = this->needs_.begin();
1309 p != this->needs_.end();
1310 ++p)
1312 if ((*p)->filename() == filename)
1314 vn = *p;
1315 break;
1319 if (vn == NULL)
1321 // We have a new filename.
1322 vn = new Verneed(filename);
1323 this->needs_.push_back(vn);
1326 ins.first->second = vn->add_name(name);
1329 // Set the version indexes. Create a new dynamic version symbol for
1330 // each new version definition.
1332 unsigned int
1333 Versions::finalize(const Target* target, Symbol_table* symtab,
1334 unsigned int dynsym_index, std::vector<Symbol*>* syms)
1336 gold_assert(!this->is_finalized_);
1338 unsigned int vi = 1;
1340 for (Defs::iterator p = this->defs_.begin();
1341 p != this->defs_.end();
1342 ++p)
1344 (*p)->set_index(vi);
1345 ++vi;
1347 // Create a version symbol if necessary.
1348 if (!(*p)->is_symbol_created())
1350 Symbol* vsym = symtab->define_as_constant(target, (*p)->name(),
1351 (*p)->name(), 0, 0,
1352 elfcpp::STT_OBJECT,
1353 elfcpp::STB_GLOBAL,
1354 elfcpp::STV_DEFAULT, 0,
1355 false);
1356 vsym->set_needs_dynsym_entry();
1357 vsym->set_dynsym_index(dynsym_index);
1358 ++dynsym_index;
1359 syms->push_back(vsym);
1360 // The name is already in the dynamic pool.
1364 // Index 1 is used for global symbols.
1365 if (vi == 1)
1367 gold_assert(this->defs_.empty());
1368 vi = 2;
1371 for (Needs::iterator p = this->needs_.begin();
1372 p != this->needs_.end();
1373 ++p)
1374 vi = (*p)->finalize(vi);
1376 this->is_finalized_ = true;
1378 return dynsym_index;
1381 // Return the version index to use for a symbol. This does two hash
1382 // table lookups: one in DYNPOOL and one in this->version_table_.
1383 // Another approach alternative would be store a pointer in SYM, which
1384 // would increase the size of the symbol table. Or perhaps we could
1385 // use a hash table from dynamic symbol pointer values to Version_base
1386 // pointers.
1388 unsigned int
1389 Versions::version_index(const Stringpool* dynpool, const Symbol* sym) const
1391 Stringpool::Key version_key;
1392 const char* version = dynpool->find(sym->version(), &version_key);
1393 gold_assert(version != NULL);
1395 Key k;
1396 if (!sym->is_from_dynobj())
1398 if (!parameters->output_is_shared())
1399 return elfcpp::VER_NDX_GLOBAL;
1400 k = Key(version_key, 0);
1402 else
1404 Object* object = sym->object();
1405 gold_assert(object->is_dynamic());
1406 Dynobj* dynobj = static_cast<Dynobj*>(object);
1408 Stringpool::Key filename_key;
1409 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1410 gold_assert(filename != NULL);
1412 k = Key(version_key, filename_key);
1415 Version_table::const_iterator p = this->version_table_.find(k);
1416 gold_assert(p != this->version_table_.end());
1418 return p->second->index();
1421 // Return an allocated buffer holding the contents of the symbol
1422 // version section.
1424 template<int size, bool big_endian>
1425 void
1426 Versions::symbol_section_contents(const Stringpool* dynpool,
1427 unsigned int local_symcount,
1428 const std::vector<Symbol*>& syms,
1429 unsigned char** pp,
1430 unsigned int* psize
1431 ACCEPT_SIZE_ENDIAN) const
1433 gold_assert(this->is_finalized_);
1435 unsigned int sz = (local_symcount + syms.size()) * 2;
1436 unsigned char* pbuf = new unsigned char[sz];
1438 for (unsigned int i = 0; i < local_symcount; ++i)
1439 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1440 elfcpp::VER_NDX_LOCAL);
1442 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1443 p != syms.end();
1444 ++p)
1446 unsigned int version_index;
1447 const char* version = (*p)->version();
1448 if (version == NULL)
1449 version_index = elfcpp::VER_NDX_GLOBAL;
1450 else
1451 version_index = this->version_index(dynpool, *p);
1452 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1453 version_index);
1456 *pp = pbuf;
1457 *psize = sz;
1460 // Return an allocated buffer holding the contents of the version
1461 // definition section.
1463 template<int size, bool big_endian>
1464 void
1465 Versions::def_section_contents(const Stringpool* dynpool,
1466 unsigned char** pp, unsigned int* psize,
1467 unsigned int* pentries
1468 ACCEPT_SIZE_ENDIAN) const
1470 gold_assert(this->is_finalized_);
1471 gold_assert(!this->defs_.empty());
1473 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1474 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1476 unsigned int sz = 0;
1477 for (Defs::const_iterator p = this->defs_.begin();
1478 p != this->defs_.end();
1479 ++p)
1481 sz += verdef_size + verdaux_size;
1482 sz += (*p)->count_dependencies() * verdaux_size;
1485 unsigned char* pbuf = new unsigned char[sz];
1487 unsigned char* pb = pbuf;
1488 Defs::const_iterator p;
1489 unsigned int i;
1490 for (p = this->defs_.begin(), i = 0;
1491 p != this->defs_.end();
1492 ++p, ++i)
1493 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1494 dynpool, i + 1 >= this->defs_.size(), pb
1495 SELECT_SIZE_ENDIAN(size, big_endian));
1497 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1499 *pp = pbuf;
1500 *psize = sz;
1501 *pentries = this->defs_.size();
1504 // Return an allocated buffer holding the contents of the version
1505 // reference section.
1507 template<int size, bool big_endian>
1508 void
1509 Versions::need_section_contents(const Stringpool* dynpool,
1510 unsigned char** pp, unsigned int *psize,
1511 unsigned int *pentries
1512 ACCEPT_SIZE_ENDIAN) const
1514 gold_assert(this->is_finalized_);
1515 gold_assert(!this->needs_.empty());
1517 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1518 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1520 unsigned int sz = 0;
1521 for (Needs::const_iterator p = this->needs_.begin();
1522 p != this->needs_.end();
1523 ++p)
1525 sz += verneed_size;
1526 sz += (*p)->count_versions() * vernaux_size;
1529 unsigned char* pbuf = new unsigned char[sz];
1531 unsigned char* pb = pbuf;
1532 Needs::const_iterator p;
1533 unsigned int i;
1534 for (p = this->needs_.begin(), i = 0;
1535 p != this->needs_.end();
1536 ++p, ++i)
1537 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1538 dynpool, i + 1 >= this->needs_.size(), pb
1539 SELECT_SIZE_ENDIAN(size, big_endian));
1541 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1543 *pp = pbuf;
1544 *psize = sz;
1545 *pentries = this->needs_.size();
1548 // Instantiate the templates we need. We could use the configure
1549 // script to restrict this to only the ones for implemented targets.
1551 #ifdef HAVE_TARGET_32_LITTLE
1552 template
1553 class Sized_dynobj<32, false>;
1554 #endif
1556 #ifdef HAVE_TARGET_32_BIG
1557 template
1558 class Sized_dynobj<32, true>;
1559 #endif
1561 #ifdef HAVE_TARGET_64_LITTLE
1562 template
1563 class Sized_dynobj<64, false>;
1564 #endif
1566 #ifdef HAVE_TARGET_64_BIG
1567 template
1568 class Sized_dynobj<64, true>;
1569 #endif
1571 #ifdef HAVE_TARGET_32_LITTLE
1572 template
1573 void
1574 Versions::symbol_section_contents<32, false>(
1575 const Stringpool*,
1576 unsigned int,
1577 const std::vector<Symbol*>&,
1578 unsigned char**,
1579 unsigned int*
1580 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1581 #endif
1583 #ifdef HAVE_TARGET_32_BIG
1584 template
1585 void
1586 Versions::symbol_section_contents<32, true>(
1587 const Stringpool*,
1588 unsigned int,
1589 const std::vector<Symbol*>&,
1590 unsigned char**,
1591 unsigned int*
1592 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1593 #endif
1595 #ifdef HAVE_TARGET_64_LITTLE
1596 template
1597 void
1598 Versions::symbol_section_contents<64, false>(
1599 const Stringpool*,
1600 unsigned int,
1601 const std::vector<Symbol*>&,
1602 unsigned char**,
1603 unsigned int*
1604 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1605 #endif
1607 #ifdef HAVE_TARGET_64_BIG
1608 template
1609 void
1610 Versions::symbol_section_contents<64, true>(
1611 const Stringpool*,
1612 unsigned int,
1613 const std::vector<Symbol*>&,
1614 unsigned char**,
1615 unsigned int*
1616 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1617 #endif
1619 #ifdef HAVE_TARGET_32_LITTLE
1620 template
1621 void
1622 Versions::def_section_contents<32, false>(
1623 const Stringpool*,
1624 unsigned char**,
1625 unsigned int*,
1626 unsigned int*
1627 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1628 #endif
1630 #ifdef HAVE_TARGET_32_BIG
1631 template
1632 void
1633 Versions::def_section_contents<32, true>(
1634 const Stringpool*,
1635 unsigned char**,
1636 unsigned int*,
1637 unsigned int*
1638 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1639 #endif
1641 #ifdef HAVE_TARGET_64_LITTLE
1642 template
1643 void
1644 Versions::def_section_contents<64, false>(
1645 const Stringpool*,
1646 unsigned char**,
1647 unsigned int*,
1648 unsigned int*
1649 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1650 #endif
1652 #ifdef HAVE_TARGET_64_BIG
1653 template
1654 void
1655 Versions::def_section_contents<64, true>(
1656 const Stringpool*,
1657 unsigned char**,
1658 unsigned int*,
1659 unsigned int*
1660 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1661 #endif
1663 #ifdef HAVE_TARGET_32_LITTLE
1664 template
1665 void
1666 Versions::need_section_contents<32, false>(
1667 const Stringpool*,
1668 unsigned char**,
1669 unsigned int*,
1670 unsigned int*
1671 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1672 #endif
1674 #ifdef HAVE_TARGET_32_BIG
1675 template
1676 void
1677 Versions::need_section_contents<32, true>(
1678 const Stringpool*,
1679 unsigned char**,
1680 unsigned int*,
1681 unsigned int*
1682 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1683 #endif
1685 #ifdef HAVE_TARGET_64_LITTLE
1686 template
1687 void
1688 Versions::need_section_contents<64, false>(
1689 const Stringpool*,
1690 unsigned char**,
1691 unsigned int*,
1692 unsigned int*
1693 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1694 #endif
1696 #ifdef HAVE_TARGET_64_BIG
1697 template
1698 void
1699 Versions::need_section_contents<64, true>(
1700 const Stringpool*,
1701 unsigned char**,
1702 unsigned int*,
1703 unsigned int*
1704 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1705 #endif
1707 } // End namespace gold.