Finished layout code.
[binutils.git] / gold / object.cc
blob85019fb1138c8c137acebf87e7b8c9b4b6fbe7a4
1 // object.cc -- support for an object file for linking in gold
3 #include "gold.h"
5 #include <cerrno>
6 #include <cstring>
7 #include <cassert>
9 #include "object.h"
10 #include "target-select.h"
11 #include "layout.h"
13 namespace gold
16 // Class Object.
18 const unsigned char*
19 Object::get_view(off_t start, off_t size)
21 return this->input_file_->file().get_view(start + this->offset_, size);
24 void
25 Object::read(off_t start, off_t size, void* p)
27 this->input_file_->file().read(start + this->offset_, size, p);
30 File_view*
31 Object::get_lasting_view(off_t start, off_t size)
33 return this->input_file_->file().get_lasting_view(start + this->offset_,
34 size);
37 // Class Sized_object.
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 local_symbol_offset_(0)
53 if (ehdr.get_e_ehsize() != This::ehdr_size)
55 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
56 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
57 This::ehdr_size);
58 gold_exit(false);
60 if (ehdr.get_e_shentsize() != This::shdr_size)
62 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
63 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
64 This::shdr_size);
65 gold_exit(false);
69 template<int size, bool big_endian>
70 Sized_object<size, big_endian>::~Sized_object()
74 // Read the section header for section SHNUM.
76 template<int size, bool big_endian>
77 const unsigned char*
78 Sized_object<size, big_endian>::section_header(unsigned int shnum)
80 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
81 return this->get_view(symtabshdroff, This::shdr_size);
84 // Set up an object file bsaed on the file header. This sets up the
85 // target and reads the section information.
87 template<int size, bool big_endian>
88 void
89 Sized_object<size, big_endian>::setup(
90 const elfcpp::Ehdr<size, big_endian>& ehdr)
92 int machine = ehdr.get_e_machine();
93 Target* target = select_target(machine, size, big_endian,
94 ehdr.get_e_ident()[elfcpp::EI_OSABI],
95 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
96 if (target == NULL)
98 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
99 program_name, this->name().c_str(), machine);
100 gold_exit(false);
102 this->set_target(target);
103 unsigned int shnum = ehdr.get_e_shnum();
104 unsigned int shstrndx = ehdr.get_e_shstrndx();
105 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
106 && this->shoff_ != 0)
108 typename This::Shdr shdr(this->section_header(0));
109 if (shnum == 0)
110 shnum = shdr.get_sh_size();
111 if (shstrndx == elfcpp::SHN_XINDEX)
112 shstrndx = shdr.get_sh_link();
114 this->set_shnum(shnum);
115 this->shstrndx_ = shstrndx;
117 if (shnum == 0)
118 return;
120 // Find the SHT_SYMTAB section.
121 const unsigned char* p = this->get_view (this->shoff_,
122 shnum * This::shdr_size);
123 // Skip the first section, which is always empty.
124 p += This::shdr_size;
125 for (unsigned int i = 1; i < shnum; ++i)
127 typename This::Shdr shdr(p);
128 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
130 this->symtab_shnum_ = i;
131 break;
133 p += This::shdr_size;
137 // Read the symbols and relocations from an object file.
139 template<int size, bool big_endian>
140 Read_symbols_data
141 Sized_object<size, big_endian>::do_read_symbols()
143 if (this->symtab_shnum_ == 0)
145 // No symbol table. Weird but legal.
146 Read_symbols_data ret;
147 ret.symbols = NULL;
148 ret.symbols_size = 0;
149 ret.symbol_names = NULL;
150 ret.symbol_names_size = 0;
151 return ret;
154 // Read the symbol table section header.
155 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
156 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
158 // We only need the external symbols.
159 const int sym_size = This::sym_size;
160 off_t locsize = symtabshdr.get_sh_info() * sym_size;
161 off_t extoff = symtabshdr.get_sh_offset() + locsize;
162 off_t extsize = symtabshdr.get_sh_size() - locsize;
164 // Read the symbol table.
165 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
167 // Read the section header for the symbol names.
168 unsigned int strtab_shnum = symtabshdr.get_sh_link();
169 if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
171 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
172 program_name, this->name().c_str(), strtab_shnum);
173 gold_exit(false);
175 typename This::Shdr strtabshdr(this->section_header(strtab_shnum));
176 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
178 fprintf(stderr,
179 _("%s: %s: symbol table name section has wrong type: %u\n"),
180 program_name, this->name().c_str(),
181 static_cast<unsigned int>(strtabshdr.get_sh_type()));
182 gold_exit(false);
185 // Read the symbol names.
186 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
187 strtabshdr.get_sh_size());
189 Read_symbols_data ret;
190 ret.symbols = fvsymtab;
191 ret.symbols_size = extsize;
192 ret.symbol_names = fvstrtab;
193 ret.symbol_names_size = strtabshdr.get_sh_size();
195 return ret;
198 // Add the symbols to the symbol table.
200 template<int size, bool big_endian>
201 void
202 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
203 Read_symbols_data sd)
205 if (sd.symbols == NULL)
207 assert(sd.symbol_names == NULL);
208 return;
211 const int sym_size = This::sym_size;
212 size_t symcount = sd.symbols_size / sym_size;
213 if (symcount * sym_size != sd.symbols_size)
215 fprintf(stderr,
216 _("%s: %s: size of symbols is not multiple of symbol size\n"),
217 program_name, this->name().c_str());
218 gold_exit(false);
221 this->symbols_ = new Symbol*[symcount];
223 const elfcpp::Sym<size, big_endian>* syms =
224 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
225 const char* sym_names =
226 reinterpret_cast<const char*>(sd.symbol_names->data());
227 symtab->add_from_object(this, syms, symcount, sym_names,
228 sd.symbol_names_size, this->symbols_);
230 delete sd.symbols;
231 delete sd.symbol_names;
234 // Return whether to include a section group in the link. LAYOUT is
235 // used to keep track of which section groups we have already seen.
236 // INDEX is the index of the section group and SHDR is the section
237 // header. If we do not want to include this group, we set bits in
238 // OMIT for each section which should be discarded.
240 template<int size, bool big_endian>
241 bool
242 Sized_object<size, big_endian>::include_section_group(
243 Layout* layout,
244 unsigned int index,
245 const elfcpp::Shdr<size, big_endian>& shdr,
246 std::vector<bool>* omit)
248 // Read the section contents.
249 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
250 shdr.get_sh_size());
251 const elfcpp::Elf_Word* pword =
252 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
254 // The first word contains flags. We only care about COMDAT section
255 // groups. Other section groups are always included in the link
256 // just like ordinary sections.
257 elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
258 if ((flags & elfcpp::GRP_COMDAT) == 0)
259 return true;
261 // Look up the group signature, which is the name of a symbol. This
262 // is a lot of effort to go to to read a string. Why didn't they
263 // just use the name of the SHT_GROUP section as the group
264 // signature?
266 // Get the appropriate symbol table header (this will normally be
267 // the single SHT_SYMTAB section, but in principle it need not be).
268 if (shdr.get_sh_link() >= this->shnum())
270 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
271 program_name, this->name().c_str(), index, shdr.get_sh_link());
272 gold_exit(false);
275 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
277 // Read the symbol table entry.
278 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
280 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
281 program_name, this->name().c_str(), index, shdr.get_sh_info());
282 gold_exit(false);
284 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
285 const unsigned char* psym = this->get_view(symoff, This::sym_size);
286 elfcpp::Sym<size, big_endian> sym(psym);
288 // Read the section header for the symbol table names.
289 if (symshdr.get_sh_link() >= this->shnum())
291 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
292 program_name, this->name().c_str(), shdr.get_sh_link(),
293 symshdr.get_sh_link());
294 gold_exit(false);
297 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
299 // Read the symbol table names.
300 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
301 symnamehdr.get_sh_size());
302 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
304 // Get the section group signature.
305 if (sym.get_st_name() >= symnamehdr.get_sh_size())
307 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
308 program_name, this->name().c_str(), shdr.get_sh_info(),
309 sym.get_st_name());
310 gold_exit(false);
313 const char* signature = psymnames + sym.get_st_name();
315 // Record this section group, and see whether we've already seen one
316 // with the same signature.
317 if (layout->add_comdat(signature, true))
318 return true;
320 // This is a duplicate. We want to discard the sections in this
321 // group.
322 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
323 for (size_t i = 1; i < count; ++i)
325 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
326 if (secnum >= this->shnum())
328 fprintf(stderr,
329 _("%s: %s: section %u in section group %u out of range"),
330 program_name, this->name().c_str(), secnum,
331 index);
332 gold_exit(false);
334 (*omit)[secnum] = true;
337 return false;
340 // Whether to include a linkonce section in the link. NAME is the
341 // name of the section and SHDR is the section header.
343 // Linkonce sections are a GNU extension implemented in the original
344 // GNU linker before section groups were defined. The semantics are
345 // that we only include one linkonce section with a given name. The
346 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
347 // where T is the type of section and SYMNAME is the name of a symbol.
348 // In an attempt to make linkonce sections interact well with section
349 // groups, we try to identify SYMNAME and use it like a section group
350 // signature. We want to block section groups with that signature,
351 // but not other linkonce sections with that signature. We also use
352 // the full name of the linkonce section as a normal section group
353 // signature.
355 template<int size, bool big_endian>
356 bool
357 Sized_object<size, big_endian>::include_linkonce_section(
358 Layout* layout,
359 const char* name,
360 const elfcpp::Shdr<size, big_endian>&)
362 const char* symname = strrchr(name, '.') + 1;
363 bool omit1 = layout->add_comdat(symname, false);
364 bool omit2 = layout->add_comdat(name, true);
365 return omit1 || omit2;
368 // Lay out the input sections. We walk through the sections and check
369 // whether they should be included in the link. If they should, we
370 // pass them to the Layout object, which will return an output section
371 // and an offset.
373 template<int size, bool big_endian>
374 void
375 Sized_object<size, big_endian>::do_layout(Layout* layout)
377 // This is always called from the main thread. Lock the file to
378 // keep the error checks happy.
379 Task_locker_obj<File_read> frl(this->input_file()->file());
381 // Get the section headers.
382 unsigned int shnum = this->shnum();
383 const unsigned char* pshdrs = this->get_view(this->shoff_,
384 shnum * This::shdr_size);
386 // Get the section names.
387 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
388 typename This::Shdr shdrnames(pshdrnames);
389 typename elfcpp::Elf_types<size>::Elf_WXword names_size =
390 shdrnames.get_sh_size();
391 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
392 shdrnames.get_sh_size());
393 const char* pnames = reinterpret_cast<const char*>(pnamesu);
395 std::vector<Map_to_output>& map_sections(this->map_to_output());
396 map_sections.reserve(shnum);
398 // Keep track of which sections to omit.
399 std::vector<bool> omit(shnum, false);
401 for (unsigned int i = 0; i < shnum; ++i)
403 typename This::Shdr shdr(pshdrs);
405 if (shdr.get_sh_name() >= names_size)
407 fprintf(stderr,
408 _("%s: %s: bad section name offset for section %u: %lu\n"),
409 program_name, this->name().c_str(), i,
410 static_cast<unsigned long>(shdr.get_sh_name()));
411 gold_exit(false);
414 const char* name = pnames + shdr.get_sh_name();
416 bool discard = omit[i];
417 if (!discard)
419 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
421 if (!this->include_section_group(layout, i, shdr, &omit))
422 discard = true;
424 else if (Layout::is_linkonce(name))
426 if (!this->include_linkonce_section(layout, name, shdr))
427 discard = true;
431 if (discard)
433 // Do not include this section in the link.
434 map_sections[i].output_section = NULL;
435 continue;
438 off_t offset;
439 Output_section* os = layout->layout(this, name, shdr, &offset);
441 map_sections[i].output_section = os;
442 map_sections[i].offset = offset;
444 pshdrs += This::shdr_size;
448 // Finalize the local symbols. Here we record the file offset at
449 // which they should be output and we add their names to *POOL.
450 // Return the new file offset. This function is always called from
451 // the main thread. The actual output of the local symbols will occur
452 // in a separate task.
454 template<int size, bool big_endian>
455 off_t
456 Sized_object<size, big_endian>::do_finalize_local_symbols(off_t off,
457 Stringpool* pool)
459 this->local_symbol_offset_ = off;
461 // Read the symbol table section header.
462 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
463 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
465 // Read the local symbols.
466 unsigned int loccount = symtabshdr.get_sh_info();
467 const int sym_size = This::sym_size;
468 off_t locsize = loccount * sym_size;
469 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
470 locsize);
472 // Read the section header for the symbol names.
473 typename This::Shdr strtabshdr(
474 this->section_header(symtabshdr.get_sh_link()));
475 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
477 // Read the symbol names.
478 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
479 strtabshdr.get_sh_size());
480 const char* pnames = reinterpret_cast<const char*>(pnamesu);
482 // Loop over the local symbols.
484 std::vector<Map_to_output>& mo(this->map_to_output());
485 unsigned int shnum = this->shnum();
486 // Skip the first, dummy, symbol.
487 psyms += sym_size;
488 for (unsigned int i = 1; i < loccount; ++i)
490 elfcpp::Sym<size, big_endian> sym(psyms);
492 unsigned int shndx = sym.get_st_shndx();
494 if (shndx >= elfcpp::SHN_LORESERVE)
496 if (shndx != elfcpp::SHN_ABS)
498 fprintf(stderr,
499 _("%s: %s: unknown section index %u "
500 "for local symbol %u\n"),
501 program_name, this->name().c_str(), shndx, i);
502 gold_exit(false);
504 // FIXME: Handle SHN_XINDEX.
506 else
508 if (shndx >= shnum)
510 fprintf(stderr,
511 _("%s: %s: local symbol %u section index %u "
512 "out of range\n"),
513 program_name, this->name().c_str(), i, shndx);
514 gold_exit(false);
517 if (mo[shndx].output_section == NULL)
518 continue;
521 pool->add(pnames + sym.get_st_name());
522 off += sym_size;
524 psyms += sym_size;
527 return off;
530 // Input_objects methods.
532 void
533 Input_objects::add_object(Object* obj)
535 this->object_list_.push_back(obj);
537 Target* target = obj->target();
538 if (this->target_ == NULL)
539 this->target_ = target;
540 else if (this->target_ != target)
542 fprintf(stderr, "%s: %s: incompatible target\n",
543 program_name, obj->name().c_str());
544 gold_exit(false);
547 if (obj->is_dynamic())
548 this->any_dynamic_ = true;
551 } // End namespace gold.
553 namespace
556 using namespace gold;
558 // Read an ELF file with the header and return the appropriate
559 // instance of Object.
561 template<int size, bool big_endian>
562 Object*
563 make_elf_sized_object(const std::string& name, Input_file* input_file,
564 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
566 int et = ehdr.get_e_type();
567 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
569 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
570 program_name, name.c_str(), static_cast<int>(et));
571 gold_exit(false);
574 if (et == elfcpp::ET_REL)
576 Sized_object<size, big_endian>* obj =
577 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
578 obj->setup(ehdr);
579 return obj;
581 else
583 // elfcpp::ET_DYN
584 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
585 program_name, name.c_str());
586 gold_exit(false);
587 // Sized_dynobj<size, big_endian>* obj =
588 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
589 // offset, ehdr);
590 // obj->setup(ehdr);
591 // return obj;
595 } // End anonymous namespace.
597 namespace gold
600 // Read an ELF file and return the appropriate instance of Object.
602 Object*
603 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
604 const unsigned char* p, off_t bytes)
606 if (bytes < elfcpp::EI_NIDENT)
608 fprintf(stderr, _("%s: %s: ELF file too short\n"),
609 program_name, name.c_str());
610 gold_exit(false);
613 int v = p[elfcpp::EI_VERSION];
614 if (v != elfcpp::EV_CURRENT)
616 if (v == elfcpp::EV_NONE)
617 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
618 program_name, name.c_str());
619 else
620 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
621 program_name, name.c_str(), v);
622 gold_exit(false);
625 int c = p[elfcpp::EI_CLASS];
626 if (c == elfcpp::ELFCLASSNONE)
628 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
629 program_name, name.c_str());
630 gold_exit(false);
632 else if (c != elfcpp::ELFCLASS32
633 && c != elfcpp::ELFCLASS64)
635 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
636 program_name, name.c_str(), c);
637 gold_exit(false);
640 int d = p[elfcpp::EI_DATA];
641 if (d == elfcpp::ELFDATANONE)
643 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
644 program_name, name.c_str());
645 gold_exit(false);
647 else if (d != elfcpp::ELFDATA2LSB
648 && d != elfcpp::ELFDATA2MSB)
650 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
651 program_name, name.c_str(), d);
652 gold_exit(false);
655 bool big_endian = d == elfcpp::ELFDATA2MSB;
657 if (c == elfcpp::ELFCLASS32)
659 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
661 fprintf(stderr, _("%s: %s: ELF file too short\n"),
662 program_name, name.c_str());
663 gold_exit(false);
665 if (big_endian)
667 elfcpp::Ehdr<32, true> ehdr(p);
668 return make_elf_sized_object<32, true>(name, input_file,
669 offset, ehdr);
671 else
673 elfcpp::Ehdr<32, false> ehdr(p);
674 return make_elf_sized_object<32, false>(name, input_file,
675 offset, ehdr);
678 else
680 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
682 fprintf(stderr, _("%s: %s: ELF file too short\n"),
683 program_name, name.c_str());
684 gold_exit(false);
686 if (big_endian)
688 elfcpp::Ehdr<64, true> ehdr(p);
689 return make_elf_sized_object<64, true>(name, input_file,
690 offset, ehdr);
692 else
694 elfcpp::Ehdr<64, false> ehdr(p);
695 return make_elf_sized_object<64, false>(name, input_file,
696 offset, ehdr);
701 // Instantiate the templates we need. We could use the configure
702 // script to restrict this to only the ones for implemented targets.
704 template
705 class Sized_object<32, false>;
707 template
708 class Sized_object<32, true>;
710 template
711 class Sized_object<64, false>;
713 template
714 class Sized_object<64, true>;
716 } // End namespace gold.