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