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