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