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 "libiberty.h"
31 #include "target-select.h"
32 #include "dwarf_reader.h"
45 // Set the target based on fields in the ELF file header.
48 Object::set_target(int machine, int size, bool big_endian, int osabi,
51 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
53 gold_fatal(_("%s: unsupported ELF machine number %d"),
54 this->name().c_str(), machine);
55 this->target_ = target;
58 // Report an error for this object file. This is used by the
59 // elfcpp::Elf_file interface, and also called by the Object code
63 Object::error(const char* format, ...) const
66 va_start(args, format);
68 if (vasprintf(&buf, format, args) < 0)
71 gold_error(_("%s: %s"), this->name().c_str(), buf);
75 // Return a view of the contents of a section.
78 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
80 Location loc(this->do_section_contents(shndx));
81 *plen = loc.data_size;
82 return this->get_view(loc.file_offset, loc.data_size, cache);
85 // Read the section data into SD. This is code common to Sized_relobj
86 // and Sized_dynobj, so we put it into Object.
88 template<int size, bool big_endian>
90 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
91 Read_symbols_data* sd)
93 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
95 // Read the section headers.
96 const off_t shoff = elf_file->shoff();
97 const unsigned int shnum = this->shnum();
98 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
100 // Read the section names.
101 const unsigned char* pshdrs = sd->section_headers->data();
102 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
103 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
105 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
106 this->error(_("section name section has wrong type: %u"),
107 static_cast<unsigned int>(shdrnames.get_sh_type()));
109 sd->section_names_size = shdrnames.get_sh_size();
110 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
111 sd->section_names_size, false);
114 // If NAME is the name of a special .gnu.warning section, arrange for
115 // the warning to be issued. SHNDX is the section index. Return
116 // whether it is a warning section.
119 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
120 Symbol_table* symtab)
122 const char warn_prefix[] = ".gnu.warning.";
123 const int warn_prefix_len = sizeof warn_prefix - 1;
124 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
126 symtab->add_warning(name + warn_prefix_len, this, shndx);
132 // Class Sized_relobj.
134 template<int size, bool big_endian>
135 Sized_relobj<size, big_endian>::Sized_relobj(
136 const std::string& name,
137 Input_file* input_file,
139 const elfcpp::Ehdr<size, big_endian>& ehdr)
140 : Relobj(name, input_file, offset),
141 elf_file_(this, ehdr),
143 local_symbol_count_(0),
144 output_local_symbol_count_(0),
145 output_local_dynsym_count_(0),
147 local_symbol_offset_(0),
148 local_dynsym_offset_(0),
150 local_got_offsets_(),
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 // Return whether SHDR has the right type and flags to be a GNU
208 // .eh_frame section.
210 template<int size, bool big_endian>
212 Sized_relobj<size, big_endian>::check_eh_frame_flags(
213 const elfcpp::Shdr<size, big_endian>* shdr) const
215 return (shdr->get_sh_size() > 0
216 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
217 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
220 // Return whether there is a GNU .eh_frame section, given the section
221 // headers and the section names.
223 template<int size, bool big_endian>
225 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
227 off_t names_size) const
229 const unsigned int shnum = this->shnum();
230 const unsigned char* p = pshdrs + This::shdr_size;
231 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
233 typename This::Shdr shdr(p);
234 if (this->check_eh_frame_flags(&shdr))
236 if (shdr.get_sh_name() >= names_size)
238 this->error(_("bad section name offset for section %u: %lu"),
239 i, static_cast<unsigned long>(shdr.get_sh_name()));
243 const char* name = names + shdr.get_sh_name();
244 if (strcmp(name, ".eh_frame") == 0)
251 // Read the sections and symbols from an object file.
253 template<int size, bool big_endian>
255 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
257 this->read_section_data(&this->elf_file_, sd);
259 const unsigned char* const pshdrs = sd->section_headers->data();
261 this->find_symtab(pshdrs);
263 const unsigned char* namesu = sd->section_names->data();
264 const char* names = reinterpret_cast<const char*>(namesu);
265 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
266 this->has_eh_frame_ = true;
269 sd->symbols_size = 0;
270 sd->external_symbols_offset = 0;
271 sd->symbol_names = NULL;
272 sd->symbol_names_size = 0;
274 if (this->symtab_shndx_ == 0)
276 // No symbol table. Weird but legal.
280 // Get the symbol table section header.
281 typename This::Shdr symtabshdr(pshdrs
282 + this->symtab_shndx_ * This::shdr_size);
283 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
285 // If this object has a .eh_frame section, we need all the symbols.
286 // Otherwise we only need the external symbols. While it would be
287 // simpler to just always read all the symbols, I've seen object
288 // files with well over 2000 local symbols, which for a 64-bit
289 // object file format is over 5 pages that we don't need to read
292 const int sym_size = This::sym_size;
293 const unsigned int loccount = symtabshdr.get_sh_info();
294 this->local_symbol_count_ = loccount;
295 this->local_values_.resize(loccount);
296 off_t locsize = loccount * sym_size;
297 off_t dataoff = symtabshdr.get_sh_offset();
298 off_t datasize = symtabshdr.get_sh_size();
299 off_t extoff = dataoff + locsize;
300 off_t extsize = datasize - locsize;
302 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
303 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
305 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
307 // Read the section header for the symbol names.
308 unsigned int strtab_shndx = symtabshdr.get_sh_link();
309 if (strtab_shndx >= this->shnum())
311 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
314 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
315 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
317 this->error(_("symbol table name section has wrong type: %u"),
318 static_cast<unsigned int>(strtabshdr.get_sh_type()));
322 // Read the symbol names.
323 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
324 strtabshdr.get_sh_size(), true);
326 sd->symbols = fvsymtab;
327 sd->symbols_size = readsize;
328 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
329 sd->symbol_names = fvstrtab;
330 sd->symbol_names_size = strtabshdr.get_sh_size();
333 // Return the section index of symbol SYM. Set *VALUE to its value in
334 // the object file. Note that for a symbol which is not defined in
335 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
336 // it will not return the final value of the symbol in the link.
338 template<int size, bool big_endian>
340 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
344 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
348 const size_t count = symbols_size / This::sym_size;
349 gold_assert(sym < count);
351 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
352 *value = elfsym.get_st_value();
353 // FIXME: Handle SHN_XINDEX.
354 return elfsym.get_st_shndx();
357 // Return whether to include a section group in the link. LAYOUT is
358 // used to keep track of which section groups we have already seen.
359 // INDEX is the index of the section group and SHDR is the section
360 // header. If we do not want to include this group, we set bits in
361 // OMIT for each section which should be discarded.
363 template<int size, bool big_endian>
365 Sized_relobj<size, big_endian>::include_section_group(
368 const elfcpp::Shdr<size, big_endian>& shdr,
369 std::vector<bool>* omit)
371 // Read the section contents.
372 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
373 shdr.get_sh_size(), false);
374 const elfcpp::Elf_Word* pword =
375 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
377 // The first word contains flags. We only care about COMDAT section
378 // groups. Other section groups are always included in the link
379 // just like ordinary sections.
380 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
381 if ((flags & elfcpp::GRP_COMDAT) == 0)
384 // Look up the group signature, which is the name of a symbol. This
385 // is a lot of effort to go to to read a string. Why didn't they
386 // just use the name of the SHT_GROUP section as the group
389 // Get the appropriate symbol table header (this will normally be
390 // the single SHT_SYMTAB section, but in principle it need not be).
391 const unsigned int link = shdr.get_sh_link();
392 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
394 // Read the symbol table entry.
395 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
397 this->error(_("section group %u info %u out of range"),
398 index, shdr.get_sh_info());
401 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
402 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
403 elfcpp::Sym<size, big_endian> sym(psym);
405 // Read the symbol table names.
407 const unsigned char* psymnamesu;
408 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
410 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
412 // Get the section group signature.
413 if (sym.get_st_name() >= symnamelen)
415 this->error(_("symbol %u name offset %u out of range"),
416 shdr.get_sh_info(), sym.get_st_name());
420 const char* signature = psymnames + sym.get_st_name();
422 // It seems that some versions of gas will create a section group
423 // associated with a section symbol, and then fail to give a name to
424 // the section symbol. In such a case, use the name of the section.
427 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
429 secname = this->section_name(sym.get_st_shndx());
430 signature = secname.c_str();
433 // Record this section group, and see whether we've already seen one
434 // with the same signature.
435 if (layout->add_comdat(signature, true))
438 // This is a duplicate. We want to discard the sections in this
440 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
441 for (size_t i = 1; i < count; ++i)
443 elfcpp::Elf_Word secnum =
444 elfcpp::Swap<32, big_endian>::readval(pword + i);
445 if (secnum >= this->shnum())
447 this->error(_("section %u in section group %u out of range"),
451 (*omit)[secnum] = true;
457 // Whether to include a linkonce section in the link. NAME is the
458 // name of the section and SHDR is the section header.
460 // Linkonce sections are a GNU extension implemented in the original
461 // GNU linker before section groups were defined. The semantics are
462 // that we only include one linkonce section with a given name. The
463 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
464 // where T is the type of section and SYMNAME is the name of a symbol.
465 // In an attempt to make linkonce sections interact well with section
466 // groups, we try to identify SYMNAME and use it like a section group
467 // signature. We want to block section groups with that signature,
468 // but not other linkonce sections with that signature. We also use
469 // the full name of the linkonce section as a normal section group
472 template<int size, bool big_endian>
474 Sized_relobj<size, big_endian>::include_linkonce_section(
477 const elfcpp::Shdr<size, big_endian>&)
479 // In general the symbol name we want will be the string following
480 // the last '.'. However, we have to handle the case of
481 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
482 // some versions of gcc. So we use a heuristic: if the name starts
483 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
484 // we look for the last '.'. We can't always simply skip
485 // ".gnu.linkonce.X", because we have to deal with cases like
486 // ".gnu.linkonce.d.rel.ro.local".
487 const char* const linkonce_t = ".gnu.linkonce.t.";
489 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
490 symname = name + strlen(linkonce_t);
492 symname = strrchr(name, '.') + 1;
493 bool include1 = layout->add_comdat(symname, false);
494 bool include2 = layout->add_comdat(name, true);
495 return include1 && include2;
498 // Lay out the input sections. We walk through the sections and check
499 // whether they should be included in the link. If they should, we
500 // pass them to the Layout object, which will return an output section
503 template<int size, bool big_endian>
505 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
507 Read_symbols_data* sd)
509 const unsigned int shnum = this->shnum();
513 // Get the section headers.
514 const unsigned char* pshdrs = sd->section_headers->data();
516 // Get the section names.
517 const unsigned char* pnamesu = sd->section_names->data();
518 const char* pnames = reinterpret_cast<const char*>(pnamesu);
520 // For each section, record the index of the reloc section if any.
521 // Use 0 to mean that there is no reloc section, -1U to mean that
522 // there is more than one.
523 std::vector<unsigned int> reloc_shndx(shnum, 0);
524 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
525 // Skip the first, dummy, section.
526 pshdrs += This::shdr_size;
527 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
529 typename This::Shdr shdr(pshdrs);
531 unsigned int sh_type = shdr.get_sh_type();
532 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
534 unsigned int target_shndx = shdr.get_sh_info();
535 if (target_shndx == 0 || target_shndx >= shnum)
537 this->error(_("relocation section %u has bad info %u"),
542 if (reloc_shndx[target_shndx] != 0)
543 reloc_shndx[target_shndx] = -1U;
546 reloc_shndx[target_shndx] = i;
547 reloc_type[target_shndx] = sh_type;
552 std::vector<Map_to_output>& map_sections(this->map_to_output());
553 map_sections.resize(shnum);
555 // Whether we've seen a .note.GNU-stack section.
556 bool seen_gnu_stack = false;
557 // The flags of a .note.GNU-stack section.
558 uint64_t gnu_stack_flags = 0;
560 // Keep track of which sections to omit.
561 std::vector<bool> omit(shnum, false);
563 // Keep track of .eh_frame sections.
564 std::vector<unsigned int> eh_frame_sections;
566 // Skip the first, dummy, section.
567 pshdrs = sd->section_headers->data() + This::shdr_size;
568 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
570 typename This::Shdr shdr(pshdrs);
572 if (shdr.get_sh_name() >= sd->section_names_size)
574 this->error(_("bad section name offset for section %u: %lu"),
575 i, static_cast<unsigned long>(shdr.get_sh_name()));
579 const char* name = pnames + shdr.get_sh_name();
581 if (this->handle_gnu_warning_section(name, i, symtab))
583 if (!parameters->output_is_object())
587 // The .note.GNU-stack section is special. It gives the
588 // protection flags that this object file requires for the stack
590 if (strcmp(name, ".note.GNU-stack") == 0)
592 seen_gnu_stack = true;
593 gnu_stack_flags |= shdr.get_sh_flags();
597 bool discard = omit[i];
600 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
602 if (!this->include_section_group(layout, i, shdr, &omit))
605 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
606 && Layout::is_linkonce(name))
608 if (!this->include_linkonce_section(layout, name, shdr))
615 // Do not include this section in the link.
616 map_sections[i].output_section = NULL;
620 // The .eh_frame section is special. It holds exception frame
621 // information that we need to read in order to generate the
622 // exception frame header. We process these after all the other
623 // sections so that the exception frame reader can reliably
624 // determine which sections are being discarded, and discard the
625 // corresponding information.
626 if (!parameters->output_is_object()
627 && strcmp(name, ".eh_frame") == 0
628 && this->check_eh_frame_flags(&shdr))
630 eh_frame_sections.push_back(i);
635 Output_section* os = layout->layout(this, i, name, shdr,
636 reloc_shndx[i], reloc_type[i],
639 map_sections[i].output_section = os;
640 map_sections[i].offset = offset;
642 // If this section requires special handling, and if there are
643 // relocs that apply to it, then we must do the special handling
644 // before we apply the relocs.
645 if (offset == -1 && reloc_shndx[i] != 0)
646 this->set_relocs_must_follow_section_writes();
649 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
651 // Handle the .eh_frame sections at the end.
652 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
653 p != eh_frame_sections.end();
656 gold_assert(this->has_eh_frame_);
657 gold_assert(sd->external_symbols_offset != 0);
660 const unsigned char *pshdr;
661 pshdr = sd->section_headers->data() + i * This::shdr_size;
662 typename This::Shdr shdr(pshdr);
665 Output_section* os = layout->layout_eh_frame(this,
668 sd->symbol_names->data(),
669 sd->symbol_names_size,
674 map_sections[i].output_section = os;
675 map_sections[i].offset = offset;
677 // If this section requires special handling, and if there are
678 // relocs that apply to it, then we must do the special handling
679 // before we apply the relocs.
680 if (offset == -1 && reloc_shndx[i] != 0)
681 this->set_relocs_must_follow_section_writes();
684 delete sd->section_headers;
685 sd->section_headers = NULL;
686 delete sd->section_names;
687 sd->section_names = NULL;
690 // Add the symbols to the symbol table.
692 template<int size, bool big_endian>
694 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
695 Read_symbols_data* sd)
697 if (sd->symbols == NULL)
699 gold_assert(sd->symbol_names == NULL);
703 const int sym_size = This::sym_size;
704 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
706 if (static_cast<off_t>(symcount * sym_size)
707 != sd->symbols_size - sd->external_symbols_offset)
709 this->error(_("size of symbols is not multiple of symbol size"));
713 this->symbols_.resize(symcount);
715 const char* sym_names =
716 reinterpret_cast<const char*>(sd->symbol_names->data());
717 symtab->add_from_relobj(this,
718 sd->symbols->data() + sd->external_symbols_offset,
719 symcount, sym_names, sd->symbol_names_size,
724 delete sd->symbol_names;
725 sd->symbol_names = NULL;
728 // Finalize the local symbols. Here we add their names to *POOL and
729 // *DYNPOOL, and we add their values to THIS->LOCAL_VALUES_.
730 // This function is always called from the main thread. The actual
731 // output of the local symbols will occur in a separate task.
733 template<int size, bool big_endian>
735 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
738 gold_assert(this->symtab_shndx_ != -1U);
739 if (this->symtab_shndx_ == 0)
741 // This object has no symbols. Weird but legal.
745 // Read the symbol table section header.
746 const unsigned int symtab_shndx = this->symtab_shndx_;
747 typename This::Shdr symtabshdr(this,
748 this->elf_file_.section_header(symtab_shndx));
749 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
751 // Read the local symbols.
752 const int sym_size = This::sym_size;
753 const unsigned int loccount = this->local_symbol_count_;
754 gold_assert(loccount == symtabshdr.get_sh_info());
755 off_t locsize = loccount * sym_size;
756 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
759 // Read the symbol names.
760 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
762 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
765 const char* pnames = reinterpret_cast<const char*>(pnamesu);
767 // Loop over the local symbols.
769 const std::vector<Map_to_output>& mo(this->map_to_output());
770 unsigned int shnum = this->shnum();
771 unsigned int count = 0;
772 unsigned int dyncount = 0;
773 // Skip the first, dummy, symbol.
775 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
777 elfcpp::Sym<size, big_endian> sym(psyms);
779 Symbol_value<size>& lv(this->local_values_[i]);
781 unsigned int shndx = sym.get_st_shndx();
782 lv.set_input_shndx(shndx);
784 if (sym.get_st_type() == elfcpp::STT_SECTION)
785 lv.set_is_section_symbol();
786 else if (sym.get_st_type() == elfcpp::STT_TLS)
787 lv.set_is_tls_symbol();
789 // Save the input symbol value for use in do_finalize_local_symbols().
790 lv.set_input_value(sym.get_st_value());
792 // Decide whether this symbol should go into the output file.
794 if (shndx < shnum && mo[shndx].output_section == NULL)
796 lv.set_no_output_symtab_entry();
800 if (sym.get_st_type() == elfcpp::STT_SECTION)
802 lv.set_no_output_symtab_entry();
806 if (sym.get_st_name() >= strtab_size)
808 this->error(_("local symbol %u section name out of range: %u >= %u"),
809 i, sym.get_st_name(),
810 static_cast<unsigned int>(strtab_size));
811 lv.set_no_output_symtab_entry();
815 // Add the symbol to the symbol table string pool.
816 const char* name = pnames + sym.get_st_name();
817 pool->add(name, true, NULL);
820 // If needed, add the symbol to the dynamic symbol table string pool.
821 if (lv.needs_output_dynsym_entry())
823 dynpool->add(name, true, NULL);
828 this->output_local_symbol_count_ = count;
829 this->output_local_dynsym_count_ = dyncount;
832 // Finalize the local symbols. Here we add their values to
833 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
834 // This function is always called from the main thread. The actual
835 // output of the local symbols will occur in a separate task.
837 template<int size, bool big_endian>
839 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
842 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
844 const unsigned int loccount = this->local_symbol_count_;
845 this->local_symbol_offset_ = off;
847 const std::vector<Map_to_output>& mo(this->map_to_output());
848 unsigned int shnum = this->shnum();
850 for (unsigned int i = 1; i < loccount; ++i)
852 Symbol_value<size>& lv(this->local_values_[i]);
854 unsigned int shndx = lv.input_shndx();
856 // Set the output symbol value.
858 if (shndx >= elfcpp::SHN_LORESERVE)
860 if (shndx == elfcpp::SHN_ABS)
861 lv.set_output_value(lv.input_value());
864 // FIXME: Handle SHN_XINDEX.
865 this->error(_("unknown section index %u for local symbol %u"),
867 lv.set_output_value(0);
874 this->error(_("local symbol %u section index %u out of range"),
879 Output_section* os = mo[shndx].output_section;
883 lv.set_output_value(0);
886 else if (mo[shndx].offset == -1)
888 // Leave the input value in place for SHF_MERGE sections.
890 else if (lv.is_tls_symbol())
891 lv.set_output_value(mo[shndx].output_section->tls_offset()
895 lv.set_output_value(mo[shndx].output_section->address()
900 if (lv.needs_output_symtab_entry())
902 lv.set_output_symtab_index(index);
909 // Set the output dynamic symbol table indexes for the local variables.
911 template<int size, bool big_endian>
913 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
915 const unsigned int loccount = this->local_symbol_count_;
916 for (unsigned int i = 1; i < loccount; ++i)
918 Symbol_value<size>& lv(this->local_values_[i]);
919 if (lv.needs_output_dynsym_entry())
921 lv.set_output_dynsym_index(index);
928 // Set the offset where local dynamic symbol information will be stored.
929 // Returns the count of local symbols contributed to the symbol table by
932 template<int size, bool big_endian>
934 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
936 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
937 this->local_dynsym_offset_ = off;
938 return this->output_local_dynsym_count_;
941 // Return the value of the local symbol symndx.
942 template<int size, bool big_endian>
943 typename elfcpp::Elf_types<size>::Elf_Addr
944 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
946 gold_assert(symndx < this->local_symbol_count_);
947 gold_assert(symndx < this->local_values_.size());
948 const Symbol_value<size>& lv(this->local_values_[symndx]);
949 return lv.value(this, 0);
952 // Return the value of a local symbol defined in input section SHNDX,
953 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
954 // indicates whether the symbol is a section symbol. This handles
955 // SHF_MERGE sections.
956 template<int size, bool big_endian>
957 typename elfcpp::Elf_types<size>::Elf_Addr
958 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
960 bool is_section_symbol,
961 Address addend) const
963 const std::vector<Map_to_output>& mo(this->map_to_output());
964 Output_section* os = mo[shndx].output_section;
967 gold_assert(mo[shndx].offset == -1);
969 // Do the mapping required by the output section. If this is not a
970 // section symbol, then we want to map the symbol value, and then
971 // include the addend. If this is a section symbol, then we need to
972 // include the addend to figure out where in the section we are,
973 // before we do the mapping. This will do the right thing provided
974 // the assembler is careful to only convert a relocation in a merged
975 // section to a section symbol if there is a zero addend. If the
976 // assembler does not do this, then in general we can't know what to
977 // do, because we can't distinguish the addend for the instruction
978 // format from the addend for the section offset.
980 if (is_section_symbol)
981 return os->output_address(this, shndx, value + addend);
983 return addend + os->output_address(this, shndx, value);
986 // Write out the local symbols.
988 template<int size, bool big_endian>
990 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
991 const Stringpool* sympool,
992 const Stringpool* dynpool)
994 if (parameters->strip_all() && this->output_local_dynsym_count_ == 0)
997 gold_assert(this->symtab_shndx_ != -1U);
998 if (this->symtab_shndx_ == 0)
1000 // This object has no symbols. Weird but legal.
1004 // Read the symbol table section header.
1005 const unsigned int symtab_shndx = this->symtab_shndx_;
1006 typename This::Shdr symtabshdr(this,
1007 this->elf_file_.section_header(symtab_shndx));
1008 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1009 const unsigned int loccount = this->local_symbol_count_;
1010 gold_assert(loccount == symtabshdr.get_sh_info());
1012 // Read the local symbols.
1013 const int sym_size = This::sym_size;
1014 off_t locsize = loccount * sym_size;
1015 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1018 // Read the symbol names.
1019 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
1021 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1024 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1026 // Get views into the output file for the portions of the symbol table
1027 // and the dynamic symbol table that we will be writing.
1028 off_t output_size = this->output_local_symbol_count_ * sym_size;
1029 unsigned char* oview;
1030 if (output_size > 0)
1031 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1033 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1034 unsigned char* dyn_oview = NULL;
1035 if (dyn_output_size > 0)
1036 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1039 const std::vector<Map_to_output>& mo(this->map_to_output());
1041 gold_assert(this->local_values_.size() == loccount);
1043 unsigned char* ov = oview;
1044 unsigned char* dyn_ov = dyn_oview;
1046 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1048 elfcpp::Sym<size, big_endian> isym(psyms);
1050 unsigned int st_shndx = isym.get_st_shndx();
1051 if (st_shndx < elfcpp::SHN_LORESERVE)
1053 gold_assert(st_shndx < mo.size());
1054 if (mo[st_shndx].output_section == NULL)
1056 st_shndx = mo[st_shndx].output_section->out_shndx();
1059 // Write the symbol to the output symbol table.
1060 if (!parameters->strip_all()
1061 && this->local_values_[i].needs_output_symtab_entry())
1063 elfcpp::Sym_write<size, big_endian> osym(ov);
1065 gold_assert(isym.get_st_name() < strtab_size);
1066 const char* name = pnames + isym.get_st_name();
1067 osym.put_st_name(sympool->get_offset(name));
1068 osym.put_st_value(this->local_values_[i].value(this, 0));
1069 osym.put_st_size(isym.get_st_size());
1070 osym.put_st_info(isym.get_st_info());
1071 osym.put_st_other(isym.get_st_other());
1072 osym.put_st_shndx(st_shndx);
1077 // Write the symbol to the output dynamic symbol table.
1078 if (this->local_values_[i].needs_output_dynsym_entry())
1080 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1081 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1083 gold_assert(isym.get_st_name() < strtab_size);
1084 const char* name = pnames + isym.get_st_name();
1085 osym.put_st_name(dynpool->get_offset(name));
1086 osym.put_st_value(this->local_values_[i].value(this, 0));
1087 osym.put_st_size(isym.get_st_size());
1088 osym.put_st_info(isym.get_st_info());
1089 osym.put_st_other(isym.get_st_other());
1090 osym.put_st_shndx(st_shndx);
1097 if (output_size > 0)
1099 gold_assert(ov - oview == output_size);
1100 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1103 if (dyn_output_size > 0)
1105 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1106 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1111 // Set *INFO to symbolic information about the offset OFFSET in the
1112 // section SHNDX. Return true if we found something, false if we
1115 template<int size, bool big_endian>
1117 Sized_relobj<size, big_endian>::get_symbol_location_info(
1120 Symbol_location_info* info)
1122 if (this->symtab_shndx_ == 0)
1126 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1130 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1132 const unsigned char* symbol_names_u =
1133 this->section_contents(symbol_names_shndx, &names_size, false);
1134 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1136 const int sym_size = This::sym_size;
1137 const size_t count = symbols_size / sym_size;
1139 const unsigned char* p = symbols;
1140 for (size_t i = 0; i < count; ++i, p += sym_size)
1142 elfcpp::Sym<size, big_endian> sym(p);
1144 if (sym.get_st_type() == elfcpp::STT_FILE)
1146 if (sym.get_st_name() >= names_size)
1147 info->source_file = "(invalid)";
1149 info->source_file = symbol_names + sym.get_st_name();
1151 else if (sym.get_st_shndx() == shndx
1152 && static_cast<off_t>(sym.get_st_value()) <= offset
1153 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1156 if (sym.get_st_name() > names_size)
1157 info->enclosing_symbol_name = "(invalid)";
1160 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1161 if (parameters->demangle())
1163 char* demangled_name = cplus_demangle(
1164 info->enclosing_symbol_name.c_str(),
1165 DMGL_ANSI | DMGL_PARAMS);
1166 if (demangled_name != NULL)
1168 info->enclosing_symbol_name.assign(demangled_name);
1169 free(demangled_name);
1180 // Input_objects methods.
1182 // Add a regular relocatable object to the list. Return false if this
1183 // object should be ignored.
1186 Input_objects::add_object(Object* obj)
1188 Target* target = obj->target();
1189 if (this->target_ == NULL)
1190 this->target_ = target;
1191 else if (this->target_ != target)
1193 gold_error(_("%s: incompatible target"), obj->name().c_str());
1197 if (!obj->is_dynamic())
1198 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1201 // See if this is a duplicate SONAME.
1202 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1203 const char* soname = dynobj->soname();
1205 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1206 this->sonames_.insert(soname);
1209 // We have already seen a dynamic object with this soname.
1213 this->dynobj_list_.push_back(dynobj);
1215 // If this is -lc, remember the directory in which we found it.
1216 // We use this when issuing warnings about undefined symbols: as
1217 // a heuristic, we don't warn about system libraries found in
1218 // the same directory as -lc.
1219 if (strncmp(soname, "libc.so", 7) == 0)
1221 const char* object_name = dynobj->name().c_str();
1222 const char* base = lbasename(object_name);
1223 if (base != object_name)
1224 this->system_library_directory_.assign(object_name,
1225 base - 1 - object_name);
1229 set_parameters_target(target);
1234 // Return whether an object was found in the system library directory.
1237 Input_objects::found_in_system_library_directory(const Object* object) const
1239 return (!this->system_library_directory_.empty()
1240 && object->name().compare(0,
1241 this->system_library_directory_.size(),
1242 this->system_library_directory_) == 0);
1245 // For each dynamic object, record whether we've seen all of its
1246 // explicit dependencies.
1249 Input_objects::check_dynamic_dependencies() const
1251 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1252 p != this->dynobj_list_.end();
1255 const Dynobj::Needed& needed((*p)->needed());
1256 bool found_all = true;
1257 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1258 pneeded != needed.end();
1261 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1267 (*p)->set_has_unknown_needed_entries(!found_all);
1271 // Relocate_info methods.
1273 // Return a string describing the location of a relocation. This is
1274 // only used in error messages.
1276 template<int size, bool big_endian>
1278 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1280 // See if we can get line-number information from debugging sections.
1281 std::string filename;
1282 std::string file_and_lineno; // Better than filename-only, if available.
1284 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1285 // This will be "" if we failed to parse the debug info for any reason.
1286 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1288 std::string ret(this->object->name());
1290 Symbol_location_info info;
1291 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1293 ret += " in function ";
1294 ret += info.enclosing_symbol_name;
1296 filename = info.source_file;
1299 if (!file_and_lineno.empty())
1300 ret += file_and_lineno;
1303 if (!filename.empty())
1306 ret += this->object->section_name(this->data_shndx);
1308 // Offsets into sections have to be positive.
1309 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1316 } // End namespace gold.
1321 using namespace gold;
1323 // Read an ELF file with the header and return the appropriate
1324 // instance of Object.
1326 template<int size, bool big_endian>
1328 make_elf_sized_object(const std::string& name, Input_file* input_file,
1329 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1331 int et = ehdr.get_e_type();
1332 if (et == elfcpp::ET_REL)
1334 Sized_relobj<size, big_endian>* obj =
1335 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1339 else if (et == elfcpp::ET_DYN)
1341 Sized_dynobj<size, big_endian>* obj =
1342 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1348 gold_error(_("%s: unsupported ELF file type %d"),
1354 } // End anonymous namespace.
1359 // Read an ELF file and return the appropriate instance of Object.
1362 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1363 const unsigned char* p, off_t bytes)
1365 if (bytes < elfcpp::EI_NIDENT)
1367 gold_error(_("%s: ELF file too short"), name.c_str());
1371 int v = p[elfcpp::EI_VERSION];
1372 if (v != elfcpp::EV_CURRENT)
1374 if (v == elfcpp::EV_NONE)
1375 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1377 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1381 int c = p[elfcpp::EI_CLASS];
1382 if (c == elfcpp::ELFCLASSNONE)
1384 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1387 else if (c != elfcpp::ELFCLASS32
1388 && c != elfcpp::ELFCLASS64)
1390 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1394 int d = p[elfcpp::EI_DATA];
1395 if (d == elfcpp::ELFDATANONE)
1397 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1400 else if (d != elfcpp::ELFDATA2LSB
1401 && d != elfcpp::ELFDATA2MSB)
1403 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1407 bool big_endian = d == elfcpp::ELFDATA2MSB;
1409 if (c == elfcpp::ELFCLASS32)
1411 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1413 gold_error(_("%s: ELF file too short"), name.c_str());
1418 #ifdef HAVE_TARGET_32_BIG
1419 elfcpp::Ehdr<32, true> ehdr(p);
1420 return make_elf_sized_object<32, true>(name, input_file,
1423 gold_error(_("%s: not configured to support "
1424 "32-bit big-endian object"),
1431 #ifdef HAVE_TARGET_32_LITTLE
1432 elfcpp::Ehdr<32, false> ehdr(p);
1433 return make_elf_sized_object<32, false>(name, input_file,
1436 gold_error(_("%s: not configured to support "
1437 "32-bit little-endian object"),
1445 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1447 gold_error(_("%s: ELF file too short"), name.c_str());
1452 #ifdef HAVE_TARGET_64_BIG
1453 elfcpp::Ehdr<64, true> ehdr(p);
1454 return make_elf_sized_object<64, true>(name, input_file,
1457 gold_error(_("%s: not configured to support "
1458 "64-bit big-endian object"),
1465 #ifdef HAVE_TARGET_64_LITTLE
1466 elfcpp::Ehdr<64, false> ehdr(p);
1467 return make_elf_sized_object<64, false>(name, input_file,
1470 gold_error(_("%s: not configured to support "
1471 "64-bit little-endian object"),
1479 // Instantiate the templates we need. We could use the configure
1480 // script to restrict this to only the ones for implemented targets.
1482 #ifdef HAVE_TARGET_32_LITTLE
1484 class Sized_relobj<32, false>;
1487 #ifdef HAVE_TARGET_32_BIG
1489 class Sized_relobj<32, true>;
1492 #ifdef HAVE_TARGET_64_LITTLE
1494 class Sized_relobj<64, false>;
1497 #ifdef HAVE_TARGET_64_BIG
1499 class Sized_relobj<64, true>;
1502 #ifdef HAVE_TARGET_32_LITTLE
1504 struct Relocate_info<32, false>;
1507 #ifdef HAVE_TARGET_32_BIG
1509 struct Relocate_info<32, true>;
1512 #ifdef HAVE_TARGET_64_LITTLE
1514 struct Relocate_info<64, false>;
1517 #ifdef HAVE_TARGET_64_BIG
1519 struct Relocate_info<64, true>;
1522 } // End namespace gold.