1 // object.cc -- support for an object file for linking in gold
9 #include "target-select.h"
19 // Class Sized_relobj.
21 template<int size, bool big_endian>
22 Sized_relobj<size, big_endian>::Sized_relobj(
23 const std::string& name,
24 Input_file* input_file,
26 const elfcpp::Ehdr<size, big_endian>& ehdr)
27 : Relobj(name, input_file, offset),
28 section_headers_(NULL),
29 flags_(ehdr.get_e_flags()),
30 shoff_(ehdr.get_e_shoff()),
33 local_symbol_count_(0),
34 output_local_symbol_count_(0),
36 local_symbol_offset_(0),
39 if (ehdr.get_e_ehsize() != This::ehdr_size)
41 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
42 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
46 if (ehdr.get_e_shentsize() != This::shdr_size)
48 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
49 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
55 template<int size, bool big_endian>
56 Sized_relobj<size, big_endian>::~Sized_relobj()
60 // Read the section header for section SHNUM.
62 template<int size, bool big_endian>
63 inline const unsigned char*
64 Sized_relobj<size, big_endian>::section_header(unsigned int shnum)
66 assert(shnum < this->shnum());
67 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
68 return this->get_view(symtabshdroff, This::shdr_size);
71 // Return the name of section SHNUM. The object must already be
74 template<int size, bool big_endian>
76 Sized_relobj<size, big_endian>::do_section_name(unsigned int shnum)
78 // Read the section names.
79 typename This::Shdr shdrnames(this->section_header(this->shstrndx_));
80 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
81 shdrnames.get_sh_size());
82 const char* pnames = reinterpret_cast<const char*>(pnamesu);
84 typename This::Shdr shdr(this->section_header(shnum));
85 if (shdr.get_sh_name() >= shdrnames.get_sh_size())
88 _("%s: %s: bad section name offset for section %u: %lu\n"),
89 program_name, this->name().c_str(), shnum,
90 static_cast<unsigned long>(shdr.get_sh_name()));
94 return std::string(pnames + shdr.get_sh_name());
97 // Return a view of the contents of section SHNUM. The object does
98 // not have to be locked.
100 template<int size, bool big_endian>
102 Sized_relobj<size, big_endian>::do_section_contents(unsigned int shnum,
105 Task_locker_obj<Object> tl(*this);
107 typename This::Shdr shdr(this->section_header(shnum));
108 *plen = shdr.get_sh_size();
109 return this->get_view(shdr.get_sh_offset(), shdr.get_sh_size());
112 // Set up an object file bsaed on the file header. This sets up the
113 // target and reads the section information.
115 template<int size, bool big_endian>
117 Sized_relobj<size, big_endian>::setup(
118 const elfcpp::Ehdr<size, big_endian>& ehdr)
120 int machine = ehdr.get_e_machine();
121 Target* target = select_target(machine, size, big_endian,
122 ehdr.get_e_ident()[elfcpp::EI_OSABI],
123 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
126 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
127 program_name, this->name().c_str(), machine);
130 this->set_target(target);
132 unsigned int shnum = ehdr.get_e_shnum();
133 unsigned int shstrndx = ehdr.get_e_shstrndx();
134 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
135 && this->shoff_ != 0)
137 typename This::Shdr shdr(this->section_header(0));
139 shnum = shdr.get_sh_size();
140 if (shstrndx == elfcpp::SHN_XINDEX)
141 shstrndx = shdr.get_sh_link();
143 this->set_shnum(shnum);
144 this->shstrndx_ = shstrndx;
149 // We store the section headers in a File_view until do_read_symbols.
150 this->section_headers_ = this->get_lasting_view(this->shoff_,
151 shnum * This::shdr_size);
153 // Find the SHT_SYMTAB section. The ELF standard says that maybe in
154 // the future there can be more than one SHT_SYMTAB section. Until
155 // somebody figures out how that could work, we assume there is only
157 const unsigned char* p = this->section_headers_->data();
159 // Skip the first section, which is always empty.
160 p += This::shdr_size;
161 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
163 typename This::Shdr shdr(p);
164 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
166 this->symtab_shnum_ = i;
172 // Read the sections and symbols from an object file.
174 template<int size, bool big_endian>
176 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
178 // Transfer our view of the section headers to SD.
179 sd->section_headers = this->section_headers_;
180 this->section_headers_ = NULL;
182 // Read the section names.
183 const unsigned char* pshdrs = sd->section_headers->data();
184 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
185 typename This::Shdr shdrnames(pshdrnames);
186 sd->section_names_size = shdrnames.get_sh_size();
187 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
188 sd->section_names_size);
190 if (this->symtab_shnum_ == 0)
192 // No symbol table. Weird but legal.
194 sd->symbols_size = 0;
195 sd->symbol_names = NULL;
196 sd->symbol_names_size = 0;
200 // Get the symbol table section header.
201 typename This::Shdr symtabshdr(pshdrs
202 + this->symtab_shnum_ * This::shdr_size);
203 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
205 // We only need the external symbols.
206 const int sym_size = This::sym_size;
207 const unsigned int loccount = symtabshdr.get_sh_info();
208 this->local_symbol_count_ = loccount;
209 off_t locsize = loccount * sym_size;
210 off_t extoff = symtabshdr.get_sh_offset() + locsize;
211 off_t extsize = symtabshdr.get_sh_size() - locsize;
213 // Read the symbol table.
214 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
216 // Read the section header for the symbol names.
217 unsigned int shnum = this->shnum();
218 unsigned int strtab_shnum = symtabshdr.get_sh_link();
219 if (strtab_shnum == 0 || strtab_shnum >= shnum)
221 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
222 program_name, this->name().c_str(), strtab_shnum);
225 typename This::Shdr strtabshdr(pshdrs + strtab_shnum * This::shdr_size);
226 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
229 _("%s: %s: symbol table name section has wrong type: %u\n"),
230 program_name, this->name().c_str(),
231 static_cast<unsigned int>(strtabshdr.get_sh_type()));
235 // Read the symbol names.
236 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
237 strtabshdr.get_sh_size());
239 sd->symbols = fvsymtab;
240 sd->symbols_size = extsize;
241 sd->symbol_names = fvstrtab;
242 sd->symbol_names_size = strtabshdr.get_sh_size();
245 // Return whether to include a section group in the link. LAYOUT is
246 // used to keep track of which section groups we have already seen.
247 // INDEX is the index of the section group and SHDR is the section
248 // header. If we do not want to include this group, we set bits in
249 // OMIT for each section which should be discarded.
251 template<int size, bool big_endian>
253 Sized_relobj<size, big_endian>::include_section_group(
256 const elfcpp::Shdr<size, big_endian>& shdr,
257 std::vector<bool>* omit)
259 // Read the section contents.
260 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
262 const elfcpp::Elf_Word* pword =
263 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
265 // The first word contains flags. We only care about COMDAT section
266 // groups. Other section groups are always included in the link
267 // just like ordinary sections.
268 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
269 if ((flags & elfcpp::GRP_COMDAT) == 0)
272 // Look up the group signature, which is the name of a symbol. This
273 // is a lot of effort to go to to read a string. Why didn't they
274 // just use the name of the SHT_GROUP section as the group
277 // Get the appropriate symbol table header (this will normally be
278 // the single SHT_SYMTAB section, but in principle it need not be).
279 if (shdr.get_sh_link() >= this->shnum())
281 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
282 program_name, this->name().c_str(), index, shdr.get_sh_link());
286 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
288 // Read the symbol table entry.
289 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
291 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
292 program_name, this->name().c_str(), index, shdr.get_sh_info());
295 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
296 const unsigned char* psym = this->get_view(symoff, This::sym_size);
297 elfcpp::Sym<size, big_endian> sym(psym);
299 // Read the section header for the symbol table names.
300 if (symshdr.get_sh_link() >= this->shnum())
302 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
303 program_name, this->name().c_str(), shdr.get_sh_link(),
304 symshdr.get_sh_link());
308 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
310 // Read the symbol table names.
311 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
312 symnamehdr.get_sh_size());
313 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
315 // Get the section group signature.
316 if (sym.get_st_name() >= symnamehdr.get_sh_size())
318 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
319 program_name, this->name().c_str(), shdr.get_sh_info(),
324 const char* signature = psymnames + sym.get_st_name();
326 // It seems that some versions of gas will create a section group
327 // associated with a section symbol, and then fail to give a name to
328 // the section symbol. In such a case, use the name of the section.
330 if (signature[0] == '\0'
331 && sym.get_st_type() == elfcpp::STT_SECTION
332 && sym.get_st_shndx() < this->shnum())
334 typename This::Shdr shdrnames(this->section_header(this->shstrndx_));
335 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
336 shdrnames.get_sh_size());
337 const char* pnames = reinterpret_cast<const char*>(pnamesu);
339 typename This::Shdr sechdr(this->section_header(sym.get_st_shndx()));
340 if (sechdr.get_sh_name() >= shdrnames.get_sh_size())
343 _("%s: %s: bad section name offset for section %u: %lu\n"),
344 program_name, this->name().c_str(), sym.get_st_shndx(),
345 static_cast<unsigned long>(sechdr.get_sh_name()));
349 signature = pnames + sechdr.get_sh_name();
352 // Record this section group, and see whether we've already seen one
353 // with the same signature.
354 if (layout->add_comdat(signature, true))
357 // This is a duplicate. We want to discard the sections in this
359 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
360 for (size_t i = 1; i < count; ++i)
362 elfcpp::Elf_Word secnum =
363 elfcpp::Swap<32, big_endian>::readval(pword + i);
364 if (secnum >= this->shnum())
367 _("%s: %s: section %u in section group %u out of range"),
368 program_name, this->name().c_str(), secnum,
372 (*omit)[secnum] = true;
378 // Whether to include a linkonce section in the link. NAME is the
379 // name of the section and SHDR is the section header.
381 // Linkonce sections are a GNU extension implemented in the original
382 // GNU linker before section groups were defined. The semantics are
383 // that we only include one linkonce section with a given name. The
384 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
385 // where T is the type of section and SYMNAME is the name of a symbol.
386 // In an attempt to make linkonce sections interact well with section
387 // groups, we try to identify SYMNAME and use it like a section group
388 // signature. We want to block section groups with that signature,
389 // but not other linkonce sections with that signature. We also use
390 // the full name of the linkonce section as a normal section group
393 template<int size, bool big_endian>
395 Sized_relobj<size, big_endian>::include_linkonce_section(
398 const elfcpp::Shdr<size, big_endian>&)
400 const char* symname = strrchr(name, '.') + 1;
401 bool include1 = layout->add_comdat(symname, false);
402 bool include2 = layout->add_comdat(name, true);
403 return include1 && include2;
406 // Lay out the input sections. We walk through the sections and check
407 // whether they should be included in the link. If they should, we
408 // pass them to the Layout object, which will return an output section
411 template<int size, bool big_endian>
413 Sized_relobj<size, big_endian>::do_layout(const General_options& options,
414 Symbol_table* symtab,
416 Read_symbols_data* sd)
418 unsigned int shnum = this->shnum();
422 // Get the section headers.
423 const unsigned char* pshdrs = sd->section_headers->data();
425 // Get the section names.
426 const unsigned char* pnamesu = sd->section_names->data();
427 const char* pnames = reinterpret_cast<const char*>(pnamesu);
429 std::vector<Map_to_output>& map_sections(this->map_to_output());
430 map_sections.resize(shnum);
432 // Keep track of which sections to omit.
433 std::vector<bool> omit(shnum, false);
435 const char warn_prefix[] = ".gnu.warning.";
436 const int warn_prefix_len = sizeof warn_prefix - 1;
438 // Skip the first, dummy, section.
439 pshdrs += This::shdr_size;
440 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
442 typename This::Shdr shdr(pshdrs);
444 if (shdr.get_sh_name() >= sd->section_names_size)
447 _("%s: %s: bad section name offset for section %u: %lu\n"),
448 program_name, this->name().c_str(), i,
449 static_cast<unsigned long>(shdr.get_sh_name()));
453 const char* name = pnames + shdr.get_sh_name();
455 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
457 symtab->add_warning(name + warn_prefix_len, this, i);
458 if (!options.is_relocatable())
462 bool discard = omit[i];
465 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
467 if (!this->include_section_group(layout, i, shdr, &omit))
470 else if (Layout::is_linkonce(name))
472 if (!this->include_linkonce_section(layout, name, shdr))
479 // Do not include this section in the link.
480 map_sections[i].output_section = NULL;
485 Output_section* os = layout->layout(this, i, name, shdr, &offset);
487 map_sections[i].output_section = os;
488 map_sections[i].offset = offset;
491 delete sd->section_headers;
492 sd->section_headers = NULL;
493 delete sd->section_names;
494 sd->section_names = NULL;
497 // Add the symbols to the symbol table.
499 template<int size, bool big_endian>
501 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
502 Read_symbols_data* sd)
504 if (sd->symbols == NULL)
506 assert(sd->symbol_names == NULL);
510 const int sym_size = This::sym_size;
511 size_t symcount = sd->symbols_size / sym_size;
512 if (symcount * sym_size != sd->symbols_size)
515 _("%s: %s: size of symbols is not multiple of symbol size\n"),
516 program_name, this->name().c_str());
520 this->symbols_ = new Symbol*[symcount];
522 const char* sym_names =
523 reinterpret_cast<const char*>(sd->symbol_names->data());
524 symtab->add_from_object<size, big_endian>(this, sd->symbols->data(),
526 sd->symbol_names_size,
531 delete sd->symbol_names;
532 sd->symbol_names = NULL;
535 // Finalize the local symbols. Here we record the file offset at
536 // which they should be output, we add their names to *POOL, and we
537 // add their values to THIS->VALUES_. Return the new file offset.
538 // This function is always called from the main thread. The actual
539 // output of the local symbols will occur in a separate task.
541 template<int size, bool big_endian>
543 Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
546 if (this->symtab_shnum_ == 0)
548 // This object has no symbols. Weird but legal.
552 off = align_address(off, size >> 3);
554 this->local_symbol_offset_ = off;
556 // Read the symbol table section header.
557 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
558 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
560 // Read the local symbols.
561 const int sym_size = This::sym_size;
562 const unsigned int loccount = this->local_symbol_count_;
563 assert(loccount == symtabshdr.get_sh_info());
564 off_t locsize = loccount * sym_size;
565 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
568 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
570 // Read the section header for the symbol names.
571 typename This::Shdr strtabshdr(
572 this->section_header(symtabshdr.get_sh_link()));
573 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
575 // Read the symbol names.
576 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
577 strtabshdr.get_sh_size());
578 const char* pnames = reinterpret_cast<const char*>(pnamesu);
580 // Loop over the local symbols.
582 std::vector<Map_to_output>& mo(this->map_to_output());
583 unsigned int shnum = this->shnum();
584 unsigned int count = 0;
585 // Skip the first, dummy, symbol.
587 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
589 elfcpp::Sym<size, big_endian> sym(psyms);
591 unsigned int shndx = sym.get_st_shndx();
593 if (shndx >= elfcpp::SHN_LORESERVE)
595 if (shndx == elfcpp::SHN_ABS)
596 this->values_[i] = sym.get_st_value();
599 // FIXME: Handle SHN_XINDEX.
601 _("%s: %s: unknown section index %u "
602 "for local symbol %u\n"),
603 program_name, this->name().c_str(), shndx, i);
612 _("%s: %s: local symbol %u section index %u "
614 program_name, this->name().c_str(), i, shndx);
618 if (mo[shndx].output_section == NULL)
620 this->values_[i] = 0;
624 this->values_[i] = (mo[shndx].output_section->address()
626 + sym.get_st_value());
629 if (sym.get_st_type() != elfcpp::STT_SECTION)
631 pool->add(pnames + sym.get_st_name(), NULL);
637 this->output_local_symbol_count_ = count;
642 // Write out the local symbols.
644 template<int size, bool big_endian>
646 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
647 const Stringpool* sympool)
649 if (this->symtab_shnum_ == 0)
651 // This object has no symbols. Weird but legal.
655 // Read the symbol table section header.
656 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
657 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
658 const unsigned int loccount = this->local_symbol_count_;
659 assert(loccount == symtabshdr.get_sh_info());
661 // Read the local symbols.
662 const int sym_size = This::sym_size;
663 off_t locsize = loccount * sym_size;
664 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
667 // Read the section header for the symbol names.
668 typename This::Shdr strtabshdr(
669 this->section_header(symtabshdr.get_sh_link()));
670 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
672 // Read the symbol names.
673 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
674 strtabshdr.get_sh_size());
675 const char* pnames = reinterpret_cast<const char*>(pnamesu);
677 // Get a view into the output file.
678 off_t output_size = this->output_local_symbol_count_ * sym_size;
679 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
682 std::vector<Map_to_output>& mo(this->map_to_output());
685 unsigned char* ov = oview;
686 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
688 elfcpp::Sym<size, big_endian> isym(psyms);
690 if (isym.get_st_type() == elfcpp::STT_SECTION)
693 unsigned int st_shndx = isym.get_st_shndx();
694 if (st_shndx < elfcpp::SHN_LORESERVE)
696 assert(st_shndx < mo.size());
697 if (mo[st_shndx].output_section == NULL)
699 st_shndx = mo[st_shndx].output_section->out_shndx();
702 elfcpp::Sym_write<size, big_endian> osym(ov);
704 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
705 osym.put_st_value(this->values_[i]);
706 osym.put_st_size(isym.get_st_size());
707 osym.put_st_info(isym.get_st_info());
708 osym.put_st_other(isym.get_st_other());
709 osym.put_st_shndx(st_shndx);
714 assert(ov - oview == output_size);
716 of->write_output_view(this->local_symbol_offset_, output_size, oview);
719 // Input_objects methods.
721 // Add a regular relocatable object to the list.
724 Input_objects::add_object(Object* obj)
726 if (obj->is_dynamic())
727 this->dynobj_list_.push_back(static_cast<Dynobj*>(obj));
729 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
731 Target* target = obj->target();
732 if (this->target_ == NULL)
733 this->target_ = target;
734 else if (this->target_ != target)
736 fprintf(stderr, "%s: %s: incompatible target\n",
737 program_name, obj->name().c_str());
742 // Relocate_info methods.
744 // Return a string describing the location of a relocation. This is
745 // only used in error messages.
747 template<int size, bool big_endian>
749 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
751 std::string ret(this->object->name());
754 snprintf(buf, sizeof buf, "%zu", relnum);
756 ret += " in reloc section ";
757 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
759 ret += " (" + this->object->section_name(this->reloc_shndx);
760 ret += ") for section ";
761 snprintf(buf, sizeof buf, "%u", this->data_shndx);
763 ret += " (" + this->object->section_name(this->data_shndx) + ")";
767 } // End namespace gold.
772 using namespace gold;
774 // Read an ELF file with the header and return the appropriate
775 // instance of Object.
777 template<int size, bool big_endian>
779 make_elf_sized_object(const std::string& name, Input_file* input_file,
780 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
782 int et = ehdr.get_e_type();
783 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
785 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
786 program_name, name.c_str(), static_cast<int>(et));
790 if (et == elfcpp::ET_REL)
792 Sized_relobj<size, big_endian>* obj =
793 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
800 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
801 program_name, name.c_str());
803 // Sized_dynobj<size, big_endian>* obj =
804 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
811 } // End anonymous namespace.
816 // Read an ELF file and return the appropriate instance of Object.
819 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
820 const unsigned char* p, off_t bytes)
822 if (bytes < elfcpp::EI_NIDENT)
824 fprintf(stderr, _("%s: %s: ELF file too short\n"),
825 program_name, name.c_str());
829 int v = p[elfcpp::EI_VERSION];
830 if (v != elfcpp::EV_CURRENT)
832 if (v == elfcpp::EV_NONE)
833 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
834 program_name, name.c_str());
836 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
837 program_name, name.c_str(), v);
841 int c = p[elfcpp::EI_CLASS];
842 if (c == elfcpp::ELFCLASSNONE)
844 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
845 program_name, name.c_str());
848 else if (c != elfcpp::ELFCLASS32
849 && c != elfcpp::ELFCLASS64)
851 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
852 program_name, name.c_str(), c);
856 int d = p[elfcpp::EI_DATA];
857 if (d == elfcpp::ELFDATANONE)
859 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
860 program_name, name.c_str());
863 else if (d != elfcpp::ELFDATA2LSB
864 && d != elfcpp::ELFDATA2MSB)
866 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
867 program_name, name.c_str(), d);
871 bool big_endian = d == elfcpp::ELFDATA2MSB;
873 if (c == elfcpp::ELFCLASS32)
875 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
877 fprintf(stderr, _("%s: %s: ELF file too short\n"),
878 program_name, name.c_str());
883 elfcpp::Ehdr<32, true> ehdr(p);
884 return make_elf_sized_object<32, true>(name, input_file,
889 elfcpp::Ehdr<32, false> ehdr(p);
890 return make_elf_sized_object<32, false>(name, input_file,
896 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
898 fprintf(stderr, _("%s: %s: ELF file too short\n"),
899 program_name, name.c_str());
904 elfcpp::Ehdr<64, true> ehdr(p);
905 return make_elf_sized_object<64, true>(name, input_file,
910 elfcpp::Ehdr<64, false> ehdr(p);
911 return make_elf_sized_object<64, false>(name, input_file,
917 // Instantiate the templates we need. We could use the configure
918 // script to restrict this to only the ones for implemented targets.
921 class Sized_relobj<32, false>;
924 class Sized_relobj<32, true>;
927 class Sized_relobj<64, false>;
930 class Sized_relobj<64, true>;
933 struct Relocate_info<32, false>;
936 struct Relocate_info<32, true>;
939 struct Relocate_info<64, false>;
942 struct Relocate_info<64, true>;
944 } // End namespace gold.