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