bfd/
[binutils.git] / gold / dynobj.cc
blob8761eaa5832c86567725226110e5b347359fa120
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),
76 dynsym_shndx_(-1U),
77 symbols_(NULL),
78 defined_count_(0)
82 // Set up the object.
84 template<int size, bool big_endian>
85 void
86 Sized_dynobj<size, big_endian>::setup(
87 const elfcpp::Ehdr<size, big_endian>& ehdr)
89 this->set_target(ehdr.get_e_machine(), size, big_endian,
90 ehdr.get_e_ident()[elfcpp::EI_OSABI],
91 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
93 const unsigned int shnum = this->elf_file_.shnum();
94 this->set_shnum(shnum);
97 // Find the SHT_DYNSYM section and the various version sections, and
98 // the dynamic section, given the section headers.
100 template<int size, bool big_endian>
101 void
102 Sized_dynobj<size, big_endian>::find_dynsym_sections(
103 const unsigned char* pshdrs,
104 unsigned int* pversym_shndx,
105 unsigned int* pverdef_shndx,
106 unsigned int* pverneed_shndx,
107 unsigned int* pdynamic_shndx)
109 *pversym_shndx = -1U;
110 *pverdef_shndx = -1U;
111 *pverneed_shndx = -1U;
112 *pdynamic_shndx = -1U;
114 unsigned int xindex_shndx = 0;
115 unsigned int xindex_link = 0;
116 const unsigned int shnum = this->shnum();
117 const unsigned char* p = pshdrs;
118 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
120 typename This::Shdr shdr(p);
122 unsigned int* pi;
123 switch (shdr.get_sh_type())
125 case elfcpp::SHT_DYNSYM:
126 this->dynsym_shndx_ = i;
127 if (xindex_shndx > 0 && xindex_link == i)
129 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
130 xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx,
131 pshdrs);
132 this->set_xindex(xindex);
134 pi = NULL;
135 break;
136 case elfcpp::SHT_GNU_versym:
137 pi = pversym_shndx;
138 break;
139 case elfcpp::SHT_GNU_verdef:
140 pi = pverdef_shndx;
141 break;
142 case elfcpp::SHT_GNU_verneed:
143 pi = pverneed_shndx;
144 break;
145 case elfcpp::SHT_DYNAMIC:
146 pi = pdynamic_shndx;
147 break;
148 case elfcpp::SHT_SYMTAB_SHNDX:
149 xindex_shndx = i;
150 xindex_link = this->adjust_shndx(shdr.get_sh_link());
151 if (xindex_link == this->dynsym_shndx_)
153 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
154 xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx,
155 pshdrs);
156 this->set_xindex(xindex);
158 pi = NULL;
159 break;
160 default:
161 pi = NULL;
162 break;
165 if (pi == NULL)
166 continue;
168 if (*pi != -1U)
169 this->error(_("unexpected duplicate type %u section: %u, %u"),
170 shdr.get_sh_type(), *pi, i);
172 *pi = i;
176 // Read the contents of section SHNDX. PSHDRS points to the section
177 // headers. TYPE is the expected section type. LINK is the expected
178 // section link. Store the data in *VIEW and *VIEW_SIZE. The
179 // section's sh_info field is stored in *VIEW_INFO.
181 template<int size, bool big_endian>
182 void
183 Sized_dynobj<size, big_endian>::read_dynsym_section(
184 const unsigned char* pshdrs,
185 unsigned int shndx,
186 elfcpp::SHT type,
187 unsigned int link,
188 File_view** view,
189 section_size_type* view_size,
190 unsigned int* view_info)
192 if (shndx == -1U)
194 *view = NULL;
195 *view_size = 0;
196 *view_info = 0;
197 return;
200 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
202 gold_assert(shdr.get_sh_type() == type);
204 if (this->adjust_shndx(shdr.get_sh_link()) != link)
205 this->error(_("unexpected link in section %u header: %u != %u"),
206 shndx, this->adjust_shndx(shdr.get_sh_link()), link);
208 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
209 true, false);
210 *view_size = convert_to_section_size_type(shdr.get_sh_size());
211 *view_info = shdr.get_sh_info();
214 // Read the dynamic tags. Set the soname field if this shared object
215 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
216 // the section headers. DYNAMIC_SHNDX is the section index of the
217 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
218 // section index and contents of a string table which may be the one
219 // associated with the SHT_DYNAMIC section.
221 template<int size, bool big_endian>
222 void
223 Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
224 unsigned int dynamic_shndx,
225 unsigned int strtab_shndx,
226 const unsigned char* strtabu,
227 off_t strtab_size)
229 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
230 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
232 const off_t dynamic_size = dynamicshdr.get_sh_size();
233 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
234 dynamic_size, true, false);
236 const unsigned int link = this->adjust_shndx(dynamicshdr.get_sh_link());
237 if (link != strtab_shndx)
239 if (link >= this->shnum())
241 this->error(_("DYNAMIC section %u link out of range: %u"),
242 dynamic_shndx, link);
243 return;
246 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
247 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
249 this->error(_("DYNAMIC section %u link %u is not a strtab"),
250 dynamic_shndx, link);
251 return;
254 strtab_size = strtabshdr.get_sh_size();
255 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false,
256 false);
259 const char* const strtab = reinterpret_cast<const char*>(strtabu);
261 for (const unsigned char* p = pdynamic;
262 p < pdynamic + dynamic_size;
263 p += This::dyn_size)
265 typename This::Dyn dyn(p);
267 switch (dyn.get_d_tag())
269 case elfcpp::DT_NULL:
270 // We should always see DT_NULL at the end of the dynamic
271 // tags.
272 return;
274 case elfcpp::DT_SONAME:
276 off_t val = dyn.get_d_val();
277 if (val >= strtab_size)
278 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
279 static_cast<long long>(val),
280 static_cast<long long>(strtab_size));
281 else
282 this->set_soname_string(strtab + val);
284 break;
286 case elfcpp::DT_NEEDED:
288 off_t val = dyn.get_d_val();
289 if (val >= strtab_size)
290 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
291 static_cast<long long>(val),
292 static_cast<long long>(strtab_size));
293 else
294 this->add_needed(strtab + val);
296 break;
298 default:
299 break;
303 this->error(_("missing DT_NULL in dynamic segment"));
306 // Read the symbols and sections from a dynamic object. We read the
307 // dynamic symbols, not the normal symbols.
309 template<int size, bool big_endian>
310 void
311 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
313 this->read_section_data(&this->elf_file_, sd);
315 const unsigned char* const pshdrs = sd->section_headers->data();
317 unsigned int versym_shndx;
318 unsigned int verdef_shndx;
319 unsigned int verneed_shndx;
320 unsigned int dynamic_shndx;
321 this->find_dynsym_sections(pshdrs, &versym_shndx, &verdef_shndx,
322 &verneed_shndx, &dynamic_shndx);
324 unsigned int strtab_shndx = -1U;
326 sd->symbols = NULL;
327 sd->symbols_size = 0;
328 sd->external_symbols_offset = 0;
329 sd->symbol_names = NULL;
330 sd->symbol_names_size = 0;
332 if (this->dynsym_shndx_ != -1U)
334 // Get the dynamic symbols.
335 typename This::Shdr dynsymshdr(pshdrs
336 + this->dynsym_shndx_ * This::shdr_size);
337 gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
339 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
340 dynsymshdr.get_sh_size(), true,
341 false);
342 sd->symbols_size =
343 convert_to_section_size_type(dynsymshdr.get_sh_size());
345 // Get the symbol names.
346 strtab_shndx = this->adjust_shndx(dynsymshdr.get_sh_link());
347 if (strtab_shndx >= this->shnum())
349 this->error(_("invalid dynamic symbol table name index: %u"),
350 strtab_shndx);
351 return;
353 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
354 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
356 this->error(_("dynamic symbol table name section "
357 "has wrong type: %u"),
358 static_cast<unsigned int>(strtabshdr.get_sh_type()));
359 return;
362 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
363 strtabshdr.get_sh_size(),
364 false, false);
365 sd->symbol_names_size =
366 convert_to_section_size_type(strtabshdr.get_sh_size());
368 // Get the version information.
370 unsigned int dummy;
371 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
372 this->dynsym_shndx_,
373 &sd->versym, &sd->versym_size, &dummy);
375 // We require that the version definition and need section link
376 // to the same string table as the dynamic symbol table. This
377 // is not a technical requirement, but it always happens in
378 // practice. We could change this if necessary.
380 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
381 strtab_shndx, &sd->verdef, &sd->verdef_size,
382 &sd->verdef_info);
384 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
385 strtab_shndx, &sd->verneed, &sd->verneed_size,
386 &sd->verneed_info);
389 // Read the SHT_DYNAMIC section to find whether this shared object
390 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
391 // doesn't really have anything to do with reading the symbols, but
392 // this is a convenient place to do it.
393 if (dynamic_shndx != -1U)
394 this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
395 (sd->symbol_names == NULL
396 ? NULL
397 : sd->symbol_names->data()),
398 sd->symbol_names_size);
401 // Return the Xindex structure to use for object with lots of
402 // sections.
404 template<int size, bool big_endian>
405 Xindex*
406 Sized_dynobj<size, big_endian>::do_initialize_xindex()
408 gold_assert(this->dynsym_shndx_ != -1U);
409 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
410 xindex->initialize_symtab_xindex<size, big_endian>(this, this->dynsym_shndx_);
411 return xindex;
414 // Lay out the input sections for a dynamic object. We don't want to
415 // include sections from a dynamic object, so all that we actually do
416 // here is check for .gnu.warning sections.
418 template<int size, bool big_endian>
419 void
420 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
421 Layout*,
422 Read_symbols_data* sd)
424 const unsigned int shnum = this->shnum();
425 if (shnum == 0)
426 return;
428 // Get the section headers.
429 const unsigned char* pshdrs = sd->section_headers->data();
431 // Get the section names.
432 const unsigned char* pnamesu = sd->section_names->data();
433 const char* pnames = reinterpret_cast<const char*>(pnamesu);
435 // Skip the first, dummy, section.
436 pshdrs += This::shdr_size;
437 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
439 typename This::Shdr shdr(pshdrs);
441 if (shdr.get_sh_name() >= sd->section_names_size)
443 this->error(_("bad section name offset for section %u: %lu"),
444 i, static_cast<unsigned long>(shdr.get_sh_name()));
445 return;
448 const char* name = pnames + shdr.get_sh_name();
450 this->handle_gnu_warning_section(name, i, symtab);
453 delete sd->section_headers;
454 sd->section_headers = NULL;
455 delete sd->section_names;
456 sd->section_names = NULL;
459 // Add an entry to the vector mapping version numbers to version
460 // strings.
462 template<int size, bool big_endian>
463 void
464 Sized_dynobj<size, big_endian>::set_version_map(
465 Version_map* version_map,
466 unsigned int ndx,
467 const char* name) const
469 if (ndx >= version_map->size())
470 version_map->resize(ndx + 1);
471 if ((*version_map)[ndx] != NULL)
472 this->error(_("duplicate definition for version %u"), ndx);
473 (*version_map)[ndx] = name;
476 // Add mappings for the version definitions to VERSION_MAP.
478 template<int size, bool big_endian>
479 void
480 Sized_dynobj<size, big_endian>::make_verdef_map(
481 Read_symbols_data* sd,
482 Version_map* version_map) const
484 if (sd->verdef == NULL)
485 return;
487 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
488 section_size_type names_size = sd->symbol_names_size;
490 const unsigned char* pverdef = sd->verdef->data();
491 section_size_type verdef_size = sd->verdef_size;
492 const unsigned int count = sd->verdef_info;
494 const unsigned char* p = pverdef;
495 for (unsigned int i = 0; i < count; ++i)
497 elfcpp::Verdef<size, big_endian> verdef(p);
499 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
501 this->error(_("unexpected verdef version %u"),
502 verdef.get_vd_version());
503 return;
506 const section_size_type vd_ndx = verdef.get_vd_ndx();
508 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
509 // sure why.
511 // The first Verdaux holds the name of this version. Subsequent
512 // ones are versions that this one depends upon, which we don't
513 // care about here.
514 const section_size_type vd_cnt = verdef.get_vd_cnt();
515 if (vd_cnt < 1)
517 this->error(_("verdef vd_cnt field too small: %u"),
518 static_cast<unsigned int>(vd_cnt));
519 return;
522 const section_size_type vd_aux = verdef.get_vd_aux();
523 if ((p - pverdef) + vd_aux >= verdef_size)
525 this->error(_("verdef vd_aux field out of range: %u"),
526 static_cast<unsigned int>(vd_aux));
527 return;
530 const unsigned char* pvda = p + vd_aux;
531 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
533 const section_size_type vda_name = verdaux.get_vda_name();
534 if (vda_name >= names_size)
536 this->error(_("verdaux vda_name field out of range: %u"),
537 static_cast<unsigned int>(vda_name));
538 return;
541 this->set_version_map(version_map, vd_ndx, names + vda_name);
543 const section_size_type vd_next = verdef.get_vd_next();
544 if ((p - pverdef) + vd_next >= verdef_size)
546 this->error(_("verdef vd_next field out of range: %u"),
547 static_cast<unsigned int>(vd_next));
548 return;
551 p += vd_next;
555 // Add mappings for the required versions to VERSION_MAP.
557 template<int size, bool big_endian>
558 void
559 Sized_dynobj<size, big_endian>::make_verneed_map(
560 Read_symbols_data* sd,
561 Version_map* version_map) const
563 if (sd->verneed == NULL)
564 return;
566 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
567 section_size_type names_size = sd->symbol_names_size;
569 const unsigned char* pverneed = sd->verneed->data();
570 const section_size_type verneed_size = sd->verneed_size;
571 const unsigned int count = sd->verneed_info;
573 const unsigned char* p = pverneed;
574 for (unsigned int i = 0; i < count; ++i)
576 elfcpp::Verneed<size, big_endian> verneed(p);
578 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
580 this->error(_("unexpected verneed version %u"),
581 verneed.get_vn_version());
582 return;
585 const section_size_type vn_aux = verneed.get_vn_aux();
587 if ((p - pverneed) + vn_aux >= verneed_size)
589 this->error(_("verneed vn_aux field out of range: %u"),
590 static_cast<unsigned int>(vn_aux));
591 return;
594 const unsigned int vn_cnt = verneed.get_vn_cnt();
595 const unsigned char* pvna = p + vn_aux;
596 for (unsigned int j = 0; j < vn_cnt; ++j)
598 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
600 const unsigned int vna_name = vernaux.get_vna_name();
601 if (vna_name >= names_size)
603 this->error(_("vernaux vna_name field out of range: %u"),
604 static_cast<unsigned int>(vna_name));
605 return;
608 this->set_version_map(version_map, vernaux.get_vna_other(),
609 names + vna_name);
611 const section_size_type vna_next = vernaux.get_vna_next();
612 if ((pvna - pverneed) + vna_next >= verneed_size)
614 this->error(_("verneed vna_next field out of range: %u"),
615 static_cast<unsigned int>(vna_next));
616 return;
619 pvna += vna_next;
622 const section_size_type vn_next = verneed.get_vn_next();
623 if ((p - pverneed) + vn_next >= verneed_size)
625 this->error(_("verneed vn_next field out of range: %u"),
626 static_cast<unsigned int>(vn_next));
627 return;
630 p += vn_next;
634 // Create a vector mapping version numbers to version strings.
636 template<int size, bool big_endian>
637 void
638 Sized_dynobj<size, big_endian>::make_version_map(
639 Read_symbols_data* sd,
640 Version_map* version_map) const
642 if (sd->verdef == NULL && sd->verneed == NULL)
643 return;
645 // A guess at the maximum version number we will see. If this is
646 // wrong we will be less efficient but still correct.
647 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
649 this->make_verdef_map(sd, version_map);
650 this->make_verneed_map(sd, version_map);
653 // Add the dynamic symbols to the symbol table.
655 template<int size, bool big_endian>
656 void
657 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
658 Read_symbols_data* sd,
659 Layout*)
661 if (sd->symbols == NULL)
663 gold_assert(sd->symbol_names == NULL);
664 gold_assert(sd->versym == NULL && sd->verdef == NULL
665 && sd->verneed == NULL);
666 return;
669 const int sym_size = This::sym_size;
670 const size_t symcount = sd->symbols_size / sym_size;
671 gold_assert(sd->external_symbols_offset == 0);
672 if (symcount * sym_size != sd->symbols_size)
674 this->error(_("size of dynamic symbols is not multiple of symbol size"));
675 return;
678 Version_map version_map;
679 this->make_version_map(sd, &version_map);
681 // If printing symbol counts, we want to track symbols.
683 if (parameters->options().user_set_print_symbol_counts())
685 this->symbols_ = new Symbols();
686 this->symbols_->resize(symcount);
689 const char* sym_names =
690 reinterpret_cast<const char*>(sd->symbol_names->data());
691 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
692 sym_names, sd->symbol_names_size,
693 (sd->versym == NULL
694 ? NULL
695 : sd->versym->data()),
696 sd->versym_size,
697 &version_map,
698 this->symbols_,
699 &this->defined_count_);
701 delete sd->symbols;
702 sd->symbols = NULL;
703 delete sd->symbol_names;
704 sd->symbol_names = NULL;
705 if (sd->versym != NULL)
707 delete sd->versym;
708 sd->versym = NULL;
710 if (sd->verdef != NULL)
712 delete sd->verdef;
713 sd->verdef = NULL;
715 if (sd->verneed != NULL)
717 delete sd->verneed;
718 sd->verneed = NULL;
721 // This is normally the last time we will read any data from this
722 // file.
723 this->clear_view_cache_marks();
726 // Get symbol counts.
728 template<int size, bool big_endian>
729 void
730 Sized_dynobj<size, big_endian>::do_get_global_symbol_counts(
731 const Symbol_table*,
732 size_t* defined,
733 size_t* used) const
735 *defined = this->defined_count_;
736 size_t count = 0;
737 for (typename Symbols::const_iterator p = this->symbols_->begin();
738 p != this->symbols_->end();
739 ++p)
740 if (*p != NULL
741 && (*p)->source() == Symbol::FROM_OBJECT
742 && (*p)->object() == this
743 && (*p)->is_defined()
744 && (*p)->dynsym_index() != -1U)
745 ++count;
746 *used = count;
749 // Given a vector of hash codes, compute the number of hash buckets to
750 // use.
752 unsigned int
753 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
754 bool for_gnu_hash_table)
756 // FIXME: Implement optional hash table optimization.
758 // Array used to determine the number of hash table buckets to use
759 // based on the number of symbols there are. If there are fewer
760 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
761 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
762 // use more than 262147 buckets. This is straight from the old GNU
763 // linker.
764 static const unsigned int buckets[] =
766 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
767 16411, 32771, 65537, 131101, 262147
769 const int buckets_count = sizeof buckets / sizeof buckets[0];
771 unsigned int symcount = hashcodes.size();
772 unsigned int ret = 1;
773 const double full_fraction
774 = 1.0 - parameters->options().hash_bucket_empty_fraction();
775 for (int i = 0; i < buckets_count; ++i)
777 if (symcount < buckets[i] * full_fraction)
778 break;
779 ret = buckets[i];
782 if (for_gnu_hash_table && ret < 2)
783 ret = 2;
785 return ret;
788 // The standard ELF hash function. This hash function must not
789 // change, as the dynamic linker uses it also.
791 uint32_t
792 Dynobj::elf_hash(const char* name)
794 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
795 uint32_t h = 0;
796 unsigned char c;
797 while ((c = *nameu++) != '\0')
799 h = (h << 4) + c;
800 uint32_t g = h & 0xf0000000;
801 if (g != 0)
803 h ^= g >> 24;
804 // The ELF ABI says h &= ~g, but using xor is equivalent in
805 // this case (since g was set from h) and may save one
806 // instruction.
807 h ^= g;
810 return h;
813 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
814 // DYNSYMS is a vector with all the global dynamic symbols.
815 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
816 // symbol table.
818 void
819 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
820 unsigned int local_dynsym_count,
821 unsigned char** pphash,
822 unsigned int* phashlen)
824 unsigned int dynsym_count = dynsyms.size();
826 // Get the hash values for all the symbols.
827 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
828 for (unsigned int i = 0; i < dynsym_count; ++i)
829 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
831 const unsigned int bucketcount =
832 Dynobj::compute_bucket_count(dynsym_hashvals, false);
834 std::vector<uint32_t> bucket(bucketcount);
835 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
837 for (unsigned int i = 0; i < dynsym_count; ++i)
839 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
840 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
841 chain[dynsym_index] = bucket[bucketpos];
842 bucket[bucketpos] = dynsym_index;
845 unsigned int hashlen = ((2
846 + bucketcount
847 + local_dynsym_count
848 + dynsym_count)
849 * 4);
850 unsigned char* phash = new unsigned char[hashlen];
852 if (parameters->target().is_big_endian())
854 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
855 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
856 hashlen);
857 #else
858 gold_unreachable();
859 #endif
861 else
863 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
864 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
865 hashlen);
866 #else
867 gold_unreachable();
868 #endif
871 *pphash = phash;
872 *phashlen = hashlen;
875 // Fill in an ELF hash table.
877 template<bool big_endian>
878 void
879 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
880 const std::vector<uint32_t>& chain,
881 unsigned char* phash,
882 unsigned int hashlen)
884 unsigned char* p = phash;
886 const unsigned int bucketcount = bucket.size();
887 const unsigned int chaincount = chain.size();
889 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
890 p += 4;
891 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
892 p += 4;
894 for (unsigned int i = 0; i < bucketcount; ++i)
896 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
897 p += 4;
900 for (unsigned int i = 0; i < chaincount; ++i)
902 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
903 p += 4;
906 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
909 // The hash function used for the GNU hash table. This hash function
910 // must not change, as the dynamic linker uses it also.
912 uint32_t
913 Dynobj::gnu_hash(const char* name)
915 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
916 uint32_t h = 5381;
917 unsigned char c;
918 while ((c = *nameu++) != '\0')
919 h = (h << 5) + h + c;
920 return h;
923 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
924 // tables are an extension to ELF which are recognized by the GNU
925 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
926 // TARGET is the target. DYNSYMS is a vector with all the global
927 // symbols which will be going into the dynamic symbol table.
928 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
929 // symbol table.
931 void
932 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
933 unsigned int local_dynsym_count,
934 unsigned char** pphash,
935 unsigned int* phashlen)
937 const unsigned int count = dynsyms.size();
939 // Sort the dynamic symbols into two vectors. Symbols which we do
940 // not want to put into the hash table we store into
941 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
942 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
943 // and records the hash codes.
945 std::vector<Symbol*> unhashed_dynsyms;
946 unhashed_dynsyms.reserve(count);
948 std::vector<Symbol*> hashed_dynsyms;
949 hashed_dynsyms.reserve(count);
951 std::vector<uint32_t> dynsym_hashvals;
952 dynsym_hashvals.reserve(count);
954 for (unsigned int i = 0; i < count; ++i)
956 Symbol* sym = dynsyms[i];
958 // FIXME: Should put on unhashed_dynsyms if the symbol is
959 // hidden.
960 if (sym->is_undefined())
961 unhashed_dynsyms.push_back(sym);
962 else
964 hashed_dynsyms.push_back(sym);
965 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
969 // Put the unhashed symbols at the start of the global portion of
970 // the dynamic symbol table.
971 const unsigned int unhashed_count = unhashed_dynsyms.size();
972 unsigned int unhashed_dynsym_index = local_dynsym_count;
973 for (unsigned int i = 0; i < unhashed_count; ++i)
975 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
976 ++unhashed_dynsym_index;
979 // For the actual data generation we call out to a templatized
980 // function.
981 int size = parameters->target().get_size();
982 bool big_endian = parameters->target().is_big_endian();
983 if (size == 32)
985 if (big_endian)
987 #ifdef HAVE_TARGET_32_BIG
988 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
989 dynsym_hashvals,
990 unhashed_dynsym_index,
991 pphash,
992 phashlen);
993 #else
994 gold_unreachable();
995 #endif
997 else
999 #ifdef HAVE_TARGET_32_LITTLE
1000 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
1001 dynsym_hashvals,
1002 unhashed_dynsym_index,
1003 pphash,
1004 phashlen);
1005 #else
1006 gold_unreachable();
1007 #endif
1010 else if (size == 64)
1012 if (big_endian)
1014 #ifdef HAVE_TARGET_64_BIG
1015 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
1016 dynsym_hashvals,
1017 unhashed_dynsym_index,
1018 pphash,
1019 phashlen);
1020 #else
1021 gold_unreachable();
1022 #endif
1024 else
1026 #ifdef HAVE_TARGET_64_LITTLE
1027 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
1028 dynsym_hashvals,
1029 unhashed_dynsym_index,
1030 pphash,
1031 phashlen);
1032 #else
1033 gold_unreachable();
1034 #endif
1037 else
1038 gold_unreachable();
1041 // Create the actual data for a GNU hash table. This is just a copy
1042 // of the code from the old GNU linker.
1044 template<int size, bool big_endian>
1045 void
1046 Dynobj::sized_create_gnu_hash_table(
1047 const std::vector<Symbol*>& hashed_dynsyms,
1048 const std::vector<uint32_t>& dynsym_hashvals,
1049 unsigned int unhashed_dynsym_count,
1050 unsigned char** pphash,
1051 unsigned int* phashlen)
1053 if (hashed_dynsyms.empty())
1055 // Special case for the empty hash table.
1056 unsigned int hashlen = 5 * 4 + size / 8;
1057 unsigned char* phash = new unsigned char[hashlen];
1058 // One empty bucket.
1059 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
1060 // Symbol index above unhashed symbols.
1061 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
1062 // One word for bitmask.
1063 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
1064 // Only bloom filter.
1065 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
1066 // No valid hashes.
1067 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
1068 // No hashes in only bucket.
1069 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
1071 *phashlen = hashlen;
1072 *pphash = phash;
1074 return;
1077 const unsigned int bucketcount =
1078 Dynobj::compute_bucket_count(dynsym_hashvals, true);
1080 const unsigned int nsyms = hashed_dynsyms.size();
1082 uint32_t maskbitslog2 = 1;
1083 uint32_t x = nsyms >> 1;
1084 while (x != 0)
1086 ++maskbitslog2;
1087 x >>= 1;
1089 if (maskbitslog2 < 3)
1090 maskbitslog2 = 5;
1091 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1092 maskbitslog2 += 3;
1093 else
1094 maskbitslog2 += 2;
1096 uint32_t shift1;
1097 if (size == 32)
1098 shift1 = 5;
1099 else
1101 if (maskbitslog2 == 5)
1102 maskbitslog2 = 6;
1103 shift1 = 6;
1105 uint32_t mask = (1U << shift1) - 1U;
1106 uint32_t shift2 = maskbitslog2;
1107 uint32_t maskbits = 1U << maskbitslog2;
1108 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1110 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1111 std::vector<Word> bitmask(maskwords);
1112 std::vector<uint32_t> counts(bucketcount);
1113 std::vector<uint32_t> indx(bucketcount);
1114 uint32_t symindx = unhashed_dynsym_count;
1116 // Count the number of times each hash bucket is used.
1117 for (unsigned int i = 0; i < nsyms; ++i)
1118 ++counts[dynsym_hashvals[i] % bucketcount];
1120 unsigned int cnt = symindx;
1121 for (unsigned int i = 0; i < bucketcount; ++i)
1123 indx[i] = cnt;
1124 cnt += counts[i];
1127 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1128 hashlen += maskbits / 8;
1129 unsigned char* phash = new unsigned char[hashlen];
1131 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1132 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1133 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1134 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1136 unsigned char* p = phash + 16 + maskbits / 8;
1137 for (unsigned int i = 0; i < bucketcount; ++i)
1139 if (counts[i] == 0)
1140 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1141 else
1142 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1143 p += 4;
1146 for (unsigned int i = 0; i < nsyms; ++i)
1148 Symbol* sym = hashed_dynsyms[i];
1149 uint32_t hashval = dynsym_hashvals[i];
1151 unsigned int bucket = hashval % bucketcount;
1152 unsigned int val = ((hashval >> shift1)
1153 & ((maskbits >> shift1) - 1));
1154 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1155 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1156 val = hashval & ~ 1U;
1157 if (counts[bucket] == 1)
1159 // Last element terminates the chain.
1160 val |= 1;
1162 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1163 val);
1164 --counts[bucket];
1166 sym->set_dynsym_index(indx[bucket]);
1167 ++indx[bucket];
1170 p = phash + 16;
1171 for (unsigned int i = 0; i < maskwords; ++i)
1173 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1174 p += size / 8;
1177 *phashlen = hashlen;
1178 *pphash = phash;
1181 // Verdef methods.
1183 // Write this definition to a buffer for the output section.
1185 template<int size, bool big_endian>
1186 unsigned char*
1187 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const
1189 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1190 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1192 elfcpp::Verdef_write<size, big_endian> vd(pb);
1193 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1194 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1195 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1196 vd.set_vd_ndx(this->index());
1197 vd.set_vd_cnt(1 + this->deps_.size());
1198 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1199 vd.set_vd_aux(verdef_size);
1200 vd.set_vd_next(is_last
1202 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1203 pb += verdef_size;
1205 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1206 vda.set_vda_name(dynpool->get_offset(this->name()));
1207 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1208 pb += verdaux_size;
1210 Deps::const_iterator p;
1211 unsigned int i;
1212 for (p = this->deps_.begin(), i = 0;
1213 p != this->deps_.end();
1214 ++p, ++i)
1216 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1217 vda.set_vda_name(dynpool->get_offset(*p));
1218 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1219 pb += verdaux_size;
1222 return pb;
1225 // Verneed methods.
1227 Verneed::~Verneed()
1229 for (Need_versions::iterator p = this->need_versions_.begin();
1230 p != this->need_versions_.end();
1231 ++p)
1232 delete *p;
1235 // Add a new version to this file reference.
1237 Verneed_version*
1238 Verneed::add_name(const char* name)
1240 Verneed_version* vv = new Verneed_version(name);
1241 this->need_versions_.push_back(vv);
1242 return vv;
1245 // Set the version indexes starting at INDEX.
1247 unsigned int
1248 Verneed::finalize(unsigned int index)
1250 for (Need_versions::iterator p = this->need_versions_.begin();
1251 p != this->need_versions_.end();
1252 ++p)
1254 (*p)->set_index(index);
1255 ++index;
1257 return index;
1260 // Write this list of referenced versions to a buffer for the output
1261 // section.
1263 template<int size, bool big_endian>
1264 unsigned char*
1265 Verneed::write(const Stringpool* dynpool, bool is_last,
1266 unsigned char* pb) const
1268 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1269 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1271 elfcpp::Verneed_write<size, big_endian> vn(pb);
1272 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1273 vn.set_vn_cnt(this->need_versions_.size());
1274 vn.set_vn_file(dynpool->get_offset(this->filename()));
1275 vn.set_vn_aux(verneed_size);
1276 vn.set_vn_next(is_last
1278 : verneed_size + this->need_versions_.size() * vernaux_size);
1279 pb += verneed_size;
1281 Need_versions::const_iterator p;
1282 unsigned int i;
1283 for (p = this->need_versions_.begin(), i = 0;
1284 p != this->need_versions_.end();
1285 ++p, ++i)
1287 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1288 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1289 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1290 vna.set_vna_flags(0);
1291 vna.set_vna_other((*p)->index());
1292 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1293 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1295 : vernaux_size);
1296 pb += vernaux_size;
1299 return pb;
1302 // Versions methods.
1304 Versions::Versions(const Version_script_info& version_script,
1305 Stringpool* dynpool)
1306 : defs_(), needs_(), version_table_(),
1307 is_finalized_(false), version_script_(version_script)
1309 // We always need a base version, so define that first. Nothing
1310 // explicitly declares itself as part of base, so it doesn't need to
1311 // be in version_table_.
1312 if (parameters->options().shared())
1314 const char* name = parameters->options().soname();
1315 if (name == NULL)
1316 name = parameters->options().output_file_name();
1317 name = dynpool->add(name, false, NULL);
1318 Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
1319 true, false, true);
1320 this->defs_.push_back(vdbase);
1323 if (!this->version_script_.empty())
1325 // Parse the version script, and insert each declared version into
1326 // defs_ and version_table_.
1327 std::vector<std::string> versions = this->version_script_.get_versions();
1328 for (size_t k = 0; k < versions.size(); ++k)
1330 Stringpool::Key version_key;
1331 const char* version = dynpool->add(versions[k].c_str(),
1332 true, &version_key);
1333 Verdef* const vd = new Verdef(
1334 version,
1335 this->version_script_.get_dependencies(version),
1336 false, false, false);
1337 this->defs_.push_back(vd);
1338 Key key(version_key, 0);
1339 this->version_table_.insert(std::make_pair(key, vd));
1344 Versions::~Versions()
1346 for (Defs::iterator p = this->defs_.begin();
1347 p != this->defs_.end();
1348 ++p)
1349 delete *p;
1351 for (Needs::iterator p = this->needs_.begin();
1352 p != this->needs_.end();
1353 ++p)
1354 delete *p;
1357 // Return the dynamic object which a symbol refers to.
1359 Dynobj*
1360 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1361 const Symbol* sym) const
1363 if (sym->is_copied_from_dynobj())
1364 return symtab->get_copy_source(sym);
1365 else
1367 Object* object = sym->object();
1368 gold_assert(object->is_dynamic());
1369 return static_cast<Dynobj*>(object);
1373 // Record version information for a symbol going into the dynamic
1374 // symbol table.
1376 void
1377 Versions::record_version(const Symbol_table* symtab,
1378 Stringpool* dynpool, const Symbol* sym)
1380 gold_assert(!this->is_finalized_);
1381 gold_assert(sym->version() != NULL);
1383 Stringpool::Key version_key;
1384 const char* version = dynpool->add(sym->version(), false, &version_key);
1386 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1388 if (parameters->options().shared())
1389 this->add_def(sym, version, version_key);
1391 else
1393 // This is a version reference.
1394 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1395 this->add_need(dynpool, dynobj->soname(), version, version_key);
1399 // We've found a symbol SYM defined in version VERSION.
1401 void
1402 Versions::add_def(const Symbol* sym, const char* version,
1403 Stringpool::Key version_key)
1405 Key k(version_key, 0);
1406 Version_base* const vbnull = NULL;
1407 std::pair<Version_table::iterator, bool> ins =
1408 this->version_table_.insert(std::make_pair(k, vbnull));
1410 if (!ins.second)
1412 // We already have an entry for this version.
1413 Version_base* vb = ins.first->second;
1415 // We have now seen a symbol in this version, so it is not
1416 // weak.
1417 gold_assert(vb != NULL);
1418 vb->clear_weak();
1420 else
1422 // If we are creating a shared object, it is an error to
1423 // find a definition of a symbol with a version which is not
1424 // in the version script.
1425 if (parameters->options().shared())
1426 gold_error(_("symbol %s has undefined version %s"),
1427 sym->demangled_name().c_str(), version);
1429 // When creating a regular executable, automatically define
1430 // a new version.
1431 Verdef* vd = new Verdef(version, std::vector<std::string>(),
1432 false, false, false);
1433 this->defs_.push_back(vd);
1434 ins.first->second = vd;
1438 // Add a reference to version NAME in file FILENAME.
1440 void
1441 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1442 Stringpool::Key name_key)
1444 Stringpool::Key filename_key;
1445 filename = dynpool->add(filename, true, &filename_key);
1447 Key k(name_key, filename_key);
1448 Version_base* const vbnull = NULL;
1449 std::pair<Version_table::iterator, bool> ins =
1450 this->version_table_.insert(std::make_pair(k, vbnull));
1452 if (!ins.second)
1454 // We already have an entry for this filename/version.
1455 return;
1458 // See whether we already have this filename. We don't expect many
1459 // version references, so we just do a linear search. This could be
1460 // replaced by a hash table.
1461 Verneed* vn = NULL;
1462 for (Needs::iterator p = this->needs_.begin();
1463 p != this->needs_.end();
1464 ++p)
1466 if ((*p)->filename() == filename)
1468 vn = *p;
1469 break;
1473 if (vn == NULL)
1475 // We have a new filename.
1476 vn = new Verneed(filename);
1477 this->needs_.push_back(vn);
1480 ins.first->second = vn->add_name(name);
1483 // Set the version indexes. Create a new dynamic version symbol for
1484 // each new version definition.
1486 unsigned int
1487 Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
1488 std::vector<Symbol*>* syms)
1490 gold_assert(!this->is_finalized_);
1492 unsigned int vi = 1;
1494 for (Defs::iterator p = this->defs_.begin();
1495 p != this->defs_.end();
1496 ++p)
1498 (*p)->set_index(vi);
1499 ++vi;
1501 // Create a version symbol if necessary.
1502 if (!(*p)->is_symbol_created())
1504 Symbol* vsym = symtab->define_as_constant((*p)->name(),
1505 (*p)->name(), 0, 0,
1506 elfcpp::STT_OBJECT,
1507 elfcpp::STB_GLOBAL,
1508 elfcpp::STV_DEFAULT, 0,
1509 false, false);
1510 vsym->set_needs_dynsym_entry();
1511 vsym->set_dynsym_index(dynsym_index);
1512 ++dynsym_index;
1513 syms->push_back(vsym);
1514 // The name is already in the dynamic pool.
1518 // Index 1 is used for global symbols.
1519 if (vi == 1)
1521 gold_assert(this->defs_.empty());
1522 vi = 2;
1525 for (Needs::iterator p = this->needs_.begin();
1526 p != this->needs_.end();
1527 ++p)
1528 vi = (*p)->finalize(vi);
1530 this->is_finalized_ = true;
1532 return dynsym_index;
1535 // Return the version index to use for a symbol. This does two hash
1536 // table lookups: one in DYNPOOL and one in this->version_table_.
1537 // Another approach alternative would be store a pointer in SYM, which
1538 // would increase the size of the symbol table. Or perhaps we could
1539 // use a hash table from dynamic symbol pointer values to Version_base
1540 // pointers.
1542 unsigned int
1543 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1544 const Symbol* sym) const
1546 Stringpool::Key version_key;
1547 const char* version = dynpool->find(sym->version(), &version_key);
1548 gold_assert(version != NULL);
1550 Key k;
1551 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1553 if (!parameters->options().shared())
1554 return elfcpp::VER_NDX_GLOBAL;
1555 k = Key(version_key, 0);
1557 else
1559 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1561 Stringpool::Key filename_key;
1562 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1563 gold_assert(filename != NULL);
1565 k = Key(version_key, filename_key);
1568 Version_table::const_iterator p = this->version_table_.find(k);
1569 gold_assert(p != this->version_table_.end());
1571 return p->second->index();
1574 // Return an allocated buffer holding the contents of the symbol
1575 // version section.
1577 template<int size, bool big_endian>
1578 void
1579 Versions::symbol_section_contents(const Symbol_table* symtab,
1580 const Stringpool* dynpool,
1581 unsigned int local_symcount,
1582 const std::vector<Symbol*>& syms,
1583 unsigned char** pp,
1584 unsigned int* psize) const
1586 gold_assert(this->is_finalized_);
1588 unsigned int sz = (local_symcount + syms.size()) * 2;
1589 unsigned char* pbuf = new unsigned char[sz];
1591 for (unsigned int i = 0; i < local_symcount; ++i)
1592 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1593 elfcpp::VER_NDX_LOCAL);
1595 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1596 p != syms.end();
1597 ++p)
1599 unsigned int version_index;
1600 const char* version = (*p)->version();
1601 if (version == NULL)
1602 version_index = elfcpp::VER_NDX_GLOBAL;
1603 else
1604 version_index = this->version_index(symtab, dynpool, *p);
1605 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1606 // the hidden bit.
1607 if ((*p)->version() != NULL && !(*p)->is_default())
1608 version_index |= elfcpp::VERSYM_HIDDEN;
1609 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1610 version_index);
1613 *pp = pbuf;
1614 *psize = sz;
1617 // Return an allocated buffer holding the contents of the version
1618 // definition section.
1620 template<int size, bool big_endian>
1621 void
1622 Versions::def_section_contents(const Stringpool* dynpool,
1623 unsigned char** pp, unsigned int* psize,
1624 unsigned int* pentries) const
1626 gold_assert(this->is_finalized_);
1627 gold_assert(!this->defs_.empty());
1629 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1630 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1632 unsigned int sz = 0;
1633 for (Defs::const_iterator p = this->defs_.begin();
1634 p != this->defs_.end();
1635 ++p)
1637 sz += verdef_size + verdaux_size;
1638 sz += (*p)->count_dependencies() * verdaux_size;
1641 unsigned char* pbuf = new unsigned char[sz];
1643 unsigned char* pb = pbuf;
1644 Defs::const_iterator p;
1645 unsigned int i;
1646 for (p = this->defs_.begin(), i = 0;
1647 p != this->defs_.end();
1648 ++p, ++i)
1649 pb = (*p)->write<size, big_endian>(dynpool,
1650 i + 1 >= this->defs_.size(),
1651 pb);
1653 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1655 *pp = pbuf;
1656 *psize = sz;
1657 *pentries = this->defs_.size();
1660 // Return an allocated buffer holding the contents of the version
1661 // reference section.
1663 template<int size, bool big_endian>
1664 void
1665 Versions::need_section_contents(const Stringpool* dynpool,
1666 unsigned char** pp, unsigned int *psize,
1667 unsigned int *pentries) const
1669 gold_assert(this->is_finalized_);
1670 gold_assert(!this->needs_.empty());
1672 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1673 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1675 unsigned int sz = 0;
1676 for (Needs::const_iterator p = this->needs_.begin();
1677 p != this->needs_.end();
1678 ++p)
1680 sz += verneed_size;
1681 sz += (*p)->count_versions() * vernaux_size;
1684 unsigned char* pbuf = new unsigned char[sz];
1686 unsigned char* pb = pbuf;
1687 Needs::const_iterator p;
1688 unsigned int i;
1689 for (p = this->needs_.begin(), i = 0;
1690 p != this->needs_.end();
1691 ++p, ++i)
1692 pb = (*p)->write<size, big_endian>(dynpool,
1693 i + 1 >= this->needs_.size(),
1694 pb);
1696 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1698 *pp = pbuf;
1699 *psize = sz;
1700 *pentries = this->needs_.size();
1703 // Instantiate the templates we need. We could use the configure
1704 // script to restrict this to only the ones for implemented targets.
1706 #ifdef HAVE_TARGET_32_LITTLE
1707 template
1708 class Sized_dynobj<32, false>;
1709 #endif
1711 #ifdef HAVE_TARGET_32_BIG
1712 template
1713 class Sized_dynobj<32, true>;
1714 #endif
1716 #ifdef HAVE_TARGET_64_LITTLE
1717 template
1718 class Sized_dynobj<64, false>;
1719 #endif
1721 #ifdef HAVE_TARGET_64_BIG
1722 template
1723 class Sized_dynobj<64, true>;
1724 #endif
1726 #ifdef HAVE_TARGET_32_LITTLE
1727 template
1728 void
1729 Versions::symbol_section_contents<32, false>(
1730 const Symbol_table*,
1731 const Stringpool*,
1732 unsigned int,
1733 const std::vector<Symbol*>&,
1734 unsigned char**,
1735 unsigned int*) const;
1736 #endif
1738 #ifdef HAVE_TARGET_32_BIG
1739 template
1740 void
1741 Versions::symbol_section_contents<32, true>(
1742 const Symbol_table*,
1743 const Stringpool*,
1744 unsigned int,
1745 const std::vector<Symbol*>&,
1746 unsigned char**,
1747 unsigned int*) const;
1748 #endif
1750 #ifdef HAVE_TARGET_64_LITTLE
1751 template
1752 void
1753 Versions::symbol_section_contents<64, false>(
1754 const Symbol_table*,
1755 const Stringpool*,
1756 unsigned int,
1757 const std::vector<Symbol*>&,
1758 unsigned char**,
1759 unsigned int*) const;
1760 #endif
1762 #ifdef HAVE_TARGET_64_BIG
1763 template
1764 void
1765 Versions::symbol_section_contents<64, true>(
1766 const Symbol_table*,
1767 const Stringpool*,
1768 unsigned int,
1769 const std::vector<Symbol*>&,
1770 unsigned char**,
1771 unsigned int*) const;
1772 #endif
1774 #ifdef HAVE_TARGET_32_LITTLE
1775 template
1776 void
1777 Versions::def_section_contents<32, false>(
1778 const Stringpool*,
1779 unsigned char**,
1780 unsigned int*,
1781 unsigned int*) const;
1782 #endif
1784 #ifdef HAVE_TARGET_32_BIG
1785 template
1786 void
1787 Versions::def_section_contents<32, true>(
1788 const Stringpool*,
1789 unsigned char**,
1790 unsigned int*,
1791 unsigned int*) const;
1792 #endif
1794 #ifdef HAVE_TARGET_64_LITTLE
1795 template
1796 void
1797 Versions::def_section_contents<64, false>(
1798 const Stringpool*,
1799 unsigned char**,
1800 unsigned int*,
1801 unsigned int*) const;
1802 #endif
1804 #ifdef HAVE_TARGET_64_BIG
1805 template
1806 void
1807 Versions::def_section_contents<64, true>(
1808 const Stringpool*,
1809 unsigned char**,
1810 unsigned int*,
1811 unsigned int*) const;
1812 #endif
1814 #ifdef HAVE_TARGET_32_LITTLE
1815 template
1816 void
1817 Versions::need_section_contents<32, false>(
1818 const Stringpool*,
1819 unsigned char**,
1820 unsigned int*,
1821 unsigned int*) const;
1822 #endif
1824 #ifdef HAVE_TARGET_32_BIG
1825 template
1826 void
1827 Versions::need_section_contents<32, true>(
1828 const Stringpool*,
1829 unsigned char**,
1830 unsigned int*,
1831 unsigned int*) const;
1832 #endif
1834 #ifdef HAVE_TARGET_64_LITTLE
1835 template
1836 void
1837 Versions::need_section_contents<64, false>(
1838 const Stringpool*,
1839 unsigned char**,
1840 unsigned int*,
1841 unsigned int*) const;
1842 #endif
1844 #ifdef HAVE_TARGET_64_BIG
1845 template
1846 void
1847 Versions::need_section_contents<64, true>(
1848 const Stringpool*,
1849 unsigned char**,
1850 unsigned int*,
1851 unsigned int*) const;
1852 #endif
1854 } // End namespace gold.