1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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"
37 class General_options;
44 class Output_symtab_xindex;
47 class Object_merge_map;
48 class Relocatable_relocs;
51 template<typename Stringpool_char>
52 class Stringpool_template;
54 // Data to pass from read_symbols() to add_symbols().
56 struct Read_symbols_data
59 File_view* section_headers;
61 File_view* section_names;
62 // Size of section name data in bytes.
63 section_size_type section_names_size;
66 // Size of symbol data in bytes.
67 section_size_type symbols_size;
68 // Offset of external symbols within symbol data. This structure
69 // sometimes contains only external symbols, in which case this will
70 // be zero. Sometimes it contains all symbols.
71 section_offset_type external_symbols_offset;
73 File_view* symbol_names;
74 // Size of symbol name data in bytes.
75 section_size_type symbol_names_size;
77 // Version information. This is only used on dynamic objects.
78 // Version symbol data (from SHT_GNU_versym section).
80 section_size_type versym_size;
81 // Version definition data (from SHT_GNU_verdef section).
83 section_size_type verdef_size;
84 unsigned int verdef_info;
85 // Needed version data (from SHT_GNU_verneed section).
87 section_size_type verneed_size;
88 unsigned int verneed_info;
91 // Information used to print error messages.
93 struct Symbol_location_info
95 std::string source_file;
96 std::string enclosing_symbol_name;
100 // Data about a single relocation section. This is read in
101 // read_relocs and processed in scan_relocs.
103 struct Section_relocs
105 // Index of reloc section.
106 unsigned int reloc_shndx;
107 // Index of section that relocs apply to.
108 unsigned int data_shndx;
109 // Contents of reloc section.
111 // Reloc section type.
112 unsigned int sh_type;
113 // Number of reloc entries.
116 Output_section* output_section;
117 // Whether this section has special handling for offsets.
118 bool needs_special_offset_handling;
119 // Whether the data section is allocated (has the SHF_ALLOC flag set).
120 bool is_data_section_allocated;
123 // Relocations in an object file. This is read in read_relocs and
124 // processed in scan_relocs.
126 struct Read_relocs_data
128 typedef std::vector<Section_relocs> Relocs_list;
131 // The local symbols.
132 File_view* local_symbols;
135 // The Xindex class manages section indexes for objects with more than
141 Xindex(int large_shndx_offset)
142 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
145 // Initialize the symtab_xindex_ array, given the object and the
146 // section index of the symbol table to use.
147 template<int size, bool big_endian>
149 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
151 // Read in the symtab_xindex_ array, given its section index.
152 // PSHDRS may optionally point to the section headers.
153 template<int size, bool big_endian>
155 read_symtab_xindex(Object*, unsigned int xindex_shndx,
156 const unsigned char* pshdrs);
158 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
159 // real section index.
161 sym_xindex_to_shndx(Object* object, unsigned int symndx);
164 // The type of the array giving the real section index for symbols
165 // whose st_shndx field holds SHN_XINDEX.
166 typedef std::vector<unsigned int> Symtab_xindex;
168 // Adjust a section index if necessary. This should only be called
169 // for ordinary section indexes.
171 adjust_shndx(unsigned int shndx)
173 if (shndx >= elfcpp::SHN_LORESERVE)
174 shndx += this->large_shndx_offset_;
178 // Adjust to apply to large section indexes.
179 int large_shndx_offset_;
180 // The data from the SHT_SYMTAB_SHNDX section.
181 Symtab_xindex symtab_xindex_;
184 // Object is an abstract base class which represents either a 32-bit
185 // or a 64-bit input object. This can be a regular object file
186 // (ET_REL) or a shared object (ET_DYN).
191 // NAME is the name of the object as we would report it to the user
192 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
193 // used to read the file. OFFSET is the offset within the input
194 // file--0 for a .o or .so file, something else for a .a file.
195 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
197 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
198 is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
199 has_no_split_stack_(false), no_export_(false), xindex_(NULL)
200 { input_file->file().add_object(); }
203 { this->input_file_->file().remove_object(); }
205 // Return the name of the object as we would report it to the tuser.
208 { return this->name_; }
210 // Get the offset into the file.
213 { return this->offset_; }
215 // Return whether this is a dynamic object.
218 { return this->is_dynamic_; }
220 // Return whether this object is needed--true if it is a dynamic
221 // object which defines some symbol referenced by a regular object.
222 // We keep the flag here rather than in Dynobj for convenience when
226 { return this->is_needed_; }
228 // Record that this object is needed.
231 { this->is_needed_ = true; }
233 // Return whether this object was compiled with -fsplit-stack.
235 uses_split_stack() const
236 { return this->uses_split_stack_; }
238 // Return whether this object contains any functions compiled with
239 // the no_split_stack attribute.
241 has_no_split_stack() const
242 { return this->has_no_split_stack_; }
244 // Returns NULL for Objects that are not plugin objects. This method
245 // is overridden in the Pluginobj class.
248 { return this->do_pluginobj(); }
250 // Get the file. We pass on const-ness.
253 { return this->input_file_; }
257 { return this->input_file_; }
259 // Lock the underlying file.
262 { this->input_file()->file().lock(t); }
264 // Unlock the underlying file.
266 unlock(const Task* t)
267 { this->input_file()->file().unlock(t); }
269 // Return whether the underlying file is locked.
272 { return this->input_file()->file().is_locked(); }
274 // Return the token, so that the task can be queued.
277 { return this->input_file()->file().token(); }
279 // Release the underlying file.
282 { this->input_file_->file().release(); }
284 // Return whether we should just read symbols from this file.
287 { return this->input_file()->just_symbols(); }
289 // Get the number of sections.
292 { return this->shnum_; }
294 // Return a view of the contents of a section. Set *PLEN to the
295 // size. CACHE is a hint as in File_read::get_view.
297 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
299 // Adjust a symbol's section index as needed. SYMNDX is the index
300 // of the symbol and SHNDX is the symbol's section from
301 // get_st_shndx. This returns the section index. It sets
302 // *IS_ORDINARY to indicate whether this is a normal section index,
303 // rather than a special code between SHN_LORESERVE and
306 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
308 if (shndx < elfcpp::SHN_LORESERVE)
310 else if (shndx == elfcpp::SHN_XINDEX)
312 if (this->xindex_ == NULL)
313 this->xindex_ = this->do_initialize_xindex();
314 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
318 *is_ordinary = false;
322 // Return the size of a section given a section index.
324 section_size(unsigned int shndx)
325 { return this->do_section_size(shndx); }
327 // Return the name of a section given a section index.
329 section_name(unsigned int shndx)
330 { return this->do_section_name(shndx); }
332 // Return the section flags given a section index.
334 section_flags(unsigned int shndx)
335 { return this->do_section_flags(shndx); }
337 // Return the section entsize given a section index.
339 section_entsize(unsigned int shndx)
340 { return this->do_section_entsize(shndx); }
342 // Return the section address given a section index.
344 section_address(unsigned int shndx)
345 { return this->do_section_address(shndx); }
347 // Return the section type given a section index.
349 section_type(unsigned int shndx)
350 { return this->do_section_type(shndx); }
352 // Return the section link field given a section index.
354 section_link(unsigned int shndx)
355 { return this->do_section_link(shndx); }
357 // Return the section info field given a section index.
359 section_info(unsigned int shndx)
360 { return this->do_section_info(shndx); }
362 // Return the required section alignment given a section index.
364 section_addralign(unsigned int shndx)
365 { return this->do_section_addralign(shndx); }
367 // Read the symbol information.
369 read_symbols(Read_symbols_data* sd)
370 { return this->do_read_symbols(sd); }
372 // Pass sections which should be included in the link to the Layout
373 // object, and record where the sections go in the output file.
375 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
376 { this->do_layout(symtab, layout, sd); }
378 // Add symbol information to the global symbol table.
380 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
381 { this->do_add_symbols(symtab, sd, layout); }
383 // Functions and types for the elfcpp::Elf_file interface. This
384 // permit us to use Object as the File template parameter for
387 // The View class is returned by view. It must support a single
388 // method, data(). This is trivial, because get_view does what we
393 View(const unsigned char* p)
402 const unsigned char* p_;
407 view(off_t file_offset, section_size_type data_size)
408 { return View(this->get_view(file_offset, data_size, true, true)); }
412 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
414 // A location in the file.
420 Location(off_t fo, section_size_type ds)
421 : file_offset(fo), data_size(ds)
425 // Get a View given a Location.
426 View view(Location loc)
427 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
429 // Get a view into the underlying file.
431 get_view(off_t start, section_size_type size, bool aligned, bool cache)
433 return this->input_file()->file().get_view(this->offset_, start, size,
437 // Get a lasting view into the underlying file.
439 get_lasting_view(off_t start, section_size_type size, bool aligned,
442 return this->input_file()->file().get_lasting_view(this->offset_, start,
443 size, aligned, cache);
446 // Read data from the underlying file.
448 read(off_t start, section_size_type size, void* p)
449 { this->input_file()->file().read(start + this->offset_, size, p); }
451 // Read multiple data from the underlying file.
453 read_multiple(const File_read::Read_multiple& rm)
454 { this->input_file()->file().read_multiple(this->offset_, rm); }
456 // Stop caching views in the underlying file.
458 clear_view_cache_marks()
459 { this->input_file()->file().clear_view_cache_marks(); }
461 // Get the number of global symbols defined by this object, and the
462 // number of the symbols whose final definition came from this
465 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
467 { this->do_get_global_symbol_counts(symtab, defined, used); }
469 // Return whether this object was found in a system directory.
471 is_in_system_directory() const
472 { return this->input_file()->is_in_system_directory(); }
474 // Return whether we found this object by searching a directory.
477 { return this->input_file()->will_search_for(); }
481 { return this->no_export_; }
484 set_no_export(bool value)
485 { this->no_export_ = value; }
488 // Returns NULL for Objects that are not plugin objects. This method
489 // is overridden in the Pluginobj class.
494 // Read the symbols--implemented by child class.
496 do_read_symbols(Read_symbols_data*) = 0;
498 // Lay out sections--implemented by child class.
500 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
502 // Add symbol information to the global symbol table--implemented by
505 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
507 // Return the location of the contents of a section. Implemented by
510 do_section_contents(unsigned int shndx) = 0;
512 // Get the size of a section--implemented by child class.
514 do_section_size(unsigned int shndx) = 0;
516 // Get the name of a section--implemented by child class.
518 do_section_name(unsigned int shndx) = 0;
520 // Get section flags--implemented by child class.
522 do_section_flags(unsigned int shndx) = 0;
524 // Get section entsize--implemented by child class.
526 do_section_entsize(unsigned int shndx) = 0;
528 // Get section address--implemented by child class.
530 do_section_address(unsigned int shndx) = 0;
532 // Get section type--implemented by child class.
534 do_section_type(unsigned int shndx) = 0;
536 // Get section link field--implemented by child class.
538 do_section_link(unsigned int shndx) = 0;
540 // Get section info field--implemented by child class.
542 do_section_info(unsigned int shndx) = 0;
544 // Get section alignment--implemented by child class.
546 do_section_addralign(unsigned int shndx) = 0;
548 // Return the Xindex structure to use.
550 do_initialize_xindex() = 0;
552 // Implement get_global_symbol_counts--implemented by child class.
554 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
556 // Set the number of sections.
559 { this->shnum_ = shnum; }
561 // Functions used by both Sized_relobj and Sized_dynobj.
563 // Read the section data into a Read_symbols_data object.
564 template<int size, bool big_endian>
566 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
569 // Let the child class initialize the xindex object directly.
571 set_xindex(Xindex* xindex)
573 gold_assert(this->xindex_ == NULL);
574 this->xindex_ = xindex;
577 // If NAME is the name of a special .gnu.warning section, arrange
578 // for the warning to be issued. SHNDX is the section index.
579 // Return whether it is a warning section.
581 handle_gnu_warning_section(const char* name, unsigned int shndx,
584 // If NAME is the name of the special section which indicates that
585 // this object was compiled with -fstack-split, mark it accordingly,
586 // and return true. Otherwise return false.
588 handle_split_stack_section(const char* name);
591 // This class may not be copied.
592 Object(const Object&);
593 Object& operator=(const Object&);
595 // Name of object as printed to user.
597 // For reading the file.
598 Input_file* input_file_;
599 // Offset within the file--0 for an object file, non-0 for an
602 // Number of input sections.
604 // Whether this is a dynamic object.
605 bool is_dynamic_ : 1;
606 // Whether this object is needed. This is only set for dynamic
607 // objects, and means that the object defined a symbol which was
608 // used by a reference from a regular object.
610 // Whether this object was compiled with -fsplit-stack.
611 bool uses_split_stack_ : 1;
612 // Whether this object contains any functions compiled with the
613 // no_split_stack attribute.
614 bool has_no_split_stack_ : 1;
615 // True if exclude this object from automatic symbol export.
616 // This is used only for archive objects.
618 // Many sections for objects with more than SHN_LORESERVE sections.
622 // A regular object (ET_REL). This is an abstract base class itself.
623 // The implementation is the template class Sized_relobj.
625 class Relobj : public Object
628 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
629 : Object(name, input_file, false, offset),
631 map_to_relocatable_relocs_(NULL),
632 object_merge_map_(NULL),
633 relocs_must_follow_section_writes_(false),
637 // During garbage collection, the Read_symbols_data pass for
638 // each object is stored as layout needs to be done after
642 { return this->sd_; }
644 // Decides which section names have to be included in the worklist
647 is_section_name_included(const char *name);
650 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
651 unsigned int section_header_size);
654 set_symbols_data(Symbols_data* sd)
657 // During garbage collection, the Read_relocs pass for all objects
658 // is done before scanning the relocs. In that case, this->rd_ is
659 // used to store the information from Read_relocs for each object.
660 // This data is also used to compute the list of relevant sections.
663 { return this->rd_; }
666 set_relocs_data(Read_relocs_data* rd)
670 is_output_section_offset_invalid(unsigned int shndx) const = 0;
674 read_relocs(Read_relocs_data* rd)
675 { return this->do_read_relocs(rd); }
677 // Process the relocs, during garbage collection only.
679 gc_process_relocs(const General_options& options, Symbol_table* symtab,
680 Layout* layout, Read_relocs_data* rd)
681 { return this->do_gc_process_relocs(options, symtab, layout, rd); }
683 // Scan the relocs and adjust the symbol table.
685 scan_relocs(const General_options& options, Symbol_table* symtab,
686 Layout* layout, Read_relocs_data* rd)
687 { return this->do_scan_relocs(options, symtab, layout, rd); }
689 // The number of local symbols in the input symbol table.
691 local_symbol_count() const
692 { return this->do_local_symbol_count(); }
694 // Initial local symbol processing: count the number of local symbols
695 // in the output symbol table and dynamic symbol table; add local symbol
696 // names to *POOL and *DYNPOOL.
698 count_local_symbols(Stringpool_template<char>* pool,
699 Stringpool_template<char>* dynpool)
700 { return this->do_count_local_symbols(pool, dynpool); }
702 // Set the values of the local symbols, set the output symbol table
703 // indexes for the local variables, and set the offset where local
704 // symbol information will be stored. Returns the new local symbol index.
706 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
707 { return this->do_finalize_local_symbols(index, off, symtab); }
709 // Set the output dynamic symbol table indexes for the local variables.
711 set_local_dynsym_indexes(unsigned int index)
712 { return this->do_set_local_dynsym_indexes(index); }
714 // Set the offset where local dynamic symbol information will be stored.
716 set_local_dynsym_offset(off_t off)
717 { return this->do_set_local_dynsym_offset(off); }
719 // Relocate the input sections and write out the local symbols.
721 relocate(const General_options& options, const Symbol_table* symtab,
722 const Layout* layout, Output_file* of)
723 { return this->do_relocate(options, symtab, layout, of); }
725 // Return whether an input section is being included in the link.
727 is_section_included(unsigned int shndx) const
729 gold_assert(shndx < this->output_sections_.size());
730 return this->output_sections_[shndx] != NULL;
733 // Given a section index, return the corresponding Output_section.
734 // The return value will be NULL if the section is not included in
737 output_section(unsigned int shndx) const
739 gold_assert(shndx < this->output_sections_.size());
740 return this->output_sections_[shndx];
743 // Given a section index, return the offset in the Output_section.
744 // The return value will be -1U if the section is specially mapped,
745 // such as a merge section.
747 output_section_offset(unsigned int shndx) const
748 { return this->do_output_section_offset(shndx); }
750 // Set the offset of an input section within its output section.
752 set_section_offset(unsigned int shndx, uint64_t off)
753 { this->do_set_section_offset(shndx, off); }
755 // Return true if we need to wait for output sections to be written
756 // before we can apply relocations. This is true if the object has
757 // any relocations for sections which require special handling, such
758 // as the exception frame section.
760 relocs_must_follow_section_writes() const
761 { return this->relocs_must_follow_section_writes_; }
763 // Return the object merge map.
766 { return this->object_merge_map_; }
768 // Set the object merge map.
770 set_merge_map(Object_merge_map* object_merge_map)
772 gold_assert(this->object_merge_map_ == NULL);
773 this->object_merge_map_ = object_merge_map;
776 // Record the relocatable reloc info for an input reloc section.
778 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
780 gold_assert(reloc_shndx < this->shnum());
781 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
784 // Get the relocatable reloc info for an input reloc section.
786 relocatable_relocs(unsigned int reloc_shndx)
788 gold_assert(reloc_shndx < this->shnum());
789 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
792 // Layout sections whose layout was deferred while waiting for
793 // input files from a plugin.
795 layout_deferred_sections(Layout* layout)
796 { this->do_layout_deferred_sections(layout); }
799 // The output section to be used for each input section, indexed by
800 // the input section number. The output section is NULL if the
801 // input section is to be discarded.
802 typedef std::vector<Output_section*> Output_sections;
804 // Read the relocs--implemented by child class.
806 do_read_relocs(Read_relocs_data*) = 0;
808 // Process the relocs--implemented by child class.
810 do_gc_process_relocs(const General_options&, Symbol_table*, Layout*,
811 Read_relocs_data*) = 0;
813 // Scan the relocs--implemented by child class.
815 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
816 Read_relocs_data*) = 0;
818 // Return the number of local symbols--implemented by child class.
820 do_local_symbol_count() const = 0;
822 // Count local symbols--implemented by child class.
824 do_count_local_symbols(Stringpool_template<char>*,
825 Stringpool_template<char>*) = 0;
827 // Finalize the local symbols. Set the output symbol table indexes
828 // for the local variables, and set the offset where local symbol
829 // information will be stored.
831 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
833 // Set the output dynamic symbol table indexes for the local variables.
835 do_set_local_dynsym_indexes(unsigned int) = 0;
837 // Set the offset where local dynamic symbol information will be stored.
839 do_set_local_dynsym_offset(off_t) = 0;
841 // Relocate the input sections and write out the local
842 // symbols--implemented by child class.
844 do_relocate(const General_options& options, const Symbol_table* symtab,
845 const Layout*, Output_file* of) = 0;
847 // Get the offset of a section--implemented by child class.
849 do_output_section_offset(unsigned int shndx) const = 0;
851 // Set the offset of a section--implemented by child class.
853 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
855 // Layout sections whose layout was deferred while waiting for
856 // input files from a plugin--implemented by child class.
858 do_layout_deferred_sections(Layout*) = 0;
860 // Return the vector mapping input sections to output sections.
863 { return this->output_sections_; }
865 const Output_sections&
866 output_sections() const
867 { return this->output_sections_; }
869 // Set the size of the relocatable relocs array.
871 size_relocatable_relocs()
873 this->map_to_relocatable_relocs_ =
874 new std::vector<Relocatable_relocs*>(this->shnum());
877 // Record that we must wait for the output sections to be written
878 // before applying relocations.
880 set_relocs_must_follow_section_writes()
881 { this->relocs_must_follow_section_writes_ = true; }
884 // Mapping from input sections to output section.
885 Output_sections output_sections_;
886 // Mapping from input section index to the information recorded for
887 // the relocations. This is only used for a relocatable link.
888 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
889 // Mappings for merge sections. This is managed by the code in the
891 Object_merge_map* object_merge_map_;
892 // Whether we need to wait for output sections to be written before
893 // we can apply relocations.
894 bool relocs_must_follow_section_writes_;
895 // Used to store the relocs data computed by the Read_relocs pass.
896 // Used during garbage collection of unused sections.
897 Read_relocs_data* rd_;
898 // Used to store the symbols data computed by the Read_symbols pass.
899 // Again used during garbage collection when laying out referenced
901 gold::Symbols_data *sd_;
904 // This class is used to handle relocations against a section symbol
905 // in an SHF_MERGE section. For such a symbol, we need to know the
906 // addend of the relocation before we can determine the final value.
907 // The addend gives us the location in the input section, and we can
908 // determine how it is mapped to the output section. For a
909 // non-section symbol, we apply the addend to the final value of the
910 // symbol; that is done in finalize_local_symbols, and does not use
914 class Merged_symbol_value
917 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
919 // We use a hash table to map offsets in the input section to output
921 typedef Unordered_map<section_offset_type, Value> Output_addresses;
923 Merged_symbol_value(Value input_value, Value output_start_address)
924 : input_value_(input_value), output_start_address_(output_start_address),
928 // Initialize the hash table.
930 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
932 // Release the hash table to save space.
934 free_input_to_output_map()
935 { this->output_addresses_.clear(); }
937 // Get the output value corresponding to an addend. The object and
938 // input section index are passed in because the caller will have
939 // them; otherwise we could store them here.
941 value(const Relobj* object, unsigned int input_shndx, Value addend) const
943 // This is a relocation against a section symbol. ADDEND is the
944 // offset in the section. The result should be the start of some
945 // merge area. If the object file wants something else, it should
946 // use a regular symbol rather than a section symbol.
947 // Unfortunately, PR 6658 shows a case in which the object file
948 // refers to the section symbol, but uses a negative ADDEND to
949 // compensate for a PC relative reloc. We can't handle the
950 // general case. However, we can handle the special case of a
951 // negative addend, by assuming that it refers to the start of the
952 // section. Of course, that means that we have to guess when
953 // ADDEND is negative. It is normal to see a 32-bit value here
954 // even when the template parameter size is 64, as 64-bit object
955 // file formats have 32-bit relocations. We know this is a merge
956 // section, so we know it has to fit into memory. So we assume
957 // that we won't see a value larger than a large 32-bit unsigned
958 // value. This will break objects with very very large merge
959 // sections; they probably break in other ways anyhow.
960 Value input_offset = this->input_value_;
961 if (addend < 0xffffff00)
963 input_offset += addend;
966 typename Output_addresses::const_iterator p =
967 this->output_addresses_.find(input_offset);
968 if (p != this->output_addresses_.end())
969 return p->second + addend;
971 return (this->value_from_output_section(object, input_shndx, input_offset)
976 // Get the output value for an input offset if we couldn't find it
977 // in the hash table.
979 value_from_output_section(const Relobj*, unsigned int input_shndx,
980 Value input_offset) const;
982 // The value of the section symbol in the input file. This is
983 // normally zero, but could in principle be something else.
985 // The start address of this merged section in the output file.
986 Value output_start_address_;
987 // A hash table which maps offsets in the input section to output
988 // addresses. This only maps specific offsets, not all offsets.
989 Output_addresses output_addresses_;
992 // This POD class is holds the value of a symbol. This is used for
993 // local symbols, and for all symbols during relocation processing.
994 // For special sections, such as SHF_MERGE sections, this calls a
995 // function to get the final symbol value.
1001 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1004 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
1005 is_ordinary_shndx_(false), is_section_symbol_(false),
1006 is_tls_symbol_(false), has_output_value_(true)
1007 { this->u_.value = 0; }
1009 // Get the value of this symbol. OBJECT is the object in which this
1010 // symbol is defined, and ADDEND is an addend to add to the value.
1011 template<bool big_endian>
1013 value(const Sized_relobj<size, big_endian>* object, Value addend) const
1015 if (this->has_output_value_)
1016 return this->u_.value + addend;
1019 gold_assert(this->is_ordinary_shndx_);
1020 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1025 // Set the value of this symbol in the output symbol table.
1027 set_output_value(Value value)
1028 { this->u_.value = value; }
1030 // For a section symbol in a merged section, we need more
1033 set_merged_symbol_value(Merged_symbol_value<size>* msv)
1035 gold_assert(this->is_section_symbol_);
1036 this->has_output_value_ = false;
1037 this->u_.merged_symbol_value = msv;
1040 // Initialize the input to output map for a section symbol in a
1041 // merged section. We also initialize the value of a non-section
1042 // symbol in a merged section.
1044 initialize_input_to_output_map(const Relobj* object)
1046 if (!this->has_output_value_)
1048 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
1049 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1050 msv->initialize_input_to_output_map(object, this->input_shndx_);
1054 // Free the input to output map for a section symbol in a merged
1057 free_input_to_output_map()
1059 if (!this->has_output_value_)
1060 this->u_.merged_symbol_value->free_input_to_output_map();
1063 // Set the value of the symbol from the input file. This is only
1064 // called by count_local_symbols, to communicate the value to
1065 // finalize_local_symbols.
1067 set_input_value(Value value)
1068 { this->u_.value = value; }
1070 // Return the input value. This is only called by
1071 // finalize_local_symbols and (in special cases) relocate_section.
1074 { return this->u_.value; }
1076 // Return whether this symbol should go into the output symbol
1079 needs_output_symtab_entry() const
1080 { return this->output_symtab_index_ != -1U; }
1082 // Return the index in the output symbol table.
1084 output_symtab_index() const
1086 gold_assert(this->output_symtab_index_ != 0);
1087 return this->output_symtab_index_;
1090 // Set the index in the output symbol table.
1092 set_output_symtab_index(unsigned int i)
1094 gold_assert(this->output_symtab_index_ == 0);
1095 this->output_symtab_index_ = i;
1098 // Record that this symbol should not go into the output symbol
1101 set_no_output_symtab_entry()
1103 gold_assert(this->output_symtab_index_ == 0);
1104 this->output_symtab_index_ = -1U;
1107 // Set the index in the output dynamic symbol table.
1109 set_needs_output_dynsym_entry()
1111 gold_assert(!this->is_section_symbol());
1112 this->output_dynsym_index_ = 0;
1115 // Return whether this symbol should go into the output symbol
1118 needs_output_dynsym_entry() const
1120 return this->output_dynsym_index_ != -1U;
1123 // Record that this symbol should go into the dynamic symbol table.
1125 set_output_dynsym_index(unsigned int i)
1127 gold_assert(this->output_dynsym_index_ == 0);
1128 this->output_dynsym_index_ = i;
1131 // Return the index in the output dynamic symbol table.
1133 output_dynsym_index() const
1135 gold_assert(this->output_dynsym_index_ != 0
1136 && this->output_dynsym_index_ != -1U);
1137 return this->output_dynsym_index_;
1140 // Set the index of the input section in the input file.
1142 set_input_shndx(unsigned int i, bool is_ordinary)
1144 this->input_shndx_ = i;
1145 // input_shndx_ field is a bitfield, so make sure that the value
1147 gold_assert(this->input_shndx_ == i);
1148 this->is_ordinary_shndx_ = is_ordinary;
1151 // Return the index of the input section in the input file.
1153 input_shndx(bool* is_ordinary) const
1155 *is_ordinary = this->is_ordinary_shndx_;
1156 return this->input_shndx_;
1159 // Whether this is a section symbol.
1161 is_section_symbol() const
1162 { return this->is_section_symbol_; }
1164 // Record that this is a section symbol.
1166 set_is_section_symbol()
1168 gold_assert(!this->needs_output_dynsym_entry());
1169 this->is_section_symbol_ = true;
1172 // Record that this is a TLS symbol.
1175 { this->is_tls_symbol_ = true; }
1177 // Return TRUE if this is a TLS symbol.
1179 is_tls_symbol() const
1180 { return this->is_tls_symbol_; }
1183 // The index of this local symbol in the output symbol table. This
1184 // will be -1 if the symbol should not go into the symbol table.
1185 unsigned int output_symtab_index_;
1186 // The index of this local symbol in the dynamic symbol table. This
1187 // will be -1 if the symbol should not go into the symbol table.
1188 unsigned int output_dynsym_index_;
1189 // The section index in the input file in which this symbol is
1191 unsigned int input_shndx_ : 28;
1192 // Whether the section index is an ordinary index, not a special
1194 bool is_ordinary_shndx_ : 1;
1195 // Whether this is a STT_SECTION symbol.
1196 bool is_section_symbol_ : 1;
1197 // Whether this is a STT_TLS symbol.
1198 bool is_tls_symbol_ : 1;
1199 // Whether this symbol has a value for the output file. This is
1200 // normally set to true during Layout::finalize, by
1201 // finalize_local_symbols. It will be false for a section symbol in
1202 // a merge section, as for such symbols we can not determine the
1203 // value to use in a relocation until we see the addend.
1204 bool has_output_value_ : 1;
1207 // This is used if has_output_value_ is true. Between
1208 // count_local_symbols and finalize_local_symbols, this is the
1209 // value in the input file. After finalize_local_symbols, it is
1210 // the value in the output file.
1212 // This is used if has_output_value_ is false. It points to the
1213 // information we need to get the value for a merge section.
1214 Merged_symbol_value<size>* merged_symbol_value;
1218 // A GOT offset list. A symbol may have more than one GOT offset
1219 // (e.g., when mixing modules compiled with two different TLS models),
1220 // but will usually have at most one. GOT_TYPE identifies the type of
1221 // GOT entry; its values are specific to each target.
1223 class Got_offset_list
1227 : got_type_(-1U), got_offset_(0), got_next_(NULL)
1230 Got_offset_list(unsigned int got_type, unsigned int got_offset)
1231 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
1236 if (this->got_next_ != NULL)
1238 delete this->got_next_;
1239 this->got_next_ = NULL;
1243 // Initialize the fields to their default values.
1247 this->got_type_ = -1U;
1248 this->got_offset_ = 0;
1249 this->got_next_ = NULL;
1252 // Set the offset for the GOT entry of type GOT_TYPE.
1254 set_offset(unsigned int got_type, unsigned int got_offset)
1256 if (this->got_type_ == -1U)
1258 this->got_type_ = got_type;
1259 this->got_offset_ = got_offset;
1263 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
1265 if (g->got_type_ == got_type)
1267 g->got_offset_ = got_offset;
1271 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1272 g->got_next_ = this->got_next_;
1273 this->got_next_ = g;
1277 // Return the offset for a GOT entry of type GOT_TYPE.
1279 get_offset(unsigned int got_type) const
1281 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
1283 if (g->got_type_ == got_type)
1284 return g->got_offset_;
1290 unsigned int got_type_;
1291 unsigned int got_offset_;
1292 Got_offset_list* got_next_;
1295 // This type is used to modify relocations for -fsplit-stack. It is
1296 // indexed by relocation index, and means that the relocation at that
1297 // index should use the symbol from the vector, rather than the one
1298 // indicated by the relocation.
1300 class Reloc_symbol_changes
1303 Reloc_symbol_changes(size_t count)
1308 set(size_t i, Symbol* sym)
1309 { this->vec_[i] = sym; }
1312 operator[](size_t i) const
1313 { return this->vec_[i]; }
1316 std::vector<Symbol*> vec_;
1319 // A regular object file. This is size and endian specific.
1321 template<int size, bool big_endian>
1322 class Sized_relobj : public Relobj
1325 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1326 typedef std::vector<Symbol*> Symbols;
1327 typedef std::vector<Symbol_value<size> > Local_values;
1329 static const Address invalid_address = static_cast<Address>(0) - 1;
1331 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
1332 const typename elfcpp::Ehdr<size, big_endian>&);
1336 // Checks if the offset of input section SHNDX within its output
1337 // section is invalid.
1339 is_output_section_offset_invalid(unsigned int shndx) const
1340 { return this->get_output_section_offset(shndx) == invalid_address; }
1342 // Set up the object file based on TARGET.
1345 { this->do_setup(); }
1347 // Return the number of symbols. This is only valid after
1348 // Object::add_symbols has been called.
1350 symbol_count() const
1351 { return this->local_symbol_count_ + this->symbols_.size(); }
1353 // If SYM is the index of a global symbol in the object file's
1354 // symbol table, return the Symbol object. Otherwise, return NULL.
1356 global_symbol(unsigned int sym) const
1358 if (sym >= this->local_symbol_count_)
1360 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1361 return this->symbols_[sym - this->local_symbol_count_];
1366 // Return the section index of symbol SYM. Set *VALUE to its value
1367 // in the object file. Set *IS_ORDINARY if this is an ordinary
1368 // section index, not a special code between SHN_LORESERVE and
1369 // SHN_HIRESERVE. Note that for a symbol which is not defined in
1370 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
1371 // it will not return the final value of the symbol in the link.
1373 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
1375 // Return a pointer to the Symbol_value structure which holds the
1376 // value of a local symbol.
1377 const Symbol_value<size>*
1378 local_symbol(unsigned int sym) const
1380 gold_assert(sym < this->local_values_.size());
1381 return &this->local_values_[sym];
1384 // Return the index of local symbol SYM in the ordinary symbol
1385 // table. A value of -1U means that the symbol is not being output.
1387 symtab_index(unsigned int sym) const
1389 gold_assert(sym < this->local_values_.size());
1390 return this->local_values_[sym].output_symtab_index();
1393 // Return the index of local symbol SYM in the dynamic symbol
1394 // table. A value of -1U means that the symbol is not being output.
1396 dynsym_index(unsigned int sym) const
1398 gold_assert(sym < this->local_values_.size());
1399 return this->local_values_[sym].output_dynsym_index();
1402 // Return the input section index of local symbol SYM.
1404 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
1406 gold_assert(sym < this->local_values_.size());
1407 return this->local_values_[sym].input_shndx(is_ordinary);
1410 // Record that local symbol SYM needs a dynamic symbol entry.
1412 set_needs_output_dynsym_entry(unsigned int sym)
1414 gold_assert(sym < this->local_values_.size());
1415 this->local_values_[sym].set_needs_output_dynsym_entry();
1418 // Return whether the local symbol SYMNDX has a GOT offset.
1419 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1421 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1423 Local_got_offsets::const_iterator p =
1424 this->local_got_offsets_.find(symndx);
1425 return (p != this->local_got_offsets_.end()
1426 && p->second->get_offset(got_type) != -1U);
1429 // Return the GOT offset of the local symbol SYMNDX.
1431 local_got_offset(unsigned int symndx, unsigned int got_type) const
1433 Local_got_offsets::const_iterator p =
1434 this->local_got_offsets_.find(symndx);
1435 gold_assert(p != this->local_got_offsets_.end());
1436 unsigned int off = p->second->get_offset(got_type);
1437 gold_assert(off != -1U);
1441 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1443 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1444 unsigned int got_offset)
1446 Local_got_offsets::const_iterator p =
1447 this->local_got_offsets_.find(symndx);
1448 if (p != this->local_got_offsets_.end())
1449 p->second->set_offset(got_type, got_offset);
1452 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
1453 std::pair<Local_got_offsets::iterator, bool> ins =
1454 this->local_got_offsets_.insert(std::make_pair(symndx, g));
1455 gold_assert(ins.second);
1459 // Get the offset of input section SHNDX within its output section.
1460 // This is -1 if the input section requires a special mapping, such
1461 // as a merge section. The output section can be found in the
1462 // output_sections_ field of the parent class Relobj.
1464 get_output_section_offset(unsigned int shndx) const
1466 gold_assert(shndx < this->section_offsets_.size());
1467 return this->section_offsets_[shndx];
1470 // Return the name of the symbol that spans the given offset in the
1471 // specified section in this object. This is used only for error
1472 // messages and is not particularly efficient.
1474 get_symbol_location_info(unsigned int shndx, off_t offset,
1475 Symbol_location_info* info);
1477 // Look for a kept section corresponding to the given discarded section,
1478 // and return its output address. This is used only for relocations in
1479 // debugging sections.
1481 map_to_kept_section(unsigned int shndx, bool* found) const;
1483 // Make section offset invalid. This is needed for relaxation.
1485 invalidate_section_offset(unsigned int shndx)
1486 { this->do_invalidate_section_offset(shndx); }
1493 // Read the symbols.
1495 do_read_symbols(Read_symbols_data*);
1497 // Return the number of local symbols.
1499 do_local_symbol_count() const
1500 { return this->local_symbol_count_; }
1502 // Lay out the input sections.
1504 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1506 // Layout sections whose layout was deferred while waiting for
1507 // input files from a plugin.
1509 do_layout_deferred_sections(Layout*);
1511 // Add the symbols to the symbol table.
1513 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1517 do_read_relocs(Read_relocs_data*);
1519 // Process the relocs to find list of referenced sections. Used only
1520 // during garbage collection.
1522 do_gc_process_relocs(const General_options&, Symbol_table*, Layout*,
1525 // Scan the relocs and adjust the symbol table.
1527 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1530 // Count the local symbols.
1532 do_count_local_symbols(Stringpool_template<char>*,
1533 Stringpool_template<char>*);
1535 // Finalize the local symbols.
1537 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1539 // Set the offset where local dynamic symbol information will be stored.
1541 do_set_local_dynsym_indexes(unsigned int);
1543 // Set the offset where local dynamic symbol information will be stored.
1545 do_set_local_dynsym_offset(off_t);
1547 // Relocate the input sections and write out the local symbols.
1549 do_relocate(const General_options& options, const Symbol_table* symtab,
1550 const Layout*, Output_file* of);
1552 // Get the size of a section.
1554 do_section_size(unsigned int shndx)
1555 { return this->elf_file_.section_size(shndx); }
1557 // Get the name of a section.
1559 do_section_name(unsigned int shndx)
1560 { return this->elf_file_.section_name(shndx); }
1562 // Return the location of the contents of a section.
1564 do_section_contents(unsigned int shndx)
1565 { return this->elf_file_.section_contents(shndx); }
1567 // Return section flags.
1569 do_section_flags(unsigned int shndx);
1571 // Return section entsize.
1573 do_section_entsize(unsigned int shndx);
1575 // Return section address.
1577 do_section_address(unsigned int shndx)
1578 { return this->elf_file_.section_addr(shndx); }
1580 // Return section type.
1582 do_section_type(unsigned int shndx)
1583 { return this->elf_file_.section_type(shndx); }
1585 // Return the section link field.
1587 do_section_link(unsigned int shndx)
1588 { return this->elf_file_.section_link(shndx); }
1590 // Return the section info field.
1592 do_section_info(unsigned int shndx)
1593 { return this->elf_file_.section_info(shndx); }
1595 // Return the section alignment.
1597 do_section_addralign(unsigned int shndx)
1598 { return this->elf_file_.section_addralign(shndx); }
1600 // Return the Xindex structure to use.
1602 do_initialize_xindex();
1604 // Get symbol counts.
1606 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1608 // Get the offset of a section.
1610 do_output_section_offset(unsigned int shndx) const
1612 Address off = this->get_output_section_offset(shndx);
1613 if (off == invalid_address)
1618 // Set the offset of a section.
1620 do_set_section_offset(unsigned int shndx, uint64_t off)
1622 gold_assert(shndx < this->section_offsets_.size());
1623 this->section_offsets_[shndx] = convert_types<Address, uint64_t>(off);
1626 // Set the offset of a section to invalid_address.
1628 do_invalidate_section_offset(unsigned int shndx)
1630 gold_assert(shndx < this->section_offsets_.size());
1631 this->section_offsets_[shndx] = invalid_address;
1634 // Adjust a section index if necessary.
1636 adjust_shndx(unsigned int shndx)
1638 if (shndx >= elfcpp::SHN_LORESERVE)
1639 shndx += this->elf_file_.large_shndx_offset();
1643 // Initialize input to output maps for section symbols in merged
1646 initialize_input_to_output_maps();
1648 // Free the input to output maps for section symbols in merged
1651 free_input_to_output_maps();
1653 // Return symbol table section index.
1655 symtab_shndx() const
1656 { return this->symtab_shndx_; }
1658 // Allow a child class to access the ELF file.
1659 elfcpp::Elf_file<size, big_endian, Object>*
1661 { return &this->elf_file_; }
1663 // Allow a child class to access the local values.
1666 { return &this->local_values_; }
1670 typedef Sized_relobj<size, big_endian> This;
1671 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1672 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1673 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1674 typedef elfcpp::Shdr<size, big_endian> Shdr;
1676 // To keep track of discarded comdat sections, we need to map a member
1677 // section index to the object and section index of the corresponding
1679 struct Kept_comdat_section
1681 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx)
1682 : object(a_object), shndx(a_shndx)
1687 typedef std::map<unsigned int, Kept_comdat_section>
1688 Kept_comdat_section_table;
1690 // Find the SHT_SYMTAB section, given the section headers.
1692 find_symtab(const unsigned char* pshdrs);
1694 // Return whether SHDR has the right flags for a GNU style exception
1697 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1699 // Return whether there is a section named .eh_frame which might be
1700 // a GNU style exception frame section.
1702 find_eh_frame(const unsigned char* pshdrs, const char* names,
1703 section_size_type names_size) const;
1705 // Whether to include a section group in the link.
1707 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1708 const unsigned char*, const char *, section_size_type,
1709 std::vector<bool>*);
1711 // Whether to include a linkonce section in the link.
1713 include_linkonce_section(Layout*, unsigned int, const char*,
1714 const elfcpp::Shdr<size, big_endian>&);
1716 // Layout an input section.
1718 layout_section(Layout* layout, unsigned int shndx, const char* name,
1719 typename This::Shdr& shdr, unsigned int reloc_shndx,
1720 unsigned int reloc_type);
1722 // Views and sizes when relocating.
1725 unsigned char* view;
1726 typename elfcpp::Elf_types<size>::Elf_Addr address;
1728 section_size_type view_size;
1729 bool is_input_output_view;
1730 bool is_postprocessing_view;
1733 typedef std::vector<View_size> Views;
1735 // Write section data to the output file. Record the views and
1736 // sizes in VIEWS for use when relocating.
1738 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1740 // Relocate the sections in the output file.
1742 relocate_sections(const General_options& options, const Symbol_table*,
1743 const Layout*, const unsigned char* pshdrs, Views*);
1745 // Scan the input relocations for --emit-relocs.
1747 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1748 const unsigned char* plocal_syms,
1749 const Read_relocs_data::Relocs_list::iterator&);
1751 // Scan the input relocations for --emit-relocs, templatized on the
1752 // type of the relocation section.
1753 template<int sh_type>
1755 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1756 const unsigned char* plocal_syms,
1757 const Read_relocs_data::Relocs_list::iterator&,
1758 Relocatable_relocs*);
1760 // Emit the relocs for --emit-relocs.
1762 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1763 unsigned int sh_type, const unsigned char* prelocs,
1764 size_t reloc_count, Output_section*, Address output_offset,
1765 unsigned char* view, Address address,
1766 section_size_type view_size,
1767 unsigned char* reloc_view, section_size_type reloc_view_size);
1769 // Emit the relocs for --emit-relocs, templatized on the type of the
1770 // relocation section.
1771 template<int sh_type>
1773 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1774 const unsigned char* prelocs, size_t reloc_count,
1775 Output_section*, Address output_offset,
1776 unsigned char* view, Address address,
1777 section_size_type view_size,
1778 unsigned char* reloc_view,
1779 section_size_type reloc_view_size);
1781 // A type shared by split_stack_adjust_reltype and find_functions.
1782 typedef std::map<section_offset_type, section_size_type> Function_offsets;
1784 // Check for -fsplit-stack routines calling non-split-stack routines.
1786 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
1787 unsigned int sh_type, unsigned int shndx,
1788 const unsigned char* prelocs, size_t reloc_count,
1789 unsigned char* view, section_size_type view_size,
1790 Reloc_symbol_changes** reloc_map);
1792 template<int sh_type>
1794 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
1795 unsigned int shndx, const unsigned char* prelocs,
1796 size_t reloc_count, unsigned char* view,
1797 section_size_type view_size,
1798 Reloc_symbol_changes** reloc_map);
1800 // Find all functions in a section.
1802 find_functions(const unsigned char* pshdrs, unsigned int shndx,
1805 // Write out the local symbols.
1807 write_local_symbols(Output_file*,
1808 const Stringpool_template<char>*,
1809 const Stringpool_template<char>*,
1810 Output_symtab_xindex*,
1811 Output_symtab_xindex*);
1813 // Clear the local symbol information.
1815 clear_local_symbols()
1817 this->local_values_.clear();
1818 this->local_got_offsets_.clear();
1821 // Record a mapping from discarded section SHNDX to the corresponding
1824 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object,
1825 unsigned int kept_shndx)
1827 Kept_comdat_section kept(kept_object, kept_shndx);
1828 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
1831 // Find the kept section corresponding to the discarded section
1832 // SHNDX. Return true if found.
1834 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object,
1835 unsigned int* kept_shndx) const
1837 typename Kept_comdat_section_table::const_iterator p =
1838 this->kept_comdat_sections_.find(shndx);
1839 if (p == this->kept_comdat_sections_.end())
1841 *kept_object = p->second.object;
1842 *kept_shndx = p->second.shndx;
1846 // The GOT offsets of local symbols. This map also stores GOT offsets
1847 // for tp-relative offsets for TLS symbols.
1848 typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
1850 // The TLS GOT offsets of local symbols. The map stores the offsets
1851 // for either a single GOT entry that holds the module index of a TLS
1852 // symbol, or a pair of GOT entries containing the module index and
1853 // dtv-relative offset.
1854 struct Tls_got_entry
1856 Tls_got_entry(int got_offset, bool have_pair)
1857 : got_offset_(got_offset),
1858 have_pair_(have_pair)
1863 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1865 // Saved information for sections whose layout was deferred.
1866 struct Deferred_layout
1868 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1869 Deferred_layout(unsigned int shndx, const char* name,
1870 const unsigned char* pshdr,
1871 unsigned int reloc_shndx, unsigned int reloc_type)
1872 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx),
1873 reloc_type_(reloc_type)
1875 memcpy(this->shdr_data_, pshdr, shdr_size);
1877 unsigned int shndx_;
1879 unsigned int reloc_shndx_;
1880 unsigned int reloc_type_;
1881 unsigned char shdr_data_[shdr_size];
1884 // General access to the ELF file.
1885 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1886 // Index of SHT_SYMTAB section.
1887 unsigned int symtab_shndx_;
1888 // The number of local symbols.
1889 unsigned int local_symbol_count_;
1890 // The number of local symbols which go into the output file.
1891 unsigned int output_local_symbol_count_;
1892 // The number of local symbols which go into the output file's dynamic
1894 unsigned int output_local_dynsym_count_;
1895 // The entries in the symbol table for the external symbols.
1897 // Number of symbols defined in object file itself.
1898 size_t defined_count_;
1899 // File offset for local symbols.
1900 off_t local_symbol_offset_;
1901 // File offset for local dynamic symbols.
1902 off_t local_dynsym_offset_;
1903 // Values of local symbols.
1904 Local_values local_values_;
1905 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1906 // for TLS symbols, indexed by symbol number.
1907 Local_got_offsets local_got_offsets_;
1908 // For each input section, the offset of the input section in its
1909 // output section. This is INVALID_ADDRESS if the input section requires a
1911 std::vector<Address> section_offsets_;
1912 // Table mapping discarded comdat sections to corresponding kept sections.
1913 Kept_comdat_section_table kept_comdat_sections_;
1914 // Whether this object has a GNU style .eh_frame section.
1916 // If this object has a GNU style .eh_frame section that is discarded in
1917 // output, record the index here. Otherwise it is -1U.
1918 unsigned int discarded_eh_frame_shndx_;
1919 // The list of sections whose layout was deferred.
1920 std::vector<Deferred_layout> deferred_layout_;
1923 // A class to manage the list of all objects.
1929 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
1932 // The type of the list of input relocateable objects.
1933 typedef std::vector<Relobj*> Relobj_list;
1934 typedef Relobj_list::const_iterator Relobj_iterator;
1936 // The type of the list of input dynamic objects.
1937 typedef std::vector<Dynobj*> Dynobj_list;
1938 typedef Dynobj_list::const_iterator Dynobj_iterator;
1940 // Add an object to the list. Return true if all is well, or false
1941 // if this object should be ignored.
1943 add_object(Object*);
1945 // Start processing an archive.
1947 archive_start(Archive*);
1949 // Stop processing an archive.
1951 archive_stop(Archive*);
1953 // For each dynamic object, check whether we've seen all of its
1954 // explicit dependencies.
1956 check_dynamic_dependencies() const;
1958 // Return whether an object was found in the system library
1961 found_in_system_library_directory(const Object*) const;
1963 // Print symbol counts.
1965 print_symbol_counts(const Symbol_table*) const;
1967 // Iterate over all regular objects.
1970 relobj_begin() const
1971 { return this->relobj_list_.begin(); }
1975 { return this->relobj_list_.end(); }
1977 // Iterate over all dynamic objects.
1980 dynobj_begin() const
1981 { return this->dynobj_list_.begin(); }
1985 { return this->dynobj_list_.end(); }
1987 // Return whether we have seen any dynamic objects.
1990 { return !this->dynobj_list_.empty(); }
1992 // Return the number of input objects.
1994 number_of_input_objects() const
1995 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1998 Input_objects(const Input_objects&);
1999 Input_objects& operator=(const Input_objects&);
2001 // The list of ordinary objects included in the link.
2002 Relobj_list relobj_list_;
2003 // The list of dynamic objects included in the link.
2004 Dynobj_list dynobj_list_;
2005 // SONAMEs that we have seen.
2006 Unordered_set<std::string> sonames_;
2007 // Manage cross-references if requested.
2011 // Some of the information we pass to the relocation routines. We
2012 // group this together to avoid passing a dozen different arguments.
2014 template<int size, bool big_endian>
2015 struct Relocate_info
2017 // Command line options.
2018 const General_options* options;
2020 const Symbol_table* symtab;
2022 const Layout* layout;
2023 // Object being relocated.
2024 Sized_relobj<size, big_endian>* object;
2025 // Section index of relocation section.
2026 unsigned int reloc_shndx;
2027 // Section header of relocation section.
2028 const unsigned char* reloc_shdr;
2029 // Section index of section being relocated.
2030 unsigned int data_shndx;
2031 // Section header of data section.
2032 const unsigned char* data_shdr;
2034 // Return a string showing the location of a relocation. This is
2035 // only used for error messages.
2037 location(size_t relnum, off_t reloffset) const;
2040 // Return whether INPUT_FILE contains an ELF object start at file
2041 // offset OFFSET. This sets *START to point to a view of the start of
2042 // the file. It sets *READ_SIZE to the number of bytes in the view.
2045 is_elf_object(Input_file* input_file, off_t offset,
2046 const unsigned char** start, int *read_size);
2048 // Return an Object appropriate for the input file. P is BYTES long,
2049 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
2050 // this sees an object the linker is not configured to support, it
2051 // sets *PUNCONFIGURED to true and returns NULL without giving an
2055 make_elf_object(const std::string& name, Input_file*,
2056 off_t offset, const unsigned char* p,
2057 section_offset_type bytes, bool* punconfigured);
2059 } // end namespace gold
2061 #endif // !defined(GOLD_OBJECT_H)