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