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