1 // object.cc -- support for an object file for linking in gold
9 #include "target-select.h"
21 // Set the target based on fields in the ELF file header.
24 Object::set_target(int machine
, int size
, bool big_endian
, int osabi
,
27 Target
* target
= select_target(machine
, size
, big_endian
, osabi
, abiversion
);
30 fprintf(stderr
, _("%s: %s: unsupported ELF machine number %d\n"),
31 program_name
, this->name().c_str(), machine
);
34 this->target_
= target
;
37 // Report an error for the elfcpp::Elf_file interface.
40 Object::error(const char* format
, ...)
44 fprintf(stderr
, "%s: %s: ", program_name
, this->name().c_str());
45 va_start(args
, format
);
46 vfprintf(stderr
, format
, args
);
53 // Return a view of the contents of a section.
56 Object::section_contents(unsigned int shndx
, off_t
* plen
)
58 Location
loc(this->do_section_contents(shndx
));
59 *plen
= loc
.data_size
;
60 return this->get_view(loc
.file_offset
, loc
.data_size
);
63 // Read the section data into SD. This is code common to Sized_relobj
64 // and Sized_dynobj, so we put it into Object.
66 template<int size
, bool big_endian
>
68 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
69 Read_symbols_data
* sd
)
71 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
73 // Read the section headers.
74 const off_t shoff
= elf_file
->shoff();
75 const unsigned int shnum
= this->shnum();
76 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
);
78 // Read the section names.
79 const unsigned char* pshdrs
= sd
->section_headers
->data();
80 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
81 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
83 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
86 _("%s: %s: section name section has wrong type: %u\n"),
87 program_name
, this->name().c_str(),
88 static_cast<unsigned int>(shdrnames
.get_sh_type()));
92 sd
->section_names_size
= shdrnames
.get_sh_size();
93 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
94 sd
->section_names_size
);
97 // If NAME is the name of a special .gnu.warning section, arrange for
98 // the warning to be issued. SHNDX is the section index. Return
99 // whether it is a warning section.
102 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
103 Symbol_table
* symtab
)
105 const char warn_prefix
[] = ".gnu.warning.";
106 const int warn_prefix_len
= sizeof warn_prefix
- 1;
107 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
109 symtab
->add_warning(name
+ warn_prefix_len
, this, shndx
);
115 // Class Sized_relobj.
117 template<int size
, bool big_endian
>
118 Sized_relobj
<size
, big_endian
>::Sized_relobj(
119 const std::string
& name
,
120 Input_file
* input_file
,
122 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
123 : Relobj(name
, input_file
, offset
),
124 elf_file_(this, ehdr
),
126 local_symbol_count_(0),
127 output_local_symbol_count_(0),
129 local_symbol_offset_(0),
134 template<int size
, bool big_endian
>
135 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
139 // Set up an object file based on the file header. This sets up the
140 // target and reads the section information.
142 template<int size
, bool big_endian
>
144 Sized_relobj
<size
, big_endian
>::setup(
145 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
147 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
148 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
149 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
151 const unsigned int shnum
= this->elf_file_
.shnum();
152 this->set_shnum(shnum
);
155 // Find the SHT_SYMTAB section, given the section headers. The ELF
156 // standard says that maybe in the future there can be more than one
157 // SHT_SYMTAB section. Until somebody figures out how that could
158 // work, we assume there is only one.
160 template<int size
, bool big_endian
>
162 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
164 const unsigned int shnum
= this->shnum();
165 this->symtab_shndx_
= 0;
168 // Look through the sections in reverse order, since gas tends
169 // to put the symbol table at the end.
170 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
171 unsigned int i
= shnum
;
175 p
-= This::shdr_size
;
176 typename
This::Shdr
shdr(p
);
177 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
179 this->symtab_shndx_
= i
;
186 // Read the sections and symbols from an object file.
188 template<int size
, bool big_endian
>
190 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
192 this->read_section_data(&this->elf_file_
, sd
);
194 const unsigned char* const pshdrs
= sd
->section_headers
->data();
196 this->find_symtab(pshdrs
);
198 if (this->symtab_shndx_
== 0)
200 // No symbol table. Weird but legal.
202 sd
->symbols_size
= 0;
203 sd
->symbol_names
= NULL
;
204 sd
->symbol_names_size
= 0;
208 // Get the symbol table section header.
209 typename
This::Shdr
symtabshdr(pshdrs
210 + this->symtab_shndx_
* This::shdr_size
);
211 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
213 // We only need the external symbols.
214 const int sym_size
= This::sym_size
;
215 const unsigned int loccount
= symtabshdr
.get_sh_info();
216 this->local_symbol_count_
= loccount
;
217 off_t locsize
= loccount
* sym_size
;
218 off_t extoff
= symtabshdr
.get_sh_offset() + locsize
;
219 off_t extsize
= symtabshdr
.get_sh_size() - locsize
;
221 // Read the symbol table.
222 File_view
* fvsymtab
= this->get_lasting_view(extoff
, extsize
);
224 // Read the section header for the symbol names.
225 unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
226 if (strtab_shndx
>= this->shnum())
228 fprintf(stderr
, _("%s: %s: invalid symbol table name index: %u\n"),
229 program_name
, this->name().c_str(), strtab_shndx
);
232 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
233 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
236 _("%s: %s: symbol table name section has wrong type: %u\n"),
237 program_name
, this->name().c_str(),
238 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
242 // Read the symbol names.
243 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
244 strtabshdr
.get_sh_size());
246 sd
->symbols
= fvsymtab
;
247 sd
->symbols_size
= extsize
;
248 sd
->symbol_names
= fvstrtab
;
249 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
252 // Return whether to include a section group in the link. LAYOUT is
253 // used to keep track of which section groups we have already seen.
254 // INDEX is the index of the section group and SHDR is the section
255 // header. If we do not want to include this group, we set bits in
256 // OMIT for each section which should be discarded.
258 template<int size
, bool big_endian
>
260 Sized_relobj
<size
, big_endian
>::include_section_group(
263 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
264 std::vector
<bool>* omit
)
266 // Read the section contents.
267 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
269 const elfcpp::Elf_Word
* pword
=
270 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
272 // The first word contains flags. We only care about COMDAT section
273 // groups. Other section groups are always included in the link
274 // just like ordinary sections.
275 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
276 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
279 // Look up the group signature, which is the name of a symbol. This
280 // is a lot of effort to go to to read a string. Why didn't they
281 // just use the name of the SHT_GROUP section as the group
284 // Get the appropriate symbol table header (this will normally be
285 // the single SHT_SYMTAB section, but in principle it need not be).
286 const unsigned int link
= shdr
.get_sh_link();
287 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
289 // Read the symbol table entry.
290 if (shdr
.get_sh_info() >= symshdr
.get_sh_size() / This::sym_size
)
292 fprintf(stderr
, _("%s: %s: section group %u info %u out of range\n"),
293 program_name
, this->name().c_str(), index
, shdr
.get_sh_info());
296 off_t symoff
= symshdr
.get_sh_offset() + shdr
.get_sh_info() * This::sym_size
;
297 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
);
298 elfcpp::Sym
<size
, big_endian
> sym(psym
);
300 // Read the symbol table names.
302 const unsigned char* psymnamesu
;
303 psymnamesu
= this->section_contents(symshdr
.get_sh_link(), &symnamelen
);
304 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
306 // Get the section group signature.
307 if (sym
.get_st_name() >= symnamelen
)
309 fprintf(stderr
, _("%s: %s: symbol %u name offset %u out of range\n"),
310 program_name
, this->name().c_str(), shdr
.get_sh_info(),
315 const char* signature
= psymnames
+ sym
.get_st_name();
317 // It seems that some versions of gas will create a section group
318 // associated with a section symbol, and then fail to give a name to
319 // the section symbol. In such a case, use the name of the section.
322 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
324 secname
= this->section_name(sym
.get_st_shndx());
325 signature
= secname
.c_str();
328 // Record this section group, and see whether we've already seen one
329 // with the same signature.
330 if (layout
->add_comdat(signature
, true))
333 // This is a duplicate. We want to discard the sections in this
335 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
336 for (size_t i
= 1; i
< count
; ++i
)
338 elfcpp::Elf_Word secnum
=
339 elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
);
340 if (secnum
>= this->shnum())
343 _("%s: %s: section %u in section group %u out of range"),
344 program_name
, this->name().c_str(), secnum
,
348 (*omit
)[secnum
] = true;
354 // Whether to include a linkonce section in the link. NAME is the
355 // name of the section and SHDR is the section header.
357 // Linkonce sections are a GNU extension implemented in the original
358 // GNU linker before section groups were defined. The semantics are
359 // that we only include one linkonce section with a given name. The
360 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
361 // where T is the type of section and SYMNAME is the name of a symbol.
362 // In an attempt to make linkonce sections interact well with section
363 // groups, we try to identify SYMNAME and use it like a section group
364 // signature. We want to block section groups with that signature,
365 // but not other linkonce sections with that signature. We also use
366 // the full name of the linkonce section as a normal section group
369 template<int size
, bool big_endian
>
371 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
374 const elfcpp::Shdr
<size
, big_endian
>&)
376 const char* symname
= strrchr(name
, '.') + 1;
377 bool include1
= layout
->add_comdat(symname
, false);
378 bool include2
= layout
->add_comdat(name
, true);
379 return include1
&& include2
;
382 // Lay out the input sections. We walk through the sections and check
383 // whether they should be included in the link. If they should, we
384 // pass them to the Layout object, which will return an output section
387 template<int size
, bool big_endian
>
389 Sized_relobj
<size
, big_endian
>::do_layout(const General_options
& options
,
390 Symbol_table
* symtab
,
392 Read_symbols_data
* sd
)
394 const unsigned int shnum
= this->shnum();
398 // Get the section headers.
399 const unsigned char* pshdrs
= sd
->section_headers
->data();
401 // Get the section names.
402 const unsigned char* pnamesu
= sd
->section_names
->data();
403 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
405 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
406 map_sections
.resize(shnum
);
408 // Keep track of which sections to omit.
409 std::vector
<bool> omit(shnum
, false);
411 // Skip the first, dummy, section.
412 pshdrs
+= This::shdr_size
;
413 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
415 typename
This::Shdr
shdr(pshdrs
);
417 if (shdr
.get_sh_name() >= sd
->section_names_size
)
420 _("%s: %s: bad section name offset for section %u: %lu\n"),
421 program_name
, this->name().c_str(), i
,
422 static_cast<unsigned long>(shdr
.get_sh_name()));
426 const char* name
= pnames
+ shdr
.get_sh_name();
428 if (this->handle_gnu_warning_section(name
, i
, symtab
))
430 if (!options
.is_relocatable())
434 bool discard
= omit
[i
];
437 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
439 if (!this->include_section_group(layout
, i
, shdr
, &omit
))
442 else if (Layout::is_linkonce(name
))
444 if (!this->include_linkonce_section(layout
, name
, shdr
))
451 // Do not include this section in the link.
452 map_sections
[i
].output_section
= NULL
;
457 Output_section
* os
= layout
->layout(this, i
, name
, shdr
, &offset
);
459 map_sections
[i
].output_section
= os
;
460 map_sections
[i
].offset
= offset
;
463 delete sd
->section_headers
;
464 sd
->section_headers
= NULL
;
465 delete sd
->section_names
;
466 sd
->section_names
= NULL
;
469 // Add the symbols to the symbol table.
471 template<int size
, bool big_endian
>
473 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
474 Read_symbols_data
* sd
)
476 if (sd
->symbols
== NULL
)
478 gold_assert(sd
->symbol_names
== NULL
);
482 const int sym_size
= This::sym_size
;
483 size_t symcount
= sd
->symbols_size
/ sym_size
;
484 if (symcount
* sym_size
!= sd
->symbols_size
)
487 _("%s: %s: size of symbols is not multiple of symbol size\n"),
488 program_name
, this->name().c_str());
492 this->symbols_
= new Symbol
*[symcount
];
494 const char* sym_names
=
495 reinterpret_cast<const char*>(sd
->symbol_names
->data());
496 symtab
->add_from_relobj(this, sd
->symbols
->data(), symcount
, sym_names
,
497 sd
->symbol_names_size
, this->symbols_
);
501 delete sd
->symbol_names
;
502 sd
->symbol_names
= NULL
;
505 // Finalize the local symbols. Here we record the file offset at
506 // which they should be output, we add their names to *POOL, and we
507 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
508 // This function is always called from the main thread. The actual
509 // output of the local symbols will occur in a separate task.
511 template<int size
, bool big_endian
>
513 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
517 gold_assert(this->symtab_shndx_
!= -1U);
518 if (this->symtab_shndx_
== 0)
520 // This object has no symbols. Weird but legal.
524 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
526 this->local_symbol_offset_
= off
;
528 // Read the symbol table section header.
529 const unsigned int symtab_shndx
= this->symtab_shndx_
;
530 typename
This::Shdr
symtabshdr(this,
531 this->elf_file_
.section_header(symtab_shndx
));
532 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
534 // Read the local symbols.
535 const int sym_size
= This::sym_size
;
536 const unsigned int loccount
= this->local_symbol_count_
;
537 gold_assert(loccount
== symtabshdr
.get_sh_info());
538 off_t locsize
= loccount
* sym_size
;
539 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
542 this->local_values_
.resize(loccount
);
544 // Read the symbol names.
545 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
547 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
549 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
551 // Loop over the local symbols.
553 const std::vector
<Map_to_output
>& mo(this->map_to_output());
554 unsigned int shnum
= this->shnum();
555 unsigned int count
= 0;
556 // Skip the first, dummy, symbol.
558 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
560 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
562 Symbol_value
<size
>& lv(this->local_values_
[i
]);
564 unsigned int shndx
= sym
.get_st_shndx();
565 lv
.set_input_shndx(shndx
);
567 if (shndx
>= elfcpp::SHN_LORESERVE
)
569 if (shndx
== elfcpp::SHN_ABS
)
570 lv
.set_output_value(sym
.get_st_value());
573 // FIXME: Handle SHN_XINDEX.
575 _("%s: %s: unknown section index %u "
576 "for local symbol %u\n"),
577 program_name
, this->name().c_str(), shndx
, i
);
586 _("%s: %s: local symbol %u section index %u "
588 program_name
, this->name().c_str(), i
, shndx
);
592 Output_section
* os
= mo
[shndx
].output_section
;
596 lv
.set_output_value(0);
597 lv
.set_no_output_symtab_entry();
601 if (mo
[shndx
].offset
== -1)
602 lv
.set_input_value(sym
.get_st_value());
604 lv
.set_output_value(mo
[shndx
].output_section
->address()
606 + sym
.get_st_value());
609 // Decide whether this symbol should go into the output file.
611 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
613 lv
.set_no_output_symtab_entry();
617 if (sym
.get_st_name() >= strtab_size
)
620 _("%s: %s: local symbol %u section name "
621 "out of range: %u >= %u\n"),
622 program_name
, this->name().c_str(),
623 i
, sym
.get_st_name(),
624 static_cast<unsigned int>(strtab_size
));
628 const char* name
= pnames
+ sym
.get_st_name();
629 pool
->add(name
, NULL
);
630 lv
.set_output_symtab_index(index
);
635 this->output_local_symbol_count_
= count
;
640 // Return the value of a local symbol defined in input section SHNDX,
641 // with value VALUE, adding addend ADDEND. This handles SHF_MERGE
643 template<int size
, bool big_endian
>
644 typename
elfcpp::Elf_types
<size
>::Elf_Addr
645 Sized_relobj
<size
, big_endian
>::local_value(unsigned int shndx
,
647 Address addend
) const
649 const std::vector
<Map_to_output
>& mo(this->map_to_output());
650 Output_section
* os
= mo
[shndx
].output_section
;
653 gold_assert(mo
[shndx
].offset
== -1);
654 return os
->output_address(this, shndx
, value
+ addend
);
657 // Write out the local symbols.
659 template<int size
, bool big_endian
>
661 Sized_relobj
<size
, big_endian
>::write_local_symbols(Output_file
* of
,
662 const Stringpool
* sympool
)
664 gold_assert(this->symtab_shndx_
!= -1U);
665 if (this->symtab_shndx_
== 0)
667 // This object has no symbols. Weird but legal.
671 // Read the symbol table section header.
672 const unsigned int symtab_shndx
= this->symtab_shndx_
;
673 typename
This::Shdr
symtabshdr(this,
674 this->elf_file_
.section_header(symtab_shndx
));
675 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
676 const unsigned int loccount
= this->local_symbol_count_
;
677 gold_assert(loccount
== symtabshdr
.get_sh_info());
679 // Read the local symbols.
680 const int sym_size
= This::sym_size
;
681 off_t locsize
= loccount
* sym_size
;
682 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
685 // Read the symbol names.
686 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
688 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
690 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
692 // Get a view into the output file.
693 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
694 unsigned char* oview
= of
->get_output_view(this->local_symbol_offset_
,
697 const std::vector
<Map_to_output
>& mo(this->map_to_output());
699 gold_assert(this->local_values_
.size() == loccount
);
701 unsigned char* ov
= oview
;
703 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
705 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
707 if (!this->local_values_
[i
].needs_output_symtab_entry())
710 unsigned int st_shndx
= isym
.get_st_shndx();
711 if (st_shndx
< elfcpp::SHN_LORESERVE
)
713 gold_assert(st_shndx
< mo
.size());
714 if (mo
[st_shndx
].output_section
== NULL
)
716 st_shndx
= mo
[st_shndx
].output_section
->out_shndx();
719 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
721 gold_assert(isym
.get_st_name() < strtab_size
);
722 const char* name
= pnames
+ isym
.get_st_name();
723 osym
.put_st_name(sympool
->get_offset(name
));
724 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
725 osym
.put_st_size(isym
.get_st_size());
726 osym
.put_st_info(isym
.get_st_info());
727 osym
.put_st_other(isym
.get_st_other());
728 osym
.put_st_shndx(st_shndx
);
733 gold_assert(ov
- oview
== output_size
);
735 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
738 // Input_objects methods.
740 // Add a regular relocatable object to the list. Return false if this
741 // object should be ignored.
744 Input_objects::add_object(Object
* obj
)
746 if (!obj
->is_dynamic())
747 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
750 // See if this is a duplicate SONAME.
751 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
753 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
754 this->sonames_
.insert(dynobj
->soname());
757 // We have already seen a dynamic object with this soname.
761 this->dynobj_list_
.push_back(dynobj
);
764 Target
* target
= obj
->target();
765 if (this->target_
== NULL
)
766 this->target_
= target
;
767 else if (this->target_
!= target
)
769 fprintf(stderr
, "%s: %s: incompatible target\n",
770 program_name
, obj
->name().c_str());
777 // Relocate_info methods.
779 // Return a string describing the location of a relocation. This is
780 // only used in error messages.
782 template<int size
, bool big_endian
>
784 Relocate_info
<size
, big_endian
>::location(size_t relnum
, off_t
) const
786 std::string
ret(this->object
->name());
789 snprintf(buf
, sizeof buf
, "%zu", relnum
);
791 ret
+= " in reloc section ";
792 snprintf(buf
, sizeof buf
, "%u", this->reloc_shndx
);
794 ret
+= " (" + this->object
->section_name(this->reloc_shndx
);
795 ret
+= ") for section ";
796 snprintf(buf
, sizeof buf
, "%u", this->data_shndx
);
798 ret
+= " (" + this->object
->section_name(this->data_shndx
) + ")";
802 } // End namespace gold.
807 using namespace gold
;
809 // Read an ELF file with the header and return the appropriate
810 // instance of Object.
812 template<int size
, bool big_endian
>
814 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
815 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
817 int et
= ehdr
.get_e_type();
818 if (et
== elfcpp::ET_REL
)
820 Sized_relobj
<size
, big_endian
>* obj
=
821 new Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
825 else if (et
== elfcpp::ET_DYN
)
827 Sized_dynobj
<size
, big_endian
>* obj
=
828 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
834 fprintf(stderr
, _("%s: %s: unsupported ELF file type %d\n"),
835 program_name
, name
.c_str(), et
);
840 } // End anonymous namespace.
845 // Read an ELF file and return the appropriate instance of Object.
848 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
849 const unsigned char* p
, off_t bytes
)
851 if (bytes
< elfcpp::EI_NIDENT
)
853 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
854 program_name
, name
.c_str());
858 int v
= p
[elfcpp::EI_VERSION
];
859 if (v
!= elfcpp::EV_CURRENT
)
861 if (v
== elfcpp::EV_NONE
)
862 fprintf(stderr
, _("%s: %s: invalid ELF version 0\n"),
863 program_name
, name
.c_str());
865 fprintf(stderr
, _("%s: %s: unsupported ELF version %d\n"),
866 program_name
, name
.c_str(), v
);
870 int c
= p
[elfcpp::EI_CLASS
];
871 if (c
== elfcpp::ELFCLASSNONE
)
873 fprintf(stderr
, _("%s: %s: invalid ELF class 0\n"),
874 program_name
, name
.c_str());
877 else if (c
!= elfcpp::ELFCLASS32
878 && c
!= elfcpp::ELFCLASS64
)
880 fprintf(stderr
, _("%s: %s: unsupported ELF class %d\n"),
881 program_name
, name
.c_str(), c
);
885 int d
= p
[elfcpp::EI_DATA
];
886 if (d
== elfcpp::ELFDATANONE
)
888 fprintf(stderr
, _("%s: %s: invalid ELF data encoding\n"),
889 program_name
, name
.c_str());
892 else if (d
!= elfcpp::ELFDATA2LSB
893 && d
!= elfcpp::ELFDATA2MSB
)
895 fprintf(stderr
, _("%s: %s: unsupported ELF data encoding %d\n"),
896 program_name
, name
.c_str(), d
);
900 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
902 if (c
== elfcpp::ELFCLASS32
)
904 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
906 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
907 program_name
, name
.c_str());
912 elfcpp::Ehdr
<32, true> ehdr(p
);
913 return make_elf_sized_object
<32, true>(name
, input_file
,
918 elfcpp::Ehdr
<32, false> ehdr(p
);
919 return make_elf_sized_object
<32, false>(name
, input_file
,
925 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
927 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
928 program_name
, name
.c_str());
933 elfcpp::Ehdr
<64, true> ehdr(p
);
934 return make_elf_sized_object
<64, true>(name
, input_file
,
939 elfcpp::Ehdr
<64, false> ehdr(p
);
940 return make_elf_sized_object
<64, false>(name
, input_file
,
946 // Instantiate the templates we need. We could use the configure
947 // script to restrict this to only the ones for implemented targets.
950 class Sized_relobj
<32, false>;
953 class Sized_relobj
<32, true>;
956 class Sized_relobj
<64, false>;
959 class Sized_relobj
<64, true>;
962 struct Relocate_info
<32, false>;
965 struct Relocate_info
<32, true>;
968 struct Relocate_info
<64, false>;
971 struct Relocate_info
<64, true>;
973 } // End namespace gold.