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, 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, 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, true);
101 // Read the section names.
102 const unsigned char* pshdrs = sd->section_headers->data();
103 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
104 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
106 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
107 this->error(_("section name section has wrong type: %u"),
108 static_cast<unsigned int>(shdrnames.get_sh_type()));
110 sd->section_names_size =
111 convert_to_section_size_type(shdrnames.get_sh_size());
112 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
113 sd->section_names_size, false);
116 // If NAME is the name of a special .gnu.warning section, arrange for
117 // the warning to be issued. SHNDX is the section index. Return
118 // whether it is a warning section.
121 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
122 Symbol_table* symtab)
124 const char warn_prefix[] = ".gnu.warning.";
125 const int warn_prefix_len = sizeof warn_prefix - 1;
126 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
128 // Read the section contents to get the warning text. It would
129 // be nicer if we only did this if we have to actually issue a
130 // warning. Unfortunately, warnings are issued as we relocate
131 // sections. That means that we can not lock the object then,
132 // as we might try to issue the same warning multiple times
134 section_size_type len;
135 const unsigned char* contents = this->section_contents(shndx, &len,
137 std::string warning(reinterpret_cast<const char*>(contents), len);
138 symtab->add_warning(name + warn_prefix_len, this, warning);
144 // Class Sized_relobj.
146 template<int size, bool big_endian>
147 Sized_relobj<size, big_endian>::Sized_relobj(
148 const std::string& name,
149 Input_file* input_file,
151 const elfcpp::Ehdr<size, big_endian>& ehdr)
152 : Relobj(name, input_file, offset),
153 elf_file_(this, ehdr),
155 local_symbol_count_(0),
156 output_local_symbol_count_(0),
157 output_local_dynsym_count_(0),
159 local_symbol_offset_(0),
160 local_dynsym_offset_(0),
162 local_got_offsets_(),
167 template<int size, bool big_endian>
168 Sized_relobj<size, big_endian>::~Sized_relobj()
172 // Set up an object file based on the file header. This sets up the
173 // target and reads the section information.
175 template<int size, bool big_endian>
177 Sized_relobj<size, big_endian>::setup(
178 const elfcpp::Ehdr<size, big_endian>& ehdr)
180 this->set_target(ehdr.get_e_machine(), size, big_endian,
181 ehdr.get_e_ident()[elfcpp::EI_OSABI],
182 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
184 const unsigned int shnum = this->elf_file_.shnum();
185 this->set_shnum(shnum);
188 // Find the SHT_SYMTAB section, given the section headers. The ELF
189 // standard says that maybe in the future there can be more than one
190 // SHT_SYMTAB section. Until somebody figures out how that could
191 // work, we assume there is only one.
193 template<int size, bool big_endian>
195 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
197 const unsigned int shnum = this->shnum();
198 this->symtab_shndx_ = 0;
201 // Look through the sections in reverse order, since gas tends
202 // to put the symbol table at the end.
203 const unsigned char* p = pshdrs + shnum * This::shdr_size;
204 unsigned int i = shnum;
208 p -= This::shdr_size;
209 typename This::Shdr shdr(p);
210 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
212 this->symtab_shndx_ = i;
219 // Return whether SHDR has the right type and flags to be a GNU
220 // .eh_frame section.
222 template<int size, bool big_endian>
224 Sized_relobj<size, big_endian>::check_eh_frame_flags(
225 const elfcpp::Shdr<size, big_endian>* shdr) const
227 return (shdr->get_sh_size() > 0
228 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
229 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
232 // Return whether there is a GNU .eh_frame section, given the section
233 // headers and the section names.
235 template<int size, bool big_endian>
237 Sized_relobj<size, big_endian>::find_eh_frame(
238 const unsigned char* pshdrs,
240 section_size_type names_size) const
242 const unsigned int shnum = this->shnum();
243 const unsigned char* p = pshdrs + This::shdr_size;
244 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
246 typename This::Shdr shdr(p);
247 if (this->check_eh_frame_flags(&shdr))
249 if (shdr.get_sh_name() >= names_size)
251 this->error(_("bad section name offset for section %u: %lu"),
252 i, static_cast<unsigned long>(shdr.get_sh_name()));
256 const char* name = names + shdr.get_sh_name();
257 if (strcmp(name, ".eh_frame") == 0)
264 // Read the sections and symbols from an object file.
266 template<int size, bool big_endian>
268 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
270 this->read_section_data(&this->elf_file_, sd);
272 const unsigned char* const pshdrs = sd->section_headers->data();
274 this->find_symtab(pshdrs);
276 const unsigned char* namesu = sd->section_names->data();
277 const char* names = reinterpret_cast<const char*>(namesu);
278 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
279 this->has_eh_frame_ = true;
282 sd->symbols_size = 0;
283 sd->external_symbols_offset = 0;
284 sd->symbol_names = NULL;
285 sd->symbol_names_size = 0;
287 if (this->symtab_shndx_ == 0)
289 // No symbol table. Weird but legal.
293 // Get the symbol table section header.
294 typename This::Shdr symtabshdr(pshdrs
295 + this->symtab_shndx_ * This::shdr_size);
296 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
298 // If this object has a .eh_frame section, we need all the symbols.
299 // Otherwise we only need the external symbols. While it would be
300 // simpler to just always read all the symbols, I've seen object
301 // files with well over 2000 local symbols, which for a 64-bit
302 // object file format is over 5 pages that we don't need to read
305 const int sym_size = This::sym_size;
306 const unsigned int loccount = symtabshdr.get_sh_info();
307 this->local_symbol_count_ = loccount;
308 this->local_values_.resize(loccount);
309 section_offset_type locsize = loccount * sym_size;
310 off_t dataoff = symtabshdr.get_sh_offset();
311 section_size_type datasize =
312 convert_to_section_size_type(symtabshdr.get_sh_size());
313 off_t extoff = dataoff + locsize;
314 section_size_type extsize = datasize - locsize;
316 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
317 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
319 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
321 // Read the section header for the symbol names.
322 unsigned int strtab_shndx = symtabshdr.get_sh_link();
323 if (strtab_shndx >= this->shnum())
325 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
328 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
329 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
331 this->error(_("symbol table name section has wrong type: %u"),
332 static_cast<unsigned int>(strtabshdr.get_sh_type()));
336 // Read the symbol names.
337 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
338 strtabshdr.get_sh_size(), true);
340 sd->symbols = fvsymtab;
341 sd->symbols_size = readsize;
342 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
343 sd->symbol_names = fvstrtab;
344 sd->symbol_names_size =
345 convert_to_section_size_type(strtabshdr.get_sh_size());
348 // Return the section index of symbol SYM. Set *VALUE to its value in
349 // the object file. Note that for a symbol which is not defined in
350 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
351 // it will not return the final value of the symbol in the link.
353 template<int size, bool big_endian>
355 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
358 section_size_type symbols_size;
359 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
363 const size_t count = symbols_size / This::sym_size;
364 gold_assert(sym < count);
366 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
367 *value = elfsym.get_st_value();
368 // FIXME: Handle SHN_XINDEX.
369 return elfsym.get_st_shndx();
372 // Return whether to include a section group in the link. LAYOUT is
373 // used to keep track of which section groups we have already seen.
374 // INDEX is the index of the section group and SHDR is the section
375 // header. If we do not want to include this group, we set bits in
376 // OMIT for each section which should be discarded.
378 template<int size, bool big_endian>
380 Sized_relobj<size, big_endian>::include_section_group(
381 Symbol_table* symtab,
385 const elfcpp::Shdr<size, big_endian>& shdr,
386 std::vector<bool>* omit)
388 // Read the section contents.
389 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
390 shdr.get_sh_size(), false);
391 const elfcpp::Elf_Word* pword =
392 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
394 // The first word contains flags. We only care about COMDAT section
395 // groups. Other section groups are always included in the link
396 // just like ordinary sections.
397 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
399 // Look up the group signature, which is the name of a symbol. This
400 // is a lot of effort to go to to read a string. Why didn't they
401 // just have the group signature point into the string table, rather
402 // than indirect through a symbol?
404 // Get the appropriate symbol table header (this will normally be
405 // the single SHT_SYMTAB section, but in principle it need not be).
406 const unsigned int link = shdr.get_sh_link();
407 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
409 // Read the symbol table entry.
410 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
412 this->error(_("section group %u info %u out of range"),
413 index, shdr.get_sh_info());
416 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
417 const unsigned char* psym = this->get_view(symoff, This::sym_size, false);
418 elfcpp::Sym<size, big_endian> sym(psym);
420 // Read the symbol table names.
421 section_size_type symnamelen;
422 const unsigned char* psymnamesu;
423 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
425 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
427 // Get the section group signature.
428 if (sym.get_st_name() >= symnamelen)
430 this->error(_("symbol %u name offset %u out of range"),
431 shdr.get_sh_info(), sym.get_st_name());
435 const char* signature = psymnames + sym.get_st_name();
437 // It seems that some versions of gas will create a section group
438 // associated with a section symbol, and then fail to give a name to
439 // the section symbol. In such a case, use the name of the section.
442 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
444 secname = this->section_name(sym.get_st_shndx());
445 signature = secname.c_str();
448 // Record this section group, and see whether we've already seen one
449 // with the same signature.
451 if ((flags & elfcpp::GRP_COMDAT) == 0
452 || layout->add_comdat(signature, true))
454 if (parameters->output_is_object())
455 layout->layout_group(symtab, this, index, name, signature, shdr,
460 // This is a duplicate. We want to discard the sections in this
462 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
463 for (size_t i = 1; i < count; ++i)
465 elfcpp::Elf_Word secnum =
466 elfcpp::Swap<32, big_endian>::readval(pword + i);
467 if (secnum >= this->shnum())
469 this->error(_("section %u in section group %u out of range"),
473 (*omit)[secnum] = true;
479 // Whether to include a linkonce section in the link. NAME is the
480 // name of the section and SHDR is the section header.
482 // Linkonce sections are a GNU extension implemented in the original
483 // GNU linker before section groups were defined. The semantics are
484 // that we only include one linkonce section with a given name. The
485 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
486 // where T is the type of section and SYMNAME is the name of a symbol.
487 // In an attempt to make linkonce sections interact well with section
488 // groups, we try to identify SYMNAME and use it like a section group
489 // signature. We want to block section groups with that signature,
490 // but not other linkonce sections with that signature. We also use
491 // the full name of the linkonce section as a normal section group
494 template<int size, bool big_endian>
496 Sized_relobj<size, big_endian>::include_linkonce_section(
499 const elfcpp::Shdr<size, big_endian>&)
501 // In general the symbol name we want will be the string following
502 // the last '.'. However, we have to handle the case of
503 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
504 // some versions of gcc. So we use a heuristic: if the name starts
505 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
506 // we look for the last '.'. We can't always simply skip
507 // ".gnu.linkonce.X", because we have to deal with cases like
508 // ".gnu.linkonce.d.rel.ro.local".
509 const char* const linkonce_t = ".gnu.linkonce.t.";
511 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
512 symname = name + strlen(linkonce_t);
514 symname = strrchr(name, '.') + 1;
515 bool include1 = layout->add_comdat(symname, false);
516 bool include2 = layout->add_comdat(name, true);
517 return include1 && include2;
520 // Lay out the input sections. We walk through the sections and check
521 // whether they should be included in the link. If they should, we
522 // pass them to the Layout object, which will return an output section
525 template<int size, bool big_endian>
527 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
529 Read_symbols_data* sd)
531 const unsigned int shnum = this->shnum();
535 // Get the section headers.
536 const unsigned char* pshdrs = sd->section_headers->data();
538 // Get the section names.
539 const unsigned char* pnamesu = sd->section_names->data();
540 const char* pnames = reinterpret_cast<const char*>(pnamesu);
542 // For each section, record the index of the reloc section if any.
543 // Use 0 to mean that there is no reloc section, -1U to mean that
544 // there is more than one.
545 std::vector<unsigned int> reloc_shndx(shnum, 0);
546 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
547 // Skip the first, dummy, section.
548 pshdrs += This::shdr_size;
549 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
551 typename This::Shdr shdr(pshdrs);
553 unsigned int sh_type = shdr.get_sh_type();
554 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
556 unsigned int target_shndx = shdr.get_sh_info();
557 if (target_shndx == 0 || target_shndx >= shnum)
559 this->error(_("relocation section %u has bad info %u"),
564 if (reloc_shndx[target_shndx] != 0)
565 reloc_shndx[target_shndx] = -1U;
568 reloc_shndx[target_shndx] = i;
569 reloc_type[target_shndx] = sh_type;
574 std::vector<Map_to_output>& map_sections(this->map_to_output());
575 map_sections.resize(shnum);
577 // If we are only linking for symbols, then there is nothing else to
579 if (this->input_file()->just_symbols())
581 delete sd->section_headers;
582 sd->section_headers = NULL;
583 delete sd->section_names;
584 sd->section_names = NULL;
588 // Whether we've seen a .note.GNU-stack section.
589 bool seen_gnu_stack = false;
590 // The flags of a .note.GNU-stack section.
591 uint64_t gnu_stack_flags = 0;
593 // Keep track of which sections to omit.
594 std::vector<bool> omit(shnum, false);
596 // Keep track of reloc sections when doing a relocatable link.
597 const bool output_is_object = parameters->output_is_object();
598 std::vector<unsigned int> reloc_sections;
600 // Keep track of .eh_frame sections.
601 std::vector<unsigned int> eh_frame_sections;
603 // Skip the first, dummy, section.
604 pshdrs = sd->section_headers->data() + This::shdr_size;
605 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
607 typename This::Shdr shdr(pshdrs);
609 if (shdr.get_sh_name() >= sd->section_names_size)
611 this->error(_("bad section name offset for section %u: %lu"),
612 i, static_cast<unsigned long>(shdr.get_sh_name()));
616 const char* name = pnames + shdr.get_sh_name();
618 if (this->handle_gnu_warning_section(name, i, symtab))
620 if (!output_is_object)
624 // The .note.GNU-stack section is special. It gives the
625 // protection flags that this object file requires for the stack
627 if (strcmp(name, ".note.GNU-stack") == 0)
629 seen_gnu_stack = true;
630 gnu_stack_flags |= shdr.get_sh_flags();
634 bool discard = omit[i];
637 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
639 if (!this->include_section_group(symtab, layout, i, name, shdr,
643 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
644 && Layout::is_linkonce(name))
646 if (!this->include_linkonce_section(layout, name, shdr))
653 // Do not include this section in the link.
654 map_sections[i].output_section = NULL;
658 // When doing a relocatable link we are going to copy input
659 // reloc sections into the output. We only want to copy the
660 // ones associated with sections which are not being discarded.
661 // However, we don't know that yet for all sections. So save
662 // reloc sections and process them later.
664 && (shdr.get_sh_type() == elfcpp::SHT_REL
665 || shdr.get_sh_type() == elfcpp::SHT_RELA))
667 reloc_sections.push_back(i);
671 if (output_is_object && shdr.get_sh_type() == elfcpp::SHT_GROUP)
674 // The .eh_frame section is special. It holds exception frame
675 // information that we need to read in order to generate the
676 // exception frame header. We process these after all the other
677 // sections so that the exception frame reader can reliably
678 // determine which sections are being discarded, and discard the
679 // corresponding information.
680 if (!output_is_object
681 && strcmp(name, ".eh_frame") == 0
682 && this->check_eh_frame_flags(&shdr))
684 eh_frame_sections.push_back(i);
689 Output_section* os = layout->layout(this, i, name, shdr,
690 reloc_shndx[i], reloc_type[i],
693 map_sections[i].output_section = os;
694 map_sections[i].offset = offset;
696 // If this section requires special handling, and if there are
697 // relocs that apply to it, then we must do the special handling
698 // before we apply the relocs.
699 if (offset == -1 && reloc_shndx[i] != 0)
700 this->set_relocs_must_follow_section_writes();
703 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
705 // When doing a relocatable link handle the reloc sections at the
707 if (output_is_object)
708 this->size_relocatable_relocs();
709 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
710 p != reloc_sections.end();
714 const unsigned char* pshdr;
715 pshdr = sd->section_headers->data() + i * This::shdr_size;
716 typename This::Shdr shdr(pshdr);
718 unsigned int data_shndx = shdr.get_sh_info();
719 if (data_shndx >= shnum)
721 // We already warned about this above.
725 Output_section* data_section = map_sections[data_shndx].output_section;
726 if (data_section == NULL)
728 map_sections[i].output_section = NULL;
732 Relocatable_relocs* rr = new Relocatable_relocs();
733 this->set_relocatable_relocs(i, rr);
735 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
737 map_sections[i].output_section = os;
738 map_sections[i].offset = -1;
741 // Handle the .eh_frame sections at the end.
742 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
743 p != eh_frame_sections.end();
746 gold_assert(this->has_eh_frame_);
747 gold_assert(sd->external_symbols_offset != 0);
750 const unsigned char *pshdr;
751 pshdr = sd->section_headers->data() + i * This::shdr_size;
752 typename This::Shdr shdr(pshdr);
755 Output_section* os = layout->layout_eh_frame(this,
758 sd->symbol_names->data(),
759 sd->symbol_names_size,
764 map_sections[i].output_section = os;
765 map_sections[i].offset = offset;
767 // If this section requires special handling, and if there are
768 // relocs that apply to it, then we must do the special handling
769 // before we apply the relocs.
770 if (offset == -1 && reloc_shndx[i] != 0)
771 this->set_relocs_must_follow_section_writes();
774 delete sd->section_headers;
775 sd->section_headers = NULL;
776 delete sd->section_names;
777 sd->section_names = NULL;
780 // Add the symbols to the symbol table.
782 template<int size, bool big_endian>
784 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
785 Read_symbols_data* sd)
787 if (sd->symbols == NULL)
789 gold_assert(sd->symbol_names == NULL);
793 const int sym_size = This::sym_size;
794 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
796 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
798 this->error(_("size of symbols is not multiple of symbol size"));
802 this->symbols_.resize(symcount);
804 const char* sym_names =
805 reinterpret_cast<const char*>(sd->symbol_names->data());
806 symtab->add_from_relobj(this,
807 sd->symbols->data() + sd->external_symbols_offset,
808 symcount, sym_names, sd->symbol_names_size,
813 delete sd->symbol_names;
814 sd->symbol_names = NULL;
817 // First pass over the local symbols. Here we add their names to
818 // *POOL and *DYNPOOL, and we store the symbol value in
819 // THIS->LOCAL_VALUES_. This function is always called from a
820 // singleton thread. This is followed by a call to
821 // finalize_local_symbols.
823 template<int size, bool big_endian>
825 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
828 gold_assert(this->symtab_shndx_ != -1U);
829 if (this->symtab_shndx_ == 0)
831 // This object has no symbols. Weird but legal.
835 // Read the symbol table section header.
836 const unsigned int symtab_shndx = this->symtab_shndx_;
837 typename This::Shdr symtabshdr(this,
838 this->elf_file_.section_header(symtab_shndx));
839 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
841 // Read the local symbols.
842 const int sym_size = This::sym_size;
843 const unsigned int loccount = this->local_symbol_count_;
844 gold_assert(loccount == symtabshdr.get_sh_info());
845 off_t locsize = loccount * sym_size;
846 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
849 // Read the symbol names.
850 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
851 section_size_type strtab_size;
852 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
855 const char* pnames = reinterpret_cast<const char*>(pnamesu);
857 // Loop over the local symbols.
859 const std::vector<Map_to_output>& mo(this->map_to_output());
860 unsigned int shnum = this->shnum();
861 unsigned int count = 0;
862 unsigned int dyncount = 0;
863 // Skip the first, dummy, symbol.
865 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
867 elfcpp::Sym<size, big_endian> sym(psyms);
869 Symbol_value<size>& lv(this->local_values_[i]);
871 unsigned int shndx = sym.get_st_shndx();
872 lv.set_input_shndx(shndx);
874 if (sym.get_st_type() == elfcpp::STT_SECTION)
875 lv.set_is_section_symbol();
876 else if (sym.get_st_type() == elfcpp::STT_TLS)
877 lv.set_is_tls_symbol();
879 // Save the input symbol value for use in do_finalize_local_symbols().
880 lv.set_input_value(sym.get_st_value());
882 // Decide whether this symbol should go into the output file.
884 if (shndx < shnum && mo[shndx].output_section == NULL)
886 lv.set_no_output_symtab_entry();
890 if (sym.get_st_type() == elfcpp::STT_SECTION)
892 lv.set_no_output_symtab_entry();
896 if (sym.get_st_name() >= strtab_size)
898 this->error(_("local symbol %u section name out of range: %u >= %u"),
899 i, sym.get_st_name(),
900 static_cast<unsigned int>(strtab_size));
901 lv.set_no_output_symtab_entry();
905 // Add the symbol to the symbol table string pool.
906 const char* name = pnames + sym.get_st_name();
907 pool->add(name, true, NULL);
910 // If needed, add the symbol to the dynamic symbol table string pool.
911 if (lv.needs_output_dynsym_entry())
913 dynpool->add(name, true, NULL);
918 this->output_local_symbol_count_ = count;
919 this->output_local_dynsym_count_ = dyncount;
922 // Finalize the local symbols. Here we set the final value in
923 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
924 // This function is always called from a singleton thread. The actual
925 // output of the local symbols will occur in a separate task.
927 template<int size, bool big_endian>
929 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
932 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
934 const unsigned int loccount = this->local_symbol_count_;
935 this->local_symbol_offset_ = off;
937 const std::vector<Map_to_output>& mo(this->map_to_output());
938 unsigned int shnum = this->shnum();
940 for (unsigned int i = 1; i < loccount; ++i)
942 Symbol_value<size>& lv(this->local_values_[i]);
944 unsigned int shndx = lv.input_shndx();
946 // Set the output symbol value.
948 if (shndx >= elfcpp::SHN_LORESERVE)
950 if (shndx == elfcpp::SHN_ABS)
951 lv.set_output_value(lv.input_value());
954 // FIXME: Handle SHN_XINDEX.
955 this->error(_("unknown section index %u for local symbol %u"),
957 lv.set_output_value(0);
964 this->error(_("local symbol %u section index %u out of range"),
969 Output_section* os = mo[shndx].output_section;
973 lv.set_output_value(0);
976 else if (mo[shndx].offset == -1)
978 // This is a SHF_MERGE section or one which otherwise
979 // requires special handling. We get the output address
980 // of the start of the merged section. If this is not a
981 // section symbol, we can then determine the final
982 // value. If it is a section symbol, we can not, as in
983 // that case we have to consider the addend to determine
984 // the value to use in a relocation.
985 if (!lv.is_section_symbol())
986 lv.set_output_value(os->output_address(this, shndx,
990 section_offset_type start =
991 os->starting_output_address(this, shndx);
992 Merged_symbol_value<size>* msv =
993 new Merged_symbol_value<size>(lv.input_value(), start);
994 lv.set_merged_symbol_value(msv);
997 else if (lv.is_tls_symbol())
998 lv.set_output_value(os->tls_offset()
1000 + lv.input_value());
1002 lv.set_output_value(os->address()
1004 + lv.input_value());
1007 if (lv.needs_output_symtab_entry())
1009 lv.set_output_symtab_index(index);
1016 // Set the output dynamic symbol table indexes for the local variables.
1018 template<int size, bool big_endian>
1020 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1022 const unsigned int loccount = this->local_symbol_count_;
1023 for (unsigned int i = 1; i < loccount; ++i)
1025 Symbol_value<size>& lv(this->local_values_[i]);
1026 if (lv.needs_output_dynsym_entry())
1028 lv.set_output_dynsym_index(index);
1035 // Set the offset where local dynamic symbol information will be stored.
1036 // Returns the count of local symbols contributed to the symbol table by
1039 template<int size, bool big_endian>
1041 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1043 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1044 this->local_dynsym_offset_ = off;
1045 return this->output_local_dynsym_count_;
1048 // Return the value of the local symbol symndx.
1049 template<int size, bool big_endian>
1050 typename elfcpp::Elf_types<size>::Elf_Addr
1051 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
1053 gold_assert(symndx < this->local_symbol_count_);
1054 gold_assert(symndx < this->local_values_.size());
1055 const Symbol_value<size>& lv(this->local_values_[symndx]);
1056 return lv.value(this, 0);
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->strip_all() && this->output_local_dynsym_count_ == 0)
1071 gold_assert(this->symtab_shndx_ != -1U);
1072 if (this->symtab_shndx_ == 0)
1074 // This object has no symbols. Weird but legal.
1078 // Read the symbol table section header.
1079 const unsigned int symtab_shndx = this->symtab_shndx_;
1080 typename This::Shdr symtabshdr(this,
1081 this->elf_file_.section_header(symtab_shndx));
1082 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1083 const unsigned int loccount = this->local_symbol_count_;
1084 gold_assert(loccount == symtabshdr.get_sh_info());
1086 // Read the local symbols.
1087 const int sym_size = This::sym_size;
1088 off_t locsize = loccount * sym_size;
1089 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1092 // Read the symbol names.
1093 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
1094 section_size_type strtab_size;
1095 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1098 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1100 // Get views into the output file for the portions of the symbol table
1101 // and the dynamic symbol table that we will be writing.
1102 off_t output_size = this->output_local_symbol_count_ * sym_size;
1103 unsigned char* oview = NULL;
1104 if (output_size > 0)
1105 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1107 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1108 unsigned char* dyn_oview = NULL;
1109 if (dyn_output_size > 0)
1110 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1113 const std::vector<Map_to_output>& mo(this->map_to_output());
1115 gold_assert(this->local_values_.size() == loccount);
1117 unsigned char* ov = oview;
1118 unsigned char* dyn_ov = dyn_oview;
1120 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1122 elfcpp::Sym<size, big_endian> isym(psyms);
1124 unsigned int st_shndx = isym.get_st_shndx();
1125 if (st_shndx < elfcpp::SHN_LORESERVE)
1127 gold_assert(st_shndx < mo.size());
1128 if (mo[st_shndx].output_section == NULL)
1130 st_shndx = mo[st_shndx].output_section->out_shndx();
1133 // Write the symbol to the output symbol table.
1134 if (!parameters->strip_all()
1135 && this->local_values_[i].needs_output_symtab_entry())
1137 elfcpp::Sym_write<size, big_endian> osym(ov);
1139 gold_assert(isym.get_st_name() < strtab_size);
1140 const char* name = pnames + isym.get_st_name();
1141 osym.put_st_name(sympool->get_offset(name));
1142 osym.put_st_value(this->local_values_[i].value(this, 0));
1143 osym.put_st_size(isym.get_st_size());
1144 osym.put_st_info(isym.get_st_info());
1145 osym.put_st_other(isym.get_st_other());
1146 osym.put_st_shndx(st_shndx);
1151 // Write the symbol to the output dynamic symbol table.
1152 if (this->local_values_[i].needs_output_dynsym_entry())
1154 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1155 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1157 gold_assert(isym.get_st_name() < strtab_size);
1158 const char* name = pnames + isym.get_st_name();
1159 osym.put_st_name(dynpool->get_offset(name));
1160 osym.put_st_value(this->local_values_[i].value(this, 0));
1161 osym.put_st_size(isym.get_st_size());
1162 osym.put_st_info(isym.get_st_info());
1163 osym.put_st_other(isym.get_st_other());
1164 osym.put_st_shndx(st_shndx);
1171 if (output_size > 0)
1173 gold_assert(ov - oview == output_size);
1174 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1177 if (dyn_output_size > 0)
1179 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1180 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1185 // Set *INFO to symbolic information about the offset OFFSET in the
1186 // section SHNDX. Return true if we found something, false if we
1189 template<int size, bool big_endian>
1191 Sized_relobj<size, big_endian>::get_symbol_location_info(
1194 Symbol_location_info* info)
1196 if (this->symtab_shndx_ == 0)
1199 section_size_type symbols_size;
1200 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1204 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1205 section_size_type names_size;
1206 const unsigned char* symbol_names_u =
1207 this->section_contents(symbol_names_shndx, &names_size, false);
1208 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1210 const int sym_size = This::sym_size;
1211 const size_t count = symbols_size / sym_size;
1213 const unsigned char* p = symbols;
1214 for (size_t i = 0; i < count; ++i, p += sym_size)
1216 elfcpp::Sym<size, big_endian> sym(p);
1218 if (sym.get_st_type() == elfcpp::STT_FILE)
1220 if (sym.get_st_name() >= names_size)
1221 info->source_file = "(invalid)";
1223 info->source_file = symbol_names + sym.get_st_name();
1225 else if (sym.get_st_shndx() == shndx
1226 && static_cast<off_t>(sym.get_st_value()) <= offset
1227 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1230 if (sym.get_st_name() > names_size)
1231 info->enclosing_symbol_name = "(invalid)";
1234 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1235 if (parameters->demangle())
1237 char* demangled_name = cplus_demangle(
1238 info->enclosing_symbol_name.c_str(),
1239 DMGL_ANSI | DMGL_PARAMS);
1240 if (demangled_name != NULL)
1242 info->enclosing_symbol_name.assign(demangled_name);
1243 free(demangled_name);
1254 // Input_objects methods.
1256 // Add a regular relocatable object to the list. Return false if this
1257 // object should be ignored.
1260 Input_objects::add_object(Object* obj)
1262 // Set the global target from the first object file we recognize.
1263 Target* target = obj->target();
1264 if (!parameters->is_target_valid())
1265 set_parameters_target(target);
1266 else if (target != parameters->target())
1268 obj->error(_("incompatible target"));
1272 if (!obj->is_dynamic())
1273 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1276 // See if this is a duplicate SONAME.
1277 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1278 const char* soname = dynobj->soname();
1280 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1281 this->sonames_.insert(soname);
1284 // We have already seen a dynamic object with this soname.
1288 this->dynobj_list_.push_back(dynobj);
1290 // If this is -lc, remember the directory in which we found it.
1291 // We use this when issuing warnings about undefined symbols: as
1292 // a heuristic, we don't warn about system libraries found in
1293 // the same directory as -lc.
1294 if (strncmp(soname, "libc.so", 7) == 0)
1296 const char* object_name = dynobj->name().c_str();
1297 const char* base = lbasename(object_name);
1298 if (base != object_name)
1299 this->system_library_directory_.assign(object_name,
1300 base - 1 - object_name);
1307 // Return whether an object was found in the system library directory.
1310 Input_objects::found_in_system_library_directory(const Object* object) const
1312 return (!this->system_library_directory_.empty()
1313 && object->name().compare(0,
1314 this->system_library_directory_.size(),
1315 this->system_library_directory_) == 0);
1318 // For each dynamic object, record whether we've seen all of its
1319 // explicit dependencies.
1322 Input_objects::check_dynamic_dependencies() const
1324 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1325 p != this->dynobj_list_.end();
1328 const Dynobj::Needed& needed((*p)->needed());
1329 bool found_all = true;
1330 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1331 pneeded != needed.end();
1334 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1340 (*p)->set_has_unknown_needed_entries(!found_all);
1344 // Relocate_info methods.
1346 // Return a string describing the location of a relocation. This is
1347 // only used in error messages.
1349 template<int size, bool big_endian>
1351 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1353 // See if we can get line-number information from debugging sections.
1354 std::string filename;
1355 std::string file_and_lineno; // Better than filename-only, if available.
1357 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1358 // This will be "" if we failed to parse the debug info for any reason.
1359 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1361 std::string ret(this->object->name());
1363 Symbol_location_info info;
1364 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1366 ret += " in function ";
1367 ret += info.enclosing_symbol_name;
1369 filename = info.source_file;
1372 if (!file_and_lineno.empty())
1373 ret += file_and_lineno;
1376 if (!filename.empty())
1379 ret += this->object->section_name(this->data_shndx);
1381 // Offsets into sections have to be positive.
1382 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1389 } // End namespace gold.
1394 using namespace gold;
1396 // Read an ELF file with the header and return the appropriate
1397 // instance of Object.
1399 template<int size, bool big_endian>
1401 make_elf_sized_object(const std::string& name, Input_file* input_file,
1402 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1404 int et = ehdr.get_e_type();
1405 if (et == elfcpp::ET_REL)
1407 Sized_relobj<size, big_endian>* obj =
1408 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1412 else if (et == elfcpp::ET_DYN)
1414 Sized_dynobj<size, big_endian>* obj =
1415 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1421 gold_error(_("%s: unsupported ELF file type %d"),
1427 } // End anonymous namespace.
1432 // Read an ELF file and return the appropriate instance of Object.
1435 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1436 const unsigned char* p, section_offset_type bytes)
1438 if (bytes < elfcpp::EI_NIDENT)
1440 gold_error(_("%s: ELF file too short"), name.c_str());
1444 int v = p[elfcpp::EI_VERSION];
1445 if (v != elfcpp::EV_CURRENT)
1447 if (v == elfcpp::EV_NONE)
1448 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1450 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1454 int c = p[elfcpp::EI_CLASS];
1455 if (c == elfcpp::ELFCLASSNONE)
1457 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1460 else if (c != elfcpp::ELFCLASS32
1461 && c != elfcpp::ELFCLASS64)
1463 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1467 int d = p[elfcpp::EI_DATA];
1468 if (d == elfcpp::ELFDATANONE)
1470 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1473 else if (d != elfcpp::ELFDATA2LSB
1474 && d != elfcpp::ELFDATA2MSB)
1476 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1480 bool big_endian = d == elfcpp::ELFDATA2MSB;
1482 if (c == elfcpp::ELFCLASS32)
1484 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1486 gold_error(_("%s: ELF file too short"), name.c_str());
1491 #ifdef HAVE_TARGET_32_BIG
1492 elfcpp::Ehdr<32, true> ehdr(p);
1493 return make_elf_sized_object<32, true>(name, input_file,
1496 gold_error(_("%s: not configured to support "
1497 "32-bit big-endian object"),
1504 #ifdef HAVE_TARGET_32_LITTLE
1505 elfcpp::Ehdr<32, false> ehdr(p);
1506 return make_elf_sized_object<32, false>(name, input_file,
1509 gold_error(_("%s: not configured to support "
1510 "32-bit little-endian object"),
1518 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1520 gold_error(_("%s: ELF file too short"), name.c_str());
1525 #ifdef HAVE_TARGET_64_BIG
1526 elfcpp::Ehdr<64, true> ehdr(p);
1527 return make_elf_sized_object<64, true>(name, input_file,
1530 gold_error(_("%s: not configured to support "
1531 "64-bit big-endian object"),
1538 #ifdef HAVE_TARGET_64_LITTLE
1539 elfcpp::Ehdr<64, false> ehdr(p);
1540 return make_elf_sized_object<64, false>(name, input_file,
1543 gold_error(_("%s: not configured to support "
1544 "64-bit little-endian object"),
1552 // Instantiate the templates we need. We could use the configure
1553 // script to restrict this to only the ones for implemented targets.
1555 #ifdef HAVE_TARGET_32_LITTLE
1557 class Sized_relobj<32, false>;
1560 #ifdef HAVE_TARGET_32_BIG
1562 class Sized_relobj<32, true>;
1565 #ifdef HAVE_TARGET_64_LITTLE
1567 class Sized_relobj<64, false>;
1570 #ifdef HAVE_TARGET_64_BIG
1572 class Sized_relobj<64, true>;
1575 #ifdef HAVE_TARGET_32_LITTLE
1577 struct Relocate_info<32, false>;
1580 #ifdef HAVE_TARGET_32_BIG
1582 struct Relocate_info<32, true>;
1585 #ifdef HAVE_TARGET_64_LITTLE
1587 struct Relocate_info<64, false>;
1590 #ifdef HAVE_TARGET_64_BIG
1592 struct Relocate_info<64, true>;
1595 } // End namespace gold.