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