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"
30 #include "dwarf_reader.h"
43 // Set the target based on fields in the ELF file header.
46 Object::set_target(int machine, int size, bool big_endian, int osabi,
49 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
51 gold_fatal(_("%s: unsupported ELF machine number %d"),
52 this->name().c_str(), machine);
53 this->target_ = target;
56 // Report an error for this object file. This is used by the
57 // elfcpp::Elf_file interface, and also called by the Object code
61 Object::error(const char* format, ...) const
64 va_start(args, format);
66 if (vasprintf(&buf, format, args) < 0)
69 gold_error(_("%s: %s"), this->name().c_str(), buf);
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)
104 this->error(_("section name section has wrong type: %u"),
105 static_cast<unsigned int>(shdrnames.get_sh_type()));
107 sd->section_names_size = shdrnames.get_sh_size();
108 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
109 sd->section_names_size, false);
112 // If NAME is the name of a special .gnu.warning section, arrange for
113 // the warning to be issued. SHNDX is the section index. Return
114 // whether it is a warning section.
117 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
118 Symbol_table* symtab)
120 const char warn_prefix[] = ".gnu.warning.";
121 const int warn_prefix_len = sizeof warn_prefix - 1;
122 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
124 symtab->add_warning(name + warn_prefix_len, this, shndx);
130 // Class Sized_relobj.
132 template<int size, bool big_endian>
133 Sized_relobj<size, big_endian>::Sized_relobj(
134 const std::string& name,
135 Input_file* input_file,
137 const elfcpp::Ehdr<size, big_endian>& ehdr)
138 : Relobj(name, input_file, offset),
139 elf_file_(this, ehdr),
141 local_symbol_count_(0),
142 output_local_symbol_count_(0),
144 local_symbol_offset_(0),
146 local_got_offsets_(),
151 template<int size, bool big_endian>
152 Sized_relobj<size, big_endian>::~Sized_relobj()
156 // Set up an object file based on the file header. This sets up the
157 // target and reads the section information.
159 template<int size, bool big_endian>
161 Sized_relobj<size, big_endian>::setup(
162 const elfcpp::Ehdr<size, big_endian>& ehdr)
164 this->set_target(ehdr.get_e_machine(), size, big_endian,
165 ehdr.get_e_ident()[elfcpp::EI_OSABI],
166 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
168 const unsigned int shnum = this->elf_file_.shnum();
169 this->set_shnum(shnum);
172 // Find the SHT_SYMTAB section, given the section headers. The ELF
173 // standard says that maybe in the future there can be more than one
174 // SHT_SYMTAB section. Until somebody figures out how that could
175 // work, we assume there is only one.
177 template<int size, bool big_endian>
179 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
181 const unsigned int shnum = this->shnum();
182 this->symtab_shndx_ = 0;
185 // Look through the sections in reverse order, since gas tends
186 // to put the symbol table at the end.
187 const unsigned char* p = pshdrs + shnum * This::shdr_size;
188 unsigned int i = shnum;
192 p -= This::shdr_size;
193 typename This::Shdr shdr(p);
194 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
196 this->symtab_shndx_ = i;
203 // Return whether SHDR has the right type and flags to be a GNU
204 // .eh_frame section.
206 template<int size, bool big_endian>
208 Sized_relobj<size, big_endian>::check_eh_frame_flags(
209 const elfcpp::Shdr<size, big_endian>* shdr) const
211 return (shdr->get_sh_size() > 0
212 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
213 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
216 // Return whether there is a GNU .eh_frame section, given the section
217 // headers and the section names.
219 template<int size, bool big_endian>
221 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
223 off_t names_size) const
225 const unsigned int shnum = this->shnum();
226 const unsigned char* p = pshdrs + This::shdr_size;
227 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
229 typename This::Shdr shdr(p);
230 if (this->check_eh_frame_flags(&shdr))
232 if (shdr.get_sh_name() >= names_size)
234 this->error(_("bad section name offset for section %u: %lu"),
235 i, static_cast<unsigned long>(shdr.get_sh_name()));
239 const char* name = names + shdr.get_sh_name();
240 if (strcmp(name, ".eh_frame") == 0)
247 // Read the sections and symbols from an object file.
249 template<int size, bool big_endian>
251 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
253 this->read_section_data(&this->elf_file_, sd);
255 const unsigned char* const pshdrs = sd->section_headers->data();
257 this->find_symtab(pshdrs);
259 const unsigned char* namesu = sd->section_names->data();
260 const char* names = reinterpret_cast<const char*>(namesu);
261 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
262 this->has_eh_frame_ = true;
265 sd->symbols_size = 0;
266 sd->external_symbols_offset = 0;
267 sd->symbol_names = NULL;
268 sd->symbol_names_size = 0;
270 if (this->symtab_shndx_ == 0)
272 // No symbol table. Weird but legal.
276 // Get the symbol table section header.
277 typename This::Shdr symtabshdr(pshdrs
278 + this->symtab_shndx_ * This::shdr_size);
279 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
281 // If this object has a .eh_frame section, we need all the symbols.
282 // Otherwise we only need the external symbols. While it would be
283 // simpler to just always read all the symbols, I've seen object
284 // files with well over 2000 local symbols, which for a 64-bit
285 // object file format is over 5 pages that we don't need to read
288 const int sym_size = This::sym_size;
289 const unsigned int loccount = symtabshdr.get_sh_info();
290 this->local_symbol_count_ = loccount;
291 off_t locsize = loccount * sym_size;
292 off_t dataoff = symtabshdr.get_sh_offset();
293 off_t datasize = symtabshdr.get_sh_size();
294 off_t extoff = dataoff + locsize;
295 off_t extsize = datasize - locsize;
297 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
298 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
300 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
302 // Read the section header for the symbol names.
303 unsigned int strtab_shndx = symtabshdr.get_sh_link();
304 if (strtab_shndx >= this->shnum())
306 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
309 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
310 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
312 this->error(_("symbol table name section has wrong type: %u"),
313 static_cast<unsigned int>(strtabshdr.get_sh_type()));
317 // Read the symbol names.
318 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
319 strtabshdr.get_sh_size(), true);
321 sd->symbols = fvsymtab;
322 sd->symbols_size = readsize;
323 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
324 sd->symbol_names = fvstrtab;
325 sd->symbol_names_size = strtabshdr.get_sh_size();
328 // Return the section index of symbol SYM. Set *VALUE to its value in
329 // the object file. Note that for a symbol which is not defined in
330 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
331 // it will not return the final value of the symbol in the link.
333 template<int size, bool big_endian>
335 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
339 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
343 const size_t count = symbols_size / This::sym_size;
344 gold_assert(sym < count);
346 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
347 *value = elfsym.get_st_value();
348 // FIXME: Handle SHN_XINDEX.
349 return elfsym.get_st_shndx();
352 // Return whether to include a section group in the link. LAYOUT is
353 // used to keep track of which section groups we have already seen.
354 // INDEX is the index of the section group and SHDR is the section
355 // header. If we do not want to include this group, we set bits in
356 // OMIT for each section which should be discarded.
358 template<int size, bool big_endian>
360 Sized_relobj<size, big_endian>::include_section_group(
363 const elfcpp::Shdr<size, big_endian>& shdr,
364 std::vector<bool>* omit)
366 // Read the section contents.
367 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
368 shdr.get_sh_size(), false);
369 const elfcpp::Elf_Word* pword =
370 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
372 // The first word contains flags. We only care about COMDAT section
373 // groups. Other section groups are always included in the link
374 // just like ordinary sections.
375 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
376 if ((flags & elfcpp::GRP_COMDAT) == 0)
379 // Look up the group signature, which is the name of a symbol. This
380 // is a lot of effort to go to to read a string. Why didn't they
381 // just use the name of the SHT_GROUP section as the group
384 // Get the appropriate symbol table header (this will normally be
385 // the single SHT_SYMTAB section, but in principle it need not be).
386 const unsigned int link = shdr.get_sh_link();
387 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
389 // Read the symbol table entry.
390 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
392 this->error(_("section group %u info %u out of range"),
393 index, shdr.get_sh_info());
396 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
397 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
398 elfcpp::Sym<size, big_endian> sym(psym);
400 // Read the symbol table names.
402 const unsigned char* psymnamesu;
403 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
405 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
407 // Get the section group signature.
408 if (sym.get_st_name() >= symnamelen)
410 this->error(_("symbol %u name offset %u out of range"),
411 shdr.get_sh_info(), sym.get_st_name());
415 const char* signature = psymnames + sym.get_st_name();
417 // It seems that some versions of gas will create a section group
418 // associated with a section symbol, and then fail to give a name to
419 // the section symbol. In such a case, use the name of the section.
422 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
424 secname = this->section_name(sym.get_st_shndx());
425 signature = secname.c_str();
428 // Record this section group, and see whether we've already seen one
429 // with the same signature.
430 if (layout->add_comdat(signature, true))
433 // This is a duplicate. We want to discard the sections in this
435 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
436 for (size_t i = 1; i < count; ++i)
438 elfcpp::Elf_Word secnum =
439 elfcpp::Swap<32, big_endian>::readval(pword + i);
440 if (secnum >= this->shnum())
442 this->error(_("section %u in section group %u out of range"),
446 (*omit)[secnum] = true;
452 // Whether to include a linkonce section in the link. NAME is the
453 // name of the section and SHDR is the section header.
455 // Linkonce sections are a GNU extension implemented in the original
456 // GNU linker before section groups were defined. The semantics are
457 // that we only include one linkonce section with a given name. The
458 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
459 // where T is the type of section and SYMNAME is the name of a symbol.
460 // In an attempt to make linkonce sections interact well with section
461 // groups, we try to identify SYMNAME and use it like a section group
462 // signature. We want to block section groups with that signature,
463 // but not other linkonce sections with that signature. We also use
464 // the full name of the linkonce section as a normal section group
467 template<int size, bool big_endian>
469 Sized_relobj<size, big_endian>::include_linkonce_section(
472 const elfcpp::Shdr<size, big_endian>&)
474 // In general the symbol name we want will be the string following
475 // the last '.'. However, we have to handle the case of
476 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
477 // some versions of gcc. So we use a heuristic: if the name starts
478 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
479 // we look for the last '.'. We can't always simply skip
480 // ".gnu.linkonce.X", because we have to deal with cases like
481 // ".gnu.linkonce.d.rel.ro.local".
482 const char* const linkonce_t = ".gnu.linkonce.t.";
484 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
485 symname = name + strlen(linkonce_t);
487 symname = strrchr(name, '.') + 1;
488 bool include1 = layout->add_comdat(symname, false);
489 bool include2 = layout->add_comdat(name, true);
490 return include1 && include2;
493 // Lay out the input sections. We walk through the sections and check
494 // whether they should be included in the link. If they should, we
495 // pass them to the Layout object, which will return an output section
498 template<int size, bool big_endian>
500 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
502 Read_symbols_data* sd)
504 const unsigned int shnum = this->shnum();
508 // Get the section headers.
509 const unsigned char* pshdrs = sd->section_headers->data();
511 // Get the section names.
512 const unsigned char* pnamesu = sd->section_names->data();
513 const char* pnames = reinterpret_cast<const char*>(pnamesu);
515 // For each section, record the index of the reloc section if any.
516 // Use 0 to mean that there is no reloc section, -1U to mean that
517 // there is more than one.
518 std::vector<unsigned int> reloc_shndx(shnum, 0);
519 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
520 // Skip the first, dummy, section.
521 pshdrs += This::shdr_size;
522 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
524 typename This::Shdr shdr(pshdrs);
526 unsigned int sh_type = shdr.get_sh_type();
527 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
529 unsigned int target_shndx = shdr.get_sh_info();
530 if (target_shndx == 0 || target_shndx >= shnum)
532 this->error(_("relocation section %u has bad info %u"),
537 if (reloc_shndx[target_shndx] != 0)
538 reloc_shndx[target_shndx] = -1U;
541 reloc_shndx[target_shndx] = i;
542 reloc_type[target_shndx] = sh_type;
547 std::vector<Map_to_output>& map_sections(this->map_to_output());
548 map_sections.resize(shnum);
550 // Whether we've seen a .note.GNU-stack section.
551 bool seen_gnu_stack = false;
552 // The flags of a .note.GNU-stack section.
553 uint64_t gnu_stack_flags = 0;
555 // Keep track of which sections to omit.
556 std::vector<bool> omit(shnum, false);
558 // Keep track of .eh_frame sections.
559 std::vector<unsigned int> eh_frame_sections;
561 // Skip the first, dummy, section.
562 pshdrs = sd->section_headers->data() + This::shdr_size;
563 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
565 typename This::Shdr shdr(pshdrs);
567 if (shdr.get_sh_name() >= sd->section_names_size)
569 this->error(_("bad section name offset for section %u: %lu"),
570 i, static_cast<unsigned long>(shdr.get_sh_name()));
574 const char* name = pnames + shdr.get_sh_name();
576 if (this->handle_gnu_warning_section(name, i, symtab))
578 if (!parameters->output_is_object())
582 // The .note.GNU-stack section is special. It gives the
583 // protection flags that this object file requires for the stack
585 if (strcmp(name, ".note.GNU-stack") == 0)
587 seen_gnu_stack = true;
588 gnu_stack_flags |= shdr.get_sh_flags();
592 bool discard = omit[i];
595 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
597 if (!this->include_section_group(layout, i, shdr, &omit))
600 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
601 && Layout::is_linkonce(name))
603 if (!this->include_linkonce_section(layout, name, shdr))
610 // Do not include this section in the link.
611 map_sections[i].output_section = NULL;
615 // The .eh_frame section is special. It holds exception frame
616 // information that we need to read in order to generate the
617 // exception frame header. We process these after all the other
618 // sections so that the exception frame reader can reliably
619 // determine which sections are being discarded, and discard the
620 // corresponding information.
621 if (!parameters->output_is_object()
622 && strcmp(name, ".eh_frame") == 0
623 && this->check_eh_frame_flags(&shdr))
625 eh_frame_sections.push_back(i);
630 Output_section* os = layout->layout(this, i, name, shdr,
631 reloc_shndx[i], reloc_type[i],
634 map_sections[i].output_section = os;
635 map_sections[i].offset = offset;
637 // If this section requires special handling, and if there are
638 // relocs that apply to it, then we must do the special handling
639 // before we apply the relocs.
640 if (offset == -1 && reloc_shndx[i] != 0)
641 this->set_relocs_must_follow_section_writes();
644 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
646 // Handle the .eh_frame sections at the end.
647 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
648 p != eh_frame_sections.end();
651 gold_assert(this->has_eh_frame_);
652 gold_assert(sd->external_symbols_offset != 0);
655 const unsigned char *pshdr;
656 pshdr = sd->section_headers->data() + i * This::shdr_size;
657 typename This::Shdr shdr(pshdr);
660 Output_section* os = layout->layout_eh_frame(this,
663 sd->symbol_names->data(),
664 sd->symbol_names_size,
669 map_sections[i].output_section = os;
670 map_sections[i].offset = offset;
672 // If this section requires special handling, and if there are
673 // relocs that apply to it, then we must do the special handling
674 // before we apply the relocs.
675 if (offset == -1 && reloc_shndx[i] != 0)
676 this->set_relocs_must_follow_section_writes();
679 delete sd->section_headers;
680 sd->section_headers = NULL;
681 delete sd->section_names;
682 sd->section_names = NULL;
685 // Add the symbols to the symbol table.
687 template<int size, bool big_endian>
689 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
690 Read_symbols_data* sd)
692 if (sd->symbols == NULL)
694 gold_assert(sd->symbol_names == NULL);
698 const int sym_size = This::sym_size;
699 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
701 if (static_cast<off_t>(symcount * sym_size)
702 != sd->symbols_size - sd->external_symbols_offset)
704 this->error(_("size of symbols is not multiple of symbol size"));
708 this->symbols_.resize(symcount);
710 const char* sym_names =
711 reinterpret_cast<const char*>(sd->symbol_names->data());
712 symtab->add_from_relobj(this,
713 sd->symbols->data() + sd->external_symbols_offset,
714 symcount, sym_names, sd->symbol_names_size,
719 delete sd->symbol_names;
720 sd->symbol_names = NULL;
723 // Finalize the local symbols. Here we record the file offset at
724 // which they should be output, we add their names to *POOL, and we
725 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
726 // This function is always called from the main thread. The actual
727 // output of the local symbols will occur in a separate task.
729 template<int size, bool big_endian>
731 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
735 gold_assert(this->symtab_shndx_ != -1U);
736 if (this->symtab_shndx_ == 0)
738 // This object has no symbols. Weird but legal.
742 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
744 this->local_symbol_offset_ = off;
746 // Read the symbol table section header.
747 const unsigned int symtab_shndx = this->symtab_shndx_;
748 typename This::Shdr symtabshdr(this,
749 this->elf_file_.section_header(symtab_shndx));
750 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
752 // Read the local symbols.
753 const int sym_size = This::sym_size;
754 const unsigned int loccount = this->local_symbol_count_;
755 gold_assert(loccount == symtabshdr.get_sh_info());
756 off_t locsize = loccount * sym_size;
757 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
760 this->local_values_.resize(loccount);
762 // Read the symbol names.
763 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
765 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
768 const char* pnames = reinterpret_cast<const char*>(pnamesu);
770 // Loop over the local symbols.
772 const std::vector<Map_to_output>& mo(this->map_to_output());
773 unsigned int shnum = this->shnum();
774 unsigned int count = 0;
775 // Skip the first, dummy, symbol.
777 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
779 elfcpp::Sym<size, big_endian> sym(psyms);
781 Symbol_value<size>& lv(this->local_values_[i]);
783 unsigned int shndx = sym.get_st_shndx();
784 lv.set_input_shndx(shndx);
786 if (sym.get_st_type() == elfcpp::STT_SECTION)
787 lv.set_is_section_symbol();
789 if (shndx >= elfcpp::SHN_LORESERVE)
791 if (shndx == elfcpp::SHN_ABS)
792 lv.set_output_value(sym.get_st_value());
795 // FIXME: Handle SHN_XINDEX.
796 this->error(_("unknown section index %u for local symbol %u"),
798 lv.set_output_value(0);
805 this->error(_("local symbol %u section index %u out of range"),
810 Output_section* os = mo[shndx].output_section;
814 lv.set_output_value(0);
815 lv.set_no_output_symtab_entry();
819 if (mo[shndx].offset == -1)
820 lv.set_input_value(sym.get_st_value());
822 lv.set_output_value(mo[shndx].output_section->address()
824 + sym.get_st_value());
827 // Decide whether this symbol should go into the output file.
829 if (sym.get_st_type() == elfcpp::STT_SECTION)
831 lv.set_no_output_symtab_entry();
835 if (sym.get_st_name() >= strtab_size)
837 this->error(_("local symbol %u section name out of range: %u >= %u"),
838 i, sym.get_st_name(),
839 static_cast<unsigned int>(strtab_size));
840 lv.set_no_output_symtab_entry();
844 const char* name = pnames + sym.get_st_name();
845 pool->add(name, true, NULL);
846 lv.set_output_symtab_index(index);
851 this->output_local_symbol_count_ = count;
856 // Return the value of the local symbol symndx.
857 template<int size, bool big_endian>
858 typename elfcpp::Elf_types<size>::Elf_Addr
859 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
861 gold_assert(symndx < this->local_symbol_count_);
862 gold_assert(symndx < this->local_values_.size());
863 const Symbol_value<size>& lv(this->local_values_[symndx]);
864 return lv.value(this, 0);
867 // Return the value of a local symbol defined in input section SHNDX,
868 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
869 // indicates whether the symbol is a section symbol. This handles
870 // SHF_MERGE sections.
871 template<int size, bool big_endian>
872 typename elfcpp::Elf_types<size>::Elf_Addr
873 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
875 bool is_section_symbol,
876 Address addend) const
878 const std::vector<Map_to_output>& mo(this->map_to_output());
879 Output_section* os = mo[shndx].output_section;
882 gold_assert(mo[shndx].offset == -1);
884 // Do the mapping required by the output section. If this is not a
885 // section symbol, then we want to map the symbol value, and then
886 // include the addend. If this is a section symbol, then we need to
887 // include the addend to figure out where in the section we are,
888 // before we do the mapping. This will do the right thing provided
889 // the assembler is careful to only convert a relocation in a merged
890 // section to a section symbol if there is a zero addend. If the
891 // assembler does not do this, then in general we can't know what to
892 // do, because we can't distinguish the addend for the instruction
893 // format from the addend for the section offset.
895 if (is_section_symbol)
896 return os->output_address(this, shndx, value + addend);
898 return addend + os->output_address(this, shndx, value);
901 // Write out the local symbols.
903 template<int size, bool big_endian>
905 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
906 const Stringpool* sympool)
908 if (parameters->strip_all())
911 gold_assert(this->symtab_shndx_ != -1U);
912 if (this->symtab_shndx_ == 0)
914 // This object has no symbols. Weird but legal.
918 // Read the symbol table section header.
919 const unsigned int symtab_shndx = this->symtab_shndx_;
920 typename This::Shdr symtabshdr(this,
921 this->elf_file_.section_header(symtab_shndx));
922 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
923 const unsigned int loccount = this->local_symbol_count_;
924 gold_assert(loccount == symtabshdr.get_sh_info());
926 // Read the local symbols.
927 const int sym_size = This::sym_size;
928 off_t locsize = loccount * sym_size;
929 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
932 // Read the symbol names.
933 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
935 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
938 const char* pnames = reinterpret_cast<const char*>(pnamesu);
940 // Get a view into the output file.
941 off_t output_size = this->output_local_symbol_count_ * sym_size;
942 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
945 const std::vector<Map_to_output>& mo(this->map_to_output());
947 gold_assert(this->local_values_.size() == loccount);
949 unsigned char* ov = oview;
951 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
953 elfcpp::Sym<size, big_endian> isym(psyms);
955 if (!this->local_values_[i].needs_output_symtab_entry())
958 unsigned int st_shndx = isym.get_st_shndx();
959 if (st_shndx < elfcpp::SHN_LORESERVE)
961 gold_assert(st_shndx < mo.size());
962 if (mo[st_shndx].output_section == NULL)
964 st_shndx = mo[st_shndx].output_section->out_shndx();
967 elfcpp::Sym_write<size, big_endian> osym(ov);
969 gold_assert(isym.get_st_name() < strtab_size);
970 const char* name = pnames + isym.get_st_name();
971 osym.put_st_name(sympool->get_offset(name));
972 osym.put_st_value(this->local_values_[i].value(this, 0));
973 osym.put_st_size(isym.get_st_size());
974 osym.put_st_info(isym.get_st_info());
975 osym.put_st_other(isym.get_st_other());
976 osym.put_st_shndx(st_shndx);
981 gold_assert(ov - oview == output_size);
983 of->write_output_view(this->local_symbol_offset_, output_size, oview);
986 // Set *INFO to symbolic information about the offset OFFSET in the
987 // section SHNDX. Return true if we found something, false if we
990 template<int size, bool big_endian>
992 Sized_relobj<size, big_endian>::get_symbol_location_info(
995 Symbol_location_info* info)
997 if (this->symtab_shndx_ == 0)
1001 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1005 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1007 const unsigned char* symbol_names_u =
1008 this->section_contents(symbol_names_shndx, &names_size, false);
1009 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1011 const int sym_size = This::sym_size;
1012 const size_t count = symbols_size / sym_size;
1014 const unsigned char* p = symbols;
1015 for (size_t i = 0; i < count; ++i, p += sym_size)
1017 elfcpp::Sym<size, big_endian> sym(p);
1019 if (sym.get_st_type() == elfcpp::STT_FILE)
1021 if (sym.get_st_name() >= names_size)
1022 info->source_file = "(invalid)";
1024 info->source_file = symbol_names + sym.get_st_name();
1026 else if (sym.get_st_shndx() == shndx
1027 && static_cast<off_t>(sym.get_st_value()) <= offset
1028 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1031 if (sym.get_st_name() > names_size)
1032 info->enclosing_symbol_name = "(invalid)";
1034 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1042 // Input_objects methods.
1044 // Add a regular relocatable object to the list. Return false if this
1045 // object should be ignored.
1048 Input_objects::add_object(Object* obj)
1050 Target* target = obj->target();
1051 if (this->target_ == NULL)
1052 this->target_ = target;
1053 else if (this->target_ != target)
1055 gold_error(_("%s: incompatible target"), obj->name().c_str());
1059 if (!obj->is_dynamic())
1060 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1063 // See if this is a duplicate SONAME.
1064 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1066 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1067 this->sonames_.insert(dynobj->soname());
1070 // We have already seen a dynamic object with this soname.
1074 this->dynobj_list_.push_back(dynobj);
1077 set_parameters_size_and_endianness(target->get_size(),
1078 target->is_big_endian());
1083 // Relocate_info methods.
1085 // Return a string describing the location of a relocation. This is
1086 // only used in error messages.
1088 template<int size, bool big_endian>
1090 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1092 // See if we can get line-number information from debugging sections.
1093 std::string filename;
1094 std::string file_and_lineno; // Better than filename-only, if available.
1096 // The line-number information is in the ".debug_line" section.
1097 unsigned int debug_shndx;
1098 off_t debuglines_size;
1099 const unsigned char* debuglines = NULL;
1100 for (debug_shndx = 0; debug_shndx < this->object->shnum(); ++debug_shndx)
1101 if (this->object->section_name(debug_shndx) == ".debug_line")
1103 debuglines = this->object->section_contents(
1104 debug_shndx, &debuglines_size, false);
1108 // Find the relocation section for ".debug_line".
1109 Track_relocs<size, big_endian> track_relocs;
1110 bool got_relocs = false;
1111 for (unsigned int reloc_shndx = 0;
1112 reloc_shndx < this->object->shnum();
1115 unsigned int reloc_sh_type = this->object->section_type(reloc_shndx);
1116 if ((reloc_sh_type == elfcpp::SHT_REL
1117 || reloc_sh_type == elfcpp::SHT_RELA)
1118 && this->object->section_info(reloc_shndx) == debug_shndx)
1120 got_relocs = track_relocs.initialize(this->object, reloc_shndx,
1126 // Finally, we need the symtab section to interpret the relocs.
1127 unsigned int symtab_shndx;
1129 const unsigned char* symtab = NULL;
1130 for (symtab_shndx = 0; symtab_shndx < this->object->shnum(); ++symtab_shndx)
1131 if (this->object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
1133 symtab = this->object->section_contents(
1134 symtab_shndx, &symtab_size, false);
1138 // If we got all three sections we need, we can try to read debug info.
1139 if (debuglines != NULL && got_relocs && symtab != NULL)
1141 Dwarf_line_info<size, big_endian> line_info(debuglines, debuglines_size,
1143 symtab, symtab_size);
1144 line_info.read_line_mappings();
1145 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1148 std::string ret(this->object->name());
1150 Symbol_location_info info;
1151 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1153 ret += " in function ";
1154 // We could demangle this name before printing, but we don't
1155 // bother because gcc runs linker output through a demangle
1156 // filter itself. The only advantage to demangling here is if
1157 // someone might call ld directly, rather than via gcc. If we
1158 // did want to demangle, cplus_demangle() is in libiberty.
1159 ret += info.enclosing_symbol_name;
1161 filename = info.source_file;
1164 if (!file_and_lineno.empty())
1165 ret += file_and_lineno;
1168 if (!filename.empty())
1171 ret += this->object->section_name(this->data_shndx);
1173 // Offsets into sections have to be positive.
1174 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1181 } // End namespace gold.
1186 using namespace gold;
1188 // Read an ELF file with the header and return the appropriate
1189 // instance of Object.
1191 template<int size, bool big_endian>
1193 make_elf_sized_object(const std::string& name, Input_file* input_file,
1194 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1196 int et = ehdr.get_e_type();
1197 if (et == elfcpp::ET_REL)
1199 Sized_relobj<size, big_endian>* obj =
1200 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1204 else if (et == elfcpp::ET_DYN)
1206 Sized_dynobj<size, big_endian>* obj =
1207 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1213 gold_error(_("%s: unsupported ELF file type %d"),
1219 } // End anonymous namespace.
1224 // Read an ELF file and return the appropriate instance of Object.
1227 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1228 const unsigned char* p, off_t bytes)
1230 if (bytes < elfcpp::EI_NIDENT)
1232 gold_error(_("%s: ELF file too short"), name.c_str());
1236 int v = p[elfcpp::EI_VERSION];
1237 if (v != elfcpp::EV_CURRENT)
1239 if (v == elfcpp::EV_NONE)
1240 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1242 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1246 int c = p[elfcpp::EI_CLASS];
1247 if (c == elfcpp::ELFCLASSNONE)
1249 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1252 else if (c != elfcpp::ELFCLASS32
1253 && c != elfcpp::ELFCLASS64)
1255 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1259 int d = p[elfcpp::EI_DATA];
1260 if (d == elfcpp::ELFDATANONE)
1262 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1265 else if (d != elfcpp::ELFDATA2LSB
1266 && d != elfcpp::ELFDATA2MSB)
1268 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1272 bool big_endian = d == elfcpp::ELFDATA2MSB;
1274 if (c == elfcpp::ELFCLASS32)
1276 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1278 gold_error(_("%s: ELF file too short"), name.c_str());
1283 #ifdef HAVE_TARGET_32_BIG
1284 elfcpp::Ehdr<32, true> ehdr(p);
1285 return make_elf_sized_object<32, true>(name, input_file,
1288 gold_error(_("%s: not configured to support "
1289 "32-bit big-endian object"),
1296 #ifdef HAVE_TARGET_32_LITTLE
1297 elfcpp::Ehdr<32, false> ehdr(p);
1298 return make_elf_sized_object<32, false>(name, input_file,
1301 gold_error(_("%s: not configured to support "
1302 "32-bit little-endian object"),
1310 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1312 gold_error(_("%s: ELF file too short"), name.c_str());
1317 #ifdef HAVE_TARGET_64_BIG
1318 elfcpp::Ehdr<64, true> ehdr(p);
1319 return make_elf_sized_object<64, true>(name, input_file,
1322 gold_error(_("%s: not configured to support "
1323 "64-bit big-endian object"),
1330 #ifdef HAVE_TARGET_64_LITTLE
1331 elfcpp::Ehdr<64, false> ehdr(p);
1332 return make_elf_sized_object<64, false>(name, input_file,
1335 gold_error(_("%s: not configured to support "
1336 "64-bit little-endian object"),
1344 // Instantiate the templates we need. We could use the configure
1345 // script to restrict this to only the ones for implemented targets.
1347 #ifdef HAVE_TARGET_32_LITTLE
1349 class Sized_relobj<32, false>;
1352 #ifdef HAVE_TARGET_32_BIG
1354 class Sized_relobj<32, true>;
1357 #ifdef HAVE_TARGET_64_LITTLE
1359 class Sized_relobj<64, false>;
1362 #ifdef HAVE_TARGET_64_BIG
1364 class Sized_relobj<64, true>;
1367 #ifdef HAVE_TARGET_32_LITTLE
1369 struct Relocate_info<32, false>;
1372 #ifdef HAVE_TARGET_32_BIG
1374 struct Relocate_info<32, true>;
1377 #ifdef HAVE_TARGET_64_LITTLE
1379 struct Relocate_info<64, false>;
1382 #ifdef HAVE_TARGET_64_BIG
1384 struct Relocate_info<64, true>;
1387 } // End namespace gold.