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