1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "target-select.h"
41 // Set the target based on fields in the ELF file header.
44 Object::set_target(int machine, int size, bool big_endian, int osabi,
47 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
50 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
51 program_name, this->name().c_str(), machine);
54 this->target_ = target;
57 // Report an error for the elfcpp::Elf_file interface.
60 Object::error(const char* format, ...)
64 fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
65 va_start(args, format);
66 vfprintf(stderr, format, args);
73 // Return a view of the contents of a section.
76 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
78 Location loc(this->do_section_contents(shndx));
79 *plen = loc.data_size;
80 return this->get_view(loc.file_offset, loc.data_size, cache);
83 // Read the section data into SD. This is code common to Sized_relobj
84 // and Sized_dynobj, so we put it into Object.
86 template<int size, bool big_endian>
88 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
89 Read_symbols_data* sd)
91 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
93 // Read the section headers.
94 const off_t shoff = elf_file->shoff();
95 const unsigned int shnum = this->shnum();
96 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
98 // Read the section names.
99 const unsigned char* pshdrs = sd->section_headers->data();
100 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
101 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
103 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
106 _("%s: %s: section name section has wrong type: %u\n"),
107 program_name, this->name().c_str(),
108 static_cast<unsigned int>(shdrnames.get_sh_type()));
112 sd->section_names_size = shdrnames.get_sh_size();
113 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
114 sd->section_names_size, false);
117 // If NAME is the name of a special .gnu.warning section, arrange for
118 // the warning to be issued. SHNDX is the section index. Return
119 // whether it is a warning section.
122 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
123 Symbol_table* symtab)
125 const char warn_prefix[] = ".gnu.warning.";
126 const int warn_prefix_len = sizeof warn_prefix - 1;
127 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
129 symtab->add_warning(name + warn_prefix_len, this, shndx);
135 // Class Sized_relobj.
137 template<int size, bool big_endian>
138 Sized_relobj<size, big_endian>::Sized_relobj(
139 const std::string& name,
140 Input_file* input_file,
142 const elfcpp::Ehdr<size, big_endian>& ehdr)
143 : Relobj(name, input_file, offset),
144 elf_file_(this, ehdr),
146 local_symbol_count_(0),
147 output_local_symbol_count_(0),
149 local_symbol_offset_(0),
155 template<int size, bool big_endian>
156 Sized_relobj<size, big_endian>::~Sized_relobj()
160 // Set up an object file based on the file header. This sets up the
161 // target and reads the section information.
163 template<int size, bool big_endian>
165 Sized_relobj<size, big_endian>::setup(
166 const elfcpp::Ehdr<size, big_endian>& ehdr)
168 this->set_target(ehdr.get_e_machine(), size, big_endian,
169 ehdr.get_e_ident()[elfcpp::EI_OSABI],
170 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
172 const unsigned int shnum = this->elf_file_.shnum();
173 this->set_shnum(shnum);
176 // Find the SHT_SYMTAB section, given the section headers. The ELF
177 // standard says that maybe in the future there can be more than one
178 // SHT_SYMTAB section. Until somebody figures out how that could
179 // work, we assume there is only one.
181 template<int size, bool big_endian>
183 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
185 const unsigned int shnum = this->shnum();
186 this->symtab_shndx_ = 0;
189 // Look through the sections in reverse order, since gas tends
190 // to put the symbol table at the end.
191 const unsigned char* p = pshdrs + shnum * This::shdr_size;
192 unsigned int i = shnum;
196 p -= This::shdr_size;
197 typename This::Shdr shdr(p);
198 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
200 this->symtab_shndx_ = i;
207 // Read the sections and symbols from an object file.
209 template<int size, bool big_endian>
211 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
213 this->read_section_data(&this->elf_file_, sd);
215 const unsigned char* const pshdrs = sd->section_headers->data();
217 this->find_symtab(pshdrs);
219 if (this->symtab_shndx_ == 0)
221 // No symbol table. Weird but legal.
223 sd->symbols_size = 0;
224 sd->symbol_names = NULL;
225 sd->symbol_names_size = 0;
229 // Get the symbol table section header.
230 typename This::Shdr symtabshdr(pshdrs
231 + this->symtab_shndx_ * This::shdr_size);
232 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
234 // We only need the external symbols.
235 const int sym_size = This::sym_size;
236 const unsigned int loccount = symtabshdr.get_sh_info();
237 this->local_symbol_count_ = loccount;
238 off_t locsize = loccount * sym_size;
239 off_t extoff = symtabshdr.get_sh_offset() + locsize;
240 off_t extsize = symtabshdr.get_sh_size() - locsize;
242 // Read the symbol table.
243 File_view* fvsymtab = this->get_lasting_view(extoff, extsize, false);
245 // Read the section header for the symbol names.
246 unsigned int strtab_shndx = symtabshdr.get_sh_link();
247 if (strtab_shndx >= this->shnum())
249 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
250 program_name, this->name().c_str(), strtab_shndx);
253 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
254 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
257 _("%s: %s: symbol table name section has wrong type: %u\n"),
258 program_name, this->name().c_str(),
259 static_cast<unsigned int>(strtabshdr.get_sh_type()));
263 // Read the symbol names.
264 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
265 strtabshdr.get_sh_size(), true);
267 sd->symbols = fvsymtab;
268 sd->symbols_size = extsize;
269 sd->symbol_names = fvstrtab;
270 sd->symbol_names_size = strtabshdr.get_sh_size();
273 // Return whether to include a section group in the link. LAYOUT is
274 // used to keep track of which section groups we have already seen.
275 // INDEX is the index of the section group and SHDR is the section
276 // header. If we do not want to include this group, we set bits in
277 // OMIT for each section which should be discarded.
279 template<int size, bool big_endian>
281 Sized_relobj<size, big_endian>::include_section_group(
284 const elfcpp::Shdr<size, big_endian>& shdr,
285 std::vector<bool>* omit)
287 // Read the section contents.
288 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
289 shdr.get_sh_size(), false);
290 const elfcpp::Elf_Word* pword =
291 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
293 // The first word contains flags. We only care about COMDAT section
294 // groups. Other section groups are always included in the link
295 // just like ordinary sections.
296 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
297 if ((flags & elfcpp::GRP_COMDAT) == 0)
300 // Look up the group signature, which is the name of a symbol. This
301 // is a lot of effort to go to to read a string. Why didn't they
302 // just use the name of the SHT_GROUP section as the group
305 // Get the appropriate symbol table header (this will normally be
306 // the single SHT_SYMTAB section, but in principle it need not be).
307 const unsigned int link = shdr.get_sh_link();
308 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
310 // Read the symbol table entry.
311 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
313 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
314 program_name, this->name().c_str(), index, shdr.get_sh_info());
317 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
318 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
319 elfcpp::Sym<size, big_endian> sym(psym);
321 // Read the symbol table names.
323 const unsigned char* psymnamesu;
324 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
326 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
328 // Get the section group signature.
329 if (sym.get_st_name() >= symnamelen)
331 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
332 program_name, this->name().c_str(), shdr.get_sh_info(),
337 const char* signature = psymnames + sym.get_st_name();
339 // It seems that some versions of gas will create a section group
340 // associated with a section symbol, and then fail to give a name to
341 // the section symbol. In such a case, use the name of the section.
344 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
346 secname = this->section_name(sym.get_st_shndx());
347 signature = secname.c_str();
350 // Record this section group, and see whether we've already seen one
351 // with the same signature.
352 if (layout->add_comdat(signature, true))
355 // This is a duplicate. We want to discard the sections in this
357 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
358 for (size_t i = 1; i < count; ++i)
360 elfcpp::Elf_Word secnum =
361 elfcpp::Swap<32, big_endian>::readval(pword + i);
362 if (secnum >= this->shnum())
365 _("%s: %s: section %u in section group %u out of range"),
366 program_name, this->name().c_str(), secnum,
370 (*omit)[secnum] = true;
376 // Whether to include a linkonce section in the link. NAME is the
377 // name of the section and SHDR is the section header.
379 // Linkonce sections are a GNU extension implemented in the original
380 // GNU linker before section groups were defined. The semantics are
381 // that we only include one linkonce section with a given name. The
382 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
383 // where T is the type of section and SYMNAME is the name of a symbol.
384 // In an attempt to make linkonce sections interact well with section
385 // groups, we try to identify SYMNAME and use it like a section group
386 // signature. We want to block section groups with that signature,
387 // but not other linkonce sections with that signature. We also use
388 // the full name of the linkonce section as a normal section group
391 template<int size, bool big_endian>
393 Sized_relobj<size, big_endian>::include_linkonce_section(
396 const elfcpp::Shdr<size, big_endian>&)
398 const char* symname = strrchr(name, '.') + 1;
399 bool include1 = layout->add_comdat(symname, false);
400 bool include2 = layout->add_comdat(name, true);
401 return include1 && include2;
404 // Lay out the input sections. We walk through the sections and check
405 // whether they should be included in the link. If they should, we
406 // pass them to the Layout object, which will return an output section
409 template<int size, bool big_endian>
411 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
413 Read_symbols_data* sd)
415 const unsigned int shnum = this->shnum();
419 // Get the section headers.
420 const unsigned char* pshdrs = sd->section_headers->data();
422 // Get the section names.
423 const unsigned char* pnamesu = sd->section_names->data();
424 const char* pnames = reinterpret_cast<const char*>(pnamesu);
426 std::vector<Map_to_output>& map_sections(this->map_to_output());
427 map_sections.resize(shnum);
429 // Keep track of which sections to omit.
430 std::vector<bool> omit(shnum, false);
432 // Skip the first, dummy, section.
433 pshdrs += This::shdr_size;
434 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
436 typename This::Shdr shdr(pshdrs);
438 if (shdr.get_sh_name() >= sd->section_names_size)
441 _("%s: %s: bad section name offset for section %u: %lu\n"),
442 program_name, this->name().c_str(), i,
443 static_cast<unsigned long>(shdr.get_sh_name()));
447 const char* name = pnames + shdr.get_sh_name();
449 if (this->handle_gnu_warning_section(name, i, symtab))
451 if (!parameters->output_is_object())
455 bool discard = omit[i];
458 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
460 if (!this->include_section_group(layout, i, shdr, &omit))
463 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
464 && Layout::is_linkonce(name))
466 if (!this->include_linkonce_section(layout, name, shdr))
473 // Do not include this section in the link.
474 map_sections[i].output_section = NULL;
479 Output_section* os = layout->layout(this, i, name, shdr, &offset);
481 map_sections[i].output_section = os;
482 map_sections[i].offset = offset;
485 delete sd->section_headers;
486 sd->section_headers = NULL;
487 delete sd->section_names;
488 sd->section_names = NULL;
491 // Add the symbols to the symbol table.
493 template<int size, bool big_endian>
495 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
496 Read_symbols_data* sd)
498 if (sd->symbols == NULL)
500 gold_assert(sd->symbol_names == NULL);
504 const int sym_size = This::sym_size;
505 size_t symcount = sd->symbols_size / sym_size;
506 if (static_cast<off_t>(symcount * sym_size) != sd->symbols_size)
509 _("%s: %s: size of symbols is not multiple of symbol size\n"),
510 program_name, this->name().c_str());
514 this->symbols_ = new Symbol*[symcount];
516 const char* sym_names =
517 reinterpret_cast<const char*>(sd->symbol_names->data());
518 symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
519 sd->symbol_names_size, this->symbols_);
523 delete sd->symbol_names;
524 sd->symbol_names = NULL;
527 // Finalize the local symbols. Here we record the file offset at
528 // which they should be output, we add their names to *POOL, and we
529 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
530 // This function is always called from the main thread. The actual
531 // output of the local symbols will occur in a separate task.
533 template<int size, bool big_endian>
535 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
539 gold_assert(this->symtab_shndx_ != -1U);
540 if (this->symtab_shndx_ == 0)
542 // This object has no symbols. Weird but legal.
546 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
548 this->local_symbol_offset_ = off;
550 // Read the symbol table section header.
551 const unsigned int symtab_shndx = this->symtab_shndx_;
552 typename This::Shdr symtabshdr(this,
553 this->elf_file_.section_header(symtab_shndx));
554 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
556 // Read the local symbols.
557 const int sym_size = This::sym_size;
558 const unsigned int loccount = this->local_symbol_count_;
559 gold_assert(loccount == symtabshdr.get_sh_info());
560 off_t locsize = loccount * sym_size;
561 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
564 this->local_values_.resize(loccount);
566 // Read the symbol names.
567 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
569 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
572 const char* pnames = reinterpret_cast<const char*>(pnamesu);
574 // Loop over the local symbols.
576 const std::vector<Map_to_output>& mo(this->map_to_output());
577 unsigned int shnum = this->shnum();
578 unsigned int count = 0;
579 // Skip the first, dummy, symbol.
581 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
583 elfcpp::Sym<size, big_endian> sym(psyms);
585 Symbol_value<size>& lv(this->local_values_[i]);
587 unsigned int shndx = sym.get_st_shndx();
588 lv.set_input_shndx(shndx);
590 if (sym.get_st_type() == elfcpp::STT_SECTION)
591 lv.set_is_section_symbol();
593 if (shndx >= elfcpp::SHN_LORESERVE)
595 if (shndx == elfcpp::SHN_ABS)
596 lv.set_output_value(sym.get_st_value());
599 // FIXME: Handle SHN_XINDEX.
601 _("%s: %s: unknown section index %u "
602 "for local symbol %u\n"),
603 program_name, this->name().c_str(), shndx, i);
612 _("%s: %s: local symbol %u section index %u "
614 program_name, this->name().c_str(), i, shndx);
618 Output_section* os = mo[shndx].output_section;
622 lv.set_output_value(0);
623 lv.set_no_output_symtab_entry();
627 if (mo[shndx].offset == -1)
628 lv.set_input_value(sym.get_st_value());
630 lv.set_output_value(mo[shndx].output_section->address()
632 + sym.get_st_value());
635 // Decide whether this symbol should go into the output file.
637 if (sym.get_st_type() == elfcpp::STT_SECTION)
639 lv.set_no_output_symtab_entry();
643 if (sym.get_st_name() >= strtab_size)
646 _("%s: %s: local symbol %u section name "
647 "out of range: %u >= %u\n"),
648 program_name, this->name().c_str(),
649 i, sym.get_st_name(),
650 static_cast<unsigned int>(strtab_size));
654 const char* name = pnames + sym.get_st_name();
655 pool->add(name, NULL);
656 lv.set_output_symtab_index(index);
661 this->output_local_symbol_count_ = count;
666 // Return the value of the local symbol symndx.
667 template<int size, bool big_endian>
668 typename elfcpp::Elf_types<size>::Elf_Addr
669 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
671 gold_assert(symndx < this->local_symbol_count_);
672 gold_assert(symndx < this->local_values_.size());
673 const Symbol_value<size>& lv(this->local_values_[symndx]);
674 return lv.value(this, 0);
677 // Return the value of a local symbol defined in input section SHNDX,
678 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
679 // indicates whether the symbol is a section symbol. This handles
680 // SHF_MERGE sections.
681 template<int size, bool big_endian>
682 typename elfcpp::Elf_types<size>::Elf_Addr
683 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
685 bool is_section_symbol,
686 Address addend) const
688 const std::vector<Map_to_output>& mo(this->map_to_output());
689 Output_section* os = mo[shndx].output_section;
692 gold_assert(mo[shndx].offset == -1);
694 // Do the mapping required by the output section. If this is not a
695 // section symbol, then we want to map the symbol value, and then
696 // include the addend. If this is a section symbol, then we need to
697 // include the addend to figure out where in the section we are,
698 // before we do the mapping. This will do the right thing provided
699 // the assembler is careful to only convert a relocation in a merged
700 // section to a section symbol if there is a zero addend. If the
701 // assembler does not do this, then in general we can't know what to
702 // do, because we can't distinguish the addend for the instruction
703 // format from the addend for the section offset.
705 if (is_section_symbol)
706 return os->output_address(this, shndx, value + addend);
708 return addend + os->output_address(this, shndx, value);
711 // Write out the local symbols.
713 template<int size, bool big_endian>
715 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
716 const Stringpool* sympool)
718 gold_assert(this->symtab_shndx_ != -1U);
719 if (this->symtab_shndx_ == 0)
721 // This object has no symbols. Weird but legal.
725 // Read the symbol table section header.
726 const unsigned int symtab_shndx = this->symtab_shndx_;
727 typename This::Shdr symtabshdr(this,
728 this->elf_file_.section_header(symtab_shndx));
729 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
730 const unsigned int loccount = this->local_symbol_count_;
731 gold_assert(loccount == symtabshdr.get_sh_info());
733 // Read the local symbols.
734 const int sym_size = This::sym_size;
735 off_t locsize = loccount * sym_size;
736 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
739 // Read the symbol names.
740 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
742 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
745 const char* pnames = reinterpret_cast<const char*>(pnamesu);
747 // Get a view into the output file.
748 off_t output_size = this->output_local_symbol_count_ * sym_size;
749 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
752 const std::vector<Map_to_output>& mo(this->map_to_output());
754 gold_assert(this->local_values_.size() == loccount);
756 unsigned char* ov = oview;
758 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
760 elfcpp::Sym<size, big_endian> isym(psyms);
762 if (!this->local_values_[i].needs_output_symtab_entry())
765 unsigned int st_shndx = isym.get_st_shndx();
766 if (st_shndx < elfcpp::SHN_LORESERVE)
768 gold_assert(st_shndx < mo.size());
769 if (mo[st_shndx].output_section == NULL)
771 st_shndx = mo[st_shndx].output_section->out_shndx();
774 elfcpp::Sym_write<size, big_endian> osym(ov);
776 gold_assert(isym.get_st_name() < strtab_size);
777 const char* name = pnames + isym.get_st_name();
778 osym.put_st_name(sympool->get_offset(name));
779 osym.put_st_value(this->local_values_[i].value(this, 0));
780 osym.put_st_size(isym.get_st_size());
781 osym.put_st_info(isym.get_st_info());
782 osym.put_st_other(isym.get_st_other());
783 osym.put_st_shndx(st_shndx);
788 gold_assert(ov - oview == output_size);
790 of->write_output_view(this->local_symbol_offset_, output_size, oview);
793 // Input_objects methods.
795 // Add a regular relocatable object to the list. Return false if this
796 // object should be ignored.
799 Input_objects::add_object(Object* obj)
801 if (!obj->is_dynamic())
802 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
805 // See if this is a duplicate SONAME.
806 Dynobj* dynobj = static_cast<Dynobj*>(obj);
808 std::pair<Unordered_set<std::string>::iterator, bool> ins =
809 this->sonames_.insert(dynobj->soname());
812 // We have already seen a dynamic object with this soname.
816 this->dynobj_list_.push_back(dynobj);
819 Target* target = obj->target();
820 if (this->target_ == NULL)
821 this->target_ = target;
822 else if (this->target_ != target)
824 fprintf(stderr, "%s: %s: incompatible target\n",
825 program_name, obj->name().c_str());
829 set_parameters_size_and_endianness(target->get_size(),
830 target->is_big_endian());
835 // Relocate_info methods.
837 // Return a string describing the location of a relocation. This is
838 // only used in error messages.
840 template<int size, bool big_endian>
842 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
844 std::string ret(this->object->name());
847 snprintf(buf, sizeof buf, "%zu", relnum);
849 ret += " in reloc section ";
850 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
852 ret += " (" + this->object->section_name(this->reloc_shndx);
853 ret += ") for section ";
854 snprintf(buf, sizeof buf, "%u", this->data_shndx);
856 ret += " (" + this->object->section_name(this->data_shndx) + ")";
860 } // End namespace gold.
865 using namespace gold;
867 // Read an ELF file with the header and return the appropriate
868 // instance of Object.
870 template<int size, bool big_endian>
872 make_elf_sized_object(const std::string& name, Input_file* input_file,
873 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
875 int et = ehdr.get_e_type();
876 if (et == elfcpp::ET_REL)
878 Sized_relobj<size, big_endian>* obj =
879 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
883 else if (et == elfcpp::ET_DYN)
885 Sized_dynobj<size, big_endian>* obj =
886 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
892 fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
893 program_name, name.c_str(), et);
898 } // End anonymous namespace.
903 // Read an ELF file and return the appropriate instance of Object.
906 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
907 const unsigned char* p, off_t bytes)
909 if (bytes < elfcpp::EI_NIDENT)
911 fprintf(stderr, _("%s: %s: ELF file too short\n"),
912 program_name, name.c_str());
916 int v = p[elfcpp::EI_VERSION];
917 if (v != elfcpp::EV_CURRENT)
919 if (v == elfcpp::EV_NONE)
920 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
921 program_name, name.c_str());
923 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
924 program_name, name.c_str(), v);
928 int c = p[elfcpp::EI_CLASS];
929 if (c == elfcpp::ELFCLASSNONE)
931 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
932 program_name, name.c_str());
935 else if (c != elfcpp::ELFCLASS32
936 && c != elfcpp::ELFCLASS64)
938 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
939 program_name, name.c_str(), c);
943 int d = p[elfcpp::EI_DATA];
944 if (d == elfcpp::ELFDATANONE)
946 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
947 program_name, name.c_str());
950 else if (d != elfcpp::ELFDATA2LSB
951 && d != elfcpp::ELFDATA2MSB)
953 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
954 program_name, name.c_str(), d);
958 bool big_endian = d == elfcpp::ELFDATA2MSB;
960 if (c == elfcpp::ELFCLASS32)
962 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
964 fprintf(stderr, _("%s: %s: ELF file too short\n"),
965 program_name, name.c_str());
970 #ifdef HAVE_TARGET_32_BIG
971 elfcpp::Ehdr<32, true> ehdr(p);
972 return make_elf_sized_object<32, true>(name, input_file,
976 _("%s: %s: not configured to support 32-bit big-endian object\n"),
977 program_name, name.c_str());
983 #ifdef HAVE_TARGET_32_LITTLE
984 elfcpp::Ehdr<32, false> ehdr(p);
985 return make_elf_sized_object<32, false>(name, input_file,
989 _("%s: %s: not configured to support 32-bit little-endian object\n"),
990 program_name, name.c_str());
997 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
999 fprintf(stderr, _("%s: %s: ELF file too short\n"),
1000 program_name, name.c_str());
1005 #ifdef HAVE_TARGET_64_BIG
1006 elfcpp::Ehdr<64, true> ehdr(p);
1007 return make_elf_sized_object<64, true>(name, input_file,
1011 _("%s: %s: not configured to support 64-bit big-endian object\n"),
1012 program_name, name.c_str());
1018 #ifdef HAVE_TARGET_64_LITTLE
1019 elfcpp::Ehdr<64, false> ehdr(p);
1020 return make_elf_sized_object<64, false>(name, input_file,
1024 _("%s: %s: not configured to support 64-bit little-endian object\n"),
1025 program_name, name.c_str());
1032 // Instantiate the templates we need. We could use the configure
1033 // script to restrict this to only the ones for implemented targets.
1035 #ifdef HAVE_TARGET_32_LITTLE
1037 class Sized_relobj<32, false>;
1040 #ifdef HAVE_TARGET_32_BIG
1042 class Sized_relobj<32, true>;
1045 #ifdef HAVE_TARGET_64_LITTLE
1047 class Sized_relobj<64, false>;
1050 #ifdef HAVE_TARGET_64_BIG
1052 class Sized_relobj<64, true>;
1055 #ifdef HAVE_TARGET_32_LITTLE
1057 struct Relocate_info<32, false>;
1060 #ifdef HAVE_TARGET_32_BIG
1062 struct Relocate_info<32, true>;
1065 #ifdef HAVE_TARGET_64_LITTLE
1067 struct Relocate_info<64, false>;
1070 #ifdef HAVE_TARGET_64_BIG
1072 struct Relocate_info<64, true>;
1075 } // End namespace gold.