1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "target-select.h"
30 #include "dwarf_reader.h"
42 // Set the target based on fields in the ELF file header.
45 Object::set_target(int machine, int size, bool big_endian, int osabi,
48 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
50 gold_fatal(_("%s: unsupported ELF machine number %d"),
51 this->name().c_str(), machine);
52 this->target_ = target;
55 // Report an error for this object file. This is used by the
56 // elfcpp::Elf_file interface, and also called by the Object code
60 Object::error(const char* format, ...) const
63 va_start(args, format);
65 if (vasprintf(&buf, format, args) < 0)
68 gold_error(_("%s: %s"), this->name().c_str(), buf);
72 // Return a view of the contents of a section.
75 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
77 Location loc(this->do_section_contents(shndx));
78 *plen = loc.data_size;
79 return this->get_view(loc.file_offset, loc.data_size, cache);
82 // Read the section data into SD. This is code common to Sized_relobj
83 // and Sized_dynobj, so we put it into Object.
85 template<int size, bool big_endian>
87 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
88 Read_symbols_data* sd)
90 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
92 // Read the section headers.
93 const off_t shoff = elf_file->shoff();
94 const unsigned int shnum = this->shnum();
95 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
97 // Read the section names.
98 const unsigned char* pshdrs = sd->section_headers->data();
99 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
100 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
102 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
103 this->error(_("section name section has wrong type: %u"),
104 static_cast<unsigned int>(shdrnames.get_sh_type()));
106 sd->section_names_size = shdrnames.get_sh_size();
107 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
108 sd->section_names_size, false);
111 // If NAME is the name of a special .gnu.warning section, arrange for
112 // the warning to be issued. SHNDX is the section index. Return
113 // whether it is a warning section.
116 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
117 Symbol_table* symtab)
119 const char warn_prefix[] = ".gnu.warning.";
120 const int warn_prefix_len = sizeof warn_prefix - 1;
121 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
123 symtab->add_warning(name + warn_prefix_len, this, shndx);
129 // Class Sized_relobj.
131 template<int size, bool big_endian>
132 Sized_relobj<size, big_endian>::Sized_relobj(
133 const std::string& name,
134 Input_file* input_file,
136 const elfcpp::Ehdr<size, big_endian>& ehdr)
137 : Relobj(name, input_file, offset),
138 elf_file_(this, ehdr),
140 local_symbol_count_(0),
141 output_local_symbol_count_(0),
143 local_symbol_offset_(0),
145 local_got_offsets_(),
150 template<int size, bool big_endian>
151 Sized_relobj<size, big_endian>::~Sized_relobj()
155 // Set up an object file based on the file header. This sets up the
156 // target and reads the section information.
158 template<int size, bool big_endian>
160 Sized_relobj<size, big_endian>::setup(
161 const elfcpp::Ehdr<size, big_endian>& ehdr)
163 this->set_target(ehdr.get_e_machine(), size, big_endian,
164 ehdr.get_e_ident()[elfcpp::EI_OSABI],
165 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
167 const unsigned int shnum = this->elf_file_.shnum();
168 this->set_shnum(shnum);
171 // Find the SHT_SYMTAB section, given the section headers. The ELF
172 // standard says that maybe in the future there can be more than one
173 // SHT_SYMTAB section. Until somebody figures out how that could
174 // work, we assume there is only one.
176 template<int size, bool big_endian>
178 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
180 const unsigned int shnum = this->shnum();
181 this->symtab_shndx_ = 0;
184 // Look through the sections in reverse order, since gas tends
185 // to put the symbol table at the end.
186 const unsigned char* p = pshdrs + shnum * This::shdr_size;
187 unsigned int i = shnum;
191 p -= This::shdr_size;
192 typename This::Shdr shdr(p);
193 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
195 this->symtab_shndx_ = i;
202 // Return whether SHDR has the right type and flags to be a GNU
203 // .eh_frame section.
205 template<int size, bool big_endian>
207 Sized_relobj<size, big_endian>::check_eh_frame_flags(
208 const elfcpp::Shdr<size, big_endian>* shdr) const
210 return (shdr->get_sh_size() > 0
211 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
212 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
215 // Return whether there is a GNU .eh_frame section, given the section
216 // headers and the section names.
218 template<int size, bool big_endian>
220 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
222 off_t names_size) const
224 const unsigned int shnum = this->shnum();
225 const unsigned char* p = pshdrs + This::shdr_size;
226 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
228 typename This::Shdr shdr(p);
229 if (this->check_eh_frame_flags(&shdr))
231 if (shdr.get_sh_name() >= names_size)
233 this->error(_("bad section name offset for section %u: %lu"),
234 i, static_cast<unsigned long>(shdr.get_sh_name()));
238 const char* name = names + shdr.get_sh_name();
239 if (strcmp(name, ".eh_frame") == 0)
246 // Read the sections and symbols from an object file.
248 template<int size, bool big_endian>
250 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
252 this->read_section_data(&this->elf_file_, sd);
254 const unsigned char* const pshdrs = sd->section_headers->data();
256 this->find_symtab(pshdrs);
258 const unsigned char* namesu = sd->section_names->data();
259 const char* names = reinterpret_cast<const char*>(namesu);
260 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
261 this->has_eh_frame_ = true;
264 sd->symbols_size = 0;
265 sd->external_symbols_offset = 0;
266 sd->symbol_names = NULL;
267 sd->symbol_names_size = 0;
269 if (this->symtab_shndx_ == 0)
271 // No symbol table. Weird but legal.
275 // Get the symbol table section header.
276 typename This::Shdr symtabshdr(pshdrs
277 + this->symtab_shndx_ * This::shdr_size);
278 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
280 // If this object has a .eh_frame section, we need all the symbols.
281 // Otherwise we only need the external symbols. While it would be
282 // simpler to just always read all the symbols, I've seen object
283 // files with well over 2000 local symbols, which for a 64-bit
284 // object file format is over 5 pages that we don't need to read
287 const int sym_size = This::sym_size;
288 const unsigned int loccount = symtabshdr.get_sh_info();
289 this->local_symbol_count_ = loccount;
290 off_t locsize = loccount * sym_size;
291 off_t dataoff = symtabshdr.get_sh_offset();
292 off_t datasize = symtabshdr.get_sh_size();
293 off_t extoff = dataoff + locsize;
294 off_t extsize = datasize - locsize;
296 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
297 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
299 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
301 // Read the section header for the symbol names.
302 unsigned int strtab_shndx = symtabshdr.get_sh_link();
303 if (strtab_shndx >= this->shnum())
305 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
308 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
309 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
311 this->error(_("symbol table name section has wrong type: %u"),
312 static_cast<unsigned int>(strtabshdr.get_sh_type()));
316 // Read the symbol names.
317 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
318 strtabshdr.get_sh_size(), true);
320 sd->symbols = fvsymtab;
321 sd->symbols_size = readsize;
322 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
323 sd->symbol_names = fvstrtab;
324 sd->symbol_names_size = strtabshdr.get_sh_size();
327 // Return the section index of symbol SYM. Set *VALUE to its value in
328 // the object file. Note that for a symbol which is not defined in
329 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
330 // it will not return the final value of the symbol in the link.
332 template<int size, bool big_endian>
334 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
338 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
342 const size_t count = symbols_size / This::sym_size;
343 gold_assert(sym < count);
345 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
346 *value = elfsym.get_st_value();
347 // FIXME: Handle SHN_XINDEX.
348 return elfsym.get_st_shndx();
351 // Return whether to include a section group in the link. LAYOUT is
352 // used to keep track of which section groups we have already seen.
353 // INDEX is the index of the section group and SHDR is the section
354 // header. If we do not want to include this group, we set bits in
355 // OMIT for each section which should be discarded.
357 template<int size, bool big_endian>
359 Sized_relobj<size, big_endian>::include_section_group(
362 const elfcpp::Shdr<size, big_endian>& shdr,
363 std::vector<bool>* omit)
365 // Read the section contents.
366 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
367 shdr.get_sh_size(), false);
368 const elfcpp::Elf_Word* pword =
369 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
371 // The first word contains flags. We only care about COMDAT section
372 // groups. Other section groups are always included in the link
373 // just like ordinary sections.
374 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
375 if ((flags & elfcpp::GRP_COMDAT) == 0)
378 // Look up the group signature, which is the name of a symbol. This
379 // is a lot of effort to go to to read a string. Why didn't they
380 // just use the name of the SHT_GROUP section as the group
383 // Get the appropriate symbol table header (this will normally be
384 // the single SHT_SYMTAB section, but in principle it need not be).
385 const unsigned int link = shdr.get_sh_link();
386 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
388 // Read the symbol table entry.
389 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
391 this->error(_("section group %u info %u out of range"),
392 index, shdr.get_sh_info());
395 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
396 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
397 elfcpp::Sym<size, big_endian> sym(psym);
399 // Read the symbol table names.
401 const unsigned char* psymnamesu;
402 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
404 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
406 // Get the section group signature.
407 if (sym.get_st_name() >= symnamelen)
409 this->error(_("symbol %u name offset %u out of range"),
410 shdr.get_sh_info(), sym.get_st_name());
414 const char* signature = psymnames + sym.get_st_name();
416 // It seems that some versions of gas will create a section group
417 // associated with a section symbol, and then fail to give a name to
418 // the section symbol. In such a case, use the name of the section.
421 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
423 secname = this->section_name(sym.get_st_shndx());
424 signature = secname.c_str();
427 // Record this section group, and see whether we've already seen one
428 // with the same signature.
429 if (layout->add_comdat(signature, true))
432 // This is a duplicate. We want to discard the sections in this
434 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
435 for (size_t i = 1; i < count; ++i)
437 elfcpp::Elf_Word secnum =
438 elfcpp::Swap<32, big_endian>::readval(pword + i);
439 if (secnum >= this->shnum())
441 this->error(_("section %u in section group %u out of range"),
445 (*omit)[secnum] = true;
451 // Whether to include a linkonce section in the link. NAME is the
452 // name of the section and SHDR is the section header.
454 // Linkonce sections are a GNU extension implemented in the original
455 // GNU linker before section groups were defined. The semantics are
456 // that we only include one linkonce section with a given name. The
457 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
458 // where T is the type of section and SYMNAME is the name of a symbol.
459 // In an attempt to make linkonce sections interact well with section
460 // groups, we try to identify SYMNAME and use it like a section group
461 // signature. We want to block section groups with that signature,
462 // but not other linkonce sections with that signature. We also use
463 // the full name of the linkonce section as a normal section group
466 template<int size, bool big_endian>
468 Sized_relobj<size, big_endian>::include_linkonce_section(
471 const elfcpp::Shdr<size, big_endian>&)
473 // In general the symbol name we want will be the string following
474 // the last '.'. However, we have to handle the case of
475 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
476 // some versions of gcc. So we use a heuristic: if the name starts
477 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
478 // we look for the last '.'. We can't always simply skip
479 // ".gnu.linkonce.X", because we have to deal with cases like
480 // ".gnu.linkonce.d.rel.ro.local".
481 const char* const linkonce_t = ".gnu.linkonce.t.";
483 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
484 symname = name + strlen(linkonce_t);
486 symname = strrchr(name, '.') + 1;
487 bool include1 = layout->add_comdat(symname, false);
488 bool include2 = layout->add_comdat(name, true);
489 return include1 && include2;
492 // Lay out the input sections. We walk through the sections and check
493 // whether they should be included in the link. If they should, we
494 // pass them to the Layout object, which will return an output section
497 template<int size, bool big_endian>
499 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
501 Read_symbols_data* sd)
503 const unsigned int shnum = this->shnum();
507 // Get the section headers.
508 const unsigned char* pshdrs = sd->section_headers->data();
510 // Get the section names.
511 const unsigned char* pnamesu = sd->section_names->data();
512 const char* pnames = reinterpret_cast<const char*>(pnamesu);
514 // For each section, record the index of the reloc section if any.
515 // Use 0 to mean that there is no reloc section, -1U to mean that
516 // there is more than one.
517 std::vector<unsigned int> reloc_shndx(shnum, 0);
518 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
519 // Skip the first, dummy, section.
520 pshdrs += This::shdr_size;
521 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
523 typename This::Shdr shdr(pshdrs);
525 unsigned int sh_type = shdr.get_sh_type();
526 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
528 unsigned int target_shndx = shdr.get_sh_info();
529 if (target_shndx == 0 || target_shndx >= shnum)
531 this->error(_("relocation section %u has bad info %u"),
536 if (reloc_shndx[target_shndx] != 0)
537 reloc_shndx[target_shndx] = -1U;
540 reloc_shndx[target_shndx] = i;
541 reloc_type[target_shndx] = sh_type;
546 std::vector<Map_to_output>& map_sections(this->map_to_output());
547 map_sections.resize(shnum);
549 // Whether we've seen a .note.GNU-stack section.
550 bool seen_gnu_stack = false;
551 // The flags of a .note.GNU-stack section.
552 uint64_t gnu_stack_flags = 0;
554 // Keep track of which sections to omit.
555 std::vector<bool> omit(shnum, false);
557 // Keep track of .eh_frame sections.
558 std::vector<unsigned int> eh_frame_sections;
560 // Skip the first, dummy, section.
561 pshdrs = sd->section_headers->data() + This::shdr_size;
562 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
564 typename This::Shdr shdr(pshdrs);
566 if (shdr.get_sh_name() >= sd->section_names_size)
568 this->error(_("bad section name offset for section %u: %lu"),
569 i, static_cast<unsigned long>(shdr.get_sh_name()));
573 const char* name = pnames + shdr.get_sh_name();
575 if (this->handle_gnu_warning_section(name, i, symtab))
577 if (!parameters->output_is_object())
581 // The .note.GNU-stack section is special. It gives the
582 // protection flags that this object file requires for the stack
584 if (strcmp(name, ".note.GNU-stack") == 0)
586 seen_gnu_stack = true;
587 gnu_stack_flags |= shdr.get_sh_flags();
591 bool discard = omit[i];
594 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
596 if (!this->include_section_group(layout, i, shdr, &omit))
599 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
600 && Layout::is_linkonce(name))
602 if (!this->include_linkonce_section(layout, name, shdr))
609 // Do not include this section in the link.
610 map_sections[i].output_section = NULL;
614 // The .eh_frame section is special. It holds exception frame
615 // information that we need to read in order to generate the
616 // exception frame header. We process these after all the other
617 // sections so that the exception frame reader can reliably
618 // determine which sections are being discarded, and discard the
619 // corresponding information.
620 if (!parameters->output_is_object()
621 && strcmp(name, ".eh_frame") == 0
622 && this->check_eh_frame_flags(&shdr))
624 eh_frame_sections.push_back(i);
629 Output_section* os = layout->layout(this, i, name, shdr,
630 reloc_shndx[i], reloc_type[i],
633 map_sections[i].output_section = os;
634 map_sections[i].offset = offset;
636 // If this section requires special handling, and if there are
637 // relocs that apply to it, then we must do the special handling
638 // before we apply the relocs.
639 if (offset == -1 && reloc_shndx[i] != 0)
640 this->set_relocs_must_follow_section_writes();
643 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
645 // Handle the .eh_frame sections at the end.
646 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
647 p != eh_frame_sections.end();
650 gold_assert(this->has_eh_frame_);
651 gold_assert(sd->external_symbols_offset != 0);
654 const unsigned char *pshdr;
655 pshdr = sd->section_headers->data() + i * This::shdr_size;
656 typename This::Shdr shdr(pshdr);
659 Output_section* os = layout->layout_eh_frame(this,
662 sd->symbol_names->data(),
663 sd->symbol_names_size,
668 map_sections[i].output_section = os;
669 map_sections[i].offset = offset;
671 // If this section requires special handling, and if there are
672 // relocs that apply to it, then we must do the special handling
673 // before we apply the relocs.
674 if (offset == -1 && reloc_shndx[i] != 0)
675 this->set_relocs_must_follow_section_writes();
678 delete sd->section_headers;
679 sd->section_headers = NULL;
680 delete sd->section_names;
681 sd->section_names = NULL;
684 // Add the symbols to the symbol table.
686 template<int size, bool big_endian>
688 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
689 Read_symbols_data* sd)
691 if (sd->symbols == NULL)
693 gold_assert(sd->symbol_names == NULL);
697 const int sym_size = This::sym_size;
698 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
700 if (static_cast<off_t>(symcount * sym_size)
701 != sd->symbols_size - sd->external_symbols_offset)
703 this->error(_("size of symbols is not multiple of symbol size"));
707 this->symbols_.resize(symcount);
709 const char* sym_names =
710 reinterpret_cast<const char*>(sd->symbol_names->data());
711 symtab->add_from_relobj(this,
712 sd->symbols->data() + sd->external_symbols_offset,
713 symcount, sym_names, sd->symbol_names_size,
718 delete sd->symbol_names;
719 sd->symbol_names = NULL;
722 // Finalize the local symbols. Here we record the file offset at
723 // which they should be output, we add their names to *POOL, and we
724 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
725 // This function is always called from the main thread. The actual
726 // output of the local symbols will occur in a separate task.
728 template<int size, bool big_endian>
730 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
734 gold_assert(this->symtab_shndx_ != -1U);
735 if (this->symtab_shndx_ == 0)
737 // This object has no symbols. Weird but legal.
741 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
743 this->local_symbol_offset_ = off;
745 // Read the symbol table section header.
746 const unsigned int symtab_shndx = this->symtab_shndx_;
747 typename This::Shdr symtabshdr(this,
748 this->elf_file_.section_header(symtab_shndx));
749 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
751 // Read the local symbols.
752 const int sym_size = This::sym_size;
753 const unsigned int loccount = this->local_symbol_count_;
754 gold_assert(loccount == symtabshdr.get_sh_info());
755 off_t locsize = loccount * sym_size;
756 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
759 this->local_values_.resize(loccount);
761 // Read the symbol names.
762 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
764 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
767 const char* pnames = reinterpret_cast<const char*>(pnamesu);
769 // Loop over the local symbols.
771 const std::vector<Map_to_output>& mo(this->map_to_output());
772 unsigned int shnum = this->shnum();
773 unsigned int count = 0;
774 // Skip the first, dummy, symbol.
776 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
778 elfcpp::Sym<size, big_endian> sym(psyms);
780 Symbol_value<size>& lv(this->local_values_[i]);
782 unsigned int shndx = sym.get_st_shndx();
783 lv.set_input_shndx(shndx);
785 if (sym.get_st_type() == elfcpp::STT_SECTION)
786 lv.set_is_section_symbol();
788 if (shndx >= elfcpp::SHN_LORESERVE)
790 if (shndx == elfcpp::SHN_ABS)
791 lv.set_output_value(sym.get_st_value());
794 // FIXME: Handle SHN_XINDEX.
795 this->error(_("unknown section index %u for local symbol %u"),
797 lv.set_output_value(0);
804 this->error(_("local symbol %u section index %u out of range"),
809 Output_section* os = mo[shndx].output_section;
813 lv.set_output_value(0);
814 lv.set_no_output_symtab_entry();
818 if (mo[shndx].offset == -1)
819 lv.set_input_value(sym.get_st_value());
821 lv.set_output_value(mo[shndx].output_section->address()
823 + sym.get_st_value());
826 // Decide whether this symbol should go into the output file.
828 if (sym.get_st_type() == elfcpp::STT_SECTION)
830 lv.set_no_output_symtab_entry();
834 if (sym.get_st_name() >= strtab_size)
836 this->error(_("local symbol %u section name out of range: %u >= %u"),
837 i, sym.get_st_name(),
838 static_cast<unsigned int>(strtab_size));
839 lv.set_no_output_symtab_entry();
843 const char* name = pnames + sym.get_st_name();
844 pool->add(name, true, NULL);
845 lv.set_output_symtab_index(index);
850 this->output_local_symbol_count_ = count;
855 // Return the value of the local symbol symndx.
856 template<int size, bool big_endian>
857 typename elfcpp::Elf_types<size>::Elf_Addr
858 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
860 gold_assert(symndx < this->local_symbol_count_);
861 gold_assert(symndx < this->local_values_.size());
862 const Symbol_value<size>& lv(this->local_values_[symndx]);
863 return lv.value(this, 0);
866 // Return the value of a local symbol defined in input section SHNDX,
867 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
868 // indicates whether the symbol is a section symbol. This handles
869 // SHF_MERGE sections.
870 template<int size, bool big_endian>
871 typename elfcpp::Elf_types<size>::Elf_Addr
872 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
874 bool is_section_symbol,
875 Address addend) const
877 const std::vector<Map_to_output>& mo(this->map_to_output());
878 Output_section* os = mo[shndx].output_section;
881 gold_assert(mo[shndx].offset == -1);
883 // Do the mapping required by the output section. If this is not a
884 // section symbol, then we want to map the symbol value, and then
885 // include the addend. If this is a section symbol, then we need to
886 // include the addend to figure out where in the section we are,
887 // before we do the mapping. This will do the right thing provided
888 // the assembler is careful to only convert a relocation in a merged
889 // section to a section symbol if there is a zero addend. If the
890 // assembler does not do this, then in general we can't know what to
891 // do, because we can't distinguish the addend for the instruction
892 // format from the addend for the section offset.
894 if (is_section_symbol)
895 return os->output_address(this, shndx, value + addend);
897 return addend + os->output_address(this, shndx, value);
900 // Write out the local symbols.
902 template<int size, bool big_endian>
904 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
905 const Stringpool* sympool)
907 if (parameters->strip_all())
910 gold_assert(this->symtab_shndx_ != -1U);
911 if (this->symtab_shndx_ == 0)
913 // This object has no symbols. Weird but legal.
917 // Read the symbol table section header.
918 const unsigned int symtab_shndx = this->symtab_shndx_;
919 typename This::Shdr symtabshdr(this,
920 this->elf_file_.section_header(symtab_shndx));
921 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
922 const unsigned int loccount = this->local_symbol_count_;
923 gold_assert(loccount == symtabshdr.get_sh_info());
925 // Read the local symbols.
926 const int sym_size = This::sym_size;
927 off_t locsize = loccount * sym_size;
928 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
931 // Read the symbol names.
932 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
934 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
937 const char* pnames = reinterpret_cast<const char*>(pnamesu);
939 // Get a view into the output file.
940 off_t output_size = this->output_local_symbol_count_ * sym_size;
941 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
944 const std::vector<Map_to_output>& mo(this->map_to_output());
946 gold_assert(this->local_values_.size() == loccount);
948 unsigned char* ov = oview;
950 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
952 elfcpp::Sym<size, big_endian> isym(psyms);
954 if (!this->local_values_[i].needs_output_symtab_entry())
957 unsigned int st_shndx = isym.get_st_shndx();
958 if (st_shndx < elfcpp::SHN_LORESERVE)
960 gold_assert(st_shndx < mo.size());
961 if (mo[st_shndx].output_section == NULL)
963 st_shndx = mo[st_shndx].output_section->out_shndx();
966 elfcpp::Sym_write<size, big_endian> osym(ov);
968 gold_assert(isym.get_st_name() < strtab_size);
969 const char* name = pnames + isym.get_st_name();
970 osym.put_st_name(sympool->get_offset(name));
971 osym.put_st_value(this->local_values_[i].value(this, 0));
972 osym.put_st_size(isym.get_st_size());
973 osym.put_st_info(isym.get_st_info());
974 osym.put_st_other(isym.get_st_other());
975 osym.put_st_shndx(st_shndx);
980 gold_assert(ov - oview == output_size);
982 of->write_output_view(this->local_symbol_offset_, output_size, oview);
985 // Set *INFO to symbolic information about the offset OFFSET in the
986 // section SHNDX. Return true if we found something, false if we
989 template<int size, bool big_endian>
991 Sized_relobj<size, big_endian>::get_symbol_location_info(
994 Symbol_location_info* info)
996 if (this->symtab_shndx_ == 0)
1000 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1004 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1006 const unsigned char* symbol_names_u =
1007 this->section_contents(symbol_names_shndx, &names_size, false);
1008 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1010 const int sym_size = This::sym_size;
1011 const size_t count = symbols_size / sym_size;
1013 const unsigned char* p = symbols;
1014 for (size_t i = 0; i < count; ++i, p += sym_size)
1016 elfcpp::Sym<size, big_endian> sym(p);
1018 if (sym.get_st_type() == elfcpp::STT_FILE)
1020 if (sym.get_st_name() >= names_size)
1021 info->source_file = "(invalid)";
1023 info->source_file = symbol_names + sym.get_st_name();
1025 else if (sym.get_st_shndx() == shndx
1026 && static_cast<off_t>(sym.get_st_value()) <= offset
1027 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1030 if (sym.get_st_name() > names_size)
1031 info->enclosing_symbol_name = "(invalid)";
1033 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1041 // Input_objects methods.
1043 // Add a regular relocatable object to the list. Return false if this
1044 // object should be ignored.
1047 Input_objects::add_object(Object* obj)
1049 Target* target = obj->target();
1050 if (this->target_ == NULL)
1051 this->target_ = target;
1052 else if (this->target_ != target)
1054 gold_error(_("%s: incompatible target"), obj->name().c_str());
1058 if (!obj->is_dynamic())
1059 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1062 // See if this is a duplicate SONAME.
1063 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1065 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1066 this->sonames_.insert(dynobj->soname());
1069 // We have already seen a dynamic object with this soname.
1073 this->dynobj_list_.push_back(dynobj);
1076 set_parameters_size_and_endianness(target->get_size(),
1077 target->is_big_endian());
1082 // Relocate_info methods.
1084 // Return a string describing the location of a relocation. This is
1085 // only used in error messages.
1087 template<int size, bool big_endian>
1089 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1091 // See if we can get line-number information from debugging sections.
1092 std::string filename;
1093 std::string file_and_lineno; // Better than filename-only, if available.
1094 for (unsigned int shndx = 0; shndx < this->object->shnum(); ++shndx)
1095 if (this->object->section_name(shndx) == ".debug_line")
1097 off_t debuglines_size;
1098 const unsigned char* debuglines = this->object->section_contents(
1099 shndx, &debuglines_size, false);
1102 Dwarf_line_info<size, big_endian> line_info(debuglines,
1104 line_info.read_line_mappings();
1105 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1110 std::string ret(this->object->name());
1112 Symbol_location_info info;
1113 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1115 ret += " in function ";
1116 // We could demangle this name before printing, but we don't
1117 // bother because gcc runs linker output through a demangle
1118 // filter itself. The only advantage to demangling here is if
1119 // someone might call ld directly, rather than via gcc. If we
1120 // did want to demangle, cplus_demangle() is in libiberty.
1121 ret += info.enclosing_symbol_name;
1123 filename = info.source_file;
1126 if (!file_and_lineno.empty())
1127 ret += file_and_lineno;
1130 if (!filename.empty())
1133 ret += this->object->section_name(this->data_shndx);
1135 // Offsets into sections have to be positive.
1136 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1143 } // End namespace gold.
1148 using namespace gold;
1150 // Read an ELF file with the header and return the appropriate
1151 // instance of Object.
1153 template<int size, bool big_endian>
1155 make_elf_sized_object(const std::string& name, Input_file* input_file,
1156 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1158 int et = ehdr.get_e_type();
1159 if (et == elfcpp::ET_REL)
1161 Sized_relobj<size, big_endian>* obj =
1162 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1166 else if (et == elfcpp::ET_DYN)
1168 Sized_dynobj<size, big_endian>* obj =
1169 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1175 gold_error(_("%s: unsupported ELF file type %d"),
1181 } // End anonymous namespace.
1186 // Read an ELF file and return the appropriate instance of Object.
1189 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1190 const unsigned char* p, off_t bytes)
1192 if (bytes < elfcpp::EI_NIDENT)
1194 gold_error(_("%s: ELF file too short"), name.c_str());
1198 int v = p[elfcpp::EI_VERSION];
1199 if (v != elfcpp::EV_CURRENT)
1201 if (v == elfcpp::EV_NONE)
1202 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1204 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1208 int c = p[elfcpp::EI_CLASS];
1209 if (c == elfcpp::ELFCLASSNONE)
1211 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1214 else if (c != elfcpp::ELFCLASS32
1215 && c != elfcpp::ELFCLASS64)
1217 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1221 int d = p[elfcpp::EI_DATA];
1222 if (d == elfcpp::ELFDATANONE)
1224 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1227 else if (d != elfcpp::ELFDATA2LSB
1228 && d != elfcpp::ELFDATA2MSB)
1230 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1234 bool big_endian = d == elfcpp::ELFDATA2MSB;
1236 if (c == elfcpp::ELFCLASS32)
1238 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1240 gold_error(_("%s: ELF file too short"), name.c_str());
1245 #ifdef HAVE_TARGET_32_BIG
1246 elfcpp::Ehdr<32, true> ehdr(p);
1247 return make_elf_sized_object<32, true>(name, input_file,
1250 gold_error(_("%s: not configured to support "
1251 "32-bit big-endian object"),
1258 #ifdef HAVE_TARGET_32_LITTLE
1259 elfcpp::Ehdr<32, false> ehdr(p);
1260 return make_elf_sized_object<32, false>(name, input_file,
1263 gold_error(_("%s: not configured to support "
1264 "32-bit little-endian object"),
1272 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1274 gold_error(_("%s: ELF file too short"), name.c_str());
1279 #ifdef HAVE_TARGET_64_BIG
1280 elfcpp::Ehdr<64, true> ehdr(p);
1281 return make_elf_sized_object<64, true>(name, input_file,
1284 gold_error(_("%s: not configured to support "
1285 "64-bit big-endian object"),
1292 #ifdef HAVE_TARGET_64_LITTLE
1293 elfcpp::Ehdr<64, false> ehdr(p);
1294 return make_elf_sized_object<64, false>(name, input_file,
1297 gold_error(_("%s: not configured to support "
1298 "64-bit little-endian object"),
1306 // Instantiate the templates we need. We could use the configure
1307 // script to restrict this to only the ones for implemented targets.
1309 #ifdef HAVE_TARGET_32_LITTLE
1311 class Sized_relobj<32, false>;
1314 #ifdef HAVE_TARGET_32_BIG
1316 class Sized_relobj<32, true>;
1319 #ifdef HAVE_TARGET_64_LITTLE
1321 class Sized_relobj<64, false>;
1324 #ifdef HAVE_TARGET_64_BIG
1326 class Sized_relobj<64, true>;
1329 #ifdef HAVE_TARGET_32_LITTLE
1331 struct Relocate_info<32, false>;
1334 #ifdef HAVE_TARGET_32_BIG
1336 struct Relocate_info<32, true>;
1339 #ifdef HAVE_TARGET_64_LITTLE
1341 struct Relocate_info<64, false>;
1344 #ifdef HAVE_TARGET_64_BIG
1346 struct Relocate_info<64, true>;
1349 } // End namespace gold.