Add Elf_file interface which can be used by both Sized_relobj and
[binutils.git] / gold / object.cc
blob677c731fb745b070254bbc968d382fc85abd7469
1 // object.cc -- support for an object file for linking in gold
3 #include "gold.h"
5 #include <cerrno>
6 #include <cstring>
7 #include <cassert>
8 #include <cstdarg>
10 #include "target-select.h"
11 #include "layout.h"
12 #include "output.h"
13 #include "symtab.h"
14 #include "object.h"
15 #include "dynobj.h"
17 namespace gold
20 // Class Object.
22 // Report an error for the elfcpp::Elf_file interface.
24 void
25 Object::error(const char* format, ...)
27 va_list args;
29 fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
30 va_start(args, format);
31 vfprintf(stderr, format, args);
32 va_end(args);
33 putc('\n', stderr);
35 gold_exit(false);
38 // Return a view of the contents of a section.
40 const unsigned char*
41 Object::section_contents(unsigned int shndx, off_t* plen)
43 Location loc(this->do_section_contents(shndx));
44 *plen = loc.data_size;
45 return this->get_view(loc.file_offset, loc.data_size);
48 // Class Sized_relobj.
50 template<int size, bool big_endian>
51 Sized_relobj<size, big_endian>::Sized_relobj(
52 const std::string& name,
53 Input_file* input_file,
54 off_t offset,
55 const elfcpp::Ehdr<size, big_endian>& ehdr)
56 : Relobj(name, input_file, offset),
57 elf_file_(this, ehdr),
58 section_headers_(NULL),
59 symtab_shndx_(0),
60 local_symbol_count_(0),
61 output_local_symbol_count_(0),
62 symbols_(NULL),
63 local_symbol_offset_(0),
64 values_(NULL)
68 template<int size, bool big_endian>
69 Sized_relobj<size, big_endian>::~Sized_relobj()
73 // Set up an object file based on the file header. This sets up the
74 // target and reads the section information.
76 template<int size, bool big_endian>
77 void
78 Sized_relobj<size, big_endian>::setup(
79 const elfcpp::Ehdr<size, big_endian>& ehdr)
81 int machine = ehdr.get_e_machine();
82 Target* target = select_target(machine, size, big_endian,
83 ehdr.get_e_ident()[elfcpp::EI_OSABI],
84 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
85 if (target == NULL)
87 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
88 program_name, this->name().c_str(), machine);
89 gold_exit(false);
91 this->set_target(target);
93 unsigned int shnum = this->elf_file_.shnum();
94 this->set_shnum(shnum);
95 if (shnum == 0)
96 return;
98 // We store the section headers in a File_view until do_read_symbols.
99 off_t shoff = this->elf_file_.shoff();
100 this->section_headers_ = this->get_lasting_view(shoff,
101 shnum * This::shdr_size);
103 // Find the SHT_SYMTAB section. The ELF standard says that maybe in
104 // the future there can be more than one SHT_SYMTAB section. Until
105 // somebody figures out how that could work, we assume there is only
106 // one.
107 const unsigned char* p = this->section_headers_->data();
109 // Skip the first section, which is always empty.
110 p += This::shdr_size;
111 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
113 typename This::Shdr shdr(p);
114 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
116 this->symtab_shndx_ = i;
117 break;
122 // Read the sections and symbols from an object file.
124 template<int size, bool big_endian>
125 void
126 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
128 // Transfer our view of the section headers to SD.
129 sd->section_headers = this->section_headers_;
130 this->section_headers_ = NULL;
132 // Read the section names.
133 const unsigned char* pshdrs = sd->section_headers->data();
134 const unsigned char* pshdrnames = (pshdrs
135 + (this->elf_file_.shstrndx()
136 * This::shdr_size));
137 typename This::Shdr shdrnames(pshdrnames);
138 sd->section_names_size = shdrnames.get_sh_size();
139 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
140 sd->section_names_size);
142 if (this->symtab_shndx_ == 0)
144 // No symbol table. Weird but legal.
145 sd->symbols = NULL;
146 sd->symbols_size = 0;
147 sd->symbol_names = NULL;
148 sd->symbol_names_size = 0;
149 return;
152 // Get the symbol table section header.
153 typename This::Shdr symtabshdr(pshdrs
154 + this->symtab_shndx_ * This::shdr_size);
155 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
157 // We only need the external symbols.
158 const int sym_size = This::sym_size;
159 const unsigned int loccount = symtabshdr.get_sh_info();
160 this->local_symbol_count_ = loccount;
161 off_t locsize = loccount * sym_size;
162 off_t extoff = symtabshdr.get_sh_offset() + locsize;
163 off_t extsize = symtabshdr.get_sh_size() - locsize;
165 // Read the symbol table.
166 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
168 // Read the section header for the symbol names.
169 unsigned int shnum = this->shnum();
170 unsigned int strtab_shnum = symtabshdr.get_sh_link();
171 if (strtab_shnum == 0 || strtab_shnum >= shnum)
173 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
174 program_name, this->name().c_str(), strtab_shnum);
175 gold_exit(false);
177 typename This::Shdr strtabshdr(pshdrs + strtab_shnum * This::shdr_size);
178 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
180 fprintf(stderr,
181 _("%s: %s: symbol table name section has wrong type: %u\n"),
182 program_name, this->name().c_str(),
183 static_cast<unsigned int>(strtabshdr.get_sh_type()));
184 gold_exit(false);
187 // Read the symbol names.
188 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
189 strtabshdr.get_sh_size());
191 sd->symbols = fvsymtab;
192 sd->symbols_size = extsize;
193 sd->symbol_names = fvstrtab;
194 sd->symbol_names_size = strtabshdr.get_sh_size();
197 // Return whether to include a section group in the link. LAYOUT is
198 // used to keep track of which section groups we have already seen.
199 // INDEX is the index of the section group and SHDR is the section
200 // header. If we do not want to include this group, we set bits in
201 // OMIT for each section which should be discarded.
203 template<int size, bool big_endian>
204 bool
205 Sized_relobj<size, big_endian>::include_section_group(
206 Layout* layout,
207 unsigned int index,
208 const elfcpp::Shdr<size, big_endian>& shdr,
209 std::vector<bool>* omit)
211 // Read the section contents.
212 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
213 shdr.get_sh_size());
214 const elfcpp::Elf_Word* pword =
215 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
217 // The first word contains flags. We only care about COMDAT section
218 // groups. Other section groups are always included in the link
219 // just like ordinary sections.
220 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
221 if ((flags & elfcpp::GRP_COMDAT) == 0)
222 return true;
224 // Look up the group signature, which is the name of a symbol. This
225 // is a lot of effort to go to to read a string. Why didn't they
226 // just use the name of the SHT_GROUP section as the group
227 // signature?
229 // Get the appropriate symbol table header (this will normally be
230 // the single SHT_SYMTAB section, but in principle it need not be).
231 const unsigned int link = shdr.get_sh_link();
232 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
234 // Read the symbol table entry.
235 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
237 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
238 program_name, this->name().c_str(), index, shdr.get_sh_info());
239 gold_exit(false);
241 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
242 const unsigned char* psym = this->get_view(symoff, This::sym_size);
243 elfcpp::Sym<size, big_endian> sym(psym);
245 // Read the symbol table names.
246 off_t symnamelen;
247 const unsigned char* psymnamesu;
248 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen);
249 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
251 // Get the section group signature.
252 if (sym.get_st_name() >= symnamelen)
254 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
255 program_name, this->name().c_str(), shdr.get_sh_info(),
256 sym.get_st_name());
257 gold_exit(false);
260 const char* signature = psymnames + sym.get_st_name();
262 // It seems that some versions of gas will create a section group
263 // associated with a section symbol, and then fail to give a name to
264 // the section symbol. In such a case, use the name of the section.
265 // FIXME.
266 std::string secname;
267 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
269 secname = this->section_name(sym.get_st_shndx());
270 signature = secname.c_str();
273 // Record this section group, and see whether we've already seen one
274 // with the same signature.
275 if (layout->add_comdat(signature, true))
276 return true;
278 // This is a duplicate. We want to discard the sections in this
279 // group.
280 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
281 for (size_t i = 1; i < count; ++i)
283 elfcpp::Elf_Word secnum =
284 elfcpp::Swap<32, big_endian>::readval(pword + i);
285 if (secnum >= this->shnum())
287 fprintf(stderr,
288 _("%s: %s: section %u in section group %u out of range"),
289 program_name, this->name().c_str(), secnum,
290 index);
291 gold_exit(false);
293 (*omit)[secnum] = true;
296 return false;
299 // Whether to include a linkonce section in the link. NAME is the
300 // name of the section and SHDR is the section header.
302 // Linkonce sections are a GNU extension implemented in the original
303 // GNU linker before section groups were defined. The semantics are
304 // that we only include one linkonce section with a given name. The
305 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
306 // where T is the type of section and SYMNAME is the name of a symbol.
307 // In an attempt to make linkonce sections interact well with section
308 // groups, we try to identify SYMNAME and use it like a section group
309 // signature. We want to block section groups with that signature,
310 // but not other linkonce sections with that signature. We also use
311 // the full name of the linkonce section as a normal section group
312 // signature.
314 template<int size, bool big_endian>
315 bool
316 Sized_relobj<size, big_endian>::include_linkonce_section(
317 Layout* layout,
318 const char* name,
319 const elfcpp::Shdr<size, big_endian>&)
321 const char* symname = strrchr(name, '.') + 1;
322 bool include1 = layout->add_comdat(symname, false);
323 bool include2 = layout->add_comdat(name, true);
324 return include1 && include2;
327 // Lay out the input sections. We walk through the sections and check
328 // whether they should be included in the link. If they should, we
329 // pass them to the Layout object, which will return an output section
330 // and an offset.
332 template<int size, bool big_endian>
333 void
334 Sized_relobj<size, big_endian>::do_layout(const General_options& options,
335 Symbol_table* symtab,
336 Layout* layout,
337 Read_symbols_data* sd)
339 unsigned int shnum = this->shnum();
340 if (shnum == 0)
341 return;
343 // Get the section headers.
344 const unsigned char* pshdrs = sd->section_headers->data();
346 // Get the section names.
347 const unsigned char* pnamesu = sd->section_names->data();
348 const char* pnames = reinterpret_cast<const char*>(pnamesu);
350 std::vector<Map_to_output>& map_sections(this->map_to_output());
351 map_sections.resize(shnum);
353 // Keep track of which sections to omit.
354 std::vector<bool> omit(shnum, false);
356 const char warn_prefix[] = ".gnu.warning.";
357 const int warn_prefix_len = sizeof warn_prefix - 1;
359 // Skip the first, dummy, section.
360 pshdrs += This::shdr_size;
361 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
363 typename This::Shdr shdr(pshdrs);
365 if (shdr.get_sh_name() >= sd->section_names_size)
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);
374 const char* name = pnames + shdr.get_sh_name();
376 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
378 symtab->add_warning(name + warn_prefix_len, this, i);
379 if (!options.is_relocatable())
380 omit[i] = true;
383 bool discard = omit[i];
384 if (!discard)
386 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
388 if (!this->include_section_group(layout, i, shdr, &omit))
389 discard = true;
391 else if (Layout::is_linkonce(name))
393 if (!this->include_linkonce_section(layout, name, shdr))
394 discard = true;
398 if (discard)
400 // Do not include this section in the link.
401 map_sections[i].output_section = NULL;
402 continue;
405 off_t offset;
406 Output_section* os = layout->layout(this, i, name, shdr, &offset);
408 map_sections[i].output_section = os;
409 map_sections[i].offset = offset;
412 delete sd->section_headers;
413 sd->section_headers = NULL;
414 delete sd->section_names;
415 sd->section_names = NULL;
418 // Add the symbols to the symbol table.
420 template<int size, bool big_endian>
421 void
422 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
423 Read_symbols_data* sd)
425 if (sd->symbols == NULL)
427 assert(sd->symbol_names == NULL);
428 return;
431 const int sym_size = This::sym_size;
432 size_t symcount = sd->symbols_size / sym_size;
433 if (symcount * sym_size != sd->symbols_size)
435 fprintf(stderr,
436 _("%s: %s: size of symbols is not multiple of symbol size\n"),
437 program_name, this->name().c_str());
438 gold_exit(false);
441 this->symbols_ = new Symbol*[symcount];
443 const char* sym_names =
444 reinterpret_cast<const char*>(sd->symbol_names->data());
445 symtab->add_from_object<size, big_endian>(this, sd->symbols->data(),
446 symcount, sym_names,
447 sd->symbol_names_size,
448 this->symbols_);
450 delete sd->symbols;
451 sd->symbols = NULL;
452 delete sd->symbol_names;
453 sd->symbol_names = NULL;
456 // Finalize the local symbols. Here we record the file offset at
457 // which they should be output, we add their names to *POOL, and we
458 // add their values to THIS->VALUES_. Return the new file offset.
459 // This function is always called from the main thread. The actual
460 // output of the local symbols will occur in a separate task.
462 template<int size, bool big_endian>
463 off_t
464 Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
465 Stringpool* pool)
467 if (this->symtab_shndx_ == 0)
469 // This object has no symbols. Weird but legal.
470 return off;
473 off = align_address(off, size >> 3);
475 this->local_symbol_offset_ = off;
477 // Read the symbol table section header.
478 const unsigned int symtab_shndx = this->symtab_shndx_;
479 typename This::Shdr symtabshdr(this,
480 this->elf_file_.section_header(symtab_shndx));
481 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
483 // Read the local symbols.
484 const int sym_size = This::sym_size;
485 const unsigned int loccount = this->local_symbol_count_;
486 assert(loccount == symtabshdr.get_sh_info());
487 off_t locsize = loccount * sym_size;
488 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
489 locsize);
491 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
493 // Read the symbol names.
494 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
495 off_t strtab_size;
496 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
497 &strtab_size);
498 const char* pnames = reinterpret_cast<const char*>(pnamesu);
500 // Loop over the local symbols.
502 std::vector<Map_to_output>& mo(this->map_to_output());
503 unsigned int shnum = this->shnum();
504 unsigned int count = 0;
505 // Skip the first, dummy, symbol.
506 psyms += sym_size;
507 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
509 elfcpp::Sym<size, big_endian> sym(psyms);
511 unsigned int shndx = sym.get_st_shndx();
513 if (shndx >= elfcpp::SHN_LORESERVE)
515 if (shndx == elfcpp::SHN_ABS)
516 this->values_[i] = sym.get_st_value();
517 else
519 // FIXME: Handle SHN_XINDEX.
520 fprintf(stderr,
521 _("%s: %s: unknown section index %u "
522 "for local symbol %u\n"),
523 program_name, this->name().c_str(), shndx, i);
524 gold_exit(false);
527 else
529 if (shndx >= shnum)
531 fprintf(stderr,
532 _("%s: %s: local symbol %u section index %u "
533 "out of range\n"),
534 program_name, this->name().c_str(), i, shndx);
535 gold_exit(false);
538 if (mo[shndx].output_section == NULL)
540 this->values_[i] = 0;
541 continue;
544 this->values_[i] = (mo[shndx].output_section->address()
545 + mo[shndx].offset
546 + sym.get_st_value());
549 if (sym.get_st_type() != elfcpp::STT_SECTION)
551 if (sym.get_st_name() >= strtab_size)
553 fprintf(stderr,
554 _("%s: %s: local symbol %u section name "
555 "out of range: %u >= %u\n"),
556 program_name, this->name().c_str(),
557 i, sym.get_st_name(),
558 static_cast<unsigned int>(strtab_size));
559 gold_exit(false);
562 pool->add(pnames + sym.get_st_name(), NULL);
563 off += sym_size;
564 ++count;
568 this->output_local_symbol_count_ = count;
570 return off;
573 // Write out the local symbols.
575 template<int size, bool big_endian>
576 void
577 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
578 const Stringpool* sympool)
580 if (this->symtab_shndx_ == 0)
582 // This object has no symbols. Weird but legal.
583 return;
586 // Read the symbol table section header.
587 const unsigned int symtab_shndx = this->symtab_shndx_;
588 typename This::Shdr symtabshdr(this,
589 this->elf_file_.section_header(symtab_shndx));
590 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
591 const unsigned int loccount = this->local_symbol_count_;
592 assert(loccount == symtabshdr.get_sh_info());
594 // Read the local symbols.
595 const int sym_size = This::sym_size;
596 off_t locsize = loccount * sym_size;
597 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
598 locsize);
600 // Read the symbol names.
601 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
602 off_t strtab_size;
603 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
604 &strtab_size);
605 const char* pnames = reinterpret_cast<const char*>(pnamesu);
607 // Get a view into the output file.
608 off_t output_size = this->output_local_symbol_count_ * sym_size;
609 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
610 output_size);
612 std::vector<Map_to_output>& mo(this->map_to_output());
614 psyms += sym_size;
615 unsigned char* ov = oview;
616 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
618 elfcpp::Sym<size, big_endian> isym(psyms);
620 if (isym.get_st_type() == elfcpp::STT_SECTION)
621 continue;
623 unsigned int st_shndx = isym.get_st_shndx();
624 if (st_shndx < elfcpp::SHN_LORESERVE)
626 assert(st_shndx < mo.size());
627 if (mo[st_shndx].output_section == NULL)
628 continue;
629 st_shndx = mo[st_shndx].output_section->out_shndx();
632 elfcpp::Sym_write<size, big_endian> osym(ov);
634 assert(isym.get_st_name() < strtab_size);
635 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
636 osym.put_st_value(this->values_[i]);
637 osym.put_st_size(isym.get_st_size());
638 osym.put_st_info(isym.get_st_info());
639 osym.put_st_other(isym.get_st_other());
640 osym.put_st_shndx(st_shndx);
642 ov += sym_size;
645 assert(ov - oview == output_size);
647 of->write_output_view(this->local_symbol_offset_, output_size, oview);
650 // Input_objects methods.
652 // Add a regular relocatable object to the list.
654 void
655 Input_objects::add_object(Object* obj)
657 if (obj->is_dynamic())
658 this->dynobj_list_.push_back(static_cast<Dynobj*>(obj));
659 else
660 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
662 Target* target = obj->target();
663 if (this->target_ == NULL)
664 this->target_ = target;
665 else if (this->target_ != target)
667 fprintf(stderr, "%s: %s: incompatible target\n",
668 program_name, obj->name().c_str());
669 gold_exit(false);
673 // Relocate_info methods.
675 // Return a string describing the location of a relocation. This is
676 // only used in error messages.
678 template<int size, bool big_endian>
679 std::string
680 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
682 std::string ret(this->object->name());
683 ret += ": reloc ";
684 char buf[100];
685 snprintf(buf, sizeof buf, "%zu", relnum);
686 ret += buf;
687 ret += " in reloc section ";
688 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
689 ret += buf;
690 ret += " (" + this->object->section_name(this->reloc_shndx);
691 ret += ") for section ";
692 snprintf(buf, sizeof buf, "%u", this->data_shndx);
693 ret += buf;
694 ret += " (" + this->object->section_name(this->data_shndx) + ")";
695 return ret;
698 } // End namespace gold.
700 namespace
703 using namespace gold;
705 // Read an ELF file with the header and return the appropriate
706 // instance of Object.
708 template<int size, bool big_endian>
709 Object*
710 make_elf_sized_object(const std::string& name, Input_file* input_file,
711 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
713 int et = ehdr.get_e_type();
714 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
716 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
717 program_name, name.c_str(), static_cast<int>(et));
718 gold_exit(false);
721 if (et == elfcpp::ET_REL)
723 Sized_relobj<size, big_endian>* obj =
724 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
725 obj->setup(ehdr);
726 return obj;
728 else
730 // elfcpp::ET_DYN
731 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
732 program_name, name.c_str());
733 gold_exit(false);
734 // Sized_dynobj<size, big_endian>* obj =
735 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
736 // offset, ehdr);
737 // obj->setup(ehdr);
738 // return obj;
742 } // End anonymous namespace.
744 namespace gold
747 // Read an ELF file and return the appropriate instance of Object.
749 Object*
750 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
751 const unsigned char* p, off_t bytes)
753 if (bytes < elfcpp::EI_NIDENT)
755 fprintf(stderr, _("%s: %s: ELF file too short\n"),
756 program_name, name.c_str());
757 gold_exit(false);
760 int v = p[elfcpp::EI_VERSION];
761 if (v != elfcpp::EV_CURRENT)
763 if (v == elfcpp::EV_NONE)
764 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
765 program_name, name.c_str());
766 else
767 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
768 program_name, name.c_str(), v);
769 gold_exit(false);
772 int c = p[elfcpp::EI_CLASS];
773 if (c == elfcpp::ELFCLASSNONE)
775 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
776 program_name, name.c_str());
777 gold_exit(false);
779 else if (c != elfcpp::ELFCLASS32
780 && c != elfcpp::ELFCLASS64)
782 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
783 program_name, name.c_str(), c);
784 gold_exit(false);
787 int d = p[elfcpp::EI_DATA];
788 if (d == elfcpp::ELFDATANONE)
790 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
791 program_name, name.c_str());
792 gold_exit(false);
794 else if (d != elfcpp::ELFDATA2LSB
795 && d != elfcpp::ELFDATA2MSB)
797 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
798 program_name, name.c_str(), d);
799 gold_exit(false);
802 bool big_endian = d == elfcpp::ELFDATA2MSB;
804 if (c == elfcpp::ELFCLASS32)
806 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
808 fprintf(stderr, _("%s: %s: ELF file too short\n"),
809 program_name, name.c_str());
810 gold_exit(false);
812 if (big_endian)
814 elfcpp::Ehdr<32, true> ehdr(p);
815 return make_elf_sized_object<32, true>(name, input_file,
816 offset, ehdr);
818 else
820 elfcpp::Ehdr<32, false> ehdr(p);
821 return make_elf_sized_object<32, false>(name, input_file,
822 offset, ehdr);
825 else
827 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
829 fprintf(stderr, _("%s: %s: ELF file too short\n"),
830 program_name, name.c_str());
831 gold_exit(false);
833 if (big_endian)
835 elfcpp::Ehdr<64, true> ehdr(p);
836 return make_elf_sized_object<64, true>(name, input_file,
837 offset, ehdr);
839 else
841 elfcpp::Ehdr<64, false> ehdr(p);
842 return make_elf_sized_object<64, false>(name, input_file,
843 offset, ehdr);
848 // Instantiate the templates we need. We could use the configure
849 // script to restrict this to only the ones for implemented targets.
851 template
852 class Sized_relobj<32, false>;
854 template
855 class Sized_relobj<32, true>;
857 template
858 class Sized_relobj<64, false>;
860 template
861 class Sized_relobj<64, true>;
863 template
864 struct Relocate_info<32, false>;
866 template
867 struct Relocate_info<32, true>;
869 template
870 struct Relocate_info<64, false>;
872 template
873 struct Relocate_info<64, true>;
875 } // End namespace gold.