1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008 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, section_size_type* plen,
81 Location loc(this->do_section_contents(shndx));
82 *plen = convert_to_section_size_type(loc.data_size);
83 return this->get_view(loc.file_offset, *plen, true, cache);
86 // Read the section data into SD. This is code common to Sized_relobj
87 // and Sized_dynobj, so we put it into Object.
89 template<int size, bool big_endian>
91 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
92 Read_symbols_data* sd)
94 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
96 // Read the section headers.
97 const off_t shoff = elf_file->shoff();
98 const unsigned int shnum = this->shnum();
99 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
102 // Read the section names.
103 const unsigned char* pshdrs = sd->section_headers->data();
104 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
105 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
107 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
108 this->error(_("section name section has wrong type: %u"),
109 static_cast<unsigned int>(shdrnames.get_sh_type()));
111 sd->section_names_size =
112 convert_to_section_size_type(shdrnames.get_sh_size());
113 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
114 sd->section_names_size, false,
118 // If NAME is the name of a special .gnu.warning section, arrange for
119 // the warning to be issued. SHNDX is the section index. Return
120 // whether it is a warning section.
123 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
124 Symbol_table* symtab)
126 const char warn_prefix[] = ".gnu.warning.";
127 const int warn_prefix_len = sizeof warn_prefix - 1;
128 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
130 // Read the section contents to get the warning text. It would
131 // be nicer if we only did this if we have to actually issue a
132 // warning. Unfortunately, warnings are issued as we relocate
133 // sections. That means that we can not lock the object then,
134 // as we might try to issue the same warning multiple times
136 section_size_type len;
137 const unsigned char* contents = this->section_contents(shndx, &len,
139 std::string warning(reinterpret_cast<const char*>(contents), len);
140 symtab->add_warning(name + warn_prefix_len, this, warning);
146 // Class Sized_relobj.
148 template<int size, bool big_endian>
149 Sized_relobj<size, big_endian>::Sized_relobj(
150 const std::string& name,
151 Input_file* input_file,
153 const elfcpp::Ehdr<size, big_endian>& ehdr)
154 : Relobj(name, input_file, offset),
155 elf_file_(this, ehdr),
157 local_symbol_count_(0),
158 output_local_symbol_count_(0),
159 output_local_dynsym_count_(0),
161 local_symbol_offset_(0),
162 local_dynsym_offset_(0),
164 local_got_offsets_(),
169 template<int size, bool big_endian>
170 Sized_relobj<size, big_endian>::~Sized_relobj()
174 // Set up an object file based on the file header. This sets up the
175 // target and reads the section information.
177 template<int size, bool big_endian>
179 Sized_relobj<size, big_endian>::setup(
180 const elfcpp::Ehdr<size, big_endian>& ehdr)
182 this->set_target(ehdr.get_e_machine(), size, big_endian,
183 ehdr.get_e_ident()[elfcpp::EI_OSABI],
184 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
186 const unsigned int shnum = this->elf_file_.shnum();
187 this->set_shnum(shnum);
190 // Find the SHT_SYMTAB section, given the section headers. The ELF
191 // standard says that maybe in the future there can be more than one
192 // SHT_SYMTAB section. Until somebody figures out how that could
193 // work, we assume there is only one.
195 template<int size, bool big_endian>
197 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
199 const unsigned int shnum = this->shnum();
200 this->symtab_shndx_ = 0;
203 // Look through the sections in reverse order, since gas tends
204 // to put the symbol table at the end.
205 const unsigned char* p = pshdrs + shnum * This::shdr_size;
206 unsigned int i = shnum;
210 p -= This::shdr_size;
211 typename This::Shdr shdr(p);
212 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
214 this->symtab_shndx_ = i;
221 // Return whether SHDR has the right type and flags to be a GNU
222 // .eh_frame section.
224 template<int size, bool big_endian>
226 Sized_relobj<size, big_endian>::check_eh_frame_flags(
227 const elfcpp::Shdr<size, big_endian>* shdr) const
229 return (shdr->get_sh_size() > 0
230 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
231 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
234 // Return whether there is a GNU .eh_frame section, given the section
235 // headers and the section names.
237 template<int size, bool big_endian>
239 Sized_relobj<size, big_endian>::find_eh_frame(
240 const unsigned char* pshdrs,
242 section_size_type names_size) const
244 const unsigned int shnum = this->shnum();
245 const unsigned char* p = pshdrs + This::shdr_size;
246 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
248 typename This::Shdr shdr(p);
249 if (this->check_eh_frame_flags(&shdr))
251 if (shdr.get_sh_name() >= names_size)
253 this->error(_("bad section name offset for section %u: %lu"),
254 i, static_cast<unsigned long>(shdr.get_sh_name()));
258 const char* name = names + shdr.get_sh_name();
259 if (strcmp(name, ".eh_frame") == 0)
266 // Read the sections and symbols from an object file.
268 template<int size, bool big_endian>
270 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
272 this->read_section_data(&this->elf_file_, sd);
274 const unsigned char* const pshdrs = sd->section_headers->data();
276 this->find_symtab(pshdrs);
278 const unsigned char* namesu = sd->section_names->data();
279 const char* names = reinterpret_cast<const char*>(namesu);
280 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
282 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
283 this->has_eh_frame_ = true;
287 sd->symbols_size = 0;
288 sd->external_symbols_offset = 0;
289 sd->symbol_names = NULL;
290 sd->symbol_names_size = 0;
292 if (this->symtab_shndx_ == 0)
294 // No symbol table. Weird but legal.
298 // Get the symbol table section header.
299 typename This::Shdr symtabshdr(pshdrs
300 + this->symtab_shndx_ * This::shdr_size);
301 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
303 // If this object has a .eh_frame section, we need all the symbols.
304 // Otherwise we only need the external symbols. While it would be
305 // simpler to just always read all the symbols, I've seen object
306 // files with well over 2000 local symbols, which for a 64-bit
307 // object file format is over 5 pages that we don't need to read
310 const int sym_size = This::sym_size;
311 const unsigned int loccount = symtabshdr.get_sh_info();
312 this->local_symbol_count_ = loccount;
313 this->local_values_.resize(loccount);
314 section_offset_type locsize = loccount * sym_size;
315 off_t dataoff = symtabshdr.get_sh_offset();
316 section_size_type datasize =
317 convert_to_section_size_type(symtabshdr.get_sh_size());
318 off_t extoff = dataoff + locsize;
319 section_size_type extsize = datasize - locsize;
321 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
322 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
324 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
326 // Read the section header for the symbol names.
327 unsigned int strtab_shndx = symtabshdr.get_sh_link();
328 if (strtab_shndx >= this->shnum())
330 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
333 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
334 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
336 this->error(_("symbol table name section has wrong type: %u"),
337 static_cast<unsigned int>(strtabshdr.get_sh_type()));
341 // Read the symbol names.
342 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
343 strtabshdr.get_sh_size(),
346 sd->symbols = fvsymtab;
347 sd->symbols_size = readsize;
348 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
349 sd->symbol_names = fvstrtab;
350 sd->symbol_names_size =
351 convert_to_section_size_type(strtabshdr.get_sh_size());
354 // Return the section index of symbol SYM. Set *VALUE to its value in
355 // the object file. Note that for a symbol which is not defined in
356 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
357 // it will not return the final value of the symbol in the link.
359 template<int size, bool big_endian>
361 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
364 section_size_type symbols_size;
365 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
369 const size_t count = symbols_size / This::sym_size;
370 gold_assert(sym < count);
372 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
373 *value = elfsym.get_st_value();
374 // FIXME: Handle SHN_XINDEX.
375 return elfsym.get_st_shndx();
378 // Return whether to include a section group in the link. LAYOUT is
379 // used to keep track of which section groups we have already seen.
380 // INDEX is the index of the section group and SHDR is the section
381 // header. If we do not want to include this group, we set bits in
382 // OMIT for each section which should be discarded.
384 template<int size, bool big_endian>
386 Sized_relobj<size, big_endian>::include_section_group(
387 Symbol_table* symtab,
391 const elfcpp::Shdr<size, big_endian>& shdr,
392 std::vector<bool>* omit)
394 // Read the section contents.
395 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
396 shdr.get_sh_size(), true, false);
397 const elfcpp::Elf_Word* pword =
398 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
400 // The first word contains flags. We only care about COMDAT section
401 // groups. Other section groups are always included in the link
402 // just like ordinary sections.
403 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
405 // Look up the group signature, which is the name of a symbol. This
406 // is a lot of effort to go to to read a string. Why didn't they
407 // just have the group signature point into the string table, rather
408 // than indirect through a symbol?
410 // Get the appropriate symbol table header (this will normally be
411 // the single SHT_SYMTAB section, but in principle it need not be).
412 const unsigned int link = shdr.get_sh_link();
413 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
415 // Read the symbol table entry.
416 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
418 this->error(_("section group %u info %u out of range"),
419 index, shdr.get_sh_info());
422 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
423 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
425 elfcpp::Sym<size, big_endian> sym(psym);
427 // Read the symbol table names.
428 section_size_type symnamelen;
429 const unsigned char* psymnamesu;
430 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
432 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
434 // Get the section group signature.
435 if (sym.get_st_name() >= symnamelen)
437 this->error(_("symbol %u name offset %u out of range"),
438 shdr.get_sh_info(), sym.get_st_name());
442 const char* signature = psymnames + sym.get_st_name();
444 // It seems that some versions of gas will create a section group
445 // associated with a section symbol, and then fail to give a name to
446 // the section symbol. In such a case, use the name of the section.
449 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
451 secname = this->section_name(sym.get_st_shndx());
452 signature = secname.c_str();
455 // Record this section group, and see whether we've already seen one
456 // with the same signature.
458 if ((flags & elfcpp::GRP_COMDAT) == 0
459 || layout->add_comdat(signature, true))
461 if (parameters->options().relocatable())
462 layout->layout_group(symtab, this, index, name, signature, shdr,
467 // This is a duplicate. We want to discard the sections in this
469 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
470 for (size_t i = 1; i < count; ++i)
472 elfcpp::Elf_Word secnum =
473 elfcpp::Swap<32, big_endian>::readval(pword + i);
474 if (secnum >= this->shnum())
476 this->error(_("section %u in section group %u out of range"),
480 (*omit)[secnum] = true;
486 // Whether to include a linkonce section in the link. NAME is the
487 // name of the section and SHDR is the section header.
489 // Linkonce sections are a GNU extension implemented in the original
490 // GNU linker before section groups were defined. The semantics are
491 // that we only include one linkonce section with a given name. The
492 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
493 // where T is the type of section and SYMNAME is the name of a symbol.
494 // In an attempt to make linkonce sections interact well with section
495 // groups, we try to identify SYMNAME and use it like a section group
496 // signature. We want to block section groups with that signature,
497 // but not other linkonce sections with that signature. We also use
498 // the full name of the linkonce section as a normal section group
501 template<int size, bool big_endian>
503 Sized_relobj<size, big_endian>::include_linkonce_section(
506 const elfcpp::Shdr<size, big_endian>&)
508 // In general the symbol name we want will be the string following
509 // the last '.'. However, we have to handle the case of
510 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
511 // some versions of gcc. So we use a heuristic: if the name starts
512 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
513 // we look for the last '.'. We can't always simply skip
514 // ".gnu.linkonce.X", because we have to deal with cases like
515 // ".gnu.linkonce.d.rel.ro.local".
516 const char* const linkonce_t = ".gnu.linkonce.t.";
518 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
519 symname = name + strlen(linkonce_t);
521 symname = strrchr(name, '.') + 1;
522 bool include1 = layout->add_comdat(symname, false);
523 bool include2 = layout->add_comdat(name, true);
524 return include1 && include2;
527 // Lay out the input sections. We walk through the sections and check
528 // whether they should be included in the link. If they should, we
529 // pass them to the Layout object, which will return an output section
532 template<int size, bool big_endian>
534 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
536 Read_symbols_data* sd)
538 const unsigned int shnum = this->shnum();
542 // Get the section headers.
543 const unsigned char* pshdrs = sd->section_headers->data();
545 // Get the section names.
546 const unsigned char* pnamesu = sd->section_names->data();
547 const char* pnames = reinterpret_cast<const char*>(pnamesu);
549 // For each section, record the index of the reloc section if any.
550 // Use 0 to mean that there is no reloc section, -1U to mean that
551 // there is more than one.
552 std::vector<unsigned int> reloc_shndx(shnum, 0);
553 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
554 // Skip the first, dummy, section.
555 pshdrs += This::shdr_size;
556 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
558 typename This::Shdr shdr(pshdrs);
560 unsigned int sh_type = shdr.get_sh_type();
561 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
563 unsigned int target_shndx = shdr.get_sh_info();
564 if (target_shndx == 0 || target_shndx >= shnum)
566 this->error(_("relocation section %u has bad info %u"),
571 if (reloc_shndx[target_shndx] != 0)
572 reloc_shndx[target_shndx] = -1U;
575 reloc_shndx[target_shndx] = i;
576 reloc_type[target_shndx] = sh_type;
581 std::vector<Map_to_output>& map_sections(this->map_to_output());
582 map_sections.resize(shnum);
584 // If we are only linking for symbols, then there is nothing else to
586 if (this->input_file()->just_symbols())
588 delete sd->section_headers;
589 sd->section_headers = NULL;
590 delete sd->section_names;
591 sd->section_names = NULL;
595 // Whether we've seen a .note.GNU-stack section.
596 bool seen_gnu_stack = false;
597 // The flags of a .note.GNU-stack section.
598 uint64_t gnu_stack_flags = 0;
600 // Keep track of which sections to omit.
601 std::vector<bool> omit(shnum, false);
603 // Keep track of reloc sections when emitting relocations.
604 const bool relocatable = parameters->options().relocatable();
605 const bool emit_relocs = (relocatable
606 || parameters->options().emit_relocs());
607 std::vector<unsigned int> reloc_sections;
609 // Keep track of .eh_frame sections.
610 std::vector<unsigned int> eh_frame_sections;
612 // Skip the first, dummy, section.
613 pshdrs = sd->section_headers->data() + This::shdr_size;
614 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
616 typename This::Shdr shdr(pshdrs);
618 if (shdr.get_sh_name() >= sd->section_names_size)
620 this->error(_("bad section name offset for section %u: %lu"),
621 i, static_cast<unsigned long>(shdr.get_sh_name()));
625 const char* name = pnames + shdr.get_sh_name();
627 if (this->handle_gnu_warning_section(name, i, symtab))
633 // The .note.GNU-stack section is special. It gives the
634 // protection flags that this object file requires for the stack
636 if (strcmp(name, ".note.GNU-stack") == 0)
638 seen_gnu_stack = true;
639 gnu_stack_flags |= shdr.get_sh_flags();
643 bool discard = omit[i];
646 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
648 if (!this->include_section_group(symtab, layout, i, name, shdr,
652 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
653 && Layout::is_linkonce(name))
655 if (!this->include_linkonce_section(layout, name, shdr))
662 // Do not include this section in the link.
663 map_sections[i].output_section = NULL;
667 // When doing a relocatable link we are going to copy input
668 // reloc sections into the output. We only want to copy the
669 // ones associated with sections which are not being discarded.
670 // However, we don't know that yet for all sections. So save
671 // reloc sections and process them later.
673 && (shdr.get_sh_type() == elfcpp::SHT_REL
674 || shdr.get_sh_type() == elfcpp::SHT_RELA))
676 reloc_sections.push_back(i);
680 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
683 // The .eh_frame section is special. It holds exception frame
684 // information that we need to read in order to generate the
685 // exception frame header. We process these after all the other
686 // sections so that the exception frame reader can reliably
687 // determine which sections are being discarded, and discard the
688 // corresponding information.
690 && strcmp(name, ".eh_frame") == 0
691 && this->check_eh_frame_flags(&shdr))
693 eh_frame_sections.push_back(i);
698 Output_section* os = layout->layout(this, i, name, shdr,
699 reloc_shndx[i], reloc_type[i],
702 map_sections[i].output_section = os;
703 map_sections[i].offset = offset;
705 // If this section requires special handling, and if there are
706 // relocs that apply to it, then we must do the special handling
707 // before we apply the relocs.
708 if (offset == -1 && reloc_shndx[i] != 0)
709 this->set_relocs_must_follow_section_writes();
712 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
714 // When doing a relocatable link handle the reloc sections at the
717 this->size_relocatable_relocs();
718 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
719 p != reloc_sections.end();
723 const unsigned char* pshdr;
724 pshdr = sd->section_headers->data() + i * This::shdr_size;
725 typename This::Shdr shdr(pshdr);
727 unsigned int data_shndx = shdr.get_sh_info();
728 if (data_shndx >= shnum)
730 // We already warned about this above.
734 Output_section* data_section = map_sections[data_shndx].output_section;
735 if (data_section == NULL)
737 map_sections[i].output_section = NULL;
741 Relocatable_relocs* rr = new Relocatable_relocs();
742 this->set_relocatable_relocs(i, rr);
744 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
746 map_sections[i].output_section = os;
747 map_sections[i].offset = -1;
750 // Handle the .eh_frame sections at the end.
751 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
752 p != eh_frame_sections.end();
755 gold_assert(this->has_eh_frame_);
756 gold_assert(sd->external_symbols_offset != 0);
759 const unsigned char *pshdr;
760 pshdr = sd->section_headers->data() + i * This::shdr_size;
761 typename This::Shdr shdr(pshdr);
764 Output_section* os = layout->layout_eh_frame(this,
767 sd->symbol_names->data(),
768 sd->symbol_names_size,
773 map_sections[i].output_section = os;
774 map_sections[i].offset = offset;
776 // If this section requires special handling, and if there are
777 // relocs that apply to it, then we must do the special handling
778 // before we apply the relocs.
779 if (offset == -1 && reloc_shndx[i] != 0)
780 this->set_relocs_must_follow_section_writes();
783 delete sd->section_headers;
784 sd->section_headers = NULL;
785 delete sd->section_names;
786 sd->section_names = NULL;
789 // Add the symbols to the symbol table.
791 template<int size, bool big_endian>
793 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
794 Read_symbols_data* sd)
796 if (sd->symbols == NULL)
798 gold_assert(sd->symbol_names == NULL);
802 const int sym_size = This::sym_size;
803 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
805 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
807 this->error(_("size of symbols is not multiple of symbol size"));
811 this->symbols_.resize(symcount);
813 const char* sym_names =
814 reinterpret_cast<const char*>(sd->symbol_names->data());
815 symtab->add_from_relobj(this,
816 sd->symbols->data() + sd->external_symbols_offset,
817 symcount, sym_names, sd->symbol_names_size,
822 delete sd->symbol_names;
823 sd->symbol_names = NULL;
826 // First pass over the local symbols. Here we add their names to
827 // *POOL and *DYNPOOL, and we store the symbol value in
828 // THIS->LOCAL_VALUES_. This function is always called from a
829 // singleton thread. This is followed by a call to
830 // finalize_local_symbols.
832 template<int size, bool big_endian>
834 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
837 gold_assert(this->symtab_shndx_ != -1U);
838 if (this->symtab_shndx_ == 0)
840 // This object has no symbols. Weird but legal.
844 // Read the symbol table section header.
845 const unsigned int symtab_shndx = this->symtab_shndx_;
846 typename This::Shdr symtabshdr(this,
847 this->elf_file_.section_header(symtab_shndx));
848 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
850 // Read the local symbols.
851 const int sym_size = This::sym_size;
852 const unsigned int loccount = this->local_symbol_count_;
853 gold_assert(loccount == symtabshdr.get_sh_info());
854 off_t locsize = loccount * sym_size;
855 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
856 locsize, true, true);
858 // Read the symbol names.
859 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
860 section_size_type strtab_size;
861 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
864 const char* pnames = reinterpret_cast<const char*>(pnamesu);
866 // Loop over the local symbols.
868 const std::vector<Map_to_output>& mo(this->map_to_output());
869 unsigned int shnum = this->shnum();
870 unsigned int count = 0;
871 unsigned int dyncount = 0;
872 // Skip the first, dummy, symbol.
874 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
876 elfcpp::Sym<size, big_endian> sym(psyms);
878 Symbol_value<size>& lv(this->local_values_[i]);
880 unsigned int shndx = sym.get_st_shndx();
881 lv.set_input_shndx(shndx);
883 if (sym.get_st_type() == elfcpp::STT_SECTION)
884 lv.set_is_section_symbol();
885 else if (sym.get_st_type() == elfcpp::STT_TLS)
886 lv.set_is_tls_symbol();
888 // Save the input symbol value for use in do_finalize_local_symbols().
889 lv.set_input_value(sym.get_st_value());
891 // Decide whether this symbol should go into the output file.
893 if (shndx < shnum && mo[shndx].output_section == NULL)
895 lv.set_no_output_symtab_entry();
896 gold_assert(!lv.needs_output_dynsym_entry());
900 if (sym.get_st_type() == elfcpp::STT_SECTION)
902 lv.set_no_output_symtab_entry();
903 gold_assert(!lv.needs_output_dynsym_entry());
907 if (sym.get_st_name() >= strtab_size)
909 this->error(_("local symbol %u section name out of range: %u >= %u"),
910 i, sym.get_st_name(),
911 static_cast<unsigned int>(strtab_size));
912 lv.set_no_output_symtab_entry();
916 // Add the symbol to the symbol table string pool.
917 const char* name = pnames + sym.get_st_name();
918 pool->add(name, true, NULL);
921 // If needed, add the symbol to the dynamic symbol table string pool.
922 if (lv.needs_output_dynsym_entry())
924 dynpool->add(name, true, NULL);
929 this->output_local_symbol_count_ = count;
930 this->output_local_dynsym_count_ = dyncount;
933 // Finalize the local symbols. Here we set the final value in
934 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
935 // This function is always called from a singleton thread. The actual
936 // output of the local symbols will occur in a separate task.
938 template<int size, bool big_endian>
940 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
943 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
945 const unsigned int loccount = this->local_symbol_count_;
946 this->local_symbol_offset_ = off;
948 const std::vector<Map_to_output>& mo(this->map_to_output());
949 unsigned int shnum = this->shnum();
951 for (unsigned int i = 1; i < loccount; ++i)
953 Symbol_value<size>& lv(this->local_values_[i]);
955 unsigned int shndx = lv.input_shndx();
957 // Set the output symbol value.
959 if (shndx >= elfcpp::SHN_LORESERVE)
961 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
962 lv.set_output_value(lv.input_value());
965 // FIXME: Handle SHN_XINDEX.
966 this->error(_("unknown section index %u for local symbol %u"),
968 lv.set_output_value(0);
975 this->error(_("local symbol %u section index %u out of range"),
980 Output_section* os = mo[shndx].output_section;
984 lv.set_output_value(0);
987 else if (mo[shndx].offset == -1)
989 // This is a SHF_MERGE section or one which otherwise
990 // requires special handling. We get the output address
991 // of the start of the merged section. If this is not a
992 // section symbol, we can then determine the final
993 // value. If it is a section symbol, we can not, as in
994 // that case we have to consider the addend to determine
995 // the value to use in a relocation.
996 if (!lv.is_section_symbol())
997 lv.set_output_value(os->output_address(this, shndx,
1001 section_offset_type start =
1002 os->starting_output_address(this, shndx);
1003 Merged_symbol_value<size>* msv =
1004 new Merged_symbol_value<size>(lv.input_value(), start);
1005 lv.set_merged_symbol_value(msv);
1008 else if (lv.is_tls_symbol())
1009 lv.set_output_value(os->tls_offset()
1011 + lv.input_value());
1013 lv.set_output_value(os->address()
1015 + lv.input_value());
1018 if (lv.needs_output_symtab_entry())
1020 lv.set_output_symtab_index(index);
1027 // Set the output dynamic symbol table indexes for the local variables.
1029 template<int size, bool big_endian>
1031 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1033 const unsigned int loccount = this->local_symbol_count_;
1034 for (unsigned int i = 1; i < loccount; ++i)
1036 Symbol_value<size>& lv(this->local_values_[i]);
1037 if (lv.needs_output_dynsym_entry())
1039 lv.set_output_dynsym_index(index);
1046 // Set the offset where local dynamic symbol information will be stored.
1047 // Returns the count of local symbols contributed to the symbol table by
1050 template<int size, bool big_endian>
1052 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1054 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1055 this->local_dynsym_offset_ = off;
1056 return this->output_local_dynsym_count_;
1059 // Write out the local symbols.
1061 template<int size, bool big_endian>
1063 Sized_relobj<size, big_endian>::write_local_symbols(
1065 const Stringpool* sympool,
1066 const Stringpool* dynpool)
1068 if (parameters->options().strip_all()
1069 && this->output_local_dynsym_count_ == 0)
1072 gold_assert(this->symtab_shndx_ != -1U);
1073 if (this->symtab_shndx_ == 0)
1075 // This object has no symbols. Weird but legal.
1079 // Read the symbol table section header.
1080 const unsigned int symtab_shndx = this->symtab_shndx_;
1081 typename This::Shdr symtabshdr(this,
1082 this->elf_file_.section_header(symtab_shndx));
1083 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1084 const unsigned int loccount = this->local_symbol_count_;
1085 gold_assert(loccount == symtabshdr.get_sh_info());
1087 // Read the local symbols.
1088 const int sym_size = This::sym_size;
1089 off_t locsize = loccount * sym_size;
1090 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1091 locsize, true, false);
1093 // Read the symbol names.
1094 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
1095 section_size_type strtab_size;
1096 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1099 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1101 // Get views into the output file for the portions of the symbol table
1102 // and the dynamic symbol table that we will be writing.
1103 off_t output_size = this->output_local_symbol_count_ * sym_size;
1104 unsigned char* oview = NULL;
1105 if (output_size > 0)
1106 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1108 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1109 unsigned char* dyn_oview = NULL;
1110 if (dyn_output_size > 0)
1111 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1114 const std::vector<Map_to_output>& mo(this->map_to_output());
1116 gold_assert(this->local_values_.size() == loccount);
1118 unsigned char* ov = oview;
1119 unsigned char* dyn_ov = dyn_oview;
1121 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1123 elfcpp::Sym<size, big_endian> isym(psyms);
1125 unsigned int st_shndx = isym.get_st_shndx();
1126 if (st_shndx < elfcpp::SHN_LORESERVE)
1128 gold_assert(st_shndx < mo.size());
1129 if (mo[st_shndx].output_section == NULL)
1131 st_shndx = mo[st_shndx].output_section->out_shndx();
1134 // Write the symbol to the output symbol table.
1135 if (!parameters->options().strip_all()
1136 && this->local_values_[i].needs_output_symtab_entry())
1138 elfcpp::Sym_write<size, big_endian> osym(ov);
1140 gold_assert(isym.get_st_name() < strtab_size);
1141 const char* name = pnames + isym.get_st_name();
1142 osym.put_st_name(sympool->get_offset(name));
1143 osym.put_st_value(this->local_values_[i].value(this, 0));
1144 osym.put_st_size(isym.get_st_size());
1145 osym.put_st_info(isym.get_st_info());
1146 osym.put_st_other(isym.get_st_other());
1147 osym.put_st_shndx(st_shndx);
1152 // Write the symbol to the output dynamic symbol table.
1153 if (this->local_values_[i].needs_output_dynsym_entry())
1155 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1156 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1158 gold_assert(isym.get_st_name() < strtab_size);
1159 const char* name = pnames + isym.get_st_name();
1160 osym.put_st_name(dynpool->get_offset(name));
1161 osym.put_st_value(this->local_values_[i].value(this, 0));
1162 osym.put_st_size(isym.get_st_size());
1163 osym.put_st_info(isym.get_st_info());
1164 osym.put_st_other(isym.get_st_other());
1165 osym.put_st_shndx(st_shndx);
1172 if (output_size > 0)
1174 gold_assert(ov - oview == output_size);
1175 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1178 if (dyn_output_size > 0)
1180 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1181 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1186 // Set *INFO to symbolic information about the offset OFFSET in the
1187 // section SHNDX. Return true if we found something, false if we
1190 template<int size, bool big_endian>
1192 Sized_relobj<size, big_endian>::get_symbol_location_info(
1195 Symbol_location_info* info)
1197 if (this->symtab_shndx_ == 0)
1200 section_size_type symbols_size;
1201 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1205 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1206 section_size_type names_size;
1207 const unsigned char* symbol_names_u =
1208 this->section_contents(symbol_names_shndx, &names_size, false);
1209 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1211 const int sym_size = This::sym_size;
1212 const size_t count = symbols_size / sym_size;
1214 const unsigned char* p = symbols;
1215 for (size_t i = 0; i < count; ++i, p += sym_size)
1217 elfcpp::Sym<size, big_endian> sym(p);
1219 if (sym.get_st_type() == elfcpp::STT_FILE)
1221 if (sym.get_st_name() >= names_size)
1222 info->source_file = "(invalid)";
1224 info->source_file = symbol_names + sym.get_st_name();
1226 else if (sym.get_st_shndx() == shndx
1227 && static_cast<off_t>(sym.get_st_value()) <= offset
1228 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1231 if (sym.get_st_name() > names_size)
1232 info->enclosing_symbol_name = "(invalid)";
1235 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1236 if (parameters->options().do_demangle())
1238 char* demangled_name = cplus_demangle(
1239 info->enclosing_symbol_name.c_str(),
1240 DMGL_ANSI | DMGL_PARAMS);
1241 if (demangled_name != NULL)
1243 info->enclosing_symbol_name.assign(demangled_name);
1244 free(demangled_name);
1255 // Input_objects methods.
1257 // Add a regular relocatable object to the list. Return false if this
1258 // object should be ignored.
1261 Input_objects::add_object(Object* obj)
1263 // Set the global target from the first object file we recognize.
1264 Target* target = obj->target();
1265 if (!parameters->target_valid())
1266 set_parameters_target(target);
1267 else if (target != ¶meters->target())
1269 obj->error(_("incompatible target"));
1273 if (!obj->is_dynamic())
1274 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1277 // See if this is a duplicate SONAME.
1278 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1279 const char* soname = dynobj->soname();
1281 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1282 this->sonames_.insert(soname);
1285 // We have already seen a dynamic object with this soname.
1289 this->dynobj_list_.push_back(dynobj);
1291 // If this is -lc, remember the directory in which we found it.
1292 // We use this when issuing warnings about undefined symbols: as
1293 // a heuristic, we don't warn about system libraries found in
1294 // the same directory as -lc.
1295 if (strncmp(soname, "libc.so", 7) == 0)
1297 const char* object_name = dynobj->name().c_str();
1298 const char* base = lbasename(object_name);
1299 if (base != object_name)
1300 this->system_library_directory_.assign(object_name,
1301 base - 1 - object_name);
1308 // Return whether an object was found in the system library directory.
1311 Input_objects::found_in_system_library_directory(const Object* object) const
1313 return (!this->system_library_directory_.empty()
1314 && object->name().compare(0,
1315 this->system_library_directory_.size(),
1316 this->system_library_directory_) == 0);
1319 // For each dynamic object, record whether we've seen all of its
1320 // explicit dependencies.
1323 Input_objects::check_dynamic_dependencies() const
1325 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1326 p != this->dynobj_list_.end();
1329 const Dynobj::Needed& needed((*p)->needed());
1330 bool found_all = true;
1331 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1332 pneeded != needed.end();
1335 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1341 (*p)->set_has_unknown_needed_entries(!found_all);
1345 // Relocate_info methods.
1347 // Return a string describing the location of a relocation. This is
1348 // only used in error messages.
1350 template<int size, bool big_endian>
1352 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1354 // See if we can get line-number information from debugging sections.
1355 std::string filename;
1356 std::string file_and_lineno; // Better than filename-only, if available.
1358 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1359 // This will be "" if we failed to parse the debug info for any reason.
1360 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1362 std::string ret(this->object->name());
1364 Symbol_location_info info;
1365 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1367 ret += " in function ";
1368 ret += info.enclosing_symbol_name;
1370 filename = info.source_file;
1373 if (!file_and_lineno.empty())
1374 ret += file_and_lineno;
1377 if (!filename.empty())
1380 ret += this->object->section_name(this->data_shndx);
1382 // Offsets into sections have to be positive.
1383 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1390 } // End namespace gold.
1395 using namespace gold;
1397 // Read an ELF file with the header and return the appropriate
1398 // instance of Object.
1400 template<int size, bool big_endian>
1402 make_elf_sized_object(const std::string& name, Input_file* input_file,
1403 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1405 int et = ehdr.get_e_type();
1406 if (et == elfcpp::ET_REL)
1408 Sized_relobj<size, big_endian>* obj =
1409 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1413 else if (et == elfcpp::ET_DYN)
1415 Sized_dynobj<size, big_endian>* obj =
1416 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1422 gold_error(_("%s: unsupported ELF file type %d"),
1428 } // End anonymous namespace.
1433 // Read an ELF file and return the appropriate instance of Object.
1436 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1437 const unsigned char* p, section_offset_type bytes)
1439 if (bytes < elfcpp::EI_NIDENT)
1441 gold_error(_("%s: ELF file too short"), name.c_str());
1445 int v = p[elfcpp::EI_VERSION];
1446 if (v != elfcpp::EV_CURRENT)
1448 if (v == elfcpp::EV_NONE)
1449 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1451 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1455 int c = p[elfcpp::EI_CLASS];
1456 if (c == elfcpp::ELFCLASSNONE)
1458 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1461 else if (c != elfcpp::ELFCLASS32
1462 && c != elfcpp::ELFCLASS64)
1464 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1468 int d = p[elfcpp::EI_DATA];
1469 if (d == elfcpp::ELFDATANONE)
1471 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1474 else if (d != elfcpp::ELFDATA2LSB
1475 && d != elfcpp::ELFDATA2MSB)
1477 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1481 bool big_endian = d == elfcpp::ELFDATA2MSB;
1483 if (c == elfcpp::ELFCLASS32)
1485 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1487 gold_error(_("%s: ELF file too short"), name.c_str());
1492 #ifdef HAVE_TARGET_32_BIG
1493 elfcpp::Ehdr<32, true> ehdr(p);
1494 return make_elf_sized_object<32, true>(name, input_file,
1497 gold_error(_("%s: not configured to support "
1498 "32-bit big-endian object"),
1505 #ifdef HAVE_TARGET_32_LITTLE
1506 elfcpp::Ehdr<32, false> ehdr(p);
1507 return make_elf_sized_object<32, false>(name, input_file,
1510 gold_error(_("%s: not configured to support "
1511 "32-bit little-endian object"),
1519 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1521 gold_error(_("%s: ELF file too short"), name.c_str());
1526 #ifdef HAVE_TARGET_64_BIG
1527 elfcpp::Ehdr<64, true> ehdr(p);
1528 return make_elf_sized_object<64, true>(name, input_file,
1531 gold_error(_("%s: not configured to support "
1532 "64-bit big-endian object"),
1539 #ifdef HAVE_TARGET_64_LITTLE
1540 elfcpp::Ehdr<64, false> ehdr(p);
1541 return make_elf_sized_object<64, false>(name, input_file,
1544 gold_error(_("%s: not configured to support "
1545 "64-bit little-endian object"),
1553 // Instantiate the templates we need.
1555 #ifdef HAVE_TARGET_32_LITTLE
1558 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1559 Read_symbols_data*);
1562 #ifdef HAVE_TARGET_32_BIG
1565 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1566 Read_symbols_data*);
1569 #ifdef HAVE_TARGET_64_LITTLE
1572 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1573 Read_symbols_data*);
1576 #ifdef HAVE_TARGET_64_BIG
1579 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1580 Read_symbols_data*);
1583 #ifdef HAVE_TARGET_32_LITTLE
1585 class Sized_relobj<32, false>;
1588 #ifdef HAVE_TARGET_32_BIG
1590 class Sized_relobj<32, true>;
1593 #ifdef HAVE_TARGET_64_LITTLE
1595 class Sized_relobj<64, false>;
1598 #ifdef HAVE_TARGET_64_BIG
1600 class Sized_relobj<64, true>;
1603 #ifdef HAVE_TARGET_32_LITTLE
1605 struct Relocate_info<32, false>;
1608 #ifdef HAVE_TARGET_32_BIG
1610 struct Relocate_info<32, true>;
1613 #ifdef HAVE_TARGET_64_LITTLE
1615 struct Relocate_info<64, false>;
1618 #ifdef HAVE_TARGET_64_BIG
1620 struct Relocate_info<64, true>;
1623 } // End namespace gold.