1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright (C) 2006-2018 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.
30 #include "elfcpp_file.h"
38 class General_options
;
45 class Output_section_data
;
47 class Output_symtab_xindex
;
50 class Object_merge_map
;
51 class Relocatable_relocs
;
54 template<typename Stringpool_char
>
55 class Stringpool_template
;
57 // Data to pass from read_symbols() to add_symbols().
59 struct Read_symbols_data
62 : section_headers(NULL
), section_names(NULL
), symbols(NULL
),
63 symbol_names(NULL
), versym(NULL
), verdef(NULL
), verneed(NULL
)
69 File_view
* section_headers
;
71 File_view
* section_names
;
72 // Size of section name data in bytes.
73 section_size_type section_names_size
;
76 // Size of symbol data in bytes.
77 section_size_type symbols_size
;
78 // Offset of external symbols within symbol data. This structure
79 // sometimes contains only external symbols, in which case this will
80 // be zero. Sometimes it contains all symbols.
81 section_offset_type external_symbols_offset
;
83 File_view
* symbol_names
;
84 // Size of symbol name data in bytes.
85 section_size_type symbol_names_size
;
87 // Version information. This is only used on dynamic objects.
88 // Version symbol data (from SHT_GNU_versym section).
90 section_size_type versym_size
;
91 // Version definition data (from SHT_GNU_verdef section).
93 section_size_type verdef_size
;
94 unsigned int verdef_info
;
95 // Needed version data (from SHT_GNU_verneed section).
97 section_size_type verneed_size
;
98 unsigned int verneed_info
;
101 // Information used to print error messages.
103 struct Symbol_location_info
105 std::string source_file
;
106 std::string enclosing_symbol_name
;
107 elfcpp::STT enclosing_symbol_type
;
110 // Data about a single relocation section. This is read in
111 // read_relocs and processed in scan_relocs.
113 struct Section_relocs
120 { delete this->contents
; }
122 // Index of reloc section.
123 unsigned int reloc_shndx
;
124 // Index of section that relocs apply to.
125 unsigned int data_shndx
;
126 // Contents of reloc section.
128 // Reloc section type.
129 unsigned int sh_type
;
130 // Number of reloc entries.
133 Output_section
* output_section
;
134 // Whether this section has special handling for offsets.
135 bool needs_special_offset_handling
;
136 // Whether the data section is allocated (has the SHF_ALLOC flag set).
137 bool is_data_section_allocated
;
140 // Relocations in an object file. This is read in read_relocs and
141 // processed in scan_relocs.
143 struct Read_relocs_data
146 : local_symbols(NULL
)
150 { delete this->local_symbols
; }
152 typedef std::vector
<Section_relocs
> Relocs_list
;
155 // The local symbols.
156 File_view
* local_symbols
;
159 // The Xindex class manages section indexes for objects with more than
165 Xindex(int large_shndx_offset
)
166 : large_shndx_offset_(large_shndx_offset
), symtab_xindex_()
169 // Initialize the symtab_xindex_ array, given the object and the
170 // section index of the symbol table to use.
171 template<int size
, bool big_endian
>
173 initialize_symtab_xindex(Object
*, unsigned int symtab_shndx
);
175 // Read in the symtab_xindex_ array, given its section index.
176 // PSHDRS may optionally point to the section headers.
177 template<int size
, bool big_endian
>
179 read_symtab_xindex(Object
*, unsigned int xindex_shndx
,
180 const unsigned char* pshdrs
);
182 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
183 // real section index.
185 sym_xindex_to_shndx(Object
* object
, unsigned int symndx
);
188 // The type of the array giving the real section index for symbols
189 // whose st_shndx field holds SHN_XINDEX.
190 typedef std::vector
<unsigned int> Symtab_xindex
;
192 // Adjust a section index if necessary. This should only be called
193 // for ordinary section indexes.
195 adjust_shndx(unsigned int shndx
)
197 if (shndx
>= elfcpp::SHN_LORESERVE
)
198 shndx
+= this->large_shndx_offset_
;
202 // Adjust to apply to large section indexes.
203 int large_shndx_offset_
;
204 // The data from the SHT_SYMTAB_SHNDX section.
205 Symtab_xindex symtab_xindex_
;
208 // A GOT offset list. A symbol may have more than one GOT offset
209 // (e.g., when mixing modules compiled with two different TLS models),
210 // but will usually have at most one. GOT_TYPE identifies the type of
211 // GOT entry; its values are specific to each target.
213 class Got_offset_list
217 : got_type_(-1U), got_offset_(0), got_next_(NULL
)
220 Got_offset_list(unsigned int got_type
, unsigned int got_offset
)
221 : got_type_(got_type
), got_offset_(got_offset
), got_next_(NULL
)
226 if (this->got_next_
!= NULL
)
228 delete this->got_next_
;
229 this->got_next_
= NULL
;
233 // Initialize the fields to their default values.
237 this->got_type_
= -1U;
238 this->got_offset_
= 0;
239 this->got_next_
= NULL
;
242 // Set the offset for the GOT entry of type GOT_TYPE.
244 set_offset(unsigned int got_type
, unsigned int got_offset
)
246 if (this->got_type_
== -1U)
248 this->got_type_
= got_type
;
249 this->got_offset_
= got_offset
;
253 for (Got_offset_list
* g
= this; g
!= NULL
; g
= g
->got_next_
)
255 if (g
->got_type_
== got_type
)
257 g
->got_offset_
= got_offset
;
261 Got_offset_list
* g
= new Got_offset_list(got_type
, got_offset
);
262 g
->got_next_
= this->got_next_
;
267 // Return the offset for a GOT entry of type GOT_TYPE.
269 get_offset(unsigned int got_type
) const
271 for (const Got_offset_list
* g
= this; g
!= NULL
; g
= g
->got_next_
)
273 if (g
->got_type_
== got_type
)
274 return g
->got_offset_
;
279 // Return a pointer to the list, or NULL if the list is empty.
280 const Got_offset_list
*
283 if (this->got_type_
== -1U)
288 // Abstract visitor class for iterating over GOT offsets.
300 visit(unsigned int, unsigned int) = 0;
303 // Loop over all GOT offset entries, calling a visitor class V for each.
305 for_all_got_offsets(Visitor
* v
) const
307 if (this->got_type_
== -1U)
309 for (const Got_offset_list
* g
= this; g
!= NULL
; g
= g
->got_next_
)
310 v
->visit(g
->got_type_
, g
->got_offset_
);
314 unsigned int got_type_
;
315 unsigned int got_offset_
;
316 Got_offset_list
* got_next_
;
319 // The Local_got_entry_key used to index the GOT offsets for local
320 // non-TLS symbols, and tp-relative offsets for TLS symbols.
322 class Local_got_entry_key
325 Local_got_entry_key(unsigned int symndx
, uint64_t addend
)
326 : symndx_(symndx
), addend_(addend
)
329 // Whether this equals to another Local_got_entry_key.
331 eq(const Local_got_entry_key
& key
) const
333 return (this->symndx_
== key
.symndx_
&& this->addend_
== key
.addend_
);
336 // Compute a hash value for this using 64-bit FNV-1a hash.
340 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
341 uint64_t prime
= 1099511628211ULL;
342 h
= (h
^ static_cast<uint64_t>(this->symndx_
)) * prime
;
343 h
= (h
^ static_cast<uint64_t>(this->addend_
)) * prime
;
347 // Functors for associative containers.
351 operator()(const Local_got_entry_key
& key1
,
352 const Local_got_entry_key
& key2
) const
353 { return key1
.eq(key2
); }
359 operator()(const Local_got_entry_key
& key
) const
360 { return key
.hash_value(); }
364 // The local symbol index.
365 unsigned int symndx_
;
370 // Type for mapping section index to uncompressed size and contents.
372 struct Compressed_section_info
374 section_size_type size
;
375 elfcpp::Elf_Xword flag
;
376 const unsigned char* contents
;
378 typedef std::map
<unsigned int, Compressed_section_info
> Compressed_section_map
;
380 template<int size
, bool big_endian
>
381 Compressed_section_map
*
382 build_compressed_section_map(const unsigned char* pshdrs
, unsigned int shnum
,
383 const char* names
, section_size_type names_size
,
384 Object
* obj
, bool decompress_if_needed
);
386 // Object is an abstract base class which represents either a 32-bit
387 // or a 64-bit input object. This can be a regular object file
388 // (ET_REL) or a shared object (ET_DYN).
393 typedef std::vector
<Symbol
*> Symbols
;
395 // NAME is the name of the object as we would report it to the user
396 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
397 // used to read the file. OFFSET is the offset within the input
398 // file--0 for a .o or .so file, something else for a .a file.
399 Object(const std::string
& name
, Input_file
* input_file
, bool is_dynamic
,
401 : name_(name
), input_file_(input_file
), offset_(offset
), shnum_(-1U),
402 is_dynamic_(is_dynamic
), is_needed_(false), uses_split_stack_(false),
403 has_no_split_stack_(false), no_export_(false),
404 is_in_system_directory_(false), as_needed_(false), xindex_(NULL
),
405 compressed_sections_(NULL
)
407 if (input_file
!= NULL
)
409 input_file
->file().add_object();
410 this->is_in_system_directory_
= input_file
->is_in_system_directory();
411 this->as_needed_
= input_file
->options().as_needed();
417 if (this->input_file_
!= NULL
)
418 this->input_file_
->file().remove_object();
421 // Return the name of the object as we would report it to the user.
424 { return this->name_
; }
426 // Get the offset into the file.
429 { return this->offset_
; }
431 // Return whether this is a dynamic object.
434 { return this->is_dynamic_
; }
436 // Return the word size of the object file.
437 virtual int elfsize() const = 0;
439 // Return TRUE if this is a big-endian object file.
440 virtual bool is_big_endian() const = 0;
442 // Return whether this object is needed--true if it is a dynamic
443 // object which defines some symbol referenced by a regular object.
444 // We keep the flag here rather than in Dynobj for convenience when
448 { return this->is_needed_
; }
450 // Record that this object is needed.
453 { this->is_needed_
= true; }
455 // Return whether this object was compiled with -fsplit-stack.
457 uses_split_stack() const
458 { return this->uses_split_stack_
; }
460 // Return whether this object contains any functions compiled with
461 // the no_split_stack attribute.
463 has_no_split_stack() const
464 { return this->has_no_split_stack_
; }
466 // Returns NULL for Objects that are not dynamic objects. This method
467 // is overridden in the Dynobj class.
470 { return this->do_dynobj(); }
472 // Returns NULL for Objects that are not plugin objects. This method
473 // is overridden in the Pluginobj class.
476 { return this->do_pluginobj(); }
478 // Get the file. We pass on const-ness.
482 gold_assert(this->input_file_
!= NULL
);
483 return this->input_file_
;
489 gold_assert(this->input_file_
!= NULL
);
490 return this->input_file_
;
493 // Lock the underlying file.
497 if (this->input_file_
!= NULL
)
498 this->input_file_
->file().lock(t
);
501 // Unlock the underlying file.
503 unlock(const Task
* t
)
505 if (this->input_file_
!= NULL
)
506 this->input_file()->file().unlock(t
);
509 // Return whether the underlying file is locked.
512 { return this->input_file_
!= NULL
&& this->input_file_
->file().is_locked(); }
514 // Return the token, so that the task can be queued.
518 if (this->input_file_
== NULL
)
520 return this->input_file()->file().token();
523 // Release the underlying file.
527 if (this->input_file_
!= NULL
)
528 this->input_file()->file().release();
531 // Return whether we should just read symbols from this file.
534 { return this->input_file()->just_symbols(); }
536 // Return whether this is an incremental object.
538 is_incremental() const
539 { return this->do_is_incremental(); }
541 // Return the last modified time of the file.
544 { return this->do_get_mtime(); }
546 // Get the number of sections.
549 { return this->shnum_
; }
551 // Return a view of the contents of a section. Set *PLEN to the
552 // size. CACHE is a hint as in File_read::get_view.
554 section_contents(unsigned int shndx
, section_size_type
* plen
, bool cache
);
556 // Adjust a symbol's section index as needed. SYMNDX is the index
557 // of the symbol and SHNDX is the symbol's section from
558 // get_st_shndx. This returns the section index. It sets
559 // *IS_ORDINARY to indicate whether this is a normal section index,
560 // rather than a special code between SHN_LORESERVE and
563 adjust_sym_shndx(unsigned int symndx
, unsigned int shndx
, bool* is_ordinary
)
565 if (shndx
< elfcpp::SHN_LORESERVE
)
567 else if (shndx
== elfcpp::SHN_XINDEX
)
569 if (this->xindex_
== NULL
)
570 this->xindex_
= this->do_initialize_xindex();
571 shndx
= this->xindex_
->sym_xindex_to_shndx(this, symndx
);
575 *is_ordinary
= false;
579 // Return the size of a section given a section index.
581 section_size(unsigned int shndx
)
582 { return this->do_section_size(shndx
); }
584 // Return the name of a section given a section index.
586 section_name(unsigned int shndx
) const
587 { return this->do_section_name(shndx
); }
589 // Return the section flags given a section index.
591 section_flags(unsigned int shndx
)
592 { return this->do_section_flags(shndx
); }
594 // Return the section entsize given a section index.
596 section_entsize(unsigned int shndx
)
597 { return this->do_section_entsize(shndx
); }
599 // Return the section address given a section index.
601 section_address(unsigned int shndx
)
602 { return this->do_section_address(shndx
); }
604 // Return the section type given a section index.
606 section_type(unsigned int shndx
)
607 { return this->do_section_type(shndx
); }
609 // Return the section link field given a section index.
611 section_link(unsigned int shndx
)
612 { return this->do_section_link(shndx
); }
614 // Return the section info field given a section index.
616 section_info(unsigned int shndx
)
617 { return this->do_section_info(shndx
); }
619 // Return the required section alignment given a section index.
621 section_addralign(unsigned int shndx
)
622 { return this->do_section_addralign(shndx
); }
624 // Return the output section given a section index.
626 output_section(unsigned int shndx
) const
627 { return this->do_output_section(shndx
); }
629 // Given a section index, return its address.
630 // The return value will be -1U if the section is specially mapped,
631 // such as a merge section.
633 output_section_address(unsigned int shndx
)
634 { return this->do_output_section_address(shndx
); }
636 // Given a section index, return the offset in the Output_section.
637 // The return value will be -1U if the section is specially mapped,
638 // such as a merge section.
640 output_section_offset(unsigned int shndx
) const
641 { return this->do_output_section_offset(shndx
); }
643 // Read the symbol information.
645 read_symbols(Read_symbols_data
* sd
)
646 { return this->do_read_symbols(sd
); }
648 // Pass sections which should be included in the link to the Layout
649 // object, and record where the sections go in the output file.
651 layout(Symbol_table
* symtab
, Layout
* layout
, Read_symbols_data
* sd
)
652 { this->do_layout(symtab
, layout
, sd
); }
654 // Add symbol information to the global symbol table.
656 add_symbols(Symbol_table
* symtab
, Read_symbols_data
* sd
, Layout
*layout
)
657 { this->do_add_symbols(symtab
, sd
, layout
); }
659 // Add symbol information to the global symbol table.
660 Archive::Should_include
661 should_include_member(Symbol_table
* symtab
, Layout
* layout
,
662 Read_symbols_data
* sd
, std::string
* why
)
663 { return this->do_should_include_member(symtab
, layout
, sd
, why
); }
665 // Iterate over global symbols, calling a visitor class V for each.
667 for_all_global_symbols(Read_symbols_data
* sd
,
668 Library_base::Symbol_visitor_base
* v
)
669 { return this->do_for_all_global_symbols(sd
, v
); }
671 // Iterate over local symbols, calling a visitor class V for each GOT offset
672 // associated with a local symbol.
674 for_all_local_got_entries(Got_offset_list::Visitor
* v
) const
675 { this->do_for_all_local_got_entries(v
); }
677 // Functions and types for the elfcpp::Elf_file interface. This
678 // permit us to use Object as the File template parameter for
681 // The View class is returned by view. It must support a single
682 // method, data(). This is trivial, because get_view does what we
687 View(const unsigned char* p
)
696 const unsigned char* p_
;
701 view(off_t file_offset
, section_size_type data_size
)
702 { return View(this->get_view(file_offset
, data_size
, true, true)); }
706 error(const char* format
, ...) const ATTRIBUTE_PRINTF_2
;
708 // A location in the file.
714 Location(off_t fo
, section_size_type ds
)
715 : file_offset(fo
), data_size(ds
)
719 // Get a View given a Location.
720 View
view(Location loc
)
721 { return View(this->get_view(loc
.file_offset
, loc
.data_size
, true, true)); }
723 // Get a view into the underlying file.
725 get_view(off_t start
, section_size_type size
, bool aligned
, bool cache
)
727 return this->input_file()->file().get_view(this->offset_
, start
, size
,
731 // Get a lasting view into the underlying file.
733 get_lasting_view(off_t start
, section_size_type size
, bool aligned
,
736 return this->input_file()->file().get_lasting_view(this->offset_
, start
,
737 size
, aligned
, cache
);
740 // Read data from the underlying file.
742 read(off_t start
, section_size_type size
, void* p
)
743 { this->input_file()->file().read(start
+ this->offset_
, size
, p
); }
745 // Read multiple data from the underlying file.
747 read_multiple(const File_read::Read_multiple
& rm
)
748 { this->input_file()->file().read_multiple(this->offset_
, rm
); }
750 // Stop caching views in the underlying file.
752 clear_view_cache_marks()
754 if (this->input_file_
!= NULL
)
755 this->input_file_
->file().clear_view_cache_marks();
758 // Get the number of global symbols defined by this object, and the
759 // number of the symbols whose final definition came from this
762 get_global_symbol_counts(const Symbol_table
* symtab
, size_t* defined
,
764 { this->do_get_global_symbol_counts(symtab
, defined
, used
); }
766 // Get the symbols defined in this object.
768 get_global_symbols() const
769 { return this->do_get_global_symbols(); }
771 // Set flag that this object was found in a system directory.
773 set_is_in_system_directory()
774 { this->is_in_system_directory_
= true; }
776 // Return whether this object was found in a system directory.
778 is_in_system_directory() const
779 { return this->is_in_system_directory_
; }
781 // Set flag that this object was linked with --as-needed.
784 { this->as_needed_
= true; }
786 // Clear flag that this object was linked with --as-needed.
789 { this->as_needed_
= false; }
791 // Return whether this object was linked with --as-needed.
794 { return this->as_needed_
; }
796 // Return whether we found this object by searching a directory.
799 { return this->input_file()->will_search_for(); }
803 { return this->no_export_
; }
806 set_no_export(bool value
)
807 { this->no_export_
= value
; }
810 section_is_compressed(unsigned int shndx
,
811 section_size_type
* uncompressed_size
) const
813 if (this->compressed_sections_
== NULL
)
815 Compressed_section_map::const_iterator p
=
816 this->compressed_sections_
->find(shndx
);
817 if (p
!= this->compressed_sections_
->end())
819 if (uncompressed_size
!= NULL
)
820 *uncompressed_size
= p
->second
.size
;
826 // Return a view of the decompressed contents of a section. Set *PLEN
827 // to the size. Set *IS_NEW to true if the contents need to be freed
830 decompressed_section_contents(unsigned int shndx
, section_size_type
* plen
,
833 // Discard any buffers of decompressed sections. This is done
834 // at the end of the Add_symbols task.
836 discard_decompressed_sections();
838 // Return the index of the first incremental relocation for symbol SYMNDX.
840 get_incremental_reloc_base(unsigned int symndx
) const
841 { return this->do_get_incremental_reloc_base(symndx
); }
843 // Return the number of incremental relocations for symbol SYMNDX.
845 get_incremental_reloc_count(unsigned int symndx
) const
846 { return this->do_get_incremental_reloc_count(symndx
); }
848 // Return the output view for section SHNDX.
850 get_output_view(unsigned int shndx
, section_size_type
* plen
) const
851 { return this->do_get_output_view(shndx
, plen
); }
854 // Returns NULL for Objects that are not dynamic objects. This method
855 // is overridden in the Dynobj class.
860 // Returns NULL for Objects that are not plugin objects. This method
861 // is overridden in the Pluginobj class.
866 // Return TRUE if this is an incremental (unchanged) input file.
867 // We return FALSE by default; the incremental object classes
868 // override this method.
870 do_is_incremental() const
873 // Return the last modified time of the file. This method may be
874 // overridden for subclasses that don't use an actual file (e.g.,
875 // Incremental objects).
878 { return this->input_file()->file().get_mtime(); }
880 // Read the symbols--implemented by child class.
882 do_read_symbols(Read_symbols_data
*) = 0;
884 // Lay out sections--implemented by child class.
886 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*) = 0;
888 // Add symbol information to the global symbol table--implemented by
891 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*) = 0;
893 virtual Archive::Should_include
894 do_should_include_member(Symbol_table
* symtab
, Layout
*, Read_symbols_data
*,
895 std::string
* why
) = 0;
897 // Iterate over global symbols, calling a visitor class V for each.
899 do_for_all_global_symbols(Read_symbols_data
* sd
,
900 Library_base::Symbol_visitor_base
* v
) = 0;
902 // Iterate over local symbols, calling a visitor class V for each GOT offset
903 // associated with a local symbol.
905 do_for_all_local_got_entries(Got_offset_list::Visitor
* v
) const = 0;
907 // Return the location of the contents of a section. Implemented by
909 virtual const unsigned char*
910 do_section_contents(unsigned int shndx
, section_size_type
* plen
,
913 // Get the size of a section--implemented by child class.
915 do_section_size(unsigned int shndx
) = 0;
917 // Get the name of a section--implemented by child class.
919 do_section_name(unsigned int shndx
) const = 0;
921 // Get section flags--implemented by child class.
923 do_section_flags(unsigned int shndx
) = 0;
925 // Get section entsize--implemented by child class.
927 do_section_entsize(unsigned int shndx
) = 0;
929 // Get section address--implemented by child class.
931 do_section_address(unsigned int shndx
) = 0;
933 // Get section type--implemented by child class.
935 do_section_type(unsigned int shndx
) = 0;
937 // Get section link field--implemented by child class.
939 do_section_link(unsigned int shndx
) = 0;
941 // Get section info field--implemented by child class.
943 do_section_info(unsigned int shndx
) = 0;
945 // Get section alignment--implemented by child class.
947 do_section_addralign(unsigned int shndx
) = 0;
949 // Return the output section given a section index--implemented
951 virtual Output_section
*
952 do_output_section(unsigned int) const
953 { gold_unreachable(); }
955 // Get the address of a section--implemented by child class.
957 do_output_section_address(unsigned int)
958 { gold_unreachable(); }
960 // Get the offset of a section--implemented by child class.
962 do_output_section_offset(unsigned int) const
963 { gold_unreachable(); }
965 // Return the Xindex structure to use.
967 do_initialize_xindex() = 0;
969 // Implement get_global_symbol_counts--implemented by child class.
971 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const = 0;
973 virtual const Symbols
*
974 do_get_global_symbols() const = 0;
976 // Set the number of sections.
979 { this->shnum_
= shnum
; }
981 // Functions used by both Sized_relobj_file and Sized_dynobj.
983 // Read the section data into a Read_symbols_data object.
984 template<int size
, bool big_endian
>
986 read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>*,
989 // Find the section header with the given NAME. If HDR is non-NULL
990 // then it is a section header returned from a previous call to this
991 // function and the next section header with the same name will be
993 template<int size
, bool big_endian
>
995 find_shdr(const unsigned char* pshdrs
, const char* name
,
996 const char* names
, section_size_type names_size
,
997 const unsigned char* hdr
) const;
999 // Let the child class initialize the xindex object directly.
1001 set_xindex(Xindex
* xindex
)
1003 gold_assert(this->xindex_
== NULL
);
1004 this->xindex_
= xindex
;
1007 // If NAME is the name of a special .gnu.warning section, arrange
1008 // for the warning to be issued. SHNDX is the section index.
1009 // Return whether it is a warning section.
1011 handle_gnu_warning_section(const char* name
, unsigned int shndx
,
1014 // If NAME is the name of the special section which indicates that
1015 // this object was compiled with -fsplit-stack, mark it accordingly,
1016 // and return true. Otherwise return false.
1018 handle_split_stack_section(const char* name
);
1020 // Discard any buffers of decompressed sections. This is done
1021 // at the end of the Add_symbols task.
1023 do_discard_decompressed_sections()
1026 // Return the index of the first incremental relocation for symbol SYMNDX--
1027 // implemented by child class.
1028 virtual unsigned int
1029 do_get_incremental_reloc_base(unsigned int) const
1030 { gold_unreachable(); }
1032 // Return the number of incremental relocations for symbol SYMNDX--
1033 // implemented by child class.
1034 virtual unsigned int
1035 do_get_incremental_reloc_count(unsigned int) const
1036 { gold_unreachable(); }
1038 // Return the output view for a section.
1039 virtual unsigned char*
1040 do_get_output_view(unsigned int, section_size_type
*) const
1041 { gold_unreachable(); }
1044 set_compressed_sections(Compressed_section_map
* compressed_sections
)
1045 { this->compressed_sections_
= compressed_sections
; }
1047 Compressed_section_map
*
1048 compressed_sections()
1049 { return this->compressed_sections_
; }
1052 // This class may not be copied.
1053 Object(const Object
&);
1054 Object
& operator=(const Object
&);
1056 // Name of object as printed to user.
1058 // For reading the file.
1059 Input_file
* input_file_
;
1060 // Offset within the file--0 for an object file, non-0 for an
1063 // Number of input sections.
1064 unsigned int shnum_
;
1065 // Whether this is a dynamic object.
1066 bool is_dynamic_
: 1;
1067 // Whether this object is needed. This is only set for dynamic
1068 // objects, and means that the object defined a symbol which was
1069 // used by a reference from a regular object.
1070 bool is_needed_
: 1;
1071 // Whether this object was compiled with -fsplit-stack.
1072 bool uses_split_stack_
: 1;
1073 // Whether this object contains any functions compiled with the
1074 // no_split_stack attribute.
1075 bool has_no_split_stack_
: 1;
1076 // True if exclude this object from automatic symbol export.
1077 // This is used only for archive objects.
1078 bool no_export_
: 1;
1079 // True if the object was found in a system directory.
1080 bool is_in_system_directory_
: 1;
1081 // True if the object was linked with --as-needed.
1082 bool as_needed_
: 1;
1083 // Many sections for objects with more than SHN_LORESERVE sections.
1085 // For compressed debug sections, map section index to uncompressed size
1087 Compressed_section_map
* compressed_sections_
;
1090 // A regular object (ET_REL). This is an abstract base class itself.
1091 // The implementation is the template class Sized_relobj_file.
1093 class Relobj
: public Object
1096 Relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
= 0)
1097 : Object(name
, input_file
, false, offset
),
1099 map_to_relocatable_relocs_(NULL
),
1100 object_merge_map_(NULL
),
1101 relocs_must_follow_section_writes_(false),
1103 reloc_counts_(NULL
),
1105 first_dyn_reloc_(0),
1109 // During garbage collection, the Read_symbols_data pass for
1110 // each object is stored as layout needs to be done after
1111 // reloc processing.
1114 { return this->sd_
; }
1116 // Decides which section names have to be included in the worklist
1119 is_section_name_included(const char* name
);
1122 copy_symbols_data(Symbols_data
* gc_sd
, Read_symbols_data
* sd
,
1123 unsigned int section_header_size
);
1126 set_symbols_data(Symbols_data
* sd
)
1129 // During garbage collection, the Read_relocs pass for all objects
1130 // is done before scanning the relocs. In that case, this->rd_ is
1131 // used to store the information from Read_relocs for each object.
1132 // This data is also used to compute the list of relevant sections.
1135 { return this->rd_
; }
1138 set_relocs_data(Read_relocs_data
* rd
)
1142 is_output_section_offset_invalid(unsigned int shndx
) const = 0;
1146 read_relocs(Read_relocs_data
* rd
)
1147 { return this->do_read_relocs(rd
); }
1149 // Process the relocs, during garbage collection only.
1151 gc_process_relocs(Symbol_table
* symtab
, Layout
* layout
, Read_relocs_data
* rd
)
1152 { return this->do_gc_process_relocs(symtab
, layout
, rd
); }
1154 // Scan the relocs and adjust the symbol table.
1156 scan_relocs(Symbol_table
* symtab
, Layout
* layout
, Read_relocs_data
* rd
)
1157 { return this->do_scan_relocs(symtab
, layout
, rd
); }
1159 // Return the value of the local symbol whose index is SYMNDX, plus
1160 // ADDEND. ADDEND is passed in so that we can correctly handle the
1161 // section symbol for a merge section.
1163 local_symbol_value(unsigned int symndx
, uint64_t addend
) const
1164 { return this->do_local_symbol_value(symndx
, addend
); }
1166 // Return the PLT offset for a local symbol. It is an error to call
1167 // this if it doesn't have one.
1169 local_plt_offset(unsigned int symndx
) const
1170 { return this->do_local_plt_offset(symndx
); }
1172 // Return whether the local symbol SYMNDX has a GOT offset of type
1175 local_has_got_offset(unsigned int symndx
, unsigned int got_type
) const
1176 { return this->do_local_has_got_offset(symndx
, got_type
, 0); }
1178 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
1179 // of type GOT_TYPE.
1181 local_has_got_offset(unsigned int symndx
, unsigned int got_type
,
1182 uint64_t addend
) const
1183 { return this->do_local_has_got_offset(symndx
, got_type
, addend
); }
1185 // Return the GOT offset of type GOT_TYPE of the local symbol
1186 // SYMNDX. It is an error to call this if the symbol does not have
1187 // a GOT offset of the specified type.
1189 local_got_offset(unsigned int symndx
, unsigned int got_type
) const
1190 { return this->do_local_got_offset(symndx
, got_type
, 0); }
1192 // Return the GOT offset of type GOT_TYPE of the local symbol
1193 // SYMNDX plus ADDEND. It is an error to call this if the symbol
1194 // does not have a GOT offset of the specified type.
1196 local_got_offset(unsigned int symndx
, unsigned int got_type
,
1197 uint64_t addend
) const
1198 { return this->do_local_got_offset(symndx
, got_type
, addend
); }
1200 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1203 set_local_got_offset(unsigned int symndx
, unsigned int got_type
,
1204 unsigned int got_offset
)
1205 { this->do_set_local_got_offset(symndx
, got_type
, got_offset
, 0); }
1207 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1208 // plus ADDEND to GOT_OFFSET.
1210 set_local_got_offset(unsigned int symndx
, unsigned int got_type
,
1211 unsigned int got_offset
, uint64_t addend
)
1212 { this->do_set_local_got_offset(symndx
, got_type
, got_offset
, addend
); }
1214 // Return whether the local symbol SYMNDX is a TLS symbol.
1216 local_is_tls(unsigned int symndx
) const
1217 { return this->do_local_is_tls(symndx
); }
1219 // The number of local symbols in the input symbol table.
1220 virtual unsigned int
1221 local_symbol_count() const
1222 { return this->do_local_symbol_count(); }
1224 // The number of local symbols in the output symbol table.
1225 virtual unsigned int
1226 output_local_symbol_count() const
1227 { return this->do_output_local_symbol_count(); }
1229 // The file offset for local symbols in the output symbol table.
1231 local_symbol_offset() const
1232 { return this->do_local_symbol_offset(); }
1234 // Initial local symbol processing: count the number of local symbols
1235 // in the output symbol table and dynamic symbol table; add local symbol
1236 // names to *POOL and *DYNPOOL.
1238 count_local_symbols(Stringpool_template
<char>* pool
,
1239 Stringpool_template
<char>* dynpool
)
1240 { return this->do_count_local_symbols(pool
, dynpool
); }
1242 // Set the values of the local symbols, set the output symbol table
1243 // indexes for the local variables, and set the offset where local
1244 // symbol information will be stored. Returns the new local symbol index.
1246 finalize_local_symbols(unsigned int index
, off_t off
, Symbol_table
* symtab
)
1247 { return this->do_finalize_local_symbols(index
, off
, symtab
); }
1249 // Set the output dynamic symbol table indexes for the local variables.
1251 set_local_dynsym_indexes(unsigned int index
)
1252 { return this->do_set_local_dynsym_indexes(index
); }
1254 // Set the offset where local dynamic symbol information will be stored.
1256 set_local_dynsym_offset(off_t off
)
1257 { return this->do_set_local_dynsym_offset(off
); }
1259 // Record a dynamic relocation against an input section from this object.
1261 add_dyn_reloc(unsigned int index
)
1263 if (this->dyn_reloc_count_
== 0)
1264 this->first_dyn_reloc_
= index
;
1265 ++this->dyn_reloc_count_
;
1268 // Return the index of the first dynamic relocation.
1270 first_dyn_reloc() const
1271 { return this->first_dyn_reloc_
; }
1273 // Return the count of dynamic relocations.
1275 dyn_reloc_count() const
1276 { return this->dyn_reloc_count_
; }
1278 // Relocate the input sections and write out the local symbols.
1280 relocate(const Symbol_table
* symtab
, const Layout
* layout
, Output_file
* of
)
1281 { return this->do_relocate(symtab
, layout
, of
); }
1283 // Return whether an input section is being included in the link.
1285 is_section_included(unsigned int shndx
) const
1287 gold_assert(shndx
< this->output_sections_
.size());
1288 return this->output_sections_
[shndx
] != NULL
;
1291 // The output section of the input section with index SHNDX.
1292 // This is only used currently to remove a section from the link in
1295 set_output_section(unsigned int shndx
, Output_section
* os
)
1297 gold_assert(shndx
< this->output_sections_
.size());
1298 this->output_sections_
[shndx
] = os
;
1301 // Set the offset of an input section within its output section.
1303 set_section_offset(unsigned int shndx
, uint64_t off
)
1304 { this->do_set_section_offset(shndx
, off
); }
1306 // Return true if we need to wait for output sections to be written
1307 // before we can apply relocations. This is true if the object has
1308 // any relocations for sections which require special handling, such
1309 // as the exception frame section.
1311 relocs_must_follow_section_writes() const
1312 { return this->relocs_must_follow_section_writes_
; }
1315 get_or_create_merge_map();
1319 initialize_input_to_output_map(unsigned int shndx
,
1320 typename
elfcpp::Elf_types
<size
>::Elf_Addr starting_address
,
1321 Unordered_map
<section_offset_type
,
1322 typename
elfcpp::Elf_types
<size
>::Elf_Addr
>* output_address
) const;
1325 add_merge_mapping(Output_section_data
*output_data
,
1326 unsigned int shndx
, section_offset_type offset
,
1327 section_size_type length
,
1328 section_offset_type output_offset
);
1331 merge_output_offset(unsigned int shndx
, section_offset_type offset
,
1332 section_offset_type
*poutput
) const;
1334 const Output_section_data
*
1335 find_merge_section(unsigned int shndx
) const;
1337 // Record the relocatable reloc info for an input reloc section.
1339 set_relocatable_relocs(unsigned int reloc_shndx
, Relocatable_relocs
* rr
)
1341 gold_assert(reloc_shndx
< this->shnum());
1342 (*this->map_to_relocatable_relocs_
)[reloc_shndx
] = rr
;
1345 // Get the relocatable reloc info for an input reloc section.
1347 relocatable_relocs(unsigned int reloc_shndx
)
1349 gold_assert(reloc_shndx
< this->shnum());
1350 return (*this->map_to_relocatable_relocs_
)[reloc_shndx
];
1353 // Layout sections whose layout was deferred while waiting for
1354 // input files from a plugin.
1356 layout_deferred_sections(Layout
* layout
)
1357 { this->do_layout_deferred_sections(layout
); }
1359 // Return the index of the first incremental relocation for symbol SYMNDX.
1360 virtual unsigned int
1361 do_get_incremental_reloc_base(unsigned int symndx
) const
1362 { return this->reloc_bases_
[symndx
]; }
1364 // Return the number of incremental relocations for symbol SYMNDX.
1365 virtual unsigned int
1366 do_get_incremental_reloc_count(unsigned int symndx
) const
1367 { return this->reloc_counts_
[symndx
]; }
1369 // Return the word size of the object file.
1372 { return this->do_elfsize(); }
1374 // Return TRUE if this is a big-endian object file.
1376 is_big_endian() const
1377 { return this->do_is_big_endian(); }
1380 // The output section to be used for each input section, indexed by
1381 // the input section number. The output section is NULL if the
1382 // input section is to be discarded.
1383 typedef std::vector
<Output_section
*> Output_sections
;
1385 // Read the relocs--implemented by child class.
1387 do_read_relocs(Read_relocs_data
*) = 0;
1389 // Process the relocs--implemented by child class.
1391 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*) = 0;
1393 // Scan the relocs--implemented by child class.
1395 do_scan_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*) = 0;
1397 // Return the value of a local symbol.
1399 do_local_symbol_value(unsigned int symndx
, uint64_t addend
) const = 0;
1401 // Return the PLT offset of a local symbol.
1402 virtual unsigned int
1403 do_local_plt_offset(unsigned int symndx
) const = 0;
1405 // Return whether a local symbol plus addend has a GOT offset
1408 do_local_has_got_offset(unsigned int symndx
,
1409 unsigned int got_type
, uint64_t addend
) const = 0;
1411 // Return the GOT offset of a given type of a local symbol plus addend.
1412 virtual unsigned int
1413 do_local_got_offset(unsigned int symndx
, unsigned int got_type
,
1414 uint64_t addend
) const = 0;
1416 // Set the GOT offset with a given type for a local symbol plus addend.
1418 do_set_local_got_offset(unsigned int symndx
, unsigned int got_type
,
1419 unsigned int got_offset
, uint64_t addend
) = 0;
1421 // Return whether local symbol SYMNDX is a TLS symbol.
1423 do_local_is_tls(unsigned int symndx
) const = 0;
1425 // Return the number of local symbols--implemented by child class.
1426 virtual unsigned int
1427 do_local_symbol_count() const = 0;
1429 // Return the number of output local symbols--implemented by child class.
1430 virtual unsigned int
1431 do_output_local_symbol_count() const = 0;
1433 // Return the file offset for local symbols--implemented by child class.
1435 do_local_symbol_offset() const = 0;
1437 // Count local symbols--implemented by child class.
1439 do_count_local_symbols(Stringpool_template
<char>*,
1440 Stringpool_template
<char>*) = 0;
1442 // Finalize the local symbols. Set the output symbol table indexes
1443 // for the local variables, and set the offset where local symbol
1444 // information will be stored.
1445 virtual unsigned int
1446 do_finalize_local_symbols(unsigned int, off_t
, Symbol_table
*) = 0;
1448 // Set the output dynamic symbol table indexes for the local variables.
1449 virtual unsigned int
1450 do_set_local_dynsym_indexes(unsigned int) = 0;
1452 // Set the offset where local dynamic symbol information will be stored.
1453 virtual unsigned int
1454 do_set_local_dynsym_offset(off_t
) = 0;
1456 // Relocate the input sections and write out the local
1457 // symbols--implemented by child class.
1459 do_relocate(const Symbol_table
* symtab
, const Layout
*, Output_file
* of
) = 0;
1461 // Set the offset of a section--implemented by child class.
1463 do_set_section_offset(unsigned int shndx
, uint64_t off
) = 0;
1465 // Layout sections whose layout was deferred while waiting for
1466 // input files from a plugin--implemented by child class.
1468 do_layout_deferred_sections(Layout
*) = 0;
1470 // Given a section index, return the corresponding Output_section.
1471 // The return value will be NULL if the section is not included in
1474 do_output_section(unsigned int shndx
) const
1476 gold_assert(shndx
< this->output_sections_
.size());
1477 return this->output_sections_
[shndx
];
1480 // Return the vector mapping input sections to output sections.
1483 { return this->output_sections_
; }
1485 const Output_sections
&
1486 output_sections() const
1487 { return this->output_sections_
; }
1489 // Set the size of the relocatable relocs array.
1491 size_relocatable_relocs()
1493 this->map_to_relocatable_relocs_
=
1494 new std::vector
<Relocatable_relocs
*>(this->shnum());
1497 // Record that we must wait for the output sections to be written
1498 // before applying relocations.
1500 set_relocs_must_follow_section_writes()
1501 { this->relocs_must_follow_section_writes_
= true; }
1503 // Allocate the array for counting incremental relocations.
1505 allocate_incremental_reloc_counts()
1507 unsigned int nsyms
= this->do_get_global_symbols()->size();
1508 this->reloc_counts_
= new unsigned int[nsyms
];
1509 gold_assert(this->reloc_counts_
!= NULL
);
1510 memset(this->reloc_counts_
, 0, nsyms
* sizeof(unsigned int));
1513 // Record a relocation in this object referencing global symbol SYMNDX.
1514 // Used for tracking incremental link information.
1516 count_incremental_reloc(unsigned int symndx
)
1518 unsigned int nsyms
= this->do_get_global_symbols()->size();
1519 gold_assert(symndx
< nsyms
);
1520 gold_assert(this->reloc_counts_
!= NULL
);
1521 ++this->reloc_counts_
[symndx
];
1524 // Finalize the incremental relocation information.
1526 finalize_incremental_relocs(Layout
* layout
, bool clear_counts
);
1528 // Return the index of the next relocation to be written for global symbol
1529 // SYMNDX. Only valid after finalize_incremental_relocs() has been called.
1531 next_incremental_reloc_index(unsigned int symndx
)
1533 unsigned int nsyms
= this->do_get_global_symbols()->size();
1535 gold_assert(this->reloc_counts_
!= NULL
);
1536 gold_assert(this->reloc_bases_
!= NULL
);
1537 gold_assert(symndx
< nsyms
);
1539 unsigned int counter
= this->reloc_counts_
[symndx
]++;
1540 return this->reloc_bases_
[symndx
] + counter
;
1543 // Return the word size of the object file--
1544 // implemented by child class.
1546 do_elfsize() const = 0;
1548 // Return TRUE if this is a big-endian object file--
1549 // implemented by child class.
1551 do_is_big_endian() const = 0;
1554 // Mapping from input sections to output section.
1555 Output_sections output_sections_
;
1556 // Mapping from input section index to the information recorded for
1557 // the relocations. This is only used for a relocatable link.
1558 std::vector
<Relocatable_relocs
*>* map_to_relocatable_relocs_
;
1559 // Mappings for merge sections. This is managed by the code in the
1561 Object_merge_map
* object_merge_map_
;
1562 // Whether we need to wait for output sections to be written before
1563 // we can apply relocations.
1564 bool relocs_must_follow_section_writes_
;
1565 // Used to store the relocs data computed by the Read_relocs pass.
1566 // Used during garbage collection of unused sections.
1567 Read_relocs_data
* rd_
;
1568 // Used to store the symbols data computed by the Read_symbols pass.
1569 // Again used during garbage collection when laying out referenced
1571 gold::Symbols_data
* sd_
;
1572 // Per-symbol counts of relocations, for incremental links.
1573 unsigned int* reloc_counts_
;
1574 // Per-symbol base indexes of relocations, for incremental links.
1575 unsigned int* reloc_bases_
;
1576 // Index of the first dynamic relocation for this object.
1577 unsigned int first_dyn_reloc_
;
1578 // Count of dynamic relocations for this object.
1579 unsigned int dyn_reloc_count_
;
1582 // This class is used to handle relocations against a section symbol
1583 // in an SHF_MERGE section. For such a symbol, we need to know the
1584 // addend of the relocation before we can determine the final value.
1585 // The addend gives us the location in the input section, and we can
1586 // determine how it is mapped to the output section. For a
1587 // non-section symbol, we apply the addend to the final value of the
1588 // symbol; that is done in finalize_local_symbols, and does not use
1592 class Merged_symbol_value
1595 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value
;
1597 // We use a hash table to map offsets in the input section to output
1599 typedef Unordered_map
<section_offset_type
, Value
> Output_addresses
;
1601 Merged_symbol_value(Value input_value
, Value output_start_address
)
1602 : input_value_(input_value
), output_start_address_(output_start_address
),
1606 // Initialize the hash table.
1608 initialize_input_to_output_map(const Relobj
*, unsigned int input_shndx
);
1610 // Release the hash table to save space.
1612 free_input_to_output_map()
1613 { this->output_addresses_
.clear(); }
1615 // Get the output value corresponding to an addend. The object and
1616 // input section index are passed in because the caller will have
1617 // them; otherwise we could store them here.
1619 value(const Relobj
* object
, unsigned int input_shndx
, Value addend
) const
1621 // This is a relocation against a section symbol. ADDEND is the
1622 // offset in the section. The result should be the start of some
1623 // merge area. If the object file wants something else, it should
1624 // use a regular symbol rather than a section symbol.
1625 // Unfortunately, PR 6658 shows a case in which the object file
1626 // refers to the section symbol, but uses a negative ADDEND to
1627 // compensate for a PC relative reloc. We can't handle the
1628 // general case. However, we can handle the special case of a
1629 // negative addend, by assuming that it refers to the start of the
1630 // section. Of course, that means that we have to guess when
1631 // ADDEND is negative. It is normal to see a 32-bit value here
1632 // even when the template parameter size is 64, as 64-bit object
1633 // file formats have 32-bit relocations. We know this is a merge
1634 // section, so we know it has to fit into memory. So we assume
1635 // that we won't see a value larger than a large 32-bit unsigned
1636 // value. This will break objects with very very large merge
1637 // sections; they probably break in other ways anyhow.
1638 Value input_offset
= this->input_value_
;
1639 if (addend
< 0xffffff00)
1641 input_offset
+= addend
;
1644 typename
Output_addresses::const_iterator p
=
1645 this->output_addresses_
.find(input_offset
);
1646 if (p
!= this->output_addresses_
.end())
1647 return p
->second
+ addend
;
1649 return (this->value_from_output_section(object
, input_shndx
, input_offset
)
1654 // Get the output value for an input offset if we couldn't find it
1655 // in the hash table.
1657 value_from_output_section(const Relobj
*, unsigned int input_shndx
,
1658 Value input_offset
) const;
1660 // The value of the section symbol in the input file. This is
1661 // normally zero, but could in principle be something else.
1663 // The start address of this merged section in the output file.
1664 Value output_start_address_
;
1665 // A hash table which maps offsets in the input section to output
1666 // addresses. This only maps specific offsets, not all offsets.
1667 Output_addresses output_addresses_
;
1670 // This POD class is holds the value of a symbol. This is used for
1671 // local symbols, and for all symbols during relocation processing.
1672 // For special sections, such as SHF_MERGE sections, this calls a
1673 // function to get the final symbol value.
1679 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value
;
1682 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
1683 is_ordinary_shndx_(false), is_section_symbol_(false),
1684 is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
1685 { this->u_
.value
= 0; }
1689 if (!this->has_output_value_
)
1690 delete this->u_
.merged_symbol_value
;
1693 // Get the value of this symbol. OBJECT is the object in which this
1694 // symbol is defined, and ADDEND is an addend to add to the value.
1695 template<bool big_endian
>
1697 value(const Sized_relobj_file
<size
, big_endian
>* object
, Value addend
) const
1699 if (this->has_output_value_
)
1700 return this->u_
.value
+ addend
;
1703 gold_assert(this->is_ordinary_shndx_
);
1704 return this->u_
.merged_symbol_value
->value(object
, this->input_shndx_
,
1709 // Set the value of this symbol in the output symbol table.
1711 set_output_value(Value value
)
1712 { this->u_
.value
= value
; }
1714 // For a section symbol in a merged section, we need more
1717 set_merged_symbol_value(Merged_symbol_value
<size
>* msv
)
1719 gold_assert(this->is_section_symbol_
);
1720 this->has_output_value_
= false;
1721 this->u_
.merged_symbol_value
= msv
;
1724 // Initialize the input to output map for a section symbol in a
1725 // merged section. We also initialize the value of a non-section
1726 // symbol in a merged section.
1728 initialize_input_to_output_map(const Relobj
* object
)
1730 if (!this->has_output_value_
)
1732 gold_assert(this->is_section_symbol_
&& this->is_ordinary_shndx_
);
1733 Merged_symbol_value
<size
>* msv
= this->u_
.merged_symbol_value
;
1734 msv
->initialize_input_to_output_map(object
, this->input_shndx_
);
1738 // Free the input to output map for a section symbol in a merged
1741 free_input_to_output_map()
1743 if (!this->has_output_value_
)
1744 this->u_
.merged_symbol_value
->free_input_to_output_map();
1747 // Set the value of the symbol from the input file. This is only
1748 // called by count_local_symbols, to communicate the value to
1749 // finalize_local_symbols.
1751 set_input_value(Value value
)
1752 { this->u_
.value
= value
; }
1754 // Return the input value. This is only called by
1755 // finalize_local_symbols and (in special cases) relocate_section.
1758 { return this->u_
.value
; }
1760 // Return whether we have set the index in the output symbol table
1763 is_output_symtab_index_set() const
1765 return (this->output_symtab_index_
!= 0
1766 && this->output_symtab_index_
!= -2U);
1769 // Return whether this symbol may be discarded from the normal
1772 may_be_discarded_from_output_symtab() const
1774 gold_assert(!this->is_output_symtab_index_set());
1775 return this->output_symtab_index_
!= -2U;
1778 // Return whether this symbol has an entry in the output symbol
1781 has_output_symtab_entry() const
1783 gold_assert(this->is_output_symtab_index_set());
1784 return this->output_symtab_index_
!= -1U;
1787 // Return the index in the output symbol table.
1789 output_symtab_index() const
1791 gold_assert(this->is_output_symtab_index_set()
1792 && this->output_symtab_index_
!= -1U);
1793 return this->output_symtab_index_
;
1796 // Set the index in the output symbol table.
1798 set_output_symtab_index(unsigned int i
)
1800 gold_assert(!this->is_output_symtab_index_set());
1801 gold_assert(i
!= 0 && i
!= -1U && i
!= -2U);
1802 this->output_symtab_index_
= i
;
1805 // Record that this symbol should not go into the output symbol
1808 set_no_output_symtab_entry()
1810 gold_assert(this->output_symtab_index_
== 0);
1811 this->output_symtab_index_
= -1U;
1814 // Record that this symbol must go into the output symbol table,
1815 // because it there is a relocation that uses it.
1817 set_must_have_output_symtab_entry()
1819 gold_assert(!this->is_output_symtab_index_set());
1820 this->output_symtab_index_
= -2U;
1823 // Set the index in the output dynamic symbol table.
1825 set_needs_output_dynsym_entry()
1827 gold_assert(!this->is_section_symbol());
1828 this->output_dynsym_index_
= 0;
1831 // Return whether this symbol should go into the dynamic symbol
1834 needs_output_dynsym_entry() const
1836 return this->output_dynsym_index_
!= -1U;
1839 // Return whether this symbol has an entry in the dynamic symbol
1842 has_output_dynsym_entry() const
1844 gold_assert(this->output_dynsym_index_
!= 0);
1845 return this->output_dynsym_index_
!= -1U;
1848 // Record that this symbol should go into the dynamic symbol table.
1850 set_output_dynsym_index(unsigned int i
)
1852 gold_assert(this->output_dynsym_index_
== 0);
1853 gold_assert(i
!= 0 && i
!= -1U);
1854 this->output_dynsym_index_
= i
;
1857 // Return the index in the output dynamic symbol table.
1859 output_dynsym_index() const
1861 gold_assert(this->output_dynsym_index_
!= 0
1862 && this->output_dynsym_index_
!= -1U);
1863 return this->output_dynsym_index_
;
1866 // Set the index of the input section in the input file.
1868 set_input_shndx(unsigned int i
, bool is_ordinary
)
1870 this->input_shndx_
= i
;
1871 // input_shndx_ field is a bitfield, so make sure that the value
1873 gold_assert(this->input_shndx_
== i
);
1874 this->is_ordinary_shndx_
= is_ordinary
;
1877 // Return the index of the input section in the input file.
1879 input_shndx(bool* is_ordinary
) const
1881 *is_ordinary
= this->is_ordinary_shndx_
;
1882 return this->input_shndx_
;
1885 // Whether this is a section symbol.
1887 is_section_symbol() const
1888 { return this->is_section_symbol_
; }
1890 // Record that this is a section symbol.
1892 set_is_section_symbol()
1894 gold_assert(!this->needs_output_dynsym_entry());
1895 this->is_section_symbol_
= true;
1898 // Record that this is a TLS symbol.
1901 { this->is_tls_symbol_
= true; }
1903 // Return true if this is a TLS symbol.
1905 is_tls_symbol() const
1906 { return this->is_tls_symbol_
; }
1908 // Record that this is an IFUNC symbol.
1910 set_is_ifunc_symbol()
1911 { this->is_ifunc_symbol_
= true; }
1913 // Return true if this is an IFUNC symbol.
1915 is_ifunc_symbol() const
1916 { return this->is_ifunc_symbol_
; }
1918 // Return true if this has output value.
1920 has_output_value() const
1921 { return this->has_output_value_
; }
1924 // The index of this local symbol in the output symbol table. This
1925 // will be 0 if no value has been assigned yet, and the symbol may
1926 // be omitted. This will be -1U if the symbol should not go into
1927 // the symbol table. This will be -2U if the symbol must go into
1928 // the symbol table, but no index has been assigned yet.
1929 unsigned int output_symtab_index_
;
1930 // The index of this local symbol in the dynamic symbol table. This
1931 // will be -1U if the symbol should not go into the symbol table.
1932 unsigned int output_dynsym_index_
;
1933 // The section index in the input file in which this symbol is
1935 unsigned int input_shndx_
: 27;
1936 // Whether the section index is an ordinary index, not a special
1938 bool is_ordinary_shndx_
: 1;
1939 // Whether this is a STT_SECTION symbol.
1940 bool is_section_symbol_
: 1;
1941 // Whether this is a STT_TLS symbol.
1942 bool is_tls_symbol_
: 1;
1943 // Whether this is a STT_GNU_IFUNC symbol.
1944 bool is_ifunc_symbol_
: 1;
1945 // Whether this symbol has a value for the output file. This is
1946 // normally set to true during Layout::finalize, by
1947 // finalize_local_symbols. It will be false for a section symbol in
1948 // a merge section, as for such symbols we can not determine the
1949 // value to use in a relocation until we see the addend.
1950 bool has_output_value_
: 1;
1953 // This is used if has_output_value_ is true. Between
1954 // count_local_symbols and finalize_local_symbols, this is the
1955 // value in the input file. After finalize_local_symbols, it is
1956 // the value in the output file.
1958 // This is used if has_output_value_ is false. It points to the
1959 // information we need to get the value for a merge section.
1960 Merged_symbol_value
<size
>* merged_symbol_value
;
1964 // This type is used to modify relocations for -fsplit-stack. It is
1965 // indexed by relocation index, and means that the relocation at that
1966 // index should use the symbol from the vector, rather than the one
1967 // indicated by the relocation.
1969 class Reloc_symbol_changes
1972 Reloc_symbol_changes(size_t count
)
1977 set(size_t i
, Symbol
* sym
)
1978 { this->vec_
[i
] = sym
; }
1981 operator[](size_t i
) const
1982 { return this->vec_
[i
]; }
1985 std::vector
<Symbol
*> vec_
;
1988 // Abstract base class for a regular object file, either a real object file
1989 // or an incremental (unchanged) object. This is size and endian specific.
1991 template<int size
, bool big_endian
>
1992 class Sized_relobj
: public Relobj
1995 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1996 typedef Relobj::Symbols Symbols
;
1998 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2000 Sized_relobj(const std::string
& name
, Input_file
* input_file
)
2001 : Relobj(name
, input_file
), local_got_offsets_(), section_offsets_()
2004 Sized_relobj(const std::string
& name
, Input_file
* input_file
,
2006 : Relobj(name
, input_file
, offset
), local_got_offsets_(), section_offsets_()
2012 // If this is a regular object, return a pointer to the Sized_relobj_file
2013 // object. Otherwise, return NULL.
2014 virtual Sized_relobj_file
<size
, big_endian
>*
2018 const virtual Sized_relobj_file
<size
, big_endian
>*
2019 sized_relobj() const
2022 // Checks if the offset of input section SHNDX within its output
2023 // section is invalid.
2025 is_output_section_offset_invalid(unsigned int shndx
) const
2026 { return this->get_output_section_offset(shndx
) == invalid_address
; }
2028 // Get the offset of input section SHNDX within its output section.
2029 // This is -1 if the input section requires a special mapping, such
2030 // as a merge section. The output section can be found in the
2031 // output_sections_ field of the parent class Relobj.
2033 get_output_section_offset(unsigned int shndx
) const
2035 gold_assert(shndx
< this->section_offsets_
.size());
2036 return this->section_offsets_
[shndx
];
2039 // Iterate over local symbols, calling a visitor class V for each GOT offset
2040 // associated with a local symbol.
2042 do_for_all_local_got_entries(Got_offset_list::Visitor
* v
) const;
2045 typedef Relobj::Output_sections Output_sections
;
2047 // Clear the local symbol information.
2050 { this->local_got_offsets_
.clear(); }
2052 // Return the vector of section offsets.
2053 std::vector
<Address
>&
2055 { return this->section_offsets_
; }
2057 // Get the address of an output section.
2059 do_output_section_address(unsigned int shndx
);
2061 // Get the offset of a section.
2063 do_output_section_offset(unsigned int shndx
) const
2065 Address off
= this->get_output_section_offset(shndx
);
2066 if (off
== invalid_address
)
2071 // Set the offset of a section.
2073 do_set_section_offset(unsigned int shndx
, uint64_t off
)
2075 gold_assert(shndx
< this->section_offsets_
.size());
2076 this->section_offsets_
[shndx
] =
2077 (off
== static_cast<uint64_t>(-1)
2079 : convert_types
<Address
, uint64_t>(off
));
2082 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
2083 // of type GOT_TYPE.
2085 do_local_has_got_offset(unsigned int symndx
, unsigned int got_type
,
2086 uint64_t addend
) const
2088 Local_got_entry_key
key(symndx
, addend
);
2089 Local_got_offsets::const_iterator p
=
2090 this->local_got_offsets_
.find(key
);
2091 return (p
!= this->local_got_offsets_
.end()
2092 && p
->second
->get_offset(got_type
) != -1U);
2095 // Return the GOT offset of type GOT_TYPE of the local symbol
2096 // SYMNDX plus ADDEND.
2098 do_local_got_offset(unsigned int symndx
, unsigned int got_type
,
2099 uint64_t addend
) const
2101 Local_got_entry_key
key(symndx
, addend
);
2102 Local_got_offsets::const_iterator p
=
2103 this->local_got_offsets_
.find(key
);
2104 gold_assert(p
!= this->local_got_offsets_
.end());
2105 unsigned int off
= p
->second
->get_offset(got_type
);
2106 gold_assert(off
!= -1U);
2110 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
2111 // plus ADDEND to GOT_OFFSET.
2113 do_set_local_got_offset(unsigned int symndx
, unsigned int got_type
,
2114 unsigned int got_offset
, uint64_t addend
)
2116 Local_got_entry_key
key(symndx
, addend
);
2117 Local_got_offsets::const_iterator p
=
2118 this->local_got_offsets_
.find(key
);
2119 if (p
!= this->local_got_offsets_
.end())
2120 p
->second
->set_offset(got_type
, got_offset
);
2123 Got_offset_list
* g
= new Got_offset_list(got_type
, got_offset
);
2124 std::pair
<Local_got_offsets::iterator
, bool> ins
=
2125 this->local_got_offsets_
.insert(std::make_pair(key
, g
));
2126 gold_assert(ins
.second
);
2130 // Return the word size of the object file.
2135 // Return TRUE if this is a big-endian object file.
2137 do_is_big_endian() const
2138 { return big_endian
; }
2141 // The GOT offsets of local symbols. This map also stores GOT offsets
2142 // for tp-relative offsets for TLS symbols.
2143 typedef Unordered_map
<Local_got_entry_key
, Got_offset_list
*,
2144 Local_got_entry_key::hash
,
2145 Local_got_entry_key::equal_to
> Local_got_offsets
;
2147 // GOT offsets for local non-TLS symbols, and tp-relative offsets
2148 // for TLS symbols, indexed by local got entry key class.
2149 Local_got_offsets local_got_offsets_
;
2150 // For each input section, the offset of the input section in its
2151 // output section. This is INVALID_ADDRESS if the input section requires a
2153 std::vector
<Address
> section_offsets_
;
2156 // A regular object file. This is size and endian specific.
2158 template<int size
, bool big_endian
>
2159 class Sized_relobj_file
: public Sized_relobj
<size
, big_endian
>
2162 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2163 typedef typename Sized_relobj
<size
, big_endian
>::Symbols Symbols
;
2164 typedef std::vector
<Symbol_value
<size
> > Local_values
;
2166 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2168 enum Compute_final_local_value_status
2172 // An error occurred.
2174 // The local symbol has no output section.
2178 Sized_relobj_file(const std::string
& name
,
2179 Input_file
* input_file
,
2181 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
2183 ~Sized_relobj_file();
2185 // Set up the object file based on TARGET.
2188 { this->do_setup(); }
2190 // Return a pointer to the Sized_relobj_file object.
2191 Sized_relobj_file
<size
, big_endian
>*
2195 const Sized_relobj_file
<size
, big_endian
>*
2196 sized_relobj() const
2199 // Return the ELF file type.
2202 { return this->e_type_
; }
2204 // Return the number of symbols. This is only valid after
2205 // Object::add_symbols has been called.
2207 symbol_count() const
2208 { return this->local_symbol_count_
+ this->symbols_
.size(); }
2210 // If SYM is the index of a global symbol in the object file's
2211 // symbol table, return the Symbol object. Otherwise, return NULL.
2213 global_symbol(unsigned int sym
) const
2215 if (sym
>= this->local_symbol_count_
)
2217 gold_assert(sym
- this->local_symbol_count_
< this->symbols_
.size());
2218 return this->symbols_
[sym
- this->local_symbol_count_
];
2223 // Return the section index of symbol SYM. Set *VALUE to its value
2224 // in the object file. Set *IS_ORDINARY if this is an ordinary
2225 // section index, not a special code between SHN_LORESERVE and
2226 // SHN_HIRESERVE. Note that for a symbol which is not defined in
2227 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
2228 // it will not return the final value of the symbol in the link.
2230 symbol_section_and_value(unsigned int sym
, Address
* value
, bool* is_ordinary
);
2232 // Return a pointer to the Symbol_value structure which holds the
2233 // value of a local symbol.
2234 const Symbol_value
<size
>*
2235 local_symbol(unsigned int sym
) const
2237 gold_assert(sym
< this->local_values_
.size());
2238 return &this->local_values_
[sym
];
2241 // Return the index of local symbol SYM in the ordinary symbol
2242 // table. A value of -1U means that the symbol is not being output.
2244 symtab_index(unsigned int sym
) const
2246 gold_assert(sym
< this->local_values_
.size());
2247 return this->local_values_
[sym
].output_symtab_index();
2250 // Return the index of local symbol SYM in the dynamic symbol
2251 // table. A value of -1U means that the symbol is not being output.
2253 dynsym_index(unsigned int sym
) const
2255 gold_assert(sym
< this->local_values_
.size());
2256 return this->local_values_
[sym
].output_dynsym_index();
2259 // Return the input section index of local symbol SYM.
2261 local_symbol_input_shndx(unsigned int sym
, bool* is_ordinary
) const
2263 gold_assert(sym
< this->local_values_
.size());
2264 return this->local_values_
[sym
].input_shndx(is_ordinary
);
2267 // Record that local symbol SYM must be in the output symbol table.
2269 set_must_have_output_symtab_entry(unsigned int sym
)
2271 gold_assert(sym
< this->local_values_
.size());
2272 this->local_values_
[sym
].set_must_have_output_symtab_entry();
2275 // Record that local symbol SYM needs a dynamic symbol entry.
2277 set_needs_output_dynsym_entry(unsigned int sym
)
2279 gold_assert(sym
< this->local_values_
.size());
2280 this->local_values_
[sym
].set_needs_output_dynsym_entry();
2283 // Return whether the local symbol SYMNDX has a PLT offset.
2285 local_has_plt_offset(unsigned int symndx
) const;
2287 // Set the PLT offset of the local symbol SYMNDX.
2289 set_local_plt_offset(unsigned int symndx
, unsigned int plt_offset
);
2291 // Adjust this local symbol value. Return false if the symbol
2292 // should be discarded from the output file.
2294 adjust_local_symbol(Symbol_value
<size
>* lv
) const
2295 { return this->do_adjust_local_symbol(lv
); }
2297 // Return the name of the symbol that spans the given offset in the
2298 // specified section in this object. This is used only for error
2299 // messages and is not particularly efficient.
2301 get_symbol_location_info(unsigned int shndx
, off_t offset
,
2302 Symbol_location_info
* info
);
2304 // Look for a kept section corresponding to the given discarded section,
2305 // and return its output address. This is used only for relocations in
2306 // debugging sections.
2308 map_to_kept_section(unsigned int shndx
, std::string
& section_name
,
2311 // Look for a kept section corresponding to the given discarded section,
2312 // and return its object file.
2314 find_kept_section_object(unsigned int shndx
, unsigned int* symndx_p
) const;
2316 // Return the name of symbol SYMNDX.
2318 get_symbol_name(unsigned int symndx
);
2320 // Compute final local symbol value. R_SYM is the local symbol index.
2321 // LV_IN points to a local symbol value containing the input value.
2322 // LV_OUT points to a local symbol value storing the final output value,
2323 // which must not be a merged symbol value since before calling this
2324 // method to avoid memory leak. SYMTAB points to a symbol table.
2326 // The method returns a status code at return. If the return status is
2327 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2328 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2329 // *LV_OUT is not modified.
2330 Compute_final_local_value_status
2331 compute_final_local_value(unsigned int r_sym
,
2332 const Symbol_value
<size
>* lv_in
,
2333 Symbol_value
<size
>* lv_out
,
2334 const Symbol_table
* symtab
);
2336 // Return true if the layout for this object was deferred.
2337 bool is_deferred_layout() const
2338 { return this->is_deferred_layout_
; }
2341 typedef typename Sized_relobj
<size
, big_endian
>::Output_sections
2348 // Read the symbols.
2350 do_read_symbols(Read_symbols_data
*);
2352 // Read the symbols. This is common code for all target-specific
2353 // overrides of do_read_symbols.
2355 base_read_symbols(Read_symbols_data
*);
2357 // Return the value of a local symbol.
2359 do_local_symbol_value(unsigned int symndx
, uint64_t addend
) const
2361 const Symbol_value
<size
>* symval
= this->local_symbol(symndx
);
2362 return symval
->value(this, addend
);
2365 // Return the PLT offset for a local symbol. It is an error to call
2366 // this if it doesn't have one.
2368 do_local_plt_offset(unsigned int symndx
) const;
2370 // Return whether local symbol SYMNDX is a TLS symbol.
2372 do_local_is_tls(unsigned int symndx
) const
2373 { return this->local_symbol(symndx
)->is_tls_symbol(); }
2375 // Return the number of local symbols.
2377 do_local_symbol_count() const
2378 { return this->local_symbol_count_
; }
2380 // Return the number of local symbols in the output symbol table.
2382 do_output_local_symbol_count() const
2383 { return this->output_local_symbol_count_
; }
2385 // Return the number of local symbols in the output symbol table.
2387 do_local_symbol_offset() const
2388 { return this->local_symbol_offset_
; }
2390 // Lay out the input sections.
2392 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
2394 // Layout sections whose layout was deferred while waiting for
2395 // input files from a plugin.
2397 do_layout_deferred_sections(Layout
*);
2399 // Add the symbols to the symbol table.
2401 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*);
2403 Archive::Should_include
2404 do_should_include_member(Symbol_table
* symtab
, Layout
*, Read_symbols_data
*,
2407 // Iterate over global symbols, calling a visitor class V for each.
2409 do_for_all_global_symbols(Read_symbols_data
* sd
,
2410 Library_base::Symbol_visitor_base
* v
);
2414 do_read_relocs(Read_relocs_data
*);
2416 // Process the relocs to find list of referenced sections. Used only
2417 // during garbage collection.
2419 do_gc_process_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
2421 // Scan the relocs and adjust the symbol table.
2423 do_scan_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
2425 // Count the local symbols.
2427 do_count_local_symbols(Stringpool_template
<char>*,
2428 Stringpool_template
<char>*);
2430 // Finalize the local symbols.
2432 do_finalize_local_symbols(unsigned int, off_t
, Symbol_table
*);
2434 // Set the offset where local dynamic symbol information will be stored.
2436 do_set_local_dynsym_indexes(unsigned int);
2438 // Set the offset where local dynamic symbol information will be stored.
2440 do_set_local_dynsym_offset(off_t
);
2442 // Relocate the input sections and write out the local symbols.
2444 do_relocate(const Symbol_table
* symtab
, const Layout
*, Output_file
* of
);
2446 // Get the size of a section.
2448 do_section_size(unsigned int shndx
)
2449 { return this->elf_file_
.section_size(shndx
); }
2451 // Get the name of a section.
2453 do_section_name(unsigned int shndx
) const
2454 { return this->elf_file_
.section_name(shndx
); }
2456 // Return the location of the contents of a section.
2457 const unsigned char*
2458 do_section_contents(unsigned int shndx
, section_size_type
* plen
,
2461 Object::Location
loc(this->elf_file_
.section_contents(shndx
));
2462 *plen
= convert_to_section_size_type(loc
.data_size
);
2465 static const unsigned char empty
[1] = { '\0' };
2468 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
2471 // Return section flags.
2473 do_section_flags(unsigned int shndx
);
2475 // Return section entsize.
2477 do_section_entsize(unsigned int shndx
);
2479 // Return section address.
2481 do_section_address(unsigned int shndx
)
2482 { return this->elf_file_
.section_addr(shndx
); }
2484 // Return section type.
2486 do_section_type(unsigned int shndx
)
2487 { return this->elf_file_
.section_type(shndx
); }
2489 // Return the section link field.
2491 do_section_link(unsigned int shndx
)
2492 { return this->elf_file_
.section_link(shndx
); }
2494 // Return the section info field.
2496 do_section_info(unsigned int shndx
)
2497 { return this->elf_file_
.section_info(shndx
); }
2499 // Return the section alignment.
2501 do_section_addralign(unsigned int shndx
)
2502 { return this->elf_file_
.section_addralign(shndx
); }
2504 // Return the Xindex structure to use.
2506 do_initialize_xindex();
2508 // Get symbol counts.
2510 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const;
2512 // Get the global symbols.
2514 do_get_global_symbols() const
2515 { return &this->symbols_
; }
2517 // Adjust a section index if necessary.
2519 adjust_shndx(unsigned int shndx
)
2521 if (shndx
>= elfcpp::SHN_LORESERVE
)
2522 shndx
+= this->elf_file_
.large_shndx_offset();
2526 // Initialize input to output maps for section symbols in merged
2529 initialize_input_to_output_maps();
2531 // Free the input to output maps for section symbols in merged
2534 free_input_to_output_maps();
2536 // Return symbol table section index.
2538 symtab_shndx() const
2539 { return this->symtab_shndx_
; }
2541 // Allow a child class to access the ELF file.
2542 elfcpp::Elf_file
<size
, big_endian
, Object
>*
2544 { return &this->elf_file_
; }
2546 // Allow a child class to access the local values.
2549 { return &this->local_values_
; }
2551 // Views and sizes when relocating.
2554 unsigned char* view
;
2555 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
2557 section_size_type view_size
;
2558 bool is_input_output_view
;
2559 bool is_postprocessing_view
;
2560 bool is_ctors_reverse_view
;
2563 typedef std::vector
<View_size
> Views
;
2565 // Stash away info for a number of special sections.
2566 // Return true if any of the sections found require local symbols to be read.
2568 do_find_special_sections(Read_symbols_data
* sd
);
2570 // This may be overriden by a child class.
2572 do_relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
2573 const unsigned char* pshdrs
, Output_file
* of
,
2576 // Relocate section data for a range of sections.
2578 relocate_section_range(const Symbol_table
* symtab
, const Layout
* layout
,
2579 const unsigned char* pshdrs
, Output_file
* of
,
2580 Views
* pviews
, unsigned int start_shndx
,
2581 unsigned int end_shndx
);
2583 // Adjust this local symbol value. Return false if the symbol
2584 // should be discarded from the output file.
2586 do_adjust_local_symbol(Symbol_value
<size
>*) const
2589 // Allow a child to set output local symbol count.
2591 set_output_local_symbol_count(unsigned int value
)
2592 { this->output_local_symbol_count_
= value
; }
2594 // Return the output view for a section.
2596 do_get_output_view(unsigned int, section_size_type
*) const;
2600 typedef Sized_relobj_file
<size
, big_endian
> This
;
2601 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
2602 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2603 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2604 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
2605 typedef elfcpp::Shdr_write
<size
, big_endian
> Shdr_write
;
2607 // To keep track of discarded comdat sections, we need to map a member
2608 // section index to the object and section index of the corresponding
2610 struct Kept_comdat_section
2612 Kept_comdat_section(uint64_t a_sh_size
, Kept_section
* a_kept_section
,
2613 unsigned int a_symndx
, bool a_is_comdat
)
2614 : sh_size(a_sh_size
), kept_section(a_kept_section
),
2615 symndx (a_symndx
), is_comdat(a_is_comdat
)
2617 uint64_t sh_size
; // Section size
2618 Kept_section
* kept_section
; // Kept section info
2619 unsigned int symndx
; // Index of key symbol
2620 bool is_comdat
; // True if comdat group, false if linkonce
2622 typedef std::map
<unsigned int, Kept_comdat_section
>
2623 Kept_comdat_section_table
;
2625 // Find the SHT_SYMTAB section, given the section headers.
2627 find_symtab(const unsigned char* pshdrs
);
2629 // Return whether SHDR has the right flags for a GNU style exception
2632 check_eh_frame_flags(const elfcpp::Shdr
<size
, big_endian
>* shdr
) const;
2634 // Return whether there is a section named .eh_frame which might be
2635 // a GNU style exception frame section.
2637 find_eh_frame(const unsigned char* pshdrs
, const char* names
,
2638 section_size_type names_size
) const;
2640 // Whether to include a section group in the link.
2642 include_section_group(Symbol_table
*, Layout
*, unsigned int, const char*,
2643 const unsigned char*, const char*, section_size_type
,
2644 std::vector
<bool>*);
2646 // Whether to include a linkonce section in the link.
2648 include_linkonce_section(Layout
*, unsigned int, const char*,
2649 const elfcpp::Shdr
<size
, big_endian
>&);
2651 // Layout an input section.
2653 layout_section(Layout
* layout
, unsigned int shndx
, const char* name
,
2654 const typename
This::Shdr
& shdr
, unsigned int sh_type
,
2655 unsigned int reloc_shndx
, unsigned int reloc_type
);
2657 // Layout an input .eh_frame section.
2659 layout_eh_frame_section(Layout
* layout
, const unsigned char* symbols_data
,
2660 section_size_type symbols_size
,
2661 const unsigned char* symbol_names_data
,
2662 section_size_type symbol_names_size
,
2663 unsigned int shndx
, const typename
This::Shdr
&,
2664 unsigned int reloc_shndx
, unsigned int reloc_type
);
2666 // Layout an input .note.gnu.property section.
2668 layout_gnu_property_section(Layout
* layout
, unsigned int shndx
);
2670 // Write section data to the output file. Record the views and
2671 // sizes in VIEWS for use when relocating.
2673 write_sections(const Layout
*, const unsigned char* pshdrs
, Output_file
*,
2676 // Relocate the sections in the output file.
2678 relocate_sections(const Symbol_table
* symtab
, const Layout
* layout
,
2679 const unsigned char* pshdrs
, Output_file
* of
,
2681 { this->do_relocate_sections(symtab
, layout
, pshdrs
, of
, pviews
); }
2683 // Reverse the words in a section. Used for .ctors sections mapped
2684 // to .init_array sections.
2686 reverse_words(unsigned char*, section_size_type
);
2688 // Scan the input relocations for --emit-relocs.
2690 emit_relocs_scan(Symbol_table
*, Layout
*, const unsigned char* plocal_syms
,
2691 const Read_relocs_data::Relocs_list::iterator
&);
2693 // Scan the input relocations for --emit-relocs, templatized on the
2694 // type of the relocation section.
2695 template<int sh_type
>
2697 emit_relocs_scan_reltype(Symbol_table
*, Layout
*,
2698 const unsigned char* plocal_syms
,
2699 const Read_relocs_data::Relocs_list::iterator
&,
2700 Relocatable_relocs
*);
2702 // Scan the input relocations for --incremental.
2704 incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator
&);
2706 // Scan the input relocations for --incremental, templatized on the
2707 // type of the relocation section.
2708 template<int sh_type
>
2710 incremental_relocs_scan_reltype(
2711 const Read_relocs_data::Relocs_list::iterator
&);
2714 incremental_relocs_write(const Relocate_info
<size
, big_endian
>*,
2715 unsigned int sh_type
,
2716 const unsigned char* prelocs
,
2719 Address output_offset
,
2722 template<int sh_type
>
2724 incremental_relocs_write_reltype(const Relocate_info
<size
, big_endian
>*,
2725 const unsigned char* prelocs
,
2728 Address output_offset
,
2731 // A type shared by split_stack_adjust_reltype and find_functions.
2732 typedef std::map
<section_offset_type
, section_size_type
> Function_offsets
;
2734 // Check for -fsplit-stack routines calling non-split-stack routines.
2736 split_stack_adjust(const Symbol_table
*, const unsigned char* pshdrs
,
2737 unsigned int sh_type
, unsigned int shndx
,
2738 const unsigned char* prelocs
, size_t reloc_count
,
2739 unsigned char* view
, section_size_type view_size
,
2740 Reloc_symbol_changes
** reloc_map
,
2741 const Sized_target
<size
, big_endian
>* target
);
2743 template<int sh_type
>
2745 split_stack_adjust_reltype(const Symbol_table
*, const unsigned char* pshdrs
,
2746 unsigned int shndx
, const unsigned char* prelocs
,
2747 size_t reloc_count
, unsigned char* view
,
2748 section_size_type view_size
,
2749 Reloc_symbol_changes
** reloc_map
,
2750 const Sized_target
<size
, big_endian
>* target
);
2752 // Find all functions in a section.
2754 find_functions(const unsigned char* pshdrs
, unsigned int shndx
,
2757 // Write out the local symbols.
2759 write_local_symbols(Output_file
*,
2760 const Stringpool_template
<char>*,
2761 const Stringpool_template
<char>*,
2762 Output_symtab_xindex
*,
2763 Output_symtab_xindex
*,
2766 // Record a mapping from discarded section SHNDX to the corresponding
2769 set_kept_comdat_section(unsigned int shndx
, bool is_comdat
,
2770 unsigned int symndx
, uint64_t sh_size
,
2771 Kept_section
* kept_section
)
2773 Kept_comdat_section
kept(sh_size
, kept_section
, symndx
, is_comdat
);
2774 this->kept_comdat_sections_
.insert(std::make_pair(shndx
, kept
));
2777 // Find the kept section corresponding to the discarded section
2778 // SHNDX. Return true if found.
2780 get_kept_comdat_section(unsigned int shndx
, bool* is_comdat
,
2781 unsigned int *symndx
, uint64_t* sh_size
,
2782 Kept_section
** kept_section
) const
2784 typename
Kept_comdat_section_table::const_iterator p
=
2785 this->kept_comdat_sections_
.find(shndx
);
2786 if (p
== this->kept_comdat_sections_
.end())
2788 *is_comdat
= p
->second
.is_comdat
;
2789 *symndx
= p
->second
.symndx
;
2790 *sh_size
= p
->second
.sh_size
;
2791 *kept_section
= p
->second
.kept_section
;
2795 // Compute final local symbol value. R_SYM is the local symbol index.
2796 // LV_IN points to a local symbol value containing the input value.
2797 // LV_OUT points to a local symbol value storing the final output value,
2798 // which must not be a merged symbol value since before calling this
2799 // method to avoid memory leak. RELOCATABLE indicates whether we are
2800 // linking a relocatable output. OUT_SECTIONS is an array of output
2801 // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB
2802 // points to a symbol table.
2804 // The method returns a status code at return. If the return status is
2805 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2806 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2807 // *LV_OUT is not modified.
2808 inline Compute_final_local_value_status
2809 compute_final_local_value_internal(unsigned int r_sym
,
2810 const Symbol_value
<size
>* lv_in
,
2811 Symbol_value
<size
>* lv_out
,
2813 const Output_sections
& out_sections
,
2814 const std::vector
<Address
>& out_offsets
,
2815 const Symbol_table
* symtab
);
2817 // The PLT offsets of local symbols.
2818 typedef Unordered_map
<unsigned int, unsigned int> Local_plt_offsets
;
2820 // Saved information for sections whose layout was deferred.
2821 struct Deferred_layout
2823 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2824 Deferred_layout(unsigned int shndx
, const char* name
,
2825 unsigned int sh_type
,
2826 const unsigned char* pshdr
,
2827 unsigned int reloc_shndx
, unsigned int reloc_type
)
2828 : name_(name
), shndx_(shndx
), reloc_shndx_(reloc_shndx
),
2829 reloc_type_(reloc_type
)
2831 typename
This::Shdr_write
shdr(this->shdr_data_
);
2832 memcpy(this->shdr_data_
, pshdr
, shdr_size
);
2833 shdr
.put_sh_type(sh_type
);
2836 unsigned int shndx_
;
2837 unsigned int reloc_shndx_
;
2838 unsigned int reloc_type_
;
2839 unsigned char shdr_data_
[shdr_size
];
2842 // General access to the ELF file.
2843 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
2844 // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed
2845 // as input files only for the --just-symbols option.
2847 // Index of SHT_SYMTAB section.
2848 unsigned int symtab_shndx_
;
2849 // The number of local symbols.
2850 unsigned int local_symbol_count_
;
2851 // The number of local symbols which go into the output file.
2852 unsigned int output_local_symbol_count_
;
2853 // The number of local symbols which go into the output file's dynamic
2855 unsigned int output_local_dynsym_count_
;
2856 // The entries in the symbol table for the external symbols.
2858 // Number of symbols defined in object file itself.
2859 size_t defined_count_
;
2860 // File offset for local symbols (relative to start of symbol table).
2861 off_t local_symbol_offset_
;
2862 // File offset for local dynamic symbols (absolute).
2863 off_t local_dynsym_offset_
;
2864 // Values of local symbols.
2865 Local_values local_values_
;
2866 // PLT offsets for local symbols.
2867 Local_plt_offsets local_plt_offsets_
;
2868 // Table mapping discarded comdat sections to corresponding kept sections.
2869 Kept_comdat_section_table kept_comdat_sections_
;
2870 // Whether this object has a GNU style .eh_frame section.
2872 // True if the layout of this object was deferred, waiting for plugin
2873 // replacement files.
2874 bool is_deferred_layout_
;
2875 // The list of sections whose layout was deferred.
2876 std::vector
<Deferred_layout
> deferred_layout_
;
2877 // The list of relocation sections whose layout was deferred.
2878 std::vector
<Deferred_layout
> deferred_layout_relocs_
;
2879 // Pointer to the list of output views; valid only during do_relocate().
2880 const Views
* output_views_
;
2883 // A class to manage the list of all objects.
2889 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL
)
2892 // The type of the list of input relocateable objects.
2893 typedef std::vector
<Relobj
*> Relobj_list
;
2894 typedef Relobj_list::const_iterator Relobj_iterator
;
2896 // The type of the list of input dynamic objects.
2897 typedef std::vector
<Dynobj
*> Dynobj_list
;
2898 typedef Dynobj_list::const_iterator Dynobj_iterator
;
2900 // Add an object to the list. Return true if all is well, or false
2901 // if this object should be ignored.
2903 add_object(Object
*);
2905 // Start processing an archive.
2907 archive_start(Archive
*);
2909 // Stop processing an archive.
2911 archive_stop(Archive
*);
2913 // For each dynamic object, check whether we've seen all of its
2914 // explicit dependencies.
2916 check_dynamic_dependencies() const;
2918 // Return whether an object was found in the system library
2921 found_in_system_library_directory(const Object
*) const;
2923 // Print symbol counts.
2925 print_symbol_counts(const Symbol_table
*) const;
2927 // Print a cross reference table.
2929 print_cref(const Symbol_table
*, FILE*) const;
2931 // Iterate over all regular objects.
2934 relobj_begin() const
2935 { return this->relobj_list_
.begin(); }
2939 { return this->relobj_list_
.end(); }
2941 // Iterate over all dynamic objects.
2944 dynobj_begin() const
2945 { return this->dynobj_list_
.begin(); }
2949 { return this->dynobj_list_
.end(); }
2951 // Return whether we have seen any dynamic objects.
2954 { return !this->dynobj_list_
.empty(); }
2956 // Return the number of non dynamic objects.
2958 number_of_relobjs() const
2959 { return this->relobj_list_
.size(); }
2961 // Return the number of input objects.
2963 number_of_input_objects() const
2964 { return this->relobj_list_
.size() + this->dynobj_list_
.size(); }
2967 Input_objects(const Input_objects
&);
2968 Input_objects
& operator=(const Input_objects
&);
2970 // The list of ordinary objects included in the link.
2971 Relobj_list relobj_list_
;
2972 // The list of dynamic objects included in the link.
2973 Dynobj_list dynobj_list_
;
2974 // SONAMEs that we have seen.
2975 Unordered_map
<std::string
, Object
*> sonames_
;
2976 // Manage cross-references if requested.
2980 // Some of the information we pass to the relocation routines. We
2981 // group this together to avoid passing a dozen different arguments.
2983 template<int size
, bool big_endian
>
2984 struct Relocate_info
2987 const Symbol_table
* symtab
;
2989 const Layout
* layout
;
2990 // Object being relocated.
2991 Sized_relobj_file
<size
, big_endian
>* object
;
2992 // Section index of relocation section.
2993 unsigned int reloc_shndx
;
2994 // Section header of relocation section.
2995 const unsigned char* reloc_shdr
;
2996 // Info about how relocs should be handled
2997 Relocatable_relocs
* rr
;
2998 // Section index of section being relocated.
2999 unsigned int data_shndx
;
3000 // Section header of data section.
3001 const unsigned char* data_shdr
;
3003 // Return a string showing the location of a relocation. This is
3004 // only used for error messages.
3006 location(size_t relnum
, off_t reloffset
) const;
3009 // This is used to represent a section in an object and is used as the
3010 // key type for various section maps.
3011 typedef std::pair
<Relobj
*, unsigned int> Section_id
;
3013 // This is similar to Section_id but is used when the section
3014 // pointers are const.
3015 typedef std::pair
<const Relobj
*, unsigned int> Const_section_id
;
3017 // The hash value is based on the address of an object in memory during
3018 // linking. It is okay to use this for looking up sections but never use
3019 // this in an unordered container that we want to traverse in a repeatable
3022 struct Section_id_hash
3024 size_t operator()(const Section_id
& loc
) const
3025 { return reinterpret_cast<uintptr_t>(loc
.first
) ^ loc
.second
; }
3028 struct Const_section_id_hash
3030 size_t operator()(const Const_section_id
& loc
) const
3031 { return reinterpret_cast<uintptr_t>(loc
.first
) ^ loc
.second
; }
3034 // Return whether INPUT_FILE contains an ELF object start at file
3035 // offset OFFSET. This sets *START to point to a view of the start of
3036 // the file. It sets *READ_SIZE to the number of bytes in the view.
3039 is_elf_object(Input_file
* input_file
, off_t offset
,
3040 const unsigned char** start
, int* read_size
);
3042 // Return an Object appropriate for the input file. P is BYTES long,
3043 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
3044 // this sees an object the linker is not configured to support, it
3045 // sets *PUNCONFIGURED to true and returns NULL without giving an
3049 make_elf_object(const std::string
& name
, Input_file
*,
3050 off_t offset
, const unsigned char* p
,
3051 section_offset_type bytes
, bool* punconfigured
);
3053 } // end namespace gold
3055 #endif // !defined(GOLD_OBJECT_H)