Add Elf_file interface which can be used by both Sized_relobj and
[platform/upstream/binutils.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 #include <cstdarg>
9
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"
16
17 namespace gold
18 {
19
20 // Class Object.
21
22 // Report an error for the elfcpp::Elf_file interface.
23
24 void
25 Object::error(const char* format, ...)
26 {
27   va_list args;
28
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);
34
35   gold_exit(false);
36 }
37
38 // Return a view of the contents of a section.
39
40 const unsigned char*
41 Object::section_contents(unsigned int shndx, off_t* plen)
42 {
43   Location loc(this->do_section_contents(shndx));
44   *plen = loc.data_size;
45   return this->get_view(loc.file_offset, loc.data_size);
46 }
47
48 // Class Sized_relobj.
49
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)
65 {
66 }
67
68 template<int size, bool big_endian>
69 Sized_relobj<size, big_endian>::~Sized_relobj()
70 {
71 }
72
73 // Set up an object file based on the file header.  This sets up the
74 // target and reads the section information.
75
76 template<int size, bool big_endian>
77 void
78 Sized_relobj<size, big_endian>::setup(
79     const elfcpp::Ehdr<size, big_endian>& ehdr)
80 {
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)
86     {
87       fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
88               program_name, this->name().c_str(), machine);
89       gold_exit(false);
90     }
91   this->set_target(target);
92
93   unsigned int shnum = this->elf_file_.shnum();
94   this->set_shnum(shnum);
95   if (shnum == 0)
96     return;
97
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);
102
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();
108
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)
112     {
113       typename This::Shdr shdr(p);
114       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
115         {
116           this->symtab_shndx_ = i;
117           break;
118         }
119     }
120 }
121
122 // Read the sections and symbols from an object file.
123
124 template<int size, bool big_endian>
125 void
126 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
127 {
128   // Transfer our view of the section headers to SD.
129   sd->section_headers = this->section_headers_;
130   this->section_headers_ = NULL;
131
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);
141
142   if (this->symtab_shndx_ == 0)
143     {
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;
150     }
151
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);
156
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;
164
165   // Read the symbol table.
166   File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
167
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)
172     {
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);
176     }
177   typename This::Shdr strtabshdr(pshdrs + strtab_shnum * This::shdr_size);
178   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
179     {
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);
185     }
186
187   // Read the symbol names.
188   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
189                                                strtabshdr.get_sh_size());
190
191   sd->symbols = fvsymtab;
192   sd->symbols_size = extsize;
193   sd->symbol_names = fvstrtab;
194   sd->symbol_names_size = strtabshdr.get_sh_size();
195 }
196
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.
202
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)
210 {
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);
216
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;
223
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?
228
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));
233
234   // Read the symbol table entry.
235   if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
236     {
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);
240     }
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);
244
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);
250
251   // Get the section group signature.
252   if (sym.get_st_name() >= symnamelen)
253     {
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);
258     }
259
260   const char* signature = psymnames + sym.get_st_name();
261
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)
268     {
269       secname = this->section_name(sym.get_st_shndx());
270       signature = secname.c_str();
271     }
272
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;
277
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)
282     {
283       elfcpp::Elf_Word secnum =
284         elfcpp::Swap<32, big_endian>::readval(pword + i);
285       if (secnum >= this->shnum())
286         {
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);
292         }
293       (*omit)[secnum] = true;
294     }
295
296   return false;
297 }
298
299 // Whether to include a linkonce section in the link.  NAME is the
300 // name of the section and SHDR is the section header.
301
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.
313
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>&)
320 {
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;
325 }
326
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.
331
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)
338 {
339   unsigned int shnum = this->shnum();
340   if (shnum == 0)
341     return;
342
343   // Get the section headers.
344   const unsigned char* pshdrs = sd->section_headers->data();
345
346   // Get the section names.
347   const unsigned char* pnamesu = sd->section_names->data();
348   const char* pnames = reinterpret_cast<const char*>(pnamesu);
349
350   std::vector<Map_to_output>& map_sections(this->map_to_output());
351   map_sections.resize(shnum);
352
353   // Keep track of which sections to omit.
354   std::vector<bool> omit(shnum, false);
355
356   const char warn_prefix[] = ".gnu.warning.";
357   const int warn_prefix_len = sizeof warn_prefix - 1;
358
359   // Skip the first, dummy, section.
360   pshdrs += This::shdr_size;
361   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
362     {
363       typename This::Shdr shdr(pshdrs);
364
365       if (shdr.get_sh_name() >= sd->section_names_size)
366         {
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);
372         }
373
374       const char* name = pnames + shdr.get_sh_name();
375
376       if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
377         {
378           symtab->add_warning(name + warn_prefix_len, this, i);
379           if (!options.is_relocatable())
380             omit[i] = true;
381         }
382
383       bool discard = omit[i];
384       if (!discard)
385         {
386           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
387             {
388               if (!this->include_section_group(layout, i, shdr, &omit))
389                 discard = true;
390             }
391           else if (Layout::is_linkonce(name))
392             {
393               if (!this->include_linkonce_section(layout, name, shdr))
394                 discard = true;
395             }
396         }
397
398       if (discard)
399         {
400           // Do not include this section in the link.
401           map_sections[i].output_section = NULL;
402           continue;
403         }
404
405       off_t offset;
406       Output_section* os = layout->layout(this, i, name, shdr, &offset);
407
408       map_sections[i].output_section = os;
409       map_sections[i].offset = offset;
410     }
411
412   delete sd->section_headers;
413   sd->section_headers = NULL;
414   delete sd->section_names;
415   sd->section_names = NULL;
416 }
417
418 // Add the symbols to the symbol table.
419
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)
424 {
425   if (sd->symbols == NULL)
426     {
427       assert(sd->symbol_names == NULL);
428       return;
429     }
430
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)
434     {
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);
439     }
440
441   this->symbols_ = new Symbol*[symcount];
442
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_);
449
450   delete sd->symbols;
451   sd->symbols = NULL;
452   delete sd->symbol_names;
453   sd->symbol_names = NULL;
454 }
455
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.
461
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)
466 {
467   if (this->symtab_shndx_ == 0)
468     {
469       // This object has no symbols.  Weird but legal.
470       return off;
471     }
472
473   off = align_address(off, size >> 3);
474
475   this->local_symbol_offset_ = off;
476
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);
482
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);
490
491   this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
492
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);
499
500   // Loop over the local symbols.
501
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)
508     {
509       elfcpp::Sym<size, big_endian> sym(psyms);
510
511       unsigned int shndx = sym.get_st_shndx();
512
513       if (shndx >= elfcpp::SHN_LORESERVE)
514         {
515           if (shndx == elfcpp::SHN_ABS)
516             this->values_[i] = sym.get_st_value();
517           else
518             {
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);
525             }
526         }
527       else
528         {
529           if (shndx >= shnum)
530             {
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);
536             }
537
538           if (mo[shndx].output_section == NULL)
539             {
540               this->values_[i] = 0;
541               continue;
542             }
543
544           this->values_[i] = (mo[shndx].output_section->address()
545                               + mo[shndx].offset
546                               + sym.get_st_value());
547         }
548
549       if (sym.get_st_type() != elfcpp::STT_SECTION)
550         {
551           if (sym.get_st_name() >= strtab_size)
552             {
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);
560             }
561
562           pool->add(pnames + sym.get_st_name(), NULL);
563           off += sym_size;
564           ++count;
565         }
566     }
567
568   this->output_local_symbol_count_ = count;
569
570   return off;
571 }
572
573 // Write out the local symbols.
574
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)
579 {
580   if (this->symtab_shndx_ == 0)
581     {
582       // This object has no symbols.  Weird but legal.
583       return;
584     }
585
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());
593
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);
599
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);
606
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);
611
612   std::vector<Map_to_output>& mo(this->map_to_output());
613
614   psyms += sym_size;
615   unsigned char* ov = oview;
616   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
617     {
618       elfcpp::Sym<size, big_endian> isym(psyms);
619
620       if (isym.get_st_type() == elfcpp::STT_SECTION)
621         continue;
622
623       unsigned int st_shndx = isym.get_st_shndx();
624       if (st_shndx < elfcpp::SHN_LORESERVE)
625         {
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();
630         }
631
632       elfcpp::Sym_write<size, big_endian> osym(ov);
633
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);
641
642       ov += sym_size;
643     }
644
645   assert(ov - oview == output_size);
646
647   of->write_output_view(this->local_symbol_offset_, output_size, oview);
648 }
649
650 // Input_objects methods.
651
652 // Add a regular relocatable object to the list.
653
654 void
655 Input_objects::add_object(Object* obj)
656 {
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));
661
662   Target* target = obj->target();
663   if (this->target_ == NULL)
664     this->target_ = target;
665   else if (this->target_ != target)
666     {
667       fprintf(stderr, "%s: %s: incompatible target\n",
668               program_name, obj->name().c_str());
669       gold_exit(false);
670     }
671 }
672
673 // Relocate_info methods.
674
675 // Return a string describing the location of a relocation.  This is
676 // only used in error messages.
677
678 template<int size, bool big_endian>
679 std::string
680 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
681 {
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;
696 }
697
698 } // End namespace gold.
699
700 namespace
701 {
702
703 using namespace gold;
704
705 // Read an ELF file with the header and return the appropriate
706 // instance of Object.
707
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)
712 {
713   int et = ehdr.get_e_type();
714   if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
715     {
716       fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
717               program_name, name.c_str(), static_cast<int>(et));
718       gold_exit(false);
719     }
720
721   if (et == elfcpp::ET_REL)
722     {
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;
727     }
728   else
729     {
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;
739     }
740 }
741
742 } // End anonymous namespace.
743
744 namespace gold
745 {
746
747 // Read an ELF file and return the appropriate instance of Object.
748
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)
752 {
753   if (bytes < elfcpp::EI_NIDENT)
754     {
755       fprintf(stderr, _("%s: %s: ELF file too short\n"),
756               program_name, name.c_str());
757       gold_exit(false);
758     }
759
760   int v = p[elfcpp::EI_VERSION];
761   if (v != elfcpp::EV_CURRENT)
762     {
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);
770     }
771
772   int c = p[elfcpp::EI_CLASS];
773   if (c == elfcpp::ELFCLASSNONE)
774     {
775       fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
776               program_name, name.c_str());
777       gold_exit(false);
778     }
779   else if (c != elfcpp::ELFCLASS32
780            && c != elfcpp::ELFCLASS64)
781     {
782       fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
783               program_name, name.c_str(), c);
784       gold_exit(false);
785     }
786
787   int d = p[elfcpp::EI_DATA];
788   if (d == elfcpp::ELFDATANONE)
789     {
790       fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
791               program_name, name.c_str());
792       gold_exit(false);
793     }
794   else if (d != elfcpp::ELFDATA2LSB
795            && d != elfcpp::ELFDATA2MSB)
796     {
797       fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
798               program_name, name.c_str(), d);
799       gold_exit(false);
800     }
801
802   bool big_endian = d == elfcpp::ELFDATA2MSB;
803
804   if (c == elfcpp::ELFCLASS32)
805     {
806       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
807         {
808           fprintf(stderr, _("%s: %s: ELF file too short\n"),
809                   program_name, name.c_str());
810           gold_exit(false);
811         }
812       if (big_endian)
813         {
814           elfcpp::Ehdr<32, true> ehdr(p);
815           return make_elf_sized_object<32, true>(name, input_file,
816                                                  offset, ehdr);
817         }
818       else
819         {
820           elfcpp::Ehdr<32, false> ehdr(p);
821           return make_elf_sized_object<32, false>(name, input_file,
822                                                   offset, ehdr);
823         }
824     }
825   else
826     {
827       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
828         {
829           fprintf(stderr, _("%s: %s: ELF file too short\n"),
830                   program_name, name.c_str());
831           gold_exit(false);
832         }
833       if (big_endian)
834         {
835           elfcpp::Ehdr<64, true> ehdr(p);
836           return make_elf_sized_object<64, true>(name, input_file,
837                                                  offset, ehdr);
838         }
839       else
840         {
841           elfcpp::Ehdr<64, false> ehdr(p);
842           return make_elf_sized_object<64, false>(name, input_file,
843                                                   offset, ehdr);
844         }
845     }
846 }
847
848 // Instantiate the templates we need.  We could use the configure
849 // script to restrict this to only the ones for implemented targets.
850
851 template
852 class Sized_relobj<32, false>;
853
854 template
855 class Sized_relobj<32, true>;
856
857 template
858 class Sized_relobj<64, false>;
859
860 template
861 class Sized_relobj<64, true>;
862
863 template
864 struct Relocate_info<32, false>;
865
866 template
867 struct Relocate_info<32, true>;
868
869 template
870 struct Relocate_info<64, false>;
871
872 template
873 struct Relocate_info<64, true>;
874
875 } // End namespace gold.