1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "libiberty.h"
33 #include "target-select.h"
34 #include "dwarf_reader.h"
43 #include "compressed_output.h"
44 #include "incremental.h"
49 // Struct Read_symbols_data.
51 // Destroy any remaining File_view objects.
53 Read_symbols_data::~Read_symbols_data()
55 if (this->section_headers != NULL)
56 delete this->section_headers;
57 if (this->section_names != NULL)
58 delete this->section_names;
59 if (this->symbols != NULL)
61 if (this->symbol_names != NULL)
62 delete this->symbol_names;
63 if (this->versym != NULL)
65 if (this->verdef != NULL)
67 if (this->verneed != NULL)
73 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
74 // section and read it in. SYMTAB_SHNDX is the index of the symbol
75 // table we care about.
77 template<int size, bool big_endian>
79 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
81 if (!this->symtab_xindex_.empty())
84 gold_assert(symtab_shndx != 0);
86 // Look through the sections in reverse order, on the theory that it
87 // is more likely to be near the end than the beginning.
88 unsigned int i = object->shnum();
92 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
93 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
95 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
100 object->error(_("missing SHT_SYMTAB_SHNDX section"));
103 // Read in the symtab_xindex_ array, given the section index of the
104 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
107 template<int size, bool big_endian>
109 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
110 const unsigned char* pshdrs)
112 section_size_type bytecount;
113 const unsigned char* contents;
115 contents = object->section_contents(xindex_shndx, &bytecount, false);
118 const unsigned char* p = (pshdrs
120 * elfcpp::Elf_sizes<size>::shdr_size));
121 typename elfcpp::Shdr<size, big_endian> shdr(p);
122 bytecount = convert_to_section_size_type(shdr.get_sh_size());
123 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
126 gold_assert(this->symtab_xindex_.empty());
127 this->symtab_xindex_.reserve(bytecount / 4);
128 for (section_size_type i = 0; i < bytecount; i += 4)
130 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
131 // We preadjust the section indexes we save.
132 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
136 // Symbol symndx has a section of SHN_XINDEX; return the real section
140 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
142 if (symndx >= this->symtab_xindex_.size())
144 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
146 return elfcpp::SHN_UNDEF;
148 unsigned int shndx = this->symtab_xindex_[symndx];
149 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
151 object->error(_("extended index for symbol %u out of range: %u"),
153 return elfcpp::SHN_UNDEF;
160 // Report an error for this object file. This is used by the
161 // elfcpp::Elf_file interface, and also called by the Object code
165 Object::error(const char* format, ...) const
168 va_start(args, format);
170 if (vasprintf(&buf, format, args) < 0)
173 gold_error(_("%s: %s"), this->name().c_str(), buf);
177 // Return a view of the contents of a section.
180 Object::section_contents(unsigned int shndx, section_size_type* plen,
182 { return this->do_section_contents(shndx, plen, cache); }
184 // Read the section data into SD. This is code common to Sized_relobj_file
185 // and Sized_dynobj, so we put it into Object.
187 template<int size, bool big_endian>
189 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
190 Read_symbols_data* sd)
192 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
194 // Read the section headers.
195 const off_t shoff = elf_file->shoff();
196 const unsigned int shnum = this->shnum();
197 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
200 // Read the section names.
201 const unsigned char* pshdrs = sd->section_headers->data();
202 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
203 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
205 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
206 this->error(_("section name section has wrong type: %u"),
207 static_cast<unsigned int>(shdrnames.get_sh_type()));
209 sd->section_names_size =
210 convert_to_section_size_type(shdrnames.get_sh_size());
211 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
212 sd->section_names_size, false,
216 // If NAME is the name of a special .gnu.warning section, arrange for
217 // the warning to be issued. SHNDX is the section index. Return
218 // whether it is a warning section.
221 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
222 Symbol_table* symtab)
224 const char warn_prefix[] = ".gnu.warning.";
225 const int warn_prefix_len = sizeof warn_prefix - 1;
226 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
228 // Read the section contents to get the warning text. It would
229 // be nicer if we only did this if we have to actually issue a
230 // warning. Unfortunately, warnings are issued as we relocate
231 // sections. That means that we can not lock the object then,
232 // as we might try to issue the same warning multiple times
234 section_size_type len;
235 const unsigned char* contents = this->section_contents(shndx, &len,
239 const char* warning = name + warn_prefix_len;
240 contents = reinterpret_cast<const unsigned char*>(warning);
241 len = strlen(warning);
243 std::string warning(reinterpret_cast<const char*>(contents), len);
244 symtab->add_warning(name + warn_prefix_len, this, warning);
250 // If NAME is the name of the special section which indicates that
251 // this object was compiled with -fsplit-stack, mark it accordingly.
254 Object::handle_split_stack_section(const char* name)
256 if (strcmp(name, ".note.GNU-split-stack") == 0)
258 this->uses_split_stack_ = true;
261 if (strcmp(name, ".note.GNU-no-split-stack") == 0)
263 this->has_no_split_stack_ = true;
271 // To copy the symbols data read from the file to a local data structure.
272 // This function is called from do_layout only while doing garbage
276 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
277 unsigned int section_header_size)
279 gc_sd->section_headers_data =
280 new unsigned char[(section_header_size)];
281 memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
282 section_header_size);
283 gc_sd->section_names_data =
284 new unsigned char[sd->section_names_size];
285 memcpy(gc_sd->section_names_data, sd->section_names->data(),
286 sd->section_names_size);
287 gc_sd->section_names_size = sd->section_names_size;
288 if (sd->symbols != NULL)
290 gc_sd->symbols_data =
291 new unsigned char[sd->symbols_size];
292 memcpy(gc_sd->symbols_data, sd->symbols->data(),
297 gc_sd->symbols_data = NULL;
299 gc_sd->symbols_size = sd->symbols_size;
300 gc_sd->external_symbols_offset = sd->external_symbols_offset;
301 if (sd->symbol_names != NULL)
303 gc_sd->symbol_names_data =
304 new unsigned char[sd->symbol_names_size];
305 memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
306 sd->symbol_names_size);
310 gc_sd->symbol_names_data = NULL;
312 gc_sd->symbol_names_size = sd->symbol_names_size;
315 // This function determines if a particular section name must be included
316 // in the link. This is used during garbage collection to determine the
317 // roots of the worklist.
320 Relobj::is_section_name_included(const char* name)
322 if (is_prefix_of(".ctors", name)
323 || is_prefix_of(".dtors", name)
324 || is_prefix_of(".note", name)
325 || is_prefix_of(".init", name)
326 || is_prefix_of(".fini", name)
327 || is_prefix_of(".gcc_except_table", name)
328 || is_prefix_of(".jcr", name)
329 || is_prefix_of(".preinit_array", name)
330 || (is_prefix_of(".text", name)
331 && strstr(name, "personality"))
332 || (is_prefix_of(".data", name)
333 && strstr(name, "personality"))
334 || (is_prefix_of(".gnu.linkonce.d", name)
335 && strstr(name, "personality")))
342 // Finalize the incremental relocation information. Allocates a block
343 // of relocation entries for each symbol, and sets the reloc_bases_
344 // array to point to the first entry in each block. If CLEAR_COUNTS
345 // is TRUE, also clear the per-symbol relocation counters.
348 Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts)
350 unsigned int nsyms = this->get_global_symbols()->size();
351 this->reloc_bases_ = new unsigned int[nsyms];
353 gold_assert(this->reloc_bases_ != NULL);
354 gold_assert(layout->incremental_inputs() != NULL);
356 unsigned int rindex = layout->incremental_inputs()->get_reloc_count();
357 for (unsigned int i = 0; i < nsyms; ++i)
359 this->reloc_bases_[i] = rindex;
360 rindex += this->reloc_counts_[i];
362 this->reloc_counts_[i] = 0;
364 layout->incremental_inputs()->set_reloc_count(rindex);
367 // Class Sized_relobj.
369 // Iterate over local symbols, calling a visitor class V for each GOT offset
370 // associated with a local symbol.
372 template<int size, bool big_endian>
374 Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
375 Got_offset_list::Visitor* v) const
377 unsigned int nsyms = this->local_symbol_count();
378 for (unsigned int i = 0; i < nsyms; i++)
380 Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i);
381 if (p != this->local_got_offsets_.end())
383 const Got_offset_list* got_offsets = p->second;
384 got_offsets->for_all_got_offsets(v);
389 // Class Sized_relobj_file.
391 template<int size, bool big_endian>
392 Sized_relobj_file<size, big_endian>::Sized_relobj_file(
393 const std::string& name,
394 Input_file* input_file,
396 const elfcpp::Ehdr<size, big_endian>& ehdr)
397 : Sized_relobj<size, big_endian>(name, input_file, offset),
398 elf_file_(this, ehdr),
400 local_symbol_count_(0),
401 output_local_symbol_count_(0),
402 output_local_dynsym_count_(0),
405 local_symbol_offset_(0),
406 local_dynsym_offset_(0),
408 local_plt_offsets_(),
409 kept_comdat_sections_(),
410 has_eh_frame_(false),
411 discarded_eh_frame_shndx_(-1U),
413 deferred_layout_relocs_(),
414 compressed_sections_()
416 this->e_type_ = ehdr.get_e_type();
419 template<int size, bool big_endian>
420 Sized_relobj_file<size, big_endian>::~Sized_relobj_file()
424 // Set up an object file based on the file header. This sets up the
425 // section information.
427 template<int size, bool big_endian>
429 Sized_relobj_file<size, big_endian>::do_setup()
431 const unsigned int shnum = this->elf_file_.shnum();
432 this->set_shnum(shnum);
435 // Find the SHT_SYMTAB section, given the section headers. The ELF
436 // standard says that maybe in the future there can be more than one
437 // SHT_SYMTAB section. Until somebody figures out how that could
438 // work, we assume there is only one.
440 template<int size, bool big_endian>
442 Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs)
444 const unsigned int shnum = this->shnum();
445 this->symtab_shndx_ = 0;
448 // Look through the sections in reverse order, since gas tends
449 // to put the symbol table at the end.
450 const unsigned char* p = pshdrs + shnum * This::shdr_size;
451 unsigned int i = shnum;
452 unsigned int xindex_shndx = 0;
453 unsigned int xindex_link = 0;
457 p -= This::shdr_size;
458 typename This::Shdr shdr(p);
459 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
461 this->symtab_shndx_ = i;
462 if (xindex_shndx > 0 && xindex_link == i)
465 new Xindex(this->elf_file_.large_shndx_offset());
466 xindex->read_symtab_xindex<size, big_endian>(this,
469 this->set_xindex(xindex);
474 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
475 // one. This will work if it follows the SHT_SYMTAB
477 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
480 xindex_link = this->adjust_shndx(shdr.get_sh_link());
486 // Return the Xindex structure to use for object with lots of
489 template<int size, bool big_endian>
491 Sized_relobj_file<size, big_endian>::do_initialize_xindex()
493 gold_assert(this->symtab_shndx_ != -1U);
494 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
495 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
499 // Return whether SHDR has the right type and flags to be a GNU
500 // .eh_frame section.
502 template<int size, bool big_endian>
504 Sized_relobj_file<size, big_endian>::check_eh_frame_flags(
505 const elfcpp::Shdr<size, big_endian>* shdr) const
507 elfcpp::Elf_Word sh_type = shdr->get_sh_type();
508 return ((sh_type == elfcpp::SHT_PROGBITS
509 || sh_type == elfcpp::SHT_X86_64_UNWIND)
510 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
513 // Find the section header with the given name.
515 template<int size, bool big_endian>
517 Sized_relobj_file<size, big_endian>::find_shdr(
518 const unsigned char* pshdrs,
521 section_size_type names_size,
522 const unsigned char* hdr) const
524 const unsigned int shnum = this->shnum();
525 const unsigned char* hdr_end = pshdrs + This::shdr_size * shnum;
532 // We found HDR last time we were called, continue looking.
533 typename This::Shdr shdr(hdr);
534 sh_name = shdr.get_sh_name();
538 // Look for the next occurrence of NAME in NAMES.
539 // The fact that .shstrtab produced by current GNU tools is
540 // string merged means we shouldn't have both .not.foo and
541 // .foo in .shstrtab, and multiple .foo sections should all
542 // have the same sh_name. However, this is not guaranteed
543 // by the ELF spec and not all ELF object file producers may
545 size_t len = strlen(name) + 1;
546 const char *p = sh_name ? names + sh_name + len : names;
547 p = reinterpret_cast<const char*>(memmem(p, names_size - (p - names),
557 hdr += This::shdr_size;
558 while (hdr < hdr_end)
560 typename This::Shdr shdr(hdr);
561 if (shdr.get_sh_name() == sh_name)
563 hdr += This::shdr_size;
571 // Return whether there is a GNU .eh_frame section, given the section
572 // headers and the section names.
574 template<int size, bool big_endian>
576 Sized_relobj_file<size, big_endian>::find_eh_frame(
577 const unsigned char* pshdrs,
579 section_size_type names_size) const
581 const unsigned char* s = NULL;
585 s = this->find_shdr(pshdrs, ".eh_frame", names, names_size, s);
589 typename This::Shdr shdr(s);
590 if (this->check_eh_frame_flags(&shdr))
595 // Return TRUE if this is a section whose contents will be needed in the
596 // Add_symbols task. This function is only called for sections that have
597 // already passed the test in is_compressed_debug_section(), so we know
598 // that the section name begins with ".zdebug".
601 need_decompressed_section(const char* name)
603 // Skip over the ".zdebug" and a quick check for the "_".
608 #ifdef ENABLE_THREADS
609 // Decompressing these sections now will help only if we're
611 if (parameters->options().threads())
613 // We will need .zdebug_str if this is not an incremental link
614 // (i.e., we are processing string merge sections) or if we need
615 // to build a gdb index.
616 if ((!parameters->incremental() || parameters->options().gdb_index())
617 && strcmp(name, "str") == 0)
620 // We will need these other sections when building a gdb index.
621 if (parameters->options().gdb_index()
622 && (strcmp(name, "info") == 0
623 || strcmp(name, "types") == 0
624 || strcmp(name, "pubnames") == 0
625 || strcmp(name, "pubtypes") == 0
626 || strcmp(name, "ranges") == 0
627 || strcmp(name, "abbrev") == 0))
632 // Even when single-threaded, we will need .zdebug_str if this is
633 // not an incremental link and we are building a gdb index.
634 // Otherwise, we would decompress the section twice: once for
635 // string merge processing, and once for building the gdb index.
636 if (!parameters->incremental()
637 && parameters->options().gdb_index()
638 && strcmp(name, "str") == 0)
644 // Build a table for any compressed debug sections, mapping each section index
645 // to the uncompressed size and (if needed) the decompressed contents.
647 template<int size, bool big_endian>
648 Compressed_section_map*
649 build_compressed_section_map(
650 const unsigned char* pshdrs,
653 section_size_type names_size,
654 Sized_relobj_file<size, big_endian>* obj)
656 Compressed_section_map* uncompressed_map = new Compressed_section_map();
657 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
658 const unsigned char* p = pshdrs + shdr_size;
660 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
662 typename elfcpp::Shdr<size, big_endian> shdr(p);
663 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
664 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
666 if (shdr.get_sh_name() >= names_size)
668 obj->error(_("bad section name offset for section %u: %lu"),
669 i, static_cast<unsigned long>(shdr.get_sh_name()));
673 const char* name = names + shdr.get_sh_name();
674 if (is_compressed_debug_section(name))
676 section_size_type len;
677 const unsigned char* contents =
678 obj->section_contents(i, &len, false);
679 uint64_t uncompressed_size = get_uncompressed_size(contents, len);
680 Compressed_section_info info;
681 info.size = convert_to_section_size_type(uncompressed_size);
682 info.contents = NULL;
683 if (uncompressed_size != -1ULL)
685 unsigned char* uncompressed_data = NULL;
686 if (need_decompressed_section(name))
688 uncompressed_data = new unsigned char[uncompressed_size];
689 if (decompress_input_section(contents, len,
692 info.contents = uncompressed_data;
694 delete[] uncompressed_data;
696 (*uncompressed_map)[i] = info;
701 return uncompressed_map;
704 // Stash away info for a number of special sections.
705 // Return true if any of the sections found require local symbols to be read.
707 template<int size, bool big_endian>
709 Sized_relobj_file<size, big_endian>::do_find_special_sections(
710 Read_symbols_data* sd)
712 const unsigned char* const pshdrs = sd->section_headers->data();
713 const unsigned char* namesu = sd->section_names->data();
714 const char* names = reinterpret_cast<const char*>(namesu);
716 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
717 this->has_eh_frame_ = true;
719 if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL)
720 this->compressed_sections_
721 = build_compressed_section_map(pshdrs, this->shnum(), names,
722 sd->section_names_size, this);
723 return (this->has_eh_frame_
724 || (!parameters->options().relocatable()
725 && parameters->options().gdb_index()
726 && (memmem(names, sd->section_names_size, "debug_info", 12) == 0
727 || memmem(names, sd->section_names_size, "debug_types",
731 // Read the sections and symbols from an object file.
733 template<int size, bool big_endian>
735 Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
737 this->read_section_data(&this->elf_file_, sd);
739 const unsigned char* const pshdrs = sd->section_headers->data();
741 this->find_symtab(pshdrs);
743 bool need_local_symbols = this->do_find_special_sections(sd);
746 sd->symbols_size = 0;
747 sd->external_symbols_offset = 0;
748 sd->symbol_names = NULL;
749 sd->symbol_names_size = 0;
751 if (this->symtab_shndx_ == 0)
753 // No symbol table. Weird but legal.
757 // Get the symbol table section header.
758 typename This::Shdr symtabshdr(pshdrs
759 + this->symtab_shndx_ * This::shdr_size);
760 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
762 // If this object has a .eh_frame section, or if building a .gdb_index
763 // section and there is debug info, we need all the symbols.
764 // Otherwise we only need the external symbols. While it would be
765 // simpler to just always read all the symbols, I've seen object
766 // files with well over 2000 local symbols, which for a 64-bit
767 // object file format is over 5 pages that we don't need to read
770 const int sym_size = This::sym_size;
771 const unsigned int loccount = symtabshdr.get_sh_info();
772 this->local_symbol_count_ = loccount;
773 this->local_values_.resize(loccount);
774 section_offset_type locsize = loccount * sym_size;
775 off_t dataoff = symtabshdr.get_sh_offset();
776 section_size_type datasize =
777 convert_to_section_size_type(symtabshdr.get_sh_size());
778 off_t extoff = dataoff + locsize;
779 section_size_type extsize = datasize - locsize;
781 off_t readoff = need_local_symbols ? dataoff : extoff;
782 section_size_type readsize = need_local_symbols ? datasize : extsize;
786 // No external symbols. Also weird but also legal.
790 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
792 // Read the section header for the symbol names.
793 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
794 if (strtab_shndx >= this->shnum())
796 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
799 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
800 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
802 this->error(_("symbol table name section has wrong type: %u"),
803 static_cast<unsigned int>(strtabshdr.get_sh_type()));
807 // Read the symbol names.
808 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
809 strtabshdr.get_sh_size(),
812 sd->symbols = fvsymtab;
813 sd->symbols_size = readsize;
814 sd->external_symbols_offset = need_local_symbols ? locsize : 0;
815 sd->symbol_names = fvstrtab;
816 sd->symbol_names_size =
817 convert_to_section_size_type(strtabshdr.get_sh_size());
820 // Return the section index of symbol SYM. Set *VALUE to its value in
821 // the object file. Set *IS_ORDINARY if this is an ordinary section
822 // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
823 // Note that for a symbol which is not defined in this object file,
824 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
825 // the final value of the symbol in the link.
827 template<int size, bool big_endian>
829 Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym,
833 section_size_type symbols_size;
834 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
838 const size_t count = symbols_size / This::sym_size;
839 gold_assert(sym < count);
841 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
842 *value = elfsym.get_st_value();
844 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
847 // Return whether to include a section group in the link. LAYOUT is
848 // used to keep track of which section groups we have already seen.
849 // INDEX is the index of the section group and SHDR is the section
850 // header. If we do not want to include this group, we set bits in
851 // OMIT for each section which should be discarded.
853 template<int size, bool big_endian>
855 Sized_relobj_file<size, big_endian>::include_section_group(
856 Symbol_table* symtab,
860 const unsigned char* shdrs,
861 const char* section_names,
862 section_size_type section_names_size,
863 std::vector<bool>* omit)
865 // Read the section contents.
866 typename This::Shdr shdr(shdrs + index * This::shdr_size);
867 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
868 shdr.get_sh_size(), true, false);
869 const elfcpp::Elf_Word* pword =
870 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
872 // The first word contains flags. We only care about COMDAT section
873 // groups. Other section groups are always included in the link
874 // just like ordinary sections.
875 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
877 // Look up the group signature, which is the name of a symbol. ELF
878 // uses a symbol name because some group signatures are long, and
879 // the name is generally already in the symbol table, so it makes
880 // sense to put the long string just once in .strtab rather than in
881 // both .strtab and .shstrtab.
883 // Get the appropriate symbol table header (this will normally be
884 // the single SHT_SYMTAB section, but in principle it need not be).
885 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
886 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
888 // Read the symbol table entry.
889 unsigned int symndx = shdr.get_sh_info();
890 if (symndx >= symshdr.get_sh_size() / This::sym_size)
892 this->error(_("section group %u info %u out of range"),
896 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
897 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
899 elfcpp::Sym<size, big_endian> sym(psym);
901 // Read the symbol table names.
902 section_size_type symnamelen;
903 const unsigned char* psymnamesu;
904 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
906 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
908 // Get the section group signature.
909 if (sym.get_st_name() >= symnamelen)
911 this->error(_("symbol %u name offset %u out of range"),
912 symndx, sym.get_st_name());
916 std::string signature(psymnames + sym.get_st_name());
918 // It seems that some versions of gas will create a section group
919 // associated with a section symbol, and then fail to give a name to
920 // the section symbol. In such a case, use the name of the section.
921 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
924 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
927 if (!is_ordinary || sym_shndx >= this->shnum())
929 this->error(_("symbol %u invalid section index %u"),
933 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
934 if (member_shdr.get_sh_name() < section_names_size)
935 signature = section_names + member_shdr.get_sh_name();
938 // Record this section group in the layout, and see whether we've already
939 // seen one with the same signature.
942 Kept_section* kept_section = NULL;
944 if ((flags & elfcpp::GRP_COMDAT) == 0)
946 include_group = true;
951 include_group = layout->find_or_add_kept_section(signature,
953 true, &kept_section);
957 if (is_comdat && include_group)
959 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
960 if (incremental_inputs != NULL)
961 incremental_inputs->report_comdat_group(this, signature.c_str());
964 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
966 std::vector<unsigned int> shndxes;
967 bool relocate_group = include_group && parameters->options().relocatable();
969 shndxes.reserve(count - 1);
971 for (size_t i = 1; i < count; ++i)
973 elfcpp::Elf_Word shndx =
974 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
977 shndxes.push_back(shndx);
979 if (shndx >= this->shnum())
981 this->error(_("section %u in section group %u out of range"),
986 // Check for an earlier section number, since we're going to get
987 // it wrong--we may have already decided to include the section.
989 this->error(_("invalid section group %u refers to earlier section %u"),
992 // Get the name of the member section.
993 typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
994 if (member_shdr.get_sh_name() >= section_names_size)
996 // This is an error, but it will be diagnosed eventually
997 // in do_layout, so we don't need to do anything here but
1001 std::string mname(section_names + member_shdr.get_sh_name());
1006 kept_section->add_comdat_section(mname, shndx,
1007 member_shdr.get_sh_size());
1011 (*omit)[shndx] = true;
1015 Relobj* kept_object = kept_section->object();
1016 if (kept_section->is_comdat())
1018 // Find the corresponding kept section, and store
1019 // that info in the discarded section table.
1020 unsigned int kept_shndx;
1022 if (kept_section->find_comdat_section(mname, &kept_shndx,
1025 // We don't keep a mapping for this section if
1026 // it has a different size. The mapping is only
1027 // used for relocation processing, and we don't
1028 // want to treat the sections as similar if the
1029 // sizes are different. Checking the section
1030 // size is the approach used by the GNU linker.
1031 if (kept_size == member_shdr.get_sh_size())
1032 this->set_kept_comdat_section(shndx, kept_object,
1038 // The existing section is a linkonce section. Add
1039 // a mapping if there is exactly one section in the
1040 // group (which is true when COUNT == 2) and if it
1041 // is the same size.
1043 && (kept_section->linkonce_size()
1044 == member_shdr.get_sh_size()))
1045 this->set_kept_comdat_section(shndx, kept_object,
1046 kept_section->shndx());
1053 layout->layout_group(symtab, this, index, name, signature.c_str(),
1054 shdr, flags, &shndxes);
1056 return include_group;
1059 // Whether to include a linkonce section in the link. NAME is the
1060 // name of the section and SHDR is the section header.
1062 // Linkonce sections are a GNU extension implemented in the original
1063 // GNU linker before section groups were defined. The semantics are
1064 // that we only include one linkonce section with a given name. The
1065 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
1066 // where T is the type of section and SYMNAME is the name of a symbol.
1067 // In an attempt to make linkonce sections interact well with section
1068 // groups, we try to identify SYMNAME and use it like a section group
1069 // signature. We want to block section groups with that signature,
1070 // but not other linkonce sections with that signature. We also use
1071 // the full name of the linkonce section as a normal section group
1074 template<int size, bool big_endian>
1076 Sized_relobj_file<size, big_endian>::include_linkonce_section(
1080 const elfcpp::Shdr<size, big_endian>& shdr)
1082 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
1083 // In general the symbol name we want will be the string following
1084 // the last '.'. However, we have to handle the case of
1085 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
1086 // some versions of gcc. So we use a heuristic: if the name starts
1087 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
1088 // we look for the last '.'. We can't always simply skip
1089 // ".gnu.linkonce.X", because we have to deal with cases like
1090 // ".gnu.linkonce.d.rel.ro.local".
1091 const char* const linkonce_t = ".gnu.linkonce.t.";
1092 const char* symname;
1093 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
1094 symname = name + strlen(linkonce_t);
1096 symname = strrchr(name, '.') + 1;
1097 std::string sig1(symname);
1098 std::string sig2(name);
1099 Kept_section* kept1;
1100 Kept_section* kept2;
1101 bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
1103 bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
1108 // We are not including this section because we already saw the
1109 // name of the section as a signature. This normally implies
1110 // that the kept section is another linkonce section. If it is
1111 // the same size, record it as the section which corresponds to
1113 if (kept2->object() != NULL
1114 && !kept2->is_comdat()
1115 && kept2->linkonce_size() == sh_size)
1116 this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
1120 // The section is being discarded on the basis of its symbol
1121 // name. This means that the corresponding kept section was
1122 // part of a comdat group, and it will be difficult to identify
1123 // the specific section within that group that corresponds to
1124 // this linkonce section. We'll handle the simple case where
1125 // the group has only one member section. Otherwise, it's not
1126 // worth the effort.
1127 unsigned int kept_shndx;
1129 if (kept1->object() != NULL
1130 && kept1->is_comdat()
1131 && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
1132 && kept_size == sh_size)
1133 this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
1137 kept1->set_linkonce_size(sh_size);
1138 kept2->set_linkonce_size(sh_size);
1141 return include1 && include2;
1144 // Layout an input section.
1146 template<int size, bool big_endian>
1148 Sized_relobj_file<size, big_endian>::layout_section(
1152 const typename This::Shdr& shdr,
1153 unsigned int reloc_shndx,
1154 unsigned int reloc_type)
1157 Output_section* os = layout->layout(this, shndx, name, shdr,
1158 reloc_shndx, reloc_type, &offset);
1160 this->output_sections()[shndx] = os;
1162 this->section_offsets()[shndx] = invalid_address;
1164 this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
1166 // If this section requires special handling, and if there are
1167 // relocs that apply to it, then we must do the special handling
1168 // before we apply the relocs.
1169 if (offset == -1 && reloc_shndx != 0)
1170 this->set_relocs_must_follow_section_writes();
1173 // Layout an input .eh_frame section.
1175 template<int size, bool big_endian>
1177 Sized_relobj_file<size, big_endian>::layout_eh_frame_section(
1179 const unsigned char* symbols_data,
1180 section_size_type symbols_size,
1181 const unsigned char* symbol_names_data,
1182 section_size_type symbol_names_size,
1184 const typename This::Shdr& shdr,
1185 unsigned int reloc_shndx,
1186 unsigned int reloc_type)
1188 gold_assert(this->has_eh_frame_);
1191 Output_section* os = layout->layout_eh_frame(this,
1201 this->output_sections()[shndx] = os;
1202 if (os == NULL || offset == -1)
1204 // An object can contain at most one section holding exception
1205 // frame information.
1206 gold_assert(this->discarded_eh_frame_shndx_ == -1U);
1207 this->discarded_eh_frame_shndx_ = shndx;
1208 this->section_offsets()[shndx] = invalid_address;
1211 this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
1213 // If this section requires special handling, and if there are
1214 // relocs that aply to it, then we must do the special handling
1215 // before we apply the relocs.
1216 if (os != NULL && offset == -1 && reloc_shndx != 0)
1217 this->set_relocs_must_follow_section_writes();
1220 // Lay out the input sections. We walk through the sections and check
1221 // whether they should be included in the link. If they should, we
1222 // pass them to the Layout object, which will return an output section
1224 // During garbage collection (--gc-sections) and identical code folding
1225 // (--icf), this function is called twice. When it is called the first
1226 // time, it is for setting up some sections as roots to a work-list for
1227 // --gc-sections and to do comdat processing. Actual layout happens the
1228 // second time around after all the relevant sections have been determined.
1229 // The first time, is_worklist_ready or is_icf_ready is false. It is then
1230 // set to true after the garbage collection worklist or identical code
1231 // folding is processed and the relevant sections to be kept are
1232 // determined. Then, this function is called again to layout the sections.
1234 template<int size, bool big_endian>
1236 Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
1238 Read_symbols_data* sd)
1240 const unsigned int shnum = this->shnum();
1241 bool is_gc_pass_one = ((parameters->options().gc_sections()
1242 && !symtab->gc()->is_worklist_ready())
1243 || (parameters->options().icf_enabled()
1244 && !symtab->icf()->is_icf_ready()));
1246 bool is_gc_pass_two = ((parameters->options().gc_sections()
1247 && symtab->gc()->is_worklist_ready())
1248 || (parameters->options().icf_enabled()
1249 && symtab->icf()->is_icf_ready()));
1251 bool is_gc_or_icf = (parameters->options().gc_sections()
1252 || parameters->options().icf_enabled());
1254 // Both is_gc_pass_one and is_gc_pass_two should not be true.
1255 gold_assert(!(is_gc_pass_one && is_gc_pass_two));
1259 Symbols_data* gc_sd = NULL;
1262 // During garbage collection save the symbols data to use it when
1263 // re-entering this function.
1264 gc_sd = new Symbols_data;
1265 this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
1266 this->set_symbols_data(gc_sd);
1268 else if (is_gc_pass_two)
1270 gc_sd = this->get_symbols_data();
1273 const unsigned char* section_headers_data = NULL;
1274 section_size_type section_names_size;
1275 const unsigned char* symbols_data = NULL;
1276 section_size_type symbols_size;
1277 const unsigned char* symbol_names_data = NULL;
1278 section_size_type symbol_names_size;
1282 section_headers_data = gc_sd->section_headers_data;
1283 section_names_size = gc_sd->section_names_size;
1284 symbols_data = gc_sd->symbols_data;
1285 symbols_size = gc_sd->symbols_size;
1286 symbol_names_data = gc_sd->symbol_names_data;
1287 symbol_names_size = gc_sd->symbol_names_size;
1291 section_headers_data = sd->section_headers->data();
1292 section_names_size = sd->section_names_size;
1293 if (sd->symbols != NULL)
1294 symbols_data = sd->symbols->data();
1295 symbols_size = sd->symbols_size;
1296 if (sd->symbol_names != NULL)
1297 symbol_names_data = sd->symbol_names->data();
1298 symbol_names_size = sd->symbol_names_size;
1301 // Get the section headers.
1302 const unsigned char* shdrs = section_headers_data;
1303 const unsigned char* pshdrs;
1305 // Get the section names.
1306 const unsigned char* pnamesu = (is_gc_or_icf)
1307 ? gc_sd->section_names_data
1308 : sd->section_names->data();
1310 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1312 // If any input files have been claimed by plugins, we need to defer
1313 // actual layout until the replacement files have arrived.
1314 const bool should_defer_layout =
1315 (parameters->options().has_plugins()
1316 && parameters->options().plugins()->should_defer_layout());
1317 unsigned int num_sections_to_defer = 0;
1319 // For each section, record the index of the reloc section if any.
1320 // Use 0 to mean that there is no reloc section, -1U to mean that
1321 // there is more than one.
1322 std::vector<unsigned int> reloc_shndx(shnum, 0);
1323 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
1324 // Skip the first, dummy, section.
1325 pshdrs = shdrs + This::shdr_size;
1326 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1328 typename This::Shdr shdr(pshdrs);
1330 // Count the number of sections whose layout will be deferred.
1331 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1332 ++num_sections_to_defer;
1334 unsigned int sh_type = shdr.get_sh_type();
1335 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
1337 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
1338 if (target_shndx == 0 || target_shndx >= shnum)
1340 this->error(_("relocation section %u has bad info %u"),
1345 if (reloc_shndx[target_shndx] != 0)
1346 reloc_shndx[target_shndx] = -1U;
1349 reloc_shndx[target_shndx] = i;
1350 reloc_type[target_shndx] = sh_type;
1355 Output_sections& out_sections(this->output_sections());
1356 std::vector<Address>& out_section_offsets(this->section_offsets());
1358 if (!is_gc_pass_two)
1360 out_sections.resize(shnum);
1361 out_section_offsets.resize(shnum);
1364 // If we are only linking for symbols, then there is nothing else to
1366 if (this->input_file()->just_symbols())
1368 if (!is_gc_pass_two)
1370 delete sd->section_headers;
1371 sd->section_headers = NULL;
1372 delete sd->section_names;
1373 sd->section_names = NULL;
1378 if (num_sections_to_defer > 0)
1380 parameters->options().plugins()->add_deferred_layout_object(this);
1381 this->deferred_layout_.reserve(num_sections_to_defer);
1384 // Whether we've seen a .note.GNU-stack section.
1385 bool seen_gnu_stack = false;
1386 // The flags of a .note.GNU-stack section.
1387 uint64_t gnu_stack_flags = 0;
1389 // Keep track of which sections to omit.
1390 std::vector<bool> omit(shnum, false);
1392 // Keep track of reloc sections when emitting relocations.
1393 const bool relocatable = parameters->options().relocatable();
1394 const bool emit_relocs = (relocatable
1395 || parameters->options().emit_relocs());
1396 std::vector<unsigned int> reloc_sections;
1398 // Keep track of .eh_frame sections.
1399 std::vector<unsigned int> eh_frame_sections;
1401 // Keep track of .debug_info and .debug_types sections.
1402 std::vector<unsigned int> debug_info_sections;
1403 std::vector<unsigned int> debug_types_sections;
1405 // Skip the first, dummy, section.
1406 pshdrs = shdrs + This::shdr_size;
1407 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1409 typename This::Shdr shdr(pshdrs);
1411 if (shdr.get_sh_name() >= section_names_size)
1413 this->error(_("bad section name offset for section %u: %lu"),
1414 i, static_cast<unsigned long>(shdr.get_sh_name()));
1418 const char* name = pnames + shdr.get_sh_name();
1420 if (!is_gc_pass_two)
1422 if (this->handle_gnu_warning_section(name, i, symtab))
1424 if (!relocatable && !parameters->options().shared())
1428 // The .note.GNU-stack section is special. It gives the
1429 // protection flags that this object file requires for the stack
1431 if (strcmp(name, ".note.GNU-stack") == 0)
1433 seen_gnu_stack = true;
1434 gnu_stack_flags |= shdr.get_sh_flags();
1438 // The .note.GNU-split-stack section is also special. It
1439 // indicates that the object was compiled with
1441 if (this->handle_split_stack_section(name))
1443 if (!relocatable && !parameters->options().shared())
1447 // Skip attributes section.
1448 if (parameters->target().is_attributes_section(name))
1453 bool discard = omit[i];
1456 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
1458 if (!this->include_section_group(symtab, layout, i, name,
1464 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
1465 && Layout::is_linkonce(name))
1467 if (!this->include_linkonce_section(layout, i, name, shdr))
1472 // Add the section to the incremental inputs layout.
1473 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1474 if (incremental_inputs != NULL
1476 && can_incremental_update(shdr.get_sh_type()))
1478 off_t sh_size = shdr.get_sh_size();
1479 section_size_type uncompressed_size;
1480 if (this->section_is_compressed(i, &uncompressed_size))
1481 sh_size = uncompressed_size;
1482 incremental_inputs->report_input_section(this, i, name, sh_size);
1487 // Do not include this section in the link.
1488 out_sections[i] = NULL;
1489 out_section_offsets[i] = invalid_address;
1494 if (is_gc_pass_one && parameters->options().gc_sections())
1496 if (this->is_section_name_included(name)
1497 || layout->keep_input_section (this, name)
1498 || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1499 || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
1501 symtab->gc()->worklist().push(Section_id(this, i));
1503 // If the section name XXX can be represented as a C identifier
1504 // it cannot be discarded if there are references to
1505 // __start_XXX and __stop_XXX symbols. These need to be
1506 // specially handled.
1507 if (is_cident(name))
1509 symtab->gc()->add_cident_section(name, Section_id(this, i));
1513 // When doing a relocatable link we are going to copy input
1514 // reloc sections into the output. We only want to copy the
1515 // ones associated with sections which are not being discarded.
1516 // However, we don't know that yet for all sections. So save
1517 // reloc sections and process them later. Garbage collection is
1518 // not triggered when relocatable code is desired.
1520 && (shdr.get_sh_type() == elfcpp::SHT_REL
1521 || shdr.get_sh_type() == elfcpp::SHT_RELA))
1523 reloc_sections.push_back(i);
1527 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
1530 // The .eh_frame section is special. It holds exception frame
1531 // information that we need to read in order to generate the
1532 // exception frame header. We process these after all the other
1533 // sections so that the exception frame reader can reliably
1534 // determine which sections are being discarded, and discard the
1535 // corresponding information.
1537 && strcmp(name, ".eh_frame") == 0
1538 && this->check_eh_frame_flags(&shdr))
1542 out_sections[i] = reinterpret_cast<Output_section*>(1);
1543 out_section_offsets[i] = invalid_address;
1545 else if (should_defer_layout)
1546 this->deferred_layout_.push_back(Deferred_layout(i, name,
1551 eh_frame_sections.push_back(i);
1555 if (is_gc_pass_two && parameters->options().gc_sections())
1557 // This is executed during the second pass of garbage
1558 // collection. do_layout has been called before and some
1559 // sections have been already discarded. Simply ignore
1560 // such sections this time around.
1561 if (out_sections[i] == NULL)
1563 gold_assert(out_section_offsets[i] == invalid_address);
1566 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1567 && symtab->gc()->is_section_garbage(this, i))
1569 if (parameters->options().print_gc_sections())
1570 gold_info(_("%s: removing unused section from '%s'"
1572 program_name, this->section_name(i).c_str(),
1573 this->name().c_str());
1574 out_sections[i] = NULL;
1575 out_section_offsets[i] = invalid_address;
1580 if (is_gc_pass_two && parameters->options().icf_enabled())
1582 if (out_sections[i] == NULL)
1584 gold_assert(out_section_offsets[i] == invalid_address);
1587 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1588 && symtab->icf()->is_section_folded(this, i))
1590 if (parameters->options().print_icf_sections())
1593 symtab->icf()->get_folded_section(this, i);
1594 Relobj* folded_obj =
1595 reinterpret_cast<Relobj*>(folded.first);
1596 gold_info(_("%s: ICF folding section '%s' in file '%s'"
1597 "into '%s' in file '%s'"),
1598 program_name, this->section_name(i).c_str(),
1599 this->name().c_str(),
1600 folded_obj->section_name(folded.second).c_str(),
1601 folded_obj->name().c_str());
1603 out_sections[i] = NULL;
1604 out_section_offsets[i] = invalid_address;
1609 // Defer layout here if input files are claimed by plugins. When gc
1610 // is turned on this function is called twice. For the second call
1611 // should_defer_layout should be false.
1612 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1614 gold_assert(!is_gc_pass_two);
1615 this->deferred_layout_.push_back(Deferred_layout(i, name,
1619 // Put dummy values here; real values will be supplied by
1620 // do_layout_deferred_sections.
1621 out_sections[i] = reinterpret_cast<Output_section*>(2);
1622 out_section_offsets[i] = invalid_address;
1626 // During gc_pass_two if a section that was previously deferred is
1627 // found, do not layout the section as layout_deferred_sections will
1628 // do it later from gold.cc.
1630 && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
1635 // This is during garbage collection. The out_sections are
1636 // assigned in the second call to this function.
1637 out_sections[i] = reinterpret_cast<Output_section*>(1);
1638 out_section_offsets[i] = invalid_address;
1642 // When garbage collection is switched on the actual layout
1643 // only happens in the second call.
1644 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1647 // When generating a .gdb_index section, we do additional
1648 // processing of .debug_info and .debug_types sections after all
1649 // the other sections for the same reason as above.
1651 && parameters->options().gdb_index()
1652 && !(shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1654 if (strcmp(name, ".debug_info") == 0
1655 || strcmp(name, ".zdebug_info") == 0)
1656 debug_info_sections.push_back(i);
1657 else if (strcmp(name, ".debug_types") == 0
1658 || strcmp(name, ".zdebug_types") == 0)
1659 debug_types_sections.push_back(i);
1664 if (!is_gc_pass_two)
1665 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this);
1667 // When doing a relocatable link handle the reloc sections at the
1668 // end. Garbage collection and Identical Code Folding is not
1669 // turned on for relocatable code.
1671 this->size_relocatable_relocs();
1673 gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
1675 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1676 p != reloc_sections.end();
1679 unsigned int i = *p;
1680 const unsigned char* pshdr;
1681 pshdr = section_headers_data + i * This::shdr_size;
1682 typename This::Shdr shdr(pshdr);
1684 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1685 if (data_shndx >= shnum)
1687 // We already warned about this above.
1691 Output_section* data_section = out_sections[data_shndx];
1692 if (data_section == reinterpret_cast<Output_section*>(2))
1694 // The layout for the data section was deferred, so we need
1695 // to defer the relocation section, too.
1696 const char* name = pnames + shdr.get_sh_name();
1697 this->deferred_layout_relocs_.push_back(
1698 Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
1699 out_sections[i] = reinterpret_cast<Output_section*>(2);
1700 out_section_offsets[i] = invalid_address;
1703 if (data_section == NULL)
1705 out_sections[i] = NULL;
1706 out_section_offsets[i] = invalid_address;
1710 Relocatable_relocs* rr = new Relocatable_relocs();
1711 this->set_relocatable_relocs(i, rr);
1713 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1715 out_sections[i] = os;
1716 out_section_offsets[i] = invalid_address;
1719 // Handle the .eh_frame sections at the end.
1720 gold_assert(!is_gc_pass_one || eh_frame_sections.empty());
1721 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1722 p != eh_frame_sections.end();
1725 unsigned int i = *p;
1726 const unsigned char* pshdr;
1727 pshdr = section_headers_data + i * This::shdr_size;
1728 typename This::Shdr shdr(pshdr);
1730 this->layout_eh_frame_section(layout,
1741 // When building a .gdb_index section, scan the .debug_info and
1742 // .debug_types sections.
1743 gold_assert(!is_gc_pass_one
1744 || (debug_info_sections.empty() && debug_types_sections.empty()));
1745 for (std::vector<unsigned int>::const_iterator p
1746 = debug_info_sections.begin();
1747 p != debug_info_sections.end();
1750 unsigned int i = *p;
1751 layout->add_to_gdb_index(false, this, symbols_data, symbols_size,
1752 i, reloc_shndx[i], reloc_type[i]);
1754 for (std::vector<unsigned int>::const_iterator p
1755 = debug_types_sections.begin();
1756 p != debug_types_sections.end();
1759 unsigned int i = *p;
1760 layout->add_to_gdb_index(true, this, symbols_data, symbols_size,
1761 i, reloc_shndx[i], reloc_type[i]);
1766 delete[] gc_sd->section_headers_data;
1767 delete[] gc_sd->section_names_data;
1768 delete[] gc_sd->symbols_data;
1769 delete[] gc_sd->symbol_names_data;
1770 this->set_symbols_data(NULL);
1774 delete sd->section_headers;
1775 sd->section_headers = NULL;
1776 delete sd->section_names;
1777 sd->section_names = NULL;
1781 // Layout sections whose layout was deferred while waiting for
1782 // input files from a plugin.
1784 template<int size, bool big_endian>
1786 Sized_relobj_file<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1788 typename std::vector<Deferred_layout>::iterator deferred;
1790 for (deferred = this->deferred_layout_.begin();
1791 deferred != this->deferred_layout_.end();
1794 typename This::Shdr shdr(deferred->shdr_data_);
1795 // If the section is not included, it is because the garbage collector
1796 // decided it is not needed. Avoid reverting that decision.
1797 if (!this->is_section_included(deferred->shndx_))
1800 if (parameters->options().relocatable()
1801 || deferred->name_ != ".eh_frame"
1802 || !this->check_eh_frame_flags(&shdr))
1803 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1804 shdr, deferred->reloc_shndx_,
1805 deferred->reloc_type_);
1808 // Reading the symbols again here may be slow.
1809 Read_symbols_data sd;
1810 this->read_symbols(&sd);
1811 this->layout_eh_frame_section(layout,
1814 sd.symbol_names->data(),
1815 sd.symbol_names_size,
1818 deferred->reloc_shndx_,
1819 deferred->reloc_type_);
1823 this->deferred_layout_.clear();
1825 // Now handle the deferred relocation sections.
1827 Output_sections& out_sections(this->output_sections());
1828 std::vector<Address>& out_section_offsets(this->section_offsets());
1830 for (deferred = this->deferred_layout_relocs_.begin();
1831 deferred != this->deferred_layout_relocs_.end();
1834 unsigned int shndx = deferred->shndx_;
1835 typename This::Shdr shdr(deferred->shdr_data_);
1836 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1838 Output_section* data_section = out_sections[data_shndx];
1839 if (data_section == NULL)
1841 out_sections[shndx] = NULL;
1842 out_section_offsets[shndx] = invalid_address;
1846 Relocatable_relocs* rr = new Relocatable_relocs();
1847 this->set_relocatable_relocs(shndx, rr);
1849 Output_section* os = layout->layout_reloc(this, shndx, shdr,
1851 out_sections[shndx] = os;
1852 out_section_offsets[shndx] = invalid_address;
1856 // Add the symbols to the symbol table.
1858 template<int size, bool big_endian>
1860 Sized_relobj_file<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1861 Read_symbols_data* sd,
1864 if (sd->symbols == NULL)
1866 gold_assert(sd->symbol_names == NULL);
1870 const int sym_size = This::sym_size;
1871 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1873 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1875 this->error(_("size of symbols is not multiple of symbol size"));
1879 this->symbols_.resize(symcount);
1881 const char* sym_names =
1882 reinterpret_cast<const char*>(sd->symbol_names->data());
1883 symtab->add_from_relobj(this,
1884 sd->symbols->data() + sd->external_symbols_offset,
1885 symcount, this->local_symbol_count_,
1886 sym_names, sd->symbol_names_size,
1888 &this->defined_count_);
1892 delete sd->symbol_names;
1893 sd->symbol_names = NULL;
1896 // Find out if this object, that is a member of a lib group, should be included
1897 // in the link. We check every symbol defined by this object. If the symbol
1898 // table has a strong undefined reference to that symbol, we have to include
1901 template<int size, bool big_endian>
1902 Archive::Should_include
1903 Sized_relobj_file<size, big_endian>::do_should_include_member(
1904 Symbol_table* symtab,
1906 Read_symbols_data* sd,
1909 char* tmpbuf = NULL;
1910 size_t tmpbuflen = 0;
1911 const char* sym_names =
1912 reinterpret_cast<const char*>(sd->symbol_names->data());
1913 const unsigned char* syms =
1914 sd->symbols->data() + sd->external_symbols_offset;
1915 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1916 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1919 const unsigned char* p = syms;
1921 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1923 elfcpp::Sym<size, big_endian> sym(p);
1924 unsigned int st_shndx = sym.get_st_shndx();
1925 if (st_shndx == elfcpp::SHN_UNDEF)
1928 unsigned int st_name = sym.get_st_name();
1929 const char* name = sym_names + st_name;
1931 Archive::Should_include t = Archive::should_include_member(symtab,
1937 if (t == Archive::SHOULD_INCLUDE_YES)
1946 return Archive::SHOULD_INCLUDE_UNKNOWN;
1949 // Iterate over global defined symbols, calling a visitor class V for each.
1951 template<int size, bool big_endian>
1953 Sized_relobj_file<size, big_endian>::do_for_all_global_symbols(
1954 Read_symbols_data* sd,
1955 Library_base::Symbol_visitor_base* v)
1957 const char* sym_names =
1958 reinterpret_cast<const char*>(sd->symbol_names->data());
1959 const unsigned char* syms =
1960 sd->symbols->data() + sd->external_symbols_offset;
1961 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1962 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1964 const unsigned char* p = syms;
1966 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1968 elfcpp::Sym<size, big_endian> sym(p);
1969 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF)
1970 v->visit(sym_names + sym.get_st_name());
1974 // Return whether the local symbol SYMNDX has a PLT offset.
1976 template<int size, bool big_endian>
1978 Sized_relobj_file<size, big_endian>::local_has_plt_offset(
1979 unsigned int symndx) const
1981 typename Local_plt_offsets::const_iterator p =
1982 this->local_plt_offsets_.find(symndx);
1983 return p != this->local_plt_offsets_.end();
1986 // Get the PLT offset of a local symbol.
1988 template<int size, bool big_endian>
1990 Sized_relobj_file<size, big_endian>::do_local_plt_offset(
1991 unsigned int symndx) const
1993 typename Local_plt_offsets::const_iterator p =
1994 this->local_plt_offsets_.find(symndx);
1995 gold_assert(p != this->local_plt_offsets_.end());
1999 // Set the PLT offset of a local symbol.
2001 template<int size, bool big_endian>
2003 Sized_relobj_file<size, big_endian>::set_local_plt_offset(
2004 unsigned int symndx, unsigned int plt_offset)
2006 std::pair<typename Local_plt_offsets::iterator, bool> ins =
2007 this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset));
2008 gold_assert(ins.second);
2011 // First pass over the local symbols. Here we add their names to
2012 // *POOL and *DYNPOOL, and we store the symbol value in
2013 // THIS->LOCAL_VALUES_. This function is always called from a
2014 // singleton thread. This is followed by a call to
2015 // finalize_local_symbols.
2017 template<int size, bool big_endian>
2019 Sized_relobj_file<size, big_endian>::do_count_local_symbols(Stringpool* pool,
2020 Stringpool* dynpool)
2022 gold_assert(this->symtab_shndx_ != -1U);
2023 if (this->symtab_shndx_ == 0)
2025 // This object has no symbols. Weird but legal.
2029 // Read the symbol table section header.
2030 const unsigned int symtab_shndx = this->symtab_shndx_;
2031 typename This::Shdr symtabshdr(this,
2032 this->elf_file_.section_header(symtab_shndx));
2033 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2035 // Read the local symbols.
2036 const int sym_size = This::sym_size;
2037 const unsigned int loccount = this->local_symbol_count_;
2038 gold_assert(loccount == symtabshdr.get_sh_info());
2039 off_t locsize = loccount * sym_size;
2040 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
2041 locsize, true, true);
2043 // Read the symbol names.
2044 const unsigned int strtab_shndx =
2045 this->adjust_shndx(symtabshdr.get_sh_link());
2046 section_size_type strtab_size;
2047 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
2050 const char* pnames = reinterpret_cast<const char*>(pnamesu);
2052 // Loop over the local symbols.
2054 const Output_sections& out_sections(this->output_sections());
2055 unsigned int shnum = this->shnum();
2056 unsigned int count = 0;
2057 unsigned int dyncount = 0;
2058 // Skip the first, dummy, symbol.
2060 bool strip_all = parameters->options().strip_all();
2061 bool discard_all = parameters->options().discard_all();
2062 bool discard_locals = parameters->options().discard_locals();
2063 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2065 elfcpp::Sym<size, big_endian> sym(psyms);
2067 Symbol_value<size>& lv(this->local_values_[i]);
2070 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2072 lv.set_input_shndx(shndx, is_ordinary);
2074 if (sym.get_st_type() == elfcpp::STT_SECTION)
2075 lv.set_is_section_symbol();
2076 else if (sym.get_st_type() == elfcpp::STT_TLS)
2077 lv.set_is_tls_symbol();
2078 else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
2079 lv.set_is_ifunc_symbol();
2081 // Save the input symbol value for use in do_finalize_local_symbols().
2082 lv.set_input_value(sym.get_st_value());
2084 // Decide whether this symbol should go into the output file.
2086 if ((shndx < shnum && out_sections[shndx] == NULL)
2087 || shndx == this->discarded_eh_frame_shndx_)
2089 lv.set_no_output_symtab_entry();
2090 gold_assert(!lv.needs_output_dynsym_entry());
2094 if (sym.get_st_type() == elfcpp::STT_SECTION)
2096 lv.set_no_output_symtab_entry();
2097 gold_assert(!lv.needs_output_dynsym_entry());
2101 if (sym.get_st_name() >= strtab_size)
2103 this->error(_("local symbol %u section name out of range: %u >= %u"),
2104 i, sym.get_st_name(),
2105 static_cast<unsigned int>(strtab_size));
2106 lv.set_no_output_symtab_entry();
2110 const char* name = pnames + sym.get_st_name();
2112 // If needed, add the symbol to the dynamic symbol table string pool.
2113 if (lv.needs_output_dynsym_entry())
2115 dynpool->add(name, true, NULL);
2120 || (discard_all && lv.may_be_discarded_from_output_symtab()))
2122 lv.set_no_output_symtab_entry();
2126 // If --discard-locals option is used, discard all temporary local
2127 // symbols. These symbols start with system-specific local label
2128 // prefixes, typically .L for ELF system. We want to be compatible
2129 // with GNU ld so here we essentially use the same check in
2130 // bfd_is_local_label(). The code is different because we already
2133 // - the symbol is local and thus cannot have global or weak binding.
2134 // - the symbol is not a section symbol.
2135 // - the symbol has a name.
2137 // We do not discard a symbol if it needs a dynamic symbol entry.
2139 && sym.get_st_type() != elfcpp::STT_FILE
2140 && !lv.needs_output_dynsym_entry()
2141 && lv.may_be_discarded_from_output_symtab()
2142 && parameters->target().is_local_label_name(name))
2144 lv.set_no_output_symtab_entry();
2148 // Discard the local symbol if -retain_symbols_file is specified
2149 // and the local symbol is not in that file.
2150 if (!parameters->options().should_retain_symbol(name))
2152 lv.set_no_output_symtab_entry();
2156 // Add the symbol to the symbol table string pool.
2157 pool->add(name, true, NULL);
2161 this->output_local_symbol_count_ = count;
2162 this->output_local_dynsym_count_ = dyncount;
2165 // Compute the final value of a local symbol.
2167 template<int size, bool big_endian>
2168 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
2169 Sized_relobj_file<size, big_endian>::compute_final_local_value_internal(
2171 const Symbol_value<size>* lv_in,
2172 Symbol_value<size>* lv_out,
2174 const Output_sections& out_sections,
2175 const std::vector<Address>& out_offsets,
2176 const Symbol_table* symtab)
2178 // We are going to overwrite *LV_OUT, if it has a merged symbol value,
2179 // we may have a memory leak.
2180 gold_assert(lv_out->has_output_value());
2183 unsigned int shndx = lv_in->input_shndx(&is_ordinary);
2185 // Set the output symbol value.
2189 if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
2190 lv_out->set_output_value(lv_in->input_value());
2193 this->error(_("unknown section index %u for local symbol %u"),
2195 lv_out->set_output_value(0);
2196 return This::CFLV_ERROR;
2201 if (shndx >= this->shnum())
2203 this->error(_("local symbol %u section index %u out of range"),
2205 lv_out->set_output_value(0);
2206 return This::CFLV_ERROR;
2209 Output_section* os = out_sections[shndx];
2210 Address secoffset = out_offsets[shndx];
2211 if (symtab->is_section_folded(this, shndx))
2213 gold_assert(os == NULL && secoffset == invalid_address);
2214 // Get the os of the section it is folded onto.
2215 Section_id folded = symtab->icf()->get_folded_section(this,
2217 gold_assert(folded.first != NULL);
2218 Sized_relobj_file<size, big_endian>* folded_obj = reinterpret_cast
2219 <Sized_relobj_file<size, big_endian>*>(folded.first);
2220 os = folded_obj->output_section(folded.second);
2221 gold_assert(os != NULL);
2222 secoffset = folded_obj->get_output_section_offset(folded.second);
2224 // This could be a relaxed input section.
2225 if (secoffset == invalid_address)
2227 const Output_relaxed_input_section* relaxed_section =
2228 os->find_relaxed_input_section(folded_obj, folded.second);
2229 gold_assert(relaxed_section != NULL);
2230 secoffset = relaxed_section->address() - os->address();
2236 // This local symbol belongs to a section we are discarding.
2237 // In some cases when applying relocations later, we will
2238 // attempt to match it to the corresponding kept section,
2239 // so we leave the input value unchanged here.
2240 return This::CFLV_DISCARDED;
2242 else if (secoffset == invalid_address)
2246 // This is a SHF_MERGE section or one which otherwise
2247 // requires special handling.
2248 if (shndx == this->discarded_eh_frame_shndx_)
2250 // This local symbol belongs to a discarded .eh_frame
2251 // section. Just treat it like the case in which
2252 // os == NULL above.
2253 gold_assert(this->has_eh_frame_);
2254 return This::CFLV_DISCARDED;
2256 else if (!lv_in->is_section_symbol())
2258 // This is not a section symbol. We can determine
2259 // the final value now.
2260 lv_out->set_output_value(
2261 os->output_address(this, shndx, lv_in->input_value()));
2263 else if (!os->find_starting_output_address(this, shndx, &start))
2265 // This is a section symbol, but apparently not one in a
2266 // merged section. First check to see if this is a relaxed
2267 // input section. If so, use its address. Otherwise just
2268 // use the start of the output section. This happens with
2269 // relocatable links when the input object has section
2270 // symbols for arbitrary non-merge sections.
2271 const Output_section_data* posd =
2272 os->find_relaxed_input_section(this, shndx);
2275 Address relocatable_link_adjustment =
2276 relocatable ? os->address() : 0;
2277 lv_out->set_output_value(posd->address()
2278 - relocatable_link_adjustment);
2281 lv_out->set_output_value(os->address());
2285 // We have to consider the addend to determine the
2286 // value to use in a relocation. START is the start
2287 // of this input section. If we are doing a relocatable
2288 // link, use offset from start output section instead of
2290 Address adjusted_start =
2291 relocatable ? start - os->address() : start;
2292 Merged_symbol_value<size>* msv =
2293 new Merged_symbol_value<size>(lv_in->input_value(),
2295 lv_out->set_merged_symbol_value(msv);
2298 else if (lv_in->is_tls_symbol())
2299 lv_out->set_output_value(os->tls_offset()
2301 + lv_in->input_value());
2303 lv_out->set_output_value((relocatable ? 0 : os->address())
2305 + lv_in->input_value());
2307 return This::CFLV_OK;
2310 // Compute final local symbol value. R_SYM is the index of a local
2311 // symbol in symbol table. LV points to a symbol value, which is
2312 // expected to hold the input value and to be over-written by the
2313 // final value. SYMTAB points to a symbol table. Some targets may want
2314 // to know would-be-finalized local symbol values in relaxation.
2315 // Hence we provide this method. Since this method updates *LV, a
2316 // callee should make a copy of the original local symbol value and
2317 // use the copy instead of modifying an object's local symbols before
2318 // everything is finalized. The caller should also free up any allocated
2319 // memory in the return value in *LV.
2320 template<int size, bool big_endian>
2321 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
2322 Sized_relobj_file<size, big_endian>::compute_final_local_value(
2324 const Symbol_value<size>* lv_in,
2325 Symbol_value<size>* lv_out,
2326 const Symbol_table* symtab)
2328 // This is just a wrapper of compute_final_local_value_internal.
2329 const bool relocatable = parameters->options().relocatable();
2330 const Output_sections& out_sections(this->output_sections());
2331 const std::vector<Address>& out_offsets(this->section_offsets());
2332 return this->compute_final_local_value_internal(r_sym, lv_in, lv_out,
2333 relocatable, out_sections,
2334 out_offsets, symtab);
2337 // Finalize the local symbols. Here we set the final value in
2338 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
2339 // This function is always called from a singleton thread. The actual
2340 // output of the local symbols will occur in a separate task.
2342 template<int size, bool big_endian>
2344 Sized_relobj_file<size, big_endian>::do_finalize_local_symbols(
2347 Symbol_table* symtab)
2349 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
2351 const unsigned int loccount = this->local_symbol_count_;
2352 this->local_symbol_offset_ = off;
2354 const bool relocatable = parameters->options().relocatable();
2355 const Output_sections& out_sections(this->output_sections());
2356 const std::vector<Address>& out_offsets(this->section_offsets());
2358 for (unsigned int i = 1; i < loccount; ++i)
2360 Symbol_value<size>* lv = &this->local_values_[i];
2362 Compute_final_local_value_status cflv_status =
2363 this->compute_final_local_value_internal(i, lv, lv, relocatable,
2364 out_sections, out_offsets,
2366 switch (cflv_status)
2369 if (!lv->is_output_symtab_index_set())
2371 lv->set_output_symtab_index(index);
2375 case CFLV_DISCARDED:
2386 // Set the output dynamic symbol table indexes for the local variables.
2388 template<int size, bool big_endian>
2390 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_indexes(
2393 const unsigned int loccount = this->local_symbol_count_;
2394 for (unsigned int i = 1; i < loccount; ++i)
2396 Symbol_value<size>& lv(this->local_values_[i]);
2397 if (lv.needs_output_dynsym_entry())
2399 lv.set_output_dynsym_index(index);
2406 // Set the offset where local dynamic symbol information will be stored.
2407 // Returns the count of local symbols contributed to the symbol table by
2410 template<int size, bool big_endian>
2412 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_offset(off_t off)
2414 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
2415 this->local_dynsym_offset_ = off;
2416 return this->output_local_dynsym_count_;
2419 // If Symbols_data is not NULL get the section flags from here otherwise
2420 // get it from the file.
2422 template<int size, bool big_endian>
2424 Sized_relobj_file<size, big_endian>::do_section_flags(unsigned int shndx)
2426 Symbols_data* sd = this->get_symbols_data();
2429 const unsigned char* pshdrs = sd->section_headers_data
2430 + This::shdr_size * shndx;
2431 typename This::Shdr shdr(pshdrs);
2432 return shdr.get_sh_flags();
2434 // If sd is NULL, read the section header from the file.
2435 return this->elf_file_.section_flags(shndx);
2438 // Get the section's ent size from Symbols_data. Called by get_section_contents
2441 template<int size, bool big_endian>
2443 Sized_relobj_file<size, big_endian>::do_section_entsize(unsigned int shndx)
2445 Symbols_data* sd = this->get_symbols_data();
2446 gold_assert(sd != NULL);
2448 const unsigned char* pshdrs = sd->section_headers_data
2449 + This::shdr_size * shndx;
2450 typename This::Shdr shdr(pshdrs);
2451 return shdr.get_sh_entsize();
2454 // Write out the local symbols.
2456 template<int size, bool big_endian>
2458 Sized_relobj_file<size, big_endian>::write_local_symbols(
2460 const Stringpool* sympool,
2461 const Stringpool* dynpool,
2462 Output_symtab_xindex* symtab_xindex,
2463 Output_symtab_xindex* dynsym_xindex,
2466 const bool strip_all = parameters->options().strip_all();
2469 if (this->output_local_dynsym_count_ == 0)
2471 this->output_local_symbol_count_ = 0;
2474 gold_assert(this->symtab_shndx_ != -1U);
2475 if (this->symtab_shndx_ == 0)
2477 // This object has no symbols. Weird but legal.
2481 // Read the symbol table section header.
2482 const unsigned int symtab_shndx = this->symtab_shndx_;
2483 typename This::Shdr symtabshdr(this,
2484 this->elf_file_.section_header(symtab_shndx));
2485 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2486 const unsigned int loccount = this->local_symbol_count_;
2487 gold_assert(loccount == symtabshdr.get_sh_info());
2489 // Read the local symbols.
2490 const int sym_size = This::sym_size;
2491 off_t locsize = loccount * sym_size;
2492 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
2493 locsize, true, false);
2495 // Read the symbol names.
2496 const unsigned int strtab_shndx =
2497 this->adjust_shndx(symtabshdr.get_sh_link());
2498 section_size_type strtab_size;
2499 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
2502 const char* pnames = reinterpret_cast<const char*>(pnamesu);
2504 // Get views into the output file for the portions of the symbol table
2505 // and the dynamic symbol table that we will be writing.
2506 off_t output_size = this->output_local_symbol_count_ * sym_size;
2507 unsigned char* oview = NULL;
2508 if (output_size > 0)
2509 oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2512 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2513 unsigned char* dyn_oview = NULL;
2514 if (dyn_output_size > 0)
2515 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2518 const Output_sections out_sections(this->output_sections());
2520 gold_assert(this->local_values_.size() == loccount);
2522 unsigned char* ov = oview;
2523 unsigned char* dyn_ov = dyn_oview;
2525 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2527 elfcpp::Sym<size, big_endian> isym(psyms);
2529 Symbol_value<size>& lv(this->local_values_[i]);
2532 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
2536 gold_assert(st_shndx < out_sections.size());
2537 if (out_sections[st_shndx] == NULL)
2539 st_shndx = out_sections[st_shndx]->out_shndx();
2540 if (st_shndx >= elfcpp::SHN_LORESERVE)
2542 if (lv.has_output_symtab_entry())
2543 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
2544 if (lv.has_output_dynsym_entry())
2545 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
2546 st_shndx = elfcpp::SHN_XINDEX;
2550 // Write the symbol to the output symbol table.
2551 if (lv.has_output_symtab_entry())
2553 elfcpp::Sym_write<size, big_endian> osym(ov);
2555 gold_assert(isym.get_st_name() < strtab_size);
2556 const char* name = pnames + isym.get_st_name();
2557 osym.put_st_name(sympool->get_offset(name));
2558 osym.put_st_value(this->local_values_[i].value(this, 0));
2559 osym.put_st_size(isym.get_st_size());
2560 osym.put_st_info(isym.get_st_info());
2561 osym.put_st_other(isym.get_st_other());
2562 osym.put_st_shndx(st_shndx);
2567 // Write the symbol to the output dynamic symbol table.
2568 if (lv.has_output_dynsym_entry())
2570 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2571 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2573 gold_assert(isym.get_st_name() < strtab_size);
2574 const char* name = pnames + isym.get_st_name();
2575 osym.put_st_name(dynpool->get_offset(name));
2576 osym.put_st_value(this->local_values_[i].value(this, 0));
2577 osym.put_st_size(isym.get_st_size());
2578 osym.put_st_info(isym.get_st_info());
2579 osym.put_st_other(isym.get_st_other());
2580 osym.put_st_shndx(st_shndx);
2587 if (output_size > 0)
2589 gold_assert(ov - oview == output_size);
2590 of->write_output_view(symtab_off + this->local_symbol_offset_,
2591 output_size, oview);
2594 if (dyn_output_size > 0)
2596 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2597 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2602 // Set *INFO to symbolic information about the offset OFFSET in the
2603 // section SHNDX. Return true if we found something, false if we
2606 template<int size, bool big_endian>
2608 Sized_relobj_file<size, big_endian>::get_symbol_location_info(
2611 Symbol_location_info* info)
2613 if (this->symtab_shndx_ == 0)
2616 section_size_type symbols_size;
2617 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
2621 unsigned int symbol_names_shndx =
2622 this->adjust_shndx(this->section_link(this->symtab_shndx_));
2623 section_size_type names_size;
2624 const unsigned char* symbol_names_u =
2625 this->section_contents(symbol_names_shndx, &names_size, false);
2626 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
2628 const int sym_size = This::sym_size;
2629 const size_t count = symbols_size / sym_size;
2631 const unsigned char* p = symbols;
2632 for (size_t i = 0; i < count; ++i, p += sym_size)
2634 elfcpp::Sym<size, big_endian> sym(p);
2636 if (sym.get_st_type() == elfcpp::STT_FILE)
2638 if (sym.get_st_name() >= names_size)
2639 info->source_file = "(invalid)";
2641 info->source_file = symbol_names + sym.get_st_name();
2646 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2649 && st_shndx == shndx
2650 && static_cast<off_t>(sym.get_st_value()) <= offset
2651 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
2654 if (sym.get_st_name() > names_size)
2655 info->enclosing_symbol_name = "(invalid)";
2658 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
2659 if (parameters->options().do_demangle())
2661 char* demangled_name = cplus_demangle(
2662 info->enclosing_symbol_name.c_str(),
2663 DMGL_ANSI | DMGL_PARAMS);
2664 if (demangled_name != NULL)
2666 info->enclosing_symbol_name.assign(demangled_name);
2667 free(demangled_name);
2678 // Look for a kept section corresponding to the given discarded section,
2679 // and return its output address. This is used only for relocations in
2680 // debugging sections. If we can't find the kept section, return 0.
2682 template<int size, bool big_endian>
2683 typename Sized_relobj_file<size, big_endian>::Address
2684 Sized_relobj_file<size, big_endian>::map_to_kept_section(
2688 Relobj* kept_object;
2689 unsigned int kept_shndx;
2690 if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
2692 Sized_relobj_file<size, big_endian>* kept_relobj =
2693 static_cast<Sized_relobj_file<size, big_endian>*>(kept_object);
2694 Output_section* os = kept_relobj->output_section(kept_shndx);
2695 Address offset = kept_relobj->get_output_section_offset(kept_shndx);
2696 if (os != NULL && offset != invalid_address)
2699 return os->address() + offset;
2706 // Get symbol counts.
2708 template<int size, bool big_endian>
2710 Sized_relobj_file<size, big_endian>::do_get_global_symbol_counts(
2711 const Symbol_table*,
2715 *defined = this->defined_count_;
2717 for (typename Symbols::const_iterator p = this->symbols_.begin();
2718 p != this->symbols_.end();
2721 && (*p)->source() == Symbol::FROM_OBJECT
2722 && (*p)->object() == this
2723 && (*p)->is_defined())
2728 // Return a view of the decompressed contents of a section. Set *PLEN
2729 // to the size. Set *IS_NEW to true if the contents need to be freed
2732 template<int size, bool big_endian>
2733 const unsigned char*
2734 Sized_relobj_file<size, big_endian>::do_decompressed_section_contents(
2736 section_size_type* plen,
2739 section_size_type buffer_size;
2740 const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
2743 if (this->compressed_sections_ == NULL)
2745 *plen = buffer_size;
2750 Compressed_section_map::const_iterator p =
2751 this->compressed_sections_->find(shndx);
2752 if (p == this->compressed_sections_->end())
2754 *plen = buffer_size;
2759 section_size_type uncompressed_size = p->second.size;
2760 if (p->second.contents != NULL)
2762 *plen = uncompressed_size;
2764 return p->second.contents;
2767 unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
2768 if (!decompress_input_section(buffer,
2772 this->error(_("could not decompress section %s"),
2773 this->do_section_name(shndx).c_str());
2775 // We could cache the results in p->second.contents and store
2776 // false in *IS_NEW, but build_compressed_section_map() would
2777 // have done so if it had expected it to be profitable. If
2778 // we reach this point, we expect to need the contents only
2779 // once in this pass.
2780 *plen = uncompressed_size;
2782 return uncompressed_data;
2785 // Discard any buffers of uncompressed sections. This is done
2786 // at the end of the Add_symbols task.
2788 template<int size, bool big_endian>
2790 Sized_relobj_file<size, big_endian>::do_discard_decompressed_sections()
2792 if (this->compressed_sections_ == NULL)
2795 for (Compressed_section_map::iterator p = this->compressed_sections_->begin();
2796 p != this->compressed_sections_->end();
2799 if (p->second.contents != NULL)
2801 delete[] p->second.contents;
2802 p->second.contents = NULL;
2807 // Input_objects methods.
2809 // Add a regular relocatable object to the list. Return false if this
2810 // object should be ignored.
2813 Input_objects::add_object(Object* obj)
2815 // Print the filename if the -t/--trace option is selected.
2816 if (parameters->options().trace())
2817 gold_info("%s", obj->name().c_str());
2819 if (!obj->is_dynamic())
2820 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
2823 // See if this is a duplicate SONAME.
2824 Dynobj* dynobj = static_cast<Dynobj*>(obj);
2825 const char* soname = dynobj->soname();
2827 std::pair<Unordered_set<std::string>::iterator, bool> ins =
2828 this->sonames_.insert(soname);
2831 // We have already seen a dynamic object with this soname.
2835 this->dynobj_list_.push_back(dynobj);
2838 // Add this object to the cross-referencer if requested.
2839 if (parameters->options().user_set_print_symbol_counts()
2840 || parameters->options().cref())
2842 if (this->cref_ == NULL)
2843 this->cref_ = new Cref();
2844 this->cref_->add_object(obj);
2850 // For each dynamic object, record whether we've seen all of its
2851 // explicit dependencies.
2854 Input_objects::check_dynamic_dependencies() const
2856 bool issued_copy_dt_needed_error = false;
2857 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
2858 p != this->dynobj_list_.end();
2861 const Dynobj::Needed& needed((*p)->needed());
2862 bool found_all = true;
2863 Dynobj::Needed::const_iterator pneeded;
2864 for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
2866 if (this->sonames_.find(*pneeded) == this->sonames_.end())
2872 (*p)->set_has_unknown_needed_entries(!found_all);
2874 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2875 // that gold does not support. However, they cause no trouble
2876 // unless there is a DT_NEEDED entry that we don't know about;
2877 // warn only in that case.
2879 && !issued_copy_dt_needed_error
2880 && (parameters->options().copy_dt_needed_entries()
2881 || parameters->options().add_needed()))
2883 const char* optname;
2884 if (parameters->options().copy_dt_needed_entries())
2885 optname = "--copy-dt-needed-entries";
2887 optname = "--add-needed";
2888 gold_error(_("%s is not supported but is required for %s in %s"),
2889 optname, (*pneeded).c_str(), (*p)->name().c_str());
2890 issued_copy_dt_needed_error = true;
2895 // Start processing an archive.
2898 Input_objects::archive_start(Archive* archive)
2900 if (parameters->options().user_set_print_symbol_counts()
2901 || parameters->options().cref())
2903 if (this->cref_ == NULL)
2904 this->cref_ = new Cref();
2905 this->cref_->add_archive_start(archive);
2909 // Stop processing an archive.
2912 Input_objects::archive_stop(Archive* archive)
2914 if (parameters->options().user_set_print_symbol_counts()
2915 || parameters->options().cref())
2916 this->cref_->add_archive_stop(archive);
2919 // Print symbol counts
2922 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
2924 if (parameters->options().user_set_print_symbol_counts()
2925 && this->cref_ != NULL)
2926 this->cref_->print_symbol_counts(symtab);
2929 // Print a cross reference table.
2932 Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
2934 if (parameters->options().cref() && this->cref_ != NULL)
2935 this->cref_->print_cref(symtab, f);
2938 // Relocate_info methods.
2940 // Return a string describing the location of a relocation when file
2941 // and lineno information is not available. This is only used in
2944 template<int size, bool big_endian>
2946 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
2948 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
2949 std::string ret = line_info.addr2line(this->data_shndx, offset, NULL);
2953 ret = this->object->name();
2955 Symbol_location_info info;
2956 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
2958 if (!info.source_file.empty())
2961 ret += info.source_file;
2963 size_t len = info.enclosing_symbol_name.length() + 100;
2964 char* buf = new char[len];
2965 snprintf(buf, len, _(":function %s"),
2966 info.enclosing_symbol_name.c_str());
2973 ret += this->object->section_name(this->data_shndx);
2975 snprintf(buf, sizeof buf, "+0x%lx)", static_cast<long>(offset));
2980 } // End namespace gold.
2985 using namespace gold;
2987 // Read an ELF file with the header and return the appropriate
2988 // instance of Object.
2990 template<int size, bool big_endian>
2992 make_elf_sized_object(const std::string& name, Input_file* input_file,
2993 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
2994 bool* punconfigured)
2996 Target* target = select_target(input_file, offset,
2997 ehdr.get_e_machine(), size, big_endian,
2998 ehdr.get_e_ident()[elfcpp::EI_OSABI],
2999 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
3001 gold_fatal(_("%s: unsupported ELF machine number %d"),
3002 name.c_str(), ehdr.get_e_machine());
3004 if (!parameters->target_valid())
3005 set_parameters_target(target);
3006 else if (target != ¶meters->target())
3008 if (punconfigured != NULL)
3009 *punconfigured = true;
3011 gold_error(_("%s: incompatible target"), name.c_str());
3015 return target->make_elf_object<size, big_endian>(name, input_file, offset,
3019 } // End anonymous namespace.
3024 // Return whether INPUT_FILE is an ELF object.
3027 is_elf_object(Input_file* input_file, off_t offset,
3028 const unsigned char** start, int* read_size)
3030 off_t filesize = input_file->file().filesize();
3031 int want = elfcpp::Elf_recognizer::max_header_size;
3032 if (filesize - offset < want)
3033 want = filesize - offset;
3035 const unsigned char* p = input_file->file().get_view(offset, 0, want,
3040 return elfcpp::Elf_recognizer::is_elf_file(p, want);
3043 // Read an ELF file and return the appropriate instance of Object.
3046 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
3047 const unsigned char* p, section_offset_type bytes,
3048 bool* punconfigured)
3050 if (punconfigured != NULL)
3051 *punconfigured = false;
3054 bool big_endian = false;
3056 if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
3057 &big_endian, &error))
3059 gold_error(_("%s: %s"), name.c_str(), error.c_str());
3067 #ifdef HAVE_TARGET_32_BIG
3068 elfcpp::Ehdr<32, true> ehdr(p);
3069 return make_elf_sized_object<32, true>(name, input_file,
3070 offset, ehdr, punconfigured);
3072 if (punconfigured != NULL)
3073 *punconfigured = true;
3075 gold_error(_("%s: not configured to support "
3076 "32-bit big-endian object"),
3083 #ifdef HAVE_TARGET_32_LITTLE
3084 elfcpp::Ehdr<32, false> ehdr(p);
3085 return make_elf_sized_object<32, false>(name, input_file,
3086 offset, ehdr, punconfigured);
3088 if (punconfigured != NULL)
3089 *punconfigured = true;
3091 gold_error(_("%s: not configured to support "
3092 "32-bit little-endian object"),
3098 else if (size == 64)
3102 #ifdef HAVE_TARGET_64_BIG
3103 elfcpp::Ehdr<64, true> ehdr(p);
3104 return make_elf_sized_object<64, true>(name, input_file,
3105 offset, ehdr, punconfigured);
3107 if (punconfigured != NULL)
3108 *punconfigured = true;
3110 gold_error(_("%s: not configured to support "
3111 "64-bit big-endian object"),
3118 #ifdef HAVE_TARGET_64_LITTLE
3119 elfcpp::Ehdr<64, false> ehdr(p);
3120 return make_elf_sized_object<64, false>(name, input_file,
3121 offset, ehdr, punconfigured);
3123 if (punconfigured != NULL)
3124 *punconfigured = true;
3126 gold_error(_("%s: not configured to support "
3127 "64-bit little-endian object"),
3137 // Instantiate the templates we need.
3139 #ifdef HAVE_TARGET_32_LITTLE
3142 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
3143 Read_symbols_data*);
3146 #ifdef HAVE_TARGET_32_BIG
3149 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
3150 Read_symbols_data*);
3153 #ifdef HAVE_TARGET_64_LITTLE
3156 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
3157 Read_symbols_data*);
3160 #ifdef HAVE_TARGET_64_BIG
3163 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
3164 Read_symbols_data*);
3167 #ifdef HAVE_TARGET_32_LITTLE
3169 class Sized_relobj_file<32, false>;
3172 #ifdef HAVE_TARGET_32_BIG
3174 class Sized_relobj_file<32, true>;
3177 #ifdef HAVE_TARGET_64_LITTLE
3179 class Sized_relobj_file<64, false>;
3182 #ifdef HAVE_TARGET_64_BIG
3184 class Sized_relobj_file<64, true>;
3187 #ifdef HAVE_TARGET_32_LITTLE
3189 struct Relocate_info<32, false>;
3192 #ifdef HAVE_TARGET_32_BIG
3194 struct Relocate_info<32, true>;
3197 #ifdef HAVE_TARGET_64_LITTLE
3199 struct Relocate_info<64, false>;
3202 #ifdef HAVE_TARGET_64_BIG
3204 struct Relocate_info<64, true>;
3207 #ifdef HAVE_TARGET_32_LITTLE
3210 Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
3214 Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
3215 const unsigned char*);
3218 #ifdef HAVE_TARGET_32_BIG
3221 Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
3225 Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
3226 const unsigned char*);
3229 #ifdef HAVE_TARGET_64_LITTLE
3232 Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
3236 Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
3237 const unsigned char*);
3240 #ifdef HAVE_TARGET_64_BIG
3243 Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
3247 Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
3248 const unsigned char*);
3251 } // End namespace gold.