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.
28 #include "libiberty.h"
30 #include "target-select.h"
31 #include "dwarf_reader.h"
44 // Set the target based on fields in the ELF file header.
47 Object::set_target(int machine, int size, bool big_endian, int osabi,
50 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
52 gold_fatal(_("%s: unsupported ELF machine number %d"),
53 this->name().c_str(), machine);
54 this->target_ = target;
57 // Report an error for this object file. This is used by the
58 // elfcpp::Elf_file interface, and also called by the Object code
62 Object::error(const char* format, ...) const
65 va_start(args, format);
67 if (vasprintf(&buf, format, args) < 0)
70 gold_error(_("%s: %s"), this->name().c_str(), buf);
74 // Return a view of the contents of a section.
77 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
79 Location loc(this->do_section_contents(shndx));
80 *plen = loc.data_size;
81 return this->get_view(loc.file_offset, loc.data_size, cache);
84 // Read the section data into SD. This is code common to Sized_relobj
85 // and Sized_dynobj, so we put it into Object.
87 template<int size, bool big_endian>
89 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
90 Read_symbols_data* sd)
92 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
94 // Read the section headers.
95 const off_t shoff = elf_file->shoff();
96 const unsigned int shnum = this->shnum();
97 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
99 // Read the section names.
100 const unsigned char* pshdrs = sd->section_headers->data();
101 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
102 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
104 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
105 this->error(_("section name section has wrong type: %u"),
106 static_cast<unsigned int>(shdrnames.get_sh_type()));
108 sd->section_names_size = shdrnames.get_sh_size();
109 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
110 sd->section_names_size, false);
113 // If NAME is the name of a special .gnu.warning section, arrange for
114 // the warning to be issued. SHNDX is the section index. Return
115 // whether it is a warning section.
118 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
119 Symbol_table* symtab)
121 const char warn_prefix[] = ".gnu.warning.";
122 const int warn_prefix_len = sizeof warn_prefix - 1;
123 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
125 symtab->add_warning(name + warn_prefix_len, this, shndx);
131 // Class Sized_relobj.
133 template<int size, bool big_endian>
134 Sized_relobj<size, big_endian>::Sized_relobj(
135 const std::string& name,
136 Input_file* input_file,
138 const elfcpp::Ehdr<size, big_endian>& ehdr)
139 : Relobj(name, input_file, offset),
140 elf_file_(this, ehdr),
142 local_symbol_count_(0),
143 output_local_symbol_count_(0),
145 local_symbol_offset_(0),
147 local_got_offsets_(),
152 template<int size, bool big_endian>
153 Sized_relobj<size, big_endian>::~Sized_relobj()
157 // Set up an object file based on the file header. This sets up the
158 // target and reads the section information.
160 template<int size, bool big_endian>
162 Sized_relobj<size, big_endian>::setup(
163 const elfcpp::Ehdr<size, big_endian>& ehdr)
165 this->set_target(ehdr.get_e_machine(), size, big_endian,
166 ehdr.get_e_ident()[elfcpp::EI_OSABI],
167 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
169 const unsigned int shnum = this->elf_file_.shnum();
170 this->set_shnum(shnum);
173 // Find the SHT_SYMTAB section, given the section headers. The ELF
174 // standard says that maybe in the future there can be more than one
175 // SHT_SYMTAB section. Until somebody figures out how that could
176 // work, we assume there is only one.
178 template<int size, bool big_endian>
180 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
182 const unsigned int shnum = this->shnum();
183 this->symtab_shndx_ = 0;
186 // Look through the sections in reverse order, since gas tends
187 // to put the symbol table at the end.
188 const unsigned char* p = pshdrs + shnum * This::shdr_size;
189 unsigned int i = shnum;
193 p -= This::shdr_size;
194 typename This::Shdr shdr(p);
195 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
197 this->symtab_shndx_ = i;
204 // Return whether SHDR has the right type and flags to be a GNU
205 // .eh_frame section.
207 template<int size, bool big_endian>
209 Sized_relobj<size, big_endian>::check_eh_frame_flags(
210 const elfcpp::Shdr<size, big_endian>* shdr) const
212 return (shdr->get_sh_size() > 0
213 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
214 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
217 // Return whether there is a GNU .eh_frame section, given the section
218 // headers and the section names.
220 template<int size, bool big_endian>
222 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
224 off_t names_size) const
226 const unsigned int shnum = this->shnum();
227 const unsigned char* p = pshdrs + This::shdr_size;
228 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
230 typename This::Shdr shdr(p);
231 if (this->check_eh_frame_flags(&shdr))
233 if (shdr.get_sh_name() >= names_size)
235 this->error(_("bad section name offset for section %u: %lu"),
236 i, static_cast<unsigned long>(shdr.get_sh_name()));
240 const char* name = names + shdr.get_sh_name();
241 if (strcmp(name, ".eh_frame") == 0)
248 // Read the sections and symbols from an object file.
250 template<int size, bool big_endian>
252 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
254 this->read_section_data(&this->elf_file_, sd);
256 const unsigned char* const pshdrs = sd->section_headers->data();
258 this->find_symtab(pshdrs);
260 const unsigned char* namesu = sd->section_names->data();
261 const char* names = reinterpret_cast<const char*>(namesu);
262 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
263 this->has_eh_frame_ = true;
266 sd->symbols_size = 0;
267 sd->external_symbols_offset = 0;
268 sd->symbol_names = NULL;
269 sd->symbol_names_size = 0;
271 if (this->symtab_shndx_ == 0)
273 // No symbol table. Weird but legal.
277 // Get the symbol table section header.
278 typename This::Shdr symtabshdr(pshdrs
279 + this->symtab_shndx_ * This::shdr_size);
280 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
282 // If this object has a .eh_frame section, we need all the symbols.
283 // Otherwise we only need the external symbols. While it would be
284 // simpler to just always read all the symbols, I've seen object
285 // files with well over 2000 local symbols, which for a 64-bit
286 // object file format is over 5 pages that we don't need to read
289 const int sym_size = This::sym_size;
290 const unsigned int loccount = symtabshdr.get_sh_info();
291 this->local_symbol_count_ = loccount;
292 off_t locsize = loccount * sym_size;
293 off_t dataoff = symtabshdr.get_sh_offset();
294 off_t datasize = symtabshdr.get_sh_size();
295 off_t extoff = dataoff + locsize;
296 off_t extsize = datasize - locsize;
298 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
299 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
301 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
303 // Read the section header for the symbol names.
304 unsigned int strtab_shndx = symtabshdr.get_sh_link();
305 if (strtab_shndx >= this->shnum())
307 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
310 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
311 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
313 this->error(_("symbol table name section has wrong type: %u"),
314 static_cast<unsigned int>(strtabshdr.get_sh_type()));
318 // Read the symbol names.
319 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
320 strtabshdr.get_sh_size(), true);
322 sd->symbols = fvsymtab;
323 sd->symbols_size = readsize;
324 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
325 sd->symbol_names = fvstrtab;
326 sd->symbol_names_size = strtabshdr.get_sh_size();
329 // Return the section index of symbol SYM. Set *VALUE to its value in
330 // the object file. Note that for a symbol which is not defined in
331 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
332 // it will not return the final value of the symbol in the link.
334 template<int size, bool big_endian>
336 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
340 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
344 const size_t count = symbols_size / This::sym_size;
345 gold_assert(sym < count);
347 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
348 *value = elfsym.get_st_value();
349 // FIXME: Handle SHN_XINDEX.
350 return elfsym.get_st_shndx();
353 // Return whether to include a section group in the link. LAYOUT is
354 // used to keep track of which section groups we have already seen.
355 // INDEX is the index of the section group and SHDR is the section
356 // header. If we do not want to include this group, we set bits in
357 // OMIT for each section which should be discarded.
359 template<int size, bool big_endian>
361 Sized_relobj<size, big_endian>::include_section_group(
364 const elfcpp::Shdr<size, big_endian>& shdr,
365 std::vector<bool>* omit)
367 // Read the section contents.
368 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
369 shdr.get_sh_size(), false);
370 const elfcpp::Elf_Word* pword =
371 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
373 // The first word contains flags. We only care about COMDAT section
374 // groups. Other section groups are always included in the link
375 // just like ordinary sections.
376 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
377 if ((flags & elfcpp::GRP_COMDAT) == 0)
380 // Look up the group signature, which is the name of a symbol. This
381 // is a lot of effort to go to to read a string. Why didn't they
382 // just use the name of the SHT_GROUP section as the group
385 // Get the appropriate symbol table header (this will normally be
386 // the single SHT_SYMTAB section, but in principle it need not be).
387 const unsigned int link = shdr.get_sh_link();
388 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
390 // Read the symbol table entry.
391 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
393 this->error(_("section group %u info %u out of range"),
394 index, shdr.get_sh_info());
397 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
398 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
399 elfcpp::Sym<size, big_endian> sym(psym);
401 // Read the symbol table names.
403 const unsigned char* psymnamesu;
404 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
406 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
408 // Get the section group signature.
409 if (sym.get_st_name() >= symnamelen)
411 this->error(_("symbol %u name offset %u out of range"),
412 shdr.get_sh_info(), sym.get_st_name());
416 const char* signature = psymnames + sym.get_st_name();
418 // It seems that some versions of gas will create a section group
419 // associated with a section symbol, and then fail to give a name to
420 // the section symbol. In such a case, use the name of the section.
423 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
425 secname = this->section_name(sym.get_st_shndx());
426 signature = secname.c_str();
429 // Record this section group, and see whether we've already seen one
430 // with the same signature.
431 if (layout->add_comdat(signature, true))
434 // This is a duplicate. We want to discard the sections in this
436 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
437 for (size_t i = 1; i < count; ++i)
439 elfcpp::Elf_Word secnum =
440 elfcpp::Swap<32, big_endian>::readval(pword + i);
441 if (secnum >= this->shnum())
443 this->error(_("section %u in section group %u out of range"),
447 (*omit)[secnum] = true;
453 // Whether to include a linkonce section in the link. NAME is the
454 // name of the section and SHDR is the section header.
456 // Linkonce sections are a GNU extension implemented in the original
457 // GNU linker before section groups were defined. The semantics are
458 // that we only include one linkonce section with a given name. The
459 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
460 // where T is the type of section and SYMNAME is the name of a symbol.
461 // In an attempt to make linkonce sections interact well with section
462 // groups, we try to identify SYMNAME and use it like a section group
463 // signature. We want to block section groups with that signature,
464 // but not other linkonce sections with that signature. We also use
465 // the full name of the linkonce section as a normal section group
468 template<int size, bool big_endian>
470 Sized_relobj<size, big_endian>::include_linkonce_section(
473 const elfcpp::Shdr<size, big_endian>&)
475 // In general the symbol name we want will be the string following
476 // the last '.'. However, we have to handle the case of
477 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
478 // some versions of gcc. So we use a heuristic: if the name starts
479 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
480 // we look for the last '.'. We can't always simply skip
481 // ".gnu.linkonce.X", because we have to deal with cases like
482 // ".gnu.linkonce.d.rel.ro.local".
483 const char* const linkonce_t = ".gnu.linkonce.t.";
485 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
486 symname = name + strlen(linkonce_t);
488 symname = strrchr(name, '.') + 1;
489 bool include1 = layout->add_comdat(symname, false);
490 bool include2 = layout->add_comdat(name, true);
491 return include1 && include2;
494 // Lay out the input sections. We walk through the sections and check
495 // whether they should be included in the link. If they should, we
496 // pass them to the Layout object, which will return an output section
499 template<int size, bool big_endian>
501 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
503 Read_symbols_data* sd)
505 const unsigned int shnum = this->shnum();
509 // Get the section headers.
510 const unsigned char* pshdrs = sd->section_headers->data();
512 // Get the section names.
513 const unsigned char* pnamesu = sd->section_names->data();
514 const char* pnames = reinterpret_cast<const char*>(pnamesu);
516 // For each section, record the index of the reloc section if any.
517 // Use 0 to mean that there is no reloc section, -1U to mean that
518 // there is more than one.
519 std::vector<unsigned int> reloc_shndx(shnum, 0);
520 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
521 // Skip the first, dummy, section.
522 pshdrs += This::shdr_size;
523 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
525 typename This::Shdr shdr(pshdrs);
527 unsigned int sh_type = shdr.get_sh_type();
528 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
530 unsigned int target_shndx = shdr.get_sh_info();
531 if (target_shndx == 0 || target_shndx >= shnum)
533 this->error(_("relocation section %u has bad info %u"),
538 if (reloc_shndx[target_shndx] != 0)
539 reloc_shndx[target_shndx] = -1U;
542 reloc_shndx[target_shndx] = i;
543 reloc_type[target_shndx] = sh_type;
548 std::vector<Map_to_output>& map_sections(this->map_to_output());
549 map_sections.resize(shnum);
551 // Whether we've seen a .note.GNU-stack section.
552 bool seen_gnu_stack = false;
553 // The flags of a .note.GNU-stack section.
554 uint64_t gnu_stack_flags = 0;
556 // Keep track of which sections to omit.
557 std::vector<bool> omit(shnum, false);
559 // Keep track of .eh_frame sections.
560 std::vector<unsigned int> eh_frame_sections;
562 // Skip the first, dummy, section.
563 pshdrs = sd->section_headers->data() + This::shdr_size;
564 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
566 typename This::Shdr shdr(pshdrs);
568 if (shdr.get_sh_name() >= sd->section_names_size)
570 this->error(_("bad section name offset for section %u: %lu"),
571 i, static_cast<unsigned long>(shdr.get_sh_name()));
575 const char* name = pnames + shdr.get_sh_name();
577 if (this->handle_gnu_warning_section(name, i, symtab))
579 if (!parameters->output_is_object())
583 // The .note.GNU-stack section is special. It gives the
584 // protection flags that this object file requires for the stack
586 if (strcmp(name, ".note.GNU-stack") == 0)
588 seen_gnu_stack = true;
589 gnu_stack_flags |= shdr.get_sh_flags();
593 bool discard = omit[i];
596 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
598 if (!this->include_section_group(layout, i, shdr, &omit))
601 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
602 && Layout::is_linkonce(name))
604 if (!this->include_linkonce_section(layout, name, shdr))
611 // Do not include this section in the link.
612 map_sections[i].output_section = NULL;
616 // The .eh_frame section is special. It holds exception frame
617 // information that we need to read in order to generate the
618 // exception frame header. We process these after all the other
619 // sections so that the exception frame reader can reliably
620 // determine which sections are being discarded, and discard the
621 // corresponding information.
622 if (!parameters->output_is_object()
623 && strcmp(name, ".eh_frame") == 0
624 && this->check_eh_frame_flags(&shdr))
626 eh_frame_sections.push_back(i);
631 Output_section* os = layout->layout(this, i, name, shdr,
632 reloc_shndx[i], reloc_type[i],
635 map_sections[i].output_section = os;
636 map_sections[i].offset = offset;
638 // If this section requires special handling, and if there are
639 // relocs that apply to it, then we must do the special handling
640 // before we apply the relocs.
641 if (offset == -1 && reloc_shndx[i] != 0)
642 this->set_relocs_must_follow_section_writes();
645 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
647 // Handle the .eh_frame sections at the end.
648 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
649 p != eh_frame_sections.end();
652 gold_assert(this->has_eh_frame_);
653 gold_assert(sd->external_symbols_offset != 0);
656 const unsigned char *pshdr;
657 pshdr = sd->section_headers->data() + i * This::shdr_size;
658 typename This::Shdr shdr(pshdr);
661 Output_section* os = layout->layout_eh_frame(this,
664 sd->symbol_names->data(),
665 sd->symbol_names_size,
670 map_sections[i].output_section = os;
671 map_sections[i].offset = offset;
673 // If this section requires special handling, and if there are
674 // relocs that apply to it, then we must do the special handling
675 // before we apply the relocs.
676 if (offset == -1 && reloc_shndx[i] != 0)
677 this->set_relocs_must_follow_section_writes();
680 delete sd->section_headers;
681 sd->section_headers = NULL;
682 delete sd->section_names;
683 sd->section_names = NULL;
686 // Add the symbols to the symbol table.
688 template<int size, bool big_endian>
690 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
691 Read_symbols_data* sd)
693 if (sd->symbols == NULL)
695 gold_assert(sd->symbol_names == NULL);
699 const int sym_size = This::sym_size;
700 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
702 if (static_cast<off_t>(symcount * sym_size)
703 != sd->symbols_size - sd->external_symbols_offset)
705 this->error(_("size of symbols is not multiple of symbol size"));
709 this->symbols_.resize(symcount);
711 const char* sym_names =
712 reinterpret_cast<const char*>(sd->symbol_names->data());
713 symtab->add_from_relobj(this,
714 sd->symbols->data() + sd->external_symbols_offset,
715 symcount, sym_names, sd->symbol_names_size,
720 delete sd->symbol_names;
721 sd->symbol_names = NULL;
724 // Finalize the local symbols. Here we record the file offset at
725 // which they should be output, we add their names to *POOL, and we
726 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
727 // This function is always called from the main thread. The actual
728 // output of the local symbols will occur in a separate task.
730 template<int size, bool big_endian>
732 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
736 gold_assert(this->symtab_shndx_ != -1U);
737 if (this->symtab_shndx_ == 0)
739 // This object has no symbols. Weird but legal.
743 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
745 this->local_symbol_offset_ = off;
747 // Read the symbol table section header.
748 const unsigned int symtab_shndx = this->symtab_shndx_;
749 typename This::Shdr symtabshdr(this,
750 this->elf_file_.section_header(symtab_shndx));
751 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
753 // Read the local symbols.
754 const int sym_size = This::sym_size;
755 const unsigned int loccount = this->local_symbol_count_;
756 gold_assert(loccount == symtabshdr.get_sh_info());
757 off_t locsize = loccount * sym_size;
758 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
761 this->local_values_.resize(loccount);
763 // Read the symbol names.
764 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
766 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
769 const char* pnames = reinterpret_cast<const char*>(pnamesu);
771 // Loop over the local symbols.
773 const std::vector<Map_to_output>& mo(this->map_to_output());
774 unsigned int shnum = this->shnum();
775 unsigned int count = 0;
776 // Skip the first, dummy, symbol.
778 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
780 elfcpp::Sym<size, big_endian> sym(psyms);
782 Symbol_value<size>& lv(this->local_values_[i]);
784 unsigned int shndx = sym.get_st_shndx();
785 lv.set_input_shndx(shndx);
787 if (sym.get_st_type() == elfcpp::STT_SECTION)
788 lv.set_is_section_symbol();
790 if (shndx >= elfcpp::SHN_LORESERVE)
792 if (shndx == elfcpp::SHN_ABS)
793 lv.set_output_value(sym.get_st_value());
796 // FIXME: Handle SHN_XINDEX.
797 this->error(_("unknown section index %u for local symbol %u"),
799 lv.set_output_value(0);
806 this->error(_("local symbol %u section index %u out of range"),
811 Output_section* os = mo[shndx].output_section;
815 lv.set_output_value(0);
816 lv.set_no_output_symtab_entry();
820 if (mo[shndx].offset == -1)
821 lv.set_input_value(sym.get_st_value());
823 lv.set_output_value(mo[shndx].output_section->address()
825 + sym.get_st_value());
828 // Decide whether this symbol should go into the output file.
830 if (sym.get_st_type() == elfcpp::STT_SECTION)
832 lv.set_no_output_symtab_entry();
836 if (sym.get_st_name() >= strtab_size)
838 this->error(_("local symbol %u section name out of range: %u >= %u"),
839 i, sym.get_st_name(),
840 static_cast<unsigned int>(strtab_size));
841 lv.set_no_output_symtab_entry();
845 const char* name = pnames + sym.get_st_name();
846 pool->add(name, true, NULL);
847 lv.set_output_symtab_index(index);
852 this->output_local_symbol_count_ = count;
857 // Return the value of the local symbol symndx.
858 template<int size, bool big_endian>
859 typename elfcpp::Elf_types<size>::Elf_Addr
860 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
862 gold_assert(symndx < this->local_symbol_count_);
863 gold_assert(symndx < this->local_values_.size());
864 const Symbol_value<size>& lv(this->local_values_[symndx]);
865 return lv.value(this, 0);
868 // Return the value of a local symbol defined in input section SHNDX,
869 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
870 // indicates whether the symbol is a section symbol. This handles
871 // SHF_MERGE sections.
872 template<int size, bool big_endian>
873 typename elfcpp::Elf_types<size>::Elf_Addr
874 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
876 bool is_section_symbol,
877 Address addend) const
879 const std::vector<Map_to_output>& mo(this->map_to_output());
880 Output_section* os = mo[shndx].output_section;
883 gold_assert(mo[shndx].offset == -1);
885 // Do the mapping required by the output section. If this is not a
886 // section symbol, then we want to map the symbol value, and then
887 // include the addend. If this is a section symbol, then we need to
888 // include the addend to figure out where in the section we are,
889 // before we do the mapping. This will do the right thing provided
890 // the assembler is careful to only convert a relocation in a merged
891 // section to a section symbol if there is a zero addend. If the
892 // assembler does not do this, then in general we can't know what to
893 // do, because we can't distinguish the addend for the instruction
894 // format from the addend for the section offset.
896 if (is_section_symbol)
897 return os->output_address(this, shndx, value + addend);
899 return addend + os->output_address(this, shndx, value);
902 // Write out the local symbols.
904 template<int size, bool big_endian>
906 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
907 const Stringpool* sympool)
909 if (parameters->strip_all())
912 gold_assert(this->symtab_shndx_ != -1U);
913 if (this->symtab_shndx_ == 0)
915 // This object has no symbols. Weird but legal.
919 // Read the symbol table section header.
920 const unsigned int symtab_shndx = this->symtab_shndx_;
921 typename This::Shdr symtabshdr(this,
922 this->elf_file_.section_header(symtab_shndx));
923 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
924 const unsigned int loccount = this->local_symbol_count_;
925 gold_assert(loccount == symtabshdr.get_sh_info());
927 // Read the local symbols.
928 const int sym_size = This::sym_size;
929 off_t locsize = loccount * sym_size;
930 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
933 // Read the symbol names.
934 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
936 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
939 const char* pnames = reinterpret_cast<const char*>(pnamesu);
941 // Get a view into the output file.
942 off_t output_size = this->output_local_symbol_count_ * sym_size;
943 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
946 const std::vector<Map_to_output>& mo(this->map_to_output());
948 gold_assert(this->local_values_.size() == loccount);
950 unsigned char* ov = oview;
952 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
954 elfcpp::Sym<size, big_endian> isym(psyms);
956 if (!this->local_values_[i].needs_output_symtab_entry())
959 unsigned int st_shndx = isym.get_st_shndx();
960 if (st_shndx < elfcpp::SHN_LORESERVE)
962 gold_assert(st_shndx < mo.size());
963 if (mo[st_shndx].output_section == NULL)
965 st_shndx = mo[st_shndx].output_section->out_shndx();
968 elfcpp::Sym_write<size, big_endian> osym(ov);
970 gold_assert(isym.get_st_name() < strtab_size);
971 const char* name = pnames + isym.get_st_name();
972 osym.put_st_name(sympool->get_offset(name));
973 osym.put_st_value(this->local_values_[i].value(this, 0));
974 osym.put_st_size(isym.get_st_size());
975 osym.put_st_info(isym.get_st_info());
976 osym.put_st_other(isym.get_st_other());
977 osym.put_st_shndx(st_shndx);
982 gold_assert(ov - oview == output_size);
984 of->write_output_view(this->local_symbol_offset_, output_size, oview);
987 // Set *INFO to symbolic information about the offset OFFSET in the
988 // section SHNDX. Return true if we found something, false if we
991 template<int size, bool big_endian>
993 Sized_relobj<size, big_endian>::get_symbol_location_info(
996 Symbol_location_info* info)
998 if (this->symtab_shndx_ == 0)
1002 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1006 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1008 const unsigned char* symbol_names_u =
1009 this->section_contents(symbol_names_shndx, &names_size, false);
1010 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1012 const int sym_size = This::sym_size;
1013 const size_t count = symbols_size / sym_size;
1015 const unsigned char* p = symbols;
1016 for (size_t i = 0; i < count; ++i, p += sym_size)
1018 elfcpp::Sym<size, big_endian> sym(p);
1020 if (sym.get_st_type() == elfcpp::STT_FILE)
1022 if (sym.get_st_name() >= names_size)
1023 info->source_file = "(invalid)";
1025 info->source_file = symbol_names + sym.get_st_name();
1027 else if (sym.get_st_shndx() == shndx
1028 && static_cast<off_t>(sym.get_st_value()) <= offset
1029 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1032 if (sym.get_st_name() > names_size)
1033 info->enclosing_symbol_name = "(invalid)";
1035 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1043 // Input_objects methods.
1045 // Add a regular relocatable object to the list. Return false if this
1046 // object should be ignored.
1049 Input_objects::add_object(Object* obj)
1051 Target* target = obj->target();
1052 if (this->target_ == NULL)
1053 this->target_ = target;
1054 else if (this->target_ != target)
1056 gold_error(_("%s: incompatible target"), obj->name().c_str());
1060 if (!obj->is_dynamic())
1061 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1064 // See if this is a duplicate SONAME.
1065 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1066 const char* soname = dynobj->soname();
1068 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1069 this->sonames_.insert(soname);
1072 // We have already seen a dynamic object with this soname.
1076 this->dynobj_list_.push_back(dynobj);
1078 // If this is -lc, remember the directory in which we found it.
1079 // We use this when issuing warnings about undefined symbols: as
1080 // a heuristic, we don't warn about system libraries found in
1081 // the same directory as -lc.
1082 if (strncmp(soname, "libc.so", 7) == 0)
1084 const char* object_name = dynobj->name().c_str();
1085 const char* base = lbasename(object_name);
1086 if (base != object_name)
1087 this->system_library_directory_.assign(object_name,
1088 base - 1 - object_name);
1092 set_parameters_size_and_endianness(target->get_size(),
1093 target->is_big_endian());
1098 // Return whether an object was found in the system library directory.
1101 Input_objects::found_in_system_library_directory(const Object* object) const
1103 return (!this->system_library_directory_.empty()
1104 && object->name().compare(0,
1105 this->system_library_directory_.size(),
1106 this->system_library_directory_) == 0);
1109 // For each dynamic object, record whether we've seen all of its
1110 // explicit dependencies.
1113 Input_objects::check_dynamic_dependencies() const
1115 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1116 p != this->dynobj_list_.end();
1119 const Dynobj::Needed& needed((*p)->needed());
1120 bool found_all = true;
1121 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1122 pneeded != needed.end();
1125 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1131 (*p)->set_has_unknown_needed_entries(!found_all);
1135 // Relocate_info methods.
1137 // Return a string describing the location of a relocation. This is
1138 // only used in error messages.
1140 template<int size, bool big_endian>
1142 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1144 // See if we can get line-number information from debugging sections.
1145 std::string filename;
1146 std::string file_and_lineno; // Better than filename-only, if available.
1148 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1149 // This will be "" if we failed to parse the debug info for any reason.
1150 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1152 std::string ret(this->object->name());
1154 Symbol_location_info info;
1155 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1157 ret += " in function ";
1158 // We could demangle this name before printing, but we don't
1159 // bother because gcc runs linker output through a demangle
1160 // filter itself. The only advantage to demangling here is if
1161 // someone might call ld directly, rather than via gcc. If we
1162 // did want to demangle, cplus_demangle() is in libiberty.
1163 ret += info.enclosing_symbol_name;
1165 filename = info.source_file;
1168 if (!file_and_lineno.empty())
1169 ret += file_and_lineno;
1172 if (!filename.empty())
1175 ret += this->object->section_name(this->data_shndx);
1177 // Offsets into sections have to be positive.
1178 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1185 } // End namespace gold.
1190 using namespace gold;
1192 // Read an ELF file with the header and return the appropriate
1193 // instance of Object.
1195 template<int size, bool big_endian>
1197 make_elf_sized_object(const std::string& name, Input_file* input_file,
1198 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1200 int et = ehdr.get_e_type();
1201 if (et == elfcpp::ET_REL)
1203 Sized_relobj<size, big_endian>* obj =
1204 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1208 else if (et == elfcpp::ET_DYN)
1210 Sized_dynobj<size, big_endian>* obj =
1211 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1217 gold_error(_("%s: unsupported ELF file type %d"),
1223 } // End anonymous namespace.
1228 // Read an ELF file and return the appropriate instance of Object.
1231 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1232 const unsigned char* p, off_t bytes)
1234 if (bytes < elfcpp::EI_NIDENT)
1236 gold_error(_("%s: ELF file too short"), name.c_str());
1240 int v = p[elfcpp::EI_VERSION];
1241 if (v != elfcpp::EV_CURRENT)
1243 if (v == elfcpp::EV_NONE)
1244 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1246 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1250 int c = p[elfcpp::EI_CLASS];
1251 if (c == elfcpp::ELFCLASSNONE)
1253 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1256 else if (c != elfcpp::ELFCLASS32
1257 && c != elfcpp::ELFCLASS64)
1259 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1263 int d = p[elfcpp::EI_DATA];
1264 if (d == elfcpp::ELFDATANONE)
1266 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1269 else if (d != elfcpp::ELFDATA2LSB
1270 && d != elfcpp::ELFDATA2MSB)
1272 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1276 bool big_endian = d == elfcpp::ELFDATA2MSB;
1278 if (c == elfcpp::ELFCLASS32)
1280 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1282 gold_error(_("%s: ELF file too short"), name.c_str());
1287 #ifdef HAVE_TARGET_32_BIG
1288 elfcpp::Ehdr<32, true> ehdr(p);
1289 return make_elf_sized_object<32, true>(name, input_file,
1292 gold_error(_("%s: not configured to support "
1293 "32-bit big-endian object"),
1300 #ifdef HAVE_TARGET_32_LITTLE
1301 elfcpp::Ehdr<32, false> ehdr(p);
1302 return make_elf_sized_object<32, false>(name, input_file,
1305 gold_error(_("%s: not configured to support "
1306 "32-bit little-endian object"),
1314 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1316 gold_error(_("%s: ELF file too short"), name.c_str());
1321 #ifdef HAVE_TARGET_64_BIG
1322 elfcpp::Ehdr<64, true> ehdr(p);
1323 return make_elf_sized_object<64, true>(name, input_file,
1326 gold_error(_("%s: not configured to support "
1327 "64-bit big-endian object"),
1334 #ifdef HAVE_TARGET_64_LITTLE
1335 elfcpp::Ehdr<64, false> ehdr(p);
1336 return make_elf_sized_object<64, false>(name, input_file,
1339 gold_error(_("%s: not configured to support "
1340 "64-bit little-endian object"),
1348 // Instantiate the templates we need. We could use the configure
1349 // script to restrict this to only the ones for implemented targets.
1351 #ifdef HAVE_TARGET_32_LITTLE
1353 class Sized_relobj<32, false>;
1356 #ifdef HAVE_TARGET_32_BIG
1358 class Sized_relobj<32, true>;
1361 #ifdef HAVE_TARGET_64_LITTLE
1363 class Sized_relobj<64, false>;
1366 #ifdef HAVE_TARGET_64_BIG
1368 class Sized_relobj<64, true>;
1371 #ifdef HAVE_TARGET_32_LITTLE
1373 struct Relocate_info<32, false>;
1376 #ifdef HAVE_TARGET_32_BIG
1378 struct Relocate_info<32, true>;
1381 #ifdef HAVE_TARGET_64_LITTLE
1383 struct Relocate_info<64, false>;
1386 #ifdef HAVE_TARGET_64_BIG
1388 struct Relocate_info<64, true>;
1391 } // End namespace gold.