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