daily update
[binutils.git] / gold / dynobj.cc
blobe593b8948de44f6d6f0c55f1a5c4ef7c6cf02167
1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <vector>
26 #include <cstring>
28 #include "elfcpp.h"
29 #include "parameters.h"
30 #include "script.h"
31 #include "symtab.h"
32 #include "dynobj.h"
34 namespace gold
37 // Class Dynobj.
39 // Sets up the default soname_ to use, in the (rare) cases we never
40 // see a DT_SONAME entry.
42 Dynobj::Dynobj(const std::string& name, Input_file* input_file, off_t offset)
43 : Object(name, input_file, true, offset),
44 needed_(),
45 unknown_needed_(UNKNOWN_NEEDED_UNSET)
47 // This will be overridden by a DT_SONAME entry, hopefully. But if
48 // we never see a DT_SONAME entry, our rule is to use the dynamic
49 // object's filename. The only exception is when the dynamic object
50 // is part of an archive (so the filename is the archive's
51 // filename). In that case, we use just the dynobj's name-in-archive.
52 this->soname_ = this->input_file()->found_name();
53 if (this->offset() != 0)
55 std::string::size_type open_paren = this->name().find('(');
56 std::string::size_type close_paren = this->name().find(')');
57 if (open_paren != std::string::npos && close_paren != std::string::npos)
59 // It's an archive, and name() is of the form 'foo.a(bar.so)'.
60 this->soname_ = this->name().substr(open_paren + 1,
61 close_paren - (open_paren + 1));
66 // Class Sized_dynobj.
68 template<int size, bool big_endian>
69 Sized_dynobj<size, big_endian>::Sized_dynobj(
70 const std::string& name,
71 Input_file* input_file,
72 off_t offset,
73 const elfcpp::Ehdr<size, big_endian>& ehdr)
74 : Dynobj(name, input_file, offset),
75 elf_file_(this, ehdr)
79 // Set up the object.
81 template<int size, bool big_endian>
82 void
83 Sized_dynobj<size, big_endian>::setup(
84 const elfcpp::Ehdr<size, big_endian>& ehdr)
86 this->set_target(ehdr.get_e_machine(), size, big_endian,
87 ehdr.get_e_ident()[elfcpp::EI_OSABI],
88 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
90 const unsigned int shnum = this->elf_file_.shnum();
91 this->set_shnum(shnum);
94 // Find the SHT_DYNSYM section and the various version sections, and
95 // the dynamic section, given the section headers.
97 template<int size, bool big_endian>
98 void
99 Sized_dynobj<size, big_endian>::find_dynsym_sections(
100 const unsigned char* pshdrs,
101 unsigned int* pdynsym_shndx,
102 unsigned int* pversym_shndx,
103 unsigned int* pverdef_shndx,
104 unsigned int* pverneed_shndx,
105 unsigned int* pdynamic_shndx)
107 *pdynsym_shndx = -1U;
108 *pversym_shndx = -1U;
109 *pverdef_shndx = -1U;
110 *pverneed_shndx = -1U;
111 *pdynamic_shndx = -1U;
113 const unsigned int shnum = this->shnum();
114 const unsigned char* p = pshdrs;
115 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
117 typename This::Shdr shdr(p);
119 unsigned int* pi;
120 switch (shdr.get_sh_type())
122 case elfcpp::SHT_DYNSYM:
123 pi = pdynsym_shndx;
124 break;
125 case elfcpp::SHT_GNU_versym:
126 pi = pversym_shndx;
127 break;
128 case elfcpp::SHT_GNU_verdef:
129 pi = pverdef_shndx;
130 break;
131 case elfcpp::SHT_GNU_verneed:
132 pi = pverneed_shndx;
133 break;
134 case elfcpp::SHT_DYNAMIC:
135 pi = pdynamic_shndx;
136 break;
137 default:
138 pi = NULL;
139 break;
142 if (pi == NULL)
143 continue;
145 if (*pi != -1U)
146 this->error(_("unexpected duplicate type %u section: %u, %u"),
147 shdr.get_sh_type(), *pi, i);
149 *pi = i;
153 // Read the contents of section SHNDX. PSHDRS points to the section
154 // headers. TYPE is the expected section type. LINK is the expected
155 // section link. Store the data in *VIEW and *VIEW_SIZE. The
156 // section's sh_info field is stored in *VIEW_INFO.
158 template<int size, bool big_endian>
159 void
160 Sized_dynobj<size, big_endian>::read_dynsym_section(
161 const unsigned char* pshdrs,
162 unsigned int shndx,
163 elfcpp::SHT type,
164 unsigned int link,
165 File_view** view,
166 section_size_type* view_size,
167 unsigned int* view_info)
169 if (shndx == -1U)
171 *view = NULL;
172 *view_size = 0;
173 *view_info = 0;
174 return;
177 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
179 gold_assert(shdr.get_sh_type() == type);
181 if (shdr.get_sh_link() != link)
182 this->error(_("unexpected link in section %u header: %u != %u"),
183 shndx, shdr.get_sh_link(), link);
185 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
186 true, false);
187 *view_size = convert_to_section_size_type(shdr.get_sh_size());
188 *view_info = shdr.get_sh_info();
191 // Read the dynamic tags. Set the soname field if this shared object
192 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
193 // the section headers. DYNAMIC_SHNDX is the section index of the
194 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
195 // section index and contents of a string table which may be the one
196 // associated with the SHT_DYNAMIC section.
198 template<int size, bool big_endian>
199 void
200 Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
201 unsigned int dynamic_shndx,
202 unsigned int strtab_shndx,
203 const unsigned char* strtabu,
204 off_t strtab_size)
206 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
207 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
209 const off_t dynamic_size = dynamicshdr.get_sh_size();
210 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
211 dynamic_size, true, false);
213 const unsigned int link = dynamicshdr.get_sh_link();
214 if (link != strtab_shndx)
216 if (link >= this->shnum())
218 this->error(_("DYNAMIC section %u link out of range: %u"),
219 dynamic_shndx, link);
220 return;
223 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
224 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
226 this->error(_("DYNAMIC section %u link %u is not a strtab"),
227 dynamic_shndx, link);
228 return;
231 strtab_size = strtabshdr.get_sh_size();
232 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false,
233 false);
236 const char* const strtab = reinterpret_cast<const char*>(strtabu);
238 for (const unsigned char* p = pdynamic;
239 p < pdynamic + dynamic_size;
240 p += This::dyn_size)
242 typename This::Dyn dyn(p);
244 switch (dyn.get_d_tag())
246 case elfcpp::DT_NULL:
247 // We should always see DT_NULL at the end of the dynamic
248 // tags.
249 return;
251 case elfcpp::DT_SONAME:
253 off_t val = dyn.get_d_val();
254 if (val >= strtab_size)
255 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
256 static_cast<long long>(val),
257 static_cast<long long>(strtab_size));
258 else
259 this->set_soname_string(strtab + val);
261 break;
263 case elfcpp::DT_NEEDED:
265 off_t val = dyn.get_d_val();
266 if (val >= strtab_size)
267 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
268 static_cast<long long>(val),
269 static_cast<long long>(strtab_size));
270 else
271 this->add_needed(strtab + val);
273 break;
275 default:
276 break;
280 this->error(_("missing DT_NULL in dynamic segment"));
283 // Read the symbols and sections from a dynamic object. We read the
284 // dynamic symbols, not the normal symbols.
286 template<int size, bool big_endian>
287 void
288 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
290 this->read_section_data(&this->elf_file_, sd);
292 const unsigned char* const pshdrs = sd->section_headers->data();
294 unsigned int dynsym_shndx;
295 unsigned int versym_shndx;
296 unsigned int verdef_shndx;
297 unsigned int verneed_shndx;
298 unsigned int dynamic_shndx;
299 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
300 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
302 unsigned int strtab_shndx = -1U;
304 sd->symbols = NULL;
305 sd->symbols_size = 0;
306 sd->external_symbols_offset = 0;
307 sd->symbol_names = NULL;
308 sd->symbol_names_size = 0;
310 if (dynsym_shndx != -1U)
312 // Get the dynamic symbols.
313 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
314 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
316 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
317 dynsymshdr.get_sh_size(), true,
318 false);
319 sd->symbols_size =
320 convert_to_section_size_type(dynsymshdr.get_sh_size());
322 // Get the symbol names.
323 strtab_shndx = dynsymshdr.get_sh_link();
324 if (strtab_shndx >= this->shnum())
326 this->error(_("invalid dynamic symbol table name index: %u"),
327 strtab_shndx);
328 return;
330 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
331 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
333 this->error(_("dynamic symbol table name section "
334 "has wrong type: %u"),
335 static_cast<unsigned int>(strtabshdr.get_sh_type()));
336 return;
339 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
340 strtabshdr.get_sh_size(),
341 false, false);
342 sd->symbol_names_size =
343 convert_to_section_size_type(strtabshdr.get_sh_size());
345 // Get the version information.
347 unsigned int dummy;
348 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
349 dynsym_shndx, &sd->versym, &sd->versym_size,
350 &dummy);
352 // We require that the version definition and need section link
353 // to the same string table as the dynamic symbol table. This
354 // is not a technical requirement, but it always happens in
355 // practice. We could change this if necessary.
357 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
358 strtab_shndx, &sd->verdef, &sd->verdef_size,
359 &sd->verdef_info);
361 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
362 strtab_shndx, &sd->verneed, &sd->verneed_size,
363 &sd->verneed_info);
366 // Read the SHT_DYNAMIC section to find whether this shared object
367 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
368 // doesn't really have anything to do with reading the symbols, but
369 // this is a convenient place to do it.
370 if (dynamic_shndx != -1U)
371 this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
372 (sd->symbol_names == NULL
373 ? NULL
374 : sd->symbol_names->data()),
375 sd->symbol_names_size);
378 // Lay out the input sections for a dynamic object. We don't want to
379 // include sections from a dynamic object, so all that we actually do
380 // here is check for .gnu.warning sections.
382 template<int size, bool big_endian>
383 void
384 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
385 Layout*,
386 Read_symbols_data* sd)
388 const unsigned int shnum = this->shnum();
389 if (shnum == 0)
390 return;
392 // Get the section headers.
393 const unsigned char* pshdrs = sd->section_headers->data();
395 // Get the section names.
396 const unsigned char* pnamesu = sd->section_names->data();
397 const char* pnames = reinterpret_cast<const char*>(pnamesu);
399 // Skip the first, dummy, section.
400 pshdrs += This::shdr_size;
401 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
403 typename This::Shdr shdr(pshdrs);
405 if (shdr.get_sh_name() >= sd->section_names_size)
407 this->error(_("bad section name offset for section %u: %lu"),
408 i, static_cast<unsigned long>(shdr.get_sh_name()));
409 return;
412 const char* name = pnames + shdr.get_sh_name();
414 this->handle_gnu_warning_section(name, i, symtab);
417 delete sd->section_headers;
418 sd->section_headers = NULL;
419 delete sd->section_names;
420 sd->section_names = NULL;
423 // Add an entry to the vector mapping version numbers to version
424 // strings.
426 template<int size, bool big_endian>
427 void
428 Sized_dynobj<size, big_endian>::set_version_map(
429 Version_map* version_map,
430 unsigned int ndx,
431 const char* name) const
433 if (ndx >= version_map->size())
434 version_map->resize(ndx + 1);
435 if ((*version_map)[ndx] != NULL)
436 this->error(_("duplicate definition for version %u"), ndx);
437 (*version_map)[ndx] = name;
440 // Add mappings for the version definitions to VERSION_MAP.
442 template<int size, bool big_endian>
443 void
444 Sized_dynobj<size, big_endian>::make_verdef_map(
445 Read_symbols_data* sd,
446 Version_map* version_map) const
448 if (sd->verdef == NULL)
449 return;
451 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
452 section_size_type names_size = sd->symbol_names_size;
454 const unsigned char* pverdef = sd->verdef->data();
455 section_size_type verdef_size = sd->verdef_size;
456 const unsigned int count = sd->verdef_info;
458 const unsigned char* p = pverdef;
459 for (unsigned int i = 0; i < count; ++i)
461 elfcpp::Verdef<size, big_endian> verdef(p);
463 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
465 this->error(_("unexpected verdef version %u"),
466 verdef.get_vd_version());
467 return;
470 const section_size_type vd_ndx = verdef.get_vd_ndx();
472 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
473 // sure why.
475 // The first Verdaux holds the name of this version. Subsequent
476 // ones are versions that this one depends upon, which we don't
477 // care about here.
478 const section_size_type vd_cnt = verdef.get_vd_cnt();
479 if (vd_cnt < 1)
481 this->error(_("verdef vd_cnt field too small: %u"),
482 static_cast<unsigned int>(vd_cnt));
483 return;
486 const section_size_type vd_aux = verdef.get_vd_aux();
487 if ((p - pverdef) + vd_aux >= verdef_size)
489 this->error(_("verdef vd_aux field out of range: %u"),
490 static_cast<unsigned int>(vd_aux));
491 return;
494 const unsigned char* pvda = p + vd_aux;
495 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
497 const section_size_type vda_name = verdaux.get_vda_name();
498 if (vda_name >= names_size)
500 this->error(_("verdaux vda_name field out of range: %u"),
501 static_cast<unsigned int>(vda_name));
502 return;
505 this->set_version_map(version_map, vd_ndx, names + vda_name);
507 const section_size_type vd_next = verdef.get_vd_next();
508 if ((p - pverdef) + vd_next >= verdef_size)
510 this->error(_("verdef vd_next field out of range: %u"),
511 static_cast<unsigned int>(vd_next));
512 return;
515 p += vd_next;
519 // Add mappings for the required versions to VERSION_MAP.
521 template<int size, bool big_endian>
522 void
523 Sized_dynobj<size, big_endian>::make_verneed_map(
524 Read_symbols_data* sd,
525 Version_map* version_map) const
527 if (sd->verneed == NULL)
528 return;
530 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
531 section_size_type names_size = sd->symbol_names_size;
533 const unsigned char* pverneed = sd->verneed->data();
534 const section_size_type verneed_size = sd->verneed_size;
535 const unsigned int count = sd->verneed_info;
537 const unsigned char* p = pverneed;
538 for (unsigned int i = 0; i < count; ++i)
540 elfcpp::Verneed<size, big_endian> verneed(p);
542 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
544 this->error(_("unexpected verneed version %u"),
545 verneed.get_vn_version());
546 return;
549 const section_size_type vn_aux = verneed.get_vn_aux();
551 if ((p - pverneed) + vn_aux >= verneed_size)
553 this->error(_("verneed vn_aux field out of range: %u"),
554 static_cast<unsigned int>(vn_aux));
555 return;
558 const unsigned int vn_cnt = verneed.get_vn_cnt();
559 const unsigned char* pvna = p + vn_aux;
560 for (unsigned int j = 0; j < vn_cnt; ++j)
562 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
564 const unsigned int vna_name = vernaux.get_vna_name();
565 if (vna_name >= names_size)
567 this->error(_("vernaux vna_name field out of range: %u"),
568 static_cast<unsigned int>(vna_name));
569 return;
572 this->set_version_map(version_map, vernaux.get_vna_other(),
573 names + vna_name);
575 const section_size_type vna_next = vernaux.get_vna_next();
576 if ((pvna - pverneed) + vna_next >= verneed_size)
578 this->error(_("verneed vna_next field out of range: %u"),
579 static_cast<unsigned int>(vna_next));
580 return;
583 pvna += vna_next;
586 const section_size_type vn_next = verneed.get_vn_next();
587 if ((p - pverneed) + vn_next >= verneed_size)
589 this->error(_("verneed vn_next field out of range: %u"),
590 static_cast<unsigned int>(vn_next));
591 return;
594 p += vn_next;
598 // Create a vector mapping version numbers to version strings.
600 template<int size, bool big_endian>
601 void
602 Sized_dynobj<size, big_endian>::make_version_map(
603 Read_symbols_data* sd,
604 Version_map* version_map) const
606 if (sd->verdef == NULL && sd->verneed == NULL)
607 return;
609 // A guess at the maximum version number we will see. If this is
610 // wrong we will be less efficient but still correct.
611 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
613 this->make_verdef_map(sd, version_map);
614 this->make_verneed_map(sd, version_map);
617 // Add the dynamic symbols to the symbol table.
619 template<int size, bool big_endian>
620 void
621 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
622 Read_symbols_data* sd)
624 if (sd->symbols == NULL)
626 gold_assert(sd->symbol_names == NULL);
627 gold_assert(sd->versym == NULL && sd->verdef == NULL
628 && sd->verneed == NULL);
629 return;
632 const int sym_size = This::sym_size;
633 const size_t symcount = sd->symbols_size / sym_size;
634 gold_assert(sd->external_symbols_offset == 0);
635 if (symcount * sym_size != sd->symbols_size)
637 this->error(_("size of dynamic symbols is not multiple of symbol size"));
638 return;
641 Version_map version_map;
642 this->make_version_map(sd, &version_map);
644 const char* sym_names =
645 reinterpret_cast<const char*>(sd->symbol_names->data());
646 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
647 sym_names, sd->symbol_names_size,
648 (sd->versym == NULL
649 ? NULL
650 : sd->versym->data()),
651 sd->versym_size,
652 &version_map);
654 delete sd->symbols;
655 sd->symbols = NULL;
656 delete sd->symbol_names;
657 sd->symbol_names = NULL;
658 if (sd->versym != NULL)
660 delete sd->versym;
661 sd->versym = NULL;
663 if (sd->verdef != NULL)
665 delete sd->verdef;
666 sd->verdef = NULL;
668 if (sd->verneed != NULL)
670 delete sd->verneed;
671 sd->verneed = NULL;
674 // This is normally the last time we will read any data from this
675 // file.
676 this->clear_view_cache_marks();
679 // Given a vector of hash codes, compute the number of hash buckets to
680 // use.
682 unsigned int
683 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
684 bool for_gnu_hash_table)
686 // FIXME: Implement optional hash table optimization.
688 // Array used to determine the number of hash table buckets to use
689 // based on the number of symbols there are. If there are fewer
690 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
691 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
692 // use more than 262147 buckets. This is straight from the old GNU
693 // linker.
694 static const unsigned int buckets[] =
696 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
697 16411, 32771, 65537, 131101, 262147
699 const int buckets_count = sizeof buckets / sizeof buckets[0];
701 unsigned int symcount = hashcodes.size();
702 unsigned int ret = 1;
703 const double full_fraction
704 = 1.0 - parameters->options().hash_bucket_empty_fraction();
705 for (int i = 0; i < buckets_count; ++i)
707 if (symcount < buckets[i] * full_fraction)
708 break;
709 ret = buckets[i];
712 if (for_gnu_hash_table && ret < 2)
713 ret = 2;
715 return ret;
718 // The standard ELF hash function. This hash function must not
719 // change, as the dynamic linker uses it also.
721 uint32_t
722 Dynobj::elf_hash(const char* name)
724 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
725 uint32_t h = 0;
726 unsigned char c;
727 while ((c = *nameu++) != '\0')
729 h = (h << 4) + c;
730 uint32_t g = h & 0xf0000000;
731 if (g != 0)
733 h ^= g >> 24;
734 // The ELF ABI says h &= ~g, but using xor is equivalent in
735 // this case (since g was set from h) and may save one
736 // instruction.
737 h ^= g;
740 return h;
743 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
744 // DYNSYMS is a vector with all the global dynamic symbols.
745 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
746 // symbol table.
748 void
749 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
750 unsigned int local_dynsym_count,
751 unsigned char** pphash,
752 unsigned int* phashlen)
754 unsigned int dynsym_count = dynsyms.size();
756 // Get the hash values for all the symbols.
757 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
758 for (unsigned int i = 0; i < dynsym_count; ++i)
759 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
761 const unsigned int bucketcount =
762 Dynobj::compute_bucket_count(dynsym_hashvals, false);
764 std::vector<uint32_t> bucket(bucketcount);
765 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
767 for (unsigned int i = 0; i < dynsym_count; ++i)
769 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
770 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
771 chain[dynsym_index] = bucket[bucketpos];
772 bucket[bucketpos] = dynsym_index;
775 unsigned int hashlen = ((2
776 + bucketcount
777 + local_dynsym_count
778 + dynsym_count)
779 * 4);
780 unsigned char* phash = new unsigned char[hashlen];
782 if (parameters->target().is_big_endian())
784 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
785 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
786 hashlen);
787 #else
788 gold_unreachable();
789 #endif
791 else
793 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
794 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
795 hashlen);
796 #else
797 gold_unreachable();
798 #endif
801 *pphash = phash;
802 *phashlen = hashlen;
805 // Fill in an ELF hash table.
807 template<bool big_endian>
808 void
809 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
810 const std::vector<uint32_t>& chain,
811 unsigned char* phash,
812 unsigned int hashlen)
814 unsigned char* p = phash;
816 const unsigned int bucketcount = bucket.size();
817 const unsigned int chaincount = chain.size();
819 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
820 p += 4;
821 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
822 p += 4;
824 for (unsigned int i = 0; i < bucketcount; ++i)
826 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
827 p += 4;
830 for (unsigned int i = 0; i < chaincount; ++i)
832 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
833 p += 4;
836 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
839 // The hash function used for the GNU hash table. This hash function
840 // must not change, as the dynamic linker uses it also.
842 uint32_t
843 Dynobj::gnu_hash(const char* name)
845 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
846 uint32_t h = 5381;
847 unsigned char c;
848 while ((c = *nameu++) != '\0')
849 h = (h << 5) + h + c;
850 return h;
853 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
854 // tables are an extension to ELF which are recognized by the GNU
855 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
856 // TARGET is the target. DYNSYMS is a vector with all the global
857 // symbols which will be going into the dynamic symbol table.
858 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
859 // symbol table.
861 void
862 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
863 unsigned int local_dynsym_count,
864 unsigned char** pphash,
865 unsigned int* phashlen)
867 const unsigned int count = dynsyms.size();
869 // Sort the dynamic symbols into two vectors. Symbols which we do
870 // not want to put into the hash table we store into
871 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
872 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
873 // and records the hash codes.
875 std::vector<Symbol*> unhashed_dynsyms;
876 unhashed_dynsyms.reserve(count);
878 std::vector<Symbol*> hashed_dynsyms;
879 hashed_dynsyms.reserve(count);
881 std::vector<uint32_t> dynsym_hashvals;
882 dynsym_hashvals.reserve(count);
884 for (unsigned int i = 0; i < count; ++i)
886 Symbol* sym = dynsyms[i];
888 // FIXME: Should put on unhashed_dynsyms if the symbol is
889 // hidden.
890 if (sym->is_undefined())
891 unhashed_dynsyms.push_back(sym);
892 else
894 hashed_dynsyms.push_back(sym);
895 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
899 // Put the unhashed symbols at the start of the global portion of
900 // the dynamic symbol table.
901 const unsigned int unhashed_count = unhashed_dynsyms.size();
902 unsigned int unhashed_dynsym_index = local_dynsym_count;
903 for (unsigned int i = 0; i < unhashed_count; ++i)
905 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
906 ++unhashed_dynsym_index;
909 // For the actual data generation we call out to a templatized
910 // function.
911 int size = parameters->target().get_size();
912 bool big_endian = parameters->target().is_big_endian();
913 if (size == 32)
915 if (big_endian)
917 #ifdef HAVE_TARGET_32_BIG
918 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
919 dynsym_hashvals,
920 unhashed_dynsym_index,
921 pphash,
922 phashlen);
923 #else
924 gold_unreachable();
925 #endif
927 else
929 #ifdef HAVE_TARGET_32_LITTLE
930 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
931 dynsym_hashvals,
932 unhashed_dynsym_index,
933 pphash,
934 phashlen);
935 #else
936 gold_unreachable();
937 #endif
940 else if (size == 64)
942 if (big_endian)
944 #ifdef HAVE_TARGET_64_BIG
945 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
946 dynsym_hashvals,
947 unhashed_dynsym_index,
948 pphash,
949 phashlen);
950 #else
951 gold_unreachable();
952 #endif
954 else
956 #ifdef HAVE_TARGET_64_LITTLE
957 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
958 dynsym_hashvals,
959 unhashed_dynsym_index,
960 pphash,
961 phashlen);
962 #else
963 gold_unreachable();
964 #endif
967 else
968 gold_unreachable();
971 // Create the actual data for a GNU hash table. This is just a copy
972 // of the code from the old GNU linker.
974 template<int size, bool big_endian>
975 void
976 Dynobj::sized_create_gnu_hash_table(
977 const std::vector<Symbol*>& hashed_dynsyms,
978 const std::vector<uint32_t>& dynsym_hashvals,
979 unsigned int unhashed_dynsym_count,
980 unsigned char** pphash,
981 unsigned int* phashlen)
983 if (hashed_dynsyms.empty())
985 // Special case for the empty hash table.
986 unsigned int hashlen = 5 * 4 + size / 8;
987 unsigned char* phash = new unsigned char[hashlen];
988 // One empty bucket.
989 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
990 // Symbol index above unhashed symbols.
991 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
992 // One word for bitmask.
993 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
994 // Only bloom filter.
995 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
996 // No valid hashes.
997 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
998 // No hashes in only bucket.
999 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
1001 *phashlen = hashlen;
1002 *pphash = phash;
1004 return;
1007 const unsigned int bucketcount =
1008 Dynobj::compute_bucket_count(dynsym_hashvals, true);
1010 const unsigned int nsyms = hashed_dynsyms.size();
1012 uint32_t maskbitslog2 = 1;
1013 uint32_t x = nsyms >> 1;
1014 while (x != 0)
1016 ++maskbitslog2;
1017 x >>= 1;
1019 if (maskbitslog2 < 3)
1020 maskbitslog2 = 5;
1021 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1022 maskbitslog2 += 3;
1023 else
1024 maskbitslog2 += 2;
1026 uint32_t shift1;
1027 if (size == 32)
1028 shift1 = 5;
1029 else
1031 if (maskbitslog2 == 5)
1032 maskbitslog2 = 6;
1033 shift1 = 6;
1035 uint32_t mask = (1U << shift1) - 1U;
1036 uint32_t shift2 = maskbitslog2;
1037 uint32_t maskbits = 1U << maskbitslog2;
1038 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1040 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1041 std::vector<Word> bitmask(maskwords);
1042 std::vector<uint32_t> counts(bucketcount);
1043 std::vector<uint32_t> indx(bucketcount);
1044 uint32_t symindx = unhashed_dynsym_count;
1046 // Count the number of times each hash bucket is used.
1047 for (unsigned int i = 0; i < nsyms; ++i)
1048 ++counts[dynsym_hashvals[i] % bucketcount];
1050 unsigned int cnt = symindx;
1051 for (unsigned int i = 0; i < bucketcount; ++i)
1053 indx[i] = cnt;
1054 cnt += counts[i];
1057 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1058 hashlen += maskbits / 8;
1059 unsigned char* phash = new unsigned char[hashlen];
1061 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1062 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1063 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1064 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1066 unsigned char* p = phash + 16 + maskbits / 8;
1067 for (unsigned int i = 0; i < bucketcount; ++i)
1069 if (counts[i] == 0)
1070 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1071 else
1072 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1073 p += 4;
1076 for (unsigned int i = 0; i < nsyms; ++i)
1078 Symbol* sym = hashed_dynsyms[i];
1079 uint32_t hashval = dynsym_hashvals[i];
1081 unsigned int bucket = hashval % bucketcount;
1082 unsigned int val = ((hashval >> shift1)
1083 & ((maskbits >> shift1) - 1));
1084 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1085 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1086 val = hashval & ~ 1U;
1087 if (counts[bucket] == 1)
1089 // Last element terminates the chain.
1090 val |= 1;
1092 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1093 val);
1094 --counts[bucket];
1096 sym->set_dynsym_index(indx[bucket]);
1097 ++indx[bucket];
1100 p = phash + 16;
1101 for (unsigned int i = 0; i < maskwords; ++i)
1103 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1104 p += size / 8;
1107 *phashlen = hashlen;
1108 *pphash = phash;
1111 // Verdef methods.
1113 // Write this definition to a buffer for the output section.
1115 template<int size, bool big_endian>
1116 unsigned char*
1117 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const
1119 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1120 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1122 elfcpp::Verdef_write<size, big_endian> vd(pb);
1123 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1124 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1125 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1126 vd.set_vd_ndx(this->index());
1127 vd.set_vd_cnt(1 + this->deps_.size());
1128 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1129 vd.set_vd_aux(verdef_size);
1130 vd.set_vd_next(is_last
1132 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1133 pb += verdef_size;
1135 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1136 vda.set_vda_name(dynpool->get_offset(this->name()));
1137 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1138 pb += verdaux_size;
1140 Deps::const_iterator p;
1141 unsigned int i;
1142 for (p = this->deps_.begin(), i = 0;
1143 p != this->deps_.end();
1144 ++p, ++i)
1146 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1147 vda.set_vda_name(dynpool->get_offset(*p));
1148 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1149 pb += verdaux_size;
1152 return pb;
1155 // Verneed methods.
1157 Verneed::~Verneed()
1159 for (Need_versions::iterator p = this->need_versions_.begin();
1160 p != this->need_versions_.end();
1161 ++p)
1162 delete *p;
1165 // Add a new version to this file reference.
1167 Verneed_version*
1168 Verneed::add_name(const char* name)
1170 Verneed_version* vv = new Verneed_version(name);
1171 this->need_versions_.push_back(vv);
1172 return vv;
1175 // Set the version indexes starting at INDEX.
1177 unsigned int
1178 Verneed::finalize(unsigned int index)
1180 for (Need_versions::iterator p = this->need_versions_.begin();
1181 p != this->need_versions_.end();
1182 ++p)
1184 (*p)->set_index(index);
1185 ++index;
1187 return index;
1190 // Write this list of referenced versions to a buffer for the output
1191 // section.
1193 template<int size, bool big_endian>
1194 unsigned char*
1195 Verneed::write(const Stringpool* dynpool, bool is_last,
1196 unsigned char* pb) const
1198 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1199 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1201 elfcpp::Verneed_write<size, big_endian> vn(pb);
1202 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1203 vn.set_vn_cnt(this->need_versions_.size());
1204 vn.set_vn_file(dynpool->get_offset(this->filename()));
1205 vn.set_vn_aux(verneed_size);
1206 vn.set_vn_next(is_last
1208 : verneed_size + this->need_versions_.size() * vernaux_size);
1209 pb += verneed_size;
1211 Need_versions::const_iterator p;
1212 unsigned int i;
1213 for (p = this->need_versions_.begin(), i = 0;
1214 p != this->need_versions_.end();
1215 ++p, ++i)
1217 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1218 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1219 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1220 vna.set_vna_flags(0);
1221 vna.set_vna_other((*p)->index());
1222 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1223 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1225 : vernaux_size);
1226 pb += vernaux_size;
1229 return pb;
1232 // Versions methods.
1234 Versions::Versions(const Version_script_info& version_script,
1235 Stringpool* dynpool)
1236 : defs_(), needs_(), version_table_(),
1237 is_finalized_(false), version_script_(version_script)
1239 // We always need a base version, so define that first. Nothing
1240 // explicitly declares itself as part of base, so it doesn't need to
1241 // be in version_table_.
1242 // FIXME: Should use soname here when creating a shared object. Is
1243 // this fixme still valid? It looks like it's doing the right thing
1244 // to me.
1245 if (parameters->options().shared())
1247 const char* name = dynpool->add(parameters->options().output_file_name(),
1248 false, NULL);
1249 Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
1250 true, false, true);
1251 this->defs_.push_back(vdbase);
1254 if (!this->version_script_.empty())
1256 // Parse the version script, and insert each declared version into
1257 // defs_ and version_table_.
1258 std::vector<std::string> versions = this->version_script_.get_versions();
1259 for (size_t k = 0; k < versions.size(); ++k)
1261 Stringpool::Key version_key;
1262 const char* version = dynpool->add(versions[k].c_str(),
1263 true, &version_key);
1264 Verdef* const vd = new Verdef(
1265 version,
1266 this->version_script_.get_dependencies(version),
1267 false, false, false);
1268 this->defs_.push_back(vd);
1269 Key key(version_key, 0);
1270 this->version_table_.insert(std::make_pair(key, vd));
1275 Versions::~Versions()
1277 for (Defs::iterator p = this->defs_.begin();
1278 p != this->defs_.end();
1279 ++p)
1280 delete *p;
1282 for (Needs::iterator p = this->needs_.begin();
1283 p != this->needs_.end();
1284 ++p)
1285 delete *p;
1288 // Return the dynamic object which a symbol refers to.
1290 Dynobj*
1291 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1292 const Symbol* sym) const
1294 if (sym->is_copied_from_dynobj())
1295 return symtab->get_copy_source(sym);
1296 else
1298 Object* object = sym->object();
1299 gold_assert(object->is_dynamic());
1300 return static_cast<Dynobj*>(object);
1304 // Record version information for a symbol going into the dynamic
1305 // symbol table.
1307 void
1308 Versions::record_version(const Symbol_table* symtab,
1309 Stringpool* dynpool, const Symbol* sym)
1311 gold_assert(!this->is_finalized_);
1312 gold_assert(sym->version() != NULL);
1314 Stringpool::Key version_key;
1315 const char* version = dynpool->add(sym->version(), false, &version_key);
1317 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1319 if (parameters->options().shared())
1320 this->add_def(sym, version, version_key);
1322 else
1324 // This is a version reference.
1325 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1326 this->add_need(dynpool, dynobj->soname(), version, version_key);
1330 // We've found a symbol SYM defined in version VERSION.
1332 void
1333 Versions::add_def(const Symbol* sym, const char* version,
1334 Stringpool::Key version_key)
1336 Key k(version_key, 0);
1337 Version_base* const vbnull = NULL;
1338 std::pair<Version_table::iterator, bool> ins =
1339 this->version_table_.insert(std::make_pair(k, vbnull));
1341 if (!ins.second)
1343 // We already have an entry for this version.
1344 Version_base* vb = ins.first->second;
1346 // We have now seen a symbol in this version, so it is not
1347 // weak.
1348 gold_assert(vb != NULL);
1349 vb->clear_weak();
1351 else
1353 // If we are creating a shared object, it is an error to
1354 // find a definition of a symbol with a version which is not
1355 // in the version script.
1356 if (parameters->options().shared())
1358 gold_error(_("symbol %s has undefined version %s"),
1359 sym->demangled_name().c_str(), version);
1360 return;
1363 // When creating a regular executable, automatically define
1364 // a new version.
1365 Verdef* vd = new Verdef(version, std::vector<std::string>(),
1366 false, false, false);
1367 this->defs_.push_back(vd);
1368 ins.first->second = vd;
1372 // Add a reference to version NAME in file FILENAME.
1374 void
1375 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1376 Stringpool::Key name_key)
1378 Stringpool::Key filename_key;
1379 filename = dynpool->add(filename, true, &filename_key);
1381 Key k(name_key, filename_key);
1382 Version_base* const vbnull = NULL;
1383 std::pair<Version_table::iterator, bool> ins =
1384 this->version_table_.insert(std::make_pair(k, vbnull));
1386 if (!ins.second)
1388 // We already have an entry for this filename/version.
1389 return;
1392 // See whether we already have this filename. We don't expect many
1393 // version references, so we just do a linear search. This could be
1394 // replaced by a hash table.
1395 Verneed* vn = NULL;
1396 for (Needs::iterator p = this->needs_.begin();
1397 p != this->needs_.end();
1398 ++p)
1400 if ((*p)->filename() == filename)
1402 vn = *p;
1403 break;
1407 if (vn == NULL)
1409 // We have a new filename.
1410 vn = new Verneed(filename);
1411 this->needs_.push_back(vn);
1414 ins.first->second = vn->add_name(name);
1417 // Set the version indexes. Create a new dynamic version symbol for
1418 // each new version definition.
1420 unsigned int
1421 Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
1422 std::vector<Symbol*>* syms)
1424 gold_assert(!this->is_finalized_);
1426 unsigned int vi = 1;
1428 for (Defs::iterator p = this->defs_.begin();
1429 p != this->defs_.end();
1430 ++p)
1432 (*p)->set_index(vi);
1433 ++vi;
1435 // Create a version symbol if necessary.
1436 if (!(*p)->is_symbol_created())
1438 Symbol* vsym = symtab->define_as_constant((*p)->name(),
1439 (*p)->name(), 0, 0,
1440 elfcpp::STT_OBJECT,
1441 elfcpp::STB_GLOBAL,
1442 elfcpp::STV_DEFAULT, 0,
1443 false, false);
1444 vsym->set_needs_dynsym_entry();
1445 vsym->set_dynsym_index(dynsym_index);
1446 ++dynsym_index;
1447 syms->push_back(vsym);
1448 // The name is already in the dynamic pool.
1452 // Index 1 is used for global symbols.
1453 if (vi == 1)
1455 gold_assert(this->defs_.empty());
1456 vi = 2;
1459 for (Needs::iterator p = this->needs_.begin();
1460 p != this->needs_.end();
1461 ++p)
1462 vi = (*p)->finalize(vi);
1464 this->is_finalized_ = true;
1466 return dynsym_index;
1469 // Return the version index to use for a symbol. This does two hash
1470 // table lookups: one in DYNPOOL and one in this->version_table_.
1471 // Another approach alternative would be store a pointer in SYM, which
1472 // would increase the size of the symbol table. Or perhaps we could
1473 // use a hash table from dynamic symbol pointer values to Version_base
1474 // pointers.
1476 unsigned int
1477 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1478 const Symbol* sym) const
1480 Stringpool::Key version_key;
1481 const char* version = dynpool->find(sym->version(), &version_key);
1482 gold_assert(version != NULL);
1484 Key k;
1485 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1487 if (!parameters->options().shared())
1488 return elfcpp::VER_NDX_GLOBAL;
1489 k = Key(version_key, 0);
1491 else
1493 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1495 Stringpool::Key filename_key;
1496 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1497 gold_assert(filename != NULL);
1499 k = Key(version_key, filename_key);
1502 Version_table::const_iterator p = this->version_table_.find(k);
1503 gold_assert(p != this->version_table_.end());
1505 return p->second->index();
1508 // Return an allocated buffer holding the contents of the symbol
1509 // version section.
1511 template<int size, bool big_endian>
1512 void
1513 Versions::symbol_section_contents(const Symbol_table* symtab,
1514 const Stringpool* dynpool,
1515 unsigned int local_symcount,
1516 const std::vector<Symbol*>& syms,
1517 unsigned char** pp,
1518 unsigned int* psize) const
1520 gold_assert(this->is_finalized_);
1522 unsigned int sz = (local_symcount + syms.size()) * 2;
1523 unsigned char* pbuf = new unsigned char[sz];
1525 for (unsigned int i = 0; i < local_symcount; ++i)
1526 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1527 elfcpp::VER_NDX_LOCAL);
1529 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1530 p != syms.end();
1531 ++p)
1533 unsigned int version_index;
1534 const char* version = (*p)->version();
1535 if (version == NULL)
1536 version_index = elfcpp::VER_NDX_GLOBAL;
1537 else
1538 version_index = this->version_index(symtab, dynpool, *p);
1539 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1540 // the hidden bit.
1541 if ((*p)->version() != NULL && !(*p)->is_default())
1542 version_index |= elfcpp::VERSYM_HIDDEN;
1543 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1544 version_index);
1547 *pp = pbuf;
1548 *psize = sz;
1551 // Return an allocated buffer holding the contents of the version
1552 // definition section.
1554 template<int size, bool big_endian>
1555 void
1556 Versions::def_section_contents(const Stringpool* dynpool,
1557 unsigned char** pp, unsigned int* psize,
1558 unsigned int* pentries) const
1560 gold_assert(this->is_finalized_);
1561 gold_assert(!this->defs_.empty());
1563 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1564 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1566 unsigned int sz = 0;
1567 for (Defs::const_iterator p = this->defs_.begin();
1568 p != this->defs_.end();
1569 ++p)
1571 sz += verdef_size + verdaux_size;
1572 sz += (*p)->count_dependencies() * verdaux_size;
1575 unsigned char* pbuf = new unsigned char[sz];
1577 unsigned char* pb = pbuf;
1578 Defs::const_iterator p;
1579 unsigned int i;
1580 for (p = this->defs_.begin(), i = 0;
1581 p != this->defs_.end();
1582 ++p, ++i)
1583 pb = (*p)->write<size, big_endian>(dynpool,
1584 i + 1 >= this->defs_.size(),
1585 pb);
1587 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1589 *pp = pbuf;
1590 *psize = sz;
1591 *pentries = this->defs_.size();
1594 // Return an allocated buffer holding the contents of the version
1595 // reference section.
1597 template<int size, bool big_endian>
1598 void
1599 Versions::need_section_contents(const Stringpool* dynpool,
1600 unsigned char** pp, unsigned int *psize,
1601 unsigned int *pentries) const
1603 gold_assert(this->is_finalized_);
1604 gold_assert(!this->needs_.empty());
1606 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1607 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1609 unsigned int sz = 0;
1610 for (Needs::const_iterator p = this->needs_.begin();
1611 p != this->needs_.end();
1612 ++p)
1614 sz += verneed_size;
1615 sz += (*p)->count_versions() * vernaux_size;
1618 unsigned char* pbuf = new unsigned char[sz];
1620 unsigned char* pb = pbuf;
1621 Needs::const_iterator p;
1622 unsigned int i;
1623 for (p = this->needs_.begin(), i = 0;
1624 p != this->needs_.end();
1625 ++p, ++i)
1626 pb = (*p)->write<size, big_endian>(dynpool,
1627 i + 1 >= this->needs_.size(),
1628 pb);
1630 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1632 *pp = pbuf;
1633 *psize = sz;
1634 *pentries = this->needs_.size();
1637 // Instantiate the templates we need. We could use the configure
1638 // script to restrict this to only the ones for implemented targets.
1640 #ifdef HAVE_TARGET_32_LITTLE
1641 template
1642 class Sized_dynobj<32, false>;
1643 #endif
1645 #ifdef HAVE_TARGET_32_BIG
1646 template
1647 class Sized_dynobj<32, true>;
1648 #endif
1650 #ifdef HAVE_TARGET_64_LITTLE
1651 template
1652 class Sized_dynobj<64, false>;
1653 #endif
1655 #ifdef HAVE_TARGET_64_BIG
1656 template
1657 class Sized_dynobj<64, true>;
1658 #endif
1660 #ifdef HAVE_TARGET_32_LITTLE
1661 template
1662 void
1663 Versions::symbol_section_contents<32, false>(
1664 const Symbol_table*,
1665 const Stringpool*,
1666 unsigned int,
1667 const std::vector<Symbol*>&,
1668 unsigned char**,
1669 unsigned int*) const;
1670 #endif
1672 #ifdef HAVE_TARGET_32_BIG
1673 template
1674 void
1675 Versions::symbol_section_contents<32, true>(
1676 const Symbol_table*,
1677 const Stringpool*,
1678 unsigned int,
1679 const std::vector<Symbol*>&,
1680 unsigned char**,
1681 unsigned int*) const;
1682 #endif
1684 #ifdef HAVE_TARGET_64_LITTLE
1685 template
1686 void
1687 Versions::symbol_section_contents<64, false>(
1688 const Symbol_table*,
1689 const Stringpool*,
1690 unsigned int,
1691 const std::vector<Symbol*>&,
1692 unsigned char**,
1693 unsigned int*) const;
1694 #endif
1696 #ifdef HAVE_TARGET_64_BIG
1697 template
1698 void
1699 Versions::symbol_section_contents<64, true>(
1700 const Symbol_table*,
1701 const Stringpool*,
1702 unsigned int,
1703 const std::vector<Symbol*>&,
1704 unsigned char**,
1705 unsigned int*) const;
1706 #endif
1708 #ifdef HAVE_TARGET_32_LITTLE
1709 template
1710 void
1711 Versions::def_section_contents<32, false>(
1712 const Stringpool*,
1713 unsigned char**,
1714 unsigned int*,
1715 unsigned int*) const;
1716 #endif
1718 #ifdef HAVE_TARGET_32_BIG
1719 template
1720 void
1721 Versions::def_section_contents<32, true>(
1722 const Stringpool*,
1723 unsigned char**,
1724 unsigned int*,
1725 unsigned int*) const;
1726 #endif
1728 #ifdef HAVE_TARGET_64_LITTLE
1729 template
1730 void
1731 Versions::def_section_contents<64, false>(
1732 const Stringpool*,
1733 unsigned char**,
1734 unsigned int*,
1735 unsigned int*) const;
1736 #endif
1738 #ifdef HAVE_TARGET_64_BIG
1739 template
1740 void
1741 Versions::def_section_contents<64, true>(
1742 const Stringpool*,
1743 unsigned char**,
1744 unsigned int*,
1745 unsigned int*) const;
1746 #endif
1748 #ifdef HAVE_TARGET_32_LITTLE
1749 template
1750 void
1751 Versions::need_section_contents<32, false>(
1752 const Stringpool*,
1753 unsigned char**,
1754 unsigned int*,
1755 unsigned int*) const;
1756 #endif
1758 #ifdef HAVE_TARGET_32_BIG
1759 template
1760 void
1761 Versions::need_section_contents<32, true>(
1762 const Stringpool*,
1763 unsigned char**,
1764 unsigned int*,
1765 unsigned int*) const;
1766 #endif
1768 #ifdef HAVE_TARGET_64_LITTLE
1769 template
1770 void
1771 Versions::need_section_contents<64, false>(
1772 const Stringpool*,
1773 unsigned char**,
1774 unsigned int*,
1775 unsigned int*) const;
1776 #endif
1778 #ifdef HAVE_TARGET_64_BIG
1779 template
1780 void
1781 Versions::need_section_contents<64, true>(
1782 const Stringpool*,
1783 unsigned char**,
1784 unsigned int*,
1785 unsigned int*) const;
1786 #endif
1788 } // End namespace gold.