2006-10-24 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / object.cc
blob5efb3cbd7fdb1df6fd4b25b3819e207d66efca0a
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>
9 #include "object.h"
10 #include "target-select.h"
11 #include "layout.h"
12 #include "output.h"
14 namespace gold
17 // Class Object.
19 const unsigned char*
20 Object::get_view(off_t start, off_t size)
22 return this->input_file_->file().get_view(start + this->offset_, size);
25 void
26 Object::read(off_t start, off_t size, void* p)
28 this->input_file_->file().read(start + this->offset_, size, p);
31 File_view*
32 Object::get_lasting_view(off_t start, off_t size)
34 return this->input_file_->file().get_lasting_view(start + this->offset_,
35 size);
38 // Class Sized_object.
40 template<int size, bool big_endian>
41 Sized_object<size, big_endian>::Sized_object(
42 const std::string& name,
43 Input_file* input_file,
44 off_t offset,
45 const elfcpp::Ehdr<size, big_endian>& ehdr)
46 : Object(name, input_file, false, offset),
47 section_headers_(NULL),
48 flags_(ehdr.get_e_flags()),
49 shoff_(ehdr.get_e_shoff()),
50 shstrndx_(0),
51 symtab_shnum_(0),
52 local_symbol_count_(0),
53 output_local_symbol_count_(0),
54 symbols_(NULL),
55 local_symbol_offset_(0),
56 values_(NULL)
58 if (ehdr.get_e_ehsize() != This::ehdr_size)
60 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
61 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
62 This::ehdr_size);
63 gold_exit(false);
65 if (ehdr.get_e_shentsize() != This::shdr_size)
67 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
68 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
69 This::shdr_size);
70 gold_exit(false);
74 template<int size, bool big_endian>
75 Sized_object<size, big_endian>::~Sized_object()
79 // Read the section header for section SHNUM.
81 template<int size, bool big_endian>
82 const unsigned char*
83 Sized_object<size, big_endian>::section_header(unsigned int shnum)
85 assert(shnum < this->shnum());
86 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
87 return this->get_view(symtabshdroff, This::shdr_size);
90 // Return the name of section SHNUM.
92 template<int size, bool big_endian>
93 std::string
94 Sized_object<size, big_endian>::do_section_name(unsigned int shnum)
96 Task_lock_obj<Object> tl(*this);
98 // Read the section names.
99 typename This::Shdr shdrnames(this->section_header(this->shstrndx_));
100 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
101 shdrnames.get_sh_size());
102 const char* pnames = reinterpret_cast<const char*>(pnamesu);
104 typename This::Shdr shdr(this->section_header(shnum));
105 if (shdr.get_sh_name() >= shdrnames.get_sh_size())
107 fprintf(stderr,
108 _("%s: %s: bad section name offset for section %u: %lu\n"),
109 program_name, this->name().c_str(), shnum,
110 static_cast<unsigned long>(shdr.get_sh_name()));
111 gold_exit(false);
114 return std::string(pnames + shdr.get_sh_name());
117 // Set up an object file bsaed on the file header. This sets up the
118 // target and reads the section information.
120 template<int size, bool big_endian>
121 void
122 Sized_object<size, big_endian>::setup(
123 const elfcpp::Ehdr<size, big_endian>& ehdr)
125 int machine = ehdr.get_e_machine();
126 Target* target = select_target(machine, size, big_endian,
127 ehdr.get_e_ident()[elfcpp::EI_OSABI],
128 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
129 if (target == NULL)
131 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
132 program_name, this->name().c_str(), machine);
133 gold_exit(false);
135 this->set_target(target);
137 unsigned int shnum = ehdr.get_e_shnum();
138 unsigned int shstrndx = ehdr.get_e_shstrndx();
139 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
140 && this->shoff_ != 0)
142 typename This::Shdr shdr(this->section_header(0));
143 if (shnum == 0)
144 shnum = shdr.get_sh_size();
145 if (shstrndx == elfcpp::SHN_XINDEX)
146 shstrndx = shdr.get_sh_link();
148 this->set_shnum(shnum);
149 this->shstrndx_ = shstrndx;
151 if (shnum == 0)
152 return;
154 // We store the section headers in a File_view until do_read_symbols.
155 this->section_headers_ = this->get_lasting_view(this->shoff_,
156 shnum * This::shdr_size);
158 // Find the SHT_SYMTAB section. The ELF standard says that maybe in
159 // the future there can be more than one SHT_SYMTAB section. Until
160 // somebody figures out how that could work, we assume there is only
161 // one.
162 const unsigned char* p = this->section_headers_->data();
164 // Skip the first section, which is always empty.
165 p += This::shdr_size;
166 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
168 typename This::Shdr shdr(p);
169 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
171 this->symtab_shnum_ = i;
172 break;
177 // Read the sections and symbols from an object file.
179 template<int size, bool big_endian>
180 void
181 Sized_object<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
183 // Transfer our view of the section headers to SD.
184 sd->section_headers = this->section_headers_;
185 this->section_headers_ = NULL;
187 // Read the section names.
188 const unsigned char* pshdrs = sd->section_headers->data();
189 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
190 typename This::Shdr shdrnames(pshdrnames);
191 sd->section_names_size = shdrnames.get_sh_size();
192 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
193 sd->section_names_size);
195 if (this->symtab_shnum_ == 0)
197 // No symbol table. Weird but legal.
198 sd->symbols = NULL;
199 sd->symbols_size = 0;
200 sd->symbol_names = NULL;
201 sd->symbol_names_size = 0;
202 return;
205 // Get the symbol table section header.
206 typename This::Shdr symtabshdr(pshdrs
207 + this->symtab_shnum_ * This::shdr_size);
208 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
210 // We only need the external symbols.
211 const int sym_size = This::sym_size;
212 const unsigned int loccount = symtabshdr.get_sh_info();
213 this->local_symbol_count_ = loccount;
214 off_t locsize = loccount * sym_size;
215 off_t extoff = symtabshdr.get_sh_offset() + locsize;
216 off_t extsize = symtabshdr.get_sh_size() - locsize;
218 // Read the symbol table.
219 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
221 // Read the section header for the symbol names.
222 unsigned int shnum = this->shnum();
223 unsigned int strtab_shnum = symtabshdr.get_sh_link();
224 if (strtab_shnum == 0 || strtab_shnum >= shnum)
226 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
227 program_name, this->name().c_str(), strtab_shnum);
228 gold_exit(false);
230 typename This::Shdr strtabshdr(pshdrs + strtab_shnum * This::shdr_size);
231 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
233 fprintf(stderr,
234 _("%s: %s: symbol table name section has wrong type: %u\n"),
235 program_name, this->name().c_str(),
236 static_cast<unsigned int>(strtabshdr.get_sh_type()));
237 gold_exit(false);
240 // Read the symbol names.
241 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
242 strtabshdr.get_sh_size());
244 sd->symbols = fvsymtab;
245 sd->symbols_size = extsize;
246 sd->symbol_names = fvstrtab;
247 sd->symbol_names_size = strtabshdr.get_sh_size();
250 // Return whether to include a section group in the link. LAYOUT is
251 // used to keep track of which section groups we have already seen.
252 // INDEX is the index of the section group and SHDR is the section
253 // header. If we do not want to include this group, we set bits in
254 // OMIT for each section which should be discarded.
256 template<int size, bool big_endian>
257 bool
258 Sized_object<size, big_endian>::include_section_group(
259 Layout* layout,
260 unsigned int index,
261 const elfcpp::Shdr<size, big_endian>& shdr,
262 std::vector<bool>* omit)
264 // Read the section contents.
265 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
266 shdr.get_sh_size());
267 const elfcpp::Elf_Word* pword =
268 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
270 // The first word contains flags. We only care about COMDAT section
271 // groups. Other section groups are always included in the link
272 // just like ordinary sections.
273 elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
274 if ((flags & elfcpp::GRP_COMDAT) == 0)
275 return true;
277 // Look up the group signature, which is the name of a symbol. This
278 // is a lot of effort to go to to read a string. Why didn't they
279 // just use the name of the SHT_GROUP section as the group
280 // signature?
282 // Get the appropriate symbol table header (this will normally be
283 // the single SHT_SYMTAB section, but in principle it need not be).
284 if (shdr.get_sh_link() >= this->shnum())
286 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
287 program_name, this->name().c_str(), index, shdr.get_sh_link());
288 gold_exit(false);
291 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
293 // Read the symbol table entry.
294 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
296 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
297 program_name, this->name().c_str(), index, shdr.get_sh_info());
298 gold_exit(false);
300 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
301 const unsigned char* psym = this->get_view(symoff, This::sym_size);
302 elfcpp::Sym<size, big_endian> sym(psym);
304 // Read the section header for the symbol table names.
305 if (symshdr.get_sh_link() >= this->shnum())
307 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
308 program_name, this->name().c_str(), shdr.get_sh_link(),
309 symshdr.get_sh_link());
310 gold_exit(false);
313 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
315 // Read the symbol table names.
316 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
317 symnamehdr.get_sh_size());
318 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
320 // Get the section group signature.
321 if (sym.get_st_name() >= symnamehdr.get_sh_size())
323 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
324 program_name, this->name().c_str(), shdr.get_sh_info(),
325 sym.get_st_name());
326 gold_exit(false);
329 const char* signature = psymnames + sym.get_st_name();
331 // Record this section group, and see whether we've already seen one
332 // with the same signature.
333 if (layout->add_comdat(signature, true))
334 return true;
336 // This is a duplicate. We want to discard the sections in this
337 // group.
338 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
339 for (size_t i = 1; i < count; ++i)
341 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
342 if (secnum >= this->shnum())
344 fprintf(stderr,
345 _("%s: %s: section %u in section group %u out of range"),
346 program_name, this->name().c_str(), secnum,
347 index);
348 gold_exit(false);
350 (*omit)[secnum] = true;
353 return false;
356 // Whether to include a linkonce section in the link. NAME is the
357 // name of the section and SHDR is the section header.
359 // Linkonce sections are a GNU extension implemented in the original
360 // GNU linker before section groups were defined. The semantics are
361 // that we only include one linkonce section with a given name. The
362 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
363 // where T is the type of section and SYMNAME is the name of a symbol.
364 // In an attempt to make linkonce sections interact well with section
365 // groups, we try to identify SYMNAME and use it like a section group
366 // signature. We want to block section groups with that signature,
367 // but not other linkonce sections with that signature. We also use
368 // the full name of the linkonce section as a normal section group
369 // signature.
371 template<int size, bool big_endian>
372 bool
373 Sized_object<size, big_endian>::include_linkonce_section(
374 Layout* layout,
375 const char* name,
376 const elfcpp::Shdr<size, big_endian>&)
378 const char* symname = strrchr(name, '.') + 1;
379 bool include1 = layout->add_comdat(symname, false);
380 bool include2 = layout->add_comdat(name, true);
381 return include1 && include2;
384 // Lay out the input sections. We walk through the sections and check
385 // whether they should be included in the link. If they should, we
386 // pass them to the Layout object, which will return an output section
387 // and an offset.
389 template<int size, bool big_endian>
390 void
391 Sized_object<size, big_endian>::do_layout(Layout* layout,
392 Read_symbols_data* sd)
394 unsigned int shnum = this->shnum();
395 if (shnum == 0)
396 return;
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 for (unsigned int i = 0; i < shnum; ++i, pshdrs += This::shdr_size)
413 typename This::Shdr shdr(pshdrs);
415 if (shdr.get_sh_name() >= sd->section_names_size)
417 fprintf(stderr,
418 _("%s: %s: bad section name offset for section %u: %lu\n"),
419 program_name, this->name().c_str(), i,
420 static_cast<unsigned long>(shdr.get_sh_name()));
421 gold_exit(false);
424 const char* name = pnames + shdr.get_sh_name();
426 bool discard = omit[i];
427 if (!discard)
429 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
431 if (!this->include_section_group(layout, i, shdr, &omit))
432 discard = true;
434 else if (Layout::is_linkonce(name))
436 if (!this->include_linkonce_section(layout, name, shdr))
437 discard = true;
441 if (discard)
443 // Do not include this section in the link.
444 map_sections[i].output_section = NULL;
445 continue;
448 off_t offset;
449 Output_section* os = layout->layout(this, name, shdr, &offset);
451 map_sections[i].output_section = os;
452 map_sections[i].offset = offset;
455 delete sd->section_headers;
456 sd->section_headers = NULL;
457 delete sd->section_names;
458 sd->section_names = NULL;
461 // Add the symbols to the symbol table.
463 template<int size, bool big_endian>
464 void
465 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
466 Read_symbols_data* sd)
468 if (sd->symbols == NULL)
470 assert(sd->symbol_names == NULL);
471 return;
474 const int sym_size = This::sym_size;
475 size_t symcount = sd->symbols_size / sym_size;
476 if (symcount * sym_size != sd->symbols_size)
478 fprintf(stderr,
479 _("%s: %s: size of symbols is not multiple of symbol size\n"),
480 program_name, this->name().c_str());
481 gold_exit(false);
484 this->symbols_ = new Symbol*[symcount];
486 const unsigned char* psyms = sd->symbols->data();
487 const elfcpp::Sym<size, big_endian>* syms =
488 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(psyms);
489 const char* sym_names =
490 reinterpret_cast<const char*>(sd->symbol_names->data());
491 symtab->add_from_object(this, syms, symcount, sym_names,
492 sd->symbol_names_size, this->symbols_);
494 delete sd->symbols;
495 sd->symbols = NULL;
496 delete sd->symbol_names;
497 sd->symbol_names = NULL;
500 // Finalize the local symbols. Here we record the file offset at
501 // which they should be output, we add their names to *POOL, and we
502 // add their values to THIS->VALUES_. Return the new file offset.
503 // This function is always called from the main thread. The actual
504 // output of the local symbols will occur in a separate task.
506 template<int size, bool big_endian>
507 off_t
508 Sized_object<size, big_endian>::do_finalize_local_symbols(off_t off,
509 Stringpool* pool)
511 if (this->symtab_shnum_ == 0)
513 // This object has no symbols. Weird but legal.
514 return off;
517 off = (off + (size >> 3) - 1) & ~ ((off_t) (size >> 3) - 1);
519 this->local_symbol_offset_ = off;
521 // Read the symbol table section header.
522 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
523 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
525 // Read the local symbols.
526 const int sym_size = This::sym_size;
527 const unsigned int loccount = this->local_symbol_count_;
528 assert(loccount == symtabshdr.get_sh_info());
529 off_t locsize = loccount * sym_size;
530 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
531 locsize);
533 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
535 // Read the section header for the symbol names.
536 typename This::Shdr strtabshdr(
537 this->section_header(symtabshdr.get_sh_link()));
538 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
540 // Read the symbol names.
541 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
542 strtabshdr.get_sh_size());
543 const char* pnames = reinterpret_cast<const char*>(pnamesu);
545 // Loop over the local symbols.
547 std::vector<Map_to_output>& mo(this->map_to_output());
548 unsigned int shnum = this->shnum();
549 unsigned int count = 0;
550 // Skip the first, dummy, symbol.
551 psyms += sym_size;
552 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
554 elfcpp::Sym<size, big_endian> sym(psyms);
556 unsigned int shndx = sym.get_st_shndx();
558 if (shndx >= elfcpp::SHN_LORESERVE)
560 if (shndx == elfcpp::SHN_ABS)
561 this->values_[i] = sym.get_st_value();
562 else
564 // FIXME: Handle SHN_XINDEX.
565 fprintf(stderr,
566 _("%s: %s: unknown section index %u "
567 "for local symbol %u\n"),
568 program_name, this->name().c_str(), shndx, i);
569 gold_exit(false);
572 else
574 if (shndx >= shnum)
576 fprintf(stderr,
577 _("%s: %s: local symbol %u section index %u "
578 "out of range\n"),
579 program_name, this->name().c_str(), i, shndx);
580 gold_exit(false);
583 if (mo[shndx].output_section == NULL)
585 this->values_[i] = 0;
586 continue;
589 this->values_[i] = (mo[shndx].output_section->address()
590 + sym.get_st_value());
593 pool->add(pnames + sym.get_st_name());
594 off += sym_size;
595 ++count;
598 this->output_local_symbol_count_ = count;
600 return off;
603 // Write out the local symbols.
605 template<int size, bool big_endian>
606 void
607 Sized_object<size, big_endian>::write_local_symbols(Output_file* of,
608 const Stringpool* sympool)
610 if (this->symtab_shnum_ == 0)
612 // This object has no symbols. Weird but legal.
613 return;
616 // Read the symbol table section header.
617 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
618 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
619 const unsigned int loccount = this->local_symbol_count_;
620 assert(loccount == symtabshdr.get_sh_info());
622 // Read the local symbols.
623 const int sym_size = This::sym_size;
624 off_t locsize = loccount * sym_size;
625 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
626 locsize);
628 // Read the section header for the symbol names.
629 typename This::Shdr strtabshdr(
630 this->section_header(symtabshdr.get_sh_link()));
631 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
633 // Read the symbol names.
634 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
635 strtabshdr.get_sh_size());
636 const char* pnames = reinterpret_cast<const char*>(pnamesu);
638 // Get a view into the output file.
639 off_t output_size = this->output_local_symbol_count_ * sym_size;
640 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
641 output_size);
643 std::vector<Map_to_output>& mo(this->map_to_output());
645 psyms += sym_size;
646 unsigned char* ov = oview;
647 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
649 elfcpp::Sym<size, big_endian> isym(psyms);
650 elfcpp::Sym_write<size, big_endian> osym(ov);
652 unsigned int st_shndx = isym.get_st_shndx();
653 if (st_shndx < elfcpp::SHN_LORESERVE)
655 assert(st_shndx < mo.size());
656 if (mo[st_shndx].output_section == NULL)
657 continue;
658 st_shndx = mo[st_shndx].output_section->shndx();
661 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
662 osym.put_st_value(this->values_[i]);
663 osym.put_st_size(isym.get_st_size());
664 osym.put_st_info(isym.get_st_info());
665 osym.put_st_other(isym.get_st_other());
666 osym.put_st_shndx(st_shndx);
668 ov += sym_size;
671 assert(ov - oview == output_size);
673 of->write_output_view(this->local_symbol_offset_, output_size, oview);
676 // Input_objects methods.
678 void
679 Input_objects::add_object(Object* obj)
681 this->object_list_.push_back(obj);
683 Target* target = obj->target();
684 if (this->target_ == NULL)
685 this->target_ = target;
686 else if (this->target_ != target)
688 fprintf(stderr, "%s: %s: incompatible target\n",
689 program_name, obj->name().c_str());
690 gold_exit(false);
693 if (obj->is_dynamic())
694 this->any_dynamic_ = true;
697 // Relocate_info methods.
699 // Return a string describing the location of a relocation. This is
700 // only used in error messages.
702 template<int size, bool big_endian>
703 std::string
704 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
706 std::string ret(this->object->name());
707 ret += ": reloc ";
708 char buf[100];
709 snprintf(buf, sizeof buf, "%zu", relnum);
710 ret += buf;
711 ret += " in reloc section ";
712 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
713 ret += buf;
714 ret += " (" + this->object->section_name(this->reloc_shndx);
715 ret += ") for section ";
716 snprintf(buf, sizeof buf, "%u", this->data_shndx);
717 ret += buf;
718 ret += " (" + this->object->section_name(this->data_shndx) + ")";
719 return ret;
722 } // End namespace gold.
724 namespace
727 using namespace gold;
729 // Read an ELF file with the header and return the appropriate
730 // instance of Object.
732 template<int size, bool big_endian>
733 Object*
734 make_elf_sized_object(const std::string& name, Input_file* input_file,
735 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
737 int et = ehdr.get_e_type();
738 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
740 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
741 program_name, name.c_str(), static_cast<int>(et));
742 gold_exit(false);
745 if (et == elfcpp::ET_REL)
747 Sized_object<size, big_endian>* obj =
748 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
749 obj->setup(ehdr);
750 return obj;
752 else
754 // elfcpp::ET_DYN
755 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
756 program_name, name.c_str());
757 gold_exit(false);
758 // Sized_dynobj<size, big_endian>* obj =
759 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
760 // offset, ehdr);
761 // obj->setup(ehdr);
762 // return obj;
766 } // End anonymous namespace.
768 namespace gold
771 // Read an ELF file and return the appropriate instance of Object.
773 Object*
774 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
775 const unsigned char* p, off_t bytes)
777 if (bytes < elfcpp::EI_NIDENT)
779 fprintf(stderr, _("%s: %s: ELF file too short\n"),
780 program_name, name.c_str());
781 gold_exit(false);
784 int v = p[elfcpp::EI_VERSION];
785 if (v != elfcpp::EV_CURRENT)
787 if (v == elfcpp::EV_NONE)
788 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
789 program_name, name.c_str());
790 else
791 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
792 program_name, name.c_str(), v);
793 gold_exit(false);
796 int c = p[elfcpp::EI_CLASS];
797 if (c == elfcpp::ELFCLASSNONE)
799 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
800 program_name, name.c_str());
801 gold_exit(false);
803 else if (c != elfcpp::ELFCLASS32
804 && c != elfcpp::ELFCLASS64)
806 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
807 program_name, name.c_str(), c);
808 gold_exit(false);
811 int d = p[elfcpp::EI_DATA];
812 if (d == elfcpp::ELFDATANONE)
814 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
815 program_name, name.c_str());
816 gold_exit(false);
818 else if (d != elfcpp::ELFDATA2LSB
819 && d != elfcpp::ELFDATA2MSB)
821 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
822 program_name, name.c_str(), d);
823 gold_exit(false);
826 bool big_endian = d == elfcpp::ELFDATA2MSB;
828 if (c == elfcpp::ELFCLASS32)
830 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
832 fprintf(stderr, _("%s: %s: ELF file too short\n"),
833 program_name, name.c_str());
834 gold_exit(false);
836 if (big_endian)
838 elfcpp::Ehdr<32, true> ehdr(p);
839 return make_elf_sized_object<32, true>(name, input_file,
840 offset, ehdr);
842 else
844 elfcpp::Ehdr<32, false> ehdr(p);
845 return make_elf_sized_object<32, false>(name, input_file,
846 offset, ehdr);
849 else
851 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
853 fprintf(stderr, _("%s: %s: ELF file too short\n"),
854 program_name, name.c_str());
855 gold_exit(false);
857 if (big_endian)
859 elfcpp::Ehdr<64, true> ehdr(p);
860 return make_elf_sized_object<64, true>(name, input_file,
861 offset, ehdr);
863 else
865 elfcpp::Ehdr<64, false> ehdr(p);
866 return make_elf_sized_object<64, false>(name, input_file,
867 offset, ehdr);
872 // Instantiate the templates we need. We could use the configure
873 // script to restrict this to only the ones for implemented targets.
875 template
876 class Sized_object<32, false>;
878 template
879 class Sized_object<32, true>;
881 template
882 class Sized_object<64, false>;
884 template
885 class Sized_object<64, true>;
887 template
888 struct Relocate_info<32, false>;
890 template
891 struct Relocate_info<32, true>;
893 template
894 struct Relocate_info<64, false>;
896 template
897 struct Relocate_info<64, true>;
899 } // End namespace gold.