gas/testsuite/
[binutils.git] / gold / dynobj.cc
blob3c3549d8e27ba1310f9aac961c747687db5e3f06
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 // Sets up the default soname_ to use, in the (rare) cases we never
39 // see a DT_SONAME entry.
41 Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
42 : Object(name, input_file, true, offset),
43 needed_(),
44 unknown_needed_(UNKNOWN_NEEDED_UNSET)
46 // This will be overridden by a DT_SONAME entry, hopefully. But if
47 // we never see a DT_SONAME entry, our rule is to use the dynamic
48 // object's filename. The only exception is when the dynamic object
49 // is part of an archive (so the filename is the archive's
50 // filename). In that case, we use just the dynobj's name-in-archive.
51 this->soname_ = this->input_file()->found_name();
52 if (this->offset() != 0)
54 std::string::size_type open_paren = this->name().find('(');
55 std::string::size_type close_paren = this->name().find(')');
56 if (open_paren != std::string::npos && close_paren != std::string::npos)
58 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
59 this->soname_ = this->name().substr(open_paren + 1,
60 close_paren - (open_paren + 1));
65 // Class Sized_dynobj.
67 template<int size, bool big_endian>
68 Sized_dynobj<size, big_endian>::Sized_dynobj(
69 const std::string& name,
70 Input_file* input_file,
71 off_t offset,
72 const elfcpp::Ehdr<size, big_endian>& ehdr)
73 : Dynobj(name, input_file, offset),
74 elf_file_(this, ehdr)
78 // Set up the object.
80 template<int size, bool big_endian>
81 void
82 Sized_dynobj<size, big_endian>::setup(
83 const elfcpp::Ehdr<size, big_endian>& ehdr)
85 this->set_target(ehdr.get_e_machine(), size, big_endian,
86 ehdr.get_e_ident()[elfcpp::EI_OSABI],
87 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
89 const unsigned int shnum = this->elf_file_.shnum();
90 this->set_shnum(shnum);
93 // Find the SHT_DYNSYM section and the various version sections, and
94 // the dynamic section, given the section headers.
96 template<int size, bool big_endian>
97 void
98 Sized_dynobj<size, big_endian>::find_dynsym_sections(
99 const unsigned char* pshdrs,
100 unsigned int* pdynsym_shndx,
101 unsigned int* pversym_shndx,
102 unsigned int* pverdef_shndx,
103 unsigned int* pverneed_shndx,
104 unsigned int* pdynamic_shndx)
106 *pdynsym_shndx = -1U;
107 *pversym_shndx = -1U;
108 *pverdef_shndx = -1U;
109 *pverneed_shndx = -1U;
110 *pdynamic_shndx = -1U;
112 const unsigned int shnum = this->shnum();
113 const unsigned char* p = pshdrs;
114 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
116 typename This::Shdr shdr(p);
118 unsigned int* pi;
119 switch (shdr.get_sh_type())
121 case elfcpp::SHT_DYNSYM:
122 pi = pdynsym_shndx;
123 break;
124 case elfcpp::SHT_GNU_versym:
125 pi = pversym_shndx;
126 break;
127 case elfcpp::SHT_GNU_verdef:
128 pi = pverdef_shndx;
129 break;
130 case elfcpp::SHT_GNU_verneed:
131 pi = pverneed_shndx;
132 break;
133 case elfcpp::SHT_DYNAMIC:
134 pi = pdynamic_shndx;
135 break;
136 default:
137 pi = NULL;
138 break;
141 if (pi == NULL)
142 continue;
144 if (*pi != -1U)
145 this->error(_("unexpected duplicate type %u section: %u, %u"),
146 shdr.get_sh_type(), *pi, i);
148 *pi = i;
152 // Read the contents of section SHNDX. PSHDRS points to the section
153 // headers. TYPE is the expected section type. LINK is the expected
154 // section link. Store the data in *VIEW and *VIEW_SIZE. The
155 // section's sh_info field is stored in *VIEW_INFO.
157 template<int size, bool big_endian>
158 void
159 Sized_dynobj<size, big_endian>::read_dynsym_section(
160 const unsigned char* pshdrs,
161 unsigned int shndx,
162 elfcpp::SHT type,
163 unsigned int link,
164 File_view** view,
165 section_size_type* view_size,
166 unsigned int* view_info)
168 if (shndx == -1U)
170 *view = NULL;
171 *view_size = 0;
172 *view_info = 0;
173 return;
176 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
178 gold_assert(shdr.get_sh_type() == type);
180 if (shdr.get_sh_link() != link)
181 this->error(_("unexpected link in section %u header: %u != %u"),
182 shndx, shdr.get_sh_link(), link);
184 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
185 false);
186 *view_size = convert_to_section_size_type(shdr.get_sh_size());
187 *view_info = shdr.get_sh_info();
190 // Read the dynamic tags. Set the soname field if this shared object
191 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
192 // the section headers. DYNAMIC_SHNDX is the section index of the
193 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
194 // section index and contents of a string table which may be the one
195 // associated with the SHT_DYNAMIC section.
197 template<int size, bool big_endian>
198 void
199 Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
200 unsigned int dynamic_shndx,
201 unsigned int strtab_shndx,
202 const unsigned char* strtabu,
203 off_t strtab_size)
205 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
206 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
208 const off_t dynamic_size = dynamicshdr.get_sh_size();
209 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
210 dynamic_size, false);
212 const unsigned int link = dynamicshdr.get_sh_link();
213 if (link != strtab_shndx)
215 if (link >= this->shnum())
217 this->error(_("DYNAMIC section %u link out of range: %u"),
218 dynamic_shndx, link);
219 return;
222 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
223 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
225 this->error(_("DYNAMIC section %u link %u is not a strtab"),
226 dynamic_shndx, link);
227 return;
230 strtab_size = strtabshdr.get_sh_size();
231 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false);
234 const char* const strtab = reinterpret_cast<const char*>(strtabu);
236 for (const unsigned char* p = pdynamic;
237 p < pdynamic + dynamic_size;
238 p += This::dyn_size)
240 typename This::Dyn dyn(p);
242 switch (dyn.get_d_tag())
244 case elfcpp::DT_NULL:
245 // We should always see DT_NULL at the end of the dynamic
246 // tags.
247 return;
249 case elfcpp::DT_SONAME:
251 off_t val = dyn.get_d_val();
252 if (val >= strtab_size)
253 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
254 static_cast<long long>(val),
255 static_cast<long long>(strtab_size));
256 else
257 this->set_soname_string(strtab + val);
259 break;
261 case elfcpp::DT_NEEDED:
263 off_t val = dyn.get_d_val();
264 if (val >= strtab_size)
265 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
266 static_cast<long long>(val),
267 static_cast<long long>(strtab_size));
268 else
269 this->add_needed(strtab + val);
271 break;
273 default:
274 break;
278 this->error(_("missing DT_NULL in dynamic segment"));
281 // Read the symbols and sections from a dynamic object. We read the
282 // dynamic symbols, not the normal symbols.
284 template<int size, bool big_endian>
285 void
286 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
288 this->read_section_data(&this->elf_file_, sd);
290 const unsigned char* const pshdrs = sd->section_headers->data();
292 unsigned int dynsym_shndx;
293 unsigned int versym_shndx;
294 unsigned int verdef_shndx;
295 unsigned int verneed_shndx;
296 unsigned int dynamic_shndx;
297 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
298 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
300 unsigned int strtab_shndx = -1U;
302 sd->symbols = NULL;
303 sd->symbols_size = 0;
304 sd->external_symbols_offset = 0;
305 sd->symbol_names = NULL;
306 sd->symbol_names_size = 0;
308 if (dynsym_shndx != -1U)
310 // Get the dynamic symbols.
311 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
312 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
314 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
315 dynsymshdr.get_sh_size(), false);
316 sd->symbols_size =
317 convert_to_section_size_type(dynsymshdr.get_sh_size());
319 // Get the symbol names.
320 strtab_shndx = dynsymshdr.get_sh_link();
321 if (strtab_shndx >= this->shnum())
323 this->error(_("invalid dynamic symbol table name index: %u"),
324 strtab_shndx);
325 return;
327 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
328 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
330 this->error(_("dynamic symbol table name section "
331 "has wrong type: %u"),
332 static_cast<unsigned int>(strtabshdr.get_sh_type()));
333 return;
336 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
337 strtabshdr.get_sh_size(),
338 false);
339 sd->symbol_names_size =
340 convert_to_section_size_type(strtabshdr.get_sh_size());
342 // Get the version information.
344 unsigned int dummy;
345 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
346 dynsym_shndx, &sd->versym, &sd->versym_size,
347 &dummy);
349 // We require that the version definition and need section link
350 // to the same string table as the dynamic symbol table. This
351 // is not a technical requirement, but it always happens in
352 // practice. We could change this if necessary.
354 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
355 strtab_shndx, &sd->verdef, &sd->verdef_size,
356 &sd->verdef_info);
358 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
359 strtab_shndx, &sd->verneed, &sd->verneed_size,
360 &sd->verneed_info);
363 // Read the SHT_DYNAMIC section to find whether this shared object
364 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
365 // doesn't really have anything to do with reading the symbols, but
366 // this is a convenient place to do it.
367 if (dynamic_shndx != -1U)
368 this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
369 (sd->symbol_names == NULL
370 ? NULL
371 : sd->symbol_names->data()),
372 sd->symbol_names_size);
375 // Lay out the input sections for a dynamic object. We don't want to
376 // include sections from a dynamic object, so all that we actually do
377 // here is check for .gnu.warning sections.
379 template<int size, bool big_endian>
380 void
381 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
382 Layout*,
383 Read_symbols_data* sd)
385 const unsigned int shnum = this->shnum();
386 if (shnum == 0)
387 return;
389 // Get the section headers.
390 const unsigned char* pshdrs = sd->section_headers->data();
392 // Get the section names.
393 const unsigned char* pnamesu = sd->section_names->data();
394 const char* pnames = reinterpret_cast<const char*>(pnamesu);
396 // Skip the first, dummy, section.
397 pshdrs += This::shdr_size;
398 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
400 typename This::Shdr shdr(pshdrs);
402 if (shdr.get_sh_name() >= sd->section_names_size)
404 this->error(_("bad section name offset for section %u: %lu"),
405 i, static_cast<unsigned long>(shdr.get_sh_name()));
406 return;
409 const char* name = pnames + shdr.get_sh_name();
411 this->handle_gnu_warning_section(name, i, symtab);
414 delete sd->section_headers;
415 sd->section_headers = NULL;
416 delete sd->section_names;
417 sd->section_names = NULL;
420 // Add an entry to the vector mapping version numbers to version
421 // strings.
423 template<int size, bool big_endian>
424 void
425 Sized_dynobj<size, big_endian>::set_version_map(
426 Version_map* version_map,
427 unsigned int ndx,
428 const char* name) const
430 if (ndx >= version_map->size())
431 version_map->resize(ndx + 1);
432 if ((*version_map)[ndx] != NULL)
433 this->error(_("duplicate definition for version %u"), ndx);
434 (*version_map)[ndx] = name;
437 // Add mappings for the version definitions to VERSION_MAP.
439 template<int size, bool big_endian>
440 void
441 Sized_dynobj<size, big_endian>::make_verdef_map(
442 Read_symbols_data* sd,
443 Version_map* version_map) const
445 if (sd->verdef == NULL)
446 return;
448 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
449 section_size_type names_size = sd->symbol_names_size;
451 const unsigned char* pverdef = sd->verdef->data();
452 section_size_type verdef_size = sd->verdef_size;
453 const unsigned int count = sd->verdef_info;
455 const unsigned char* p = pverdef;
456 for (unsigned int i = 0; i < count; ++i)
458 elfcpp::Verdef<size, big_endian> verdef(p);
460 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
462 this->error(_("unexpected verdef version %u"),
463 verdef.get_vd_version());
464 return;
467 const section_size_type vd_ndx = verdef.get_vd_ndx();
469 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
470 // sure why.
472 // The first Verdaux holds the name of this version. Subsequent
473 // ones are versions that this one depends upon, which we don't
474 // care about here.
475 const section_size_type vd_cnt = verdef.get_vd_cnt();
476 if (vd_cnt < 1)
478 this->error(_("verdef vd_cnt field too small: %u"),
479 static_cast<unsigned int>(vd_cnt));
480 return;
483 const section_size_type vd_aux = verdef.get_vd_aux();
484 if ((p - pverdef) + vd_aux >= verdef_size)
486 this->error(_("verdef vd_aux field out of range: %u"),
487 static_cast<unsigned int>(vd_aux));
488 return;
491 const unsigned char* pvda = p + vd_aux;
492 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
494 const section_size_type vda_name = verdaux.get_vda_name();
495 if (vda_name >= names_size)
497 this->error(_("verdaux vda_name field out of range: %u"),
498 static_cast<unsigned int>(vda_name));
499 return;
502 this->set_version_map(version_map, vd_ndx, names + vda_name);
504 const section_size_type vd_next = verdef.get_vd_next();
505 if ((p - pverdef) + vd_next >= verdef_size)
507 this->error(_("verdef vd_next field out of range: %u"),
508 static_cast<unsigned int>(vd_next));
509 return;
512 p += vd_next;
516 // Add mappings for the required versions to VERSION_MAP.
518 template<int size, bool big_endian>
519 void
520 Sized_dynobj<size, big_endian>::make_verneed_map(
521 Read_symbols_data* sd,
522 Version_map* version_map) const
524 if (sd->verneed == NULL)
525 return;
527 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
528 section_size_type names_size = sd->symbol_names_size;
530 const unsigned char* pverneed = sd->verneed->data();
531 const section_size_type verneed_size = sd->verneed_size;
532 const unsigned int count = sd->verneed_info;
534 const unsigned char* p = pverneed;
535 for (unsigned int i = 0; i < count; ++i)
537 elfcpp::Verneed<size, big_endian> verneed(p);
539 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
541 this->error(_("unexpected verneed version %u"),
542 verneed.get_vn_version());
543 return;
546 const section_size_type vn_aux = verneed.get_vn_aux();
548 if ((p - pverneed) + vn_aux >= verneed_size)
550 this->error(_("verneed vn_aux field out of range: %u"),
551 static_cast<unsigned int>(vn_aux));
552 return;
555 const unsigned int vn_cnt = verneed.get_vn_cnt();
556 const unsigned char* pvna = p + vn_aux;
557 for (unsigned int j = 0; j < vn_cnt; ++j)
559 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
561 const unsigned int vna_name = vernaux.get_vna_name();
562 if (vna_name >= names_size)
564 this->error(_("vernaux vna_name field out of range: %u"),
565 static_cast<unsigned int>(vna_name));
566 return;
569 this->set_version_map(version_map, vernaux.get_vna_other(),
570 names + vna_name);
572 const section_size_type vna_next = vernaux.get_vna_next();
573 if ((pvna - pverneed) + vna_next >= verneed_size)
575 this->error(_("verneed vna_next field out of range: %u"),
576 static_cast<unsigned int>(vna_next));
577 return;
580 pvna += vna_next;
583 const section_size_type vn_next = verneed.get_vn_next();
584 if ((p - pverneed) + vn_next >= verneed_size)
586 this->error(_("verneed vn_next field out of range: %u"),
587 static_cast<unsigned int>(vn_next));
588 return;
591 p += vn_next;
595 // Create a vector mapping version numbers to version strings.
597 template<int size, bool big_endian>
598 void
599 Sized_dynobj<size, big_endian>::make_version_map(
600 Read_symbols_data* sd,
601 Version_map* version_map) const
603 if (sd->verdef == NULL && sd->verneed == NULL)
604 return;
606 // A guess at the maximum version number we will see. If this is
607 // wrong we will be less efficient but still correct.
608 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
610 this->make_verdef_map(sd, version_map);
611 this->make_verneed_map(sd, version_map);
614 // Add the dynamic symbols to the symbol table.
616 template<int size, bool big_endian>
617 void
618 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
619 Read_symbols_data* sd)
621 if (sd->symbols == NULL)
623 gold_assert(sd->symbol_names == NULL);
624 gold_assert(sd->versym == NULL && sd->verdef == NULL
625 && sd->verneed == NULL);
626 return;
629 const int sym_size = This::sym_size;
630 const size_t symcount = sd->symbols_size / sym_size;
631 gold_assert(sd->external_symbols_offset == 0);
632 if (symcount * sym_size != sd->symbols_size)
634 this->error(_("size of dynamic symbols is not multiple of symbol size"));
635 return;
638 Version_map version_map;
639 this->make_version_map(sd, &version_map);
641 const char* sym_names =
642 reinterpret_cast<const char*>(sd->symbol_names->data());
643 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
644 sym_names, sd->symbol_names_size,
645 (sd->versym == NULL
646 ? NULL
647 : sd->versym->data()),
648 sd->versym_size,
649 &version_map);
651 delete sd->symbols;
652 sd->symbols = NULL;
653 delete sd->symbol_names;
654 sd->symbol_names = NULL;
655 if (sd->versym != NULL)
657 delete sd->versym;
658 sd->versym = NULL;
660 if (sd->verdef != NULL)
662 delete sd->verdef;
663 sd->verdef = NULL;
665 if (sd->verneed != NULL)
667 delete sd->verneed;
668 sd->verneed = NULL;
671 // This is normally the last time we will read any data from this
672 // file.
673 this->clear_view_cache_marks();
676 // Given a vector of hash codes, compute the number of hash buckets to
677 // use.
679 unsigned int
680 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
681 bool for_gnu_hash_table)
683 // FIXME: Implement optional hash table optimization.
685 // Array used to determine the number of hash table buckets to use
686 // based on the number of symbols there are. If there are fewer
687 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
688 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
689 // use more than 32771 buckets. This is straight from the old GNU
690 // linker.
691 static const unsigned int buckets[] =
693 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
694 16411, 32771
696 const int buckets_count = sizeof buckets / sizeof buckets[0];
698 unsigned int symcount = hashcodes.size();
699 unsigned int ret = 1;
700 for (int i = 0; i < buckets_count; ++i)
702 if (symcount < buckets[i])
703 break;
704 ret = buckets[i];
707 if (for_gnu_hash_table && ret < 2)
708 ret = 2;
710 return ret;
713 // The standard ELF hash function. This hash function must not
714 // change, as the dynamic linker uses it also.
716 uint32_t
717 Dynobj::elf_hash(const char* name)
719 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
720 uint32_t h = 0;
721 unsigned char c;
722 while ((c = *nameu++) != '\0')
724 h = (h << 4) + c;
725 uint32_t g = h & 0xf0000000;
726 if (g != 0)
728 h ^= g >> 24;
729 // The ELF ABI says h &= ~g, but using xor is equivalent in
730 // this case (since g was set from h) and may save one
731 // instruction.
732 h ^= g;
735 return h;
738 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
739 // DYNSYMS is a vector with all the global dynamic symbols.
740 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
741 // symbol table.
743 void
744 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
745 unsigned int local_dynsym_count,
746 unsigned char** pphash,
747 unsigned int* phashlen)
749 unsigned int dynsym_count = dynsyms.size();
751 // Get the hash values for all the symbols.
752 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
753 for (unsigned int i = 0; i < dynsym_count; ++i)
754 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
756 const unsigned int bucketcount =
757 Dynobj::compute_bucket_count(dynsym_hashvals, false);
759 std::vector<uint32_t> bucket(bucketcount);
760 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
762 for (unsigned int i = 0; i < dynsym_count; ++i)
764 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
765 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
766 chain[dynsym_index] = bucket[bucketpos];
767 bucket[bucketpos] = dynsym_index;
770 unsigned int hashlen = ((2
771 + bucketcount
772 + local_dynsym_count
773 + dynsym_count)
774 * 4);
775 unsigned char* phash = new unsigned char[hashlen];
777 if (parameters->is_big_endian())
779 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
780 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
781 hashlen);
782 #else
783 gold_unreachable();
784 #endif
786 else
788 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
789 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
790 hashlen);
791 #else
792 gold_unreachable();
793 #endif
796 *pphash = phash;
797 *phashlen = hashlen;
800 // Fill in an ELF hash table.
802 template<bool big_endian>
803 void
804 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
805 const std::vector<uint32_t>& chain,
806 unsigned char* phash,
807 unsigned int hashlen)
809 unsigned char* p = phash;
811 const unsigned int bucketcount = bucket.size();
812 const unsigned int chaincount = chain.size();
814 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
815 p += 4;
816 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
817 p += 4;
819 for (unsigned int i = 0; i < bucketcount; ++i)
821 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
822 p += 4;
825 for (unsigned int i = 0; i < chaincount; ++i)
827 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
828 p += 4;
831 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
834 // The hash function used for the GNU hash table. This hash function
835 // must not change, as the dynamic linker uses it also.
837 uint32_t
838 Dynobj::gnu_hash(const char* name)
840 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
841 uint32_t h = 5381;
842 unsigned char c;
843 while ((c = *nameu++) != '\0')
844 h = (h << 5) + h + c;
845 return h;
848 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
849 // tables are an extension to ELF which are recognized by the GNU
850 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
851 // TARGET is the target. DYNSYMS is a vector with all the global
852 // symbols which will be going into the dynamic symbol table.
853 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
854 // symbol table.
856 void
857 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
858 unsigned int local_dynsym_count,
859 unsigned char** pphash,
860 unsigned int* phashlen)
862 const unsigned int count = dynsyms.size();
864 // Sort the dynamic symbols into two vectors. Symbols which we do
865 // not want to put into the hash table we store into
866 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
867 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
868 // and records the hash codes.
870 std::vector<Symbol*> unhashed_dynsyms;
871 unhashed_dynsyms.reserve(count);
873 std::vector<Symbol*> hashed_dynsyms;
874 hashed_dynsyms.reserve(count);
876 std::vector<uint32_t> dynsym_hashvals;
877 dynsym_hashvals.reserve(count);
879 for (unsigned int i = 0; i < count; ++i)
881 Symbol* sym = dynsyms[i];
883 // FIXME: Should put on unhashed_dynsyms if the symbol is
884 // hidden.
885 if (sym->is_undefined())
886 unhashed_dynsyms.push_back(sym);
887 else
889 hashed_dynsyms.push_back(sym);
890 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
894 // Put the unhashed symbols at the start of the global portion of
895 // the dynamic symbol table.
896 const unsigned int unhashed_count = unhashed_dynsyms.size();
897 unsigned int unhashed_dynsym_index = local_dynsym_count;
898 for (unsigned int i = 0; i < unhashed_count; ++i)
900 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
901 ++unhashed_dynsym_index;
904 // For the actual data generation we call out to a templatized
905 // function.
906 int size = parameters->get_size();
907 bool big_endian = parameters->is_big_endian();
908 if (size == 32)
910 if (big_endian)
912 #ifdef HAVE_TARGET_32_BIG
913 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
914 dynsym_hashvals,
915 unhashed_dynsym_index,
916 pphash,
917 phashlen);
918 #else
919 gold_unreachable();
920 #endif
922 else
924 #ifdef HAVE_TARGET_32_LITTLE
925 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
926 dynsym_hashvals,
927 unhashed_dynsym_index,
928 pphash,
929 phashlen);
930 #else
931 gold_unreachable();
932 #endif
935 else if (size == 64)
937 if (big_endian)
939 #ifdef HAVE_TARGET_64_BIG
940 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
941 dynsym_hashvals,
942 unhashed_dynsym_index,
943 pphash,
944 phashlen);
945 #else
946 gold_unreachable();
947 #endif
949 else
951 #ifdef HAVE_TARGET_64_LITTLE
952 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
953 dynsym_hashvals,
954 unhashed_dynsym_index,
955 pphash,
956 phashlen);
957 #else
958 gold_unreachable();
959 #endif
962 else
963 gold_unreachable();
966 // Create the actual data for a GNU hash table. This is just a copy
967 // of the code from the old GNU linker.
969 template<int size, bool big_endian>
970 void
971 Dynobj::sized_create_gnu_hash_table(
972 const std::vector<Symbol*>& hashed_dynsyms,
973 const std::vector<uint32_t>& dynsym_hashvals,
974 unsigned int unhashed_dynsym_count,
975 unsigned char** pphash,
976 unsigned int* phashlen)
978 if (hashed_dynsyms.empty())
980 // Special case for the empty hash table.
981 unsigned int hashlen = 5 * 4 + size / 8;
982 unsigned char* phash = new unsigned char[hashlen];
983 // One empty bucket.
984 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
985 // Symbol index above unhashed symbols.
986 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
987 // One word for bitmask.
988 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
989 // Only bloom filter.
990 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
991 // No valid hashes.
992 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
993 // No hashes in only bucket.
994 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
996 *phashlen = hashlen;
997 *pphash = phash;
999 return;
1002 const unsigned int bucketcount =
1003 Dynobj::compute_bucket_count(dynsym_hashvals, true);
1005 const unsigned int nsyms = hashed_dynsyms.size();
1007 uint32_t maskbitslog2 = 1;
1008 uint32_t x = nsyms >> 1;
1009 while (x != 0)
1011 ++maskbitslog2;
1012 x >>= 1;
1014 if (maskbitslog2 < 3)
1015 maskbitslog2 = 5;
1016 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1017 maskbitslog2 += 3;
1018 else
1019 maskbitslog2 += 2;
1021 uint32_t shift1;
1022 if (size == 32)
1023 shift1 = 5;
1024 else
1026 if (maskbitslog2 == 5)
1027 maskbitslog2 = 6;
1028 shift1 = 6;
1030 uint32_t mask = (1U << shift1) - 1U;
1031 uint32_t shift2 = maskbitslog2;
1032 uint32_t maskbits = 1U << maskbitslog2;
1033 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1035 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1036 std::vector<Word> bitmask(maskwords);
1037 std::vector<uint32_t> counts(bucketcount);
1038 std::vector<uint32_t> indx(bucketcount);
1039 uint32_t symindx = unhashed_dynsym_count;
1041 // Count the number of times each hash bucket is used.
1042 for (unsigned int i = 0; i < nsyms; ++i)
1043 ++counts[dynsym_hashvals[i] % bucketcount];
1045 unsigned int cnt = symindx;
1046 for (unsigned int i = 0; i < bucketcount; ++i)
1048 indx[i] = cnt;
1049 cnt += counts[i];
1052 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1053 hashlen += maskbits / 8;
1054 unsigned char* phash = new unsigned char[hashlen];
1056 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1057 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1058 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1059 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1061 unsigned char* p = phash + 16 + maskbits / 8;
1062 for (unsigned int i = 0; i < bucketcount; ++i)
1064 if (counts[i] == 0)
1065 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1066 else
1067 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1068 p += 4;
1071 for (unsigned int i = 0; i < nsyms; ++i)
1073 Symbol* sym = hashed_dynsyms[i];
1074 uint32_t hashval = dynsym_hashvals[i];
1076 unsigned int bucket = hashval % bucketcount;
1077 unsigned int val = ((hashval >> shift1)
1078 & ((maskbits >> shift1) - 1));
1079 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1080 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1081 val = hashval & ~ 1U;
1082 if (counts[bucket] == 1)
1084 // Last element terminates the chain.
1085 val |= 1;
1087 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1088 val);
1089 --counts[bucket];
1091 sym->set_dynsym_index(indx[bucket]);
1092 ++indx[bucket];
1095 p = phash + 16;
1096 for (unsigned int i = 0; i < maskwords; ++i)
1098 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1099 p += size / 8;
1102 *phashlen = hashlen;
1103 *pphash = phash;
1106 // Verdef methods.
1108 // Write this definition to a buffer for the output section.
1110 template<int size, bool big_endian>
1111 unsigned char*
1112 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb
1113 ACCEPT_SIZE_ENDIAN) const
1115 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1116 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1118 elfcpp::Verdef_write<size, big_endian> vd(pb);
1119 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1120 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1121 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1122 vd.set_vd_ndx(this->index());
1123 vd.set_vd_cnt(1 + this->deps_.size());
1124 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1125 vd.set_vd_aux(verdef_size);
1126 vd.set_vd_next(is_last
1128 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1129 pb += verdef_size;
1131 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1132 vda.set_vda_name(dynpool->get_offset(this->name()));
1133 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1134 pb += verdaux_size;
1136 Deps::const_iterator p;
1137 unsigned int i;
1138 for (p = this->deps_.begin(), i = 0;
1139 p != this->deps_.end();
1140 ++p, ++i)
1142 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1143 vda.set_vda_name(dynpool->get_offset(*p));
1144 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1145 pb += verdaux_size;
1148 return pb;
1151 // Verneed methods.
1153 Verneed::~Verneed()
1155 for (Need_versions::iterator p = this->need_versions_.begin();
1156 p != this->need_versions_.end();
1157 ++p)
1158 delete *p;
1161 // Add a new version to this file reference.
1163 Verneed_version*
1164 Verneed::add_name(const char* name)
1166 Verneed_version* vv = new Verneed_version(name);
1167 this->need_versions_.push_back(vv);
1168 return vv;
1171 // Set the version indexes starting at INDEX.
1173 unsigned int
1174 Verneed::finalize(unsigned int index)
1176 for (Need_versions::iterator p = this->need_versions_.begin();
1177 p != this->need_versions_.end();
1178 ++p)
1180 (*p)->set_index(index);
1181 ++index;
1183 return index;
1186 // Write this list of referenced versions to a buffer for the output
1187 // section.
1189 template<int size, bool big_endian>
1190 unsigned char*
1191 Verneed::write(const Stringpool* dynpool, bool is_last,
1192 unsigned char* pb ACCEPT_SIZE_ENDIAN) const
1194 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1195 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1197 elfcpp::Verneed_write<size, big_endian> vn(pb);
1198 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1199 vn.set_vn_cnt(this->need_versions_.size());
1200 vn.set_vn_file(dynpool->get_offset(this->filename()));
1201 vn.set_vn_aux(verneed_size);
1202 vn.set_vn_next(is_last
1204 : verneed_size + this->need_versions_.size() * vernaux_size);
1205 pb += verneed_size;
1207 Need_versions::const_iterator p;
1208 unsigned int i;
1209 for (p = this->need_versions_.begin(), i = 0;
1210 p != this->need_versions_.end();
1211 ++p, ++i)
1213 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1214 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1215 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1216 vna.set_vna_flags(0);
1217 vna.set_vna_other((*p)->index());
1218 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1219 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1221 : vernaux_size);
1222 pb += vernaux_size;
1225 return pb;
1228 // Versions methods.
1230 Versions::~Versions()
1232 for (Defs::iterator p = this->defs_.begin();
1233 p != this->defs_.end();
1234 ++p)
1235 delete *p;
1237 for (Needs::iterator p = this->needs_.begin();
1238 p != this->needs_.end();
1239 ++p)
1240 delete *p;
1243 // Return the dynamic object which a symbol refers to.
1245 Dynobj*
1246 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1247 const Symbol* sym) const
1249 if (sym->is_copied_from_dynobj())
1250 return symtab->get_copy_source(sym);
1251 else
1253 Object* object = sym->object();
1254 gold_assert(object->is_dynamic());
1255 return static_cast<Dynobj*>(object);
1259 // Record version information for a symbol going into the dynamic
1260 // symbol table.
1262 void
1263 Versions::record_version(const Symbol_table* symtab,
1264 Stringpool* dynpool, const Symbol* sym)
1266 gold_assert(!this->is_finalized_);
1267 gold_assert(sym->version() != NULL);
1269 Stringpool::Key version_key;
1270 const char* version = dynpool->add(sym->version(), false, &version_key);
1272 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1274 if (parameters->output_is_shared())
1275 this->add_def(sym, version, version_key);
1277 else
1279 // This is a version reference.
1280 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1281 this->add_need(dynpool, dynobj->soname(), version, version_key);
1285 // We've found a symbol SYM defined in version VERSION.
1287 void
1288 Versions::add_def(const Symbol* sym, const char* version,
1289 Stringpool::Key version_key)
1291 Key k(version_key, 0);
1292 Version_base* const vbnull = NULL;
1293 std::pair<Version_table::iterator, bool> ins =
1294 this->version_table_.insert(std::make_pair(k, vbnull));
1296 if (!ins.second)
1298 // We already have an entry for this version.
1299 Version_base* vb = ins.first->second;
1301 // We have now seen a symbol in this version, so it is not
1302 // weak.
1303 gold_assert(vb != NULL);
1304 vb->clear_weak();
1306 // FIXME: When we support version scripts, we will need to
1307 // check whether this symbol should be forced local.
1309 else
1311 // If we are creating a shared object, it is an error to
1312 // find a definition of a symbol with a version which is not
1313 // in the version script.
1314 if (parameters->output_is_shared())
1316 gold_error(_("symbol %s has undefined version %s"),
1317 sym->demangled_name().c_str(), version);
1318 return;
1321 // If this is the first version we are defining, first define
1322 // the base version. FIXME: Should use soname here when
1323 // creating a shared object.
1324 Verdef* vdbase = new Verdef(parameters->output_file_name(), true, false,
1325 true);
1326 this->defs_.push_back(vdbase);
1328 // When creating a regular executable, automatically define
1329 // a new version.
1330 Verdef* vd = new Verdef(version, false, false, false);
1331 this->defs_.push_back(vd);
1332 ins.first->second = vd;
1336 // Add a reference to version NAME in file FILENAME.
1338 void
1339 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1340 Stringpool::Key name_key)
1342 Stringpool::Key filename_key;
1343 filename = dynpool->add(filename, true, &filename_key);
1345 Key k(name_key, filename_key);
1346 Version_base* const vbnull = NULL;
1347 std::pair<Version_table::iterator, bool> ins =
1348 this->version_table_.insert(std::make_pair(k, vbnull));
1350 if (!ins.second)
1352 // We already have an entry for this filename/version.
1353 return;
1356 // See whether we already have this filename. We don't expect many
1357 // version references, so we just do a linear search. This could be
1358 // replaced by a hash table.
1359 Verneed* vn = NULL;
1360 for (Needs::iterator p = this->needs_.begin();
1361 p != this->needs_.end();
1362 ++p)
1364 if ((*p)->filename() == filename)
1366 vn = *p;
1367 break;
1371 if (vn == NULL)
1373 // We have a new filename.
1374 vn = new Verneed(filename);
1375 this->needs_.push_back(vn);
1378 ins.first->second = vn->add_name(name);
1381 // Set the version indexes. Create a new dynamic version symbol for
1382 // each new version definition.
1384 unsigned int
1385 Versions::finalize(const Target* target, Symbol_table* symtab,
1386 unsigned int dynsym_index, std::vector<Symbol*>* syms)
1388 gold_assert(!this->is_finalized_);
1390 unsigned int vi = 1;
1392 for (Defs::iterator p = this->defs_.begin();
1393 p != this->defs_.end();
1394 ++p)
1396 (*p)->set_index(vi);
1397 ++vi;
1399 // Create a version symbol if necessary.
1400 if (!(*p)->is_symbol_created())
1402 Symbol* vsym = symtab->define_as_constant(target, (*p)->name(),
1403 (*p)->name(), 0, 0,
1404 elfcpp::STT_OBJECT,
1405 elfcpp::STB_GLOBAL,
1406 elfcpp::STV_DEFAULT, 0,
1407 false);
1408 vsym->set_needs_dynsym_entry();
1409 vsym->set_dynsym_index(dynsym_index);
1410 ++dynsym_index;
1411 syms->push_back(vsym);
1412 // The name is already in the dynamic pool.
1416 // Index 1 is used for global symbols.
1417 if (vi == 1)
1419 gold_assert(this->defs_.empty());
1420 vi = 2;
1423 for (Needs::iterator p = this->needs_.begin();
1424 p != this->needs_.end();
1425 ++p)
1426 vi = (*p)->finalize(vi);
1428 this->is_finalized_ = true;
1430 return dynsym_index;
1433 // Return the version index to use for a symbol. This does two hash
1434 // table lookups: one in DYNPOOL and one in this->version_table_.
1435 // Another approach alternative would be store a pointer in SYM, which
1436 // would increase the size of the symbol table. Or perhaps we could
1437 // use a hash table from dynamic symbol pointer values to Version_base
1438 // pointers.
1440 unsigned int
1441 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1442 const Symbol* sym) const
1444 Stringpool::Key version_key;
1445 const char* version = dynpool->find(sym->version(), &version_key);
1446 gold_assert(version != NULL);
1448 Key k;
1449 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1451 if (!parameters->output_is_shared())
1452 return elfcpp::VER_NDX_GLOBAL;
1453 k = Key(version_key, 0);
1455 else
1457 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1459 Stringpool::Key filename_key;
1460 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1461 gold_assert(filename != NULL);
1463 k = Key(version_key, filename_key);
1466 Version_table::const_iterator p = this->version_table_.find(k);
1467 gold_assert(p != this->version_table_.end());
1469 return p->second->index();
1472 // Return an allocated buffer holding the contents of the symbol
1473 // version section.
1475 template<int size, bool big_endian>
1476 void
1477 Versions::symbol_section_contents(const Symbol_table* symtab,
1478 const Stringpool* dynpool,
1479 unsigned int local_symcount,
1480 const std::vector<Symbol*>& syms,
1481 unsigned char** pp,
1482 unsigned int* psize
1483 ACCEPT_SIZE_ENDIAN) const
1485 gold_assert(this->is_finalized_);
1487 unsigned int sz = (local_symcount + syms.size()) * 2;
1488 unsigned char* pbuf = new unsigned char[sz];
1490 for (unsigned int i = 0; i < local_symcount; ++i)
1491 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1492 elfcpp::VER_NDX_LOCAL);
1494 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1495 p != syms.end();
1496 ++p)
1498 unsigned int version_index;
1499 const char* version = (*p)->version();
1500 if (version == NULL)
1501 version_index = elfcpp::VER_NDX_GLOBAL;
1502 else
1503 version_index = this->version_index(symtab, dynpool, *p);
1504 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1505 version_index);
1508 *pp = pbuf;
1509 *psize = sz;
1512 // Return an allocated buffer holding the contents of the version
1513 // definition section.
1515 template<int size, bool big_endian>
1516 void
1517 Versions::def_section_contents(const Stringpool* dynpool,
1518 unsigned char** pp, unsigned int* psize,
1519 unsigned int* pentries
1520 ACCEPT_SIZE_ENDIAN) const
1522 gold_assert(this->is_finalized_);
1523 gold_assert(!this->defs_.empty());
1525 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1526 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1528 unsigned int sz = 0;
1529 for (Defs::const_iterator p = this->defs_.begin();
1530 p != this->defs_.end();
1531 ++p)
1533 sz += verdef_size + verdaux_size;
1534 sz += (*p)->count_dependencies() * verdaux_size;
1537 unsigned char* pbuf = new unsigned char[sz];
1539 unsigned char* pb = pbuf;
1540 Defs::const_iterator p;
1541 unsigned int i;
1542 for (p = this->defs_.begin(), i = 0;
1543 p != this->defs_.end();
1544 ++p, ++i)
1545 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1546 dynpool, i + 1 >= this->defs_.size(), pb
1547 SELECT_SIZE_ENDIAN(size, big_endian));
1549 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1551 *pp = pbuf;
1552 *psize = sz;
1553 *pentries = this->defs_.size();
1556 // Return an allocated buffer holding the contents of the version
1557 // reference section.
1559 template<int size, bool big_endian>
1560 void
1561 Versions::need_section_contents(const Stringpool* dynpool,
1562 unsigned char** pp, unsigned int *psize,
1563 unsigned int *pentries
1564 ACCEPT_SIZE_ENDIAN) const
1566 gold_assert(this->is_finalized_);
1567 gold_assert(!this->needs_.empty());
1569 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1570 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1572 unsigned int sz = 0;
1573 for (Needs::const_iterator p = this->needs_.begin();
1574 p != this->needs_.end();
1575 ++p)
1577 sz += verneed_size;
1578 sz += (*p)->count_versions() * vernaux_size;
1581 unsigned char* pbuf = new unsigned char[sz];
1583 unsigned char* pb = pbuf;
1584 Needs::const_iterator p;
1585 unsigned int i;
1586 for (p = this->needs_.begin(), i = 0;
1587 p != this->needs_.end();
1588 ++p, ++i)
1589 pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1590 dynpool, i + 1 >= this->needs_.size(), pb
1591 SELECT_SIZE_ENDIAN(size, big_endian));
1593 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1595 *pp = pbuf;
1596 *psize = sz;
1597 *pentries = this->needs_.size();
1600 // Instantiate the templates we need. We could use the configure
1601 // script to restrict this to only the ones for implemented targets.
1603 #ifdef HAVE_TARGET_32_LITTLE
1604 template
1605 class Sized_dynobj<32, false>;
1606 #endif
1608 #ifdef HAVE_TARGET_32_BIG
1609 template
1610 class Sized_dynobj<32, true>;
1611 #endif
1613 #ifdef HAVE_TARGET_64_LITTLE
1614 template
1615 class Sized_dynobj<64, false>;
1616 #endif
1618 #ifdef HAVE_TARGET_64_BIG
1619 template
1620 class Sized_dynobj<64, true>;
1621 #endif
1623 #ifdef HAVE_TARGET_32_LITTLE
1624 template
1625 void
1626 Versions::symbol_section_contents<32, false>(
1627 const Symbol_table*,
1628 const Stringpool*,
1629 unsigned int,
1630 const std::vector<Symbol*>&,
1631 unsigned char**,
1632 unsigned int*
1633 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1634 #endif
1636 #ifdef HAVE_TARGET_32_BIG
1637 template
1638 void
1639 Versions::symbol_section_contents<32, true>(
1640 const Symbol_table*,
1641 const Stringpool*,
1642 unsigned int,
1643 const std::vector<Symbol*>&,
1644 unsigned char**,
1645 unsigned int*
1646 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1647 #endif
1649 #ifdef HAVE_TARGET_64_LITTLE
1650 template
1651 void
1652 Versions::symbol_section_contents<64, false>(
1653 const Symbol_table*,
1654 const Stringpool*,
1655 unsigned int,
1656 const std::vector<Symbol*>&,
1657 unsigned char**,
1658 unsigned int*
1659 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1660 #endif
1662 #ifdef HAVE_TARGET_64_BIG
1663 template
1664 void
1665 Versions::symbol_section_contents<64, true>(
1666 const Symbol_table*,
1667 const Stringpool*,
1668 unsigned int,
1669 const std::vector<Symbol*>&,
1670 unsigned char**,
1671 unsigned int*
1672 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1673 #endif
1675 #ifdef HAVE_TARGET_32_LITTLE
1676 template
1677 void
1678 Versions::def_section_contents<32, false>(
1679 const Stringpool*,
1680 unsigned char**,
1681 unsigned int*,
1682 unsigned int*
1683 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1684 #endif
1686 #ifdef HAVE_TARGET_32_BIG
1687 template
1688 void
1689 Versions::def_section_contents<32, true>(
1690 const Stringpool*,
1691 unsigned char**,
1692 unsigned int*,
1693 unsigned int*
1694 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1695 #endif
1697 #ifdef HAVE_TARGET_64_LITTLE
1698 template
1699 void
1700 Versions::def_section_contents<64, false>(
1701 const Stringpool*,
1702 unsigned char**,
1703 unsigned int*,
1704 unsigned int*
1705 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1706 #endif
1708 #ifdef HAVE_TARGET_64_BIG
1709 template
1710 void
1711 Versions::def_section_contents<64, true>(
1712 const Stringpool*,
1713 unsigned char**,
1714 unsigned int*,
1715 unsigned int*
1716 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1717 #endif
1719 #ifdef HAVE_TARGET_32_LITTLE
1720 template
1721 void
1722 Versions::need_section_contents<32, false>(
1723 const Stringpool*,
1724 unsigned char**,
1725 unsigned int*,
1726 unsigned int*
1727 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1728 #endif
1730 #ifdef HAVE_TARGET_32_BIG
1731 template
1732 void
1733 Versions::need_section_contents<32, true>(
1734 const Stringpool*,
1735 unsigned char**,
1736 unsigned int*,
1737 unsigned int*
1738 ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1739 #endif
1741 #ifdef HAVE_TARGET_64_LITTLE
1742 template
1743 void
1744 Versions::need_section_contents<64, false>(
1745 const Stringpool*,
1746 unsigned char**,
1747 unsigned int*,
1748 unsigned int*
1749 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1750 #endif
1752 #ifdef HAVE_TARGET_64_BIG
1753 template
1754 void
1755 Versions::need_section_contents<64, true>(
1756 const Stringpool*,
1757 unsigned char**,
1758 unsigned int*,
1759 unsigned int*
1760 ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1761 #endif
1763 } // End namespace gold.