From Cary Coutant: only check for a linkonce section if the SHF_GROUP
[external/binutils.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 #include "gold.h"
4
5 #include <cerrno>
6 #include <cstring>
7 #include <cstdarg>
8
9 #include "target-select.h"
10 #include "layout.h"
11 #include "output.h"
12 #include "symtab.h"
13 #include "object.h"
14 #include "dynobj.h"
15
16 namespace gold
17 {
18
19 // Class Object.
20
21 // Set the target based on fields in the ELF file header.
22
23 void
24 Object::set_target(int machine, int size, bool big_endian, int osabi,
25                    int abiversion)
26 {
27   Target* target = select_target(machine, size, big_endian, osabi, abiversion);
28   if (target == NULL)
29     {
30       fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
31               program_name, this->name().c_str(), machine);
32       gold_exit(false);
33     }
34   this->target_ = target;
35 }
36
37 // Report an error for the elfcpp::Elf_file interface.
38
39 void
40 Object::error(const char* format, ...)
41 {
42   va_list args;
43
44   fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
45   va_start(args, format);
46   vfprintf(stderr, format, args);
47   va_end(args);
48   putc('\n', stderr);
49
50   gold_exit(false);
51 }
52
53 // Return a view of the contents of a section.
54
55 const unsigned char*
56 Object::section_contents(unsigned int shndx, off_t* plen)
57 {
58   Location loc(this->do_section_contents(shndx));
59   *plen = loc.data_size;
60   return this->get_view(loc.file_offset, loc.data_size);
61 }
62
63 // Read the section data into SD.  This is code common to Sized_relobj
64 // and Sized_dynobj, so we put it into Object.
65
66 template<int size, bool big_endian>
67 void
68 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
69                           Read_symbols_data* sd)
70 {
71   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
72
73   // Read the section headers.
74   const off_t shoff = elf_file->shoff();
75   const unsigned int shnum = this->shnum();
76   sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size);
77
78   // Read the section names.
79   const unsigned char* pshdrs = sd->section_headers->data();
80   const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
81   typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
82
83   if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
84     {
85       fprintf(stderr,
86               _("%s: %s: section name section has wrong type: %u\n"),
87               program_name, this->name().c_str(),
88               static_cast<unsigned int>(shdrnames.get_sh_type()));
89       gold_exit(false);
90     }
91
92   sd->section_names_size = shdrnames.get_sh_size();
93   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
94                                              sd->section_names_size);
95 }
96
97 // If NAME is the name of a special .gnu.warning section, arrange for
98 // the warning to be issued.  SHNDX is the section index.  Return
99 // whether it is a warning section.
100
101 bool
102 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
103                                    Symbol_table* symtab)
104 {
105   const char warn_prefix[] = ".gnu.warning.";
106   const int warn_prefix_len = sizeof warn_prefix - 1;
107   if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
108     {
109       symtab->add_warning(name + warn_prefix_len, this, shndx);
110       return true;
111     }
112   return false;
113 }
114
115 // Class Sized_relobj.
116
117 template<int size, bool big_endian>
118 Sized_relobj<size, big_endian>::Sized_relobj(
119     const std::string& name,
120     Input_file* input_file,
121     off_t offset,
122     const elfcpp::Ehdr<size, big_endian>& ehdr)
123   : Relobj(name, input_file, offset),
124     elf_file_(this, ehdr),
125     symtab_shndx_(-1U),
126     local_symbol_count_(0),
127     output_local_symbol_count_(0),
128     symbols_(NULL),
129     local_symbol_offset_(0),
130     local_values_()
131 {
132 }
133
134 template<int size, bool big_endian>
135 Sized_relobj<size, big_endian>::~Sized_relobj()
136 {
137 }
138
139 // Set up an object file based on the file header.  This sets up the
140 // target and reads the section information.
141
142 template<int size, bool big_endian>
143 void
144 Sized_relobj<size, big_endian>::setup(
145     const elfcpp::Ehdr<size, big_endian>& ehdr)
146 {
147   this->set_target(ehdr.get_e_machine(), size, big_endian,
148                    ehdr.get_e_ident()[elfcpp::EI_OSABI],
149                    ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
150
151   const unsigned int shnum = this->elf_file_.shnum();
152   this->set_shnum(shnum);
153 }
154
155 // Find the SHT_SYMTAB section, given the section headers.  The ELF
156 // standard says that maybe in the future there can be more than one
157 // SHT_SYMTAB section.  Until somebody figures out how that could
158 // work, we assume there is only one.
159
160 template<int size, bool big_endian>
161 void
162 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
163 {
164   const unsigned int shnum = this->shnum();
165   this->symtab_shndx_ = 0;
166   if (shnum > 0)
167     {
168       // Look through the sections in reverse order, since gas tends
169       // to put the symbol table at the end.
170       const unsigned char* p = pshdrs + shnum * This::shdr_size;
171       unsigned int i = shnum;
172       while (i > 0)
173         {
174           --i;
175           p -= This::shdr_size;
176           typename This::Shdr shdr(p);
177           if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
178             {
179               this->symtab_shndx_ = i;
180               break;
181             }
182         }
183     }
184 }
185
186 // Read the sections and symbols from an object file.
187
188 template<int size, bool big_endian>
189 void
190 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
191 {
192   this->read_section_data(&this->elf_file_, sd);
193
194   const unsigned char* const pshdrs = sd->section_headers->data();
195
196   this->find_symtab(pshdrs);
197
198   if (this->symtab_shndx_ == 0)
199     {
200       // No symbol table.  Weird but legal.
201       sd->symbols = NULL;
202       sd->symbols_size = 0;
203       sd->symbol_names = NULL;
204       sd->symbol_names_size = 0;
205       return;
206     }
207
208   // Get the symbol table section header.
209   typename This::Shdr symtabshdr(pshdrs
210                                  + this->symtab_shndx_ * This::shdr_size);
211   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
212
213   // We only need the external symbols.
214   const int sym_size = This::sym_size;
215   const unsigned int loccount = symtabshdr.get_sh_info();
216   this->local_symbol_count_ = loccount;
217   off_t locsize = loccount * sym_size;
218   off_t extoff = symtabshdr.get_sh_offset() + locsize;
219   off_t extsize = symtabshdr.get_sh_size() - locsize;
220
221   // Read the symbol table.
222   File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
223
224   // Read the section header for the symbol names.
225   unsigned int strtab_shndx = symtabshdr.get_sh_link();
226   if (strtab_shndx >= this->shnum())
227     {
228       fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
229               program_name, this->name().c_str(), strtab_shndx);
230       gold_exit(false);
231     }
232   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
233   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
234     {
235       fprintf(stderr,
236               _("%s: %s: symbol table name section has wrong type: %u\n"),
237               program_name, this->name().c_str(),
238               static_cast<unsigned int>(strtabshdr.get_sh_type()));
239       gold_exit(false);
240     }
241
242   // Read the symbol names.
243   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
244                                                strtabshdr.get_sh_size());
245
246   sd->symbols = fvsymtab;
247   sd->symbols_size = extsize;
248   sd->symbol_names = fvstrtab;
249   sd->symbol_names_size = strtabshdr.get_sh_size();
250 }
251
252 // Return whether to include a section group in the link.  LAYOUT is
253 // used to keep track of which section groups we have already seen.
254 // INDEX is the index of the section group and SHDR is the section
255 // header.  If we do not want to include this group, we set bits in
256 // OMIT for each section which should be discarded.
257
258 template<int size, bool big_endian>
259 bool
260 Sized_relobj<size, big_endian>::include_section_group(
261     Layout* layout,
262     unsigned int index,
263     const elfcpp::Shdr<size, big_endian>& shdr,
264     std::vector<bool>* omit)
265 {
266   // Read the section contents.
267   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
268                                              shdr.get_sh_size());
269   const elfcpp::Elf_Word* pword =
270     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
271
272   // The first word contains flags.  We only care about COMDAT section
273   // groups.  Other section groups are always included in the link
274   // just like ordinary sections.
275   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
276   if ((flags & elfcpp::GRP_COMDAT) == 0)
277     return true;
278
279   // Look up the group signature, which is the name of a symbol.  This
280   // is a lot of effort to go to to read a string.  Why didn't they
281   // just use the name of the SHT_GROUP section as the group
282   // signature?
283
284   // Get the appropriate symbol table header (this will normally be
285   // the single SHT_SYMTAB section, but in principle it need not be).
286   const unsigned int link = shdr.get_sh_link();
287   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
288
289   // Read the symbol table entry.
290   if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
291     {
292       fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
293               program_name, this->name().c_str(), index, shdr.get_sh_info());
294       gold_exit(false);
295     }
296   off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
297   const unsigned char* psym = this->get_view(symoff, This::sym_size);
298   elfcpp::Sym<size, big_endian> sym(psym);
299
300   // Read the symbol table names.
301   off_t symnamelen;
302   const unsigned char* psymnamesu;
303   psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen);
304   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
305
306   // Get the section group signature.
307   if (sym.get_st_name() >= symnamelen)
308     {
309       fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
310               program_name, this->name().c_str(), shdr.get_sh_info(),
311               sym.get_st_name());
312       gold_exit(false);
313     }
314
315   const char* signature = psymnames + sym.get_st_name();
316
317   // It seems that some versions of gas will create a section group
318   // associated with a section symbol, and then fail to give a name to
319   // the section symbol.  In such a case, use the name of the section.
320   // FIXME.
321   std::string secname;
322   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
323     {
324       secname = this->section_name(sym.get_st_shndx());
325       signature = secname.c_str();
326     }
327
328   // Record this section group, and see whether we've already seen one
329   // with the same signature.
330   if (layout->add_comdat(signature, true))
331     return true;
332
333   // This is a duplicate.  We want to discard the sections in this
334   // group.
335   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
336   for (size_t i = 1; i < count; ++i)
337     {
338       elfcpp::Elf_Word secnum =
339         elfcpp::Swap<32, big_endian>::readval(pword + i);
340       if (secnum >= this->shnum())
341         {
342           fprintf(stderr,
343                   _("%s: %s: section %u in section group %u out of range"),
344                   program_name, this->name().c_str(), secnum,
345                   index);
346           gold_exit(false);
347         }
348       (*omit)[secnum] = true;
349     }
350
351   return false;
352 }
353
354 // Whether to include a linkonce section in the link.  NAME is the
355 // name of the section and SHDR is the section header.
356
357 // Linkonce sections are a GNU extension implemented in the original
358 // GNU linker before section groups were defined.  The semantics are
359 // that we only include one linkonce section with a given name.  The
360 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
361 // where T is the type of section and SYMNAME is the name of a symbol.
362 // In an attempt to make linkonce sections interact well with section
363 // groups, we try to identify SYMNAME and use it like a section group
364 // signature.  We want to block section groups with that signature,
365 // but not other linkonce sections with that signature.  We also use
366 // the full name of the linkonce section as a normal section group
367 // signature.
368
369 template<int size, bool big_endian>
370 bool
371 Sized_relobj<size, big_endian>::include_linkonce_section(
372     Layout* layout,
373     const char* name,
374     const elfcpp::Shdr<size, big_endian>&)
375 {
376   const char* symname = strrchr(name, '.') + 1;
377   bool include1 = layout->add_comdat(symname, false);
378   bool include2 = layout->add_comdat(name, true);
379   return include1 && include2;
380 }
381
382 // Lay out the input sections.  We walk through the sections and check
383 // whether they should be included in the link.  If they should, we
384 // pass them to the Layout object, which will return an output section
385 // and an offset.
386
387 template<int size, bool big_endian>
388 void
389 Sized_relobj<size, big_endian>::do_layout(const General_options& options,
390                                           Symbol_table* symtab,
391                                           Layout* layout,
392                                           Read_symbols_data* sd)
393 {
394   const unsigned int shnum = this->shnum();
395   if (shnum == 0)
396     return;
397
398   // Get the section headers.
399   const unsigned char* pshdrs = sd->section_headers->data();
400
401   // Get the section names.
402   const unsigned char* pnamesu = sd->section_names->data();
403   const char* pnames = reinterpret_cast<const char*>(pnamesu);
404
405   std::vector<Map_to_output>& map_sections(this->map_to_output());
406   map_sections.resize(shnum);
407
408   // Keep track of which sections to omit.
409   std::vector<bool> omit(shnum, false);
410
411   // Skip the first, dummy, section.
412   pshdrs += This::shdr_size;
413   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
414     {
415       typename This::Shdr shdr(pshdrs);
416
417       if (shdr.get_sh_name() >= sd->section_names_size)
418         {
419           fprintf(stderr,
420                   _("%s: %s: bad section name offset for section %u: %lu\n"),
421                   program_name, this->name().c_str(), i,
422                   static_cast<unsigned long>(shdr.get_sh_name()));
423           gold_exit(false);
424         }
425
426       const char* name = pnames + shdr.get_sh_name();
427
428       if (this->handle_gnu_warning_section(name, i, symtab))
429         {
430           if (!options.is_relocatable())
431             omit[i] = true;
432         }
433
434       bool discard = omit[i];
435       if (!discard)
436         {
437           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
438             {
439               if (!this->include_section_group(layout, i, shdr, &omit))
440                 discard = true;
441             }
442           else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
443                    && Layout::is_linkonce(name))
444             {
445               if (!this->include_linkonce_section(layout, name, shdr))
446                 discard = true;
447             }
448         }
449
450       if (discard)
451         {
452           // Do not include this section in the link.
453           map_sections[i].output_section = NULL;
454           continue;
455         }
456
457       off_t offset;
458       Output_section* os = layout->layout(this, i, name, shdr, &offset);
459
460       map_sections[i].output_section = os;
461       map_sections[i].offset = offset;
462     }
463
464   delete sd->section_headers;
465   sd->section_headers = NULL;
466   delete sd->section_names;
467   sd->section_names = NULL;
468 }
469
470 // Add the symbols to the symbol table.
471
472 template<int size, bool big_endian>
473 void
474 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
475                                                Read_symbols_data* sd)
476 {
477   if (sd->symbols == NULL)
478     {
479       gold_assert(sd->symbol_names == NULL);
480       return;
481     }
482
483   const int sym_size = This::sym_size;
484   size_t symcount = sd->symbols_size / sym_size;
485   if (symcount * sym_size != sd->symbols_size)
486     {
487       fprintf(stderr,
488               _("%s: %s: size of symbols is not multiple of symbol size\n"),
489               program_name, this->name().c_str());
490       gold_exit(false);
491     }
492
493   this->symbols_ = new Symbol*[symcount];
494
495   const char* sym_names =
496     reinterpret_cast<const char*>(sd->symbol_names->data());
497   symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
498                           sd->symbol_names_size, this->symbols_);
499
500   delete sd->symbols;
501   sd->symbols = NULL;
502   delete sd->symbol_names;
503   sd->symbol_names = NULL;
504 }
505
506 // Finalize the local symbols.  Here we record the file offset at
507 // which they should be output, we add their names to *POOL, and we
508 // add their values to THIS->LOCAL_VALUES_.  Return the symbol index.
509 // This function is always called from the main thread.  The actual
510 // output of the local symbols will occur in a separate task.
511
512 template<int size, bool big_endian>
513 unsigned int
514 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
515                                                           off_t off,
516                                                           Stringpool* pool)
517 {
518   gold_assert(this->symtab_shndx_ != -1U);
519   if (this->symtab_shndx_ == 0)
520     {
521       // This object has no symbols.  Weird but legal.
522       return index;
523     }
524
525   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
526
527   this->local_symbol_offset_ = off;
528
529   // Read the symbol table section header.
530   const unsigned int symtab_shndx = this->symtab_shndx_;
531   typename This::Shdr symtabshdr(this,
532                                  this->elf_file_.section_header(symtab_shndx));
533   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
534
535   // Read the local symbols.
536   const int sym_size = This::sym_size;
537   const unsigned int loccount = this->local_symbol_count_;
538   gold_assert(loccount == symtabshdr.get_sh_info());
539   off_t locsize = loccount * sym_size;
540   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
541                                               locsize);
542
543   this->local_values_.resize(loccount);
544
545   // Read the symbol names.
546   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
547   off_t strtab_size;
548   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
549                                                         &strtab_size);
550   const char* pnames = reinterpret_cast<const char*>(pnamesu);
551
552   // Loop over the local symbols.
553
554   const std::vector<Map_to_output>& mo(this->map_to_output());
555   unsigned int shnum = this->shnum();
556   unsigned int count = 0;
557   // Skip the first, dummy, symbol.
558   psyms += sym_size;
559   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
560     {
561       elfcpp::Sym<size, big_endian> sym(psyms);
562
563       Symbol_value<size>& lv(this->local_values_[i]);
564
565       unsigned int shndx = sym.get_st_shndx();
566       lv.set_input_shndx(shndx);
567
568       if (shndx >= elfcpp::SHN_LORESERVE)
569         {
570           if (shndx == elfcpp::SHN_ABS)
571             lv.set_output_value(sym.get_st_value());
572           else
573             {
574               // FIXME: Handle SHN_XINDEX.
575               fprintf(stderr,
576                       _("%s: %s: unknown section index %u "
577                         "for local symbol %u\n"),
578                       program_name, this->name().c_str(), shndx, i);
579               gold_exit(false);
580             }
581         }
582       else
583         {
584           if (shndx >= shnum)
585             {
586               fprintf(stderr,
587                       _("%s: %s: local symbol %u section index %u "
588                         "out of range\n"),
589                       program_name, this->name().c_str(), i, shndx);
590               gold_exit(false);
591             }
592
593           Output_section* os = mo[shndx].output_section;
594
595           if (os == NULL)
596             {
597               lv.set_output_value(0);
598               lv.set_no_output_symtab_entry();
599               continue;
600             }
601
602           if (mo[shndx].offset == -1)
603             lv.set_input_value(sym.get_st_value());
604           else
605             lv.set_output_value(mo[shndx].output_section->address()
606                                 + mo[shndx].offset
607                                 + sym.get_st_value());
608         }
609
610       // Decide whether this symbol should go into the output file.
611
612       if (sym.get_st_type() == elfcpp::STT_SECTION)
613         {
614           lv.set_no_output_symtab_entry();
615           continue;
616         }
617
618       if (sym.get_st_name() >= strtab_size)
619         {
620           fprintf(stderr,
621                   _("%s: %s: local symbol %u section name "
622                     "out of range: %u >= %u\n"),
623                   program_name, this->name().c_str(),
624                   i, sym.get_st_name(),
625                   static_cast<unsigned int>(strtab_size));
626           gold_exit(false);
627         }
628
629       const char* name = pnames + sym.get_st_name();
630       pool->add(name, NULL);
631       lv.set_output_symtab_index(index);
632       ++index;
633       ++count;
634     }
635
636   this->output_local_symbol_count_ = count;
637
638   return index;
639 }
640
641 // Return the value of a local symbol defined in input section SHNDX,
642 // with value VALUE, adding addend ADDEND.  This handles SHF_MERGE
643 // sections.
644 template<int size, bool big_endian>
645 typename elfcpp::Elf_types<size>::Elf_Addr
646 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
647                                             Address value,
648                                             Address addend) const
649 {
650   const std::vector<Map_to_output>& mo(this->map_to_output());
651   Output_section* os = mo[shndx].output_section;
652   if (os == NULL)
653     return addend;
654   gold_assert(mo[shndx].offset == -1);
655   return os->output_address(this, shndx, value + addend);
656 }
657
658 // Write out the local symbols.
659
660 template<int size, bool big_endian>
661 void
662 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
663                                                     const Stringpool* sympool)
664 {
665   gold_assert(this->symtab_shndx_ != -1U);
666   if (this->symtab_shndx_ == 0)
667     {
668       // This object has no symbols.  Weird but legal.
669       return;
670     }
671
672   // Read the symbol table section header.
673   const unsigned int symtab_shndx = this->symtab_shndx_;
674   typename This::Shdr symtabshdr(this,
675                                  this->elf_file_.section_header(symtab_shndx));
676   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
677   const unsigned int loccount = this->local_symbol_count_;
678   gold_assert(loccount == symtabshdr.get_sh_info());
679
680   // Read the local symbols.
681   const int sym_size = This::sym_size;
682   off_t locsize = loccount * sym_size;
683   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
684                                               locsize);
685
686   // Read the symbol names.
687   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
688   off_t strtab_size;
689   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
690                                                         &strtab_size);
691   const char* pnames = reinterpret_cast<const char*>(pnamesu);
692
693   // Get a view into the output file.
694   off_t output_size = this->output_local_symbol_count_ * sym_size;
695   unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
696                                              output_size);
697
698   const std::vector<Map_to_output>& mo(this->map_to_output());
699
700   gold_assert(this->local_values_.size() == loccount);
701
702   unsigned char* ov = oview;
703   psyms += sym_size;
704   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
705     {
706       elfcpp::Sym<size, big_endian> isym(psyms);
707
708       if (!this->local_values_[i].needs_output_symtab_entry())
709         continue;
710
711       unsigned int st_shndx = isym.get_st_shndx();
712       if (st_shndx < elfcpp::SHN_LORESERVE)
713         {
714           gold_assert(st_shndx < mo.size());
715           if (mo[st_shndx].output_section == NULL)
716             continue;
717           st_shndx = mo[st_shndx].output_section->out_shndx();
718         }
719
720       elfcpp::Sym_write<size, big_endian> osym(ov);
721
722       gold_assert(isym.get_st_name() < strtab_size);
723       const char* name = pnames + isym.get_st_name();
724       osym.put_st_name(sympool->get_offset(name));
725       osym.put_st_value(this->local_values_[i].value(this, 0));
726       osym.put_st_size(isym.get_st_size());
727       osym.put_st_info(isym.get_st_info());
728       osym.put_st_other(isym.get_st_other());
729       osym.put_st_shndx(st_shndx);
730
731       ov += sym_size;
732     }
733
734   gold_assert(ov - oview == output_size);
735
736   of->write_output_view(this->local_symbol_offset_, output_size, oview);
737 }
738
739 // Input_objects methods.
740
741 // Add a regular relocatable object to the list.  Return false if this
742 // object should be ignored.
743
744 bool
745 Input_objects::add_object(Object* obj)
746 {
747   if (!obj->is_dynamic())
748     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
749   else
750     {
751       // See if this is a duplicate SONAME.
752       Dynobj* dynobj = static_cast<Dynobj*>(obj);
753
754       std::pair<Unordered_set<std::string>::iterator, bool> ins =
755         this->sonames_.insert(dynobj->soname());
756       if (!ins.second)
757         {
758           // We have already seen a dynamic object with this soname.
759           return false;
760         }
761
762       this->dynobj_list_.push_back(dynobj);
763     }
764
765   Target* target = obj->target();
766   if (this->target_ == NULL)
767     this->target_ = target;
768   else if (this->target_ != target)
769     {
770       fprintf(stderr, "%s: %s: incompatible target\n",
771               program_name, obj->name().c_str());
772       gold_exit(false);
773     }
774
775   return true;
776 }
777
778 // Relocate_info methods.
779
780 // Return a string describing the location of a relocation.  This is
781 // only used in error messages.
782
783 template<int size, bool big_endian>
784 std::string
785 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
786 {
787   std::string ret(this->object->name());
788   ret += ": reloc ";
789   char buf[100];
790   snprintf(buf, sizeof buf, "%zu", relnum);
791   ret += buf;
792   ret += " in reloc section ";
793   snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
794   ret += buf;
795   ret += " (" + this->object->section_name(this->reloc_shndx);
796   ret += ") for section ";
797   snprintf(buf, sizeof buf, "%u", this->data_shndx);
798   ret += buf;
799   ret += " (" + this->object->section_name(this->data_shndx) + ")";
800   return ret;
801 }
802
803 } // End namespace gold.
804
805 namespace
806 {
807
808 using namespace gold;
809
810 // Read an ELF file with the header and return the appropriate
811 // instance of Object.
812
813 template<int size, bool big_endian>
814 Object*
815 make_elf_sized_object(const std::string& name, Input_file* input_file,
816                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
817 {
818   int et = ehdr.get_e_type();
819   if (et == elfcpp::ET_REL)
820     {
821       Sized_relobj<size, big_endian>* obj =
822         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
823       obj->setup(ehdr);
824       return obj;
825     }
826   else if (et == elfcpp::ET_DYN)
827     {
828       Sized_dynobj<size, big_endian>* obj =
829         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
830       obj->setup(ehdr);
831       return obj;
832     }
833   else
834     {
835       fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
836               program_name, name.c_str(), et);
837       gold_exit(false);
838     }
839 }
840
841 } // End anonymous namespace.
842
843 namespace gold
844 {
845
846 // Read an ELF file and return the appropriate instance of Object.
847
848 Object*
849 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
850                 const unsigned char* p, off_t bytes)
851 {
852   if (bytes < elfcpp::EI_NIDENT)
853     {
854       fprintf(stderr, _("%s: %s: ELF file too short\n"),
855               program_name, name.c_str());
856       gold_exit(false);
857     }
858
859   int v = p[elfcpp::EI_VERSION];
860   if (v != elfcpp::EV_CURRENT)
861     {
862       if (v == elfcpp::EV_NONE)
863         fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
864                 program_name, name.c_str());
865       else
866         fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
867                 program_name, name.c_str(), v);
868       gold_exit(false);
869     }
870
871   int c = p[elfcpp::EI_CLASS];
872   if (c == elfcpp::ELFCLASSNONE)
873     {
874       fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
875               program_name, name.c_str());
876       gold_exit(false);
877     }
878   else if (c != elfcpp::ELFCLASS32
879            && c != elfcpp::ELFCLASS64)
880     {
881       fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
882               program_name, name.c_str(), c);
883       gold_exit(false);
884     }
885
886   int d = p[elfcpp::EI_DATA];
887   if (d == elfcpp::ELFDATANONE)
888     {
889       fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
890               program_name, name.c_str());
891       gold_exit(false);
892     }
893   else if (d != elfcpp::ELFDATA2LSB
894            && d != elfcpp::ELFDATA2MSB)
895     {
896       fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
897               program_name, name.c_str(), d);
898       gold_exit(false);
899     }
900
901   bool big_endian = d == elfcpp::ELFDATA2MSB;
902
903   if (c == elfcpp::ELFCLASS32)
904     {
905       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
906         {
907           fprintf(stderr, _("%s: %s: ELF file too short\n"),
908                   program_name, name.c_str());
909           gold_exit(false);
910         }
911       if (big_endian)
912         {
913 #ifdef HAVE_TARGET_32_BIG
914           elfcpp::Ehdr<32, true> ehdr(p);
915           return make_elf_sized_object<32, true>(name, input_file,
916                                                  offset, ehdr);
917 #else
918           fprintf(stderr,
919                   _("%s: %s: not configured to support 32-bit big-endian object\n"),
920                   program_name, name.c_str());
921           gold_exit(false);
922 #endif
923         }
924       else
925         {
926 #ifdef HAVE_TARGET_32_LITTLE
927           elfcpp::Ehdr<32, false> ehdr(p);
928           return make_elf_sized_object<32, false>(name, input_file,
929                                                   offset, ehdr);
930 #else
931           fprintf(stderr,
932                   _("%s: %s: not configured to support 32-bit little-endian object\n"),
933                   program_name, name.c_str());
934           gold_exit(false);
935 #endif
936         }
937     }
938   else
939     {
940       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
941         {
942           fprintf(stderr, _("%s: %s: ELF file too short\n"),
943                   program_name, name.c_str());
944           gold_exit(false);
945         }
946       if (big_endian)
947         {
948 #ifdef HAVE_TARGET_64_BIG
949           elfcpp::Ehdr<64, true> ehdr(p);
950           return make_elf_sized_object<64, true>(name, input_file,
951                                                  offset, ehdr);
952 #else
953           fprintf(stderr,
954                   _("%s: %s: not configured to support 64-bit big-endian object\n"),
955                   program_name, name.c_str());
956           gold_exit(false);
957 #endif
958         }
959       else
960         {
961 #ifdef HAVE_TARGET_64_LITTLE
962           elfcpp::Ehdr<64, false> ehdr(p);
963           return make_elf_sized_object<64, false>(name, input_file,
964                                                   offset, ehdr);
965 #else
966           fprintf(stderr,
967                   _("%s: %s: not configured to support 64-bit little-endian object\n"),
968                   program_name, name.c_str());
969           gold_exit(false);
970 #endif
971         }
972     }
973 }
974
975 // Instantiate the templates we need.  We could use the configure
976 // script to restrict this to only the ones for implemented targets.
977
978 #ifdef HAVE_TARGET_32_LITTLE
979 template
980 class Sized_relobj<32, false>;
981 #endif
982
983 #ifdef HAVE_TARGET_32_BIG
984 template
985 class Sized_relobj<32, true>;
986 #endif
987
988 #ifdef HAVE_TARGET_64_LITTLE
989 template
990 class Sized_relobj<64, false>;
991 #endif
992
993 #ifdef HAVE_TARGET_64_BIG
994 template
995 class Sized_relobj<64, true>;
996 #endif
997
998 #ifdef HAVE_TARGET_32_LITTLE
999 template
1000 struct Relocate_info<32, false>;
1001 #endif
1002
1003 #ifdef HAVE_TARGET_32_BIG
1004 template
1005 struct Relocate_info<32, true>;
1006 #endif
1007
1008 #ifdef HAVE_TARGET_64_LITTLE
1009 template
1010 struct Relocate_info<64, false>;
1011 #endif
1012
1013 #ifdef HAVE_TARGET_64_BIG
1014 template
1015 struct Relocate_info<64, true>;
1016 #endif
1017
1018 } // End namespace gold.