Snapshot. Now able to produce a minimal executable which actually
[binutils-gdb.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 #include "gold.h"
4
5 #include <cerrno>
6 #include <cstring>
7 #include <cassert>
8
9 #include "object.h"
10 #include "target-select.h"
11 #include "layout.h"
12 #include "output.h"
13
14 namespace gold
15 {
16
17 // Class Object.
18
19 const unsigned char*
20 Object::get_view(off_t start, off_t size)
21 {
22 return this->input_file_->file().get_view(start + this->offset_, size);
23 }
24
25 void
26 Object::read(off_t start, off_t size, void* p)
27 {
28 this->input_file_->file().read(start + this->offset_, size, p);
29 }
30
31 File_view*
32 Object::get_lasting_view(off_t start, off_t size)
33 {
34 return this->input_file_->file().get_lasting_view(start + this->offset_,
35 size);
36 }
37
38 // Class Sized_object.
39
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 flags_(ehdr.get_e_flags()),
48 shoff_(ehdr.get_e_shoff()),
49 shstrndx_(0),
50 symtab_shnum_(0),
51 local_symbol_count_(0),
52 output_local_symbol_count_(0),
53 symbols_(NULL),
54 local_symbol_offset_(0),
55 values_(NULL)
56 {
57 if (ehdr.get_e_ehsize() != This::ehdr_size)
58 {
59 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
60 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
61 This::ehdr_size);
62 gold_exit(false);
63 }
64 if (ehdr.get_e_shentsize() != This::shdr_size)
65 {
66 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
67 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
68 This::shdr_size);
69 gold_exit(false);
70 }
71 }
72
73 template<int size, bool big_endian>
74 Sized_object<size, big_endian>::~Sized_object()
75 {
76 }
77
78 // Read the section header for section SHNUM.
79
80 template<int size, bool big_endian>
81 const unsigned char*
82 Sized_object<size, big_endian>::section_header(unsigned int shnum)
83 {
84 assert(shnum < this->shnum());
85 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
86 return this->get_view(symtabshdroff, This::shdr_size);
87 }
88
89 // Set up an object file bsaed on the file header. This sets up the
90 // target and reads the section information.
91
92 template<int size, bool big_endian>
93 void
94 Sized_object<size, big_endian>::setup(
95 const elfcpp::Ehdr<size, big_endian>& ehdr)
96 {
97 int machine = ehdr.get_e_machine();
98 Target* target = select_target(machine, size, big_endian,
99 ehdr.get_e_ident()[elfcpp::EI_OSABI],
100 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
101 if (target == NULL)
102 {
103 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
104 program_name, this->name().c_str(), machine);
105 gold_exit(false);
106 }
107 this->set_target(target);
108 unsigned int shnum = ehdr.get_e_shnum();
109 unsigned int shstrndx = ehdr.get_e_shstrndx();
110 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
111 && this->shoff_ != 0)
112 {
113 typename This::Shdr shdr(this->section_header(0));
114 if (shnum == 0)
115 shnum = shdr.get_sh_size();
116 if (shstrndx == elfcpp::SHN_XINDEX)
117 shstrndx = shdr.get_sh_link();
118 }
119 this->set_shnum(shnum);
120 this->shstrndx_ = shstrndx;
121
122 if (shnum == 0)
123 return;
124
125 // Find the SHT_SYMTAB section.
126 const unsigned char* p = this->get_view (this->shoff_,
127 shnum * This::shdr_size);
128 // Skip the first section, which is always empty.
129 p += This::shdr_size;
130 for (unsigned int i = 1; i < shnum; ++i)
131 {
132 typename This::Shdr shdr(p);
133 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
134 {
135 this->symtab_shnum_ = i;
136 break;
137 }
138 p += This::shdr_size;
139 }
140 }
141
142 // Read the symbols and relocations from an object file.
143
144 template<int size, bool big_endian>
145 Read_symbols_data
146 Sized_object<size, big_endian>::do_read_symbols()
147 {
148 if (this->symtab_shnum_ == 0)
149 {
150 // No symbol table. Weird but legal.
151 Read_symbols_data ret;
152 ret.symbols = NULL;
153 ret.symbols_size = 0;
154 ret.symbol_names = NULL;
155 ret.symbol_names_size = 0;
156 return ret;
157 }
158
159 // Read the symbol table section header.
160 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
161 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
162
163 // We only need the external symbols.
164 const int sym_size = This::sym_size;
165 off_t locsize = symtabshdr.get_sh_info() * sym_size;
166 off_t extoff = symtabshdr.get_sh_offset() + locsize;
167 off_t extsize = symtabshdr.get_sh_size() - locsize;
168
169 // Read the symbol table.
170 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
171
172 // Read the section header for the symbol names.
173 unsigned int strtab_shnum = symtabshdr.get_sh_link();
174 if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
175 {
176 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
177 program_name, this->name().c_str(), strtab_shnum);
178 gold_exit(false);
179 }
180 typename This::Shdr strtabshdr(this->section_header(strtab_shnum));
181 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
182 {
183 fprintf(stderr,
184 _("%s: %s: symbol table name section has wrong type: %u\n"),
185 program_name, this->name().c_str(),
186 static_cast<unsigned int>(strtabshdr.get_sh_type()));
187 gold_exit(false);
188 }
189
190 // Read the symbol names.
191 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
192 strtabshdr.get_sh_size());
193
194 Read_symbols_data ret;
195 ret.symbols = fvsymtab;
196 ret.symbols_size = extsize;
197 ret.symbol_names = fvstrtab;
198 ret.symbol_names_size = strtabshdr.get_sh_size();
199
200 return ret;
201 }
202
203 // Add the symbols to the symbol table.
204
205 template<int size, bool big_endian>
206 void
207 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
208 Read_symbols_data sd)
209 {
210 if (sd.symbols == NULL)
211 {
212 assert(sd.symbol_names == NULL);
213 return;
214 }
215
216 const int sym_size = This::sym_size;
217 size_t symcount = sd.symbols_size / sym_size;
218 if (symcount * sym_size != sd.symbols_size)
219 {
220 fprintf(stderr,
221 _("%s: %s: size of symbols is not multiple of symbol size\n"),
222 program_name, this->name().c_str());
223 gold_exit(false);
224 }
225
226 this->symbols_ = new Symbol*[symcount];
227
228 const elfcpp::Sym<size, big_endian>* syms =
229 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
230 const char* sym_names =
231 reinterpret_cast<const char*>(sd.symbol_names->data());
232 symtab->add_from_object(this, syms, symcount, sym_names,
233 sd.symbol_names_size, this->symbols_);
234
235 delete sd.symbols;
236 delete sd.symbol_names;
237 }
238
239 // Return whether to include a section group in the link. LAYOUT is
240 // used to keep track of which section groups we have already seen.
241 // INDEX is the index of the section group and SHDR is the section
242 // header. If we do not want to include this group, we set bits in
243 // OMIT for each section which should be discarded.
244
245 template<int size, bool big_endian>
246 bool
247 Sized_object<size, big_endian>::include_section_group(
248 Layout* layout,
249 unsigned int index,
250 const elfcpp::Shdr<size, big_endian>& shdr,
251 std::vector<bool>* omit)
252 {
253 // Read the section contents.
254 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
255 shdr.get_sh_size());
256 const elfcpp::Elf_Word* pword =
257 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
258
259 // The first word contains flags. We only care about COMDAT section
260 // groups. Other section groups are always included in the link
261 // just like ordinary sections.
262 elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
263 if ((flags & elfcpp::GRP_COMDAT) == 0)
264 return true;
265
266 // Look up the group signature, which is the name of a symbol. This
267 // is a lot of effort to go to to read a string. Why didn't they
268 // just use the name of the SHT_GROUP section as the group
269 // signature?
270
271 // Get the appropriate symbol table header (this will normally be
272 // the single SHT_SYMTAB section, but in principle it need not be).
273 if (shdr.get_sh_link() >= this->shnum())
274 {
275 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
276 program_name, this->name().c_str(), index, shdr.get_sh_link());
277 gold_exit(false);
278 }
279
280 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
281
282 // Read the symbol table entry.
283 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
284 {
285 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
286 program_name, this->name().c_str(), index, shdr.get_sh_info());
287 gold_exit(false);
288 }
289 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
290 const unsigned char* psym = this->get_view(symoff, This::sym_size);
291 elfcpp::Sym<size, big_endian> sym(psym);
292
293 // Read the section header for the symbol table names.
294 if (symshdr.get_sh_link() >= this->shnum())
295 {
296 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
297 program_name, this->name().c_str(), shdr.get_sh_link(),
298 symshdr.get_sh_link());
299 gold_exit(false);
300 }
301
302 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
303
304 // Read the symbol table names.
305 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
306 symnamehdr.get_sh_size());
307 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
308
309 // Get the section group signature.
310 if (sym.get_st_name() >= symnamehdr.get_sh_size())
311 {
312 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
313 program_name, this->name().c_str(), shdr.get_sh_info(),
314 sym.get_st_name());
315 gold_exit(false);
316 }
317
318 const char* signature = psymnames + sym.get_st_name();
319
320 // Record this section group, and see whether we've already seen one
321 // with the same signature.
322 if (layout->add_comdat(signature, true))
323 return true;
324
325 // This is a duplicate. We want to discard the sections in this
326 // group.
327 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
328 for (size_t i = 1; i < count; ++i)
329 {
330 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
331 if (secnum >= this->shnum())
332 {
333 fprintf(stderr,
334 _("%s: %s: section %u in section group %u out of range"),
335 program_name, this->name().c_str(), secnum,
336 index);
337 gold_exit(false);
338 }
339 (*omit)[secnum] = true;
340 }
341
342 return false;
343 }
344
345 // Whether to include a linkonce section in the link. NAME is the
346 // name of the section and SHDR is the section header.
347
348 // Linkonce sections are a GNU extension implemented in the original
349 // GNU linker before section groups were defined. The semantics are
350 // that we only include one linkonce section with a given name. The
351 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
352 // where T is the type of section and SYMNAME is the name of a symbol.
353 // In an attempt to make linkonce sections interact well with section
354 // groups, we try to identify SYMNAME and use it like a section group
355 // signature. We want to block section groups with that signature,
356 // but not other linkonce sections with that signature. We also use
357 // the full name of the linkonce section as a normal section group
358 // signature.
359
360 template<int size, bool big_endian>
361 bool
362 Sized_object<size, big_endian>::include_linkonce_section(
363 Layout* layout,
364 const char* name,
365 const elfcpp::Shdr<size, big_endian>&)
366 {
367 const char* symname = strrchr(name, '.') + 1;
368 bool omit1 = layout->add_comdat(symname, false);
369 bool omit2 = layout->add_comdat(name, true);
370 return omit1 || omit2;
371 }
372
373 // Lay out the input sections. We walk through the sections and check
374 // whether they should be included in the link. If they should, we
375 // pass them to the Layout object, which will return an output section
376 // and an offset.
377
378 template<int size, bool big_endian>
379 void
380 Sized_object<size, big_endian>::do_layout(Layout* layout)
381 {
382 // This is always called from the main thread. Lock the file to
383 // keep the error checks happy.
384 Task_locker_obj<File_read> frl(this->input_file()->file());
385
386 // Get the section headers.
387 unsigned int shnum = this->shnum();
388 const unsigned char* pshdrs = this->get_view(this->shoff_,
389 shnum * This::shdr_size);
390
391 // Get the section names.
392 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
393 typename This::Shdr shdrnames(pshdrnames);
394 typename elfcpp::Elf_types<size>::Elf_WXword names_size =
395 shdrnames.get_sh_size();
396 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
397 shdrnames.get_sh_size());
398 const char* pnames = reinterpret_cast<const char*>(pnamesu);
399
400 std::vector<Map_to_output>& map_sections(this->map_to_output());
401 map_sections.resize(shnum);
402
403 // Keep track of which sections to omit.
404 std::vector<bool> omit(shnum, false);
405
406 for (unsigned int i = 0; i < shnum; ++i)
407 {
408 typename This::Shdr shdr(pshdrs);
409
410 if (shdr.get_sh_name() >= names_size)
411 {
412 fprintf(stderr,
413 _("%s: %s: bad section name offset for section %u: %lu\n"),
414 program_name, this->name().c_str(), i,
415 static_cast<unsigned long>(shdr.get_sh_name()));
416 gold_exit(false);
417 }
418
419 const char* name = pnames + shdr.get_sh_name();
420
421 bool discard = omit[i];
422 if (!discard)
423 {
424 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
425 {
426 if (!this->include_section_group(layout, i, shdr, &omit))
427 discard = true;
428 }
429 else if (Layout::is_linkonce(name))
430 {
431 if (!this->include_linkonce_section(layout, name, shdr))
432 discard = true;
433 }
434 }
435
436 if (discard)
437 {
438 // Do not include this section in the link.
439 map_sections[i].output_section = NULL;
440 continue;
441 }
442
443 off_t offset;
444 Output_section* os = layout->layout(this, name, shdr, &offset);
445
446 map_sections[i].output_section = os;
447 map_sections[i].offset = offset;
448
449 pshdrs += This::shdr_size;
450 }
451 }
452
453 // Finalize the local symbols. Here we record the file offset at
454 // which they should be output, we add their names to *POOL, and we
455 // add their values to THIS->VALUES_. Return the new file offset.
456 // This function is always called from the main thread. The actual
457 // output of the local symbols will occur in a separate task.
458
459 template<int size, bool big_endian>
460 off_t
461 Sized_object<size, big_endian>::do_finalize_local_symbols(off_t off,
462 Stringpool* pool)
463 {
464 if (this->symtab_shnum_ == 0)
465 {
466 // This object has no symbols. Weird but legal.
467 return off;
468 }
469
470 off = (off + (size >> 3) - 1) & ~ ((off_t) (size >> 3) - 1);
471
472 this->local_symbol_offset_ = off;
473
474 // Read the symbol table section header.
475 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
476 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
477
478 // Read the local symbols.
479 unsigned int loccount = symtabshdr.get_sh_info();
480 const int sym_size = This::sym_size;
481 off_t locsize = loccount * sym_size;
482 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
483 locsize);
484
485 this->local_symbol_count_ = loccount;
486
487 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
488
489 // Read the section header for the symbol names.
490 typename This::Shdr strtabshdr(
491 this->section_header(symtabshdr.get_sh_link()));
492 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
493
494 // Read the symbol names.
495 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
496 strtabshdr.get_sh_size());
497 const char* pnames = reinterpret_cast<const char*>(pnamesu);
498
499 // Loop over the local symbols.
500
501 std::vector<Map_to_output>& mo(this->map_to_output());
502 unsigned int shnum = this->shnum();
503 unsigned int count = 0;
504 // Skip the first, dummy, symbol.
505 psyms += sym_size;
506 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
507 {
508 elfcpp::Sym<size, big_endian> sym(psyms);
509
510 unsigned int shndx = sym.get_st_shndx();
511
512 if (shndx >= elfcpp::SHN_LORESERVE)
513 {
514 if (shndx == elfcpp::SHN_ABS)
515 this->values_[i] = sym.get_st_value();
516 else
517 {
518 // FIXME: Handle SHN_XINDEX.
519 fprintf(stderr,
520 _("%s: %s: unknown section index %u "
521 "for local symbol %u\n"),
522 program_name, this->name().c_str(), shndx, i);
523 gold_exit(false);
524 }
525 }
526 else
527 {
528 if (shndx >= shnum)
529 {
530 fprintf(stderr,
531 _("%s: %s: local symbol %u section index %u "
532 "out of range\n"),
533 program_name, this->name().c_str(), i, shndx);
534 gold_exit(false);
535 }
536
537 if (mo[shndx].output_section == NULL)
538 {
539 this->values_[i] = 0;
540 continue;
541 }
542
543 this->values_[i] = (mo[shndx].output_section->address()
544 + sym.get_st_value());
545 }
546
547 pool->add(pnames + sym.get_st_name());
548 off += sym_size;
549 ++count;
550 }
551
552 this->output_local_symbol_count_ = count;
553
554 return off;
555 }
556
557 // Write out the local symbols.
558
559 template<int size, bool big_endian>
560 void
561 Sized_object<size, big_endian>::write_local_symbols(Output_file* of,
562 const Stringpool* sympool)
563 {
564 if (this->symtab_shnum_ == 0)
565 {
566 // This object has no symbols. Weird but legal.
567 return;
568 }
569
570 // Read the symbol table section header.
571 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
572 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
573 unsigned int local_symbol_count = this->local_symbol_count_;
574 assert(local_symbol_count == symtabshdr.get_sh_info());
575
576 // Read the local symbols.
577 const int sym_size = This::sym_size;
578 off_t locsize = local_symbol_count * sym_size;
579 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
580 locsize);
581
582 // Read the section header for the symbol names.
583 typename This::Shdr strtabshdr(
584 this->section_header(symtabshdr.get_sh_link()));
585 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
586
587 // Read the symbol names.
588 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
589 strtabshdr.get_sh_size());
590 const char* pnames = reinterpret_cast<const char*>(pnamesu);
591
592 // Get a view into the output file.
593 off_t output_size = this->output_local_symbol_count_ * sym_size;
594 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
595 output_size);
596
597 std::vector<Map_to_output>& mo(this->map_to_output());
598
599 psyms += sym_size;
600 unsigned char* ov = oview;
601 for (unsigned int i = 1; i < local_symbol_count; ++i, psyms += sym_size)
602 {
603 elfcpp::Sym<size, big_endian> isym(psyms);
604 elfcpp::Sym_write<size, big_endian> osym(ov);
605
606 unsigned int st_shndx = isym.get_st_shndx();
607 if (st_shndx < elfcpp::SHN_LORESERVE)
608 {
609 assert(st_shndx < mo.size());
610 if (mo[st_shndx].output_section == NULL)
611 continue;
612 st_shndx = mo[st_shndx].output_section->shndx();
613 }
614
615 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
616 osym.put_st_value(this->values_[i]);
617 osym.put_st_size(isym.get_st_size());
618 osym.put_st_info(isym.get_st_info());
619 osym.put_st_other(isym.get_st_other());
620 osym.put_st_shndx(st_shndx);
621
622 ov += sym_size;
623 }
624
625 assert(ov - oview == output_size);
626
627 of->write_output_view(this->local_symbol_offset_, output_size, oview);
628 }
629
630 // Input_objects methods.
631
632 void
633 Input_objects::add_object(Object* obj)
634 {
635 this->object_list_.push_back(obj);
636
637 Target* target = obj->target();
638 if (this->target_ == NULL)
639 this->target_ = target;
640 else if (this->target_ != target)
641 {
642 fprintf(stderr, "%s: %s: incompatible target\n",
643 program_name, obj->name().c_str());
644 gold_exit(false);
645 }
646
647 if (obj->is_dynamic())
648 this->any_dynamic_ = true;
649 }
650
651 } // End namespace gold.
652
653 namespace
654 {
655
656 using namespace gold;
657
658 // Read an ELF file with the header and return the appropriate
659 // instance of Object.
660
661 template<int size, bool big_endian>
662 Object*
663 make_elf_sized_object(const std::string& name, Input_file* input_file,
664 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
665 {
666 int et = ehdr.get_e_type();
667 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
668 {
669 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
670 program_name, name.c_str(), static_cast<int>(et));
671 gold_exit(false);
672 }
673
674 if (et == elfcpp::ET_REL)
675 {
676 Sized_object<size, big_endian>* obj =
677 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
678 obj->setup(ehdr);
679 return obj;
680 }
681 else
682 {
683 // elfcpp::ET_DYN
684 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
685 program_name, name.c_str());
686 gold_exit(false);
687 // Sized_dynobj<size, big_endian>* obj =
688 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
689 // offset, ehdr);
690 // obj->setup(ehdr);
691 // return obj;
692 }
693 }
694
695 } // End anonymous namespace.
696
697 namespace gold
698 {
699
700 // Read an ELF file and return the appropriate instance of Object.
701
702 Object*
703 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
704 const unsigned char* p, off_t bytes)
705 {
706 if (bytes < elfcpp::EI_NIDENT)
707 {
708 fprintf(stderr, _("%s: %s: ELF file too short\n"),
709 program_name, name.c_str());
710 gold_exit(false);
711 }
712
713 int v = p[elfcpp::EI_VERSION];
714 if (v != elfcpp::EV_CURRENT)
715 {
716 if (v == elfcpp::EV_NONE)
717 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
718 program_name, name.c_str());
719 else
720 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
721 program_name, name.c_str(), v);
722 gold_exit(false);
723 }
724
725 int c = p[elfcpp::EI_CLASS];
726 if (c == elfcpp::ELFCLASSNONE)
727 {
728 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
729 program_name, name.c_str());
730 gold_exit(false);
731 }
732 else if (c != elfcpp::ELFCLASS32
733 && c != elfcpp::ELFCLASS64)
734 {
735 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
736 program_name, name.c_str(), c);
737 gold_exit(false);
738 }
739
740 int d = p[elfcpp::EI_DATA];
741 if (d == elfcpp::ELFDATANONE)
742 {
743 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
744 program_name, name.c_str());
745 gold_exit(false);
746 }
747 else if (d != elfcpp::ELFDATA2LSB
748 && d != elfcpp::ELFDATA2MSB)
749 {
750 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
751 program_name, name.c_str(), d);
752 gold_exit(false);
753 }
754
755 bool big_endian = d == elfcpp::ELFDATA2MSB;
756
757 if (c == elfcpp::ELFCLASS32)
758 {
759 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
760 {
761 fprintf(stderr, _("%s: %s: ELF file too short\n"),
762 program_name, name.c_str());
763 gold_exit(false);
764 }
765 if (big_endian)
766 {
767 elfcpp::Ehdr<32, true> ehdr(p);
768 return make_elf_sized_object<32, true>(name, input_file,
769 offset, ehdr);
770 }
771 else
772 {
773 elfcpp::Ehdr<32, false> ehdr(p);
774 return make_elf_sized_object<32, false>(name, input_file,
775 offset, ehdr);
776 }
777 }
778 else
779 {
780 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
781 {
782 fprintf(stderr, _("%s: %s: ELF file too short\n"),
783 program_name, name.c_str());
784 gold_exit(false);
785 }
786 if (big_endian)
787 {
788 elfcpp::Ehdr<64, true> ehdr(p);
789 return make_elf_sized_object<64, true>(name, input_file,
790 offset, ehdr);
791 }
792 else
793 {
794 elfcpp::Ehdr<64, false> ehdr(p);
795 return make_elf_sized_object<64, false>(name, input_file,
796 offset, ehdr);
797 }
798 }
799 }
800
801 // Instantiate the templates we need. We could use the configure
802 // script to restrict this to only the ones for implemented targets.
803
804 template
805 class Sized_object<32, false>;
806
807 template
808 class Sized_object<32, true>;
809
810 template
811 class Sized_object<64, false>;
812
813 template
814 class Sized_object<64, true>;
815
816 } // End namespace gold.