New drop, with first cut of section layout code.
[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
13 namespace gold
14 {
15
16 // Class Object.
17
18 const unsigned char*
19 Object::get_view(off_t start, off_t size)
20 {
21   return this->input_file_->file().get_view(start + this->offset_, size);
22 }
23
24 void
25 Object::read(off_t start, off_t size, void* p)
26 {
27   this->input_file_->file().read(start + this->offset_, size, p);
28 }
29
30 File_view*
31 Object::get_lasting_view(off_t start, off_t size)
32 {
33   return this->input_file_->file().get_lasting_view(start + this->offset_,
34                                                     size);
35 }
36
37 // Class Sized_object.
38
39 template<int size, bool big_endian>
40 Sized_object<size, big_endian>::Sized_object(
41     const std::string& name,
42     Input_file* input_file,
43     off_t offset,
44     const elfcpp::Ehdr<size, big_endian>& ehdr)
45   : Object(name, input_file, false, offset),
46     flags_(ehdr.get_e_flags()),
47     shoff_(ehdr.get_e_shoff()),
48     shstrndx_(0),
49     symtab_shnum_(0),
50     symbols_(NULL)
51 {
52   if (ehdr.get_e_ehsize() != This::ehdr_size)
53     {
54       fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
55               program_name, this->name().c_str(), ehdr.get_e_ehsize(),
56               This::ehdr_size);
57       gold_exit(false);
58     }
59   if (ehdr.get_e_shentsize() != This::shdr_size)
60     {
61       fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
62               program_name, this->name().c_str(), ehdr.get_e_shentsize(),
63               This::shdr_size);
64       gold_exit(false);
65     }
66 }
67
68 template<int size, bool big_endian>
69 Sized_object<size, big_endian>::~Sized_object()
70 {
71 }
72
73 // Set up an object file bsaed on the file header.  This sets up the
74 // target and reads the section information.
75
76 template<int size, bool big_endian>
77 void
78 Sized_object<size, big_endian>::setup(
79     const elfcpp::Ehdr<size, big_endian>& ehdr)
80 {
81   int machine = ehdr.get_e_machine();
82   Target* target = select_target(machine, size, big_endian,
83                                  ehdr.get_e_ident()[elfcpp::EI_OSABI],
84                                  ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
85   if (target == NULL)
86     {
87       fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
88               program_name, this->name().c_str(), machine);
89       gold_exit(false);
90     }
91   this->set_target(target);
92   unsigned int shnum = ehdr.get_e_shnum();
93   unsigned int shstrndx = ehdr.get_e_shstrndx();
94   if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
95       && this->shoff_ != 0)
96     {
97       const unsigned char* p = this->get_view (this->shoff_, This::shdr_size);
98       elfcpp::Shdr<size, big_endian> shdr(p);
99       if (shnum == 0)
100         shnum = shdr.get_sh_size();
101       if (shstrndx == elfcpp::SHN_XINDEX)
102         shstrndx = shdr.get_sh_link();
103     }
104   this->set_shnum(shnum);
105   this->shstrndx_ = shstrndx;
106
107   if (shnum == 0)
108     return;
109
110   // Find the SHT_SYMTAB section.
111   const unsigned char* p = this->get_view (this->shoff_,
112                                            shnum * This::shdr_size);
113   // Skip the first section, which is always empty.
114   p += This::shdr_size;
115   for (unsigned int i = 1; i < shnum; ++i)
116     {
117       elfcpp::Shdr<size, big_endian> shdr(p);
118       if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
119         {
120           this->symtab_shnum_ = i;
121           break;
122         }
123       p += This::shdr_size;
124     }
125 }
126
127 // Read the symbols and relocations from an object file.
128
129 template<int size, bool big_endian>
130 Read_symbols_data
131 Sized_object<size, big_endian>::do_read_symbols()
132 {
133   if (this->symtab_shnum_ == 0)
134     {
135       // No symbol table.  Weird but legal.
136       Read_symbols_data ret;
137       ret.symbols = NULL;
138       ret.symbols_size = 0;
139       ret.symbol_names = NULL;
140       ret.symbol_names_size = 0;
141       return ret;
142     }
143
144   const int shdr_size = This::shdr_size;
145
146   // Read the symbol table section header.
147   off_t symtabshdroff = this->shoff_ + (this->symtab_shnum_ * shdr_size);
148   const unsigned char* psymtabshdr = this->get_view(symtabshdroff, shdr_size);
149   elfcpp::Shdr<size, big_endian> symtabshdr(psymtabshdr);
150   assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
151
152   // We only need the external symbols.
153   const int sym_size = This::sym_size;
154   off_t locsize = symtabshdr.get_sh_info() * sym_size;
155   off_t extoff = symtabshdr.get_sh_offset() + locsize;
156   off_t extsize = symtabshdr.get_sh_size() - locsize;
157
158   // Read the symbol table.
159   File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
160
161   // Read the section header for the symbol names.
162   unsigned int strtab_shnum = symtabshdr.get_sh_link();
163   if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
164     {
165       fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
166               program_name, this->name().c_str(), strtab_shnum);
167       gold_exit(false);
168     }
169   off_t strtabshdroff = this->shoff_ + (strtab_shnum * shdr_size);
170   const unsigned char *pstrtabshdr = this->get_view(strtabshdroff, shdr_size);
171   elfcpp::Shdr<size, big_endian> strtabshdr(pstrtabshdr);
172   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
173     {
174       fprintf(stderr,
175               _("%s: %s: symbol table name section has wrong type: %u\n"),
176               program_name, this->name().c_str(),
177               static_cast<unsigned int>(strtabshdr.get_sh_type()));
178       gold_exit(false);
179     }
180
181   // Read the symbol names.
182   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
183                                                strtabshdr.get_sh_size());
184
185   Read_symbols_data ret;
186   ret.symbols = fvsymtab;
187   ret.symbols_size = extsize;
188   ret.symbol_names = fvstrtab;
189   ret.symbol_names_size = strtabshdr.get_sh_size();
190
191   return ret;
192 }
193
194 // Add the symbols to the symbol table.
195
196 template<int size, bool big_endian>
197 void
198 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
199                                                Read_symbols_data sd)
200 {
201   if (sd.symbols == NULL)
202     {
203       assert(sd.symbol_names == NULL);
204       return;
205     }
206
207   const int sym_size = This::sym_size;
208   size_t symcount = sd.symbols_size / sym_size;
209   if (symcount * sym_size != sd.symbols_size)
210     {
211       fprintf(stderr,
212               _("%s: %s: size of symbols is not multiple of symbol size\n"),
213               program_name, this->name().c_str());
214       gold_exit(false);
215     }
216
217   this->symbols_ = new Symbol*[symcount];
218
219   const elfcpp::Sym<size, big_endian>* syms =
220     reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
221   const char* sym_names =
222     reinterpret_cast<const char*>(sd.symbol_names->data());
223   symtab->add_from_object(this, syms, symcount, sym_names, 
224                           sd.symbol_names_size,  this->symbols_);
225
226   delete sd.symbols;
227   delete sd.symbol_names;
228 }
229
230 // Return whether to include a section group in the link.  LAYOUT is
231 // used to keep track of which section groups we have already seen.
232 // INDEX is the index of the section group and SHDR is the section
233 // header.  If we do not want to include this group, we set bits in
234 // OMIT for each section which should be discarded.
235
236 template<int size, bool big_endian>
237 bool
238 Sized_object<size, big_endian>::include_section_group(
239     Layout* layout,
240     unsigned int index,
241     const elfcpp::Shdr<size, big_endian>& shdr,
242     std::vector<bool>* omit)
243 {
244   // Read the section contents.
245   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
246                                              shdr.get_sh_size());
247   const elfcpp::Elf_Word* pword =
248     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
249
250   // The first word contains flags.  We only care about COMDAT section
251   // groups.  Other section groups are always included in the link
252   // just like ordinary sections.
253   elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
254   if ((flags & elfcpp::GRP_COMDAT) == 0)
255     return true;
256
257   // Look up the group signature, which is the name of a symbol.  This
258   // is a lot of effort to go to to read a string.  Why didn't they
259   // just use the name of the SHT_GROUP section as the group
260   // signature?
261
262   // Get the appropriate symbol table header (this will normally be
263   // the single SHT_SYMTAB section, but in principle it need not be).
264   if (shdr.get_sh_link() >= this->shnum())
265     {
266       fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
267               program_name, this->name().c_str(), index, shdr.get_sh_link());
268       gold_exit(false);
269     }
270   off_t off = this->shoff_ + shdr.get_sh_link() * This::shdr_size;
271   const unsigned char* psymshdr = this->get_view(off, This::shdr_size);
272   elfcpp::Shdr<size, big_endian> symshdr(psymshdr);
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   off_t symnameoff = this->shoff_ + symshdr.get_sh_link() * This::shdr_size;
294   const unsigned char* psymnamehdr = this->get_view(symnameoff,
295                                                     This::shdr_size);
296   elfcpp::Shdr<size, big_endian> symnamehdr(psymnamehdr);
297
298   // Read the symbol table names.
299   const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
300                                                    symnamehdr.get_sh_size());
301   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
302
303   // Get the section group signature.
304   if (sym.get_st_name() >= symnamehdr.get_sh_size())
305     {
306       fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
307               program_name, this->name().c_str(), shdr.get_sh_info(),
308               sym.get_st_name());
309       gold_exit(false);
310     }
311
312   const char* signature = psymnames + sym.get_st_name();
313
314   // Record this section group, and see whether we've already seen one
315   // with the same signature.
316   if (layout->add_comdat(signature, true))
317     return true;
318
319   // This is a duplicate.  We want to discard the sections in this
320   // group.
321   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
322   for (size_t i = 1; i < count; ++i)
323     {
324       elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
325       if (secnum >= this->shnum())
326         {
327           fprintf(stderr,
328                   _("%s: %s: section %u in section group %u out of range"),
329                   program_name, this->name().c_str(), secnum,
330                   index);
331           gold_exit(false);
332         }
333       (*omit)[secnum] = true;
334     }
335
336   return false;
337 }
338
339 // Whether to include a linkonce section in the link.  NAME is the
340 // name of the section and SHDR is the section header.
341
342 // Linkonce sections are a GNU extension implemented in the original
343 // GNU linker before section groups were defined.  The semantics are
344 // that we only include one linkonce section with a given name.  The
345 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
346 // where T is the type of section and SYMNAME is the name of a symbol.
347 // In an attempt to make linkonce sections interact well with section
348 // groups, we try to identify SYMNAME and use it like a section group
349 // signature.  We want to block section groups with that signature,
350 // but not other linkonce sections with that signature.  We also use
351 // the full name of the linkonce section as a normal section group
352 // signature.
353
354 template<int size, bool big_endian>
355 bool
356 Sized_object<size, big_endian>::include_linkonce_section(
357     Layout* layout,
358     const char* name,
359     const elfcpp::Shdr<size, big_endian>&)
360 {
361   const char* symname = strrchr(name, '.') + 1;
362   bool omit1 = layout->add_comdat(symname, false);
363   bool omit2 = layout->add_comdat(name, true);
364   return omit1 || omit2;
365 }
366
367 // Lay out the input sections.  We walk through the sections and check
368 // whether they should be included in the link.  If they should, we
369 // pass them to the Layout object, which will return an output section
370 // and an offset.
371
372 template<int size, bool big_endian>
373 void
374 Sized_object<size, big_endian>::do_layout(Layout* layout)
375 {
376   // This is always called from the main thread.  Lock the file to
377   // keep the error checks happy.
378   Task_locker_obj<File_read> frl(this->input_file()->file());
379
380   // Get the section headers.
381   unsigned int shnum = this->shnum();
382   const unsigned char* pshdrs = this->get_view(this->shoff_,
383                                                shnum * This::shdr_size);
384
385   // Get the section names.
386   const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
387   elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
388   typename elfcpp::Elf_types<size>::Elf_WXword names_size =
389     shdrnames.get_sh_size();
390   const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
391                                                 shdrnames.get_sh_size());
392   const char* pnames = reinterpret_cast<const char*>(pnamesu);
393
394   std::vector<Map_to_output>& map_sections(this->map_to_output());
395   map_sections.reserve(shnum);
396
397   // Keep track of which sections to omit.
398   std::vector<bool> omit(shnum, false);
399
400   for (unsigned int i = 0; i < shnum; ++i)
401     {
402       elfcpp::Shdr<size, big_endian> shdr(pshdrs);
403
404       if (shdr.get_sh_name() >= names_size)
405         {
406           fprintf(stderr,
407                   _("%s: %s: bad section name offset for section %u: %lu\n"),
408                   program_name, this->name().c_str(), i,
409                   static_cast<unsigned long>(shdr.get_sh_name()));
410           gold_exit(false);
411         }
412
413       const char* name = pnames + shdr.get_sh_name();
414
415       bool discard = omit[i];
416       if (!discard)
417         {
418           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
419             {
420               if (!this->include_section_group(layout, i, shdr, &omit))
421                 discard = true;
422             }
423           else if (Layout::is_linkonce(name))
424             {
425               if (!this->include_linkonce_section(layout, name, shdr))
426                 discard = true;
427             }
428         }
429
430       if (discard)
431         {
432           // Do not include this section in the link.
433           map_sections[i].output_section = NULL;
434           continue;
435         }
436
437       off_t offset;
438       Output_section* os = layout->layout(this, name, shdr, &offset);
439
440       map_sections[i].output_section = os;
441       map_sections[i].offset = offset;
442
443       pshdrs += This::shdr_size;
444     }
445 }
446
447 } // End namespace gold.
448
449 namespace
450 {
451
452 using namespace gold;
453
454 // Read an ELF file with the header and return the appropriate
455 // instance of Object.
456
457 template<int size, bool big_endian>
458 Object*
459 make_elf_sized_object(const std::string& name, Input_file* input_file,
460                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
461 {
462   int et = ehdr.get_e_type();
463   if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
464     {
465       fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
466               program_name, name.c_str(), static_cast<int>(et));
467       gold_exit(false);
468     }
469
470   if (et == elfcpp::ET_REL)
471     {
472       Sized_object<size, big_endian>* obj =
473         new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
474       obj->setup(ehdr);
475       return obj;
476     }
477   else
478     {
479       // elfcpp::ET_DYN
480       fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
481               program_name, name.c_str());
482       gold_exit(false);
483 //       Sized_dynobj<size, big_endian>* obj =
484 //      new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
485 //                                         offset, ehdr);
486 //       obj->setup(ehdr);
487 //       return obj;
488     }
489 }
490
491 } // End anonymous namespace.
492
493 namespace gold
494 {
495
496 // Read an ELF file and return the appropriate instance of Object.
497
498 Object*
499 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
500                 const unsigned char* p, off_t bytes)
501 {
502   if (bytes < elfcpp::EI_NIDENT)
503     {
504       fprintf(stderr, _("%s: %s: ELF file too short\n"),
505               program_name, name.c_str());
506       gold_exit(false);
507     }
508
509   int v = p[elfcpp::EI_VERSION];
510   if (v != elfcpp::EV_CURRENT)
511     {
512       if (v == elfcpp::EV_NONE)
513         fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
514                 program_name, name.c_str());
515       else
516         fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
517                 program_name, name.c_str(), v);
518       gold_exit(false);
519     }
520
521   int c = p[elfcpp::EI_CLASS];
522   if (c == elfcpp::ELFCLASSNONE)
523     {
524       fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
525               program_name, name.c_str());
526       gold_exit(false);
527     }
528   else if (c != elfcpp::ELFCLASS32
529            && c != elfcpp::ELFCLASS64)
530     {
531       fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
532               program_name, name.c_str(), c);
533       gold_exit(false);
534     }
535
536   int d = p[elfcpp::EI_DATA];
537   if (d == elfcpp::ELFDATANONE)
538     {
539       fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
540               program_name, name.c_str());
541       gold_exit(false);
542     }
543   else if (d != elfcpp::ELFDATA2LSB
544            && d != elfcpp::ELFDATA2MSB)
545     {
546       fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
547               program_name, name.c_str(), d);
548       gold_exit(false);
549     }
550
551   bool big_endian = d == elfcpp::ELFDATA2MSB;
552
553   if (c == elfcpp::ELFCLASS32)
554     {
555       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
556         {
557           fprintf(stderr, _("%s: %s: ELF file too short\n"),
558                   program_name, name.c_str());
559           gold_exit(false);
560         }
561       if (big_endian)
562         {
563           elfcpp::Ehdr<32, true> ehdr(p);
564           return make_elf_sized_object<32, true>(name, input_file,
565                                                  offset, ehdr);
566         }
567       else
568         {
569           elfcpp::Ehdr<32, false> ehdr(p);
570           return make_elf_sized_object<32, false>(name, input_file,
571                                                   offset, ehdr);
572         }
573     }
574   else
575     {
576       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
577         {
578           fprintf(stderr, _("%s: %s: ELF file too short\n"),
579                   program_name, name.c_str());
580           gold_exit(false);
581         }
582       if (big_endian)
583         {
584           elfcpp::Ehdr<64, true> ehdr(p);
585           return make_elf_sized_object<64, true>(name, input_file,
586                                                  offset, ehdr);
587         }
588       else
589         {
590           elfcpp::Ehdr<64, false> ehdr(p);
591           return make_elf_sized_object<64, false>(name, input_file,
592                                                   offset, ehdr);
593         }
594     }
595 }
596
597 // Instantiate the templates we need.  We could use the configure
598 // script to restrict this to only the ones for implemented targets.
599
600 template
601 class Sized_object<32, false>;
602
603 template
604 class Sized_object<32, true>;
605
606 template
607 class Sized_object<64, false>;
608
609 template
610 class Sized_object<64, true>;
611
612 } // End namespace gold.