gas/
[external/binutils.git] / gold / dynobj.cc
1 // dynobj.cc -- dynamic object support for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <vector>
26 #include <cstring>
27
28 #include "elfcpp.h"
29 #include "parameters.h"
30 #include "symtab.h"
31 #include "dynobj.h"
32
33 namespace gold
34 {
35
36 // Class Dynobj.
37
38 // Return the string to use in a DT_NEEDED entry.
39
40 const char*
41 Dynobj::soname() const
42 {
43   if (!this->soname_.empty())
44     return this->soname_.c_str();
45   return this->name().c_str();
46 }
47
48 // Class Sized_dynobj.
49
50 template<int size, bool big_endian>
51 Sized_dynobj<size, big_endian>::Sized_dynobj(
52     const std::string& name,
53     Input_file* input_file,
54     off_t offset,
55     const elfcpp::Ehdr<size, big_endian>& ehdr)
56   : Dynobj(name, input_file, offset),
57     elf_file_(this, ehdr)
58 {
59 }
60
61 // Set up the object.
62
63 template<int size, bool big_endian>
64 void
65 Sized_dynobj<size, big_endian>::setup(
66     const elfcpp::Ehdr<size, big_endian>& ehdr)
67 {
68   this->set_target(ehdr.get_e_machine(), size, big_endian,
69                    ehdr.get_e_ident()[elfcpp::EI_OSABI],
70                    ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
71
72   const unsigned int shnum = this->elf_file_.shnum();
73   this->set_shnum(shnum);
74 }
75
76 // Find the SHT_DYNSYM section and the various version sections, and
77 // the dynamic section, given the section headers.
78
79 template<int size, bool big_endian>
80 void
81 Sized_dynobj<size, big_endian>::find_dynsym_sections(
82     const unsigned char* pshdrs,
83     unsigned int* pdynsym_shndx,
84     unsigned int* pversym_shndx,
85     unsigned int* pverdef_shndx,
86     unsigned int* pverneed_shndx,
87     unsigned int* pdynamic_shndx)
88 {
89   *pdynsym_shndx = -1U;
90   *pversym_shndx = -1U;
91   *pverdef_shndx = -1U;
92   *pverneed_shndx = -1U;
93   *pdynamic_shndx = -1U;
94
95   const unsigned int shnum = this->shnum();
96   const unsigned char* p = pshdrs;
97   for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
98     {
99       typename This::Shdr shdr(p);
100
101       unsigned int* pi;
102       switch (shdr.get_sh_type())
103         {
104         case elfcpp::SHT_DYNSYM:
105           pi = pdynsym_shndx;
106           break;
107         case elfcpp::SHT_GNU_versym:
108           pi = pversym_shndx;
109           break;
110         case elfcpp::SHT_GNU_verdef:
111           pi = pverdef_shndx;
112           break;
113         case elfcpp::SHT_GNU_verneed:
114           pi = pverneed_shndx;
115           break;
116         case elfcpp::SHT_DYNAMIC:
117           pi = pdynamic_shndx;
118           break;
119         default:
120           pi = NULL;
121           break;
122         }
123
124       if (pi == NULL)
125         continue;
126
127       if (*pi != -1U)
128         {
129           fprintf(stderr,
130                   _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
131                   program_name, this->name().c_str(), shdr.get_sh_type(),
132                   *pi, i);
133           gold_exit(false);
134         }
135
136       *pi = i;
137     }
138 }
139
140 // Read the contents of section SHNDX.  PSHDRS points to the section
141 // headers.  TYPE is the expected section type.  LINK is the expected
142 // section link.  Store the data in *VIEW and *VIEW_SIZE.  The
143 // section's sh_info field is stored in *VIEW_INFO.
144
145 template<int size, bool big_endian>
146 void
147 Sized_dynobj<size, big_endian>::read_dynsym_section(
148     const unsigned char* pshdrs,
149     unsigned int shndx,
150     elfcpp::SHT type,
151     unsigned int link,
152     File_view** view,
153     off_t* view_size,
154     unsigned int* view_info)
155 {
156   if (shndx == -1U)
157     {
158       *view = NULL;
159       *view_size = 0;
160       *view_info = 0;
161       return;
162     }
163
164   typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
165
166   gold_assert(shdr.get_sh_type() == type);
167
168   if (shdr.get_sh_link() != link)
169     {
170       fprintf(stderr,
171               _("%s: %s: unexpected link in section %u header: %u != %u\n"),
172               program_name, this->name().c_str(), shndx,
173               shdr.get_sh_link(), link);
174       gold_exit(false);
175     }
176
177   *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size(),
178                                  false);
179   *view_size = shdr.get_sh_size();
180   *view_info = shdr.get_sh_info();
181 }
182
183 // Set the soname field if this shared object has a DT_SONAME tag.
184 // PSHDRS points to the section headers.  DYNAMIC_SHNDX is the section
185 // index of the SHT_DYNAMIC section.  STRTAB_SHNDX, STRTAB, and
186 // STRTAB_SIZE are the section index and contents of a string table
187 // which may be the one associated with the SHT_DYNAMIC section.
188
189 template<int size, bool big_endian>
190 void
191 Sized_dynobj<size, big_endian>::set_soname(const unsigned char* pshdrs,
192                                            unsigned int dynamic_shndx,
193                                            unsigned int strtab_shndx,
194                                            const unsigned char* strtabu,
195                                            off_t strtab_size)
196 {
197   typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
198   gold_assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
199
200   const off_t dynamic_size = dynamicshdr.get_sh_size();
201   const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
202                                                  dynamic_size, false);
203
204   const unsigned int link = dynamicshdr.get_sh_link();
205   if (link != strtab_shndx)
206     {
207       if (link >= this->shnum())
208         {
209           fprintf(stderr,
210                   _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
211                   program_name, this->name().c_str(),
212                   dynamic_shndx, link);
213           gold_exit(false);
214         }
215
216       typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
217       if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
218         {
219           fprintf(stderr,
220                   _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
221                   program_name, this->name().c_str(),
222                   dynamic_shndx, link);
223           gold_exit(false);
224         }
225
226       strtab_size = strtabshdr.get_sh_size();
227       strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size, false);
228     }
229
230   for (const unsigned char* p = pdynamic;
231        p < pdynamic + dynamic_size;
232        p += This::dyn_size)
233     {
234       typename This::Dyn dyn(p);
235
236       if (dyn.get_d_tag() == elfcpp::DT_SONAME)
237         {
238           off_t val = dyn.get_d_val();
239           if (val >= strtab_size)
240             {
241               fprintf(stderr,
242                       _("%s: %s: DT_SONAME value out of range: "
243                         "%lld >= %lld\n"),
244                       program_name, this->name().c_str(),
245                       static_cast<long long>(val),
246                       static_cast<long long>(strtab_size));
247               gold_exit(false);
248             }
249
250           const char* strtab = reinterpret_cast<const char*>(strtabu);
251           this->set_soname_string(strtab + val);
252           return;
253         }
254
255       if (dyn.get_d_tag() == elfcpp::DT_NULL)
256         return;
257     }
258
259   fprintf(stderr, _("%s: %s: missing DT_NULL in dynamic segment\n"),
260           program_name, this->name().c_str());
261   gold_exit(false);
262 }
263
264 // Read the symbols and sections from a dynamic object.  We read the
265 // dynamic symbols, not the normal symbols.
266
267 template<int size, bool big_endian>
268 void
269 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
270 {
271   this->read_section_data(&this->elf_file_, sd);
272
273   const unsigned char* const pshdrs = sd->section_headers->data();
274
275   unsigned int dynsym_shndx;
276   unsigned int versym_shndx;
277   unsigned int verdef_shndx;
278   unsigned int verneed_shndx;
279   unsigned int dynamic_shndx;
280   this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
281                              &verdef_shndx, &verneed_shndx, &dynamic_shndx);
282
283   unsigned int strtab_shndx = -1U;
284
285   if (dynsym_shndx == -1U)
286     {
287       sd->symbols = NULL;
288       sd->symbols_size = 0;
289       sd->symbol_names = NULL;
290       sd->symbol_names_size = 0;
291     }
292   else
293     {
294       // Get the dynamic symbols.
295       typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
296       gold_assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
297
298       sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
299                                            dynsymshdr.get_sh_size(), false);
300       sd->symbols_size = dynsymshdr.get_sh_size();
301
302       // Get the symbol names.
303       strtab_shndx = dynsymshdr.get_sh_link();
304       if (strtab_shndx >= this->shnum())
305         {
306           fprintf(stderr,
307                   _("%s: %s: invalid dynamic symbol table name index: %u\n"),
308                   program_name, this->name().c_str(), strtab_shndx);
309           gold_exit(false);
310         }
311       typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
312       if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
313         {
314           fprintf(stderr,
315                   _("%s: %s: dynamic symbol table name section "
316                     "has wrong type: %u\n"),
317                   program_name, this->name().c_str(),
318                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
319           gold_exit(false);
320         }
321
322       sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
323                                                 strtabshdr.get_sh_size(),
324                                                 true);
325       sd->symbol_names_size = strtabshdr.get_sh_size();
326
327       // Get the version information.
328
329       unsigned int dummy;
330       this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
331                                 dynsym_shndx, &sd->versym, &sd->versym_size,
332                                 &dummy);
333
334       // We require that the version definition and need section link
335       // to the same string table as the dynamic symbol table.  This
336       // is not a technical requirement, but it always happens in
337       // practice.  We could change this if necessary.
338
339       this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
340                                 strtab_shndx, &sd->verdef, &sd->verdef_size,
341                                 &sd->verdef_info);
342
343       this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
344                                 strtab_shndx, &sd->verneed, &sd->verneed_size,
345                                 &sd->verneed_info);
346     }
347
348   // Read the SHT_DYNAMIC section to find whether this shared object
349   // has a DT_SONAME tag.  This doesn't really have anything to do
350   // with reading the symbols, but this is a convenient place to do
351   // it.
352   if (dynamic_shndx != -1U)
353     this->set_soname(pshdrs, dynamic_shndx, strtab_shndx,
354                      (sd->symbol_names == NULL
355                       ? NULL
356                       : sd->symbol_names->data()),
357                      sd->symbol_names_size);
358 }
359
360 // Lay out the input sections for a dynamic object.  We don't want to
361 // include sections from a dynamic object, so all that we actually do
362 // here is check for .gnu.warning sections.
363
364 template<int size, bool big_endian>
365 void
366 Sized_dynobj<size, big_endian>::do_layout(Symbol_table* symtab,
367                                           Layout*,
368                                           Read_symbols_data* sd)
369 {
370   const unsigned int shnum = this->shnum();
371   if (shnum == 0)
372     return;
373
374   // Get the section headers.
375   const unsigned char* pshdrs = sd->section_headers->data();
376
377   // Get the section names.
378   const unsigned char* pnamesu = sd->section_names->data();
379   const char* pnames = reinterpret_cast<const char*>(pnamesu);
380
381   // Skip the first, dummy, section.
382   pshdrs += This::shdr_size;
383   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
384     {
385       typename This::Shdr shdr(pshdrs);
386
387       if (shdr.get_sh_name() >= sd->section_names_size)
388         {
389           fprintf(stderr,
390                   _("%s: %s: bad section name offset for section %u: %lu\n"),
391                   program_name, this->name().c_str(), i,
392                   static_cast<unsigned long>(shdr.get_sh_name()));
393           gold_exit(false);
394         }
395
396       const char* name = pnames + shdr.get_sh_name();
397
398       this->handle_gnu_warning_section(name, i, symtab);
399     }
400
401   delete sd->section_headers;
402   sd->section_headers = NULL;
403   delete sd->section_names;
404   sd->section_names = NULL;
405 }
406
407 // Add an entry to the vector mapping version numbers to version
408 // strings.
409
410 template<int size, bool big_endian>
411 void
412 Sized_dynobj<size, big_endian>::set_version_map(
413     Version_map* version_map,
414     unsigned int ndx,
415     const char* name) const
416 {
417   if (ndx >= version_map->size())
418     version_map->resize(ndx + 1);
419   if ((*version_map)[ndx] != NULL)
420     {
421       fprintf(stderr, _("%s: %s: duplicate definition for version %u\n"),
422               program_name, this->name().c_str(), ndx);
423       gold_exit(false);
424     }
425   (*version_map)[ndx] = name;
426 }
427
428 // Add mappings for the version definitions to VERSION_MAP.
429
430 template<int size, bool big_endian>
431 void
432 Sized_dynobj<size, big_endian>::make_verdef_map(
433     Read_symbols_data* sd,
434     Version_map* version_map) const
435 {
436   if (sd->verdef == NULL)
437     return;
438
439   const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
440   off_t names_size = sd->symbol_names_size;
441
442   const unsigned char* pverdef = sd->verdef->data();
443   off_t verdef_size = sd->verdef_size;
444   const unsigned int count = sd->verdef_info;
445
446   const unsigned char* p = pverdef;
447   for (unsigned int i = 0; i < count; ++i)
448     {
449       elfcpp::Verdef<size, big_endian> verdef(p);
450
451       if (verdef.get_vd_version() != elfcpp::VER_DEF_CURRENT)
452         {
453           fprintf(stderr, _("%s: %s: unexpected verdef version %u\n"),
454                   program_name, this->name().c_str(), verdef.get_vd_version());
455           gold_exit(false);
456         }
457
458       const unsigned int vd_ndx = verdef.get_vd_ndx();
459
460       // The GNU linker clears the VERSYM_HIDDEN bit.  I'm not
461       // sure why.
462
463       // The first Verdaux holds the name of this version.  Subsequent
464       // ones are versions that this one depends upon, which we don't
465       // care about here.
466       const unsigned int vd_cnt = verdef.get_vd_cnt();
467       if (vd_cnt < 1)
468         {
469           fprintf(stderr, _("%s: %s: verdef vd_cnt field too small: %u\n"),
470                   program_name, this->name().c_str(), vd_cnt);
471           gold_exit(false);
472         }
473
474       const unsigned int vd_aux = verdef.get_vd_aux();
475       if ((p - pverdef) + vd_aux >= verdef_size)
476         {
477           fprintf(stderr,
478                   _("%s: %s: verdef vd_aux field out of range: %u\n"),
479                   program_name, this->name().c_str(), vd_aux);
480           gold_exit(false);
481         }
482
483       const unsigned char* pvda = p + vd_aux;
484       elfcpp::Verdaux<size, big_endian> verdaux(pvda);
485
486       const unsigned int vda_name = verdaux.get_vda_name();
487       if (vda_name >= names_size)
488         {
489           fprintf(stderr,
490                   _("%s: %s: verdaux vda_name field out of range: %u\n"),
491                   program_name, this->name().c_str(), vda_name);
492           gold_exit(false);
493         }
494
495       this->set_version_map(version_map, vd_ndx, names + vda_name);
496
497       const unsigned int vd_next = verdef.get_vd_next();
498       if ((p - pverdef) + vd_next >= verdef_size)
499         {
500           fprintf(stderr,
501                   _("%s: %s: verdef vd_next field out of range: %u\n"),
502                   program_name, this->name().c_str(), vd_next);
503           gold_exit(false);
504         }
505
506       p += vd_next;
507     }
508 }
509
510 // Add mappings for the required versions to VERSION_MAP.
511
512 template<int size, bool big_endian>
513 void
514 Sized_dynobj<size, big_endian>::make_verneed_map(
515     Read_symbols_data* sd,
516     Version_map* version_map) const
517 {
518   if (sd->verneed == NULL)
519     return;
520
521   const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
522   off_t names_size = sd->symbol_names_size;
523
524   const unsigned char* pverneed = sd->verneed->data();
525   const off_t verneed_size = sd->verneed_size;
526   const unsigned int count = sd->verneed_info;
527
528   const unsigned char* p = pverneed;
529   for (unsigned int i = 0; i < count; ++i)
530     {
531       elfcpp::Verneed<size, big_endian> verneed(p);
532
533       if (verneed.get_vn_version() != elfcpp::VER_NEED_CURRENT)
534         {
535           fprintf(stderr, _("%s: %s: unexpected verneed version %u\n"),
536                   program_name, this->name().c_str(),
537                   verneed.get_vn_version());
538           gold_exit(false);
539         }
540
541       const unsigned int vn_aux = verneed.get_vn_aux();
542
543       if ((p - pverneed) + vn_aux >= verneed_size)
544         {
545           fprintf(stderr,
546                   _("%s: %s: verneed vn_aux field out of range: %u\n"),
547                   program_name, this->name().c_str(), vn_aux);
548           gold_exit(false);
549         }
550
551       const unsigned int vn_cnt = verneed.get_vn_cnt();
552       const unsigned char* pvna = p + vn_aux;
553       for (unsigned int j = 0; j < vn_cnt; ++j)
554         {
555           elfcpp::Vernaux<size, big_endian> vernaux(pvna);
556
557           const unsigned int vna_name = vernaux.get_vna_name();
558           if (vna_name >= names_size)
559             {
560               fprintf(stderr,
561                       _("%s: %s: vernaux vna_name field "
562                         "out of range: %u\n"),
563                       program_name, this->name().c_str(), vna_name);
564               gold_exit(false);
565             }
566
567           this->set_version_map(version_map, vernaux.get_vna_other(),
568                                 names + vna_name);
569
570           const unsigned int vna_next = vernaux.get_vna_next();
571           if ((pvna - pverneed) + vna_next >= verneed_size)
572             {
573               fprintf(stderr,
574                       _("%s: %s: verneed vna_next field "
575                         "out of range: %u\n"),
576                       program_name, this->name().c_str(), vna_next);
577               gold_exit(false);
578             }
579
580           pvna += vna_next;
581         }
582
583       const unsigned int vn_next = verneed.get_vn_next();
584       if ((p - pverneed) + vn_next >= verneed_size)
585         {
586           fprintf(stderr,
587                   _("%s: %s: verneed vn_next field out of range: %u\n"),
588                   program_name, this->name().c_str(), vn_next);
589           gold_exit(false);
590         }
591
592       p += vn_next;
593     }
594 }
595
596 // Create a vector mapping version numbers to version strings.
597
598 template<int size, bool big_endian>
599 void
600 Sized_dynobj<size, big_endian>::make_version_map(
601     Read_symbols_data* sd,
602     Version_map* version_map) const
603 {
604   if (sd->verdef == NULL && sd->verneed == NULL)
605     return;
606
607   // A guess at the maximum version number we will see.  If this is
608   // wrong we will be less efficient but still correct.
609   version_map->reserve(sd->verdef_info + sd->verneed_info * 10);
610
611   this->make_verdef_map(sd, version_map);
612   this->make_verneed_map(sd, version_map);
613 }
614
615 // Add the dynamic symbols to the symbol table.
616
617 template<int size, bool big_endian>
618 void
619 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
620                                                Read_symbols_data* sd)
621 {
622   if (sd->symbols == NULL)
623     {
624       gold_assert(sd->symbol_names == NULL);
625       gold_assert(sd->versym == NULL && sd->verdef == NULL
626                   && sd->verneed == NULL);
627       return;
628     }
629
630   const int sym_size = This::sym_size;
631   const size_t symcount = sd->symbols_size / sym_size;
632   if (symcount * sym_size != sd->symbols_size)
633     {
634       fprintf(stderr,
635               _("%s: %s: size of dynamic symbols is not "
636                 "multiple of symbol size\n"),
637               program_name, this->name().c_str());
638       gold_exit(false);
639     }
640
641   Version_map version_map;
642   this->make_version_map(sd, &version_map);
643
644   const char* sym_names =
645     reinterpret_cast<const char*>(sd->symbol_names->data());
646   symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
647                           sym_names, sd->symbol_names_size,
648                           (sd->versym == NULL
649                            ? NULL
650                            : sd->versym->data()),
651                           sd->versym_size,
652                           &version_map);
653
654   delete sd->symbols;
655   sd->symbols = NULL;
656   delete sd->symbol_names;
657   sd->symbol_names = NULL;
658   if (sd->versym != NULL)
659     {
660       delete sd->versym;
661       sd->versym = NULL;
662     }
663   if (sd->verdef != NULL)
664     {
665       delete sd->verdef;
666       sd->verdef = NULL;
667     }
668   if (sd->verneed != NULL)
669     {
670       delete sd->verneed;
671       sd->verneed = NULL;
672     }
673 }
674
675 // Given a vector of hash codes, compute the number of hash buckets to
676 // use.
677
678 unsigned int
679 Dynobj::compute_bucket_count(const std::vector<uint32_t>& hashcodes,
680                              bool for_gnu_hash_table)
681 {
682   // FIXME: Implement optional hash table optimization.
683
684   // Array used to determine the number of hash table buckets to use
685   // based on the number of symbols there are.  If there are fewer
686   // than 3 symbols we use 1 bucket, fewer than 17 symbols we use 3
687   // buckets, fewer than 37 we use 17 buckets, and so forth.  We never
688   // use more than 32771 buckets.  This is straight from the old GNU
689   // linker.
690   static const unsigned int buckets[] =
691   {
692     1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
693     16411, 32771
694   };
695   const int buckets_count = sizeof buckets / sizeof buckets[0];
696
697   unsigned int symcount = hashcodes.size();
698   unsigned int ret = 1;
699   for (int i = 0; i < buckets_count; ++i)
700     {
701       if (symcount < buckets[i])
702         break;
703       ret = buckets[i];
704     }
705
706   if (for_gnu_hash_table && ret < 2)
707     ret = 2;
708
709   return ret;
710 }
711
712 // The standard ELF hash function.  This hash function must not
713 // change, as the dynamic linker uses it also.
714
715 uint32_t
716 Dynobj::elf_hash(const char* name)
717 {
718   const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
719   uint32_t h = 0;
720   unsigned char c;
721   while ((c = *nameu++) != '\0')
722     {
723       h = (h << 4) + c;
724       uint32_t g = h & 0xf0000000;
725       if (g != 0)
726         {
727           h ^= g >> 24;
728           // The ELF ABI says h &= ~g, but using xor is equivalent in
729           // this case (since g was set from h) and may save one
730           // instruction.
731           h ^= g;
732         }
733     }
734   return h;
735 }
736
737 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
738 // DYNSYMS is a vector with all the global dynamic symbols.
739 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
740 // symbol table.
741
742 void
743 Dynobj::create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
744                               unsigned int local_dynsym_count,
745                               unsigned char** pphash,
746                               unsigned int* phashlen)
747 {
748   unsigned int dynsym_count = dynsyms.size();
749
750   // Get the hash values for all the symbols.
751   std::vector<uint32_t> dynsym_hashvals(dynsym_count);
752   for (unsigned int i = 0; i < dynsym_count; ++i)
753     dynsym_hashvals[i] = Dynobj::elf_hash(dynsyms[i]->name());
754
755   const unsigned int bucketcount =
756     Dynobj::compute_bucket_count(dynsym_hashvals, false);
757
758   std::vector<uint32_t> bucket(bucketcount);
759   std::vector<uint32_t> chain(local_dynsym_count + dynsym_count);
760
761   for (unsigned int i = 0; i < dynsym_count; ++i)
762     {
763       unsigned int dynsym_index = dynsyms[i]->dynsym_index();
764       unsigned int bucketpos = dynsym_hashvals[i] % bucketcount;
765       chain[dynsym_index] = bucket[bucketpos];
766       bucket[bucketpos] = dynsym_index;
767     }
768
769   unsigned int hashlen = ((2
770                            + bucketcount
771                            + local_dynsym_count
772                            + dynsym_count)
773                           * 4);
774   unsigned char* phash = new unsigned char[hashlen];
775
776   if (parameters->is_big_endian())
777     {
778 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
779       Dynobj::sized_create_elf_hash_table<true>(bucket, chain, phash,
780                                                 hashlen);
781 #else
782       gold_unreachable();
783 #endif
784     }
785   else
786     {
787 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
788       Dynobj::sized_create_elf_hash_table<false>(bucket, chain, phash,
789                                                  hashlen);
790 #else
791       gold_unreachable();
792 #endif
793     }
794
795   *pphash = phash;
796   *phashlen = hashlen;
797 }
798
799 // Fill in an ELF hash table.
800
801 template<bool big_endian>
802 void
803 Dynobj::sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
804                                     const std::vector<uint32_t>& chain,
805                                     unsigned char* phash,
806                                     unsigned int hashlen)
807 {
808   unsigned char* p = phash;
809
810   const unsigned int bucketcount = bucket.size();
811   const unsigned int chaincount = chain.size();
812
813   elfcpp::Swap<32, big_endian>::writeval(p, bucketcount);
814   p += 4;
815   elfcpp::Swap<32, big_endian>::writeval(p, chaincount);
816   p += 4;
817
818   for (unsigned int i = 0; i < bucketcount; ++i)
819     {
820       elfcpp::Swap<32, big_endian>::writeval(p, bucket[i]);
821       p += 4;
822     }
823
824   for (unsigned int i = 0; i < chaincount; ++i)
825     {
826       elfcpp::Swap<32, big_endian>::writeval(p, chain[i]);
827       p += 4;
828     }
829
830   gold_assert(static_cast<unsigned int>(p - phash) == hashlen);
831 }
832
833 // The hash function used for the GNU hash table.  This hash function
834 // must not change, as the dynamic linker uses it also.
835
836 uint32_t
837 Dynobj::gnu_hash(const char* name)
838 {
839   const unsigned char* nameu = reinterpret_cast<const unsigned char*>(name);
840   uint32_t h = 5381;
841   unsigned char c;
842   while ((c = *nameu++) != '\0')
843     h = (h << 5) + h + c;
844   return h;
845 }
846
847 // Create a GNU hash table, setting *PPHASH and *PHASHLEN.  GNU hash
848 // tables are an extension to ELF which are recognized by the GNU
849 // dynamic linker.  They are referenced using dynamic tag DT_GNU_HASH.
850 // TARGET is the target.  DYNSYMS is a vector with all the global
851 // symbols which will be going into the dynamic symbol table.
852 // LOCAL_DYNSYM_COUNT is the number of local symbols in the dynamic
853 // symbol table.
854
855 void
856 Dynobj::create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
857                               unsigned int local_dynsym_count,
858                               unsigned char** pphash,
859                               unsigned int* phashlen)
860 {
861   const unsigned int count = dynsyms.size();
862
863   // Sort the dynamic symbols into two vectors.  Symbols which we do
864   // not want to put into the hash table we store into
865   // UNHASHED_DYNSYMS.  Symbols which we do want to store we put into
866   // HASHED_DYNSYMS.  DYNSYM_HASHVALS is parallel to HASHED_DYNSYMS,
867   // and records the hash codes.
868
869   std::vector<Symbol*> unhashed_dynsyms;
870   unhashed_dynsyms.reserve(count);
871
872   std::vector<Symbol*> hashed_dynsyms;
873   hashed_dynsyms.reserve(count);
874
875   std::vector<uint32_t> dynsym_hashvals;
876   dynsym_hashvals.reserve(count);
877   
878   for (unsigned int i = 0; i < count; ++i)
879     {
880       Symbol* sym = dynsyms[i];
881
882       // FIXME: Should put on unhashed_dynsyms if the symbol is
883       // hidden.
884       if (sym->is_undefined())
885         unhashed_dynsyms.push_back(sym);
886       else
887         {
888           hashed_dynsyms.push_back(sym);
889           dynsym_hashvals.push_back(Dynobj::gnu_hash(sym->name()));
890         }
891     }
892
893   // Put the unhashed symbols at the start of the global portion of
894   // the dynamic symbol table.
895   const unsigned int unhashed_count = unhashed_dynsyms.size();
896   unsigned int unhashed_dynsym_index = local_dynsym_count;
897   for (unsigned int i = 0; i < unhashed_count; ++i)
898     {
899       unhashed_dynsyms[i]->set_dynsym_index(unhashed_dynsym_index);
900       ++unhashed_dynsym_index;
901     }
902
903   // For the actual data generation we call out to a templatized
904   // function.
905   int size = parameters->get_size();
906   bool big_endian = parameters->is_big_endian();
907   if (size == 32)
908     {
909       if (big_endian)
910         {
911 #ifdef HAVE_TARGET_32_BIG
912           Dynobj::sized_create_gnu_hash_table<32, true>(hashed_dynsyms,
913                                                         dynsym_hashvals,
914                                                         unhashed_dynsym_index,
915                                                         pphash,
916                                                         phashlen);
917 #else
918           gold_unreachable();
919 #endif
920         }
921       else
922         {
923 #ifdef HAVE_TARGET_32_LITTLE
924           Dynobj::sized_create_gnu_hash_table<32, false>(hashed_dynsyms,
925                                                          dynsym_hashvals,
926                                                          unhashed_dynsym_index,
927                                                          pphash,
928                                                          phashlen);
929 #else
930           gold_unreachable();
931 #endif
932         }
933     }
934   else if (size == 64)
935     {
936       if (big_endian)
937         {
938 #ifdef HAVE_TARGET_64_BIG
939           Dynobj::sized_create_gnu_hash_table<64, true>(hashed_dynsyms,
940                                                         dynsym_hashvals,
941                                                         unhashed_dynsym_index,
942                                                         pphash,
943                                                         phashlen);
944 #else
945           gold_unreachable();
946 #endif
947         }
948       else
949         {
950 #ifdef HAVE_TARGET_64_LITTLE
951           Dynobj::sized_create_gnu_hash_table<64, false>(hashed_dynsyms,
952                                                          dynsym_hashvals,
953                                                          unhashed_dynsym_index,
954                                                          pphash,
955                                                          phashlen);
956 #else
957           gold_unreachable();
958 #endif
959         }
960     }
961   else
962     gold_unreachable();
963 }
964
965 // Create the actual data for a GNU hash table.  This is just a copy
966 // of the code from the old GNU linker.
967
968 template<int size, bool big_endian>
969 void
970 Dynobj::sized_create_gnu_hash_table(
971     const std::vector<Symbol*>& hashed_dynsyms,
972     const std::vector<uint32_t>& dynsym_hashvals,
973     unsigned int unhashed_dynsym_count,
974     unsigned char** pphash,
975     unsigned int* phashlen)
976 {
977   if (hashed_dynsyms.empty())
978     {
979       // Special case for the empty hash table.
980       unsigned int hashlen = 5 * 4 + size / 8;
981       unsigned char* phash = new unsigned char[hashlen];
982       // One empty bucket.
983       elfcpp::Swap<32, big_endian>::writeval(phash, 1);
984       // Symbol index above unhashed symbols.
985       elfcpp::Swap<32, big_endian>::writeval(phash + 4, unhashed_dynsym_count);
986       // One word for bitmask.
987       elfcpp::Swap<32, big_endian>::writeval(phash + 8, 1);
988       // Only bloom filter.
989       elfcpp::Swap<32, big_endian>::writeval(phash + 12, 0);
990       // No valid hashes.
991       elfcpp::Swap<size, big_endian>::writeval(phash + 16, 0);
992       // No hashes in only bucket.
993       elfcpp::Swap<32, big_endian>::writeval(phash + 16 + size / 8, 0);
994
995       *phashlen = hashlen;
996       *pphash = phash;
997
998       return;
999     }
1000
1001   const unsigned int bucketcount =
1002     Dynobj::compute_bucket_count(dynsym_hashvals, true);
1003
1004   const unsigned int nsyms = hashed_dynsyms.size();
1005
1006   uint32_t maskbitslog2 = 1;
1007   uint32_t x = nsyms >> 1;
1008   while (x != 0)
1009     {
1010       ++maskbitslog2;
1011       x >>= 1;
1012     }
1013   if (maskbitslog2 < 3)
1014     maskbitslog2 = 5;
1015   else if (((1U << (maskbitslog2 - 2)) & nsyms) != 0)
1016     maskbitslog2 += 3;
1017   else
1018     maskbitslog2 += 2;
1019
1020   uint32_t shift1;
1021   if (size == 32)
1022     shift1 = 5;
1023   else
1024     {
1025       if (maskbitslog2 == 5)
1026         maskbitslog2 = 6;
1027       shift1 = 6;
1028     }
1029   uint32_t mask = (1U << shift1) - 1U;
1030   uint32_t shift2 = maskbitslog2;
1031   uint32_t maskbits = 1U << maskbitslog2;
1032   uint32_t maskwords = 1U << (maskbitslog2 - shift1);
1033
1034   typedef typename elfcpp::Elf_types<size>::Elf_WXword Word;
1035   std::vector<Word> bitmask(maskwords);
1036   std::vector<uint32_t> counts(bucketcount);
1037   std::vector<uint32_t> indx(bucketcount);
1038   uint32_t symindx = unhashed_dynsym_count;
1039
1040   // Count the number of times each hash bucket is used.
1041   for (unsigned int i = 0; i < nsyms; ++i)
1042     ++counts[dynsym_hashvals[i] % bucketcount];
1043
1044   unsigned int cnt = symindx;
1045   for (unsigned int i = 0; i < bucketcount; ++i)
1046     {
1047       indx[i] = cnt;
1048       cnt += counts[i];
1049     }
1050
1051   unsigned int hashlen = (4 + bucketcount + nsyms) * 4;
1052   hashlen += maskbits / 8;
1053   unsigned char* phash = new unsigned char[hashlen];
1054
1055   elfcpp::Swap<32, big_endian>::writeval(phash, bucketcount);
1056   elfcpp::Swap<32, big_endian>::writeval(phash + 4, symindx);
1057   elfcpp::Swap<32, big_endian>::writeval(phash + 8, maskwords);
1058   elfcpp::Swap<32, big_endian>::writeval(phash + 12, shift2);
1059
1060   unsigned char* p = phash + 16 + maskbits / 8;
1061   for (unsigned int i = 0; i < bucketcount; ++i)
1062     {
1063       if (counts[i] == 0)
1064         elfcpp::Swap<32, big_endian>::writeval(p, 0);
1065       else
1066         elfcpp::Swap<32, big_endian>::writeval(p, indx[i]);
1067       p += 4;
1068     }
1069
1070   for (unsigned int i = 0; i < nsyms; ++i)
1071     {
1072       Symbol* sym = hashed_dynsyms[i];
1073       uint32_t hashval = dynsym_hashvals[i];
1074
1075       unsigned int bucket = hashval % bucketcount;
1076       unsigned int val = ((hashval >> shift1)
1077                           & ((maskbits >> shift1) - 1));
1078       bitmask[val] |= (static_cast<Word>(1U)) << (hashval & mask);
1079       bitmask[val] |= (static_cast<Word>(1U)) << ((hashval >> shift2) & mask);
1080       val = hashval & ~ 1U;
1081       if (counts[bucket] == 1)
1082         {
1083           // Last element terminates the chain.
1084           val |= 1;
1085         }
1086       elfcpp::Swap<32, big_endian>::writeval(p + (indx[bucket] - symindx) * 4,
1087                                              val);
1088       --counts[bucket];
1089
1090       sym->set_dynsym_index(indx[bucket]);
1091       ++indx[bucket];
1092     }
1093
1094   p = phash + 16;
1095   for (unsigned int i = 0; i < maskwords; ++i)
1096     {
1097       elfcpp::Swap<size, big_endian>::writeval(p, bitmask[i]);
1098       p += size / 8;
1099     }
1100
1101   *phashlen = hashlen;
1102   *pphash = phash;
1103 }
1104
1105 // Verdef methods.
1106
1107 // Write this definition to a buffer for the output section.
1108
1109 template<int size, bool big_endian>
1110 unsigned char*
1111 Verdef::write(const Stringpool* dynpool, bool is_last, unsigned char* pb
1112               ACCEPT_SIZE_ENDIAN) const
1113 {
1114   const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1115   const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1116
1117   elfcpp::Verdef_write<size, big_endian> vd(pb);
1118   vd.set_vd_version(elfcpp::VER_DEF_CURRENT);
1119   vd.set_vd_flags((this->is_base_ ? elfcpp::VER_FLG_BASE : 0)
1120                   | (this->is_weak_ ? elfcpp::VER_FLG_WEAK : 0));
1121   vd.set_vd_ndx(this->index());
1122   vd.set_vd_cnt(1 + this->deps_.size());
1123   vd.set_vd_hash(Dynobj::elf_hash(this->name()));
1124   vd.set_vd_aux(verdef_size);
1125   vd.set_vd_next(is_last
1126                  ? 0
1127                  : verdef_size + (1 + this->deps_.size()) * verdaux_size);
1128   pb += verdef_size;
1129
1130   elfcpp::Verdaux_write<size, big_endian> vda(pb);
1131   vda.set_vda_name(dynpool->get_offset(this->name()));
1132   vda.set_vda_next(this->deps_.empty() ? 0 : verdaux_size);
1133   pb += verdaux_size;
1134
1135   Deps::const_iterator p;
1136   unsigned int i;
1137   for (p = this->deps_.begin(), i = 0;
1138        p != this->deps_.end();
1139        ++p, ++i)
1140     {
1141       elfcpp::Verdaux_write<size, big_endian> vda(pb);
1142       vda.set_vda_name(dynpool->get_offset(*p));
1143       vda.set_vda_next(i + 1 >= this->deps_.size() ? 0 : verdaux_size);
1144       pb += verdaux_size;
1145     }
1146
1147   return pb;
1148 }
1149
1150 // Verneed methods.
1151
1152 Verneed::~Verneed()
1153 {
1154   for (Need_versions::iterator p = this->need_versions_.begin();
1155        p != this->need_versions_.end();
1156        ++p)
1157     delete *p;
1158 }
1159
1160 // Add a new version to this file reference.
1161
1162 Verneed_version*
1163 Verneed::add_name(const char* name)
1164 {
1165   Verneed_version* vv = new Verneed_version(name);
1166   this->need_versions_.push_back(vv);
1167   return vv;
1168 }
1169
1170 // Set the version indexes starting at INDEX.
1171
1172 unsigned int
1173 Verneed::finalize(unsigned int index)
1174 {
1175   for (Need_versions::iterator p = this->need_versions_.begin();
1176        p != this->need_versions_.end();
1177        ++p)
1178     {
1179       (*p)->set_index(index);
1180       ++index;
1181     }
1182   return index;
1183 }
1184
1185 // Write this list of referenced versions to a buffer for the output
1186 // section.
1187
1188 template<int size, bool big_endian>
1189 unsigned char*
1190 Verneed::write(const Stringpool* dynpool, bool is_last,
1191                unsigned char* pb ACCEPT_SIZE_ENDIAN) const
1192 {
1193   const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1194   const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1195
1196   elfcpp::Verneed_write<size, big_endian> vn(pb);
1197   vn.set_vn_version(elfcpp::VER_NEED_CURRENT);
1198   vn.set_vn_cnt(this->need_versions_.size());
1199   vn.set_vn_file(dynpool->get_offset(this->filename()));
1200   vn.set_vn_aux(verneed_size);
1201   vn.set_vn_next(is_last
1202                  ? 0
1203                  : verneed_size + this->need_versions_.size() * vernaux_size);
1204   pb += verneed_size;
1205
1206   Need_versions::const_iterator p;
1207   unsigned int i;
1208   for (p = this->need_versions_.begin(), i = 0;
1209        p != this->need_versions_.end();
1210        ++p, ++i)
1211     {
1212       elfcpp::Vernaux_write<size, big_endian> vna(pb);
1213       vna.set_vna_hash(Dynobj::elf_hash((*p)->version()));
1214       // FIXME: We need to sometimes set VER_FLG_WEAK here.
1215       vna.set_vna_flags(0);
1216       vna.set_vna_other((*p)->index());
1217       vna.set_vna_name(dynpool->get_offset((*p)->version()));
1218       vna.set_vna_next(i + 1 >= this->need_versions_.size()
1219                        ? 0
1220                        : vernaux_size);
1221       pb += vernaux_size;
1222     }
1223
1224   return pb;
1225 }
1226
1227 // Versions methods.
1228
1229 Versions::~Versions()
1230 {
1231   for (Defs::iterator p = this->defs_.begin();
1232        p != this->defs_.end();
1233        ++p)
1234     delete *p;
1235
1236   for (Needs::iterator p = this->needs_.begin();
1237        p != this->needs_.end();
1238        ++p)
1239     delete *p;
1240 }
1241
1242 // Record version information for a symbol going into the dynamic
1243 // symbol table.
1244
1245 void
1246 Versions::record_version(const General_options* options,
1247                          Stringpool* dynpool, const Symbol* sym)
1248 {
1249   gold_assert(!this->is_finalized_);
1250   gold_assert(sym->version() != NULL);
1251
1252   Stringpool::Key version_key;
1253   const char* version = dynpool->add(sym->version(), &version_key);
1254
1255   if (!sym->is_from_dynobj())
1256     {
1257       if (parameters->output_is_shared())
1258         this->add_def(options, sym, version, version_key);
1259     }
1260   else
1261     {
1262       // This is a version reference.
1263
1264       Object* object = sym->object();
1265       gold_assert(object->is_dynamic());
1266       Dynobj* dynobj = static_cast<Dynobj*>(object);
1267
1268       this->add_need(dynpool, dynobj->soname(), version, version_key);
1269     }
1270 }
1271
1272 // We've found a symbol SYM defined in version VERSION.
1273
1274 void
1275 Versions::add_def(const General_options* options, const Symbol* sym,
1276                   const char* version, Stringpool::Key version_key)
1277 {
1278   Key k(version_key, 0);
1279   Version_base* const vbnull = NULL;
1280   std::pair<Version_table::iterator, bool> ins =
1281     this->version_table_.insert(std::make_pair(k, vbnull));
1282
1283   if (!ins.second)
1284     {
1285       // We already have an entry for this version.
1286       Version_base* vb = ins.first->second;
1287
1288       // We have now seen a symbol in this version, so it is not
1289       // weak.
1290       vb->clear_weak();
1291
1292       // FIXME: When we support version scripts, we will need to
1293       // check whether this symbol should be forced local.
1294     }
1295   else
1296     {
1297       // If we are creating a shared object, it is an error to
1298       // find a definition of a symbol with a version which is not
1299       // in the version script.
1300       if (parameters->output_is_shared())
1301         {
1302           fprintf(stderr, _("%s: symbol %s has undefined version %s\n"),
1303                   program_name, sym->name(), version);
1304           gold_exit(false);
1305         }
1306
1307       // If this is the first version we are defining, first define
1308       // the base version.  FIXME: Should use soname here when
1309       // creating a shared object.
1310       Verdef* vdbase = new Verdef(options->output_file_name(), true, false,
1311                                   true);
1312       this->defs_.push_back(vdbase);
1313
1314       // When creating a regular executable, automatically define
1315       // a new version.
1316       Verdef* vd = new Verdef(version, false, false, false);
1317       this->defs_.push_back(vd);
1318       ins.first->second = vd;
1319     }
1320 }
1321
1322 // Add a reference to version NAME in file FILENAME.
1323
1324 void
1325 Versions::add_need(Stringpool* dynpool, const char* filename, const char* name,
1326                    Stringpool::Key name_key)
1327 {
1328   Stringpool::Key filename_key;
1329   filename = dynpool->add(filename, &filename_key);
1330
1331   Key k(name_key, filename_key);
1332   Version_base* const vbnull = NULL;
1333   std::pair<Version_table::iterator, bool> ins =
1334     this->version_table_.insert(std::make_pair(k, vbnull));
1335
1336   if (!ins.second)
1337     {
1338       // We already have an entry for this filename/version.
1339       return;
1340     }
1341
1342   // See whether we already have this filename.  We don't expect many
1343   // version references, so we just do a linear search.  This could be
1344   // replaced by a hash table.
1345   Verneed* vn = NULL;
1346   for (Needs::iterator p = this->needs_.begin();
1347        p != this->needs_.end();
1348        ++p)
1349     {
1350       if ((*p)->filename() == filename)
1351         {
1352           vn = *p;
1353           break;
1354         }
1355     }
1356
1357   if (vn == NULL)
1358     {
1359       // We have a new filename.
1360       vn = new Verneed(filename);
1361       this->needs_.push_back(vn);
1362     }
1363
1364   ins.first->second = vn->add_name(name);
1365 }
1366
1367 // Set the version indexes.  Create a new dynamic version symbol for
1368 // each new version definition.
1369
1370 unsigned int
1371 Versions::finalize(const Target* target, Symbol_table* symtab,
1372                    unsigned int dynsym_index, std::vector<Symbol*>* syms)
1373 {
1374   gold_assert(!this->is_finalized_);
1375
1376   unsigned int vi = 1;
1377
1378   for (Defs::iterator p = this->defs_.begin();
1379        p != this->defs_.end();
1380        ++p)
1381     {
1382       (*p)->set_index(vi);
1383       ++vi;
1384
1385       // Create a version symbol if necessary.
1386       if (!(*p)->is_symbol_created())
1387         {
1388           Symbol* vsym = symtab->define_as_constant(target, (*p)->name(),
1389                                                     (*p)->name(), 0, 0,
1390                                                     elfcpp::STT_OBJECT,
1391                                                     elfcpp::STB_GLOBAL,
1392                                                     elfcpp::STV_DEFAULT, 0,
1393                                                     false);
1394           vsym->set_needs_dynsym_entry();
1395           vsym->set_dynsym_index(dynsym_index);
1396           ++dynsym_index;
1397           syms->push_back(vsym);
1398           // The name is already in the dynamic pool.
1399         }
1400     }
1401
1402   // Index 1 is used for global symbols.
1403   if (vi == 1)
1404     {
1405       gold_assert(this->defs_.empty());
1406       vi = 2;
1407     }
1408
1409   for (Needs::iterator p = this->needs_.begin();
1410        p != this->needs_.end();
1411        ++p)
1412     vi = (*p)->finalize(vi);
1413
1414   this->is_finalized_ = true;
1415
1416   return dynsym_index;
1417 }
1418
1419 // Return the version index to use for a symbol.  This does two hash
1420 // table lookups: one in DYNPOOL and one in this->version_table_.
1421 // Another approach alternative would be store a pointer in SYM, which
1422 // would increase the size of the symbol table.  Or perhaps we could
1423 // use a hash table from dynamic symbol pointer values to Version_base
1424 // pointers.
1425
1426 unsigned int
1427 Versions::version_index(const Stringpool* dynpool, const Symbol* sym) const
1428 {
1429   Stringpool::Key version_key;
1430   const char* version = dynpool->find(sym->version(), &version_key);
1431   gold_assert(version != NULL);
1432
1433   Key k;
1434   if (!sym->is_from_dynobj())
1435     {
1436       if (!parameters->output_is_shared())
1437         return elfcpp::VER_NDX_GLOBAL;
1438       k = Key(version_key, 0);
1439     }
1440   else
1441     {
1442       Object* object = sym->object();
1443       gold_assert(object->is_dynamic());
1444       Dynobj* dynobj = static_cast<Dynobj*>(object);
1445
1446       Stringpool::Key filename_key;
1447       const char* filename = dynpool->find(dynobj->soname(), &filename_key);
1448       gold_assert(filename != NULL);
1449
1450       k = Key(version_key, filename_key);
1451     }
1452
1453   Version_table::const_iterator p = this->version_table_.find(k);
1454   gold_assert(p != this->version_table_.end());
1455
1456   return p->second->index();
1457 }
1458
1459 // Return an allocated buffer holding the contents of the symbol
1460 // version section.
1461
1462 template<int size, bool big_endian>
1463 void
1464 Versions::symbol_section_contents(const Stringpool* dynpool,
1465                                   unsigned int local_symcount,
1466                                   const std::vector<Symbol*>& syms,
1467                                   unsigned char** pp,
1468                                   unsigned int* psize
1469                                   ACCEPT_SIZE_ENDIAN) const
1470 {
1471   gold_assert(this->is_finalized_);
1472
1473   unsigned int sz = (local_symcount + syms.size()) * 2;
1474   unsigned char* pbuf = new unsigned char[sz];
1475
1476   for (unsigned int i = 0; i < local_symcount; ++i)
1477     elfcpp::Swap<16, big_endian>::writeval(pbuf + i * 2,
1478                                            elfcpp::VER_NDX_LOCAL);
1479
1480   for (std::vector<Symbol*>::const_iterator p = syms.begin();
1481        p != syms.end();
1482        ++p)
1483     {
1484       unsigned int version_index;
1485       const char* version = (*p)->version();
1486       if (version == NULL)
1487         version_index = elfcpp::VER_NDX_GLOBAL;
1488       else
1489         version_index = this->version_index(dynpool, *p);
1490       elfcpp::Swap<16, big_endian>::writeval(pbuf + (*p)->dynsym_index() * 2,
1491                                              version_index);
1492     }
1493
1494   *pp = pbuf;
1495   *psize = sz;
1496 }
1497
1498 // Return an allocated buffer holding the contents of the version
1499 // definition section.
1500
1501 template<int size, bool big_endian>
1502 void
1503 Versions::def_section_contents(const Stringpool* dynpool,
1504                                unsigned char** pp, unsigned int* psize,
1505                                unsigned int* pentries
1506                                ACCEPT_SIZE_ENDIAN) const
1507 {
1508   gold_assert(this->is_finalized_);
1509   gold_assert(!this->defs_.empty());
1510
1511   const int verdef_size = elfcpp::Elf_sizes<size>::verdef_size;
1512   const int verdaux_size = elfcpp::Elf_sizes<size>::verdaux_size;
1513
1514   unsigned int sz = 0;
1515   for (Defs::const_iterator p = this->defs_.begin();
1516        p != this->defs_.end();
1517        ++p)
1518     {
1519       sz += verdef_size + verdaux_size;
1520       sz += (*p)->count_dependencies() * verdaux_size;
1521     }
1522
1523   unsigned char* pbuf = new unsigned char[sz];
1524
1525   unsigned char* pb = pbuf;
1526   Defs::const_iterator p;
1527   unsigned int i;
1528   for (p = this->defs_.begin(), i = 0;
1529        p != this->defs_.end();
1530        ++p, ++i)
1531     pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1532             dynpool, i + 1 >= this->defs_.size(), pb
1533             SELECT_SIZE_ENDIAN(size, big_endian));
1534
1535   gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1536
1537   *pp = pbuf;
1538   *psize = sz;
1539   *pentries = this->defs_.size();
1540 }
1541
1542 // Return an allocated buffer holding the contents of the version
1543 // reference section.
1544
1545 template<int size, bool big_endian>
1546 void
1547 Versions::need_section_contents(const Stringpool* dynpool,
1548                                 unsigned char** pp, unsigned int *psize,
1549                                 unsigned int *pentries
1550                                 ACCEPT_SIZE_ENDIAN) const
1551 {
1552   gold_assert(this->is_finalized_);
1553   gold_assert(!this->needs_.empty());
1554
1555   const int verneed_size = elfcpp::Elf_sizes<size>::verneed_size;
1556   const int vernaux_size = elfcpp::Elf_sizes<size>::vernaux_size;
1557
1558   unsigned int sz = 0;
1559   for (Needs::const_iterator p = this->needs_.begin();
1560        p != this->needs_.end();
1561        ++p)
1562     {
1563       sz += verneed_size;
1564       sz += (*p)->count_versions() * vernaux_size;
1565     }
1566
1567   unsigned char* pbuf = new unsigned char[sz];
1568
1569   unsigned char* pb = pbuf;
1570   Needs::const_iterator p;
1571   unsigned int i;
1572   for (p = this->needs_.begin(), i = 0;
1573        p != this->needs_.end();
1574        ++p, ++i)
1575     pb = (*p)->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1576             dynpool, i + 1 >= this->needs_.size(), pb
1577             SELECT_SIZE_ENDIAN(size, big_endian));
1578
1579   gold_assert(static_cast<unsigned int>(pb - pbuf) == sz);
1580
1581   *pp = pbuf;
1582   *psize = sz;
1583   *pentries = this->needs_.size();
1584 }
1585
1586 // Instantiate the templates we need.  We could use the configure
1587 // script to restrict this to only the ones for implemented targets.
1588
1589 #ifdef HAVE_TARGET_32_LITTLE
1590 template
1591 class Sized_dynobj<32, false>;
1592 #endif
1593
1594 #ifdef HAVE_TARGET_32_BIG
1595 template
1596 class Sized_dynobj<32, true>;
1597 #endif
1598
1599 #ifdef HAVE_TARGET_64_LITTLE
1600 template
1601 class Sized_dynobj<64, false>;
1602 #endif
1603
1604 #ifdef HAVE_TARGET_64_BIG
1605 template
1606 class Sized_dynobj<64, true>;
1607 #endif
1608
1609 #ifdef HAVE_TARGET_32_LITTLE
1610 template
1611 void
1612 Versions::symbol_section_contents<32, false>(
1613     const Stringpool*,
1614     unsigned int,
1615     const std::vector<Symbol*>&,
1616     unsigned char**,
1617     unsigned int*
1618     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1619 #endif
1620
1621 #ifdef HAVE_TARGET_32_BIG
1622 template
1623 void
1624 Versions::symbol_section_contents<32, true>(
1625     const Stringpool*,
1626     unsigned int,
1627     const std::vector<Symbol*>&,
1628     unsigned char**,
1629     unsigned int*
1630     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1631 #endif
1632
1633 #ifdef HAVE_TARGET_64_LITTLE
1634 template
1635 void
1636 Versions::symbol_section_contents<64, false>(
1637     const Stringpool*,
1638     unsigned int,
1639     const std::vector<Symbol*>&,
1640     unsigned char**,
1641     unsigned int*
1642     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1643 #endif
1644
1645 #ifdef HAVE_TARGET_64_BIG
1646 template
1647 void
1648 Versions::symbol_section_contents<64, true>(
1649     const Stringpool*,
1650     unsigned int,
1651     const std::vector<Symbol*>&,
1652     unsigned char**,
1653     unsigned int*
1654     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1655 #endif
1656
1657 #ifdef HAVE_TARGET_32_LITTLE
1658 template
1659 void
1660 Versions::def_section_contents<32, false>(
1661     const Stringpool*,
1662     unsigned char**,
1663     unsigned int*,
1664     unsigned int*
1665     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1666 #endif
1667
1668 #ifdef HAVE_TARGET_32_BIG
1669 template
1670 void
1671 Versions::def_section_contents<32, true>(
1672     const Stringpool*,
1673     unsigned char**,
1674     unsigned int*,
1675     unsigned int*
1676     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1677 #endif
1678
1679 #ifdef HAVE_TARGET_64_LITTLE
1680 template
1681 void
1682 Versions::def_section_contents<64, false>(
1683     const Stringpool*,
1684     unsigned char**,
1685     unsigned int*,
1686     unsigned int*
1687     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1688 #endif
1689
1690 #ifdef HAVE_TARGET_64_BIG
1691 template
1692 void
1693 Versions::def_section_contents<64, true>(
1694     const Stringpool*,
1695     unsigned char**,
1696     unsigned int*,
1697     unsigned int*
1698     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1699 #endif
1700
1701 #ifdef HAVE_TARGET_32_LITTLE
1702 template
1703 void
1704 Versions::need_section_contents<32, false>(
1705     const Stringpool*,
1706     unsigned char**,
1707     unsigned int*,
1708     unsigned int*
1709     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, false)) const;
1710 #endif
1711
1712 #ifdef HAVE_TARGET_32_BIG
1713 template
1714 void
1715 Versions::need_section_contents<32, true>(
1716     const Stringpool*,
1717     unsigned char**,
1718     unsigned int*,
1719     unsigned int*
1720     ACCEPT_SIZE_ENDIAN_EXPLICIT(32, true)) const;
1721 #endif
1722
1723 #ifdef HAVE_TARGET_64_LITTLE
1724 template
1725 void
1726 Versions::need_section_contents<64, false>(
1727     const Stringpool*,
1728     unsigned char**,
1729     unsigned int*,
1730     unsigned int*
1731     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, false)) const;
1732 #endif
1733
1734 #ifdef HAVE_TARGET_64_BIG
1735 template
1736 void
1737 Versions::need_section_contents<64, true>(
1738     const Stringpool*,
1739     unsigned char**,
1740     unsigned int*,
1741     unsigned int*
1742     ACCEPT_SIZE_ENDIAN_EXPLICIT(64, true)) const;
1743 #endif
1744
1745 } // End namespace gold.