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