More dynamic object support, initial scripting support.
[deliverable/binutils-gdb.git] / gold / dynobj.cc
1 // dynobj.cc -- dynamic object support for gold
2
3 #include "gold.h"
4
5 #include <vector>
6 #include <cstring>
7
8 #include "symtab.h"
9 #include "dynobj.h"
10
11 namespace gold
12 {
13
14 // Class Sized_dynobj.
15
16 template<int size, bool big_endian>
17 Sized_dynobj<size, big_endian>::Sized_dynobj(
18 const std::string& name,
19 Input_file* input_file,
20 off_t offset,
21 const elfcpp::Ehdr<size, big_endian>& ehdr)
22 : Dynobj(name, input_file, offset),
23 elf_file_(this, ehdr),
24 soname_()
25 {
26 }
27
28 // Set up the object.
29
30 template<int size, bool big_endian>
31 void
32 Sized_dynobj<size, big_endian>::setup(
33 const elfcpp::Ehdr<size, big_endian>& ehdr)
34 {
35 this->set_target(ehdr.get_e_machine(), size, big_endian,
36 ehdr.get_e_ident()[elfcpp::EI_OSABI],
37 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
38
39 const unsigned int shnum = this->elf_file_.shnum();
40 this->set_shnum(shnum);
41 }
42
43 // Find the SHT_DYNSYM section and the various version sections, and
44 // the dynamic section, given the section headers.
45
46 template<int size, bool big_endian>
47 void
48 Sized_dynobj<size, big_endian>::find_dynsym_sections(
49 const unsigned char* pshdrs,
50 unsigned int* pdynsym_shndx,
51 unsigned int* pversym_shndx,
52 unsigned int* pverdef_shndx,
53 unsigned int* pverneed_shndx,
54 unsigned int* pdynamic_shndx)
55 {
56 *pdynsym_shndx = -1U;
57 *pversym_shndx = -1U;
58 *pverdef_shndx = -1U;
59 *pverneed_shndx = -1U;
60 *pdynamic_shndx = -1U;
61
62 const unsigned int shnum = this->shnum();
63 const unsigned char* p = pshdrs;
64 for (unsigned int i = 0; i < shnum; ++i, p += This::shdr_size)
65 {
66 typename This::Shdr shdr(p);
67
68 unsigned int* pi;
69 switch (shdr.get_sh_type())
70 {
71 case elfcpp::SHT_DYNSYM:
72 pi = pdynsym_shndx;
73 break;
74 case elfcpp::SHT_GNU_versym:
75 pi = pversym_shndx;
76 break;
77 case elfcpp::SHT_GNU_verdef:
78 pi = pverdef_shndx;
79 break;
80 case elfcpp::SHT_GNU_verneed:
81 pi = pverneed_shndx;
82 break;
83 case elfcpp::SHT_DYNAMIC:
84 pi = pdynamic_shndx;
85 break;
86 default:
87 pi = NULL;
88 break;
89 }
90
91 if (pi == NULL)
92 continue;
93
94 if (*pi != -1U)
95 {
96 fprintf(stderr,
97 _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
98 program_name, this->name().c_str(), shdr.get_sh_type(),
99 *pi, i);
100 gold_exit(false);
101 }
102
103 *pi = i;
104 }
105 }
106
107 // Read the contents of section SHNDX. PSHDRS points to the section
108 // headers. TYPE is the expected section type. LINK is the expected
109 // section link. Store the data in *VIEW and *VIEW_SIZE. The
110 // section's sh_info field is stored in *VIEW_INFO.
111
112 template<int size, bool big_endian>
113 void
114 Sized_dynobj<size, big_endian>::read_dynsym_section(
115 const unsigned char* pshdrs,
116 unsigned int shndx,
117 elfcpp::SHT type,
118 unsigned int link,
119 File_view** view,
120 off_t* view_size,
121 unsigned int* view_info)
122 {
123 if (shndx == -1U)
124 {
125 *view = NULL;
126 *view_size = 0;
127 *view_info = 0;
128 return;
129 }
130
131 typename This::Shdr shdr(pshdrs + shndx * This::shdr_size);
132
133 assert(shdr.get_sh_type() == type);
134
135 if (shdr.get_sh_link() != link)
136 {
137 fprintf(stderr,
138 _("%s: %s: unexpected link in section %u header: %u != %u\n"),
139 program_name, this->name().c_str(), shndx,
140 shdr.get_sh_link(), link);
141 gold_exit(false);
142 }
143
144 *view = this->get_lasting_view(shdr.get_sh_offset(), shdr.get_sh_size());
145 *view_size = shdr.get_sh_size();
146 *view_info = shdr.get_sh_info();
147 }
148
149 // Set soname_ if this shared object has a DT_SONAME tag. PSHDRS
150 // points to the section headers. DYNAMIC_SHNDX is the section index
151 // of the SHT_DYNAMIC section. STRTAB_SHNDX, STRTAB, and STRTAB_SIZE
152 // are the section index and contents of a string table which may be
153 // the one associated with the SHT_DYNAMIC section.
154
155 template<int size, bool big_endian>
156 void
157 Sized_dynobj<size, big_endian>::set_soname(const unsigned char* pshdrs,
158 unsigned int dynamic_shndx,
159 unsigned int strtab_shndx,
160 const unsigned char* strtabu,
161 off_t strtab_size)
162 {
163 typename This::Shdr dynamicshdr(pshdrs + dynamic_shndx * This::shdr_size);
164 assert(dynamicshdr.get_sh_type() == elfcpp::SHT_DYNAMIC);
165
166 const off_t dynamic_size = dynamicshdr.get_sh_size();
167 const unsigned char* pdynamic = this->get_view(dynamicshdr.get_sh_offset(),
168 dynamic_size);
169
170 const unsigned int link = dynamicshdr.get_sh_link();
171 if (link != strtab_shndx)
172 {
173 if (link >= this->shnum())
174 {
175 fprintf(stderr,
176 _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
177 program_name, this->name().c_str(),
178 dynamic_shndx, link);
179 gold_exit(false);
180 }
181
182 typename This::Shdr strtabshdr(pshdrs + link * This::shdr_size);
183 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
184 {
185 fprintf(stderr,
186 _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
187 program_name, this->name().c_str(),
188 dynamic_shndx, link);
189 gold_exit(false);
190 }
191
192 strtab_size = strtabshdr.get_sh_size();
193 strtabu = this->get_view(strtabshdr.get_sh_offset(), strtab_size);
194 }
195
196 for (const unsigned char* p = pdynamic;
197 p < pdynamic + dynamic_size;
198 p += This::dyn_size)
199 {
200 typename This::Dyn dyn(p);
201
202 if (dyn.get_d_tag() == elfcpp::DT_SONAME)
203 {
204 off_t val = dyn.get_d_val();
205 if (val >= strtab_size)
206 {
207 fprintf(stderr,
208 _("%s: %s: DT_SONAME value out of range: "
209 "%lld >= %lld\n"),
210 program_name, this->name().c_str(),
211 static_cast<long long>(val),
212 static_cast<long long>(strtab_size));
213 gold_exit(false);
214 }
215
216 const char* strtab = reinterpret_cast<const char*>(strtabu);
217 this->soname_ = std::string(strtab + val);
218 return;
219 }
220
221 if (dyn.get_d_tag() == elfcpp::DT_NULL)
222 return;
223 }
224
225 fprintf(stderr, _("%s: %s: missing DT_NULL in dynamic segment\n"),
226 program_name, this->name().c_str());
227 gold_exit(false);
228 }
229
230 // Read the symbols and sections from a dynamic object. We read the
231 // dynamic symbols, not the normal symbols.
232
233 template<int size, bool big_endian>
234 void
235 Sized_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
236 {
237 this->read_section_data(&this->elf_file_, sd);
238
239 const unsigned char* const pshdrs = sd->section_headers->data();
240
241 unsigned int dynsym_shndx;
242 unsigned int versym_shndx;
243 unsigned int verdef_shndx;
244 unsigned int verneed_shndx;
245 unsigned int dynamic_shndx;
246 this->find_dynsym_sections(pshdrs, &dynsym_shndx, &versym_shndx,
247 &verdef_shndx, &verneed_shndx, &dynamic_shndx);
248
249 unsigned int strtab_shndx = -1U;
250
251 if (dynsym_shndx == -1U)
252 {
253 sd->symbols = NULL;
254 sd->symbols_size = 0;
255 sd->symbol_names = NULL;
256 sd->symbol_names_size = 0;
257 }
258 else
259 {
260 // Get the dynamic symbols.
261 typename This::Shdr dynsymshdr(pshdrs + dynsym_shndx * This::shdr_size);
262 assert(dynsymshdr.get_sh_type() == elfcpp::SHT_DYNSYM);
263
264 sd->symbols = this->get_lasting_view(dynsymshdr.get_sh_offset(),
265 dynsymshdr.get_sh_size());
266 sd->symbols_size = dynsymshdr.get_sh_size();
267
268 // Get the symbol names.
269 strtab_shndx = dynsymshdr.get_sh_link();
270 if (strtab_shndx >= this->shnum())
271 {
272 fprintf(stderr,
273 _("%s: %s: invalid dynamic symbol table name index: %u\n"),
274 program_name, this->name().c_str(), strtab_shndx);
275 gold_exit(false);
276 }
277 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
278 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
279 {
280 fprintf(stderr,
281 _("%s: %s: dynamic symbol table name section "
282 "has wrong type: %u\n"),
283 program_name, this->name().c_str(),
284 static_cast<unsigned int>(strtabshdr.get_sh_type()));
285 gold_exit(false);
286 }
287
288 sd->symbol_names = this->get_lasting_view(strtabshdr.get_sh_offset(),
289 strtabshdr.get_sh_size());
290 sd->symbol_names_size = strtabshdr.get_sh_size();
291
292 // Get the version information.
293
294 unsigned int dummy;
295 this->read_dynsym_section(pshdrs, versym_shndx, elfcpp::SHT_GNU_versym,
296 dynsym_shndx, &sd->versym, &sd->versym_size,
297 &dummy);
298
299 // We require that the version definition and need section link
300 // to the same string table as the dynamic symbol table. This
301 // is not a technical requirement, but it always happens in
302 // practice. We could change this if necessary.
303
304 this->read_dynsym_section(pshdrs, verdef_shndx, elfcpp::SHT_GNU_verdef,
305 strtab_shndx, &sd->verdef, &sd->verdef_size,
306 &sd->verdef_info);
307
308 this->read_dynsym_section(pshdrs, verneed_shndx, elfcpp::SHT_GNU_verneed,
309 strtab_shndx, &sd->verneed, &sd->verneed_size,
310 &sd->verneed_info);
311 }
312
313 // Read the SHT_DYNAMIC section to find whether this shared object
314 // has a DT_SONAME tag. This doesn't really have anything to do
315 // with reading the symbols, but this is a convenient place to do
316 // it.
317 if (dynamic_shndx != -1U)
318 this->set_soname(pshdrs, dynamic_shndx, strtab_shndx,
319 (sd->symbol_names == NULL
320 ? NULL
321 : sd->symbol_names->data()),
322 sd->symbol_names_size);
323 }
324
325 // Lay out the input sections for a dynamic object. We don't want to
326 // include sections from a dynamic object, so all that we actually do
327 // here is check for .gnu.warning sections.
328
329 template<int size, bool big_endian>
330 void
331 Sized_dynobj<size, big_endian>::do_layout(const General_options&,
332 Symbol_table* symtab,
333 Layout*,
334 Read_symbols_data* sd)
335 {
336 const unsigned int shnum = this->shnum();
337 if (shnum == 0)
338 return;
339
340 // Get the section headers.
341 const unsigned char* pshdrs = sd->section_headers->data();
342
343 // Get the section names.
344 const unsigned char* pnamesu = sd->section_names->data();
345 const char* pnames = reinterpret_cast<const char*>(pnamesu);
346
347 // Skip the first, dummy, section.
348 pshdrs += This::shdr_size;
349 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
350 {
351 typename This::Shdr shdr(pshdrs);
352
353 if (shdr.get_sh_name() >= sd->section_names_size)
354 {
355 fprintf(stderr,
356 _("%s: %s: bad section name offset for section %u: %lu\n"),
357 program_name, this->name().c_str(), i,
358 static_cast<unsigned long>(shdr.get_sh_name()));
359 gold_exit(false);
360 }
361
362 const char* name = pnames + shdr.get_sh_name();
363
364 this->handle_gnu_warning_section(name, i, symtab);
365 }
366
367 delete sd->section_headers;
368 sd->section_headers = NULL;
369 delete sd->section_names;
370 sd->section_names = NULL;
371 }
372
373 // Add an entry to the vector mapping version numbers to version
374 // strings.
375
376 template<int size, bool big_endian>
377 void
378 Sized_dynobj<size, big_endian>::set_version_map(
379 Version_map* version_map,
380 unsigned int ndx,
381 const char* name) const
382 {
383 assert(ndx < version_map->size());
384 if ((*version_map)[ndx] != NULL)
385 {
386 fprintf(stderr, _("%s: %s: duplicate definition for version %u\n"),
387 program_name, this->name().c_str(), ndx);
388 gold_exit(false);
389 }
390 (*version_map)[ndx] = name;
391 }
392
393 // Create a vector mapping version numbers to version strings.
394
395 template<int size, bool big_endian>
396 void
397 Sized_dynobj<size, big_endian>::make_version_map(
398 Read_symbols_data* sd,
399 Version_map* version_map) const
400 {
401 if (sd->verdef == NULL && sd->verneed == NULL)
402 return;
403
404 // First find the largest version index.
405 unsigned int maxver = 0;
406
407 if (sd->verdef != NULL)
408 {
409 const unsigned char* pverdef = sd->verdef->data();
410 off_t verdef_size = sd->verdef_size;
411 const unsigned int count = sd->verdef_info;
412
413 const unsigned char* p = pverdef;
414 for (unsigned int i = 0; i < count; ++i)
415 {
416 elfcpp::Verdef<size, big_endian> verdef(p);
417
418 const unsigned int vd_ndx = verdef.get_vd_ndx();
419
420 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
421 // sure why.
422
423 if (vd_ndx > maxver)
424 maxver = vd_ndx;
425
426 const unsigned int vd_next = verdef.get_vd_next();
427 if ((p - pverdef) + vd_next >= verdef_size)
428 {
429 fprintf(stderr,
430 _("%s: %s: verdef vd_next field out of range: %u\n"),
431 program_name, this->name().c_str(), vd_next);
432 gold_exit(false);
433 }
434
435 p += vd_next;
436 }
437 }
438
439 if (sd->verneed != NULL)
440 {
441 const unsigned char* pverneed = sd->verneed->data();
442 off_t verneed_size = sd->verneed_size;
443 const unsigned int count = sd->verneed_info;
444
445 const unsigned char* p = pverneed;
446 for (unsigned int i = 0; i < count; ++i)
447 {
448 elfcpp::Verneed<size, big_endian> verneed(p);
449
450 const unsigned int vn_aux = verneed.get_vn_aux();
451 if ((p - pverneed) + vn_aux >= verneed_size)
452 {
453 fprintf(stderr,
454 _("%s: %s: verneed vn_aux field out of range: %u\n"),
455 program_name, this->name().c_str(), vn_aux);
456 gold_exit(false);
457 }
458
459 const unsigned int vn_cnt = verneed.get_vn_cnt();
460 const unsigned char* pvna = p + vn_aux;
461 for (unsigned int j = 0; j < vn_cnt; ++j)
462 {
463 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
464
465 const unsigned int vna_other = vernaux.get_vna_other();
466 if (vna_other > maxver)
467 maxver = vna_other;
468
469 const unsigned int vna_next = vernaux.get_vna_next();
470 if ((pvna - pverneed) + vna_next >= verneed_size)
471 {
472 fprintf(stderr,
473 _("%s: %s: verneed vna_next field "
474 "out of range: %u\n"),
475 program_name, this->name().c_str(), vna_next);
476 gold_exit(false);
477 }
478
479 pvna += vna_next;
480 }
481
482 const unsigned int vn_next = verneed.get_vn_next();
483 if ((p - pverneed) + vn_next >= verneed_size)
484 {
485 fprintf(stderr,
486 _("%s: %s: verneed vn_next field out of range: %u\n"),
487 program_name, this->name().c_str(), vn_next);
488 gold_exit(false);
489 }
490
491 p += vn_next;
492 }
493 }
494
495 // Now MAXVER is the largest version index we have seen.
496
497 version_map->resize(maxver + 1);
498
499 const char* names = reinterpret_cast<const char*>(sd->symbol_names->data());
500 off_t names_size = sd->symbol_names_size;
501
502 if (sd->verdef != NULL)
503 {
504 const unsigned char* pverdef = sd->verdef->data();
505 off_t verdef_size = sd->verdef_size;
506 const unsigned int count = sd->verdef_info;
507
508 const unsigned char* p = pverdef;
509 for (unsigned int i = 0; i < count; ++i)
510 {
511 elfcpp::Verdef<size, big_endian> verdef(p);
512
513 const unsigned int vd_cnt = verdef.get_vd_cnt();
514 if (vd_cnt < 1)
515 {
516 fprintf(stderr, _("%s: %s: verdef vd_cnt field too small: %u\n"),
517 program_name, this->name().c_str(), vd_cnt);
518 gold_exit(false);
519 }
520
521 const unsigned int vd_aux = verdef.get_vd_aux();
522 if ((p - pverdef) + vd_aux >= verdef_size)
523 {
524 fprintf(stderr,
525 _("%s: %s: verdef vd_aux field out of range: %u\n"),
526 program_name, this->name().c_str(), vd_aux);
527 gold_exit(false);
528 }
529
530 const unsigned char* pvda = p + vd_aux;
531 elfcpp::Verdaux<size, big_endian> verdaux(pvda);
532
533 const unsigned int vda_name = verdaux.get_vda_name();
534 if (vda_name >= names_size)
535 {
536 fprintf(stderr,
537 _("%s: %s: verdaux vda_name field out of range: %u\n"),
538 program_name, this->name().c_str(), vda_name);
539 gold_exit(false);
540 }
541
542 this->set_version_map(version_map, verdef.get_vd_ndx(),
543 names + vda_name);
544
545 const unsigned int vd_next = verdef.get_vd_next();
546 if ((p - pverdef) + vd_next >= verdef_size)
547 {
548 fprintf(stderr,
549 _("%s: %s: verdef vd_next field out of range: %u\n"),
550 program_name, this->name().c_str(), vd_next);
551 gold_exit(false);
552 }
553
554 p += vd_next;
555 }
556 }
557
558 if (sd->verneed != NULL)
559 {
560 const unsigned char* pverneed = sd->verneed->data();
561 const unsigned int count = sd->verneed_info;
562
563 const unsigned char* p = pverneed;
564 for (unsigned int i = 0; i < count; ++i)
565 {
566 elfcpp::Verneed<size, big_endian> verneed(p);
567
568 const unsigned int vn_aux = verneed.get_vn_aux();
569 const unsigned int vn_cnt = verneed.get_vn_cnt();
570 const unsigned char* pvna = p + vn_aux;
571 for (unsigned int j = 0; j < vn_cnt; ++j)
572 {
573 elfcpp::Vernaux<size, big_endian> vernaux(pvna);
574
575 const unsigned int vna_name = vernaux.get_vna_name();
576 if (vna_name >= names_size)
577 {
578 fprintf(stderr,
579 _("%s: %s: vernaux vna_name field "
580 "out of range: %u\n"),
581 program_name, this->name().c_str(), vna_name);
582 gold_exit(false);
583 }
584
585 this->set_version_map(version_map, vernaux.get_vna_other(),
586 names + vna_name);
587
588 pvna += vernaux.get_vna_next();
589 }
590
591 p += verneed.get_vn_next();
592 }
593 }
594 }
595
596 // Add the dynamic symbols to the symbol table.
597
598 template<int size, bool big_endian>
599 void
600 Sized_dynobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
601 Read_symbols_data* sd)
602 {
603 if (sd->symbols == NULL)
604 {
605 assert(sd->symbol_names == NULL);
606 assert(sd->versym == NULL && sd->verdef == NULL && sd->verneed == NULL);
607 return;
608 }
609
610 const int sym_size = This::sym_size;
611 const size_t symcount = sd->symbols_size / sym_size;
612 if (symcount * sym_size != sd->symbols_size)
613 {
614 fprintf(stderr,
615 _("%s: %s: size of dynamic symbols is not "
616 "multiple of symbol size\n"),
617 program_name, this->name().c_str());
618 gold_exit(false);
619 }
620
621 Version_map version_map;
622 this->make_version_map(sd, &version_map);
623
624 const char* sym_names =
625 reinterpret_cast<const char*>(sd->symbol_names->data());
626 symtab->add_from_dynobj(this, sd->symbols->data(), symcount,
627 sym_names, sd->symbol_names_size,
628 (sd->versym == NULL
629 ? NULL
630 : sd->versym->data()),
631 sd->versym_size,
632 &version_map);
633
634 delete sd->symbols;
635 sd->symbols = NULL;
636 delete sd->symbol_names;
637 sd->symbol_names = NULL;
638 if (sd->versym != NULL)
639 {
640 delete sd->versym;
641 sd->versym = NULL;
642 }
643 if (sd->verdef != NULL)
644 {
645 delete sd->verdef;
646 sd->verdef = NULL;
647 }
648 if (sd->verneed != NULL)
649 {
650 delete sd->verneed;
651 sd->verneed = NULL;
652 }
653 }
654
655 // Instantiate the templates we need. We could use the configure
656 // script to restrict this to only the ones for implemented targets.
657
658 template
659 class Sized_dynobj<32, false>;
660
661 template
662 class Sized_dynobj<32, true>;
663
664 template
665 class Sized_dynobj<64, false>;
666
667 template
668 class Sized_dynobj<64, true>;
669
670 } // End namespace gold.
This page took 0.051642 seconds and 5 git commands to generate.