1 // dynobj.cc -- dynamic object support for gold
14 // Class Sized_dynobj.
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
,
21 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
22 : Dynobj(name
, input_file
, offset
),
23 elf_file_(this, ehdr
),
30 template<int size
, bool big_endian
>
32 Sized_dynobj
<size
, big_endian
>::setup(
33 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
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
]);
39 const unsigned int shnum
= this->elf_file_
.shnum();
40 this->set_shnum(shnum
);
43 // Find the SHT_DYNSYM section and the various version sections, and
44 // the dynamic section, given the section headers.
46 template<int size
, bool big_endian
>
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
)
59 *pverneed_shndx
= -1U;
60 *pdynamic_shndx
= -1U;
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
)
66 typename
This::Shdr
shdr(p
);
69 switch (shdr
.get_sh_type())
71 case elfcpp::SHT_DYNSYM
:
74 case elfcpp::SHT_GNU_versym
:
77 case elfcpp::SHT_GNU_verdef
:
80 case elfcpp::SHT_GNU_verneed
:
83 case elfcpp::SHT_DYNAMIC
:
97 _("%s: %s: unexpected duplicate type %u section: %u, %u\n"),
98 program_name
, this->name().c_str(), shdr
.get_sh_type(),
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.
112 template<int size
, bool big_endian
>
114 Sized_dynobj
<size
, big_endian
>::read_dynsym_section(
115 const unsigned char* pshdrs
,
121 unsigned int* view_info
)
131 typename
This::Shdr
shdr(pshdrs
+ shndx
* This::shdr_size
);
133 assert(shdr
.get_sh_type() == type
);
135 if (shdr
.get_sh_link() != link
)
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
);
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();
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.
155 template<int size
, bool big_endian
>
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
,
163 typename
This::Shdr
dynamicshdr(pshdrs
+ dynamic_shndx
* This::shdr_size
);
164 assert(dynamicshdr
.get_sh_type() == elfcpp::SHT_DYNAMIC
);
166 const off_t dynamic_size
= dynamicshdr
.get_sh_size();
167 const unsigned char* pdynamic
= this->get_view(dynamicshdr
.get_sh_offset(),
170 const unsigned int link
= dynamicshdr
.get_sh_link();
171 if (link
!= strtab_shndx
)
173 if (link
>= this->shnum())
176 _("%s: %s: DYNAMIC section %u link out of range: %u\n"),
177 program_name
, this->name().c_str(),
178 dynamic_shndx
, link
);
182 typename
This::Shdr
strtabshdr(pshdrs
+ link
* This::shdr_size
);
183 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
186 _("%s: %s: DYNAMIC section %u link %u is not a strtab\n"),
187 program_name
, this->name().c_str(),
188 dynamic_shndx
, link
);
192 strtab_size
= strtabshdr
.get_sh_size();
193 strtabu
= this->get_view(strtabshdr
.get_sh_offset(), strtab_size
);
196 for (const unsigned char* p
= pdynamic
;
197 p
< pdynamic
+ dynamic_size
;
200 typename
This::Dyn
dyn(p
);
202 if (dyn
.get_d_tag() == elfcpp::DT_SONAME
)
204 off_t val
= dyn
.get_d_val();
205 if (val
>= strtab_size
)
208 _("%s: %s: DT_SONAME value out of range: "
210 program_name
, this->name().c_str(),
211 static_cast<long long>(val
),
212 static_cast<long long>(strtab_size
));
216 const char* strtab
= reinterpret_cast<const char*>(strtabu
);
217 this->soname_
= std::string(strtab
+ val
);
221 if (dyn
.get_d_tag() == elfcpp::DT_NULL
)
225 fprintf(stderr
, _("%s: %s: missing DT_NULL in dynamic segment\n"),
226 program_name
, this->name().c_str());
230 // Read the symbols and sections from a dynamic object. We read the
231 // dynamic symbols, not the normal symbols.
233 template<int size
, bool big_endian
>
235 Sized_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
237 this->read_section_data(&this->elf_file_
, sd
);
239 const unsigned char* const pshdrs
= sd
->section_headers
->data();
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
);
249 unsigned int strtab_shndx
= -1U;
251 if (dynsym_shndx
== -1U)
254 sd
->symbols_size
= 0;
255 sd
->symbol_names
= NULL
;
256 sd
->symbol_names_size
= 0;
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
);
264 sd
->symbols
= this->get_lasting_view(dynsymshdr
.get_sh_offset(),
265 dynsymshdr
.get_sh_size());
266 sd
->symbols_size
= dynsymshdr
.get_sh_size();
268 // Get the symbol names.
269 strtab_shndx
= dynsymshdr
.get_sh_link();
270 if (strtab_shndx
>= this->shnum())
273 _("%s: %s: invalid dynamic symbol table name index: %u\n"),
274 program_name
, this->name().c_str(), strtab_shndx
);
277 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
278 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
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()));
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();
292 // Get the version information.
295 this->read_dynsym_section(pshdrs
, versym_shndx
, elfcpp::SHT_GNU_versym
,
296 dynsym_shndx
, &sd
->versym
, &sd
->versym_size
,
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.
304 this->read_dynsym_section(pshdrs
, verdef_shndx
, elfcpp::SHT_GNU_verdef
,
305 strtab_shndx
, &sd
->verdef
, &sd
->verdef_size
,
308 this->read_dynsym_section(pshdrs
, verneed_shndx
, elfcpp::SHT_GNU_verneed
,
309 strtab_shndx
, &sd
->verneed
, &sd
->verneed_size
,
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
317 if (dynamic_shndx
!= -1U)
318 this->set_soname(pshdrs
, dynamic_shndx
, strtab_shndx
,
319 (sd
->symbol_names
== NULL
321 : sd
->symbol_names
->data()),
322 sd
->symbol_names_size
);
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.
329 template<int size
, bool big_endian
>
331 Sized_dynobj
<size
, big_endian
>::do_layout(const General_options
&,
332 Symbol_table
* symtab
,
334 Read_symbols_data
* sd
)
336 const unsigned int shnum
= this->shnum();
340 // Get the section headers.
341 const unsigned char* pshdrs
= sd
->section_headers
->data();
343 // Get the section names.
344 const unsigned char* pnamesu
= sd
->section_names
->data();
345 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
347 // Skip the first, dummy, section.
348 pshdrs
+= This::shdr_size
;
349 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
351 typename
This::Shdr
shdr(pshdrs
);
353 if (shdr
.get_sh_name() >= sd
->section_names_size
)
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()));
362 const char* name
= pnames
+ shdr
.get_sh_name();
364 this->handle_gnu_warning_section(name
, i
, symtab
);
367 delete sd
->section_headers
;
368 sd
->section_headers
= NULL
;
369 delete sd
->section_names
;
370 sd
->section_names
= NULL
;
373 // Add an entry to the vector mapping version numbers to version
376 template<int size
, bool big_endian
>
378 Sized_dynobj
<size
, big_endian
>::set_version_map(
379 Version_map
* version_map
,
381 const char* name
) const
383 assert(ndx
< version_map
->size());
384 if ((*version_map
)[ndx
] != NULL
)
386 fprintf(stderr
, _("%s: %s: duplicate definition for version %u\n"),
387 program_name
, this->name().c_str(), ndx
);
390 (*version_map
)[ndx
] = name
;
393 // Create a vector mapping version numbers to version strings.
395 template<int size
, bool big_endian
>
397 Sized_dynobj
<size
, big_endian
>::make_version_map(
398 Read_symbols_data
* sd
,
399 Version_map
* version_map
) const
401 if (sd
->verdef
== NULL
&& sd
->verneed
== NULL
)
404 // First find the largest version index.
405 unsigned int maxver
= 0;
407 if (sd
->verdef
!= NULL
)
409 const unsigned char* pverdef
= sd
->verdef
->data();
410 off_t verdef_size
= sd
->verdef_size
;
411 const unsigned int count
= sd
->verdef_info
;
413 const unsigned char* p
= pverdef
;
414 for (unsigned int i
= 0; i
< count
; ++i
)
416 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
418 const unsigned int vd_ndx
= verdef
.get_vd_ndx();
420 // The GNU linker clears the VERSYM_HIDDEN bit. I'm not
426 const unsigned int vd_next
= verdef
.get_vd_next();
427 if ((p
- pverdef
) + vd_next
>= verdef_size
)
430 _("%s: %s: verdef vd_next field out of range: %u\n"),
431 program_name
, this->name().c_str(), vd_next
);
439 if (sd
->verneed
!= NULL
)
441 const unsigned char* pverneed
= sd
->verneed
->data();
442 off_t verneed_size
= sd
->verneed_size
;
443 const unsigned int count
= sd
->verneed_info
;
445 const unsigned char* p
= pverneed
;
446 for (unsigned int i
= 0; i
< count
; ++i
)
448 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
450 const unsigned int vn_aux
= verneed
.get_vn_aux();
451 if ((p
- pverneed
) + vn_aux
>= verneed_size
)
454 _("%s: %s: verneed vn_aux field out of range: %u\n"),
455 program_name
, this->name().c_str(), vn_aux
);
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
)
463 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
465 const unsigned int vna_other
= vernaux
.get_vna_other();
466 if (vna_other
> maxver
)
469 const unsigned int vna_next
= vernaux
.get_vna_next();
470 if ((pvna
- pverneed
) + vna_next
>= verneed_size
)
473 _("%s: %s: verneed vna_next field "
474 "out of range: %u\n"),
475 program_name
, this->name().c_str(), vna_next
);
482 const unsigned int vn_next
= verneed
.get_vn_next();
483 if ((p
- pverneed
) + vn_next
>= verneed_size
)
486 _("%s: %s: verneed vn_next field out of range: %u\n"),
487 program_name
, this->name().c_str(), vn_next
);
495 // Now MAXVER is the largest version index we have seen.
497 version_map
->resize(maxver
+ 1);
499 const char* names
= reinterpret_cast<const char*>(sd
->symbol_names
->data());
500 off_t names_size
= sd
->symbol_names_size
;
502 if (sd
->verdef
!= NULL
)
504 const unsigned char* pverdef
= sd
->verdef
->data();
505 off_t verdef_size
= sd
->verdef_size
;
506 const unsigned int count
= sd
->verdef_info
;
508 const unsigned char* p
= pverdef
;
509 for (unsigned int i
= 0; i
< count
; ++i
)
511 elfcpp::Verdef
<size
, big_endian
> verdef(p
);
513 const unsigned int vd_cnt
= verdef
.get_vd_cnt();
516 fprintf(stderr
, _("%s: %s: verdef vd_cnt field too small: %u\n"),
517 program_name
, this->name().c_str(), vd_cnt
);
521 const unsigned int vd_aux
= verdef
.get_vd_aux();
522 if ((p
- pverdef
) + vd_aux
>= verdef_size
)
525 _("%s: %s: verdef vd_aux field out of range: %u\n"),
526 program_name
, this->name().c_str(), vd_aux
);
530 const unsigned char* pvda
= p
+ vd_aux
;
531 elfcpp::Verdaux
<size
, big_endian
> verdaux(pvda
);
533 const unsigned int vda_name
= verdaux
.get_vda_name();
534 if (vda_name
>= names_size
)
537 _("%s: %s: verdaux vda_name field out of range: %u\n"),
538 program_name
, this->name().c_str(), vda_name
);
542 this->set_version_map(version_map
, verdef
.get_vd_ndx(),
545 const unsigned int vd_next
= verdef
.get_vd_next();
546 if ((p
- pverdef
) + vd_next
>= verdef_size
)
549 _("%s: %s: verdef vd_next field out of range: %u\n"),
550 program_name
, this->name().c_str(), vd_next
);
558 if (sd
->verneed
!= NULL
)
560 const unsigned char* pverneed
= sd
->verneed
->data();
561 const unsigned int count
= sd
->verneed_info
;
563 const unsigned char* p
= pverneed
;
564 for (unsigned int i
= 0; i
< count
; ++i
)
566 elfcpp::Verneed
<size
, big_endian
> verneed(p
);
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
)
573 elfcpp::Vernaux
<size
, big_endian
> vernaux(pvna
);
575 const unsigned int vna_name
= vernaux
.get_vna_name();
576 if (vna_name
>= names_size
)
579 _("%s: %s: vernaux vna_name field "
580 "out of range: %u\n"),
581 program_name
, this->name().c_str(), vna_name
);
585 this->set_version_map(version_map
, vernaux
.get_vna_other(),
588 pvna
+= vernaux
.get_vna_next();
591 p
+= verneed
.get_vn_next();
596 // Add the dynamic symbols to the symbol table.
598 template<int size
, bool big_endian
>
600 Sized_dynobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
601 Read_symbols_data
* sd
)
603 if (sd
->symbols
== NULL
)
605 assert(sd
->symbol_names
== NULL
);
606 assert(sd
->versym
== NULL
&& sd
->verdef
== NULL
&& sd
->verneed
== NULL
);
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
)
615 _("%s: %s: size of dynamic symbols is not "
616 "multiple of symbol size\n"),
617 program_name
, this->name().c_str());
621 Version_map version_map
;
622 this->make_version_map(sd
, &version_map
);
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
,
630 : sd
->versym
->data()),
636 delete sd
->symbol_names
;
637 sd
->symbol_names
= NULL
;
638 if (sd
->versym
!= NULL
)
643 if (sd
->verdef
!= NULL
)
648 if (sd
->verneed
!= NULL
)
655 // Instantiate the templates we need. We could use the configure
656 // script to restrict this to only the ones for implemented targets.
659 class Sized_dynobj
<32, false>;
662 class Sized_dynobj
<32, true>;
665 class Sized_dynobj
<64, false>;
668 class Sized_dynobj
<64, true>;
670 } // End namespace gold.
This page took 0.042543 seconds and 4 git commands to generate.