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);
49 gold_fatal(_("%s: unsupported ELF machine number %d"),
50 this->name().c_str(), machine);
51 this->target_ = target;
54 // Report an error for this object file. This is used by the
55 // elfcpp::Elf_file interface, and also called by the Object code
59 Object::error(const char* format, ...) const
62 va_start(args, format);
64 if (vasprintf(&buf, format, args) < 0)
67 gold_error(_("%s: %s"), this->name().c_str(), buf);
71 // Return a view of the contents of a section.
74 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
76 Location loc(this->do_section_contents(shndx));
77 *plen = loc.data_size;
78 return this->get_view(loc.file_offset, loc.data_size, cache);
81 // Read the section data into SD. This is code common to Sized_relobj
82 // and Sized_dynobj, so we put it into Object.
84 template<int size, bool big_endian>
86 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
87 Read_symbols_data* sd)
89 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
91 // Read the section headers.
92 const off_t shoff = elf_file->shoff();
93 const unsigned int shnum = this->shnum();
94 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
96 // Read the section names.
97 const unsigned char* pshdrs = sd->section_headers->data();
98 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
99 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
101 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
102 this->error(_("section name section has wrong type: %u"),
103 static_cast<unsigned int>(shdrnames.get_sh_type()));
105 sd->section_names_size = shdrnames.get_sh_size();
106 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
107 sd->section_names_size, false);
110 // If NAME is the name of a special .gnu.warning section, arrange for
111 // the warning to be issued. SHNDX is the section index. Return
112 // whether it is a warning section.
115 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
116 Symbol_table* symtab)
118 const char warn_prefix[] = ".gnu.warning.";
119 const int warn_prefix_len = sizeof warn_prefix - 1;
120 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
122 symtab->add_warning(name + warn_prefix_len, this, shndx);
128 // Class Sized_relobj.
130 template<int size, bool big_endian>
131 Sized_relobj<size, big_endian>::Sized_relobj(
132 const std::string& name,
133 Input_file* input_file,
135 const elfcpp::Ehdr<size, big_endian>& ehdr)
136 : Relobj(name, input_file, offset),
137 elf_file_(this, ehdr),
139 local_symbol_count_(0),
140 output_local_symbol_count_(0),
142 local_symbol_offset_(0),
148 template<int size, bool big_endian>
149 Sized_relobj<size, big_endian>::~Sized_relobj()
153 // Set up an object file based on the file header. This sets up the
154 // target and reads the section information.
156 template<int size, bool big_endian>
158 Sized_relobj<size, big_endian>::setup(
159 const elfcpp::Ehdr<size, big_endian>& ehdr)
161 this->set_target(ehdr.get_e_machine(), size, big_endian,
162 ehdr.get_e_ident()[elfcpp::EI_OSABI],
163 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
165 const unsigned int shnum = this->elf_file_.shnum();
166 this->set_shnum(shnum);
169 // Find the SHT_SYMTAB section, given the section headers. The ELF
170 // standard says that maybe in the future there can be more than one
171 // SHT_SYMTAB section. Until somebody figures out how that could
172 // work, we assume there is only one.
174 template<int size, bool big_endian>
176 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
178 const unsigned int shnum = this->shnum();
179 this->symtab_shndx_ = 0;
182 // Look through the sections in reverse order, since gas tends
183 // to put the symbol table at the end.
184 const unsigned char* p = pshdrs + shnum * This::shdr_size;
185 unsigned int i = shnum;
189 p -= This::shdr_size;
190 typename This::Shdr shdr(p);
191 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
193 this->symtab_shndx_ = i;
200 // Read the sections and symbols from an object file.
202 template<int size, bool big_endian>
204 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
206 this->read_section_data(&this->elf_file_, sd);
208 const unsigned char* const pshdrs = sd->section_headers->data();
210 this->find_symtab(pshdrs);
213 sd->symbols_size = 0;
214 sd->symbol_names = NULL;
215 sd->symbol_names_size = 0;
217 if (this->symtab_shndx_ == 0)
219 // No symbol table. Weird but legal.
223 // Get the symbol table section header.
224 typename This::Shdr symtabshdr(pshdrs
225 + this->symtab_shndx_ * This::shdr_size);
226 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
228 // We only need the external symbols.
229 const int sym_size = This::sym_size;
230 const unsigned int loccount = symtabshdr.get_sh_info();
231 this->local_symbol_count_ = loccount;
232 off_t locsize = loccount * sym_size;
233 off_t extoff = symtabshdr.get_sh_offset() + locsize;
234 off_t extsize = symtabshdr.get_sh_size() - locsize;
236 // Read the symbol table.
237 File_view* fvsymtab = this->get_lasting_view(extoff, extsize, false);
239 // Read the section header for the symbol names.
240 unsigned int strtab_shndx = symtabshdr.get_sh_link();
241 if (strtab_shndx >= this->shnum())
243 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
246 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
247 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
249 this->error(_("symbol table name section has wrong type: %u"),
250 static_cast<unsigned int>(strtabshdr.get_sh_type()));
254 // Read the symbol names.
255 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
256 strtabshdr.get_sh_size(), true);
258 sd->symbols = fvsymtab;
259 sd->symbols_size = extsize;
260 sd->symbol_names = fvstrtab;
261 sd->symbol_names_size = strtabshdr.get_sh_size();
264 // Return whether to include a section group in the link. LAYOUT is
265 // used to keep track of which section groups we have already seen.
266 // INDEX is the index of the section group and SHDR is the section
267 // header. If we do not want to include this group, we set bits in
268 // OMIT for each section which should be discarded.
270 template<int size, bool big_endian>
272 Sized_relobj<size, big_endian>::include_section_group(
275 const elfcpp::Shdr<size, big_endian>& shdr,
276 std::vector<bool>* omit)
278 // Read the section contents.
279 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
280 shdr.get_sh_size(), false);
281 const elfcpp::Elf_Word* pword =
282 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
284 // The first word contains flags. We only care about COMDAT section
285 // groups. Other section groups are always included in the link
286 // just like ordinary sections.
287 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
288 if ((flags & elfcpp::GRP_COMDAT) == 0)
291 // Look up the group signature, which is the name of a symbol. This
292 // is a lot of effort to go to to read a string. Why didn't they
293 // just use the name of the SHT_GROUP section as the group
296 // Get the appropriate symbol table header (this will normally be
297 // the single SHT_SYMTAB section, but in principle it need not be).
298 const unsigned int link = shdr.get_sh_link();
299 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
301 // Read the symbol table entry.
302 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
304 this->error(_("section group %u info %u out of range"),
305 index, shdr.get_sh_info());
308 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
309 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
310 elfcpp::Sym<size, big_endian> sym(psym);
312 // Read the symbol table names.
314 const unsigned char* psymnamesu;
315 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
317 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
319 // Get the section group signature.
320 if (sym.get_st_name() >= symnamelen)
322 this->error(_("symbol %u name offset %u out of range"),
323 shdr.get_sh_info(), sym.get_st_name());
327 const char* signature = psymnames + sym.get_st_name();
329 // It seems that some versions of gas will create a section group
330 // associated with a section symbol, and then fail to give a name to
331 // the section symbol. In such a case, use the name of the section.
334 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
336 secname = this->section_name(sym.get_st_shndx());
337 signature = secname.c_str();
340 // Record this section group, and see whether we've already seen one
341 // with the same signature.
342 if (layout->add_comdat(signature, true))
345 // This is a duplicate. We want to discard the sections in this
347 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
348 for (size_t i = 1; i < count; ++i)
350 elfcpp::Elf_Word secnum =
351 elfcpp::Swap<32, big_endian>::readval(pword + i);
352 if (secnum >= this->shnum())
354 this->error(_("section %u in section group %u out of range"),
358 (*omit)[secnum] = true;
364 // Whether to include a linkonce section in the link. NAME is the
365 // name of the section and SHDR is the section header.
367 // Linkonce sections are a GNU extension implemented in the original
368 // GNU linker before section groups were defined. The semantics are
369 // that we only include one linkonce section with a given name. The
370 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
371 // where T is the type of section and SYMNAME is the name of a symbol.
372 // In an attempt to make linkonce sections interact well with section
373 // groups, we try to identify SYMNAME and use it like a section group
374 // signature. We want to block section groups with that signature,
375 // but not other linkonce sections with that signature. We also use
376 // the full name of the linkonce section as a normal section group
379 template<int size, bool big_endian>
381 Sized_relobj<size, big_endian>::include_linkonce_section(
384 const elfcpp::Shdr<size, big_endian>&)
386 // In general the symbol name we want will be the string following
387 // the last '.'. However, we have to handle the case of
388 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
389 // some versions of gcc. So we use a heuristic: if the name starts
390 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
391 // we look for the last '.'. We can't always simply skip
392 // ".gnu.linkonce.X", because we have to deal with cases like
393 // ".gnu.linkonce.d.rel.ro.local".
394 const char* const linkonce_t = ".gnu.linkonce.t.";
396 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
397 symname = name + strlen(linkonce_t);
399 symname = strrchr(name, '.') + 1;
400 bool include1 = layout->add_comdat(symname, false);
401 bool include2 = layout->add_comdat(name, true);
402 return include1 && include2;
405 // Lay out the input sections. We walk through the sections and check
406 // whether they should be included in the link. If they should, we
407 // pass them to the Layout object, which will return an output section
410 template<int size, bool big_endian>
412 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
414 Read_symbols_data* sd)
416 const unsigned int shnum = this->shnum();
420 // Get the section headers.
421 const unsigned char* pshdrs = sd->section_headers->data();
423 // Get the section names.
424 const unsigned char* pnamesu = sd->section_names->data();
425 const char* pnames = reinterpret_cast<const char*>(pnamesu);
427 std::vector<Map_to_output>& map_sections(this->map_to_output());
428 map_sections.resize(shnum);
430 // Whether we've seen a .note.GNU-stack section.
431 bool seen_gnu_stack = false;
432 // The flags of a .note.GNU-stack section.
433 uint64_t gnu_stack_flags = 0;
435 // Keep track of which sections to omit.
436 std::vector<bool> omit(shnum, false);
438 // Skip the first, dummy, section.
439 pshdrs += This::shdr_size;
440 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
442 typename This::Shdr shdr(pshdrs);
444 if (shdr.get_sh_name() >= sd->section_names_size)
446 this->error(_("bad section name offset for section %u: %lu"),
447 i, static_cast<unsigned long>(shdr.get_sh_name()));
451 const char* name = pnames + shdr.get_sh_name();
453 if (this->handle_gnu_warning_section(name, i, symtab))
455 if (!parameters->output_is_object())
459 // The .note.GNU-stack section is special. It gives the
460 // protection flags that this object file requires for the stack
462 if (strcmp(name, ".note.GNU-stack") == 0)
464 seen_gnu_stack = true;
465 gnu_stack_flags |= shdr.get_sh_flags();
469 bool discard = omit[i];
472 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
474 if (!this->include_section_group(layout, i, shdr, &omit))
477 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
478 && Layout::is_linkonce(name))
480 if (!this->include_linkonce_section(layout, name, shdr))
487 // Do not include this section in the link.
488 map_sections[i].output_section = NULL;
493 Output_section* os = layout->layout(this, i, name, shdr, &offset);
495 map_sections[i].output_section = os;
496 map_sections[i].offset = offset;
499 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
501 delete sd->section_headers;
502 sd->section_headers = NULL;
503 delete sd->section_names;
504 sd->section_names = NULL;
507 // Add the symbols to the symbol table.
509 template<int size, bool big_endian>
511 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
512 Read_symbols_data* sd)
514 if (sd->symbols == NULL)
516 gold_assert(sd->symbol_names == NULL);
520 const int sym_size = This::sym_size;
521 size_t symcount = sd->symbols_size / sym_size;
522 if (static_cast<off_t>(symcount * sym_size) != sd->symbols_size)
524 this->error(_("size of symbols is not multiple of symbol size"));
528 this->symbols_ = new Symbol*[symcount];
530 const char* sym_names =
531 reinterpret_cast<const char*>(sd->symbol_names->data());
532 symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
533 sd->symbol_names_size, this->symbols_);
537 delete sd->symbol_names;
538 sd->symbol_names = NULL;
541 // Finalize the local symbols. Here we record the file offset at
542 // which they should be output, we add their names to *POOL, and we
543 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
544 // This function is always called from the main thread. The actual
545 // output of the local symbols will occur in a separate task.
547 template<int size, bool big_endian>
549 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
553 gold_assert(this->symtab_shndx_ != -1U);
554 if (this->symtab_shndx_ == 0)
556 // This object has no symbols. Weird but legal.
560 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
562 this->local_symbol_offset_ = off;
564 // Read the symbol table section header.
565 const unsigned int symtab_shndx = this->symtab_shndx_;
566 typename This::Shdr symtabshdr(this,
567 this->elf_file_.section_header(symtab_shndx));
568 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
570 // Read the local symbols.
571 const int sym_size = This::sym_size;
572 const unsigned int loccount = this->local_symbol_count_;
573 gold_assert(loccount == symtabshdr.get_sh_info());
574 off_t locsize = loccount * sym_size;
575 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
578 this->local_values_.resize(loccount);
580 // Read the symbol names.
581 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
583 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
586 const char* pnames = reinterpret_cast<const char*>(pnamesu);
588 // Loop over the local symbols.
590 const std::vector<Map_to_output>& mo(this->map_to_output());
591 unsigned int shnum = this->shnum();
592 unsigned int count = 0;
593 // Skip the first, dummy, symbol.
595 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
597 elfcpp::Sym<size, big_endian> sym(psyms);
599 Symbol_value<size>& lv(this->local_values_[i]);
601 unsigned int shndx = sym.get_st_shndx();
602 lv.set_input_shndx(shndx);
604 if (sym.get_st_type() == elfcpp::STT_SECTION)
605 lv.set_is_section_symbol();
607 if (shndx >= elfcpp::SHN_LORESERVE)
609 if (shndx == elfcpp::SHN_ABS)
610 lv.set_output_value(sym.get_st_value());
613 // FIXME: Handle SHN_XINDEX.
614 this->error(_("unknown section index %u for local symbol %u"),
616 lv.set_output_value(0);
623 this->error(_("local symbol %u section index %u out of range"),
628 Output_section* os = mo[shndx].output_section;
632 lv.set_output_value(0);
633 lv.set_no_output_symtab_entry();
637 if (mo[shndx].offset == -1)
638 lv.set_input_value(sym.get_st_value());
640 lv.set_output_value(mo[shndx].output_section->address()
642 + sym.get_st_value());
645 // Decide whether this symbol should go into the output file.
647 if (sym.get_st_type() == elfcpp::STT_SECTION)
649 lv.set_no_output_symtab_entry();
653 if (sym.get_st_name() >= strtab_size)
655 this->error(_("local symbol %u section name out of range: %u >= %u"),
656 i, sym.get_st_name(),
657 static_cast<unsigned int>(strtab_size));
658 lv.set_no_output_symtab_entry();
662 const char* name = pnames + sym.get_st_name();
663 pool->add(name, true, NULL);
664 lv.set_output_symtab_index(index);
669 this->output_local_symbol_count_ = count;
674 // Return the value of the local symbol symndx.
675 template<int size, bool big_endian>
676 typename elfcpp::Elf_types<size>::Elf_Addr
677 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
679 gold_assert(symndx < this->local_symbol_count_);
680 gold_assert(symndx < this->local_values_.size());
681 const Symbol_value<size>& lv(this->local_values_[symndx]);
682 return lv.value(this, 0);
685 // Return the value of a local symbol defined in input section SHNDX,
686 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
687 // indicates whether the symbol is a section symbol. This handles
688 // SHF_MERGE sections.
689 template<int size, bool big_endian>
690 typename elfcpp::Elf_types<size>::Elf_Addr
691 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
693 bool is_section_symbol,
694 Address addend) const
696 const std::vector<Map_to_output>& mo(this->map_to_output());
697 Output_section* os = mo[shndx].output_section;
700 gold_assert(mo[shndx].offset == -1);
702 // Do the mapping required by the output section. If this is not a
703 // section symbol, then we want to map the symbol value, and then
704 // include the addend. If this is a section symbol, then we need to
705 // include the addend to figure out where in the section we are,
706 // before we do the mapping. This will do the right thing provided
707 // the assembler is careful to only convert a relocation in a merged
708 // section to a section symbol if there is a zero addend. If the
709 // assembler does not do this, then in general we can't know what to
710 // do, because we can't distinguish the addend for the instruction
711 // format from the addend for the section offset.
713 if (is_section_symbol)
714 return os->output_address(this, shndx, value + addend);
716 return addend + os->output_address(this, shndx, value);
719 // Write out the local symbols.
721 template<int size, bool big_endian>
723 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
724 const Stringpool* sympool)
726 if (parameters->strip_all())
729 gold_assert(this->symtab_shndx_ != -1U);
730 if (this->symtab_shndx_ == 0)
732 // This object has no symbols. Weird but legal.
736 // Read the symbol table section header.
737 const unsigned int symtab_shndx = this->symtab_shndx_;
738 typename This::Shdr symtabshdr(this,
739 this->elf_file_.section_header(symtab_shndx));
740 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
741 const unsigned int loccount = this->local_symbol_count_;
742 gold_assert(loccount == symtabshdr.get_sh_info());
744 // Read the local symbols.
745 const int sym_size = This::sym_size;
746 off_t locsize = loccount * sym_size;
747 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
750 // Read the symbol names.
751 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
753 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
756 const char* pnames = reinterpret_cast<const char*>(pnamesu);
758 // Get a view into the output file.
759 off_t output_size = this->output_local_symbol_count_ * sym_size;
760 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
763 const std::vector<Map_to_output>& mo(this->map_to_output());
765 gold_assert(this->local_values_.size() == loccount);
767 unsigned char* ov = oview;
769 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
771 elfcpp::Sym<size, big_endian> isym(psyms);
773 if (!this->local_values_[i].needs_output_symtab_entry())
776 unsigned int st_shndx = isym.get_st_shndx();
777 if (st_shndx < elfcpp::SHN_LORESERVE)
779 gold_assert(st_shndx < mo.size());
780 if (mo[st_shndx].output_section == NULL)
782 st_shndx = mo[st_shndx].output_section->out_shndx();
785 elfcpp::Sym_write<size, big_endian> osym(ov);
787 gold_assert(isym.get_st_name() < strtab_size);
788 const char* name = pnames + isym.get_st_name();
789 osym.put_st_name(sympool->get_offset(name));
790 osym.put_st_value(this->local_values_[i].value(this, 0));
791 osym.put_st_size(isym.get_st_size());
792 osym.put_st_info(isym.get_st_info());
793 osym.put_st_other(isym.get_st_other());
794 osym.put_st_shndx(st_shndx);
799 gold_assert(ov - oview == output_size);
801 of->write_output_view(this->local_symbol_offset_, output_size, oview);
804 // Set *INFO to symbolic information about the offset OFFSET in the
805 // section SHNDX. Return true if we found something, false if we
808 template<int size, bool big_endian>
810 Sized_relobj<size, big_endian>::get_symbol_location_info(
813 Symbol_location_info* info)
815 if (this->symtab_shndx_ == 0)
819 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
823 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
825 const unsigned char* symbol_names_u =
826 this->section_contents(symbol_names_shndx, &names_size, false);
827 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
829 const int sym_size = This::sym_size;
830 const size_t count = symbols_size / sym_size;
832 const unsigned char* p = symbols;
833 for (size_t i = 0; i < count; ++i, p += sym_size)
835 elfcpp::Sym<size, big_endian> sym(p);
837 if (sym.get_st_type() == elfcpp::STT_FILE)
839 if (sym.get_st_name() >= names_size)
840 info->source_file = "(invalid)";
842 info->source_file = symbol_names + sym.get_st_name();
844 else if (sym.get_st_shndx() == shndx
845 && static_cast<off_t>(sym.get_st_value()) <= offset
846 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
849 if (sym.get_st_name() > names_size)
850 info->enclosing_symbol_name = "(invalid)";
852 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
860 // Input_objects methods.
862 // Add a regular relocatable object to the list. Return false if this
863 // object should be ignored.
866 Input_objects::add_object(Object* obj)
868 if (!obj->is_dynamic())
869 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
872 // See if this is a duplicate SONAME.
873 Dynobj* dynobj = static_cast<Dynobj*>(obj);
875 std::pair<Unordered_set<std::string>::iterator, bool> ins =
876 this->sonames_.insert(dynobj->soname());
879 // We have already seen a dynamic object with this soname.
883 this->dynobj_list_.push_back(dynobj);
886 Target* target = obj->target();
887 if (this->target_ == NULL)
888 this->target_ = target;
889 else if (this->target_ != target)
891 gold_error(_("%s: incompatible target"), obj->name().c_str());
895 set_parameters_size_and_endianness(target->get_size(),
896 target->is_big_endian());
901 // Relocate_info methods.
903 // Return a string describing the location of a relocation. This is
904 // only used in error messages.
906 template<int size, bool big_endian>
908 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
910 // FIXME: We would like to print the following:
911 // /tmp/foo.o: in function 'fn':foo.c:12: undefined reference to 'xxx'
912 // We're missing line numbers.
913 std::string ret(this->object->name());
915 Symbol_location_info info;
916 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
918 ret += " in function ";
919 ret += info.enclosing_symbol_name;
921 ret += info.source_file;
924 ret += this->object->section_name(this->data_shndx);
926 // Offsets into sections have to be positive.
927 snprintf(buf, sizeof(buf), "+0x%lx)", static_cast<long>(offset));
932 } // End namespace gold.
937 using namespace gold;
939 // Read an ELF file with the header and return the appropriate
940 // instance of Object.
942 template<int size, bool big_endian>
944 make_elf_sized_object(const std::string& name, Input_file* input_file,
945 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
947 int et = ehdr.get_e_type();
948 if (et == elfcpp::ET_REL)
950 Sized_relobj<size, big_endian>* obj =
951 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
955 else if (et == elfcpp::ET_DYN)
957 Sized_dynobj<size, big_endian>* obj =
958 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
964 gold_error(_("%s: unsupported ELF file type %d"),
970 } // End anonymous namespace.
975 // Read an ELF file and return the appropriate instance of Object.
978 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
979 const unsigned char* p, off_t bytes)
981 if (bytes < elfcpp::EI_NIDENT)
983 gold_error(_("%s: ELF file too short"), name.c_str());
987 int v = p[elfcpp::EI_VERSION];
988 if (v != elfcpp::EV_CURRENT)
990 if (v == elfcpp::EV_NONE)
991 gold_error(_("%s: invalid ELF version 0"), name.c_str());
993 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
997 int c = p[elfcpp::EI_CLASS];
998 if (c == elfcpp::ELFCLASSNONE)
1000 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1003 else if (c != elfcpp::ELFCLASS32
1004 && c != elfcpp::ELFCLASS64)
1006 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1010 int d = p[elfcpp::EI_DATA];
1011 if (d == elfcpp::ELFDATANONE)
1013 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1016 else if (d != elfcpp::ELFDATA2LSB
1017 && d != elfcpp::ELFDATA2MSB)
1019 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1023 bool big_endian = d == elfcpp::ELFDATA2MSB;
1025 if (c == elfcpp::ELFCLASS32)
1027 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1029 gold_error(_("%s: ELF file too short"), name.c_str());
1034 #ifdef HAVE_TARGET_32_BIG
1035 elfcpp::Ehdr<32, true> ehdr(p);
1036 return make_elf_sized_object<32, true>(name, input_file,
1039 gold_error(_("%s: not configured to support "
1040 "32-bit big-endian object"),
1047 #ifdef HAVE_TARGET_32_LITTLE
1048 elfcpp::Ehdr<32, false> ehdr(p);
1049 return make_elf_sized_object<32, false>(name, input_file,
1052 gold_error(_("%s: not configured to support "
1053 "32-bit little-endian object"),
1061 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1063 gold_error(_("%s: ELF file too short"), name.c_str());
1068 #ifdef HAVE_TARGET_64_BIG
1069 elfcpp::Ehdr<64, true> ehdr(p);
1070 return make_elf_sized_object<64, true>(name, input_file,
1073 gold_error(_("%s: not configured to support "
1074 "64-bit big-endian object"),
1081 #ifdef HAVE_TARGET_64_LITTLE
1082 elfcpp::Ehdr<64, false> ehdr(p);
1083 return make_elf_sized_object<64, false>(name, input_file,
1086 gold_error(_("%s: not configured to support "
1087 "64-bit little-endian object"),
1095 // Instantiate the templates we need. We could use the configure
1096 // script to restrict this to only the ones for implemented targets.
1098 #ifdef HAVE_TARGET_32_LITTLE
1100 class Sized_relobj<32, false>;
1103 #ifdef HAVE_TARGET_32_BIG
1105 class Sized_relobj<32, true>;
1108 #ifdef HAVE_TARGET_64_LITTLE
1110 class Sized_relobj<64, false>;
1113 #ifdef HAVE_TARGET_64_BIG
1115 class Sized_relobj<64, true>;
1118 #ifdef HAVE_TARGET_32_LITTLE
1120 struct Relocate_info<32, false>;
1123 #ifdef HAVE_TARGET_32_BIG
1125 struct Relocate_info<32, true>;
1128 #ifdef HAVE_TARGET_64_LITTLE
1130 struct Relocate_info<64, false>;
1133 #ifdef HAVE_TARGET_64_BIG
1135 struct Relocate_info<64, true>;
1138 } // End namespace gold.