Add support for SHF_MERGE sections.
[platform/upstream/binutils.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 #include "gold.h"
4
5 #include <cerrno>
6 #include <cstring>
7 #include <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 (Layout::is_linkonce(name))
443             {
444               if (!this->include_linkonce_section(layout, name, shdr))
445                 discard = true;
446             }
447         }
448
449       if (discard)
450         {
451           // Do not include this section in the link.
452           map_sections[i].output_section = NULL;
453           continue;
454         }
455
456       off_t offset;
457       Output_section* os = layout->layout(this, i, name, shdr, &offset);
458
459       map_sections[i].output_section = os;
460       map_sections[i].offset = offset;
461     }
462
463   delete sd->section_headers;
464   sd->section_headers = NULL;
465   delete sd->section_names;
466   sd->section_names = NULL;
467 }
468
469 // Add the symbols to the symbol table.
470
471 template<int size, bool big_endian>
472 void
473 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
474                                                Read_symbols_data* sd)
475 {
476   if (sd->symbols == NULL)
477     {
478       gold_assert(sd->symbol_names == NULL);
479       return;
480     }
481
482   const int sym_size = This::sym_size;
483   size_t symcount = sd->symbols_size / sym_size;
484   if (symcount * sym_size != sd->symbols_size)
485     {
486       fprintf(stderr,
487               _("%s: %s: size of symbols is not multiple of symbol size\n"),
488               program_name, this->name().c_str());
489       gold_exit(false);
490     }
491
492   this->symbols_ = new Symbol*[symcount];
493
494   const char* sym_names =
495     reinterpret_cast<const char*>(sd->symbol_names->data());
496   symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names, 
497                           sd->symbol_names_size, this->symbols_);
498
499   delete sd->symbols;
500   sd->symbols = NULL;
501   delete sd->symbol_names;
502   sd->symbol_names = NULL;
503 }
504
505 // Finalize the local symbols.  Here we record the file offset at
506 // which they should be output, we add their names to *POOL, and we
507 // add their values to THIS->LOCAL_VALUES_.  Return the symbol index.
508 // This function is always called from the main thread.  The actual
509 // output of the local symbols will occur in a separate task.
510
511 template<int size, bool big_endian>
512 unsigned int
513 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
514                                                           off_t off,
515                                                           Stringpool* pool)
516 {
517   gold_assert(this->symtab_shndx_ != -1U);
518   if (this->symtab_shndx_ == 0)
519     {
520       // This object has no symbols.  Weird but legal.
521       return index;
522     }
523
524   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
525
526   this->local_symbol_offset_ = off;
527
528   // Read the symbol table section header.
529   const unsigned int symtab_shndx = this->symtab_shndx_;
530   typename This::Shdr symtabshdr(this,
531                                  this->elf_file_.section_header(symtab_shndx));
532   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
533
534   // Read the local symbols.
535   const int sym_size = This::sym_size;
536   const unsigned int loccount = this->local_symbol_count_;
537   gold_assert(loccount == symtabshdr.get_sh_info());
538   off_t locsize = loccount * sym_size;
539   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
540                                               locsize);
541
542   this->local_values_.resize(loccount);
543
544   // Read the symbol names.
545   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
546   off_t strtab_size;
547   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
548                                                         &strtab_size);
549   const char* pnames = reinterpret_cast<const char*>(pnamesu);
550
551   // Loop over the local symbols.
552
553   const std::vector<Map_to_output>& mo(this->map_to_output());
554   unsigned int shnum = this->shnum();
555   unsigned int count = 0;
556   // Skip the first, dummy, symbol.
557   psyms += sym_size;
558   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
559     {
560       elfcpp::Sym<size, big_endian> sym(psyms);
561
562       Symbol_value<size>& lv(this->local_values_[i]);
563
564       unsigned int shndx = sym.get_st_shndx();
565       lv.set_input_shndx(shndx);
566
567       if (shndx >= elfcpp::SHN_LORESERVE)
568         {
569           if (shndx == elfcpp::SHN_ABS)
570             lv.set_output_value(sym.get_st_value());
571           else
572             {
573               // FIXME: Handle SHN_XINDEX.
574               fprintf(stderr,
575                       _("%s: %s: unknown section index %u "
576                         "for local symbol %u\n"),
577                       program_name, this->name().c_str(), shndx, i);
578               gold_exit(false);
579             }
580         }
581       else
582         {
583           if (shndx >= shnum)
584             {
585               fprintf(stderr,
586                       _("%s: %s: local symbol %u section index %u "
587                         "out of range\n"),
588                       program_name, this->name().c_str(), i, shndx);
589               gold_exit(false);
590             }
591
592           Output_section* os = mo[shndx].output_section;
593
594           if (os == NULL)
595             {
596               lv.set_output_value(0);
597               lv.set_no_output_symtab_entry();
598               continue;
599             }
600
601           if (mo[shndx].offset == -1)
602             lv.set_input_value(sym.get_st_value());
603           else
604             lv.set_output_value(mo[shndx].output_section->address()
605                                 + mo[shndx].offset
606                                 + sym.get_st_value());
607         }
608
609       // Decide whether this symbol should go into the output file.
610
611       if (sym.get_st_type() == elfcpp::STT_SECTION)
612         {
613           lv.set_no_output_symtab_entry();
614           continue;
615         }
616
617       if (sym.get_st_name() >= strtab_size)
618         {
619           fprintf(stderr,
620                   _("%s: %s: local symbol %u section name "
621                     "out of range: %u >= %u\n"),
622                   program_name, this->name().c_str(),
623                   i, sym.get_st_name(),
624                   static_cast<unsigned int>(strtab_size));
625           gold_exit(false);
626         }
627
628       const char* name = pnames + sym.get_st_name();
629       pool->add(name, NULL);
630       lv.set_output_symtab_index(index);
631       ++index;
632       ++count;
633     }
634
635   this->output_local_symbol_count_ = count;
636
637   return index;
638 }
639
640 // Return the value of a local symbol defined in input section SHNDX,
641 // with value VALUE, adding addend ADDEND.  This handles SHF_MERGE
642 // sections.
643 template<int size, bool big_endian>
644 typename elfcpp::Elf_types<size>::Elf_Addr
645 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
646                                             Address value,
647                                             Address addend) const
648 {
649   const std::vector<Map_to_output>& mo(this->map_to_output());
650   Output_section* os = mo[shndx].output_section;
651   if (os == NULL)
652     return addend;
653   gold_assert(mo[shndx].offset == -1);
654   return os->output_address(this, shndx, value + addend);
655 }
656
657 // Write out the local symbols.
658
659 template<int size, bool big_endian>
660 void
661 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
662                                                     const Stringpool* sympool)
663 {
664   gold_assert(this->symtab_shndx_ != -1U);
665   if (this->symtab_shndx_ == 0)
666     {
667       // This object has no symbols.  Weird but legal.
668       return;
669     }
670
671   // Read the symbol table section header.
672   const unsigned int symtab_shndx = this->symtab_shndx_;
673   typename This::Shdr symtabshdr(this,
674                                  this->elf_file_.section_header(symtab_shndx));
675   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
676   const unsigned int loccount = this->local_symbol_count_;
677   gold_assert(loccount == symtabshdr.get_sh_info());
678
679   // Read the local symbols.
680   const int sym_size = This::sym_size;
681   off_t locsize = loccount * sym_size;
682   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
683                                               locsize);
684
685   // Read the symbol names.
686   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
687   off_t strtab_size;
688   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
689                                                         &strtab_size);
690   const char* pnames = reinterpret_cast<const char*>(pnamesu);
691
692   // Get a view into the output file.
693   off_t output_size = this->output_local_symbol_count_ * sym_size;
694   unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
695                                              output_size);
696
697   const std::vector<Map_to_output>& mo(this->map_to_output());
698
699   gold_assert(this->local_values_.size() == loccount);
700
701   unsigned char* ov = oview;
702   psyms += sym_size;
703   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
704     {
705       elfcpp::Sym<size, big_endian> isym(psyms);
706
707       if (!this->local_values_[i].needs_output_symtab_entry())
708         continue;
709
710       unsigned int st_shndx = isym.get_st_shndx();
711       if (st_shndx < elfcpp::SHN_LORESERVE)
712         {
713           gold_assert(st_shndx < mo.size());
714           if (mo[st_shndx].output_section == NULL)
715             continue;
716           st_shndx = mo[st_shndx].output_section->out_shndx();
717         }
718
719       elfcpp::Sym_write<size, big_endian> osym(ov);
720
721       gold_assert(isym.get_st_name() < strtab_size);
722       const char* name = pnames + isym.get_st_name();
723       osym.put_st_name(sympool->get_offset(name));
724       osym.put_st_value(this->local_values_[i].value(this, 0));
725       osym.put_st_size(isym.get_st_size());
726       osym.put_st_info(isym.get_st_info());
727       osym.put_st_other(isym.get_st_other());
728       osym.put_st_shndx(st_shndx);
729
730       ov += sym_size;
731     }
732
733   gold_assert(ov - oview == output_size);
734
735   of->write_output_view(this->local_symbol_offset_, output_size, oview);
736 }
737
738 // Input_objects methods.
739
740 // Add a regular relocatable object to the list.  Return false if this
741 // object should be ignored.
742
743 bool
744 Input_objects::add_object(Object* obj)
745 {
746   if (!obj->is_dynamic())
747     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
748   else
749     {
750       // See if this is a duplicate SONAME.
751       Dynobj* dynobj = static_cast<Dynobj*>(obj);
752
753       std::pair<Unordered_set<std::string>::iterator, bool> ins =
754         this->sonames_.insert(dynobj->soname());
755       if (!ins.second)
756         {
757           // We have already seen a dynamic object with this soname.
758           return false;
759         }
760
761       this->dynobj_list_.push_back(dynobj);
762     }
763
764   Target* target = obj->target();
765   if (this->target_ == NULL)
766     this->target_ = target;
767   else if (this->target_ != target)
768     {
769       fprintf(stderr, "%s: %s: incompatible target\n",
770               program_name, obj->name().c_str());
771       gold_exit(false);
772     }
773
774   return true;
775 }
776
777 // Relocate_info methods.
778
779 // Return a string describing the location of a relocation.  This is
780 // only used in error messages.
781
782 template<int size, bool big_endian>
783 std::string
784 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
785 {
786   std::string ret(this->object->name());
787   ret += ": reloc ";
788   char buf[100];
789   snprintf(buf, sizeof buf, "%zu", relnum);
790   ret += buf;
791   ret += " in reloc section ";
792   snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
793   ret += buf;
794   ret += " (" + this->object->section_name(this->reloc_shndx);
795   ret += ") for section ";
796   snprintf(buf, sizeof buf, "%u", this->data_shndx);
797   ret += buf;
798   ret += " (" + this->object->section_name(this->data_shndx) + ")";
799   return ret;
800 }
801
802 } // End namespace gold.
803
804 namespace
805 {
806
807 using namespace gold;
808
809 // Read an ELF file with the header and return the appropriate
810 // instance of Object.
811
812 template<int size, bool big_endian>
813 Object*
814 make_elf_sized_object(const std::string& name, Input_file* input_file,
815                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
816 {
817   int et = ehdr.get_e_type();
818   if (et == elfcpp::ET_REL)
819     {
820       Sized_relobj<size, big_endian>* obj =
821         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
822       obj->setup(ehdr);
823       return obj;
824     }
825   else if (et == elfcpp::ET_DYN)
826     {
827       Sized_dynobj<size, big_endian>* obj =
828         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
829       obj->setup(ehdr);
830       return obj;
831     }
832   else
833     {
834       fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
835               program_name, name.c_str(), et);
836       gold_exit(false);
837     }
838 }
839
840 } // End anonymous namespace.
841
842 namespace gold
843 {
844
845 // Read an ELF file and return the appropriate instance of Object.
846
847 Object*
848 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
849                 const unsigned char* p, off_t bytes)
850 {
851   if (bytes < elfcpp::EI_NIDENT)
852     {
853       fprintf(stderr, _("%s: %s: ELF file too short\n"),
854               program_name, name.c_str());
855       gold_exit(false);
856     }
857
858   int v = p[elfcpp::EI_VERSION];
859   if (v != elfcpp::EV_CURRENT)
860     {
861       if (v == elfcpp::EV_NONE)
862         fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
863                 program_name, name.c_str());
864       else
865         fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
866                 program_name, name.c_str(), v);
867       gold_exit(false);
868     }
869
870   int c = p[elfcpp::EI_CLASS];
871   if (c == elfcpp::ELFCLASSNONE)
872     {
873       fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
874               program_name, name.c_str());
875       gold_exit(false);
876     }
877   else if (c != elfcpp::ELFCLASS32
878            && c != elfcpp::ELFCLASS64)
879     {
880       fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
881               program_name, name.c_str(), c);
882       gold_exit(false);
883     }
884
885   int d = p[elfcpp::EI_DATA];
886   if (d == elfcpp::ELFDATANONE)
887     {
888       fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
889               program_name, name.c_str());
890       gold_exit(false);
891     }
892   else if (d != elfcpp::ELFDATA2LSB
893            && d != elfcpp::ELFDATA2MSB)
894     {
895       fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
896               program_name, name.c_str(), d);
897       gold_exit(false);
898     }
899
900   bool big_endian = d == elfcpp::ELFDATA2MSB;
901
902   if (c == elfcpp::ELFCLASS32)
903     {
904       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
905         {
906           fprintf(stderr, _("%s: %s: ELF file too short\n"),
907                   program_name, name.c_str());
908           gold_exit(false);
909         }
910       if (big_endian)
911         {
912           elfcpp::Ehdr<32, true> ehdr(p);
913           return make_elf_sized_object<32, true>(name, input_file,
914                                                  offset, ehdr);
915         }
916       else
917         {
918           elfcpp::Ehdr<32, false> ehdr(p);
919           return make_elf_sized_object<32, false>(name, input_file,
920                                                   offset, ehdr);
921         }
922     }
923   else
924     {
925       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
926         {
927           fprintf(stderr, _("%s: %s: ELF file too short\n"),
928                   program_name, name.c_str());
929           gold_exit(false);
930         }
931       if (big_endian)
932         {
933           elfcpp::Ehdr<64, true> ehdr(p);
934           return make_elf_sized_object<64, true>(name, input_file,
935                                                  offset, ehdr);
936         }
937       else
938         {
939           elfcpp::Ehdr<64, false> ehdr(p);
940           return make_elf_sized_object<64, false>(name, input_file,
941                                                   offset, ehdr);
942         }
943     }
944 }
945
946 // Instantiate the templates we need.  We could use the configure
947 // script to restrict this to only the ones for implemented targets.
948
949 template
950 class Sized_relobj<32, false>;
951
952 template
953 class Sized_relobj<32, true>;
954
955 template
956 class Sized_relobj<64, false>;
957
958 template
959 class Sized_relobj<64, true>;
960
961 template
962 struct Relocate_info<32, false>;
963
964 template
965 struct Relocate_info<32, true>;
966
967 template
968 struct Relocate_info<64, false>;
969
970 template
971 struct Relocate_info<64, true>;
972
973 } // End namespace gold.