Add section_size_type and section_offset_type, use them to replace a
[platform/upstream/binutils.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "demangle.h"
29 #include "libiberty.h"
30
31 #include "target-select.h"
32 #include "dwarf_reader.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "symtab.h"
36 #include "reloc.h"
37 #include "object.h"
38 #include "dynobj.h"
39
40 namespace gold
41 {
42
43 // Class Object.
44
45 // Set the target based on fields in the ELF file header.
46
47 void
48 Object::set_target(int machine, int size, bool big_endian, int osabi,
49                    int abiversion)
50 {
51   Target* target = select_target(machine, size, big_endian, osabi, abiversion);
52   if (target == NULL)
53     gold_fatal(_("%s: unsupported ELF machine number %d"),
54                this->name().c_str(), machine);
55   this->target_ = target;
56 }
57
58 // Report an error for this object file.  This is used by the
59 // elfcpp::Elf_file interface, and also called by the Object code
60 // itself.
61
62 void
63 Object::error(const char* format, ...) const
64 {
65   va_list args;
66   va_start(args, format);
67   char* buf = NULL;
68   if (vasprintf(&buf, format, args) < 0)
69     gold_nomem();
70   va_end(args);
71   gold_error(_("%s: %s"), this->name().c_str(), buf);
72   free(buf);
73 }
74
75 // Return a view of the contents of a section.
76
77 const unsigned char*
78 Object::section_contents(unsigned int shndx, section_size_type* plen,
79                          bool cache)
80 {
81   Location loc(this->do_section_contents(shndx));
82   *plen = convert_to_section_size_type(loc.data_size);
83   return this->get_view(loc.file_offset, *plen, cache);
84 }
85
86 // Read the section data into SD.  This is code common to Sized_relobj
87 // and Sized_dynobj, so we put it into Object.
88
89 template<int size, bool big_endian>
90 void
91 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
92                           Read_symbols_data* sd)
93 {
94   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
95
96   // Read the section headers.
97   const off_t shoff = elf_file->shoff();
98   const unsigned int shnum = this->shnum();
99   sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
100
101   // Read the section names.
102   const unsigned char* pshdrs = sd->section_headers->data();
103   const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
104   typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
105
106   if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
107     this->error(_("section name section has wrong type: %u"),
108                 static_cast<unsigned int>(shdrnames.get_sh_type()));
109
110   sd->section_names_size =
111     convert_to_section_size_type(shdrnames.get_sh_size());
112   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
113                                              sd->section_names_size, false);
114 }
115
116 // If NAME is the name of a special .gnu.warning section, arrange for
117 // the warning to be issued.  SHNDX is the section index.  Return
118 // whether it is a warning section.
119
120 bool
121 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
122                                    Symbol_table* symtab)
123 {
124   const char warn_prefix[] = ".gnu.warning.";
125   const int warn_prefix_len = sizeof warn_prefix - 1;
126   if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
127     {
128       symtab->add_warning(name + warn_prefix_len, this, shndx);
129       return true;
130     }
131   return false;
132 }
133
134 // Class Sized_relobj.
135
136 template<int size, bool big_endian>
137 Sized_relobj<size, big_endian>::Sized_relobj(
138     const std::string& name,
139     Input_file* input_file,
140     off_t offset,
141     const elfcpp::Ehdr<size, big_endian>& ehdr)
142   : Relobj(name, input_file, offset),
143     elf_file_(this, ehdr),
144     symtab_shndx_(-1U),
145     local_symbol_count_(0),
146     output_local_symbol_count_(0),
147     output_local_dynsym_count_(0),
148     symbols_(),
149     local_symbol_offset_(0),
150     local_dynsym_offset_(0),
151     local_values_(),
152     local_got_offsets_(),
153     has_eh_frame_(false)
154 {
155 }
156
157 template<int size, bool big_endian>
158 Sized_relobj<size, big_endian>::~Sized_relobj()
159 {
160 }
161
162 // Set up an object file based on the file header.  This sets up the
163 // target and reads the section information.
164
165 template<int size, bool big_endian>
166 void
167 Sized_relobj<size, big_endian>::setup(
168     const elfcpp::Ehdr<size, big_endian>& ehdr)
169 {
170   this->set_target(ehdr.get_e_machine(), size, big_endian,
171                    ehdr.get_e_ident()[elfcpp::EI_OSABI],
172                    ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
173
174   const unsigned int shnum = this->elf_file_.shnum();
175   this->set_shnum(shnum);
176 }
177
178 // Find the SHT_SYMTAB section, given the section headers.  The ELF
179 // standard says that maybe in the future there can be more than one
180 // SHT_SYMTAB section.  Until somebody figures out how that could
181 // work, we assume there is only one.
182
183 template<int size, bool big_endian>
184 void
185 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
186 {
187   const unsigned int shnum = this->shnum();
188   this->symtab_shndx_ = 0;
189   if (shnum > 0)
190     {
191       // Look through the sections in reverse order, since gas tends
192       // to put the symbol table at the end.
193       const unsigned char* p = pshdrs + shnum * This::shdr_size;
194       unsigned int i = shnum;
195       while (i > 0)
196         {
197           --i;
198           p -= This::shdr_size;
199           typename This::Shdr shdr(p);
200           if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
201             {
202               this->symtab_shndx_ = i;
203               break;
204             }
205         }
206     }
207 }
208
209 // Return whether SHDR has the right type and flags to be a GNU
210 // .eh_frame section.
211
212 template<int size, bool big_endian>
213 bool
214 Sized_relobj<size, big_endian>::check_eh_frame_flags(
215     const elfcpp::Shdr<size, big_endian>* shdr) const
216 {
217   return (shdr->get_sh_size() > 0
218           && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
219           && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
220 }
221
222 // Return whether there is a GNU .eh_frame section, given the section
223 // headers and the section names.
224
225 template<int size, bool big_endian>
226 bool
227 Sized_relobj<size, big_endian>::find_eh_frame(
228     const unsigned char* pshdrs,
229     const char* names,
230     section_size_type names_size) const
231 {
232   const unsigned int shnum = this->shnum();
233   const unsigned char* p = pshdrs + This::shdr_size;
234   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
235     {
236       typename This::Shdr shdr(p);
237       if (this->check_eh_frame_flags(&shdr))
238         {
239           if (shdr.get_sh_name() >= names_size)
240             {
241               this->error(_("bad section name offset for section %u: %lu"),
242                           i, static_cast<unsigned long>(shdr.get_sh_name()));
243               continue;
244             }
245
246           const char* name = names + shdr.get_sh_name();
247           if (strcmp(name, ".eh_frame") == 0)
248             return true;
249         }
250     }
251   return false;
252 }
253
254 // Read the sections and symbols from an object file.
255
256 template<int size, bool big_endian>
257 void
258 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
259 {
260   this->read_section_data(&this->elf_file_, sd);
261
262   const unsigned char* const pshdrs = sd->section_headers->data();
263
264   this->find_symtab(pshdrs);
265
266   const unsigned char* namesu = sd->section_names->data();
267   const char* names = reinterpret_cast<const char*>(namesu);
268   if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
269     this->has_eh_frame_ = true;
270
271   sd->symbols = NULL;
272   sd->symbols_size = 0;
273   sd->external_symbols_offset = 0;
274   sd->symbol_names = NULL;
275   sd->symbol_names_size = 0;
276
277   if (this->symtab_shndx_ == 0)
278     {
279       // No symbol table.  Weird but legal.
280       return;
281     }
282
283   // Get the symbol table section header.
284   typename This::Shdr symtabshdr(pshdrs
285                                  + this->symtab_shndx_ * This::shdr_size);
286   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
287
288   // If this object has a .eh_frame section, we need all the symbols.
289   // Otherwise we only need the external symbols.  While it would be
290   // simpler to just always read all the symbols, I've seen object
291   // files with well over 2000 local symbols, which for a 64-bit
292   // object file format is over 5 pages that we don't need to read
293   // now.
294
295   const int sym_size = This::sym_size;
296   const unsigned int loccount = symtabshdr.get_sh_info();
297   this->local_symbol_count_ = loccount;
298   this->local_values_.resize(loccount);
299   section_offset_type locsize = loccount * sym_size;
300   off_t dataoff = symtabshdr.get_sh_offset();
301   section_size_type datasize =
302     convert_to_section_size_type(symtabshdr.get_sh_size());
303   off_t extoff = dataoff + locsize;
304   section_size_type extsize = datasize - locsize;
305
306   off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
307   section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
308
309   File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
310
311   // Read the section header for the symbol names.
312   unsigned int strtab_shndx = symtabshdr.get_sh_link();
313   if (strtab_shndx >= this->shnum())
314     {
315       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
316       return;
317     }
318   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
319   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
320     {
321       this->error(_("symbol table name section has wrong type: %u"),
322                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
323       return;
324     }
325
326   // Read the symbol names.
327   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
328                                                strtabshdr.get_sh_size(), true);
329
330   sd->symbols = fvsymtab;
331   sd->symbols_size = readsize;
332   sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
333   sd->symbol_names = fvstrtab;
334   sd->symbol_names_size =
335     convert_to_section_size_type(strtabshdr.get_sh_size());
336 }
337
338 // Return the section index of symbol SYM.  Set *VALUE to its value in
339 // the object file.  Note that for a symbol which is not defined in
340 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
341 // it will not return the final value of the symbol in the link.
342
343 template<int size, bool big_endian>
344 unsigned int
345 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
346                                                          Address* value)
347 {
348   section_size_type symbols_size;
349   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
350                                                         &symbols_size,
351                                                         false);
352
353   const size_t count = symbols_size / This::sym_size;
354   gold_assert(sym < count);
355
356   elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
357   *value = elfsym.get_st_value();
358   // FIXME: Handle SHN_XINDEX.
359   return elfsym.get_st_shndx();
360 }
361
362 // Return whether to include a section group in the link.  LAYOUT is
363 // used to keep track of which section groups we have already seen.
364 // INDEX is the index of the section group and SHDR is the section
365 // header.  If we do not want to include this group, we set bits in
366 // OMIT for each section which should be discarded.
367
368 template<int size, bool big_endian>
369 bool
370 Sized_relobj<size, big_endian>::include_section_group(
371     Layout* layout,
372     unsigned int index,
373     const elfcpp::Shdr<size, big_endian>& shdr,
374     std::vector<bool>* omit)
375 {
376   // Read the section contents.
377   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
378                                              shdr.get_sh_size(), false);
379   const elfcpp::Elf_Word* pword =
380     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
381
382   // The first word contains flags.  We only care about COMDAT section
383   // groups.  Other section groups are always included in the link
384   // just like ordinary sections.
385   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
386   if ((flags & elfcpp::GRP_COMDAT) == 0)
387     return true;
388
389   // Look up the group signature, which is the name of a symbol.  This
390   // is a lot of effort to go to to read a string.  Why didn't they
391   // just use the name of the SHT_GROUP section as the group
392   // signature?
393
394   // Get the appropriate symbol table header (this will normally be
395   // the single SHT_SYMTAB section, but in principle it need not be).
396   const unsigned int link = shdr.get_sh_link();
397   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
398
399   // Read the symbol table entry.
400   if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
401     {
402       this->error(_("section group %u info %u out of range"),
403                   index, shdr.get_sh_info());
404       return false;
405     }
406   off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
407   const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
408   elfcpp::Sym<size, big_endian> sym(psym);
409
410   // Read the symbol table names.
411   section_size_type symnamelen;
412   const unsigned char* psymnamesu;
413   psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
414                                       true);
415   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
416
417   // Get the section group signature.
418   if (sym.get_st_name() >= symnamelen)
419     {
420       this->error(_("symbol %u name offset %u out of range"),
421                   shdr.get_sh_info(), sym.get_st_name());
422       return false;
423     }
424
425   const char* signature = psymnames + sym.get_st_name();
426
427   // It seems that some versions of gas will create a section group
428   // associated with a section symbol, and then fail to give a name to
429   // the section symbol.  In such a case, use the name of the section.
430   // FIXME.
431   std::string secname;
432   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
433     {
434       secname = this->section_name(sym.get_st_shndx());
435       signature = secname.c_str();
436     }
437
438   // Record this section group, and see whether we've already seen one
439   // with the same signature.
440   if (layout->add_comdat(signature, true))
441     return true;
442
443   // This is a duplicate.  We want to discard the sections in this
444   // group.
445   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
446   for (size_t i = 1; i < count; ++i)
447     {
448       elfcpp::Elf_Word secnum =
449         elfcpp::Swap<32, big_endian>::readval(pword + i);
450       if (secnum >= this->shnum())
451         {
452           this->error(_("section %u in section group %u out of range"),
453                       secnum, index);
454           continue;
455         }
456       (*omit)[secnum] = true;
457     }
458
459   return false;
460 }
461
462 // Whether to include a linkonce section in the link.  NAME is the
463 // name of the section and SHDR is the section header.
464
465 // Linkonce sections are a GNU extension implemented in the original
466 // GNU linker before section groups were defined.  The semantics are
467 // that we only include one linkonce section with a given name.  The
468 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
469 // where T is the type of section and SYMNAME is the name of a symbol.
470 // In an attempt to make linkonce sections interact well with section
471 // groups, we try to identify SYMNAME and use it like a section group
472 // signature.  We want to block section groups with that signature,
473 // but not other linkonce sections with that signature.  We also use
474 // the full name of the linkonce section as a normal section group
475 // signature.
476
477 template<int size, bool big_endian>
478 bool
479 Sized_relobj<size, big_endian>::include_linkonce_section(
480     Layout* layout,
481     const char* name,
482     const elfcpp::Shdr<size, big_endian>&)
483 {
484   // In general the symbol name we want will be the string following
485   // the last '.'.  However, we have to handle the case of
486   // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
487   // some versions of gcc.  So we use a heuristic: if the name starts
488   // with ".gnu.linkonce.t.", we use everything after that.  Otherwise
489   // we look for the last '.'.  We can't always simply skip
490   // ".gnu.linkonce.X", because we have to deal with cases like
491   // ".gnu.linkonce.d.rel.ro.local".
492   const char* const linkonce_t = ".gnu.linkonce.t.";
493   const char* symname;
494   if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
495     symname = name + strlen(linkonce_t);
496   else
497     symname = strrchr(name, '.') + 1;
498   bool include1 = layout->add_comdat(symname, false);
499   bool include2 = layout->add_comdat(name, true);
500   return include1 && include2;
501 }
502
503 // Lay out the input sections.  We walk through the sections and check
504 // whether they should be included in the link.  If they should, we
505 // pass them to the Layout object, which will return an output section
506 // and an offset.
507
508 template<int size, bool big_endian>
509 void
510 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
511                                           Layout* layout,
512                                           Read_symbols_data* sd)
513 {
514   const unsigned int shnum = this->shnum();
515   if (shnum == 0)
516     return;
517
518   // Get the section headers.
519   const unsigned char* pshdrs = sd->section_headers->data();
520
521   // Get the section names.
522   const unsigned char* pnamesu = sd->section_names->data();
523   const char* pnames = reinterpret_cast<const char*>(pnamesu);
524
525   // For each section, record the index of the reloc section if any.
526   // Use 0 to mean that there is no reloc section, -1U to mean that
527   // there is more than one.
528   std::vector<unsigned int> reloc_shndx(shnum, 0);
529   std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
530   // Skip the first, dummy, section.
531   pshdrs += This::shdr_size;
532   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
533     {
534       typename This::Shdr shdr(pshdrs);
535
536       unsigned int sh_type = shdr.get_sh_type();
537       if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
538         {
539           unsigned int target_shndx = shdr.get_sh_info();
540           if (target_shndx == 0 || target_shndx >= shnum)
541             {
542               this->error(_("relocation section %u has bad info %u"),
543                           i, target_shndx);
544               continue;
545             }
546
547           if (reloc_shndx[target_shndx] != 0)
548             reloc_shndx[target_shndx] = -1U;
549           else
550             {
551               reloc_shndx[target_shndx] = i;
552               reloc_type[target_shndx] = sh_type;
553             }
554         }
555     }
556
557   std::vector<Map_to_output>& map_sections(this->map_to_output());
558   map_sections.resize(shnum);
559
560   // Whether we've seen a .note.GNU-stack section.
561   bool seen_gnu_stack = false;
562   // The flags of a .note.GNU-stack section.
563   uint64_t gnu_stack_flags = 0;
564
565   // Keep track of which sections to omit.
566   std::vector<bool> omit(shnum, false);
567
568   // Keep track of .eh_frame sections.
569   std::vector<unsigned int> eh_frame_sections;
570
571   // Skip the first, dummy, section.
572   pshdrs = sd->section_headers->data() + This::shdr_size;
573   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
574     {
575       typename This::Shdr shdr(pshdrs);
576
577       if (shdr.get_sh_name() >= sd->section_names_size)
578         {
579           this->error(_("bad section name offset for section %u: %lu"),
580                       i, static_cast<unsigned long>(shdr.get_sh_name()));
581           return;
582         }
583
584       const char* name = pnames + shdr.get_sh_name();
585
586       if (this->handle_gnu_warning_section(name, i, symtab))
587         {
588           if (!parameters->output_is_object())
589             omit[i] = true;
590         }
591
592       // The .note.GNU-stack section is special.  It gives the
593       // protection flags that this object file requires for the stack
594       // in memory.
595       if (strcmp(name, ".note.GNU-stack") == 0)
596         {
597           seen_gnu_stack = true;
598           gnu_stack_flags |= shdr.get_sh_flags();
599           omit[i] = true;
600         }
601
602       bool discard = omit[i];
603       if (!discard)
604         {
605           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
606             {
607               if (!this->include_section_group(layout, i, shdr, &omit))
608                 discard = true;
609             }
610           else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
611                    && Layout::is_linkonce(name))
612             {
613               if (!this->include_linkonce_section(layout, name, shdr))
614                 discard = true;
615             }
616         }
617
618       if (discard)
619         {
620           // Do not include this section in the link.
621           map_sections[i].output_section = NULL;
622           continue;
623         }
624
625       // The .eh_frame section is special.  It holds exception frame
626       // information that we need to read in order to generate the
627       // exception frame header.  We process these after all the other
628       // sections so that the exception frame reader can reliably
629       // determine which sections are being discarded, and discard the
630       // corresponding information.
631       if (!parameters->output_is_object()
632           && strcmp(name, ".eh_frame") == 0
633           && this->check_eh_frame_flags(&shdr))
634         {
635           eh_frame_sections.push_back(i);
636           continue;
637         }
638
639       off_t offset;
640       Output_section* os = layout->layout(this, i, name, shdr,
641                                           reloc_shndx[i], reloc_type[i],
642                                           &offset);
643
644       map_sections[i].output_section = os;
645       map_sections[i].offset = offset;
646
647       // If this section requires special handling, and if there are
648       // relocs that apply to it, then we must do the special handling
649       // before we apply the relocs.
650       if (offset == -1 && reloc_shndx[i] != 0)
651         this->set_relocs_must_follow_section_writes();
652     }
653
654   layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
655
656   // Handle the .eh_frame sections at the end.
657   for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
658        p != eh_frame_sections.end();
659        ++p)
660     {
661       gold_assert(this->has_eh_frame_);
662       gold_assert(sd->external_symbols_offset != 0);
663
664       unsigned int i = *p;
665       const unsigned char *pshdr;
666       pshdr = sd->section_headers->data() + i * This::shdr_size;
667       typename This::Shdr shdr(pshdr);
668
669       off_t offset;
670       Output_section* os = layout->layout_eh_frame(this,
671                                                    sd->symbols->data(),
672                                                    sd->symbols_size,
673                                                    sd->symbol_names->data(),
674                                                    sd->symbol_names_size,
675                                                    i, shdr,
676                                                    reloc_shndx[i],
677                                                    reloc_type[i],
678                                                    &offset);
679       map_sections[i].output_section = os;
680       map_sections[i].offset = offset;
681
682       // If this section requires special handling, and if there are
683       // relocs that apply to it, then we must do the special handling
684       // before we apply the relocs.
685       if (offset == -1 && reloc_shndx[i] != 0)
686         this->set_relocs_must_follow_section_writes();
687     }
688
689   delete sd->section_headers;
690   sd->section_headers = NULL;
691   delete sd->section_names;
692   sd->section_names = NULL;
693 }
694
695 // Add the symbols to the symbol table.
696
697 template<int size, bool big_endian>
698 void
699 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
700                                                Read_symbols_data* sd)
701 {
702   if (sd->symbols == NULL)
703     {
704       gold_assert(sd->symbol_names == NULL);
705       return;
706     }
707
708   const int sym_size = This::sym_size;
709   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
710                      / sym_size);
711   if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
712     {
713       this->error(_("size of symbols is not multiple of symbol size"));
714       return;
715     }
716
717   this->symbols_.resize(symcount);
718
719   const char* sym_names =
720     reinterpret_cast<const char*>(sd->symbol_names->data());
721   symtab->add_from_relobj(this,
722                           sd->symbols->data() + sd->external_symbols_offset,
723                           symcount, sym_names, sd->symbol_names_size,
724                           &this->symbols_);
725
726   delete sd->symbols;
727   sd->symbols = NULL;
728   delete sd->symbol_names;
729   sd->symbol_names = NULL;
730 }
731
732 // Finalize the local symbols.  Here we add their names to *POOL and
733 // *DYNPOOL, and we add their values to THIS->LOCAL_VALUES_.  This
734 // function is always called from a singleton thread.  The actual
735 // output of the local symbols will occur in a separate task.
736
737 template<int size, bool big_endian>
738 void
739 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
740                                                        Stringpool* dynpool)
741 {
742   gold_assert(this->symtab_shndx_ != -1U);
743   if (this->symtab_shndx_ == 0)
744     {
745       // This object has no symbols.  Weird but legal.
746       return;
747     }
748
749   // Read the symbol table section header.
750   const unsigned int symtab_shndx = this->symtab_shndx_;
751   typename This::Shdr symtabshdr(this,
752                                  this->elf_file_.section_header(symtab_shndx));
753   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
754
755   // Read the local symbols.
756   const int sym_size = This::sym_size;
757   const unsigned int loccount = this->local_symbol_count_;
758   gold_assert(loccount == symtabshdr.get_sh_info());
759   off_t locsize = loccount * sym_size;
760   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
761                                               locsize, true);
762
763   // Read the symbol names.
764   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
765   section_size_type strtab_size;
766   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
767                                                         &strtab_size,
768                                                         true);
769   const char* pnames = reinterpret_cast<const char*>(pnamesu);
770
771   // Loop over the local symbols.
772
773   const std::vector<Map_to_output>& mo(this->map_to_output());
774   unsigned int shnum = this->shnum();
775   unsigned int count = 0;
776   unsigned int dyncount = 0;
777   // Skip the first, dummy, symbol.
778   psyms += sym_size;
779   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
780     {
781       elfcpp::Sym<size, big_endian> sym(psyms);
782
783       Symbol_value<size>& lv(this->local_values_[i]);
784
785       unsigned int shndx = sym.get_st_shndx();
786       lv.set_input_shndx(shndx);
787
788       if (sym.get_st_type() == elfcpp::STT_SECTION)
789         lv.set_is_section_symbol();
790       else if (sym.get_st_type() == elfcpp::STT_TLS)
791         lv.set_is_tls_symbol();
792
793       // Save the input symbol value for use in do_finalize_local_symbols().
794       lv.set_input_value(sym.get_st_value());
795
796       // Decide whether this symbol should go into the output file.
797
798       if (shndx < shnum && mo[shndx].output_section == NULL)
799         {
800           lv.set_no_output_symtab_entry();
801           continue;
802         }
803
804       if (sym.get_st_type() == elfcpp::STT_SECTION)
805         {
806           lv.set_no_output_symtab_entry();
807           continue;
808         }
809
810       if (sym.get_st_name() >= strtab_size)
811         {
812           this->error(_("local symbol %u section name out of range: %u >= %u"),
813                       i, sym.get_st_name(),
814                       static_cast<unsigned int>(strtab_size));
815           lv.set_no_output_symtab_entry();
816           continue;
817         }
818
819       // Add the symbol to the symbol table string pool.
820       const char* name = pnames + sym.get_st_name();
821       pool->add(name, true, NULL);
822       ++count;
823
824       // If needed, add the symbol to the dynamic symbol table string pool.
825       if (lv.needs_output_dynsym_entry())
826         {
827           dynpool->add(name, true, NULL);
828           ++dyncount;
829         }
830     }
831
832   this->output_local_symbol_count_ = count;
833   this->output_local_dynsym_count_ = dyncount;
834 }
835
836 // Finalize the local symbols.  Here we add their values to
837 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
838 // This function is always called from a singleton thread.  The actual
839 // output of the local symbols will occur in a separate task.
840
841 template<int size, bool big_endian>
842 unsigned int
843 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
844                                                           off_t off)
845 {
846   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
847
848   const unsigned int loccount = this->local_symbol_count_;
849   this->local_symbol_offset_ = off;
850
851   const std::vector<Map_to_output>& mo(this->map_to_output());
852   unsigned int shnum = this->shnum();
853
854   for (unsigned int i = 1; i < loccount; ++i)
855     {
856       Symbol_value<size>& lv(this->local_values_[i]);
857
858       unsigned int shndx = lv.input_shndx();
859
860       // Set the output symbol value.
861       
862       if (shndx >= elfcpp::SHN_LORESERVE)
863         {
864           if (shndx == elfcpp::SHN_ABS)
865             lv.set_output_value(lv.input_value());
866           else
867             {
868               // FIXME: Handle SHN_XINDEX.
869               this->error(_("unknown section index %u for local symbol %u"),
870                           shndx, i);
871               lv.set_output_value(0);
872             }
873         }
874       else
875         {
876           if (shndx >= shnum)
877             {
878               this->error(_("local symbol %u section index %u out of range"),
879                           i, shndx);
880               shndx = 0;
881             }
882
883           Output_section* os = mo[shndx].output_section;
884
885           if (os == NULL)
886             {
887               lv.set_output_value(0);
888               continue;
889             }
890           else if (mo[shndx].offset == -1)
891             {
892               // Leave the input value in place for SHF_MERGE sections.
893             }
894           else if (lv.is_tls_symbol())
895             lv.set_output_value(mo[shndx].output_section->tls_offset()
896                                 + mo[shndx].offset
897                                 + lv.input_value());
898           else
899             lv.set_output_value(mo[shndx].output_section->address()
900                                 + mo[shndx].offset
901                                 + lv.input_value());
902         }
903
904       if (lv.needs_output_symtab_entry())
905         {
906           lv.set_output_symtab_index(index);
907           ++index;
908         }
909     }
910   return index;
911 }
912
913 // Set the output dynamic symbol table indexes for the local variables.
914
915 template<int size, bool big_endian>
916 unsigned int
917 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
918 {
919   const unsigned int loccount = this->local_symbol_count_;
920   for (unsigned int i = 1; i < loccount; ++i)
921     {
922       Symbol_value<size>& lv(this->local_values_[i]);
923       if (lv.needs_output_dynsym_entry())
924         {
925           lv.set_output_dynsym_index(index);
926           ++index;
927         }
928     }
929   return index;
930 }
931
932 // Set the offset where local dynamic symbol information will be stored.
933 // Returns the count of local symbols contributed to the symbol table by
934 // this object.
935
936 template<int size, bool big_endian>
937 unsigned int
938 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
939 {
940   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
941   this->local_dynsym_offset_ = off;
942   return this->output_local_dynsym_count_;
943 }
944
945 // Return the value of the local symbol symndx.
946 template<int size, bool big_endian>
947 typename elfcpp::Elf_types<size>::Elf_Addr
948 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
949 {
950   gold_assert(symndx < this->local_symbol_count_);
951   gold_assert(symndx < this->local_values_.size());
952   const Symbol_value<size>& lv(this->local_values_[symndx]);
953   return lv.value(this, 0);
954 }
955
956 // Return the value of a local symbol defined in input section SHNDX,
957 // with value VALUE, adding addend ADDEND.  IS_SECTION_SYMBOL
958 // indicates whether the symbol is a section symbol.  This handles
959 // SHF_MERGE sections.
960 template<int size, bool big_endian>
961 typename elfcpp::Elf_types<size>::Elf_Addr
962 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
963                                             Address value,
964                                             bool is_section_symbol,
965                                             Address addend) const
966 {
967   const std::vector<Map_to_output>& mo(this->map_to_output());
968   Output_section* os = mo[shndx].output_section;
969   if (os == NULL)
970     return addend;
971   gold_assert(mo[shndx].offset == -1);
972
973   // Do the mapping required by the output section.  If this is not a
974   // section symbol, then we want to map the symbol value, and then
975   // include the addend.  If this is a section symbol, then we need to
976   // include the addend to figure out where in the section we are,
977   // before we do the mapping.  This will do the right thing provided
978   // the assembler is careful to only convert a relocation in a merged
979   // section to a section symbol if there is a zero addend.  If the
980   // assembler does not do this, then in general we can't know what to
981   // do, because we can't distinguish the addend for the instruction
982   // format from the addend for the section offset.
983
984   if (is_section_symbol)
985     return os->output_address(this, shndx, value + addend);
986   else
987     return addend + os->output_address(this, shndx, value);
988 }
989
990 // Write out the local symbols.
991
992 template<int size, bool big_endian>
993 void
994 Sized_relobj<size, big_endian>::write_local_symbols(
995     Output_file* of,
996     const Stringpool* sympool,
997     const Stringpool* dynpool)
998 {
999   if (parameters->strip_all() && this->output_local_dynsym_count_ == 0)
1000     return;
1001
1002   gold_assert(this->symtab_shndx_ != -1U);
1003   if (this->symtab_shndx_ == 0)
1004     {
1005       // This object has no symbols.  Weird but legal.
1006       return;
1007     }
1008
1009   // Read the symbol table section header.
1010   const unsigned int symtab_shndx = this->symtab_shndx_;
1011   typename This::Shdr symtabshdr(this,
1012                                  this->elf_file_.section_header(symtab_shndx));
1013   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1014   const unsigned int loccount = this->local_symbol_count_;
1015   gold_assert(loccount == symtabshdr.get_sh_info());
1016
1017   // Read the local symbols.
1018   const int sym_size = This::sym_size;
1019   off_t locsize = loccount * sym_size;
1020   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1021                                               locsize, false);
1022
1023   // Read the symbol names.
1024   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
1025   section_size_type strtab_size;
1026   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1027                                                         &strtab_size,
1028                                                         true);
1029   const char* pnames = reinterpret_cast<const char*>(pnamesu);
1030
1031   // Get views into the output file for the portions of the symbol table
1032   // and the dynamic symbol table that we will be writing.
1033   off_t output_size = this->output_local_symbol_count_ * sym_size;
1034   unsigned char* oview = NULL;
1035   if (output_size > 0)
1036     oview = of->get_output_view(this->local_symbol_offset_, output_size);
1037
1038   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1039   unsigned char* dyn_oview = NULL;
1040   if (dyn_output_size > 0)
1041     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1042                                     dyn_output_size);
1043
1044   const std::vector<Map_to_output>& mo(this->map_to_output());
1045
1046   gold_assert(this->local_values_.size() == loccount);
1047
1048   unsigned char* ov = oview;
1049   unsigned char* dyn_ov = dyn_oview;
1050   psyms += sym_size;
1051   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1052     {
1053       elfcpp::Sym<size, big_endian> isym(psyms);
1054
1055       unsigned int st_shndx = isym.get_st_shndx();
1056       if (st_shndx < elfcpp::SHN_LORESERVE)
1057         {
1058           gold_assert(st_shndx < mo.size());
1059           if (mo[st_shndx].output_section == NULL)
1060             continue;
1061           st_shndx = mo[st_shndx].output_section->out_shndx();
1062         }
1063
1064       // Write the symbol to the output symbol table.
1065       if (!parameters->strip_all()
1066           && this->local_values_[i].needs_output_symtab_entry())
1067         {
1068           elfcpp::Sym_write<size, big_endian> osym(ov);
1069
1070           gold_assert(isym.get_st_name() < strtab_size);
1071           const char* name = pnames + isym.get_st_name();
1072           osym.put_st_name(sympool->get_offset(name));
1073           osym.put_st_value(this->local_values_[i].value(this, 0));
1074           osym.put_st_size(isym.get_st_size());
1075           osym.put_st_info(isym.get_st_info());
1076           osym.put_st_other(isym.get_st_other());
1077           osym.put_st_shndx(st_shndx);
1078
1079           ov += sym_size;
1080         }
1081
1082       // Write the symbol to the output dynamic symbol table.
1083       if (this->local_values_[i].needs_output_dynsym_entry())
1084         {
1085           gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1086           elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1087
1088           gold_assert(isym.get_st_name() < strtab_size);
1089           const char* name = pnames + isym.get_st_name();
1090           osym.put_st_name(dynpool->get_offset(name));
1091           osym.put_st_value(this->local_values_[i].value(this, 0));
1092           osym.put_st_size(isym.get_st_size());
1093           osym.put_st_info(isym.get_st_info());
1094           osym.put_st_other(isym.get_st_other());
1095           osym.put_st_shndx(st_shndx);
1096
1097           dyn_ov += sym_size;
1098         }
1099     }
1100
1101
1102   if (output_size > 0)
1103     {
1104       gold_assert(ov - oview == output_size);
1105       of->write_output_view(this->local_symbol_offset_, output_size, oview);
1106     }
1107
1108   if (dyn_output_size > 0)
1109     {
1110       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1111       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1112                             dyn_oview);
1113     }
1114 }
1115
1116 // Set *INFO to symbolic information about the offset OFFSET in the
1117 // section SHNDX.  Return true if we found something, false if we
1118 // found nothing.
1119
1120 template<int size, bool big_endian>
1121 bool
1122 Sized_relobj<size, big_endian>::get_symbol_location_info(
1123     unsigned int shndx,
1124     off_t offset,
1125     Symbol_location_info* info)
1126 {
1127   if (this->symtab_shndx_ == 0)
1128     return false;
1129
1130   section_size_type symbols_size;
1131   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1132                                                         &symbols_size,
1133                                                         false);
1134
1135   unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1136   section_size_type names_size;
1137   const unsigned char* symbol_names_u =
1138     this->section_contents(symbol_names_shndx, &names_size, false);
1139   const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1140
1141   const int sym_size = This::sym_size;
1142   const size_t count = symbols_size / sym_size;
1143
1144   const unsigned char* p = symbols;
1145   for (size_t i = 0; i < count; ++i, p += sym_size)
1146     {
1147       elfcpp::Sym<size, big_endian> sym(p);
1148
1149       if (sym.get_st_type() == elfcpp::STT_FILE)
1150         {
1151           if (sym.get_st_name() >= names_size)
1152             info->source_file = "(invalid)";
1153           else
1154             info->source_file = symbol_names + sym.get_st_name();
1155         }
1156       else if (sym.get_st_shndx() == shndx
1157                && static_cast<off_t>(sym.get_st_value()) <= offset
1158                && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1159                    > offset))
1160         {
1161           if (sym.get_st_name() > names_size)
1162             info->enclosing_symbol_name = "(invalid)";
1163           else
1164             {
1165               info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1166               if (parameters->demangle())
1167                 {
1168                   char* demangled_name = cplus_demangle(
1169                       info->enclosing_symbol_name.c_str(),
1170                       DMGL_ANSI | DMGL_PARAMS);
1171                   if (demangled_name != NULL)
1172                     {
1173                       info->enclosing_symbol_name.assign(demangled_name);
1174                       free(demangled_name);
1175                     }
1176                 }
1177             }
1178           return true;
1179         }
1180     }
1181
1182   return false;
1183 }
1184
1185 // Input_objects methods.
1186
1187 // Add a regular relocatable object to the list.  Return false if this
1188 // object should be ignored.
1189
1190 bool
1191 Input_objects::add_object(Object* obj)
1192 {
1193   Target* target = obj->target();
1194   if (this->target_ == NULL)
1195     this->target_ = target;
1196   else if (this->target_ != target)
1197     {
1198       gold_error(_("%s: incompatible target"), obj->name().c_str());
1199       return false;
1200     }
1201
1202   if (!obj->is_dynamic())
1203     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1204   else
1205     {
1206       // See if this is a duplicate SONAME.
1207       Dynobj* dynobj = static_cast<Dynobj*>(obj);
1208       const char* soname = dynobj->soname();
1209
1210       std::pair<Unordered_set<std::string>::iterator, bool> ins =
1211         this->sonames_.insert(soname);
1212       if (!ins.second)
1213         {
1214           // We have already seen a dynamic object with this soname.
1215           return false;
1216         }
1217
1218       this->dynobj_list_.push_back(dynobj);
1219
1220       // If this is -lc, remember the directory in which we found it.
1221       // We use this when issuing warnings about undefined symbols: as
1222       // a heuristic, we don't warn about system libraries found in
1223       // the same directory as -lc.
1224       if (strncmp(soname, "libc.so", 7) == 0)
1225         {
1226           const char* object_name = dynobj->name().c_str();
1227           const char* base = lbasename(object_name);
1228           if (base != object_name)
1229             this->system_library_directory_.assign(object_name,
1230                                                    base - 1 - object_name);
1231         }
1232     }
1233
1234   set_parameters_target(target);
1235
1236   return true;
1237 }
1238
1239 // Return whether an object was found in the system library directory.
1240
1241 bool
1242 Input_objects::found_in_system_library_directory(const Object* object) const
1243 {
1244   return (!this->system_library_directory_.empty()
1245           && object->name().compare(0,
1246                                     this->system_library_directory_.size(),
1247                                     this->system_library_directory_) == 0);
1248 }
1249
1250 // For each dynamic object, record whether we've seen all of its
1251 // explicit dependencies.
1252
1253 void
1254 Input_objects::check_dynamic_dependencies() const
1255 {
1256   for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1257        p != this->dynobj_list_.end();
1258        ++p)
1259     {
1260       const Dynobj::Needed& needed((*p)->needed());
1261       bool found_all = true;
1262       for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1263            pneeded != needed.end();
1264            ++pneeded)
1265         {
1266           if (this->sonames_.find(*pneeded) == this->sonames_.end())
1267             {
1268               found_all = false;
1269               break;
1270             }
1271         }
1272       (*p)->set_has_unknown_needed_entries(!found_all);
1273     }
1274 }
1275
1276 // Relocate_info methods.
1277
1278 // Return a string describing the location of a relocation.  This is
1279 // only used in error messages.
1280
1281 template<int size, bool big_endian>
1282 std::string
1283 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1284 {
1285   // See if we can get line-number information from debugging sections.
1286   std::string filename;
1287   std::string file_and_lineno;   // Better than filename-only, if available.
1288
1289   Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1290   // This will be "" if we failed to parse the debug info for any reason.
1291   file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1292
1293   std::string ret(this->object->name());
1294   ret += ':';
1295   Symbol_location_info info;
1296   if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1297     {
1298       ret += " in function ";
1299       ret += info.enclosing_symbol_name;
1300       ret += ":";
1301       filename = info.source_file;
1302     }
1303
1304   if (!file_and_lineno.empty())
1305     ret += file_and_lineno;
1306   else
1307     {
1308       if (!filename.empty())
1309         ret += filename;
1310       ret += "(";
1311       ret += this->object->section_name(this->data_shndx);
1312       char buf[100];
1313       // Offsets into sections have to be positive.
1314       snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1315       ret += buf;
1316       ret += ")";
1317     }
1318   return ret;
1319 }
1320
1321 } // End namespace gold.
1322
1323 namespace
1324 {
1325
1326 using namespace gold;
1327
1328 // Read an ELF file with the header and return the appropriate
1329 // instance of Object.
1330
1331 template<int size, bool big_endian>
1332 Object*
1333 make_elf_sized_object(const std::string& name, Input_file* input_file,
1334                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1335 {
1336   int et = ehdr.get_e_type();
1337   if (et == elfcpp::ET_REL)
1338     {
1339       Sized_relobj<size, big_endian>* obj =
1340         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1341       obj->setup(ehdr);
1342       return obj;
1343     }
1344   else if (et == elfcpp::ET_DYN)
1345     {
1346       Sized_dynobj<size, big_endian>* obj =
1347         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1348       obj->setup(ehdr);
1349       return obj;
1350     }
1351   else
1352     {
1353       gold_error(_("%s: unsupported ELF file type %d"),
1354                  name.c_str(), et);
1355       return NULL;
1356     }
1357 }
1358
1359 } // End anonymous namespace.
1360
1361 namespace gold
1362 {
1363
1364 // Read an ELF file and return the appropriate instance of Object.
1365
1366 Object*
1367 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1368                 const unsigned char* p, section_offset_type bytes)
1369 {
1370   if (bytes < elfcpp::EI_NIDENT)
1371     {
1372       gold_error(_("%s: ELF file too short"), name.c_str());
1373       return NULL;
1374     }
1375
1376   int v = p[elfcpp::EI_VERSION];
1377   if (v != elfcpp::EV_CURRENT)
1378     {
1379       if (v == elfcpp::EV_NONE)
1380         gold_error(_("%s: invalid ELF version 0"), name.c_str());
1381       else
1382         gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1383       return NULL;
1384     }
1385
1386   int c = p[elfcpp::EI_CLASS];
1387   if (c == elfcpp::ELFCLASSNONE)
1388     {
1389       gold_error(_("%s: invalid ELF class 0"), name.c_str());
1390       return NULL;
1391     }
1392   else if (c != elfcpp::ELFCLASS32
1393            && c != elfcpp::ELFCLASS64)
1394     {
1395       gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1396       return NULL;
1397     }
1398
1399   int d = p[elfcpp::EI_DATA];
1400   if (d == elfcpp::ELFDATANONE)
1401     {
1402       gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1403       return NULL;
1404     }
1405   else if (d != elfcpp::ELFDATA2LSB
1406            && d != elfcpp::ELFDATA2MSB)
1407     {
1408       gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1409       return NULL;
1410     }
1411
1412   bool big_endian = d == elfcpp::ELFDATA2MSB;
1413
1414   if (c == elfcpp::ELFCLASS32)
1415     {
1416       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1417         {
1418           gold_error(_("%s: ELF file too short"), name.c_str());
1419           return NULL;
1420         }
1421       if (big_endian)
1422         {
1423 #ifdef HAVE_TARGET_32_BIG
1424           elfcpp::Ehdr<32, true> ehdr(p);
1425           return make_elf_sized_object<32, true>(name, input_file,
1426                                                  offset, ehdr);
1427 #else
1428           gold_error(_("%s: not configured to support "
1429                        "32-bit big-endian object"),
1430                      name.c_str());
1431           return NULL;
1432 #endif
1433         }
1434       else
1435         {
1436 #ifdef HAVE_TARGET_32_LITTLE
1437           elfcpp::Ehdr<32, false> ehdr(p);
1438           return make_elf_sized_object<32, false>(name, input_file,
1439                                                   offset, ehdr);
1440 #else
1441           gold_error(_("%s: not configured to support "
1442                        "32-bit little-endian object"),
1443                      name.c_str());
1444           return NULL;
1445 #endif
1446         }
1447     }
1448   else
1449     {
1450       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1451         {
1452           gold_error(_("%s: ELF file too short"), name.c_str());
1453           return NULL;
1454         }
1455       if (big_endian)
1456         {
1457 #ifdef HAVE_TARGET_64_BIG
1458           elfcpp::Ehdr<64, true> ehdr(p);
1459           return make_elf_sized_object<64, true>(name, input_file,
1460                                                  offset, ehdr);
1461 #else
1462           gold_error(_("%s: not configured to support "
1463                        "64-bit big-endian object"),
1464                      name.c_str());
1465           return NULL;
1466 #endif
1467         }
1468       else
1469         {
1470 #ifdef HAVE_TARGET_64_LITTLE
1471           elfcpp::Ehdr<64, false> ehdr(p);
1472           return make_elf_sized_object<64, false>(name, input_file,
1473                                                   offset, ehdr);
1474 #else
1475           gold_error(_("%s: not configured to support "
1476                        "64-bit little-endian object"),
1477                      name.c_str());
1478           return NULL;
1479 #endif
1480         }
1481     }
1482 }
1483
1484 // Instantiate the templates we need.  We could use the configure
1485 // script to restrict this to only the ones for implemented targets.
1486
1487 #ifdef HAVE_TARGET_32_LITTLE
1488 template
1489 class Sized_relobj<32, false>;
1490 #endif
1491
1492 #ifdef HAVE_TARGET_32_BIG
1493 template
1494 class Sized_relobj<32, true>;
1495 #endif
1496
1497 #ifdef HAVE_TARGET_64_LITTLE
1498 template
1499 class Sized_relobj<64, false>;
1500 #endif
1501
1502 #ifdef HAVE_TARGET_64_BIG
1503 template
1504 class Sized_relobj<64, true>;
1505 #endif
1506
1507 #ifdef HAVE_TARGET_32_LITTLE
1508 template
1509 struct Relocate_info<32, false>;
1510 #endif
1511
1512 #ifdef HAVE_TARGET_32_BIG
1513 template
1514 struct Relocate_info<32, true>;
1515 #endif
1516
1517 #ifdef HAVE_TARGET_64_LITTLE
1518 template
1519 struct Relocate_info<64, false>;
1520 #endif
1521
1522 #ifdef HAVE_TARGET_64_BIG
1523 template
1524 struct Relocate_info<64, true>;
1525 #endif
1526
1527 } // End namespace gold.