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