1 // object.cc -- support for an object file for linking in gold
10 #include "target-select.h"
20 Object::get_view(off_t start, off_t size)
22 return this->input_file_->file().get_view(start + this->offset_, size);
26 Object::read(off_t start, off_t size, void* p)
28 this->input_file_->file().read(start + this->offset_, size, p);
32 Object::get_lasting_view(off_t start, off_t size)
34 return this->input_file_->file().get_lasting_view(start + this->offset_,
38 // Class Sized_object.
40 template<int size, bool big_endian>
41 Sized_object<size, big_endian>::Sized_object(
42 const std::string& name,
43 Input_file* input_file,
45 const elfcpp::Ehdr<size, big_endian>& ehdr)
46 : Object(name, input_file, false, offset),
47 flags_(ehdr.get_e_flags()),
48 shoff_(ehdr.get_e_shoff()),
51 local_symbol_count_(0),
52 output_local_symbol_count_(0),
54 local_symbol_offset_(0),
57 if (ehdr.get_e_ehsize() != This::ehdr_size)
59 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
60 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
64 if (ehdr.get_e_shentsize() != This::shdr_size)
66 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
67 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
73 template<int size, bool big_endian>
74 Sized_object<size, big_endian>::~Sized_object()
78 // Read the section header for section SHNUM.
80 template<int size, bool big_endian>
82 Sized_object<size, big_endian>::section_header(unsigned int shnum)
84 assert(shnum < this->shnum());
85 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
86 return this->get_view(symtabshdroff, This::shdr_size);
89 // Set up an object file bsaed on the file header. This sets up the
90 // target and reads the section information.
92 template<int size, bool big_endian>
94 Sized_object<size, big_endian>::setup(
95 const elfcpp::Ehdr<size, big_endian>& ehdr)
97 int machine = ehdr.get_e_machine();
98 Target* target = select_target(machine, size, big_endian,
99 ehdr.get_e_ident()[elfcpp::EI_OSABI],
100 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
103 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
104 program_name, this->name().c_str(), machine);
107 this->set_target(target);
108 unsigned int shnum = ehdr.get_e_shnum();
109 unsigned int shstrndx = ehdr.get_e_shstrndx();
110 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
111 && this->shoff_ != 0)
113 typename This::Shdr shdr(this->section_header(0));
115 shnum = shdr.get_sh_size();
116 if (shstrndx == elfcpp::SHN_XINDEX)
117 shstrndx = shdr.get_sh_link();
119 this->set_shnum(shnum);
120 this->shstrndx_ = shstrndx;
125 // Find the SHT_SYMTAB section.
126 const unsigned char* p = this->get_view (this->shoff_,
127 shnum * This::shdr_size);
128 // Skip the first section, which is always empty.
129 p += This::shdr_size;
130 for (unsigned int i = 1; i < shnum; ++i)
132 typename This::Shdr shdr(p);
133 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
135 this->symtab_shnum_ = i;
138 p += This::shdr_size;
142 // Read the symbols and relocations from an object file.
144 template<int size, bool big_endian>
146 Sized_object<size, big_endian>::do_read_symbols()
148 if (this->symtab_shnum_ == 0)
150 // No symbol table. Weird but legal.
151 Read_symbols_data ret;
153 ret.symbols_size = 0;
154 ret.symbol_names = NULL;
155 ret.symbol_names_size = 0;
159 // Read the symbol table section header.
160 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
161 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
163 // We only need the external symbols.
164 const int sym_size = This::sym_size;
165 off_t locsize = symtabshdr.get_sh_info() * sym_size;
166 off_t extoff = symtabshdr.get_sh_offset() + locsize;
167 off_t extsize = symtabshdr.get_sh_size() - locsize;
169 // Read the symbol table.
170 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
172 // Read the section header for the symbol names.
173 unsigned int strtab_shnum = symtabshdr.get_sh_link();
174 if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
176 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
177 program_name, this->name().c_str(), strtab_shnum);
180 typename This::Shdr strtabshdr(this->section_header(strtab_shnum));
181 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
184 _("%s: %s: symbol table name section has wrong type: %u\n"),
185 program_name, this->name().c_str(),
186 static_cast<unsigned int>(strtabshdr.get_sh_type()));
190 // Read the symbol names.
191 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
192 strtabshdr.get_sh_size());
194 Read_symbols_data ret;
195 ret.symbols = fvsymtab;
196 ret.symbols_size = extsize;
197 ret.symbol_names = fvstrtab;
198 ret.symbol_names_size = strtabshdr.get_sh_size();
203 // Add the symbols to the symbol table.
205 template<int size, bool big_endian>
207 Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
208 Read_symbols_data sd)
210 if (sd.symbols == NULL)
212 assert(sd.symbol_names == NULL);
216 const int sym_size = This::sym_size;
217 size_t symcount = sd.symbols_size / sym_size;
218 if (symcount * sym_size != sd.symbols_size)
221 _("%s: %s: size of symbols is not multiple of symbol size\n"),
222 program_name, this->name().c_str());
226 this->symbols_ = new Symbol*[symcount];
228 const elfcpp::Sym<size, big_endian>* syms =
229 reinterpret_cast<const elfcpp::Sym<size, big_endian>*>(sd.symbols->data());
230 const char* sym_names =
231 reinterpret_cast<const char*>(sd.symbol_names->data());
232 symtab->add_from_object(this, syms, symcount, sym_names,
233 sd.symbol_names_size, this->symbols_);
236 delete sd.symbol_names;
239 // Return whether to include a section group in the link. LAYOUT is
240 // used to keep track of which section groups we have already seen.
241 // INDEX is the index of the section group and SHDR is the section
242 // header. If we do not want to include this group, we set bits in
243 // OMIT for each section which should be discarded.
245 template<int size, bool big_endian>
247 Sized_object<size, big_endian>::include_section_group(
250 const elfcpp::Shdr<size, big_endian>& shdr,
251 std::vector<bool>* omit)
253 // Read the section contents.
254 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
256 const elfcpp::Elf_Word* pword =
257 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
259 // The first word contains flags. We only care about COMDAT section
260 // groups. Other section groups are always included in the link
261 // just like ordinary sections.
262 elfcpp::Elf_Word flags = elfcpp::read_elf_word<big_endian>(pword);
263 if ((flags & elfcpp::GRP_COMDAT) == 0)
266 // Look up the group signature, which is the name of a symbol. This
267 // is a lot of effort to go to to read a string. Why didn't they
268 // just use the name of the SHT_GROUP section as the group
271 // Get the appropriate symbol table header (this will normally be
272 // the single SHT_SYMTAB section, but in principle it need not be).
273 if (shdr.get_sh_link() >= this->shnum())
275 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
276 program_name, this->name().c_str(), index, shdr.get_sh_link());
280 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
282 // Read the symbol table entry.
283 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
285 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
286 program_name, this->name().c_str(), index, shdr.get_sh_info());
289 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
290 const unsigned char* psym = this->get_view(symoff, This::sym_size);
291 elfcpp::Sym<size, big_endian> sym(psym);
293 // Read the section header for the symbol table names.
294 if (symshdr.get_sh_link() >= this->shnum())
296 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
297 program_name, this->name().c_str(), shdr.get_sh_link(),
298 symshdr.get_sh_link());
302 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
304 // Read the symbol table names.
305 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
306 symnamehdr.get_sh_size());
307 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
309 // Get the section group signature.
310 if (sym.get_st_name() >= symnamehdr.get_sh_size())
312 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
313 program_name, this->name().c_str(), shdr.get_sh_info(),
318 const char* signature = psymnames + sym.get_st_name();
320 // Record this section group, and see whether we've already seen one
321 // with the same signature.
322 if (layout->add_comdat(signature, true))
325 // This is a duplicate. We want to discard the sections in this
327 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
328 for (size_t i = 1; i < count; ++i)
330 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
331 if (secnum >= this->shnum())
334 _("%s: %s: section %u in section group %u out of range"),
335 program_name, this->name().c_str(), secnum,
339 (*omit)[secnum] = true;
345 // Whether to include a linkonce section in the link. NAME is the
346 // name of the section and SHDR is the section header.
348 // Linkonce sections are a GNU extension implemented in the original
349 // GNU linker before section groups were defined. The semantics are
350 // that we only include one linkonce section with a given name. The
351 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
352 // where T is the type of section and SYMNAME is the name of a symbol.
353 // In an attempt to make linkonce sections interact well with section
354 // groups, we try to identify SYMNAME and use it like a section group
355 // signature. We want to block section groups with that signature,
356 // but not other linkonce sections with that signature. We also use
357 // the full name of the linkonce section as a normal section group
360 template<int size, bool big_endian>
362 Sized_object<size, big_endian>::include_linkonce_section(
365 const elfcpp::Shdr<size, big_endian>&)
367 const char* symname = strrchr(name, '.') + 1;
368 bool omit1 = layout->add_comdat(symname, false);
369 bool omit2 = layout->add_comdat(name, true);
370 return omit1 || omit2;
373 // Lay out the input sections. We walk through the sections and check
374 // whether they should be included in the link. If they should, we
375 // pass them to the Layout object, which will return an output section
378 template<int size, bool big_endian>
380 Sized_object<size, big_endian>::do_layout(Layout* layout)
382 // This is always called from the main thread. Lock the file to
383 // keep the error checks happy.
384 Task_locker_obj<File_read> frl(this->input_file()->file());
386 // Get the section headers.
387 unsigned int shnum = this->shnum();
388 const unsigned char* pshdrs = this->get_view(this->shoff_,
389 shnum * This::shdr_size);
391 // Get the section names.
392 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
393 typename This::Shdr shdrnames(pshdrnames);
394 typename elfcpp::Elf_types<size>::Elf_WXword names_size =
395 shdrnames.get_sh_size();
396 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
397 shdrnames.get_sh_size());
398 const char* pnames = reinterpret_cast<const char*>(pnamesu);
400 std::vector<Map_to_output>& map_sections(this->map_to_output());
401 map_sections.resize(shnum);
403 // Keep track of which sections to omit.
404 std::vector<bool> omit(shnum, false);
406 for (unsigned int i = 0; i < shnum; ++i)
408 typename This::Shdr shdr(pshdrs);
410 if (shdr.get_sh_name() >= names_size)
413 _("%s: %s: bad section name offset for section %u: %lu\n"),
414 program_name, this->name().c_str(), i,
415 static_cast<unsigned long>(shdr.get_sh_name()));
419 const char* name = pnames + shdr.get_sh_name();
421 bool discard = omit[i];
424 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
426 if (!this->include_section_group(layout, i, shdr, &omit))
429 else if (Layout::is_linkonce(name))
431 if (!this->include_linkonce_section(layout, name, shdr))
438 // Do not include this section in the link.
439 map_sections[i].output_section = NULL;
444 Output_section* os = layout->layout(this, name, shdr, &offset);
446 map_sections[i].output_section = os;
447 map_sections[i].offset = offset;
449 pshdrs += This::shdr_size;
453 // Finalize the local symbols. Here we record the file offset at
454 // which they should be output, we add their names to *POOL, and we
455 // add their values to THIS->VALUES_. Return the new file offset.
456 // This function is always called from the main thread. The actual
457 // output of the local symbols will occur in a separate task.
459 template<int size, bool big_endian>
461 Sized_object<size, big_endian>::do_finalize_local_symbols(off_t off,
464 if (this->symtab_shnum_ == 0)
466 // This object has no symbols. Weird but legal.
470 off = (off + (size >> 3) - 1) & ~ ((off_t) (size >> 3) - 1);
472 this->local_symbol_offset_ = off;
474 // Read the symbol table section header.
475 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
476 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
478 // Read the local symbols.
479 unsigned int loccount = symtabshdr.get_sh_info();
480 const int sym_size = This::sym_size;
481 off_t locsize = loccount * sym_size;
482 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
485 this->local_symbol_count_ = loccount;
487 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
489 // Read the section header for the symbol names.
490 typename This::Shdr strtabshdr(
491 this->section_header(symtabshdr.get_sh_link()));
492 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
494 // Read the symbol names.
495 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
496 strtabshdr.get_sh_size());
497 const char* pnames = reinterpret_cast<const char*>(pnamesu);
499 // Loop over the local symbols.
501 std::vector<Map_to_output>& mo(this->map_to_output());
502 unsigned int shnum = this->shnum();
503 unsigned int count = 0;
504 // Skip the first, dummy, symbol.
506 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
508 elfcpp::Sym<size, big_endian> sym(psyms);
510 unsigned int shndx = sym.get_st_shndx();
512 if (shndx >= elfcpp::SHN_LORESERVE)
514 if (shndx == elfcpp::SHN_ABS)
515 this->values_[i] = sym.get_st_value();
518 // FIXME: Handle SHN_XINDEX.
520 _("%s: %s: unknown section index %u "
521 "for local symbol %u\n"),
522 program_name, this->name().c_str(), shndx, i);
531 _("%s: %s: local symbol %u section index %u "
533 program_name, this->name().c_str(), i, shndx);
537 if (mo[shndx].output_section == NULL)
539 this->values_[i] = 0;
543 this->values_[i] = (mo[shndx].output_section->address()
544 + sym.get_st_value());
547 pool->add(pnames + sym.get_st_name());
552 this->output_local_symbol_count_ = count;
557 // Write out the local symbols.
559 template<int size, bool big_endian>
561 Sized_object<size, big_endian>::write_local_symbols(Output_file* of,
562 const Stringpool* sympool)
564 if (this->symtab_shnum_ == 0)
566 // This object has no symbols. Weird but legal.
570 // Read the symbol table section header.
571 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
572 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
573 unsigned int local_symbol_count = this->local_symbol_count_;
574 assert(local_symbol_count == symtabshdr.get_sh_info());
576 // Read the local symbols.
577 const int sym_size = This::sym_size;
578 off_t locsize = local_symbol_count * sym_size;
579 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
582 // Read the section header for the symbol names.
583 typename This::Shdr strtabshdr(
584 this->section_header(symtabshdr.get_sh_link()));
585 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
587 // Read the symbol names.
588 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
589 strtabshdr.get_sh_size());
590 const char* pnames = reinterpret_cast<const char*>(pnamesu);
592 // Get a view into the output file.
593 off_t output_size = this->output_local_symbol_count_ * sym_size;
594 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
597 std::vector<Map_to_output>& mo(this->map_to_output());
600 unsigned char* ov = oview;
601 for (unsigned int i = 1; i < local_symbol_count; ++i, psyms += sym_size)
603 elfcpp::Sym<size, big_endian> isym(psyms);
604 elfcpp::Sym_write<size, big_endian> osym(ov);
606 unsigned int st_shndx = isym.get_st_shndx();
607 if (st_shndx < elfcpp::SHN_LORESERVE)
609 assert(st_shndx < mo.size());
610 if (mo[st_shndx].output_section == NULL)
612 st_shndx = mo[st_shndx].output_section->shndx();
615 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
616 osym.put_st_value(this->values_[i]);
617 osym.put_st_size(isym.get_st_size());
618 osym.put_st_info(isym.get_st_info());
619 osym.put_st_other(isym.get_st_other());
620 osym.put_st_shndx(st_shndx);
625 assert(ov - oview == output_size);
627 of->write_output_view(this->local_symbol_offset_, output_size, oview);
630 // Input_objects methods.
633 Input_objects::add_object(Object* obj)
635 this->object_list_.push_back(obj);
637 Target* target = obj->target();
638 if (this->target_ == NULL)
639 this->target_ = target;
640 else if (this->target_ != target)
642 fprintf(stderr, "%s: %s: incompatible target\n",
643 program_name, obj->name().c_str());
647 if (obj->is_dynamic())
648 this->any_dynamic_ = true;
651 } // End namespace gold.
656 using namespace gold;
658 // Read an ELF file with the header and return the appropriate
659 // instance of Object.
661 template<int size, bool big_endian>
663 make_elf_sized_object(const std::string& name, Input_file* input_file,
664 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
666 int et = ehdr.get_e_type();
667 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
669 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
670 program_name, name.c_str(), static_cast<int>(et));
674 if (et == elfcpp::ET_REL)
676 Sized_object<size, big_endian>* obj =
677 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
684 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
685 program_name, name.c_str());
687 // Sized_dynobj<size, big_endian>* obj =
688 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
695 } // End anonymous namespace.
700 // Read an ELF file and return the appropriate instance of Object.
703 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
704 const unsigned char* p, off_t bytes)
706 if (bytes < elfcpp::EI_NIDENT)
708 fprintf(stderr, _("%s: %s: ELF file too short\n"),
709 program_name, name.c_str());
713 int v = p[elfcpp::EI_VERSION];
714 if (v != elfcpp::EV_CURRENT)
716 if (v == elfcpp::EV_NONE)
717 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
718 program_name, name.c_str());
720 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
721 program_name, name.c_str(), v);
725 int c = p[elfcpp::EI_CLASS];
726 if (c == elfcpp::ELFCLASSNONE)
728 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
729 program_name, name.c_str());
732 else if (c != elfcpp::ELFCLASS32
733 && c != elfcpp::ELFCLASS64)
735 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
736 program_name, name.c_str(), c);
740 int d = p[elfcpp::EI_DATA];
741 if (d == elfcpp::ELFDATANONE)
743 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
744 program_name, name.c_str());
747 else if (d != elfcpp::ELFDATA2LSB
748 && d != elfcpp::ELFDATA2MSB)
750 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
751 program_name, name.c_str(), d);
755 bool big_endian = d == elfcpp::ELFDATA2MSB;
757 if (c == elfcpp::ELFCLASS32)
759 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
761 fprintf(stderr, _("%s: %s: ELF file too short\n"),
762 program_name, name.c_str());
767 elfcpp::Ehdr<32, true> ehdr(p);
768 return make_elf_sized_object<32, true>(name, input_file,
773 elfcpp::Ehdr<32, false> ehdr(p);
774 return make_elf_sized_object<32, false>(name, input_file,
780 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
782 fprintf(stderr, _("%s: %s: ELF file too short\n"),
783 program_name, name.c_str());
788 elfcpp::Ehdr<64, true> ehdr(p);
789 return make_elf_sized_object<64, true>(name, input_file,
794 elfcpp::Ehdr<64, false> ehdr(p);
795 return make_elf_sized_object<64, false>(name, input_file,
801 // Instantiate the templates we need. We could use the configure
802 // script to restrict this to only the ones for implemented targets.
805 class Sized_object<32, false>;
808 class Sized_object<32, true>;
811 class Sized_object<64, false>;
814 class Sized_object<64, true>;
816 } // End namespace gold.