Disable -Werror on coff-tic4x.lo and coff-tic54x.lo.
[binutils.git] / gold / dynobj.cc
blobe4a976dcba079ea5a893bef1316292f8671fb9ba
1 // dynobj.cc -- dynamic object support for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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()
88 const unsigned int shnum = this->elf_file_.shnum();
89 this->set_shnum(shnum);
92 // Find the SHT_DYNSYM section and the various version sections, and
93 // the dynamic section, given the section headers.
95 template<int size, bool big_endian>
96 void
97 Sized_dynobj<size, big_endian>::find_dynsym_sections(
98 const unsigned char* pshdrs,
99 unsigned int* pversym_shndx,
100 unsigned int* pverdef_shndx,
101 unsigned int* pverneed_shndx,
102 unsigned int* pdynamic_shndx)
104 *pversym_shndx = -1U;
105 *pverdef_shndx = -1U;
106 *pverneed_shndx = -1U;
107 *pdynamic_shndx = -1U;
109 unsigned int symtab_shndx = 0;
110 unsigned int xindex_shndx = 0;
111 unsigned int xindex_link = 0;
112 const unsigned int shnum = this->shnum();
113 const unsigned char* p = pshdrs;
114 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
116 typename This::Shdr shdr(p);
118 unsigned int* pi;
119 switch (shdr.get_sh_type())
121 case elfcpp::SHT_DYNSYM:
122 this->dynsym_shndx_ = i;
123 if (xindex_shndx > 0 && xindex_link == i)
125 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
126 xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx,
127 pshdrs);
128 this->set_xindex(xindex);
130 pi = NULL;
131 break;
132 case elfcpp::SHT_SYMTAB:
133 symtab_shndx = i;
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;
175 // If there is no dynamic symbol table, use the normal symbol table.
176 // On some SVR4 systems, a shared library is stored in an archive.
177 // The version stored in the archive only has a normal symbol table.
178 // It has an SONAME entry which points to another copy in the file
179 // system which has a dynamic symbol table as usual. This is way of
180 // addressing the issues which glibc addresses using GROUP with
181 // libc_nonshared.a.
182 if (this->dynsym_shndx_ == -1U && symtab_shndx != 0)
184 this->dynsym_shndx_ = symtab_shndx;
185 if (xindex_shndx > 0 && xindex_link == symtab_shndx)
187 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
188 xindex->read_symtab_xindex<size, big_endian>(this, xindex_shndx,
189 pshdrs);
190 this->set_xindex(xindex);
195 // Read the contents of section SHNDX. PSHDRS points to the section
196 // headers. TYPE is the expected section type. LINK is the expected
197 // section link. Store the data in *VIEW and *VIEW_SIZE. The
198 // section's sh_info field is stored in *VIEW_INFO.
200 template<int size, bool big_endian>
201 void
202 Sized_dynobj<size, big_endian>::read_dynsym_section(
203 const unsigned char* pshdrs,
204 unsigned int shndx,
205 elfcpp::SHT type,
206 unsigned int link,
207 File_view** view,
208 section_size_type* view_size,
209 unsigned int* view_info)
211 if (shndx == -1U)
213 *view = NULL;
214 *view_size = 0;
215 *view_info = 0;
216 return;
219 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
221 gold_assert(shdr.get_sh_type() == type);
223 if (this->adjust_shndx(shdr.get_sh_link()) != link)
224 this->error(_("unexpected link in section %u header: %u != %u"),
225 shndx, this->adjust_shndx(shdr.get_sh_link()), link);
227 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
228 true, false);
229 *view_size = convert_to_section_size_type(shdr.get_sh_size());
230 *view_info = shdr.get_sh_info();
233 // Read the dynamic tags. Set the soname field if this shared object
234 // has a DT_SONAME tag. Record the DT_NEEDED tags. PSHDRS points to
235 // the section headers. DYNAMIC_SHNDX is the section index of the
236 // SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE are the
237 // section index and contents of a string table which may be the one
238 // associated with the SHT_DYNAMIC section.
240 template<int size, bool big_endian>
241 void
242 Sized_dynobj<size, big_endian>::read_dynamic(const unsigned char* pshdrs,
243 unsigned int dynamic_shndx,
244 unsigned int strtab_shndx,
245 const unsigned char* strtabu,
246 off_t strtab_size)
248 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
249 gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
251 const off_t dynamic_size = dynamicshdr.get_sh_size();
252 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
253 dynamic_size, true, false);
255 const unsigned int link = this->adjust_shndx(dynamicshdr.get_sh_link());
256 if (link != strtab_shndx)
258 if (link >= this->shnum())
260 this->error(_("DYNAMIC section %u link out of range: %u"),
261 dynamic_shndx, link);
262 return;
265 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
266 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
268 this->error(_("DYNAMIC section %u link %u is not a strtab"),
269 dynamic_shndx, link);
270 return;
273 strtab_size = strtabshdr.get_sh_size();
274 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false,
275 false);
278 const char* const strtab = reinterpret_cast<const char*>(strtabu);
280 for (const unsigned char* p = pdynamic;
281 p < pdynamic + dynamic_size;
282 p += This::dyn_size)
284 typename This::Dyn dyn(p);
286 switch (dyn.get_d_tag())
288 case elfcpp::DT_NULL:
289 // We should always see DT_NULL at the end of the dynamic
290 // tags.
291 return;
293 case elfcpp::DT_SONAME:
295 off_t val = dyn.get_d_val();
296 if (val >= strtab_size)
297 this->error(_("DT_SONAME value out of range: %lld >= %lld"),
298 static_cast<long long>(val),
299 static_cast<long long>(strtab_size));
300 else
301 this->set_soname_string(strtab + val);
303 break;
305 case elfcpp::DT_NEEDED:
307 off_t val = dyn.get_d_val();
308 if (val >= strtab_size)
309 this->error(_("DT_NEEDED value out of range: %lld >= %lld"),
310 static_cast<long long>(val),
311 static_cast<long long>(strtab_size));
312 else
313 this->add_needed(strtab + val);
315 break;
317 default:
318 break;
322 this->error(_("missing DT_NULL in dynamic segment"));
325 // Read the symbols and sections from a dynamic object. We read the
326 // dynamic symbols, not the normal symbols.
328 template<int size, bool big_endian>
329 void
330 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
332 this->read_section_data(&this->elf_file_, sd);
334 const unsigned char* const pshdrs = sd->section_headers->data();
336 unsigned int versym_shndx;
337 unsigned int verdef_shndx;
338 unsigned int verneed_shndx;
339 unsigned int dynamic_shndx;
340 this->find_dynsym_sections(pshdrs, &versym_shndx, &verdef_shndx,
341 &verneed_shndx, &dynamic_shndx);
343 unsigned int strtab_shndx = -1U;
345 sd->symbols = NULL;
346 sd->symbols_size = 0;
347 sd->external_symbols_offset = 0;
348 sd->symbol_names = NULL;
349 sd->symbol_names_size = 0;
350 sd->versym = NULL;
351 sd->versym_size = 0;
352 sd->verdef = NULL;
353 sd->verdef_size = 0;
354 sd->verdef_info = 0;
355 sd->verneed = NULL;
356 sd->verneed_size = 0;
357 sd->verneed_info = 0;
359 if (this->dynsym_shndx_ != -1U)
361 // Get the dynamic symbols.
362 typename This::Shdr dynsymshdr(pshdrs
363 + this->dynsym_shndx_ * This::shdr_size);
365 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
366 dynsymshdr.get_sh_size(), true,
367 false);
368 sd->symbols_size =
369 convert_to_section_size_type(dynsymshdr.get_sh_size());
371 // Get the symbol names.
372 strtab_shndx = this->adjust_shndx(dynsymshdr.get_sh_link());
373 if (strtab_shndx >= this->shnum())
375 this->error(_("invalid dynamic symbol table name index: %u"),
376 strtab_shndx);
377 return;
379 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
380 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
382 this->error(_("dynamic symbol table name section "
383 "has wrong type: %u"),
384 static_cast<unsigned int>(strtabshdr.get_sh_type()));
385 return;
388 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
389 strtabshdr.get_sh_size(),
390 false, false);
391 sd->symbol_names_size =
392 convert_to_section_size_type(strtabshdr.get_sh_size());
394 // Get the version information.
396 unsigned int dummy;
397 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
398 this->dynsym_shndx_,
399 &sd->versym, &sd->versym_size, &dummy);
401 // We require that the version definition and need section link
402 // to the same string table as the dynamic symbol table. This
403 // is not a technical requirement, but it always happens in
404 // practice. We could change this if necessary.
406 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
407 strtab_shndx, &sd->verdef, &sd->verdef_size,
408 &sd->verdef_info);
410 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
411 strtab_shndx, &sd->verneed, &sd->verneed_size,
412 &sd->verneed_info);
415 // Read the SHT_DYNAMIC section to find whether this shared object
416 // has a DT_SONAME tag and to record any DT_NEEDED tags. This
417 // doesn't really have anything to do with reading the symbols, but
418 // this is a convenient place to do it.
419 if (dynamic_shndx != -1U)
420 this->read_dynamic(pshdrs, dynamic_shndx, strtab_shndx,
421 (sd->symbol_names == NULL
422 ? NULL
423 : sd->symbol_names->data()),
424 sd->symbol_names_size);
427 // Return the Xindex structure to use for object with lots of
428 // sections.
430 template<int size, bool big_endian>
431 Xindex*
432 Sized_dynobj<size, big_endian>::do_initialize_xindex()
434 gold_assert(this->dynsym_shndx_ != -1U);
435 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
436 xindex->initialize_symtab_xindex<size, big_endian>(this, this->dynsym_shndx_);
437 return xindex;
440 // Lay out the input sections for a dynamic object. We don't want to
441 // include sections from a dynamic object, so all that we actually do
442 // here is check for .gnu.warning and .note.GNU-split-stack sections.
444 template<int size, bool big_endian>
445 void
446 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
447 Layout*,
448 Read_symbols_data* sd)
450 const unsigned int shnum = this->shnum();
451 if (shnum == 0)
452 return;
454 // Get the section headers.
455 const unsigned char* pshdrs = sd->section_headers->data();
457 // Get the section names.
458 const unsigned char* pnamesu = sd->section_names->data();
459 const char* pnames = reinterpret_cast<const char*>(pnamesu);
461 // Skip the first, dummy, section.
462 pshdrs += This::shdr_size;
463 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
465 typename This::Shdr shdr(pshdrs);
467 if (shdr.get_sh_name() >= sd->section_names_size)
469 this->error(_("bad section name offset for section %u: %lu"),
470 i, static_cast<unsigned long>(shdr.get_sh_name()));
471 return;
474 const char* name = pnames + shdr.get_sh_name();
476 this->handle_gnu_warning_section(name, i, symtab);
477 this->handle_split_stack_section(name);
480 delete sd->section_headers;
481 sd->section_headers = NULL;
482 delete sd->section_names;
483 sd->section_names = NULL;
486 // Add an entry to the vector mapping version numbers to version
487 // strings.
489 template<int size, bool big_endian>
490 void
491 Sized_dynobj<size, big_endian>::set_version_map(
492 Version_map* version_map,
493 unsigned int ndx,
494 const char* name) const
496 if (ndx >= version_map->size())
497 version_map->resize(ndx + 1);
498 if ((*version_map)[ndx] != NULL)
499 this->error(_("duplicate definition for version %u"), ndx);
500 (*version_map)[ndx] = name;
503 // Add mappings for the version definitions to VERSION_MAP.
505 template<int size, bool big_endian>
506 void
507 Sized_dynobj<size, big_endian>::make_verdef_map(
508 Read_symbols_data* sd,
509 Version_map* version_map) const
511 if (sd->verdef == NULL)
512 return;
514 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
515 section_size_type names_size = sd->symbol_names_size;
517 const unsigned char* pverdef = sd->verdef->data();
518 section_size_type verdef_size = sd->verdef_size;
519 const unsigned int count = sd->verdef_info;
521 const unsigned char* p = pverdef;
522 for (unsigned int i = 0; i < count; ++i)
524 elfcpp::Verdef<size, big_endian> verdef(p);
526 if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
528 this->error(_("unexpected verdef version %u"),
529 verdef.get_vd_version());
530 return;
533 const section_size_type vd_ndx = verdef.get_vd_ndx();
535 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
536 // sure why.
538 // The first Verdaux holds the name of this version. Subsequent
539 // ones are versions that this one depends upon, which we don't
540 // care about here.
541 const section_size_type vd_cnt = verdef.get_vd_cnt();
542 if (vd_cnt < 1)
544 this->error(_("verdef vd_cnt field too small: %u"),
545 static_cast<unsigned int>(vd_cnt));
546 return;
549 const section_size_type vd_aux = verdef.get_vd_aux();
550 if ((p - pverdef) + vd_aux >= verdef_size)
552 this->error(_("verdef vd_aux field out of range: %u"),
553 static_cast<unsigned int>(vd_aux));
554 return;
557 const unsigned char* pvda = p + vd_aux;
558 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
560 const section_size_type vda_name = verdaux.get_vda_name();
561 if (vda_name >= names_size)
563 this->error(_("verdaux vda_name field out of range: %u"),
564 static_cast<unsigned int>(vda_name));
565 return;
568 this->set_version_map(version_map, vd_ndx, names + vda_name);
570 const section_size_type vd_next = verdef.get_vd_next();
571 if ((p - pverdef) + vd_next >= verdef_size)
573 this->error(_("verdef vd_next field out of range: %u"),
574 static_cast<unsigned int>(vd_next));
575 return;
578 p += vd_next;
582 // Add mappings for the required versions to VERSION_MAP.
584 template<int size, bool big_endian>
585 void
586 Sized_dynobj<size, big_endian>::make_verneed_map(
587 Read_symbols_data* sd,
588 Version_map* version_map) const
590 if (sd->verneed == NULL)
591 return;
593 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
594 section_size_type names_size = sd->symbol_names_size;
596 const unsigned char* pverneed = sd->verneed->data();
597 const section_size_type verneed_size = sd->verneed_size;
598 const unsigned int count = sd->verneed_info;
600 const unsigned char* p = pverneed;
601 for (unsigned int i = 0; i < count; ++i)
603 elfcpp::Verneed<size, big_endian> verneed(p);
605 if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
607 this->error(_("unexpected verneed version %u"),
608 verneed.get_vn_version());
609 return;
612 const section_size_type vn_aux = verneed.get_vn_aux();
614 if ((p - pverneed) + vn_aux >= verneed_size)
616 this->error(_("verneed vn_aux field out of range: %u"),
617 static_cast<unsigned int>(vn_aux));
618 return;
621 const unsigned int vn_cnt = verneed.get_vn_cnt();
622 const unsigned char* pvna = p + vn_aux;
623 for (unsigned int j = 0; j < vn_cnt; ++j)
625 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
627 const unsigned int vna_name = vernaux.get_vna_name();
628 if (vna_name >= names_size)
630 this->error(_("vernaux vna_name field out of range: %u"),
631 static_cast<unsigned int>(vna_name));
632 return;
635 this->set_version_map(version_map, vernaux.get_vna_other(),
636 names + vna_name);
638 const section_size_type vna_next = vernaux.get_vna_next();
639 if ((pvna - pverneed) + vna_next >= verneed_size)
641 this->error(_("verneed vna_next field out of range: %u"),
642 static_cast<unsigned int>(vna_next));
643 return;
646 pvna += vna_next;
649 const section_size_type vn_next = verneed.get_vn_next();
650 if ((p - pverneed) + vn_next >= verneed_size)
652 this->error(_("verneed vn_next field out of range: %u"),
653 static_cast<unsigned int>(vn_next));
654 return;
657 p += vn_next;
661 // Create a vector mapping version numbers to version strings.
663 template<int size, bool big_endian>
664 void
665 Sized_dynobj<size, big_endian>::make_version_map(
666 Read_symbols_data* sd,
667 Version_map* version_map) const
669 if (sd->verdef == NULL && sd->verneed == NULL)
670 return;
672 // A guess at the maximum version number we will see. If this is
673 // wrong we will be less efficient but still correct.
674 version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
676 this->make_verdef_map(sd, version_map);
677 this->make_verneed_map(sd, version_map);
680 // Add the dynamic symbols to the symbol table.
682 template<int size, bool big_endian>
683 void
684 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
685 Read_symbols_data* sd,
686 Layout*)
688 if (sd->symbols == NULL)
690 gold_assert(sd->symbol_names == NULL);
691 gold_assert(sd->versym == NULL && sd->verdef == NULL
692 && sd->verneed == NULL);
693 return;
696 const int sym_size = This::sym_size;
697 const size_t symcount = sd->symbols_size / sym_size;
698 gold_assert(sd->external_symbols_offset == 0);
699 if (symcount * sym_size != sd->symbols_size)
701 this->error(_("size of dynamic symbols is not multiple of symbol size"));
702 return;
705 Version_map version_map;
706 this->make_version_map(sd, &version_map);
708 // If printing symbol counts or a cross reference table, we want to
709 // track symbols.
710 if (parameters->options().user_set_print_symbol_counts()
711 || parameters->options().cref())
713 this->symbols_ = new Symbols();
714 this->symbols_->resize(symcount);
717 const char* sym_names =
718 reinterpret_cast<const char*>(sd->symbol_names->data());
719 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
720 sym_names, sd->symbol_names_size,
721 (sd->versym == NULL
722 ? NULL
723 : sd->versym->data()),
724 sd->versym_size,
725 &version_map,
726 this->symbols_,
727 &this->defined_count_);
729 delete sd->symbols;
730 sd->symbols = NULL;
731 delete sd->symbol_names;
732 sd->symbol_names = NULL;
733 if (sd->versym != NULL)
735 delete sd->versym;
736 sd->versym = NULL;
738 if (sd->verdef != NULL)
740 delete sd->verdef;
741 sd->verdef = NULL;
743 if (sd->verneed != NULL)
745 delete sd->verneed;
746 sd->verneed = NULL;
749 // This is normally the last time we will read any data from this
750 // file.
751 this->clear_view_cache_marks();
754 template<int size, bool big_endian>
755 Archive::Should_include
756 Sized_dynobj<size, big_endian>::do_should_include_member(Symbol_table*,
757 Layout*,
758 Read_symbols_data*,
759 std::string*)
761 return Archive::SHOULD_INCLUDE_YES;
764 // Get symbol counts.
766 template<int size, bool big_endian>
767 void
768 Sized_dynobj<size, big_endian>::do_get_global_symbol_counts(
769 const Symbol_table*,
770 size_t* defined,
771 size_t* used) const
773 *defined = this->defined_count_;
774 size_t count = 0;
775 for (typename Symbols::const_iterator p = this->symbols_->begin();
776 p != this->symbols_->end();
777 ++p)
778 if (*p != NULL
779 && (*p)->source() == Symbol::FROM_OBJECT
780 && (*p)->object() == this
781 && (*p)->is_defined()
782 && (*p)->dynsym_index() != -1U)
783 ++count;
784 *used = count;
787 // Given a vector of hash codes, compute the number of hash buckets to
788 // use.
790 unsigned int
791 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
792 bool for_gnu_hash_table)
794 // FIXME: Implement optional hash table optimization.
796 // Array used to determine the number of hash table buckets to use
797 // based on the number of symbols there are. If there are fewer
798 // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
799 // buckets, fewer than 37 we use 17 buckets, and so forth. We never
800 // use more than 262147 buckets. This is straight from the old GNU
801 // linker.
802 static const unsigned int buckets[] =
804 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
805 16411, 32771, 65537, 131101, 262147
807 const int buckets_count = sizeof buckets / sizeof buckets[0];
809 unsigned int symcount = hashcodes.size();
810 unsigned int ret = 1;
811 const double full_fraction
812 = 1.0 - parameters->options().hash_bucket_empty_fraction();
813 for (int i = 0; i < buckets_count; ++i)
815 if (symcount < buckets[i] * full_fraction)
816 break;
817 ret = buckets[i];
820 if (for_gnu_hash_table && ret < 2)
821 ret = 2;
823 return ret;
826 // The standard ELF hash function. This hash function must not
827 // change, as the dynamic linker uses it also.
829 uint32_t
830 Dynobj::elf_hash(const char* name)
832 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
833 uint32_t h = 0;
834 unsigned char c;
835 while ((c = *nameu++) != '\0')
837 h = (h << 4) + c;
838 uint32_t g = h & 0xf0000000;
839 if (g != 0)
841 h ^= g >> 24;
842 // The ELF ABI says h &= ~g, but using xor is equivalent in
843 // this case (since g was set from h) and may save one
844 // instruction.
845 h ^= g;
848 return h;
851 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
852 // DYNSYMS is a vector with all the global dynamic symbols.
853 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
854 // symbol table.
856 void
857 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
858 unsigned int local_dynsym_count,
859 unsigned char** pphash,
860 unsigned int* phashlen)
862 unsigned int dynsym_count = dynsyms.size();
864 // Get the hash values for all the symbols.
865 std::vector<uint32_t> dynsym_hashvals(dynsym_count);
866 for (unsigned int i = 0; i < dynsym_count; ++i)
867 dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
869 const unsigned int bucketcount =
870 Dynobj::compute_bucket_count(dynsym_hashvals, false);
872 std::vector<uint32_t> bucket(bucketcount);
873 std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
875 for (unsigned int i = 0; i < dynsym_count; ++i)
877 unsigned int dynsym_index = dynsyms[i]->dynsym_index();
878 unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
879 chain[dynsym_index] = bucket[bucketpos];
880 bucket[bucketpos] = dynsym_index;
883 unsigned int hashlen = ((2
884 + bucketcount
885 + local_dynsym_count
886 + dynsym_count)
887 * 4);
888 unsigned char* phash = new unsigned char[hashlen];
890 if (parameters->target().is_big_endian())
892 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
893 Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
894 hashlen);
895 #else
896 gold_unreachable();
897 #endif
899 else
901 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
902 Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
903 hashlen);
904 #else
905 gold_unreachable();
906 #endif
909 *pphash = phash;
910 *phashlen = hashlen;
913 // Fill in an ELF hash table.
915 template<bool big_endian>
916 void
917 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
918 const std::vector<uint32_t>& chain,
919 unsigned char* phash,
920 unsigned int hashlen)
922 unsigned char* p = phash;
924 const unsigned int bucketcount = bucket.size();
925 const unsigned int chaincount = chain.size();
927 elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
928 p += 4;
929 elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
930 p += 4;
932 for (unsigned int i = 0; i < bucketcount; ++i)
934 elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
935 p += 4;
938 for (unsigned int i = 0; i < chaincount; ++i)
940 elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
941 p += 4;
944 gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
947 // The hash function used for the GNU hash table. This hash function
948 // must not change, as the dynamic linker uses it also.
950 uint32_t
951 Dynobj::gnu_hash(const char* name)
953 const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
954 uint32_t h = 5381;
955 unsigned char c;
956 while ((c = *nameu++) != '\0')
957 h = (h << 5) + h + c;
958 return h;
961 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. GNU hash
962 // tables are an extension to ELF which are recognized by the GNU
963 // dynamic linker. They are referenced using dynamic tag DT_GNU_HASH.
964 // TARGET is the target. DYNSYMS is a vector with all the global
965 // symbols which will be going into the dynamic symbol table.
966 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
967 // symbol table.
969 void
970 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
971 unsigned int local_dynsym_count,
972 unsigned char** pphash,
973 unsigned int* phashlen)
975 const unsigned int count = dynsyms.size();
977 // Sort the dynamic symbols into two vectors. Symbols which we do
978 // not want to put into the hash table we store into
979 // UNHASHED_DYNSYMS. Symbols which we do want to store we put into
980 // HASHED_DYNSYMS. DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
981 // and records the hash codes.
983 std::vector<Symbol*> unhashed_dynsyms;
984 unhashed_dynsyms.reserve(count);
986 std::vector<Symbol*> hashed_dynsyms;
987 hashed_dynsyms.reserve(count);
989 std::vector<uint32_t> dynsym_hashvals;
990 dynsym_hashvals.reserve(count);
992 for (unsigned int i = 0; i < count; ++i)
994 Symbol* sym = dynsyms[i];
996 if (!sym->needs_dynsym_value()
997 && (sym->is_undefined()
998 || sym->is_from_dynobj()
999 || sym->is_forced_local()))
1000 unhashed_dynsyms.push_back(sym);
1001 else
1003 hashed_dynsyms.push_back(sym);
1004 dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
1008 // Put the unhashed symbols at the start of the global portion of
1009 // the dynamic symbol table.
1010 const unsigned int unhashed_count = unhashed_dynsyms.size();
1011 unsigned int unhashed_dynsym_index = local_dynsym_count;
1012 for (unsigned int i = 0; i < unhashed_count; ++i)
1014 unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
1015 ++unhashed_dynsym_index;
1018 // For the actual data generation we call out to a templatized
1019 // function.
1020 int size = parameters->target().get_size();
1021 bool big_endian = parameters->target().is_big_endian();
1022 if (size == 32)
1024 if (big_endian)
1026 #ifdef HAVE_TARGET_32_BIG
1027 Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
1028 dynsym_hashvals,
1029 unhashed_dynsym_index,
1030 pphash,
1031 phashlen);
1032 #else
1033 gold_unreachable();
1034 #endif
1036 else
1038 #ifdef HAVE_TARGET_32_LITTLE
1039 Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
1040 dynsym_hashvals,
1041 unhashed_dynsym_index,
1042 pphash,
1043 phashlen);
1044 #else
1045 gold_unreachable();
1046 #endif
1049 else if (size == 64)
1051 if (big_endian)
1053 #ifdef HAVE_TARGET_64_BIG
1054 Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
1055 dynsym_hashvals,
1056 unhashed_dynsym_index,
1057 pphash,
1058 phashlen);
1059 #else
1060 gold_unreachable();
1061 #endif
1063 else
1065 #ifdef HAVE_TARGET_64_LITTLE
1066 Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
1067 dynsym_hashvals,
1068 unhashed_dynsym_index,
1069 pphash,
1070 phashlen);
1071 #else
1072 gold_unreachable();
1073 #endif
1076 else
1077 gold_unreachable();
1080 // Create the actual data for a GNU hash table. This is just a copy
1081 // of the code from the old GNU linker.
1083 template<int size, bool big_endian>
1084 void
1085 Dynobj::sized_create_gnu_hash_table(
1086 const std::vector<Symbol*>& hashed_dynsyms,
1087 const std::vector<uint32_t>& dynsym_hashvals,
1088 unsigned int unhashed_dynsym_count,
1089 unsigned char** pphash,
1090 unsigned int* phashlen)
1092 if (hashed_dynsyms.empty())
1094 // Special case for the empty hash table.
1095 unsigned int hashlen = 5 * 4 + size / 8;
1096 unsigned char* phash = new unsigned char[hashlen];
1097 // One empty bucket.
1098 elfcpp::Swap<32, big_endian>::writeval(phash, 1);
1099 // Symbol index above unhashed symbols.
1100 elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
1101 // One word for bitmask.
1102 elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
1103 // Only bloom filter.
1104 elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
1105 // No valid hashes.
1106 elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
1107 // No hashes in only bucket.
1108 elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
1110 *phashlen = hashlen;
1111 *pphash = phash;
1113 return;
1116 const unsigned int bucketcount =
1117 Dynobj::compute_bucket_count(dynsym_hashvals, true);
1119 const unsigned int nsyms = hashed_dynsyms.size();
1121 uint32_t maskbitslog2 = 1;
1122 uint32_t x = nsyms >> 1;
1123 while (x != 0)
1125 ++maskbitslog2;
1126 x >>= 1;
1128 if (maskbitslog2 < 3)
1129 maskbitslog2 = 5;
1130 else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1131 maskbitslog2 += 3;
1132 else
1133 maskbitslog2 += 2;
1135 uint32_t shift1;
1136 if (size == 32)
1137 shift1 = 5;
1138 else
1140 if (maskbitslog2 == 5)
1141 maskbitslog2 = 6;
1142 shift1 = 6;
1144 uint32_t mask = (1U << shift1) - 1U;
1145 uint32_t shift2 = maskbitslog2;
1146 uint32_t maskbits = 1U << maskbitslog2;
1147 uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1149 typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1150 std::vector<Word> bitmask(maskwords);
1151 std::vector<uint32_t> counts(bucketcount);
1152 std::vector<uint32_t> indx(bucketcount);
1153 uint32_t symindx = unhashed_dynsym_count;
1155 // Count the number of times each hash bucket is used.
1156 for (unsigned int i = 0; i < nsyms; ++i)
1157 ++counts[dynsym_hashvals[i] % bucketcount];
1159 unsigned int cnt = symindx;
1160 for (unsigned int i = 0; i < bucketcount; ++i)
1162 indx[i] = cnt;
1163 cnt += counts[i];
1166 unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1167 hashlen += maskbits / 8;
1168 unsigned char* phash = new unsigned char[hashlen];
1170 elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1171 elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1172 elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1173 elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1175 unsigned char* p = phash + 16 + maskbits / 8;
1176 for (unsigned int i = 0; i < bucketcount; ++i)
1178 if (counts[i] == 0)
1179 elfcpp::Swap<32, big_endian>::writeval(p, 0);
1180 else
1181 elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1182 p += 4;
1185 for (unsigned int i = 0; i < nsyms; ++i)
1187 Symbol* sym = hashed_dynsyms[i];
1188 uint32_t hashval = dynsym_hashvals[i];
1190 unsigned int bucket = hashval % bucketcount;
1191 unsigned int val = ((hashval >> shift1)
1192 & ((maskbits >> shift1) - 1));
1193 bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1194 bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1195 val = hashval & ~ 1U;
1196 if (counts[bucket] == 1)
1198 // Last element terminates the chain.
1199 val |= 1;
1201 elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1202 val);
1203 --counts[bucket];
1205 sym->set_dynsym_index(indx[bucket]);
1206 ++indx[bucket];
1209 p = phash + 16;
1210 for (unsigned int i = 0; i < maskwords; ++i)
1212 elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1213 p += size / 8;
1216 *phashlen = hashlen;
1217 *pphash = phash;
1220 // Verdef methods.
1222 // Write this definition to a buffer for the output section.
1224 template<int size, bool big_endian>
1225 unsigned char*
1226 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb) const
1228 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1229 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1231 elfcpp::Verdef_write<size, big_endian> vd(pb);
1232 vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1233 vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1234 | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0)
1235 | (this->is_info_ ? elfcpp::VER_FLG_INFO : 0));
1236 vd.set_vd_ndx(this->index());
1237 vd.set_vd_cnt(1 + this->deps_.size());
1238 vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1239 vd.set_vd_aux(verdef_size);
1240 vd.set_vd_next(is_last
1242 : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1243 pb += verdef_size;
1245 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1246 vda.set_vda_name(dynpool->get_offset(this->name()));
1247 vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1248 pb += verdaux_size;
1250 Deps::const_iterator p;
1251 unsigned int i;
1252 for (p = this->deps_.begin(), i = 0;
1253 p != this->deps_.end();
1254 ++p, ++i)
1256 elfcpp::Verdaux_write<size, big_endian> vda(pb);
1257 vda.set_vda_name(dynpool->get_offset(*p));
1258 vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1259 pb += verdaux_size;
1262 return pb;
1265 // Verneed methods.
1267 Verneed::~Verneed()
1269 for (Need_versions::iterator p = this->need_versions_.begin();
1270 p != this->need_versions_.end();
1271 ++p)
1272 delete *p;
1275 // Add a new version to this file reference.
1277 Verneed_version*
1278 Verneed::add_name(const char* name)
1280 Verneed_version* vv = new Verneed_version(name);
1281 this->need_versions_.push_back(vv);
1282 return vv;
1285 // Set the version indexes starting at INDEX.
1287 unsigned int
1288 Verneed::finalize(unsigned int index)
1290 for (Need_versions::iterator p = this->need_versions_.begin();
1291 p != this->need_versions_.end();
1292 ++p)
1294 (*p)->set_index(index);
1295 ++index;
1297 return index;
1300 // Write this list of referenced versions to a buffer for the output
1301 // section.
1303 template<int size, bool big_endian>
1304 unsigned char*
1305 Verneed::write(const Stringpool* dynpool, bool is_last,
1306 unsigned char* pb) const
1308 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1309 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1311 elfcpp::Verneed_write<size, big_endian> vn(pb);
1312 vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1313 vn.set_vn_cnt(this->need_versions_.size());
1314 vn.set_vn_file(dynpool->get_offset(this->filename()));
1315 vn.set_vn_aux(verneed_size);
1316 vn.set_vn_next(is_last
1318 : verneed_size + this->need_versions_.size() * vernaux_size);
1319 pb += verneed_size;
1321 Need_versions::const_iterator p;
1322 unsigned int i;
1323 for (p = this->need_versions_.begin(), i = 0;
1324 p != this->need_versions_.end();
1325 ++p, ++i)
1327 elfcpp::Vernaux_write<size, big_endian> vna(pb);
1328 vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1329 // FIXME: We need to sometimes set VER_FLG_WEAK here.
1330 vna.set_vna_flags(0);
1331 vna.set_vna_other((*p)->index());
1332 vna.set_vna_name(dynpool->get_offset((*p)->version()));
1333 vna.set_vna_next(i + 1 >= this->need_versions_.size()
1335 : vernaux_size);
1336 pb += vernaux_size;
1339 return pb;
1342 // Versions methods.
1344 Versions::Versions(const Version_script_info& version_script,
1345 Stringpool* dynpool)
1346 : defs_(), needs_(), version_table_(),
1347 is_finalized_(false), version_script_(version_script),
1348 needs_base_version_(parameters->options().shared())
1350 if (!this->version_script_.empty())
1352 // Parse the version script, and insert each declared version into
1353 // defs_ and version_table_.
1354 std::vector<std::string> versions = this->version_script_.get_versions();
1356 if (this->needs_base_version_ && !versions.empty())
1357 this->define_base_version(dynpool);
1359 for (size_t k = 0; k < versions.size(); ++k)
1361 Stringpool::Key version_key;
1362 const char* version = dynpool->add(versions[k].c_str(),
1363 true, &version_key);
1364 Verdef* const vd = new Verdef(
1365 version,
1366 this->version_script_.get_dependencies(version),
1367 false, false, false, false);
1368 this->defs_.push_back(vd);
1369 Key key(version_key, 0);
1370 this->version_table_.insert(std::make_pair(key, vd));
1375 Versions::~Versions()
1377 for (Defs::iterator p = this->defs_.begin();
1378 p != this->defs_.end();
1379 ++p)
1380 delete *p;
1382 for (Needs::iterator p = this->needs_.begin();
1383 p != this->needs_.end();
1384 ++p)
1385 delete *p;
1388 // Define the base version of a shared library. The base version definition
1389 // must be the first entry in defs_. We insert it lazily so that defs_ is
1390 // empty if no symbol versioning is used. Then layout can just drop the
1391 // version sections.
1393 void
1394 Versions::define_base_version(Stringpool* dynpool)
1396 // If we do any versioning at all, we always need a base version, so
1397 // define that first. Nothing explicitly declares itself as part of base,
1398 // so it doesn't need to be in version_table_.
1399 gold_assert(this->defs_.empty());
1400 const char* name = parameters->options().soname();
1401 if (name == NULL)
1402 name = parameters->options().output_file_name();
1403 name = dynpool->add(name, false, NULL);
1404 Verdef* vdbase = new Verdef(name, std::vector<std::string>(),
1405 true, false, false, true);
1406 this->defs_.push_back(vdbase);
1407 this->needs_base_version_ = false;
1410 // Return the dynamic object which a symbol refers to.
1412 Dynobj*
1413 Versions::get_dynobj_for_sym(const Symbol_table* symtab,
1414 const Symbol* sym) const
1416 if (sym->is_copied_from_dynobj())
1417 return symtab->get_copy_source(sym);
1418 else
1420 Object* object = sym->object();
1421 gold_assert(object->is_dynamic());
1422 return static_cast<Dynobj*>(object);
1426 // Record version information for a symbol going into the dynamic
1427 // symbol table.
1429 void
1430 Versions::record_version(const Symbol_table* symtab,
1431 Stringpool* dynpool, const Symbol* sym)
1433 gold_assert(!this->is_finalized_);
1434 gold_assert(sym->version() != NULL);
1436 Stringpool::Key version_key;
1437 const char* version = dynpool->add(sym->version(), false, &version_key);
1439 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1441 if (parameters->options().shared())
1442 this->add_def(sym, version, version_key);
1444 else
1446 // This is a version reference.
1447 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1448 this->add_need(dynpool, dynobj->soname(), version, version_key);
1452 // We've found a symbol SYM defined in version VERSION.
1454 void
1455 Versions::add_def(const Symbol* sym, const char* version,
1456 Stringpool::Key version_key)
1458 Key k(version_key, 0);
1459 Version_base* const vbnull = NULL;
1460 std::pair<Version_table::iterator, bool> ins =
1461 this->version_table_.insert(std::make_pair(k, vbnull));
1463 if (!ins.second)
1465 // We already have an entry for this version.
1466 Version_base* vb = ins.first->second;
1468 // We have now seen a symbol in this version, so it is not
1469 // weak.
1470 gold_assert(vb != NULL);
1471 vb->clear_weak();
1473 else
1475 // If we are creating a shared object, it is an error to
1476 // find a definition of a symbol with a version which is not
1477 // in the version script.
1478 if (parameters->options().shared())
1479 gold_error(_("symbol %s has undefined version %s"),
1480 sym->demangled_name().c_str(), version);
1481 else
1482 // We only insert a base version for shared library.
1483 gold_assert(!this->needs_base_version_);
1485 // When creating a regular executable, automatically define
1486 // a new version.
1487 Verdef* vd = new Verdef(version, std::vector<std::string>(),
1488 false, false, false, false);
1489 this->defs_.push_back(vd);
1490 ins.first->second = vd;
1494 // Add a reference to version NAME in file FILENAME.
1496 void
1497 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1498 Stringpool::Key name_key)
1500 Stringpool::Key filename_key;
1501 filename = dynpool->add(filename, true, &filename_key);
1503 Key k(name_key, filename_key);
1504 Version_base* const vbnull = NULL;
1505 std::pair<Version_table::iterator, bool> ins =
1506 this->version_table_.insert(std::make_pair(k, vbnull));
1508 if (!ins.second)
1510 // We already have an entry for this filename/version.
1511 return;
1514 // See whether we already have this filename. We don't expect many
1515 // version references, so we just do a linear search. This could be
1516 // replaced by a hash table.
1517 Verneed* vn = NULL;
1518 for (Needs::iterator p = this->needs_.begin();
1519 p != this->needs_.end();
1520 ++p)
1522 if ((*p)->filename() == filename)
1524 vn = *p;
1525 break;
1529 if (vn == NULL)
1531 // Create base version definition lazily for shared library.
1532 if (this->needs_base_version_)
1533 this->define_base_version(dynpool);
1535 // We have a new filename.
1536 vn = new Verneed(filename);
1537 this->needs_.push_back(vn);
1540 ins.first->second = vn->add_name(name);
1543 // Set the version indexes. Create a new dynamic version symbol for
1544 // each new version definition.
1546 unsigned int
1547 Versions::finalize(Symbol_table* symtab, unsigned int dynsym_index,
1548 std::vector<Symbol*>* syms)
1550 gold_assert(!this->is_finalized_);
1552 unsigned int vi = 1;
1554 for (Defs::iterator p = this->defs_.begin();
1555 p != this->defs_.end();
1556 ++p)
1558 (*p)->set_index(vi);
1559 ++vi;
1561 // Create a version symbol if necessary.
1562 if (!(*p)->is_symbol_created())
1564 Symbol* vsym = symtab->define_as_constant((*p)->name(),
1565 (*p)->name(),
1566 Symbol_table::PREDEFINED,
1567 0, 0,
1568 elfcpp::STT_OBJECT,
1569 elfcpp::STB_GLOBAL,
1570 elfcpp::STV_DEFAULT, 0,
1571 false, false);
1572 vsym->set_needs_dynsym_entry();
1573 vsym->set_dynsym_index(dynsym_index);
1574 vsym->set_is_default();
1575 ++dynsym_index;
1576 syms->push_back(vsym);
1577 // The name is already in the dynamic pool.
1581 // Index 1 is used for global symbols.
1582 if (vi == 1)
1584 gold_assert(this->defs_.empty());
1585 vi = 2;
1588 for (Needs::iterator p = this->needs_.begin();
1589 p != this->needs_.end();
1590 ++p)
1591 vi = (*p)->finalize(vi);
1593 this->is_finalized_ = true;
1595 return dynsym_index;
1598 // Return the version index to use for a symbol. This does two hash
1599 // table lookups: one in DYNPOOL and one in this->version_table_.
1600 // Another approach alternative would be store a pointer in SYM, which
1601 // would increase the size of the symbol table. Or perhaps we could
1602 // use a hash table from dynamic symbol pointer values to Version_base
1603 // pointers.
1605 unsigned int
1606 Versions::version_index(const Symbol_table* symtab, const Stringpool* dynpool,
1607 const Symbol* sym) const
1609 Stringpool::Key version_key;
1610 const char* version = dynpool->find(sym->version(), &version_key);
1611 gold_assert(version != NULL);
1613 Key k;
1614 if (!sym->is_from_dynobj() && !sym->is_copied_from_dynobj())
1616 if (!parameters->options().shared())
1617 return elfcpp::VER_NDX_GLOBAL;
1618 k = Key(version_key, 0);
1620 else
1622 Dynobj* dynobj = this->get_dynobj_for_sym(symtab, sym);
1624 Stringpool::Key filename_key;
1625 const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1626 gold_assert(filename != NULL);
1628 k = Key(version_key, filename_key);
1631 Version_table::const_iterator p = this->version_table_.find(k);
1632 gold_assert(p != this->version_table_.end());
1634 return p->second->index();
1637 // Return an allocated buffer holding the contents of the symbol
1638 // version section.
1640 template<int size, bool big_endian>
1641 void
1642 Versions::symbol_section_contents(const Symbol_table* symtab,
1643 const Stringpool* dynpool,
1644 unsigned int local_symcount,
1645 const std::vector<Symbol*>& syms,
1646 unsigned char** pp,
1647 unsigned int* psize) const
1649 gold_assert(this->is_finalized_);
1651 unsigned int sz = (local_symcount + syms.size()) * 2;
1652 unsigned char* pbuf = new unsigned char[sz];
1654 for (unsigned int i = 0; i < local_symcount; ++i)
1655 elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1656 elfcpp::VER_NDX_LOCAL);
1658 for (std::vector<Symbol*>::const_iterator p = syms.begin();
1659 p != syms.end();
1660 ++p)
1662 unsigned int version_index;
1663 const char* version = (*p)->version();
1664 if (version != NULL)
1665 version_index = this->version_index(symtab, dynpool, *p);
1666 else
1668 if ((*p)->is_defined() && !(*p)->is_from_dynobj())
1669 version_index = elfcpp::VER_NDX_GLOBAL;
1670 else
1671 version_index = elfcpp::VER_NDX_LOCAL;
1673 // If the symbol was defined as foo@V1 instead of foo@@V1, add
1674 // the hidden bit.
1675 if ((*p)->version() != NULL && !(*p)->is_default())
1676 version_index |= elfcpp::VERSYM_HIDDEN;
1677 elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1678 version_index);
1681 *pp = pbuf;
1682 *psize = sz;
1685 // Return an allocated buffer holding the contents of the version
1686 // definition section.
1688 template<int size, bool big_endian>
1689 void
1690 Versions::def_section_contents(const Stringpool* dynpool,
1691 unsigned char** pp, unsigned int* psize,
1692 unsigned int* pentries) const
1694 gold_assert(this->is_finalized_);
1695 gold_assert(!this->defs_.empty());
1697 const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1698 const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1700 unsigned int sz = 0;
1701 for (Defs::const_iterator p = this->defs_.begin();
1702 p != this->defs_.end();
1703 ++p)
1705 sz += verdef_size + verdaux_size;
1706 sz += (*p)->count_dependencies() * verdaux_size;
1709 unsigned char* pbuf = new unsigned char[sz];
1711 unsigned char* pb = pbuf;
1712 Defs::const_iterator p;
1713 unsigned int i;
1714 for (p = this->defs_.begin(), i = 0;
1715 p != this->defs_.end();
1716 ++p, ++i)
1717 pb = (*p)->write<size, big_endian>(dynpool,
1718 i + 1 >= this->defs_.size(),
1719 pb);
1721 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1723 *pp = pbuf;
1724 *psize = sz;
1725 *pentries = this->defs_.size();
1728 // Return an allocated buffer holding the contents of the version
1729 // reference section.
1731 template<int size, bool big_endian>
1732 void
1733 Versions::need_section_contents(const Stringpool* dynpool,
1734 unsigned char** pp, unsigned int *psize,
1735 unsigned int *pentries) const
1737 gold_assert(this->is_finalized_);
1738 gold_assert(!this->needs_.empty());
1740 const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1741 const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1743 unsigned int sz = 0;
1744 for (Needs::const_iterator p = this->needs_.begin();
1745 p != this->needs_.end();
1746 ++p)
1748 sz += verneed_size;
1749 sz += (*p)->count_versions() * vernaux_size;
1752 unsigned char* pbuf = new unsigned char[sz];
1754 unsigned char* pb = pbuf;
1755 Needs::const_iterator p;
1756 unsigned int i;
1757 for (p = this->needs_.begin(), i = 0;
1758 p != this->needs_.end();
1759 ++p, ++i)
1760 pb = (*p)->write<size, big_endian>(dynpool,
1761 i + 1 >= this->needs_.size(),
1762 pb);
1764 gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1766 *pp = pbuf;
1767 *psize = sz;
1768 *pentries = this->needs_.size();
1771 // Instantiate the templates we need. We could use the configure
1772 // script to restrict this to only the ones for implemented targets.
1774 #ifdef HAVE_TARGET_32_LITTLE
1775 template
1776 class Sized_dynobj<32, false>;
1777 #endif
1779 #ifdef HAVE_TARGET_32_BIG
1780 template
1781 class Sized_dynobj<32, true>;
1782 #endif
1784 #ifdef HAVE_TARGET_64_LITTLE
1785 template
1786 class Sized_dynobj<64, false>;
1787 #endif
1789 #ifdef HAVE_TARGET_64_BIG
1790 template
1791 class Sized_dynobj<64, true>;
1792 #endif
1794 #ifdef HAVE_TARGET_32_LITTLE
1795 template
1796 void
1797 Versions::symbol_section_contents<32, false>(
1798 const Symbol_table*,
1799 const Stringpool*,
1800 unsigned int,
1801 const std::vector<Symbol*>&,
1802 unsigned char**,
1803 unsigned int*) const;
1804 #endif
1806 #ifdef HAVE_TARGET_32_BIG
1807 template
1808 void
1809 Versions::symbol_section_contents<32, true>(
1810 const Symbol_table*,
1811 const Stringpool*,
1812 unsigned int,
1813 const std::vector<Symbol*>&,
1814 unsigned char**,
1815 unsigned int*) const;
1816 #endif
1818 #ifdef HAVE_TARGET_64_LITTLE
1819 template
1820 void
1821 Versions::symbol_section_contents<64, false>(
1822 const Symbol_table*,
1823 const Stringpool*,
1824 unsigned int,
1825 const std::vector<Symbol*>&,
1826 unsigned char**,
1827 unsigned int*) const;
1828 #endif
1830 #ifdef HAVE_TARGET_64_BIG
1831 template
1832 void
1833 Versions::symbol_section_contents<64, true>(
1834 const Symbol_table*,
1835 const Stringpool*,
1836 unsigned int,
1837 const std::vector<Symbol*>&,
1838 unsigned char**,
1839 unsigned int*) const;
1840 #endif
1842 #ifdef HAVE_TARGET_32_LITTLE
1843 template
1844 void
1845 Versions::def_section_contents<32, false>(
1846 const Stringpool*,
1847 unsigned char**,
1848 unsigned int*,
1849 unsigned int*) const;
1850 #endif
1852 #ifdef HAVE_TARGET_32_BIG
1853 template
1854 void
1855 Versions::def_section_contents<32, true>(
1856 const Stringpool*,
1857 unsigned char**,
1858 unsigned int*,
1859 unsigned int*) const;
1860 #endif
1862 #ifdef HAVE_TARGET_64_LITTLE
1863 template
1864 void
1865 Versions::def_section_contents<64, false>(
1866 const Stringpool*,
1867 unsigned char**,
1868 unsigned int*,
1869 unsigned int*) const;
1870 #endif
1872 #ifdef HAVE_TARGET_64_BIG
1873 template
1874 void
1875 Versions::def_section_contents<64, true>(
1876 const Stringpool*,
1877 unsigned char**,
1878 unsigned int*,
1879 unsigned int*) const;
1880 #endif
1882 #ifdef HAVE_TARGET_32_LITTLE
1883 template
1884 void
1885 Versions::need_section_contents<32, false>(
1886 const Stringpool*,
1887 unsigned char**,
1888 unsigned int*,
1889 unsigned int*) const;
1890 #endif
1892 #ifdef HAVE_TARGET_32_BIG
1893 template
1894 void
1895 Versions::need_section_contents<32, true>(
1896 const Stringpool*,
1897 unsigned char**,
1898 unsigned int*,
1899 unsigned int*) const;
1900 #endif
1902 #ifdef HAVE_TARGET_64_LITTLE
1903 template
1904 void
1905 Versions::need_section_contents<64, false>(
1906 const Stringpool*,
1907 unsigned char**,
1908 unsigned int*,
1909 unsigned int*) const;
1910 #endif
1912 #ifdef HAVE_TARGET_64_BIG
1913 template
1914 void
1915 Versions::need_section_contents<64, true>(
1916 const Stringpool*,
1917 unsigned char**,
1918 unsigned int*,
1919 unsigned int*) const;
1920 #endif
1922 } // End namespace gold.