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