1 // object.cc -- support for an object file for linking in gold
3 // Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "libiberty.h"
32 #include "target-select.h"
33 #include "dwarf_reader.h"
42 #include "compressed_output.h"
43 #include "incremental.h"
49 // Struct Read_symbols_data.
51 // Destroy any remaining File_view objects and buffers of decompressed
54 Read_symbols_data::~Read_symbols_data()
56 if (this->section_headers
!= NULL
)
57 delete this->section_headers
;
58 if (this->section_names
!= NULL
)
59 delete this->section_names
;
60 if (this->symbols
!= NULL
)
62 if (this->symbol_names
!= NULL
)
63 delete this->symbol_names
;
64 if (this->versym
!= NULL
)
66 if (this->verdef
!= NULL
)
68 if (this->verneed
!= NULL
)
74 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
75 // section and read it in. SYMTAB_SHNDX is the index of the symbol
76 // table we care about.
78 template<int size
, bool big_endian
>
80 Xindex::initialize_symtab_xindex(Object
* object
, unsigned int symtab_shndx
)
82 if (!this->symtab_xindex_
.empty())
85 gold_assert(symtab_shndx
!= 0);
87 // Look through the sections in reverse order, on the theory that it
88 // is more likely to be near the end than the beginning.
89 unsigned int i
= object
->shnum();
93 if (object
->section_type(i
) == elfcpp::SHT_SYMTAB_SHNDX
94 && this->adjust_shndx(object
->section_link(i
)) == symtab_shndx
)
96 this->read_symtab_xindex
<size
, big_endian
>(object
, i
, NULL
);
101 object
->error(_("missing SHT_SYMTAB_SHNDX section"));
104 // Read in the symtab_xindex_ array, given the section index of the
105 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
108 template<int size
, bool big_endian
>
110 Xindex::read_symtab_xindex(Object
* object
, unsigned int xindex_shndx
,
111 const unsigned char* pshdrs
)
113 section_size_type bytecount
;
114 const unsigned char* contents
;
116 contents
= object
->section_contents(xindex_shndx
, &bytecount
, false);
119 const unsigned char* p
= (pshdrs
121 * elfcpp::Elf_sizes
<size
>::shdr_size
));
122 typename
elfcpp::Shdr
<size
, big_endian
> shdr(p
);
123 bytecount
= convert_to_section_size_type(shdr
.get_sh_size());
124 contents
= object
->get_view(shdr
.get_sh_offset(), bytecount
, true, false);
127 gold_assert(this->symtab_xindex_
.empty());
128 this->symtab_xindex_
.reserve(bytecount
/ 4);
129 for (section_size_type i
= 0; i
< bytecount
; i
+= 4)
131 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
132 // We preadjust the section indexes we save.
133 this->symtab_xindex_
.push_back(this->adjust_shndx(shndx
));
137 // Symbol symndx has a section of SHN_XINDEX; return the real section
141 Xindex::sym_xindex_to_shndx(Object
* object
, unsigned int symndx
)
143 if (symndx
>= this->symtab_xindex_
.size())
145 object
->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
147 return elfcpp::SHN_UNDEF
;
149 unsigned int shndx
= this->symtab_xindex_
[symndx
];
150 if (shndx
< elfcpp::SHN_LORESERVE
|| shndx
>= object
->shnum())
152 object
->error(_("extended index for symbol %u out of range: %u"),
154 return elfcpp::SHN_UNDEF
;
161 // Report an error for this object file. This is used by the
162 // elfcpp::Elf_file interface, and also called by the Object code
166 Object::error(const char* format
, ...) const
169 va_start(args
, format
);
171 if (vasprintf(&buf
, format
, args
) < 0)
174 gold_error(_("%s: %s"), this->name().c_str(), buf
);
178 // Return a view of the contents of a section.
181 Object::section_contents(unsigned int shndx
, section_size_type
* plen
,
183 { return this->do_section_contents(shndx
, plen
, cache
); }
185 // Read the section data into SD. This is code common to Sized_relobj_file
186 // and Sized_dynobj, so we put it into Object.
188 template<int size
, bool big_endian
>
190 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
191 Read_symbols_data
* sd
)
193 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
195 // Read the section headers.
196 const off_t shoff
= elf_file
->shoff();
197 const unsigned int shnum
= this->shnum();
198 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
,
201 // Read the section names.
202 const unsigned char* pshdrs
= sd
->section_headers
->data();
203 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
204 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
206 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
207 this->error(_("section name section has wrong type: %u"),
208 static_cast<unsigned int>(shdrnames
.get_sh_type()));
210 sd
->section_names_size
=
211 convert_to_section_size_type(shdrnames
.get_sh_size());
212 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
213 sd
->section_names_size
, false,
217 // If NAME is the name of a special .gnu.warning section, arrange for
218 // the warning to be issued. SHNDX is the section index. Return
219 // whether it is a warning section.
222 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
223 Symbol_table
* symtab
)
225 const char warn_prefix
[] = ".gnu.warning.";
226 const int warn_prefix_len
= sizeof warn_prefix
- 1;
227 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
229 // Read the section contents to get the warning text. It would
230 // be nicer if we only did this if we have to actually issue a
231 // warning. Unfortunately, warnings are issued as we relocate
232 // sections. That means that we can not lock the object then,
233 // as we might try to issue the same warning multiple times
235 section_size_type len
;
236 const unsigned char* contents
= this->section_contents(shndx
, &len
,
240 const char* warning
= name
+ warn_prefix_len
;
241 contents
= reinterpret_cast<const unsigned char*>(warning
);
242 len
= strlen(warning
);
244 std::string
warning(reinterpret_cast<const char*>(contents
), len
);
245 symtab
->add_warning(name
+ warn_prefix_len
, this, warning
);
251 // If NAME is the name of the special section which indicates that
252 // this object was compiled with -fsplit-stack, mark it accordingly.
255 Object::handle_split_stack_section(const char* name
)
257 if (strcmp(name
, ".note.GNU-split-stack") == 0)
259 this->uses_split_stack_
= true;
262 if (strcmp(name
, ".note.GNU-no-split-stack") == 0)
264 this->has_no_split_stack_
= true;
274 Relobj::initialize_input_to_output_map(unsigned int shndx
,
275 typename
elfcpp::Elf_types
<size
>::Elf_Addr starting_address
,
276 Unordered_map
<section_offset_type
,
277 typename
elfcpp::Elf_types
<size
>::Elf_Addr
>* output_addresses
) const {
278 Object_merge_map
*map
= this->object_merge_map_
;
279 map
->initialize_input_to_output_map
<size
>(shndx
, starting_address
,
284 Relobj::add_merge_mapping(Output_section_data
*output_data
,
285 unsigned int shndx
, section_offset_type offset
,
286 section_size_type length
,
287 section_offset_type output_offset
) {
288 if (this->object_merge_map_
== NULL
)
290 this->object_merge_map_
= new Object_merge_map();
293 this->object_merge_map_
->add_mapping(output_data
, shndx
, offset
, length
,
298 Relobj::merge_output_offset(unsigned int shndx
, section_offset_type offset
,
299 section_offset_type
*poutput
) const {
300 Object_merge_map
* object_merge_map
= this->object_merge_map_
;
301 if (object_merge_map
== NULL
)
303 return object_merge_map
->get_output_offset(shndx
, offset
, poutput
);
307 Relobj::is_merge_section_for(const Output_section_data
* output_data
,
308 unsigned int shndx
) const {
309 Object_merge_map
* object_merge_map
= this->object_merge_map_
;
310 if (object_merge_map
== NULL
)
312 return object_merge_map
->is_merge_section_for(output_data
, shndx
);
316 // To copy the symbols data read from the file to a local data structure.
317 // This function is called from do_layout only while doing garbage
321 Relobj::copy_symbols_data(Symbols_data
* gc_sd
, Read_symbols_data
* sd
,
322 unsigned int section_header_size
)
324 gc_sd
->section_headers_data
=
325 new unsigned char[(section_header_size
)];
326 memcpy(gc_sd
->section_headers_data
, sd
->section_headers
->data(),
327 section_header_size
);
328 gc_sd
->section_names_data
=
329 new unsigned char[sd
->section_names_size
];
330 memcpy(gc_sd
->section_names_data
, sd
->section_names
->data(),
331 sd
->section_names_size
);
332 gc_sd
->section_names_size
= sd
->section_names_size
;
333 if (sd
->symbols
!= NULL
)
335 gc_sd
->symbols_data
=
336 new unsigned char[sd
->symbols_size
];
337 memcpy(gc_sd
->symbols_data
, sd
->symbols
->data(),
342 gc_sd
->symbols_data
= NULL
;
344 gc_sd
->symbols_size
= sd
->symbols_size
;
345 gc_sd
->external_symbols_offset
= sd
->external_symbols_offset
;
346 if (sd
->symbol_names
!= NULL
)
348 gc_sd
->symbol_names_data
=
349 new unsigned char[sd
->symbol_names_size
];
350 memcpy(gc_sd
->symbol_names_data
, sd
->symbol_names
->data(),
351 sd
->symbol_names_size
);
355 gc_sd
->symbol_names_data
= NULL
;
357 gc_sd
->symbol_names_size
= sd
->symbol_names_size
;
360 // This function determines if a particular section name must be included
361 // in the link. This is used during garbage collection to determine the
362 // roots of the worklist.
365 Relobj::is_section_name_included(const char* name
)
367 if (is_prefix_of(".ctors", name
)
368 || is_prefix_of(".dtors", name
)
369 || is_prefix_of(".note", name
)
370 || is_prefix_of(".init", name
)
371 || is_prefix_of(".fini", name
)
372 || is_prefix_of(".gcc_except_table", name
)
373 || is_prefix_of(".jcr", name
)
374 || is_prefix_of(".preinit_array", name
)
375 || (is_prefix_of(".text", name
)
376 && strstr(name
, "personality"))
377 || (is_prefix_of(".data", name
)
378 && strstr(name
, "personality"))
379 || (is_prefix_of(".sdata", name
)
380 && strstr(name
, "personality"))
381 || (is_prefix_of(".gnu.linkonce.d", name
)
382 && strstr(name
, "personality"))
383 || (is_prefix_of(".rodata", name
)
384 && strstr(name
, "nptl_version")))
391 // Finalize the incremental relocation information. Allocates a block
392 // of relocation entries for each symbol, and sets the reloc_bases_
393 // array to point to the first entry in each block. If CLEAR_COUNTS
394 // is TRUE, also clear the per-symbol relocation counters.
397 Relobj::finalize_incremental_relocs(Layout
* layout
, bool clear_counts
)
399 unsigned int nsyms
= this->get_global_symbols()->size();
400 this->reloc_bases_
= new unsigned int[nsyms
];
402 gold_assert(this->reloc_bases_
!= NULL
);
403 gold_assert(layout
->incremental_inputs() != NULL
);
405 unsigned int rindex
= layout
->incremental_inputs()->get_reloc_count();
406 for (unsigned int i
= 0; i
< nsyms
; ++i
)
408 this->reloc_bases_
[i
] = rindex
;
409 rindex
+= this->reloc_counts_
[i
];
411 this->reloc_counts_
[i
] = 0;
413 layout
->incremental_inputs()->set_reloc_count(rindex
);
416 // Class Sized_relobj.
418 // Iterate over local symbols, calling a visitor class V for each GOT offset
419 // associated with a local symbol.
421 template<int size
, bool big_endian
>
423 Sized_relobj
<size
, big_endian
>::do_for_all_local_got_entries(
424 Got_offset_list::Visitor
* v
) const
426 unsigned int nsyms
= this->local_symbol_count();
427 for (unsigned int i
= 0; i
< nsyms
; i
++)
429 Local_got_offsets::const_iterator p
= this->local_got_offsets_
.find(i
);
430 if (p
!= this->local_got_offsets_
.end())
432 const Got_offset_list
* got_offsets
= p
->second
;
433 got_offsets
->for_all_got_offsets(v
);
438 // Get the address of an output section.
440 template<int size
, bool big_endian
>
442 Sized_relobj
<size
, big_endian
>::do_output_section_address(
445 // If the input file is linked as --just-symbols, the output
446 // section address is the input section address.
447 if (this->just_symbols())
448 return this->section_address(shndx
);
450 const Output_section
* os
= this->do_output_section(shndx
);
451 gold_assert(os
!= NULL
);
452 return os
->address();
455 // Class Sized_relobj_file.
457 template<int size
, bool big_endian
>
458 Sized_relobj_file
<size
, big_endian
>::Sized_relobj_file(
459 const std::string
& name
,
460 Input_file
* input_file
,
462 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
463 : Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
),
464 elf_file_(this, ehdr
),
466 local_symbol_count_(0),
467 output_local_symbol_count_(0),
468 output_local_dynsym_count_(0),
471 local_symbol_offset_(0),
472 local_dynsym_offset_(0),
474 local_plt_offsets_(),
475 kept_comdat_sections_(),
476 has_eh_frame_(false),
477 discarded_eh_frame_shndx_(-1U),
478 is_deferred_layout_(false),
480 deferred_layout_relocs_()
482 this->e_type_
= ehdr
.get_e_type();
485 template<int size
, bool big_endian
>
486 Sized_relobj_file
<size
, big_endian
>::~Sized_relobj_file()
490 // Set up an object file based on the file header. This sets up the
491 // section information.
493 template<int size
, bool big_endian
>
495 Sized_relobj_file
<size
, big_endian
>::do_setup()
497 const unsigned int shnum
= this->elf_file_
.shnum();
498 this->set_shnum(shnum
);
501 // Find the SHT_SYMTAB section, given the section headers. The ELF
502 // standard says that maybe in the future there can be more than one
503 // SHT_SYMTAB section. Until somebody figures out how that could
504 // work, we assume there is only one.
506 template<int size
, bool big_endian
>
508 Sized_relobj_file
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
510 const unsigned int shnum
= this->shnum();
511 this->symtab_shndx_
= 0;
514 // Look through the sections in reverse order, since gas tends
515 // to put the symbol table at the end.
516 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
517 unsigned int i
= shnum
;
518 unsigned int xindex_shndx
= 0;
519 unsigned int xindex_link
= 0;
523 p
-= This::shdr_size
;
524 typename
This::Shdr
shdr(p
);
525 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
527 this->symtab_shndx_
= i
;
528 if (xindex_shndx
> 0 && xindex_link
== i
)
531 new Xindex(this->elf_file_
.large_shndx_offset());
532 xindex
->read_symtab_xindex
<size
, big_endian
>(this,
535 this->set_xindex(xindex
);
540 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
541 // one. This will work if it follows the SHT_SYMTAB
543 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX
)
546 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
552 // Return the Xindex structure to use for object with lots of
555 template<int size
, bool big_endian
>
557 Sized_relobj_file
<size
, big_endian
>::do_initialize_xindex()
559 gold_assert(this->symtab_shndx_
!= -1U);
560 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
561 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->symtab_shndx_
);
565 // Return whether SHDR has the right type and flags to be a GNU
566 // .eh_frame section.
568 template<int size
, bool big_endian
>
570 Sized_relobj_file
<size
, big_endian
>::check_eh_frame_flags(
571 const elfcpp::Shdr
<size
, big_endian
>* shdr
) const
573 elfcpp::Elf_Word sh_type
= shdr
->get_sh_type();
574 return ((sh_type
== elfcpp::SHT_PROGBITS
575 || sh_type
== elfcpp::SHT_X86_64_UNWIND
)
576 && (shdr
->get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
579 // Find the section header with the given name.
581 template<int size
, bool big_endian
>
584 const unsigned char* pshdrs
,
587 section_size_type names_size
,
588 const unsigned char* hdr
) const
590 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
591 const unsigned int shnum
= this->shnum();
592 const unsigned char* hdr_end
= pshdrs
+ shdr_size
* shnum
;
599 // We found HDR last time we were called, continue looking.
600 typename
elfcpp::Shdr
<size
, big_endian
> shdr(hdr
);
601 sh_name
= shdr
.get_sh_name();
605 // Look for the next occurrence of NAME in NAMES.
606 // The fact that .shstrtab produced by current GNU tools is
607 // string merged means we shouldn't have both .not.foo and
608 // .foo in .shstrtab, and multiple .foo sections should all
609 // have the same sh_name. However, this is not guaranteed
610 // by the ELF spec and not all ELF object file producers may
612 size_t len
= strlen(name
) + 1;
613 const char *p
= sh_name
? names
+ sh_name
+ len
: names
;
614 p
= reinterpret_cast<const char*>(memmem(p
, names_size
- (p
- names
),
625 while (hdr
< hdr_end
)
627 typename
elfcpp::Shdr
<size
, big_endian
> shdr(hdr
);
628 if (shdr
.get_sh_name() == sh_name
)
638 // Return whether there is a GNU .eh_frame section, given the section
639 // headers and the section names.
641 template<int size
, bool big_endian
>
643 Sized_relobj_file
<size
, big_endian
>::find_eh_frame(
644 const unsigned char* pshdrs
,
646 section_size_type names_size
) const
648 const unsigned char* s
= NULL
;
652 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".eh_frame",
653 names
, names_size
, s
);
657 typename
This::Shdr
shdr(s
);
658 if (this->check_eh_frame_flags(&shdr
))
663 // Return TRUE if this is a section whose contents will be needed in the
664 // Add_symbols task. This function is only called for sections that have
665 // already passed the test in is_compressed_debug_section(), so we know
666 // that the section name begins with ".zdebug".
669 need_decompressed_section(const char* name
)
671 // Skip over the ".zdebug" and a quick check for the "_".
676 #ifdef ENABLE_THREADS
677 // Decompressing these sections now will help only if we're
679 if (parameters
->options().threads())
681 // We will need .zdebug_str if this is not an incremental link
682 // (i.e., we are processing string merge sections) or if we need
683 // to build a gdb index.
684 if ((!parameters
->incremental() || parameters
->options().gdb_index())
685 && strcmp(name
, "str") == 0)
688 // We will need these other sections when building a gdb index.
689 if (parameters
->options().gdb_index()
690 && (strcmp(name
, "info") == 0
691 || strcmp(name
, "types") == 0
692 || strcmp(name
, "pubnames") == 0
693 || strcmp(name
, "pubtypes") == 0
694 || strcmp(name
, "ranges") == 0
695 || strcmp(name
, "abbrev") == 0))
700 // Even when single-threaded, we will need .zdebug_str if this is
701 // not an incremental link and we are building a gdb index.
702 // Otherwise, we would decompress the section twice: once for
703 // string merge processing, and once for building the gdb index.
704 if (!parameters
->incremental()
705 && parameters
->options().gdb_index()
706 && strcmp(name
, "str") == 0)
712 // Build a table for any compressed debug sections, mapping each section index
713 // to the uncompressed size and (if needed) the decompressed contents.
715 template<int size
, bool big_endian
>
716 Compressed_section_map
*
717 build_compressed_section_map(
718 const unsigned char* pshdrs
,
721 section_size_type names_size
,
723 bool decompress_if_needed
)
725 Compressed_section_map
* uncompressed_map
= new Compressed_section_map();
726 const unsigned int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
727 const unsigned char* p
= pshdrs
+ shdr_size
;
729 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= shdr_size
)
731 typename
elfcpp::Shdr
<size
, big_endian
> shdr(p
);
732 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
733 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) == 0)
735 if (shdr
.get_sh_name() >= names_size
)
737 obj
->error(_("bad section name offset for section %u: %lu"),
738 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
742 const char* name
= names
+ shdr
.get_sh_name();
743 if (is_compressed_debug_section(name
))
745 section_size_type len
;
746 const unsigned char* contents
=
747 obj
->section_contents(i
, &len
, false);
748 uint64_t uncompressed_size
= get_uncompressed_size(contents
, len
);
749 Compressed_section_info info
;
750 info
.size
= convert_to_section_size_type(uncompressed_size
);
751 info
.contents
= NULL
;
752 if (uncompressed_size
!= -1ULL)
754 unsigned char* uncompressed_data
= NULL
;
755 if (decompress_if_needed
&& need_decompressed_section(name
))
757 uncompressed_data
= new unsigned char[uncompressed_size
];
758 if (decompress_input_section(contents
, len
,
761 info
.contents
= uncompressed_data
;
763 delete[] uncompressed_data
;
765 (*uncompressed_map
)[i
] = info
;
770 return uncompressed_map
;
773 // Stash away info for a number of special sections.
774 // Return true if any of the sections found require local symbols to be read.
776 template<int size
, bool big_endian
>
778 Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(
779 Read_symbols_data
* sd
)
781 const unsigned char* const pshdrs
= sd
->section_headers
->data();
782 const unsigned char* namesu
= sd
->section_names
->data();
783 const char* names
= reinterpret_cast<const char*>(namesu
);
785 if (this->find_eh_frame(pshdrs
, names
, sd
->section_names_size
))
786 this->has_eh_frame_
= true;
788 if (memmem(names
, sd
->section_names_size
, ".zdebug_", 8) != NULL
)
790 Compressed_section_map
* compressed_sections
=
791 build_compressed_section_map
<size
, big_endian
>(
792 pshdrs
, this->shnum(), names
, sd
->section_names_size
, this, true);
793 if (compressed_sections
!= NULL
)
794 this->set_compressed_sections(compressed_sections
);
797 return (this->has_eh_frame_
798 || (!parameters
->options().relocatable()
799 && parameters
->options().gdb_index()
800 && (memmem(names
, sd
->section_names_size
, "debug_info", 12) == 0
801 || memmem(names
, sd
->section_names_size
, "debug_types",
805 // Read the sections and symbols from an object file.
807 template<int size
, bool big_endian
>
809 Sized_relobj_file
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
811 this->base_read_symbols(sd
);
814 // Read the sections and symbols from an object file. This is common
815 // code for all target-specific overrides of do_read_symbols().
817 template<int size
, bool big_endian
>
819 Sized_relobj_file
<size
, big_endian
>::base_read_symbols(Read_symbols_data
* sd
)
821 this->read_section_data(&this->elf_file_
, sd
);
823 const unsigned char* const pshdrs
= sd
->section_headers
->data();
825 this->find_symtab(pshdrs
);
827 bool need_local_symbols
= this->do_find_special_sections(sd
);
830 sd
->symbols_size
= 0;
831 sd
->external_symbols_offset
= 0;
832 sd
->symbol_names
= NULL
;
833 sd
->symbol_names_size
= 0;
835 if (this->symtab_shndx_
== 0)
837 // No symbol table. Weird but legal.
841 // Get the symbol table section header.
842 typename
This::Shdr
symtabshdr(pshdrs
843 + this->symtab_shndx_
* This::shdr_size
);
844 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
846 // If this object has a .eh_frame section, or if building a .gdb_index
847 // section and there is debug info, we need all the symbols.
848 // Otherwise we only need the external symbols. While it would be
849 // simpler to just always read all the symbols, I've seen object
850 // files with well over 2000 local symbols, which for a 64-bit
851 // object file format is over 5 pages that we don't need to read
854 const int sym_size
= This::sym_size
;
855 const unsigned int loccount
= symtabshdr
.get_sh_info();
856 this->local_symbol_count_
= loccount
;
857 this->local_values_
.resize(loccount
);
858 section_offset_type locsize
= loccount
* sym_size
;
859 off_t dataoff
= symtabshdr
.get_sh_offset();
860 section_size_type datasize
=
861 convert_to_section_size_type(symtabshdr
.get_sh_size());
862 off_t extoff
= dataoff
+ locsize
;
863 section_size_type extsize
= datasize
- locsize
;
865 off_t readoff
= need_local_symbols
? dataoff
: extoff
;
866 section_size_type readsize
= need_local_symbols
? datasize
: extsize
;
870 // No external symbols. Also weird but also legal.
874 File_view
* fvsymtab
= this->get_lasting_view(readoff
, readsize
, true, false);
876 // Read the section header for the symbol names.
877 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
878 if (strtab_shndx
>= this->shnum())
880 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
883 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
884 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
886 this->error(_("symbol table name section has wrong type: %u"),
887 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
891 // Read the symbol names.
892 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
893 strtabshdr
.get_sh_size(),
896 sd
->symbols
= fvsymtab
;
897 sd
->symbols_size
= readsize
;
898 sd
->external_symbols_offset
= need_local_symbols
? locsize
: 0;
899 sd
->symbol_names
= fvstrtab
;
900 sd
->symbol_names_size
=
901 convert_to_section_size_type(strtabshdr
.get_sh_size());
904 // Return the section index of symbol SYM. Set *VALUE to its value in
905 // the object file. Set *IS_ORDINARY if this is an ordinary section
906 // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
907 // Note that for a symbol which is not defined in this object file,
908 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
909 // the final value of the symbol in the link.
911 template<int size
, bool big_endian
>
913 Sized_relobj_file
<size
, big_endian
>::symbol_section_and_value(unsigned int sym
,
917 section_size_type symbols_size
;
918 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
922 const size_t count
= symbols_size
/ This::sym_size
;
923 gold_assert(sym
< count
);
925 elfcpp::Sym
<size
, big_endian
> elfsym(symbols
+ sym
* This::sym_size
);
926 *value
= elfsym
.get_st_value();
928 return this->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
931 // Return whether to include a section group in the link. LAYOUT is
932 // used to keep track of which section groups we have already seen.
933 // INDEX is the index of the section group and SHDR is the section
934 // header. If we do not want to include this group, we set bits in
935 // OMIT for each section which should be discarded.
937 template<int size
, bool big_endian
>
939 Sized_relobj_file
<size
, big_endian
>::include_section_group(
940 Symbol_table
* symtab
,
944 const unsigned char* shdrs
,
945 const char* section_names
,
946 section_size_type section_names_size
,
947 std::vector
<bool>* omit
)
949 // Read the section contents.
950 typename
This::Shdr
shdr(shdrs
+ index
* This::shdr_size
);
951 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
952 shdr
.get_sh_size(), true, false);
953 const elfcpp::Elf_Word
* pword
=
954 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
956 // The first word contains flags. We only care about COMDAT section
957 // groups. Other section groups are always included in the link
958 // just like ordinary sections.
959 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
961 // Look up the group signature, which is the name of a symbol. ELF
962 // uses a symbol name because some group signatures are long, and
963 // the name is generally already in the symbol table, so it makes
964 // sense to put the long string just once in .strtab rather than in
965 // both .strtab and .shstrtab.
967 // Get the appropriate symbol table header (this will normally be
968 // the single SHT_SYMTAB section, but in principle it need not be).
969 const unsigned int link
= this->adjust_shndx(shdr
.get_sh_link());
970 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
972 // Read the symbol table entry.
973 unsigned int symndx
= shdr
.get_sh_info();
974 if (symndx
>= symshdr
.get_sh_size() / This::sym_size
)
976 this->error(_("section group %u info %u out of range"),
980 off_t symoff
= symshdr
.get_sh_offset() + symndx
* This::sym_size
;
981 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true,
983 elfcpp::Sym
<size
, big_endian
> sym(psym
);
985 // Read the symbol table names.
986 section_size_type symnamelen
;
987 const unsigned char* psymnamesu
;
988 psymnamesu
= this->section_contents(this->adjust_shndx(symshdr
.get_sh_link()),
990 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
992 // Get the section group signature.
993 if (sym
.get_st_name() >= symnamelen
)
995 this->error(_("symbol %u name offset %u out of range"),
996 symndx
, sym
.get_st_name());
1000 std::string
signature(psymnames
+ sym
.get_st_name());
1002 // It seems that some versions of gas will create a section group
1003 // associated with a section symbol, and then fail to give a name to
1004 // the section symbol. In such a case, use the name of the section.
1005 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
1008 unsigned int sym_shndx
= this->adjust_sym_shndx(symndx
,
1011 if (!is_ordinary
|| sym_shndx
>= this->shnum())
1013 this->error(_("symbol %u invalid section index %u"),
1017 typename
This::Shdr
member_shdr(shdrs
+ sym_shndx
* This::shdr_size
);
1018 if (member_shdr
.get_sh_name() < section_names_size
)
1019 signature
= section_names
+ member_shdr
.get_sh_name();
1022 // Record this section group in the layout, and see whether we've already
1023 // seen one with the same signature.
1026 Kept_section
* kept_section
= NULL
;
1028 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
1030 include_group
= true;
1035 include_group
= layout
->find_or_add_kept_section(signature
,
1037 true, &kept_section
);
1041 if (is_comdat
&& include_group
)
1043 Incremental_inputs
* incremental_inputs
= layout
->incremental_inputs();
1044 if (incremental_inputs
!= NULL
)
1045 incremental_inputs
->report_comdat_group(this, signature
.c_str());
1048 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
1050 std::vector
<unsigned int> shndxes
;
1051 bool relocate_group
= include_group
&& parameters
->options().relocatable();
1053 shndxes
.reserve(count
- 1);
1055 for (size_t i
= 1; i
< count
; ++i
)
1057 elfcpp::Elf_Word shndx
=
1058 this->adjust_shndx(elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
));
1061 shndxes
.push_back(shndx
);
1063 if (shndx
>= this->shnum())
1065 this->error(_("section %u in section group %u out of range"),
1070 // Check for an earlier section number, since we're going to get
1071 // it wrong--we may have already decided to include the section.
1073 this->error(_("invalid section group %u refers to earlier section %u"),
1076 // Get the name of the member section.
1077 typename
This::Shdr
member_shdr(shdrs
+ shndx
* This::shdr_size
);
1078 if (member_shdr
.get_sh_name() >= section_names_size
)
1080 // This is an error, but it will be diagnosed eventually
1081 // in do_layout, so we don't need to do anything here but
1085 std::string
mname(section_names
+ member_shdr
.get_sh_name());
1090 kept_section
->add_comdat_section(mname
, shndx
,
1091 member_shdr
.get_sh_size());
1095 (*omit
)[shndx
] = true;
1099 Relobj
* kept_object
= kept_section
->object();
1100 if (kept_section
->is_comdat())
1102 // Find the corresponding kept section, and store
1103 // that info in the discarded section table.
1104 unsigned int kept_shndx
;
1106 if (kept_section
->find_comdat_section(mname
, &kept_shndx
,
1109 // We don't keep a mapping for this section if
1110 // it has a different size. The mapping is only
1111 // used for relocation processing, and we don't
1112 // want to treat the sections as similar if the
1113 // sizes are different. Checking the section
1114 // size is the approach used by the GNU linker.
1115 if (kept_size
== member_shdr
.get_sh_size())
1116 this->set_kept_comdat_section(shndx
, kept_object
,
1122 // The existing section is a linkonce section. Add
1123 // a mapping if there is exactly one section in the
1124 // group (which is true when COUNT == 2) and if it
1125 // is the same size.
1127 && (kept_section
->linkonce_size()
1128 == member_shdr
.get_sh_size()))
1129 this->set_kept_comdat_section(shndx
, kept_object
,
1130 kept_section
->shndx());
1137 layout
->layout_group(symtab
, this, index
, name
, signature
.c_str(),
1138 shdr
, flags
, &shndxes
);
1140 return include_group
;
1143 // Whether to include a linkonce section in the link. NAME is the
1144 // name of the section and SHDR is the section header.
1146 // Linkonce sections are a GNU extension implemented in the original
1147 // GNU linker before section groups were defined. The semantics are
1148 // that we only include one linkonce section with a given name. The
1149 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
1150 // where T is the type of section and SYMNAME is the name of a symbol.
1151 // In an attempt to make linkonce sections interact well with section
1152 // groups, we try to identify SYMNAME and use it like a section group
1153 // signature. We want to block section groups with that signature,
1154 // but not other linkonce sections with that signature. We also use
1155 // the full name of the linkonce section as a normal section group
1158 template<int size
, bool big_endian
>
1160 Sized_relobj_file
<size
, big_endian
>::include_linkonce_section(
1164 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
1166 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_size
= shdr
.get_sh_size();
1167 // In general the symbol name we want will be the string following
1168 // the last '.'. However, we have to handle the case of
1169 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
1170 // some versions of gcc. So we use a heuristic: if the name starts
1171 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
1172 // we look for the last '.'. We can't always simply skip
1173 // ".gnu.linkonce.X", because we have to deal with cases like
1174 // ".gnu.linkonce.d.rel.ro.local".
1175 const char* const linkonce_t
= ".gnu.linkonce.t.";
1176 const char* symname
;
1177 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
1178 symname
= name
+ strlen(linkonce_t
);
1180 symname
= strrchr(name
, '.') + 1;
1181 std::string
sig1(symname
);
1182 std::string
sig2(name
);
1183 Kept_section
* kept1
;
1184 Kept_section
* kept2
;
1185 bool include1
= layout
->find_or_add_kept_section(sig1
, this, index
, false,
1187 bool include2
= layout
->find_or_add_kept_section(sig2
, this, index
, false,
1192 // We are not including this section because we already saw the
1193 // name of the section as a signature. This normally implies
1194 // that the kept section is another linkonce section. If it is
1195 // the same size, record it as the section which corresponds to
1197 if (kept2
->object() != NULL
1198 && !kept2
->is_comdat()
1199 && kept2
->linkonce_size() == sh_size
)
1200 this->set_kept_comdat_section(index
, kept2
->object(), kept2
->shndx());
1204 // The section is being discarded on the basis of its symbol
1205 // name. This means that the corresponding kept section was
1206 // part of a comdat group, and it will be difficult to identify
1207 // the specific section within that group that corresponds to
1208 // this linkonce section. We'll handle the simple case where
1209 // the group has only one member section. Otherwise, it's not
1210 // worth the effort.
1211 unsigned int kept_shndx
;
1213 if (kept1
->object() != NULL
1214 && kept1
->is_comdat()
1215 && kept1
->find_single_comdat_section(&kept_shndx
, &kept_size
)
1216 && kept_size
== sh_size
)
1217 this->set_kept_comdat_section(index
, kept1
->object(), kept_shndx
);
1221 kept1
->set_linkonce_size(sh_size
);
1222 kept2
->set_linkonce_size(sh_size
);
1225 return include1
&& include2
;
1228 // Layout an input section.
1230 template<int size
, bool big_endian
>
1232 Sized_relobj_file
<size
, big_endian
>::layout_section(
1236 const typename
This::Shdr
& shdr
,
1237 unsigned int reloc_shndx
,
1238 unsigned int reloc_type
)
1241 Output_section
* os
= layout
->layout(this, shndx
, name
, shdr
,
1242 reloc_shndx
, reloc_type
, &offset
);
1244 this->output_sections()[shndx
] = os
;
1246 this->section_offsets()[shndx
] = invalid_address
;
1248 this->section_offsets()[shndx
] = convert_types
<Address
, off_t
>(offset
);
1250 // If this section requires special handling, and if there are
1251 // relocs that apply to it, then we must do the special handling
1252 // before we apply the relocs.
1253 if (offset
== -1 && reloc_shndx
!= 0)
1254 this->set_relocs_must_follow_section_writes();
1257 // Layout an input .eh_frame section.
1259 template<int size
, bool big_endian
>
1261 Sized_relobj_file
<size
, big_endian
>::layout_eh_frame_section(
1263 const unsigned char* symbols_data
,
1264 section_size_type symbols_size
,
1265 const unsigned char* symbol_names_data
,
1266 section_size_type symbol_names_size
,
1268 const typename
This::Shdr
& shdr
,
1269 unsigned int reloc_shndx
,
1270 unsigned int reloc_type
)
1272 gold_assert(this->has_eh_frame_
);
1275 Output_section
* os
= layout
->layout_eh_frame(this,
1285 this->output_sections()[shndx
] = os
;
1286 if (os
== NULL
|| offset
== -1)
1288 // An object can contain at most one section holding exception
1289 // frame information.
1290 gold_assert(this->discarded_eh_frame_shndx_
== -1U);
1291 this->discarded_eh_frame_shndx_
= shndx
;
1292 this->section_offsets()[shndx
] = invalid_address
;
1295 this->section_offsets()[shndx
] = convert_types
<Address
, off_t
>(offset
);
1297 // If this section requires special handling, and if there are
1298 // relocs that aply to it, then we must do the special handling
1299 // before we apply the relocs.
1300 if (os
!= NULL
&& offset
== -1 && reloc_shndx
!= 0)
1301 this->set_relocs_must_follow_section_writes();
1304 // Lay out the input sections. We walk through the sections and check
1305 // whether they should be included in the link. If they should, we
1306 // pass them to the Layout object, which will return an output section
1308 // This function is called twice sometimes, two passes, when mapping
1309 // of input sections to output sections must be delayed.
1310 // This is true for the following :
1311 // * Garbage collection (--gc-sections): Some input sections will be
1312 // discarded and hence the assignment must wait until the second pass.
1313 // In the first pass, it is for setting up some sections as roots to
1314 // a work-list for --gc-sections and to do comdat processing.
1315 // * Identical Code Folding (--icf=<safe,all>): Some input sections
1316 // will be folded and hence the assignment must wait.
1317 // * Using plugins to map some sections to unique segments: Mapping
1318 // some sections to unique segments requires mapping them to unique
1319 // output sections too. This can be done via plugins now and this
1320 // information is not available in the first pass.
1322 template<int size
, bool big_endian
>
1324 Sized_relobj_file
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
1326 Read_symbols_data
* sd
)
1328 const unsigned int shnum
= this->shnum();
1330 /* Should this function be called twice? */
1331 bool is_two_pass
= (parameters
->options().gc_sections()
1332 || parameters
->options().icf_enabled()
1333 || layout
->is_unique_segment_for_sections_specified());
1335 /* Only one of is_pass_one and is_pass_two is true. Both are false when
1336 a two-pass approach is not needed. */
1337 bool is_pass_one
= false;
1338 bool is_pass_two
= false;
1340 Symbols_data
* gc_sd
= NULL
;
1342 /* Check if do_layout needs to be two-pass. If so, find out which pass
1343 should happen. In the first pass, the data in sd is saved to be used
1344 later in the second pass. */
1347 gc_sd
= this->get_symbols_data();
1350 gold_assert(sd
!= NULL
);
1355 if (parameters
->options().gc_sections())
1356 gold_assert(symtab
->gc()->is_worklist_ready());
1357 if (parameters
->options().icf_enabled())
1358 gold_assert(symtab
->icf()->is_icf_ready());
1368 // During garbage collection save the symbols data to use it when
1369 // re-entering this function.
1370 gc_sd
= new Symbols_data
;
1371 this->copy_symbols_data(gc_sd
, sd
, This::shdr_size
* shnum
);
1372 this->set_symbols_data(gc_sd
);
1375 const unsigned char* section_headers_data
= NULL
;
1376 section_size_type section_names_size
;
1377 const unsigned char* symbols_data
= NULL
;
1378 section_size_type symbols_size
;
1379 const unsigned char* symbol_names_data
= NULL
;
1380 section_size_type symbol_names_size
;
1384 section_headers_data
= gc_sd
->section_headers_data
;
1385 section_names_size
= gc_sd
->section_names_size
;
1386 symbols_data
= gc_sd
->symbols_data
;
1387 symbols_size
= gc_sd
->symbols_size
;
1388 symbol_names_data
= gc_sd
->symbol_names_data
;
1389 symbol_names_size
= gc_sd
->symbol_names_size
;
1393 section_headers_data
= sd
->section_headers
->data();
1394 section_names_size
= sd
->section_names_size
;
1395 if (sd
->symbols
!= NULL
)
1396 symbols_data
= sd
->symbols
->data();
1397 symbols_size
= sd
->symbols_size
;
1398 if (sd
->symbol_names
!= NULL
)
1399 symbol_names_data
= sd
->symbol_names
->data();
1400 symbol_names_size
= sd
->symbol_names_size
;
1403 // Get the section headers.
1404 const unsigned char* shdrs
= section_headers_data
;
1405 const unsigned char* pshdrs
;
1407 // Get the section names.
1408 const unsigned char* pnamesu
= (is_two_pass
1409 ? gc_sd
->section_names_data
1410 : sd
->section_names
->data());
1412 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1414 // If any input files have been claimed by plugins, we need to defer
1415 // actual layout until the replacement files have arrived.
1416 const bool should_defer_layout
=
1417 (parameters
->options().has_plugins()
1418 && parameters
->options().plugins()->should_defer_layout());
1419 unsigned int num_sections_to_defer
= 0;
1421 // For each section, record the index of the reloc section if any.
1422 // Use 0 to mean that there is no reloc section, -1U to mean that
1423 // there is more than one.
1424 std::vector
<unsigned int> reloc_shndx(shnum
, 0);
1425 std::vector
<unsigned int> reloc_type(shnum
, elfcpp::SHT_NULL
);
1426 // Skip the first, dummy, section.
1427 pshdrs
= shdrs
+ This::shdr_size
;
1428 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
1430 typename
This::Shdr
shdr(pshdrs
);
1432 // Count the number of sections whose layout will be deferred.
1433 if (should_defer_layout
&& (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1434 ++num_sections_to_defer
;
1436 unsigned int sh_type
= shdr
.get_sh_type();
1437 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
1439 unsigned int target_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1440 if (target_shndx
== 0 || target_shndx
>= shnum
)
1442 this->error(_("relocation section %u has bad info %u"),
1447 if (reloc_shndx
[target_shndx
] != 0)
1448 reloc_shndx
[target_shndx
] = -1U;
1451 reloc_shndx
[target_shndx
] = i
;
1452 reloc_type
[target_shndx
] = sh_type
;
1457 Output_sections
& out_sections(this->output_sections());
1458 std::vector
<Address
>& out_section_offsets(this->section_offsets());
1462 out_sections
.resize(shnum
);
1463 out_section_offsets
.resize(shnum
);
1466 // If we are only linking for symbols, then there is nothing else to
1468 if (this->input_file()->just_symbols())
1472 delete sd
->section_headers
;
1473 sd
->section_headers
= NULL
;
1474 delete sd
->section_names
;
1475 sd
->section_names
= NULL
;
1480 if (num_sections_to_defer
> 0)
1482 parameters
->options().plugins()->add_deferred_layout_object(this);
1483 this->deferred_layout_
.reserve(num_sections_to_defer
);
1484 this->is_deferred_layout_
= true;
1487 // Whether we've seen a .note.GNU-stack section.
1488 bool seen_gnu_stack
= false;
1489 // The flags of a .note.GNU-stack section.
1490 uint64_t gnu_stack_flags
= 0;
1492 // Keep track of which sections to omit.
1493 std::vector
<bool> omit(shnum
, false);
1495 // Keep track of reloc sections when emitting relocations.
1496 const bool relocatable
= parameters
->options().relocatable();
1497 const bool emit_relocs
= (relocatable
1498 || parameters
->options().emit_relocs());
1499 std::vector
<unsigned int> reloc_sections
;
1501 // Keep track of .eh_frame sections.
1502 std::vector
<unsigned int> eh_frame_sections
;
1504 // Keep track of .debug_info and .debug_types sections.
1505 std::vector
<unsigned int> debug_info_sections
;
1506 std::vector
<unsigned int> debug_types_sections
;
1508 // Skip the first, dummy, section.
1509 pshdrs
= shdrs
+ This::shdr_size
;
1510 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
1512 typename
This::Shdr
shdr(pshdrs
);
1514 if (shdr
.get_sh_name() >= section_names_size
)
1516 this->error(_("bad section name offset for section %u: %lu"),
1517 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
1521 const char* name
= pnames
+ shdr
.get_sh_name();
1525 if (this->handle_gnu_warning_section(name
, i
, symtab
))
1527 if (!relocatable
&& !parameters
->options().shared())
1531 // The .note.GNU-stack section is special. It gives the
1532 // protection flags that this object file requires for the stack
1534 if (strcmp(name
, ".note.GNU-stack") == 0)
1536 seen_gnu_stack
= true;
1537 gnu_stack_flags
|= shdr
.get_sh_flags();
1541 // The .note.GNU-split-stack section is also special. It
1542 // indicates that the object was compiled with
1544 if (this->handle_split_stack_section(name
))
1546 if (!relocatable
&& !parameters
->options().shared())
1550 // Skip attributes section.
1551 if (parameters
->target().is_attributes_section(name
))
1556 bool discard
= omit
[i
];
1559 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
1561 if (!this->include_section_group(symtab
, layout
, i
, name
,
1567 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
1568 && Layout::is_linkonce(name
))
1570 if (!this->include_linkonce_section(layout
, i
, name
, shdr
))
1575 // Add the section to the incremental inputs layout.
1576 Incremental_inputs
* incremental_inputs
= layout
->incremental_inputs();
1577 if (incremental_inputs
!= NULL
1579 && can_incremental_update(shdr
.get_sh_type()))
1581 off_t sh_size
= shdr
.get_sh_size();
1582 section_size_type uncompressed_size
;
1583 if (this->section_is_compressed(i
, &uncompressed_size
))
1584 sh_size
= uncompressed_size
;
1585 incremental_inputs
->report_input_section(this, i
, name
, sh_size
);
1590 // Do not include this section in the link.
1591 out_sections
[i
] = NULL
;
1592 out_section_offsets
[i
] = invalid_address
;
1597 if (is_pass_one
&& parameters
->options().gc_sections())
1599 if (this->is_section_name_included(name
)
1600 || layout
->keep_input_section (this, name
)
1601 || shdr
.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1602 || shdr
.get_sh_type() == elfcpp::SHT_FINI_ARRAY
)
1604 symtab
->gc()->worklist().push(Section_id(this, i
));
1606 // If the section name XXX can be represented as a C identifier
1607 // it cannot be discarded if there are references to
1608 // __start_XXX and __stop_XXX symbols. These need to be
1609 // specially handled.
1610 if (is_cident(name
))
1612 symtab
->gc()->add_cident_section(name
, Section_id(this, i
));
1616 // When doing a relocatable link we are going to copy input
1617 // reloc sections into the output. We only want to copy the
1618 // ones associated with sections which are not being discarded.
1619 // However, we don't know that yet for all sections. So save
1620 // reloc sections and process them later. Garbage collection is
1621 // not triggered when relocatable code is desired.
1623 && (shdr
.get_sh_type() == elfcpp::SHT_REL
1624 || shdr
.get_sh_type() == elfcpp::SHT_RELA
))
1626 reloc_sections
.push_back(i
);
1630 if (relocatable
&& shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
1633 // The .eh_frame section is special. It holds exception frame
1634 // information that we need to read in order to generate the
1635 // exception frame header. We process these after all the other
1636 // sections so that the exception frame reader can reliably
1637 // determine which sections are being discarded, and discard the
1638 // corresponding information.
1640 && strcmp(name
, ".eh_frame") == 0
1641 && this->check_eh_frame_flags(&shdr
))
1645 if (this->is_deferred_layout())
1646 out_sections
[i
] = reinterpret_cast<Output_section
*>(2);
1648 out_sections
[i
] = reinterpret_cast<Output_section
*>(1);
1649 out_section_offsets
[i
] = invalid_address
;
1651 else if (this->is_deferred_layout())
1652 this->deferred_layout_
.push_back(Deferred_layout(i
, name
,
1657 eh_frame_sections
.push_back(i
);
1661 if (is_pass_two
&& parameters
->options().gc_sections())
1663 // This is executed during the second pass of garbage
1664 // collection. do_layout has been called before and some
1665 // sections have been already discarded. Simply ignore
1666 // such sections this time around.
1667 if (out_sections
[i
] == NULL
)
1669 gold_assert(out_section_offsets
[i
] == invalid_address
);
1672 if (((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1673 && symtab
->gc()->is_section_garbage(this, i
))
1675 if (parameters
->options().print_gc_sections())
1676 gold_info(_("%s: removing unused section from '%s'"
1678 program_name
, this->section_name(i
).c_str(),
1679 this->name().c_str());
1680 out_sections
[i
] = NULL
;
1681 out_section_offsets
[i
] = invalid_address
;
1686 if (is_pass_two
&& parameters
->options().icf_enabled())
1688 if (out_sections
[i
] == NULL
)
1690 gold_assert(out_section_offsets
[i
] == invalid_address
);
1693 if (((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1694 && symtab
->icf()->is_section_folded(this, i
))
1696 if (parameters
->options().print_icf_sections())
1699 symtab
->icf()->get_folded_section(this, i
);
1700 Relobj
* folded_obj
=
1701 reinterpret_cast<Relobj
*>(folded
.first
);
1702 gold_info(_("%s: ICF folding section '%s' in file '%s' "
1703 "into '%s' in file '%s'"),
1704 program_name
, this->section_name(i
).c_str(),
1705 this->name().c_str(),
1706 folded_obj
->section_name(folded
.second
).c_str(),
1707 folded_obj
->name().c_str());
1709 out_sections
[i
] = NULL
;
1710 out_section_offsets
[i
] = invalid_address
;
1715 // Defer layout here if input files are claimed by plugins. When gc
1716 // is turned on this function is called twice; we only want to do this
1717 // on the first pass.
1719 && this->is_deferred_layout()
1720 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1722 this->deferred_layout_
.push_back(Deferred_layout(i
, name
,
1726 // Put dummy values here; real values will be supplied by
1727 // do_layout_deferred_sections.
1728 out_sections
[i
] = reinterpret_cast<Output_section
*>(2);
1729 out_section_offsets
[i
] = invalid_address
;
1733 // During gc_pass_two if a section that was previously deferred is
1734 // found, do not layout the section as layout_deferred_sections will
1735 // do it later from gold.cc.
1737 && (out_sections
[i
] == reinterpret_cast<Output_section
*>(2)))
1742 // This is during garbage collection. The out_sections are
1743 // assigned in the second call to this function.
1744 out_sections
[i
] = reinterpret_cast<Output_section
*>(1);
1745 out_section_offsets
[i
] = invalid_address
;
1749 // When garbage collection is switched on the actual layout
1750 // only happens in the second call.
1751 this->layout_section(layout
, i
, name
, shdr
, reloc_shndx
[i
],
1754 // When generating a .gdb_index section, we do additional
1755 // processing of .debug_info and .debug_types sections after all
1756 // the other sections for the same reason as above.
1758 && parameters
->options().gdb_index()
1759 && !(shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1761 if (strcmp(name
, ".debug_info") == 0
1762 || strcmp(name
, ".zdebug_info") == 0)
1763 debug_info_sections
.push_back(i
);
1764 else if (strcmp(name
, ".debug_types") == 0
1765 || strcmp(name
, ".zdebug_types") == 0)
1766 debug_types_sections
.push_back(i
);
1772 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
, this);
1774 // Handle the .eh_frame sections after the other sections.
1775 gold_assert(!is_pass_one
|| eh_frame_sections
.empty());
1776 for (std::vector
<unsigned int>::const_iterator p
= eh_frame_sections
.begin();
1777 p
!= eh_frame_sections
.end();
1780 unsigned int i
= *p
;
1781 const unsigned char* pshdr
;
1782 pshdr
= section_headers_data
+ i
* This::shdr_size
;
1783 typename
This::Shdr
shdr(pshdr
);
1785 this->layout_eh_frame_section(layout
,
1796 // When doing a relocatable link handle the reloc sections at the
1797 // end. Garbage collection and Identical Code Folding is not
1798 // turned on for relocatable code.
1800 this->size_relocatable_relocs();
1802 gold_assert(!is_two_pass
|| reloc_sections
.empty());
1804 for (std::vector
<unsigned int>::const_iterator p
= reloc_sections
.begin();
1805 p
!= reloc_sections
.end();
1808 unsigned int i
= *p
;
1809 const unsigned char* pshdr
;
1810 pshdr
= section_headers_data
+ i
* This::shdr_size
;
1811 typename
This::Shdr
shdr(pshdr
);
1813 unsigned int data_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1814 if (data_shndx
>= shnum
)
1816 // We already warned about this above.
1820 Output_section
* data_section
= out_sections
[data_shndx
];
1821 if (data_section
== reinterpret_cast<Output_section
*>(2))
1825 // The layout for the data section was deferred, so we need
1826 // to defer the relocation section, too.
1827 const char* name
= pnames
+ shdr
.get_sh_name();
1828 this->deferred_layout_relocs_
.push_back(
1829 Deferred_layout(i
, name
, pshdr
, 0, elfcpp::SHT_NULL
));
1830 out_sections
[i
] = reinterpret_cast<Output_section
*>(2);
1831 out_section_offsets
[i
] = invalid_address
;
1834 if (data_section
== NULL
)
1836 out_sections
[i
] = NULL
;
1837 out_section_offsets
[i
] = invalid_address
;
1841 Relocatable_relocs
* rr
= new Relocatable_relocs();
1842 this->set_relocatable_relocs(i
, rr
);
1844 Output_section
* os
= layout
->layout_reloc(this, i
, shdr
, data_section
,
1846 out_sections
[i
] = os
;
1847 out_section_offsets
[i
] = invalid_address
;
1850 // When building a .gdb_index section, scan the .debug_info and
1851 // .debug_types sections.
1852 gold_assert(!is_pass_one
1853 || (debug_info_sections
.empty() && debug_types_sections
.empty()));
1854 for (std::vector
<unsigned int>::const_iterator p
1855 = debug_info_sections
.begin();
1856 p
!= debug_info_sections
.end();
1859 unsigned int i
= *p
;
1860 layout
->add_to_gdb_index(false, this, symbols_data
, symbols_size
,
1861 i
, reloc_shndx
[i
], reloc_type
[i
]);
1863 for (std::vector
<unsigned int>::const_iterator p
1864 = debug_types_sections
.begin();
1865 p
!= debug_types_sections
.end();
1868 unsigned int i
= *p
;
1869 layout
->add_to_gdb_index(true, this, symbols_data
, symbols_size
,
1870 i
, reloc_shndx
[i
], reloc_type
[i
]);
1875 delete[] gc_sd
->section_headers_data
;
1876 delete[] gc_sd
->section_names_data
;
1877 delete[] gc_sd
->symbols_data
;
1878 delete[] gc_sd
->symbol_names_data
;
1879 this->set_symbols_data(NULL
);
1883 delete sd
->section_headers
;
1884 sd
->section_headers
= NULL
;
1885 delete sd
->section_names
;
1886 sd
->section_names
= NULL
;
1890 // Layout sections whose layout was deferred while waiting for
1891 // input files from a plugin.
1893 template<int size
, bool big_endian
>
1895 Sized_relobj_file
<size
, big_endian
>::do_layout_deferred_sections(Layout
* layout
)
1897 typename
std::vector
<Deferred_layout
>::iterator deferred
;
1899 for (deferred
= this->deferred_layout_
.begin();
1900 deferred
!= this->deferred_layout_
.end();
1903 typename
This::Shdr
shdr(deferred
->shdr_data_
);
1905 if (!parameters
->options().relocatable()
1906 && deferred
->name_
== ".eh_frame"
1907 && this->check_eh_frame_flags(&shdr
))
1909 // Checking is_section_included is not reliable for
1910 // .eh_frame sections, because they do not have an output
1911 // section. This is not a problem normally because we call
1912 // layout_eh_frame_section unconditionally, but when
1913 // deferring sections that is not true. We don't want to
1914 // keep all .eh_frame sections because that will cause us to
1915 // keep all sections that they refer to, which is the wrong
1916 // way around. Instead, the eh_frame code will discard
1917 // .eh_frame sections that refer to discarded sections.
1919 // Reading the symbols again here may be slow.
1920 Read_symbols_data sd
;
1921 this->base_read_symbols(&sd
);
1922 this->layout_eh_frame_section(layout
,
1925 sd
.symbol_names
->data(),
1926 sd
.symbol_names_size
,
1929 deferred
->reloc_shndx_
,
1930 deferred
->reloc_type_
);
1934 // If the section is not included, it is because the garbage collector
1935 // decided it is not needed. Avoid reverting that decision.
1936 if (!this->is_section_included(deferred
->shndx_
))
1939 this->layout_section(layout
, deferred
->shndx_
, deferred
->name_
.c_str(),
1940 shdr
, deferred
->reloc_shndx_
,
1941 deferred
->reloc_type_
);
1944 this->deferred_layout_
.clear();
1946 // Now handle the deferred relocation sections.
1948 Output_sections
& out_sections(this->output_sections());
1949 std::vector
<Address
>& out_section_offsets(this->section_offsets());
1951 for (deferred
= this->deferred_layout_relocs_
.begin();
1952 deferred
!= this->deferred_layout_relocs_
.end();
1955 unsigned int shndx
= deferred
->shndx_
;
1956 typename
This::Shdr
shdr(deferred
->shdr_data_
);
1957 unsigned int data_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1959 Output_section
* data_section
= out_sections
[data_shndx
];
1960 if (data_section
== NULL
)
1962 out_sections
[shndx
] = NULL
;
1963 out_section_offsets
[shndx
] = invalid_address
;
1967 Relocatable_relocs
* rr
= new Relocatable_relocs();
1968 this->set_relocatable_relocs(shndx
, rr
);
1970 Output_section
* os
= layout
->layout_reloc(this, shndx
, shdr
,
1972 out_sections
[shndx
] = os
;
1973 out_section_offsets
[shndx
] = invalid_address
;
1977 // Add the symbols to the symbol table.
1979 template<int size
, bool big_endian
>
1981 Sized_relobj_file
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1982 Read_symbols_data
* sd
,
1985 if (sd
->symbols
== NULL
)
1987 gold_assert(sd
->symbol_names
== NULL
);
1991 const int sym_size
= This::sym_size
;
1992 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
1994 if (symcount
* sym_size
!= sd
->symbols_size
- sd
->external_symbols_offset
)
1996 this->error(_("size of symbols is not multiple of symbol size"));
2000 this->symbols_
.resize(symcount
);
2002 const char* sym_names
=
2003 reinterpret_cast<const char*>(sd
->symbol_names
->data());
2004 symtab
->add_from_relobj(this,
2005 sd
->symbols
->data() + sd
->external_symbols_offset
,
2006 symcount
, this->local_symbol_count_
,
2007 sym_names
, sd
->symbol_names_size
,
2009 &this->defined_count_
);
2013 delete sd
->symbol_names
;
2014 sd
->symbol_names
= NULL
;
2017 // Find out if this object, that is a member of a lib group, should be included
2018 // in the link. We check every symbol defined by this object. If the symbol
2019 // table has a strong undefined reference to that symbol, we have to include
2022 template<int size
, bool big_endian
>
2023 Archive::Should_include
2024 Sized_relobj_file
<size
, big_endian
>::do_should_include_member(
2025 Symbol_table
* symtab
,
2027 Read_symbols_data
* sd
,
2030 char* tmpbuf
= NULL
;
2031 size_t tmpbuflen
= 0;
2032 const char* sym_names
=
2033 reinterpret_cast<const char*>(sd
->symbol_names
->data());
2034 const unsigned char* syms
=
2035 sd
->symbols
->data() + sd
->external_symbols_offset
;
2036 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2037 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
2040 const unsigned char* p
= syms
;
2042 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
2044 elfcpp::Sym
<size
, big_endian
> sym(p
);
2045 unsigned int st_shndx
= sym
.get_st_shndx();
2046 if (st_shndx
== elfcpp::SHN_UNDEF
)
2049 unsigned int st_name
= sym
.get_st_name();
2050 const char* name
= sym_names
+ st_name
;
2052 Archive::Should_include t
= Archive::should_include_member(symtab
,
2058 if (t
== Archive::SHOULD_INCLUDE_YES
)
2067 return Archive::SHOULD_INCLUDE_UNKNOWN
;
2070 // Iterate over global defined symbols, calling a visitor class V for each.
2072 template<int size
, bool big_endian
>
2074 Sized_relobj_file
<size
, big_endian
>::do_for_all_global_symbols(
2075 Read_symbols_data
* sd
,
2076 Library_base::Symbol_visitor_base
* v
)
2078 const char* sym_names
=
2079 reinterpret_cast<const char*>(sd
->symbol_names
->data());
2080 const unsigned char* syms
=
2081 sd
->symbols
->data() + sd
->external_symbols_offset
;
2082 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2083 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
2085 const unsigned char* p
= syms
;
2087 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
2089 elfcpp::Sym
<size
, big_endian
> sym(p
);
2090 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
2091 v
->visit(sym_names
+ sym
.get_st_name());
2095 // Return whether the local symbol SYMNDX has a PLT offset.
2097 template<int size
, bool big_endian
>
2099 Sized_relobj_file
<size
, big_endian
>::local_has_plt_offset(
2100 unsigned int symndx
) const
2102 typename
Local_plt_offsets::const_iterator p
=
2103 this->local_plt_offsets_
.find(symndx
);
2104 return p
!= this->local_plt_offsets_
.end();
2107 // Get the PLT offset of a local symbol.
2109 template<int size
, bool big_endian
>
2111 Sized_relobj_file
<size
, big_endian
>::do_local_plt_offset(
2112 unsigned int symndx
) const
2114 typename
Local_plt_offsets::const_iterator p
=
2115 this->local_plt_offsets_
.find(symndx
);
2116 gold_assert(p
!= this->local_plt_offsets_
.end());
2120 // Set the PLT offset of a local symbol.
2122 template<int size
, bool big_endian
>
2124 Sized_relobj_file
<size
, big_endian
>::set_local_plt_offset(
2125 unsigned int symndx
, unsigned int plt_offset
)
2127 std::pair
<typename
Local_plt_offsets::iterator
, bool> ins
=
2128 this->local_plt_offsets_
.insert(std::make_pair(symndx
, plt_offset
));
2129 gold_assert(ins
.second
);
2132 // First pass over the local symbols. Here we add their names to
2133 // *POOL and *DYNPOOL, and we store the symbol value in
2134 // THIS->LOCAL_VALUES_. This function is always called from a
2135 // singleton thread. This is followed by a call to
2136 // finalize_local_symbols.
2138 template<int size
, bool big_endian
>
2140 Sized_relobj_file
<size
, big_endian
>::do_count_local_symbols(Stringpool
* pool
,
2141 Stringpool
* dynpool
)
2143 gold_assert(this->symtab_shndx_
!= -1U);
2144 if (this->symtab_shndx_
== 0)
2146 // This object has no symbols. Weird but legal.
2150 // Read the symbol table section header.
2151 const unsigned int symtab_shndx
= this->symtab_shndx_
;
2152 typename
This::Shdr
symtabshdr(this,
2153 this->elf_file_
.section_header(symtab_shndx
));
2154 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
2156 // Read the local symbols.
2157 const int sym_size
= This::sym_size
;
2158 const unsigned int loccount
= this->local_symbol_count_
;
2159 gold_assert(loccount
== symtabshdr
.get_sh_info());
2160 off_t locsize
= loccount
* sym_size
;
2161 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
2162 locsize
, true, true);
2164 // Read the symbol names.
2165 const unsigned int strtab_shndx
=
2166 this->adjust_shndx(symtabshdr
.get_sh_link());
2167 section_size_type strtab_size
;
2168 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
2171 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
2173 // Loop over the local symbols.
2175 const Output_sections
& out_sections(this->output_sections());
2176 unsigned int shnum
= this->shnum();
2177 unsigned int count
= 0;
2178 unsigned int dyncount
= 0;
2179 // Skip the first, dummy, symbol.
2181 bool strip_all
= parameters
->options().strip_all();
2182 bool discard_all
= parameters
->options().discard_all();
2183 bool discard_locals
= parameters
->options().discard_locals();
2184 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2186 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2188 Symbol_value
<size
>& lv(this->local_values_
[i
]);
2191 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
2193 lv
.set_input_shndx(shndx
, is_ordinary
);
2195 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
2196 lv
.set_is_section_symbol();
2197 else if (sym
.get_st_type() == elfcpp::STT_TLS
)
2198 lv
.set_is_tls_symbol();
2199 else if (sym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
2200 lv
.set_is_ifunc_symbol();
2202 // Save the input symbol value for use in do_finalize_local_symbols().
2203 lv
.set_input_value(sym
.get_st_value());
2205 // Decide whether this symbol should go into the output file.
2207 if ((shndx
< shnum
&& out_sections
[shndx
] == NULL
)
2208 || shndx
== this->discarded_eh_frame_shndx_
)
2210 lv
.set_no_output_symtab_entry();
2211 gold_assert(!lv
.needs_output_dynsym_entry());
2215 if (sym
.get_st_type() == elfcpp::STT_SECTION
2216 || !this->adjust_local_symbol(&lv
))
2218 lv
.set_no_output_symtab_entry();
2219 gold_assert(!lv
.needs_output_dynsym_entry());
2223 if (sym
.get_st_name() >= strtab_size
)
2225 this->error(_("local symbol %u section name out of range: %u >= %u"),
2226 i
, sym
.get_st_name(),
2227 static_cast<unsigned int>(strtab_size
));
2228 lv
.set_no_output_symtab_entry();
2232 const char* name
= pnames
+ sym
.get_st_name();
2234 // If needed, add the symbol to the dynamic symbol table string pool.
2235 if (lv
.needs_output_dynsym_entry())
2237 dynpool
->add(name
, true, NULL
);
2242 || (discard_all
&& lv
.may_be_discarded_from_output_symtab()))
2244 lv
.set_no_output_symtab_entry();
2248 // If --discard-locals option is used, discard all temporary local
2249 // symbols. These symbols start with system-specific local label
2250 // prefixes, typically .L for ELF system. We want to be compatible
2251 // with GNU ld so here we essentially use the same check in
2252 // bfd_is_local_label(). The code is different because we already
2255 // - the symbol is local and thus cannot have global or weak binding.
2256 // - the symbol is not a section symbol.
2257 // - the symbol has a name.
2259 // We do not discard a symbol if it needs a dynamic symbol entry.
2261 && sym
.get_st_type() != elfcpp::STT_FILE
2262 && !lv
.needs_output_dynsym_entry()
2263 && lv
.may_be_discarded_from_output_symtab()
2264 && parameters
->target().is_local_label_name(name
))
2266 lv
.set_no_output_symtab_entry();
2270 // Discard the local symbol if -retain_symbols_file is specified
2271 // and the local symbol is not in that file.
2272 if (!parameters
->options().should_retain_symbol(name
))
2274 lv
.set_no_output_symtab_entry();
2278 // Add the symbol to the symbol table string pool.
2279 pool
->add(name
, true, NULL
);
2283 this->output_local_symbol_count_
= count
;
2284 this->output_local_dynsym_count_
= dyncount
;
2287 // Compute the final value of a local symbol.
2289 template<int size
, bool big_endian
>
2290 typename Sized_relobj_file
<size
, big_endian
>::Compute_final_local_value_status
2291 Sized_relobj_file
<size
, big_endian
>::compute_final_local_value_internal(
2293 const Symbol_value
<size
>* lv_in
,
2294 Symbol_value
<size
>* lv_out
,
2296 const Output_sections
& out_sections
,
2297 const std::vector
<Address
>& out_offsets
,
2298 const Symbol_table
* symtab
)
2300 // We are going to overwrite *LV_OUT, if it has a merged symbol value,
2301 // we may have a memory leak.
2302 gold_assert(lv_out
->has_output_value());
2305 unsigned int shndx
= lv_in
->input_shndx(&is_ordinary
);
2307 // Set the output symbol value.
2311 if (shndx
== elfcpp::SHN_ABS
|| Symbol::is_common_shndx(shndx
))
2312 lv_out
->set_output_value(lv_in
->input_value());
2315 this->error(_("unknown section index %u for local symbol %u"),
2317 lv_out
->set_output_value(0);
2318 return This::CFLV_ERROR
;
2323 if (shndx
>= this->shnum())
2325 this->error(_("local symbol %u section index %u out of range"),
2327 lv_out
->set_output_value(0);
2328 return This::CFLV_ERROR
;
2331 Output_section
* os
= out_sections
[shndx
];
2332 Address secoffset
= out_offsets
[shndx
];
2333 if (symtab
->is_section_folded(this, shndx
))
2335 gold_assert(os
== NULL
&& secoffset
== invalid_address
);
2336 // Get the os of the section it is folded onto.
2337 Section_id folded
= symtab
->icf()->get_folded_section(this,
2339 gold_assert(folded
.first
!= NULL
);
2340 Sized_relobj_file
<size
, big_endian
>* folded_obj
= reinterpret_cast
2341 <Sized_relobj_file
<size
, big_endian
>*>(folded
.first
);
2342 os
= folded_obj
->output_section(folded
.second
);
2343 gold_assert(os
!= NULL
);
2344 secoffset
= folded_obj
->get_output_section_offset(folded
.second
);
2346 // This could be a relaxed input section.
2347 if (secoffset
== invalid_address
)
2349 const Output_relaxed_input_section
* relaxed_section
=
2350 os
->find_relaxed_input_section(folded_obj
, folded
.second
);
2351 gold_assert(relaxed_section
!= NULL
);
2352 secoffset
= relaxed_section
->address() - os
->address();
2358 // This local symbol belongs to a section we are discarding.
2359 // In some cases when applying relocations later, we will
2360 // attempt to match it to the corresponding kept section,
2361 // so we leave the input value unchanged here.
2362 return This::CFLV_DISCARDED
;
2364 else if (secoffset
== invalid_address
)
2368 // This is a SHF_MERGE section or one which otherwise
2369 // requires special handling.
2370 if (shndx
== this->discarded_eh_frame_shndx_
)
2372 // This local symbol belongs to a discarded .eh_frame
2373 // section. Just treat it like the case in which
2374 // os == NULL above.
2375 gold_assert(this->has_eh_frame_
);
2376 return This::CFLV_DISCARDED
;
2378 else if (!lv_in
->is_section_symbol())
2380 // This is not a section symbol. We can determine
2381 // the final value now.
2382 lv_out
->set_output_value(
2383 os
->output_address(this, shndx
, lv_in
->input_value()));
2385 else if (!os
->find_starting_output_address(this, shndx
, &start
))
2387 // This is a section symbol, but apparently not one in a
2388 // merged section. First check to see if this is a relaxed
2389 // input section. If so, use its address. Otherwise just
2390 // use the start of the output section. This happens with
2391 // relocatable links when the input object has section
2392 // symbols for arbitrary non-merge sections.
2393 const Output_section_data
* posd
=
2394 os
->find_relaxed_input_section(this, shndx
);
2397 Address relocatable_link_adjustment
=
2398 relocatable
? os
->address() : 0;
2399 lv_out
->set_output_value(posd
->address()
2400 - relocatable_link_adjustment
);
2403 lv_out
->set_output_value(os
->address());
2407 // We have to consider the addend to determine the
2408 // value to use in a relocation. START is the start
2409 // of this input section. If we are doing a relocatable
2410 // link, use offset from start output section instead of
2412 Address adjusted_start
=
2413 relocatable
? start
- os
->address() : start
;
2414 Merged_symbol_value
<size
>* msv
=
2415 new Merged_symbol_value
<size
>(lv_in
->input_value(),
2417 lv_out
->set_merged_symbol_value(msv
);
2420 else if (lv_in
->is_tls_symbol()
2421 || (lv_in
->is_section_symbol()
2422 && (os
->flags() & elfcpp::SHF_TLS
)))
2423 lv_out
->set_output_value(os
->tls_offset()
2425 + lv_in
->input_value());
2427 lv_out
->set_output_value((relocatable
? 0 : os
->address())
2429 + lv_in
->input_value());
2431 return This::CFLV_OK
;
2434 // Compute final local symbol value. R_SYM is the index of a local
2435 // symbol in symbol table. LV points to a symbol value, which is
2436 // expected to hold the input value and to be over-written by the
2437 // final value. SYMTAB points to a symbol table. Some targets may want
2438 // to know would-be-finalized local symbol values in relaxation.
2439 // Hence we provide this method. Since this method updates *LV, a
2440 // callee should make a copy of the original local symbol value and
2441 // use the copy instead of modifying an object's local symbols before
2442 // everything is finalized. The caller should also free up any allocated
2443 // memory in the return value in *LV.
2444 template<int size
, bool big_endian
>
2445 typename Sized_relobj_file
<size
, big_endian
>::Compute_final_local_value_status
2446 Sized_relobj_file
<size
, big_endian
>::compute_final_local_value(
2448 const Symbol_value
<size
>* lv_in
,
2449 Symbol_value
<size
>* lv_out
,
2450 const Symbol_table
* symtab
)
2452 // This is just a wrapper of compute_final_local_value_internal.
2453 const bool relocatable
= parameters
->options().relocatable();
2454 const Output_sections
& out_sections(this->output_sections());
2455 const std::vector
<Address
>& out_offsets(this->section_offsets());
2456 return this->compute_final_local_value_internal(r_sym
, lv_in
, lv_out
,
2457 relocatable
, out_sections
,
2458 out_offsets
, symtab
);
2461 // Finalize the local symbols. Here we set the final value in
2462 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
2463 // This function is always called from a singleton thread. The actual
2464 // output of the local symbols will occur in a separate task.
2466 template<int size
, bool big_endian
>
2468 Sized_relobj_file
<size
, big_endian
>::do_finalize_local_symbols(
2471 Symbol_table
* symtab
)
2473 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
2475 const unsigned int loccount
= this->local_symbol_count_
;
2476 this->local_symbol_offset_
= off
;
2478 const bool relocatable
= parameters
->options().relocatable();
2479 const Output_sections
& out_sections(this->output_sections());
2480 const std::vector
<Address
>& out_offsets(this->section_offsets());
2482 for (unsigned int i
= 1; i
< loccount
; ++i
)
2484 Symbol_value
<size
>* lv
= &this->local_values_
[i
];
2486 Compute_final_local_value_status cflv_status
=
2487 this->compute_final_local_value_internal(i
, lv
, lv
, relocatable
,
2488 out_sections
, out_offsets
,
2490 switch (cflv_status
)
2493 if (!lv
->is_output_symtab_index_set())
2495 lv
->set_output_symtab_index(index
);
2499 case CFLV_DISCARDED
:
2510 // Set the output dynamic symbol table indexes for the local variables.
2512 template<int size
, bool big_endian
>
2514 Sized_relobj_file
<size
, big_endian
>::do_set_local_dynsym_indexes(
2517 const unsigned int loccount
= this->local_symbol_count_
;
2518 for (unsigned int i
= 1; i
< loccount
; ++i
)
2520 Symbol_value
<size
>& lv(this->local_values_
[i
]);
2521 if (lv
.needs_output_dynsym_entry())
2523 lv
.set_output_dynsym_index(index
);
2530 // Set the offset where local dynamic symbol information will be stored.
2531 // Returns the count of local symbols contributed to the symbol table by
2534 template<int size
, bool big_endian
>
2536 Sized_relobj_file
<size
, big_endian
>::do_set_local_dynsym_offset(off_t off
)
2538 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
2539 this->local_dynsym_offset_
= off
;
2540 return this->output_local_dynsym_count_
;
2543 // If Symbols_data is not NULL get the section flags from here otherwise
2544 // get it from the file.
2546 template<int size
, bool big_endian
>
2548 Sized_relobj_file
<size
, big_endian
>::do_section_flags(unsigned int shndx
)
2550 Symbols_data
* sd
= this->get_symbols_data();
2553 const unsigned char* pshdrs
= sd
->section_headers_data
2554 + This::shdr_size
* shndx
;
2555 typename
This::Shdr
shdr(pshdrs
);
2556 return shdr
.get_sh_flags();
2558 // If sd is NULL, read the section header from the file.
2559 return this->elf_file_
.section_flags(shndx
);
2562 // Get the section's ent size from Symbols_data. Called by get_section_contents
2565 template<int size
, bool big_endian
>
2567 Sized_relobj_file
<size
, big_endian
>::do_section_entsize(unsigned int shndx
)
2569 Symbols_data
* sd
= this->get_symbols_data();
2570 gold_assert(sd
!= NULL
);
2572 const unsigned char* pshdrs
= sd
->section_headers_data
2573 + This::shdr_size
* shndx
;
2574 typename
This::Shdr
shdr(pshdrs
);
2575 return shdr
.get_sh_entsize();
2578 // Write out the local symbols.
2580 template<int size
, bool big_endian
>
2582 Sized_relobj_file
<size
, big_endian
>::write_local_symbols(
2584 const Stringpool
* sympool
,
2585 const Stringpool
* dynpool
,
2586 Output_symtab_xindex
* symtab_xindex
,
2587 Output_symtab_xindex
* dynsym_xindex
,
2590 const bool strip_all
= parameters
->options().strip_all();
2593 if (this->output_local_dynsym_count_
== 0)
2595 this->output_local_symbol_count_
= 0;
2598 gold_assert(this->symtab_shndx_
!= -1U);
2599 if (this->symtab_shndx_
== 0)
2601 // This object has no symbols. Weird but legal.
2605 // Read the symbol table section header.
2606 const unsigned int symtab_shndx
= this->symtab_shndx_
;
2607 typename
This::Shdr
symtabshdr(this,
2608 this->elf_file_
.section_header(symtab_shndx
));
2609 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
2610 const unsigned int loccount
= this->local_symbol_count_
;
2611 gold_assert(loccount
== symtabshdr
.get_sh_info());
2613 // Read the local symbols.
2614 const int sym_size
= This::sym_size
;
2615 off_t locsize
= loccount
* sym_size
;
2616 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
2617 locsize
, true, false);
2619 // Read the symbol names.
2620 const unsigned int strtab_shndx
=
2621 this->adjust_shndx(symtabshdr
.get_sh_link());
2622 section_size_type strtab_size
;
2623 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
2626 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
2628 // Get views into the output file for the portions of the symbol table
2629 // and the dynamic symbol table that we will be writing.
2630 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
2631 unsigned char* oview
= NULL
;
2632 if (output_size
> 0)
2633 oview
= of
->get_output_view(symtab_off
+ this->local_symbol_offset_
,
2636 off_t dyn_output_size
= this->output_local_dynsym_count_
* sym_size
;
2637 unsigned char* dyn_oview
= NULL
;
2638 if (dyn_output_size
> 0)
2639 dyn_oview
= of
->get_output_view(this->local_dynsym_offset_
,
2642 const Output_sections
& out_sections(this->output_sections());
2644 gold_assert(this->local_values_
.size() == loccount
);
2646 unsigned char* ov
= oview
;
2647 unsigned char* dyn_ov
= dyn_oview
;
2649 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2651 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
2653 Symbol_value
<size
>& lv(this->local_values_
[i
]);
2656 unsigned int st_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
2660 gold_assert(st_shndx
< out_sections
.size());
2661 if (out_sections
[st_shndx
] == NULL
)
2663 st_shndx
= out_sections
[st_shndx
]->out_shndx();
2664 if (st_shndx
>= elfcpp::SHN_LORESERVE
)
2666 if (lv
.has_output_symtab_entry())
2667 symtab_xindex
->add(lv
.output_symtab_index(), st_shndx
);
2668 if (lv
.has_output_dynsym_entry())
2669 dynsym_xindex
->add(lv
.output_dynsym_index(), st_shndx
);
2670 st_shndx
= elfcpp::SHN_XINDEX
;
2674 // Write the symbol to the output symbol table.
2675 if (lv
.has_output_symtab_entry())
2677 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
2679 gold_assert(isym
.get_st_name() < strtab_size
);
2680 const char* name
= pnames
+ isym
.get_st_name();
2681 osym
.put_st_name(sympool
->get_offset(name
));
2682 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
2683 osym
.put_st_size(isym
.get_st_size());
2684 osym
.put_st_info(isym
.get_st_info());
2685 osym
.put_st_other(isym
.get_st_other());
2686 osym
.put_st_shndx(st_shndx
);
2691 // Write the symbol to the output dynamic symbol table.
2692 if (lv
.has_output_dynsym_entry())
2694 gold_assert(dyn_ov
< dyn_oview
+ dyn_output_size
);
2695 elfcpp::Sym_write
<size
, big_endian
> osym(dyn_ov
);
2697 gold_assert(isym
.get_st_name() < strtab_size
);
2698 const char* name
= pnames
+ isym
.get_st_name();
2699 osym
.put_st_name(dynpool
->get_offset(name
));
2700 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
2701 osym
.put_st_size(isym
.get_st_size());
2702 osym
.put_st_info(isym
.get_st_info());
2703 osym
.put_st_other(isym
.get_st_other());
2704 osym
.put_st_shndx(st_shndx
);
2711 if (output_size
> 0)
2713 gold_assert(ov
- oview
== output_size
);
2714 of
->write_output_view(symtab_off
+ this->local_symbol_offset_
,
2715 output_size
, oview
);
2718 if (dyn_output_size
> 0)
2720 gold_assert(dyn_ov
- dyn_oview
== dyn_output_size
);
2721 of
->write_output_view(this->local_dynsym_offset_
, dyn_output_size
,
2726 // Set *INFO to symbolic information about the offset OFFSET in the
2727 // section SHNDX. Return true if we found something, false if we
2730 template<int size
, bool big_endian
>
2732 Sized_relobj_file
<size
, big_endian
>::get_symbol_location_info(
2735 Symbol_location_info
* info
)
2737 if (this->symtab_shndx_
== 0)
2740 section_size_type symbols_size
;
2741 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
2745 unsigned int symbol_names_shndx
=
2746 this->adjust_shndx(this->section_link(this->symtab_shndx_
));
2747 section_size_type names_size
;
2748 const unsigned char* symbol_names_u
=
2749 this->section_contents(symbol_names_shndx
, &names_size
, false);
2750 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
2752 const int sym_size
= This::sym_size
;
2753 const size_t count
= symbols_size
/ sym_size
;
2755 const unsigned char* p
= symbols
;
2756 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
2758 elfcpp::Sym
<size
, big_endian
> sym(p
);
2760 if (sym
.get_st_type() == elfcpp::STT_FILE
)
2762 if (sym
.get_st_name() >= names_size
)
2763 info
->source_file
= "(invalid)";
2765 info
->source_file
= symbol_names
+ sym
.get_st_name();
2770 unsigned int st_shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
2773 && st_shndx
== shndx
2774 && static_cast<off_t
>(sym
.get_st_value()) <= offset
2775 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
2778 info
->enclosing_symbol_type
= sym
.get_st_type();
2779 if (sym
.get_st_name() > names_size
)
2780 info
->enclosing_symbol_name
= "(invalid)";
2783 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
2784 if (parameters
->options().do_demangle())
2786 char* demangled_name
= cplus_demangle(
2787 info
->enclosing_symbol_name
.c_str(),
2788 DMGL_ANSI
| DMGL_PARAMS
);
2789 if (demangled_name
!= NULL
)
2791 info
->enclosing_symbol_name
.assign(demangled_name
);
2792 free(demangled_name
);
2803 // Look for a kept section corresponding to the given discarded section,
2804 // and return its output address. This is used only for relocations in
2805 // debugging sections. If we can't find the kept section, return 0.
2807 template<int size
, bool big_endian
>
2808 typename Sized_relobj_file
<size
, big_endian
>::Address
2809 Sized_relobj_file
<size
, big_endian
>::map_to_kept_section(
2813 Relobj
* kept_object
;
2814 unsigned int kept_shndx
;
2815 if (this->get_kept_comdat_section(shndx
, &kept_object
, &kept_shndx
))
2817 Sized_relobj_file
<size
, big_endian
>* kept_relobj
=
2818 static_cast<Sized_relobj_file
<size
, big_endian
>*>(kept_object
);
2819 Output_section
* os
= kept_relobj
->output_section(kept_shndx
);
2820 Address offset
= kept_relobj
->get_output_section_offset(kept_shndx
);
2821 if (os
!= NULL
&& offset
!= invalid_address
)
2824 return os
->address() + offset
;
2831 // Get symbol counts.
2833 template<int size
, bool big_endian
>
2835 Sized_relobj_file
<size
, big_endian
>::do_get_global_symbol_counts(
2836 const Symbol_table
*,
2840 *defined
= this->defined_count_
;
2842 for (typename
Symbols::const_iterator p
= this->symbols_
.begin();
2843 p
!= this->symbols_
.end();
2846 && (*p
)->source() == Symbol::FROM_OBJECT
2847 && (*p
)->object() == this
2848 && (*p
)->is_defined())
2853 // Return a view of the decompressed contents of a section. Set *PLEN
2854 // to the size. Set *IS_NEW to true if the contents need to be freed
2857 const unsigned char*
2858 Object::decompressed_section_contents(
2860 section_size_type
* plen
,
2863 section_size_type buffer_size
;
2864 const unsigned char* buffer
= this->do_section_contents(shndx
, &buffer_size
,
2867 if (this->compressed_sections_
== NULL
)
2869 *plen
= buffer_size
;
2874 Compressed_section_map::const_iterator p
=
2875 this->compressed_sections_
->find(shndx
);
2876 if (p
== this->compressed_sections_
->end())
2878 *plen
= buffer_size
;
2883 section_size_type uncompressed_size
= p
->second
.size
;
2884 if (p
->second
.contents
!= NULL
)
2886 *plen
= uncompressed_size
;
2888 return p
->second
.contents
;
2891 unsigned char* uncompressed_data
= new unsigned char[uncompressed_size
];
2892 if (!decompress_input_section(buffer
,
2896 this->error(_("could not decompress section %s"),
2897 this->do_section_name(shndx
).c_str());
2899 // We could cache the results in p->second.contents and store
2900 // false in *IS_NEW, but build_compressed_section_map() would
2901 // have done so if it had expected it to be profitable. If
2902 // we reach this point, we expect to need the contents only
2903 // once in this pass.
2904 *plen
= uncompressed_size
;
2906 return uncompressed_data
;
2909 // Discard any buffers of uncompressed sections. This is done
2910 // at the end of the Add_symbols task.
2913 Object::discard_decompressed_sections()
2915 if (this->compressed_sections_
== NULL
)
2918 for (Compressed_section_map::iterator p
= this->compressed_sections_
->begin();
2919 p
!= this->compressed_sections_
->end();
2922 if (p
->second
.contents
!= NULL
)
2924 delete[] p
->second
.contents
;
2925 p
->second
.contents
= NULL
;
2930 // Input_objects methods.
2932 // Add a regular relocatable object to the list. Return false if this
2933 // object should be ignored.
2936 Input_objects::add_object(Object
* obj
)
2938 // Print the filename if the -t/--trace option is selected.
2939 if (parameters
->options().trace())
2940 gold_info("%s", obj
->name().c_str());
2942 if (!obj
->is_dynamic())
2943 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
2946 // See if this is a duplicate SONAME.
2947 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
2948 const char* soname
= dynobj
->soname();
2950 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
2951 this->sonames_
.insert(soname
);
2954 // We have already seen a dynamic object with this soname.
2958 this->dynobj_list_
.push_back(dynobj
);
2961 // Add this object to the cross-referencer if requested.
2962 if (parameters
->options().user_set_print_symbol_counts()
2963 || parameters
->options().cref())
2965 if (this->cref_
== NULL
)
2966 this->cref_
= new Cref();
2967 this->cref_
->add_object(obj
);
2973 // For each dynamic object, record whether we've seen all of its
2974 // explicit dependencies.
2977 Input_objects::check_dynamic_dependencies() const
2979 bool issued_copy_dt_needed_error
= false;
2980 for (Dynobj_list::const_iterator p
= this->dynobj_list_
.begin();
2981 p
!= this->dynobj_list_
.end();
2984 const Dynobj::Needed
& needed((*p
)->needed());
2985 bool found_all
= true;
2986 Dynobj::Needed::const_iterator pneeded
;
2987 for (pneeded
= needed
.begin(); pneeded
!= needed
.end(); ++pneeded
)
2989 if (this->sonames_
.find(*pneeded
) == this->sonames_
.end())
2995 (*p
)->set_has_unknown_needed_entries(!found_all
);
2997 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2998 // that gold does not support. However, they cause no trouble
2999 // unless there is a DT_NEEDED entry that we don't know about;
3000 // warn only in that case.
3002 && !issued_copy_dt_needed_error
3003 && (parameters
->options().copy_dt_needed_entries()
3004 || parameters
->options().add_needed()))
3006 const char* optname
;
3007 if (parameters
->options().copy_dt_needed_entries())
3008 optname
= "--copy-dt-needed-entries";
3010 optname
= "--add-needed";
3011 gold_error(_("%s is not supported but is required for %s in %s"),
3012 optname
, (*pneeded
).c_str(), (*p
)->name().c_str());
3013 issued_copy_dt_needed_error
= true;
3018 // Start processing an archive.
3021 Input_objects::archive_start(Archive
* archive
)
3023 if (parameters
->options().user_set_print_symbol_counts()
3024 || parameters
->options().cref())
3026 if (this->cref_
== NULL
)
3027 this->cref_
= new Cref();
3028 this->cref_
->add_archive_start(archive
);
3032 // Stop processing an archive.
3035 Input_objects::archive_stop(Archive
* archive
)
3037 if (parameters
->options().user_set_print_symbol_counts()
3038 || parameters
->options().cref())
3039 this->cref_
->add_archive_stop(archive
);
3042 // Print symbol counts
3045 Input_objects::print_symbol_counts(const Symbol_table
* symtab
) const
3047 if (parameters
->options().user_set_print_symbol_counts()
3048 && this->cref_
!= NULL
)
3049 this->cref_
->print_symbol_counts(symtab
);
3052 // Print a cross reference table.
3055 Input_objects::print_cref(const Symbol_table
* symtab
, FILE* f
) const
3057 if (parameters
->options().cref() && this->cref_
!= NULL
)
3058 this->cref_
->print_cref(symtab
, f
);
3061 // Relocate_info methods.
3063 // Return a string describing the location of a relocation when file
3064 // and lineno information is not available. This is only used in
3067 template<int size
, bool big_endian
>
3069 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
3071 Sized_dwarf_line_info
<size
, big_endian
> line_info(this->object
);
3072 std::string ret
= line_info
.addr2line(this->data_shndx
, offset
, NULL
);
3076 ret
= this->object
->name();
3078 Symbol_location_info info
;
3079 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
3081 if (!info
.source_file
.empty())
3084 ret
+= info
.source_file
;
3087 if (info
.enclosing_symbol_type
== elfcpp::STT_FUNC
)
3088 ret
+= _("function ");
3089 ret
+= info
.enclosing_symbol_name
;
3094 ret
+= this->object
->section_name(this->data_shndx
);
3096 snprintf(buf
, sizeof buf
, "+0x%lx)", static_cast<long>(offset
));
3101 } // End namespace gold.
3106 using namespace gold
;
3108 // Read an ELF file with the header and return the appropriate
3109 // instance of Object.
3111 template<int size
, bool big_endian
>
3113 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
3114 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
,
3115 bool* punconfigured
)
3117 Target
* target
= select_target(input_file
, offset
,
3118 ehdr
.get_e_machine(), size
, big_endian
,
3119 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
3120 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
3122 gold_fatal(_("%s: unsupported ELF machine number %d"),
3123 name
.c_str(), ehdr
.get_e_machine());
3125 if (!parameters
->target_valid())
3126 set_parameters_target(target
);
3127 else if (target
!= ¶meters
->target())
3129 if (punconfigured
!= NULL
)
3130 *punconfigured
= true;
3132 gold_error(_("%s: incompatible target"), name
.c_str());
3136 return target
->make_elf_object
<size
, big_endian
>(name
, input_file
, offset
,
3140 } // End anonymous namespace.
3145 // Return whether INPUT_FILE is an ELF object.
3148 is_elf_object(Input_file
* input_file
, off_t offset
,
3149 const unsigned char** start
, int* read_size
)
3151 off_t filesize
= input_file
->file().filesize();
3152 int want
= elfcpp::Elf_recognizer::max_header_size
;
3153 if (filesize
- offset
< want
)
3154 want
= filesize
- offset
;
3156 const unsigned char* p
= input_file
->file().get_view(offset
, 0, want
,
3161 return elfcpp::Elf_recognizer::is_elf_file(p
, want
);
3164 // Read an ELF file and return the appropriate instance of Object.
3167 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
3168 const unsigned char* p
, section_offset_type bytes
,
3169 bool* punconfigured
)
3171 if (punconfigured
!= NULL
)
3172 *punconfigured
= false;
3175 bool big_endian
= false;
3177 if (!elfcpp::Elf_recognizer::is_valid_header(p
, bytes
, &size
,
3178 &big_endian
, &error
))
3180 gold_error(_("%s: %s"), name
.c_str(), error
.c_str());
3188 #ifdef HAVE_TARGET_32_BIG
3189 elfcpp::Ehdr
<32, true> ehdr(p
);
3190 return make_elf_sized_object
<32, true>(name
, input_file
,
3191 offset
, ehdr
, punconfigured
);
3193 if (punconfigured
!= NULL
)
3194 *punconfigured
= true;
3196 gold_error(_("%s: not configured to support "
3197 "32-bit big-endian object"),
3204 #ifdef HAVE_TARGET_32_LITTLE
3205 elfcpp::Ehdr
<32, false> ehdr(p
);
3206 return make_elf_sized_object
<32, false>(name
, input_file
,
3207 offset
, ehdr
, punconfigured
);
3209 if (punconfigured
!= NULL
)
3210 *punconfigured
= true;
3212 gold_error(_("%s: not configured to support "
3213 "32-bit little-endian object"),
3219 else if (size
== 64)
3223 #ifdef HAVE_TARGET_64_BIG
3224 elfcpp::Ehdr
<64, true> ehdr(p
);
3225 return make_elf_sized_object
<64, true>(name
, input_file
,
3226 offset
, ehdr
, punconfigured
);
3228 if (punconfigured
!= NULL
)
3229 *punconfigured
= true;
3231 gold_error(_("%s: not configured to support "
3232 "64-bit big-endian object"),
3239 #ifdef HAVE_TARGET_64_LITTLE
3240 elfcpp::Ehdr
<64, false> ehdr(p
);
3241 return make_elf_sized_object
<64, false>(name
, input_file
,
3242 offset
, ehdr
, punconfigured
);
3244 if (punconfigured
!= NULL
)
3245 *punconfigured
= true;
3247 gold_error(_("%s: not configured to support "
3248 "64-bit little-endian object"),
3258 // Instantiate the templates we need.
3260 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3263 Relobj::initialize_input_to_output_map
<64>(unsigned int shndx
,
3264 elfcpp::Elf_types
<64>::Elf_Addr starting_address
,
3265 Unordered_map
<section_offset_type
,
3266 elfcpp::Elf_types
<64>::Elf_Addr
>* output_addresses
) const;
3269 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3272 Relobj::initialize_input_to_output_map
<32>(unsigned int shndx
,
3273 elfcpp::Elf_types
<32>::Elf_Addr starting_address
,
3274 Unordered_map
<section_offset_type
,
3275 elfcpp::Elf_types
<32>::Elf_Addr
>* output_addresses
) const;
3278 #ifdef HAVE_TARGET_32_LITTLE
3281 Object::read_section_data
<32, false>(elfcpp::Elf_file
<32, false, Object
>*,
3282 Read_symbols_data
*);
3284 const unsigned char*
3285 Object::find_shdr
<32,false>(const unsigned char*, const char*, const char*,
3286 section_size_type
, const unsigned char*) const;
3289 #ifdef HAVE_TARGET_32_BIG
3292 Object::read_section_data
<32, true>(elfcpp::Elf_file
<32, true, Object
>*,
3293 Read_symbols_data
*);
3295 const unsigned char*
3296 Object::find_shdr
<32,true>(const unsigned char*, const char*, const char*,
3297 section_size_type
, const unsigned char*) const;
3300 #ifdef HAVE_TARGET_64_LITTLE
3303 Object::read_section_data
<64, false>(elfcpp::Elf_file
<64, false, Object
>*,
3304 Read_symbols_data
*);
3306 const unsigned char*
3307 Object::find_shdr
<64,false>(const unsigned char*, const char*, const char*,
3308 section_size_type
, const unsigned char*) const;
3311 #ifdef HAVE_TARGET_64_BIG
3314 Object::read_section_data
<64, true>(elfcpp::Elf_file
<64, true, Object
>*,
3315 Read_symbols_data
*);
3317 const unsigned char*
3318 Object::find_shdr
<64,true>(const unsigned char*, const char*, const char*,
3319 section_size_type
, const unsigned char*) const;
3322 #ifdef HAVE_TARGET_32_LITTLE
3324 class Sized_relobj
<32, false>;
3327 class Sized_relobj_file
<32, false>;
3330 #ifdef HAVE_TARGET_32_BIG
3332 class Sized_relobj
<32, true>;
3335 class Sized_relobj_file
<32, true>;
3338 #ifdef HAVE_TARGET_64_LITTLE
3340 class Sized_relobj
<64, false>;
3343 class Sized_relobj_file
<64, false>;
3346 #ifdef HAVE_TARGET_64_BIG
3348 class Sized_relobj
<64, true>;
3351 class Sized_relobj_file
<64, true>;
3354 #ifdef HAVE_TARGET_32_LITTLE
3356 struct Relocate_info
<32, false>;
3359 #ifdef HAVE_TARGET_32_BIG
3361 struct Relocate_info
<32, true>;
3364 #ifdef HAVE_TARGET_64_LITTLE
3366 struct Relocate_info
<64, false>;
3369 #ifdef HAVE_TARGET_64_BIG
3371 struct Relocate_info
<64, true>;
3374 #ifdef HAVE_TARGET_32_LITTLE
3377 Xindex::initialize_symtab_xindex
<32, false>(Object
*, unsigned int);
3381 Xindex::read_symtab_xindex
<32, false>(Object
*, unsigned int,
3382 const unsigned char*);
3385 #ifdef HAVE_TARGET_32_BIG
3388 Xindex::initialize_symtab_xindex
<32, true>(Object
*, unsigned int);
3392 Xindex::read_symtab_xindex
<32, true>(Object
*, unsigned int,
3393 const unsigned char*);
3396 #ifdef HAVE_TARGET_64_LITTLE
3399 Xindex::initialize_symtab_xindex
<64, false>(Object
*, unsigned int);
3403 Xindex::read_symtab_xindex
<64, false>(Object
*, unsigned int,
3404 const unsigned char*);
3407 #ifdef HAVE_TARGET_64_BIG
3410 Xindex::initialize_symtab_xindex
<64, true>(Object
*, unsigned int);
3414 Xindex::read_symtab_xindex
<64, true>(Object
*, unsigned int,
3415 const unsigned char*);
3418 } // End namespace gold.