1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright (C) 2006-2017 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;
44 class Output_section_data;
46 class Output_symtab_xindex;
49 class Object_merge_map;
50 class Relocatable_relocs;
53 template<typename Stringpool_char>
54 class Stringpool_template;
56 // Data to pass from read_symbols() to add_symbols().
58 struct Read_symbols_data
61 : section_headers(NULL), section_names(NULL), symbols(NULL),
62 symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL)
68 File_view* section_headers;
70 File_view* section_names;
71 // Size of section name data in bytes.
72 section_size_type section_names_size;
75 // Size of symbol data in bytes.
76 section_size_type symbols_size;
77 // Offset of external symbols within symbol data. This structure
78 // sometimes contains only external symbols, in which case this will
79 // be zero. Sometimes it contains all symbols.
80 section_offset_type external_symbols_offset;
82 File_view* symbol_names;
83 // Size of symbol name data in bytes.
84 section_size_type symbol_names_size;
86 // Version information. This is only used on dynamic objects.
87 // Version symbol data (from SHT_GNU_versym section).
89 section_size_type versym_size;
90 // Version definition data (from SHT_GNU_verdef section).
92 section_size_type verdef_size;
93 unsigned int verdef_info;
94 // Needed version data (from SHT_GNU_verneed section).
96 section_size_type verneed_size;
97 unsigned int verneed_info;
100 // Information used to print error messages.
102 struct Symbol_location_info
104 std::string source_file;
105 std::string enclosing_symbol_name;
106 elfcpp::STT enclosing_symbol_type;
109 // Data about a single relocation section. This is read in
110 // read_relocs and processed in scan_relocs.
112 struct Section_relocs
119 { delete this->contents; }
121 // Index of reloc section.
122 unsigned int reloc_shndx;
123 // Index of section that relocs apply to.
124 unsigned int data_shndx;
125 // Contents of reloc section.
127 // Reloc section type.
128 unsigned int sh_type;
129 // Number of reloc entries.
132 Output_section* output_section;
133 // Whether this section has special handling for offsets.
134 bool needs_special_offset_handling;
135 // Whether the data section is allocated (has the SHF_ALLOC flag set).
136 bool is_data_section_allocated;
139 // Relocations in an object file. This is read in read_relocs and
140 // processed in scan_relocs.
142 struct Read_relocs_data
145 : local_symbols(NULL)
149 { delete this->local_symbols; }
151 typedef std::vector<Section_relocs> Relocs_list;
154 // The local symbols.
155 File_view* local_symbols;
158 // The Xindex class manages section indexes for objects with more than
164 Xindex(int large_shndx_offset)
165 : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
168 // Initialize the symtab_xindex_ array, given the object and the
169 // section index of the symbol table to use.
170 template<int size, bool big_endian>
172 initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
174 // Read in the symtab_xindex_ array, given its section index.
175 // PSHDRS may optionally point to the section headers.
176 template<int size, bool big_endian>
178 read_symtab_xindex(Object*, unsigned int xindex_shndx,
179 const unsigned char* pshdrs);
181 // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
182 // real section index.
184 sym_xindex_to_shndx(Object* object, unsigned int symndx);
187 // The type of the array giving the real section index for symbols
188 // whose st_shndx field holds SHN_XINDEX.
189 typedef std::vector<unsigned int> Symtab_xindex;
191 // Adjust a section index if necessary. This should only be called
192 // for ordinary section indexes.
194 adjust_shndx(unsigned int shndx)
196 if (shndx >= elfcpp::SHN_LORESERVE)
197 shndx += this->large_shndx_offset_;
201 // Adjust to apply to large section indexes.
202 int large_shndx_offset_;
203 // The data from the SHT_SYMTAB_SHNDX section.
204 Symtab_xindex symtab_xindex_;
207 // A GOT offset list. A symbol may have more than one GOT offset
208 // (e.g., when mixing modules compiled with two different TLS models),
209 // but will usually have at most one. GOT_TYPE identifies the type of
210 // GOT entry; its values are specific to each target.
212 class Got_offset_list
216 : got_type_(-1U), got_offset_(0), got_next_(NULL)
219 Got_offset_list(unsigned int got_type, unsigned int got_offset)
220 : got_type_(got_type), got_offset_(got_offset), got_next_(NULL)
225 if (this->got_next_ != NULL)
227 delete this->got_next_;
228 this->got_next_ = NULL;
232 // Initialize the fields to their default values.
236 this->got_type_ = -1U;
237 this->got_offset_ = 0;
238 this->got_next_ = NULL;
241 // Set the offset for the GOT entry of type GOT_TYPE.
243 set_offset(unsigned int got_type, unsigned int got_offset)
245 if (this->got_type_ == -1U)
247 this->got_type_ = got_type;
248 this->got_offset_ = got_offset;
252 for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
254 if (g->got_type_ == got_type)
256 g->got_offset_ = got_offset;
260 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
261 g->got_next_ = this->got_next_;
266 // Return the offset for a GOT entry of type GOT_TYPE.
268 get_offset(unsigned int got_type) const
270 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
272 if (g->got_type_ == got_type)
273 return g->got_offset_;
278 // Return a pointer to the list, or NULL if the list is empty.
279 const Got_offset_list*
282 if (this->got_type_ == -1U)
287 // Abstract visitor class for iterating over GOT offsets.
299 visit(unsigned int, unsigned int) = 0;
302 // Loop over all GOT offset entries, calling a visitor class V for each.
304 for_all_got_offsets(Visitor* v) const
306 if (this->got_type_ == -1U)
308 for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
309 v->visit(g->got_type_, g->got_offset_);
313 unsigned int got_type_;
314 unsigned int got_offset_;
315 Got_offset_list* got_next_;
318 // The Local_got_entry_key used to index the GOT offsets for local
319 // non-TLS symbols, and tp-relative offsets for TLS symbols.
321 class Local_got_entry_key
324 Local_got_entry_key(unsigned int symndx, uint64_t addend)
325 : symndx_(symndx), addend_(addend)
328 // Whether this equals to another Local_got_entry_key.
330 eq(const Local_got_entry_key& key) const
332 return (this->symndx_ == key.symndx_ && this->addend_ == key.addend_);
335 // Compute a hash value for this using 64-bit FNV-1a hash.
339 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
340 uint64_t prime = 1099511628211ULL;
341 h = (h ^ static_cast<uint64_t>(this->symndx_)) * prime;
342 h = (h ^ static_cast<uint64_t>(this->addend_)) * prime;
346 // Functors for associative containers.
350 operator()(const Local_got_entry_key& key1,
351 const Local_got_entry_key& key2) const
352 { return key1.eq(key2); }
358 operator()(const Local_got_entry_key& key) const
359 { return key.hash_value(); }
363 // The local symbol index.
364 unsigned int symndx_;
369 // Type for mapping section index to uncompressed size and contents.
371 struct Compressed_section_info
373 section_size_type size;
374 elfcpp::Elf_Xword flag;
375 const unsigned char* contents;
377 typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map;
379 template<int size, bool big_endian>
380 Compressed_section_map*
381 build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum,
382 const char* names, section_size_type names_size,
383 Object* obj, bool decompress_if_needed);
385 // Object is an abstract base class which represents either a 32-bit
386 // or a 64-bit input object. This can be a regular object file
387 // (ET_REL) or a shared object (ET_DYN).
392 typedef std::vector<Symbol*> Symbols;
394 // NAME is the name of the object as we would report it to the user
395 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
396 // used to read the file. OFFSET is the offset within the input
397 // file--0 for a .o or .so file, something else for a .a file.
398 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
400 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
401 is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
402 has_no_split_stack_(false), no_export_(false),
403 is_in_system_directory_(false), as_needed_(false), xindex_(NULL),
404 compressed_sections_(NULL)
406 if (input_file != NULL)
408 input_file->file().add_object();
409 this->is_in_system_directory_ = input_file->is_in_system_directory();
410 this->as_needed_ = input_file->options().as_needed();
416 if (this->input_file_ != NULL)
417 this->input_file_->file().remove_object();
420 // Return the name of the object as we would report it to the user.
423 { return this->name_; }
425 // Get the offset into the file.
428 { return this->offset_; }
430 // Return whether this is a dynamic object.
433 { return this->is_dynamic_; }
435 // Return the word size of the object file.
436 virtual int elfsize() const = 0;
438 // Return TRUE if this is a big-endian object file.
439 virtual bool is_big_endian() const = 0;
441 // Return whether this object is needed--true if it is a dynamic
442 // object which defines some symbol referenced by a regular object.
443 // We keep the flag here rather than in Dynobj for convenience when
447 { return this->is_needed_; }
449 // Record that this object is needed.
452 { this->is_needed_ = true; }
454 // Return whether this object was compiled with -fsplit-stack.
456 uses_split_stack() const
457 { return this->uses_split_stack_; }
459 // Return whether this object contains any functions compiled with
460 // the no_split_stack attribute.
462 has_no_split_stack() const
463 { return this->has_no_split_stack_; }
465 // Returns NULL for Objects that are not dynamic objects. This method
466 // is overridden in the Dynobj class.
469 { return this->do_dynobj(); }
471 // Returns NULL for Objects that are not plugin objects. This method
472 // is overridden in the Pluginobj class.
475 { return this->do_pluginobj(); }
477 // Get the file. We pass on const-ness.
481 gold_assert(this->input_file_ != NULL);
482 return this->input_file_;
488 gold_assert(this->input_file_ != NULL);
489 return this->input_file_;
492 // Lock the underlying file.
496 if (this->input_file_ != NULL)
497 this->input_file_->file().lock(t);
500 // Unlock the underlying file.
502 unlock(const Task* t)
504 if (this->input_file_ != NULL)
505 this->input_file()->file().unlock(t);
508 // Return whether the underlying file is locked.
511 { return this->input_file_ != NULL && this->input_file_->file().is_locked(); }
513 // Return the token, so that the task can be queued.
517 if (this->input_file_ == NULL)
519 return this->input_file()->file().token();
522 // Release the underlying file.
526 if (this->input_file_ != NULL)
527 this->input_file()->file().release();
530 // Return whether we should just read symbols from this file.
533 { return this->input_file()->just_symbols(); }
535 // Return whether this is an incremental object.
537 is_incremental() const
538 { return this->do_is_incremental(); }
540 // Return the last modified time of the file.
543 { return this->do_get_mtime(); }
545 // Get the number of sections.
548 { return this->shnum_; }
550 // Return a view of the contents of a section. Set *PLEN to the
551 // size. CACHE is a hint as in File_read::get_view.
553 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
555 // Adjust a symbol's section index as needed. SYMNDX is the index
556 // of the symbol and SHNDX is the symbol's section from
557 // get_st_shndx. This returns the section index. It sets
558 // *IS_ORDINARY to indicate whether this is a normal section index,
559 // rather than a special code between SHN_LORESERVE and
562 adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
564 if (shndx < elfcpp::SHN_LORESERVE)
566 else if (shndx == elfcpp::SHN_XINDEX)
568 if (this->xindex_ == NULL)
569 this->xindex_ = this->do_initialize_xindex();
570 shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
574 *is_ordinary = false;
578 // Return the size of a section given a section index.
580 section_size(unsigned int shndx)
581 { return this->do_section_size(shndx); }
583 // Return the name of a section given a section index.
585 section_name(unsigned int shndx) const
586 { return this->do_section_name(shndx); }
588 // Return the section flags given a section index.
590 section_flags(unsigned int shndx)
591 { return this->do_section_flags(shndx); }
593 // Return the section entsize given a section index.
595 section_entsize(unsigned int shndx)
596 { return this->do_section_entsize(shndx); }
598 // Return the section address given a section index.
600 section_address(unsigned int shndx)
601 { return this->do_section_address(shndx); }
603 // Return the section type given a section index.
605 section_type(unsigned int shndx)
606 { return this->do_section_type(shndx); }
608 // Return the section link field given a section index.
610 section_link(unsigned int shndx)
611 { return this->do_section_link(shndx); }
613 // Return the section info field given a section index.
615 section_info(unsigned int shndx)
616 { return this->do_section_info(shndx); }
618 // Return the required section alignment given a section index.
620 section_addralign(unsigned int shndx)
621 { return this->do_section_addralign(shndx); }
623 // Return the output section given a section index.
625 output_section(unsigned int shndx) const
626 { return this->do_output_section(shndx); }
628 // Given a section index, return its address.
629 // The return value will be -1U if the section is specially mapped,
630 // such as a merge section.
632 output_section_address(unsigned int shndx)
633 { return this->do_output_section_address(shndx); }
635 // Given a section index, return the offset in the Output_section.
636 // The return value will be -1U if the section is specially mapped,
637 // such as a merge section.
639 output_section_offset(unsigned int shndx) const
640 { return this->do_output_section_offset(shndx); }
642 // Read the symbol information.
644 read_symbols(Read_symbols_data* sd)
645 { return this->do_read_symbols(sd); }
647 // Pass sections which should be included in the link to the Layout
648 // object, and record where the sections go in the output file.
650 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
651 { this->do_layout(symtab, layout, sd); }
653 // Add symbol information to the global symbol table.
655 add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
656 { this->do_add_symbols(symtab, sd, layout); }
658 // Add symbol information to the global symbol table.
659 Archive::Should_include
660 should_include_member(Symbol_table* symtab, Layout* layout,
661 Read_symbols_data* sd, std::string* why)
662 { return this->do_should_include_member(symtab, layout, sd, why); }
664 // Iterate over global symbols, calling a visitor class V for each.
666 for_all_global_symbols(Read_symbols_data* sd,
667 Library_base::Symbol_visitor_base* v)
668 { return this->do_for_all_global_symbols(sd, v); }
670 // Iterate over local symbols, calling a visitor class V for each GOT offset
671 // associated with a local symbol.
673 for_all_local_got_entries(Got_offset_list::Visitor* v) const
674 { this->do_for_all_local_got_entries(v); }
676 // Functions and types for the elfcpp::Elf_file interface. This
677 // permit us to use Object as the File template parameter for
680 // The View class is returned by view. It must support a single
681 // method, data(). This is trivial, because get_view does what we
686 View(const unsigned char* p)
695 const unsigned char* p_;
700 view(off_t file_offset, section_size_type data_size)
701 { return View(this->get_view(file_offset, data_size, true, true)); }
705 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
707 // A location in the file.
713 Location(off_t fo, section_size_type ds)
714 : file_offset(fo), data_size(ds)
718 // Get a View given a Location.
719 View view(Location loc)
720 { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
722 // Get a view into the underlying file.
724 get_view(off_t start, section_size_type size, bool aligned, bool cache)
726 return this->input_file()->file().get_view(this->offset_, start, size,
730 // Get a lasting view into the underlying file.
732 get_lasting_view(off_t start, section_size_type size, bool aligned,
735 return this->input_file()->file().get_lasting_view(this->offset_, start,
736 size, aligned, cache);
739 // Read data from the underlying file.
741 read(off_t start, section_size_type size, void* p)
742 { this->input_file()->file().read(start + this->offset_, size, p); }
744 // Read multiple data from the underlying file.
746 read_multiple(const File_read::Read_multiple& rm)
747 { this->input_file()->file().read_multiple(this->offset_, rm); }
749 // Stop caching views in the underlying file.
751 clear_view_cache_marks()
753 if (this->input_file_ != NULL)
754 this->input_file_->file().clear_view_cache_marks();
757 // Get the number of global symbols defined by this object, and the
758 // number of the symbols whose final definition came from this
761 get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
763 { this->do_get_global_symbol_counts(symtab, defined, used); }
765 // Get the symbols defined in this object.
767 get_global_symbols() const
768 { return this->do_get_global_symbols(); }
770 // Set flag that this object was found in a system directory.
772 set_is_in_system_directory()
773 { this->is_in_system_directory_ = true; }
775 // Return whether this object was found in a system directory.
777 is_in_system_directory() const
778 { return this->is_in_system_directory_; }
780 // Set flag that this object was linked with --as-needed.
783 { this->as_needed_ = true; }
785 // Clear flag that this object was linked with --as-needed.
788 { this->as_needed_ = false; }
790 // Return whether this object was linked with --as-needed.
793 { return this->as_needed_; }
795 // Return whether we found this object by searching a directory.
798 { return this->input_file()->will_search_for(); }
802 { return this->no_export_; }
805 set_no_export(bool value)
806 { this->no_export_ = value; }
809 section_is_compressed(unsigned int shndx,
810 section_size_type* uncompressed_size) const
812 if (this->compressed_sections_ == NULL)
814 Compressed_section_map::const_iterator p =
815 this->compressed_sections_->find(shndx);
816 if (p != this->compressed_sections_->end())
818 if (uncompressed_size != NULL)
819 *uncompressed_size = p->second.size;
825 // Return a view of the decompressed contents of a section. Set *PLEN
826 // to the size. Set *IS_NEW to true if the contents need to be freed
829 decompressed_section_contents(unsigned int shndx, section_size_type* plen,
832 // Discard any buffers of decompressed sections. This is done
833 // at the end of the Add_symbols task.
835 discard_decompressed_sections();
837 // Return the index of the first incremental relocation for symbol SYMNDX.
839 get_incremental_reloc_base(unsigned int symndx) const
840 { return this->do_get_incremental_reloc_base(symndx); }
842 // Return the number of incremental relocations for symbol SYMNDX.
844 get_incremental_reloc_count(unsigned int symndx) const
845 { return this->do_get_incremental_reloc_count(symndx); }
847 // Return the output view for section SHNDX.
849 get_output_view(unsigned int shndx, section_size_type* plen) const
850 { return this->do_get_output_view(shndx, plen); }
853 // Returns NULL for Objects that are not dynamic objects. This method
854 // is overridden in the Dynobj class.
859 // Returns NULL for Objects that are not plugin objects. This method
860 // is overridden in the Pluginobj class.
865 // Return TRUE if this is an incremental (unchanged) input file.
866 // We return FALSE by default; the incremental object classes
867 // override this method.
869 do_is_incremental() const
872 // Return the last modified time of the file. This method may be
873 // overridden for subclasses that don't use an actual file (e.g.,
874 // Incremental objects).
877 { return this->input_file()->file().get_mtime(); }
879 // Read the symbols--implemented by child class.
881 do_read_symbols(Read_symbols_data*) = 0;
883 // Lay out sections--implemented by child class.
885 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
887 // Add symbol information to the global symbol table--implemented by
890 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
892 virtual Archive::Should_include
893 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
894 std::string* why) = 0;
896 // Iterate over global symbols, calling a visitor class V for each.
898 do_for_all_global_symbols(Read_symbols_data* sd,
899 Library_base::Symbol_visitor_base* v) = 0;
901 // Iterate over local symbols, calling a visitor class V for each GOT offset
902 // associated with a local symbol.
904 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0;
906 // Return the location of the contents of a section. Implemented by
908 virtual const unsigned char*
909 do_section_contents(unsigned int shndx, section_size_type* plen,
912 // Get the size of a section--implemented by child class.
914 do_section_size(unsigned int shndx) = 0;
916 // Get the name of a section--implemented by child class.
918 do_section_name(unsigned int shndx) const = 0;
920 // Get section flags--implemented by child class.
922 do_section_flags(unsigned int shndx) = 0;
924 // Get section entsize--implemented by child class.
926 do_section_entsize(unsigned int shndx) = 0;
928 // Get section address--implemented by child class.
930 do_section_address(unsigned int shndx) = 0;
932 // Get section type--implemented by child class.
934 do_section_type(unsigned int shndx) = 0;
936 // Get section link field--implemented by child class.
938 do_section_link(unsigned int shndx) = 0;
940 // Get section info field--implemented by child class.
942 do_section_info(unsigned int shndx) = 0;
944 // Get section alignment--implemented by child class.
946 do_section_addralign(unsigned int shndx) = 0;
948 // Return the output section given a section index--implemented
950 virtual Output_section*
951 do_output_section(unsigned int) const
952 { gold_unreachable(); }
954 // Get the address of a section--implemented by child class.
956 do_output_section_address(unsigned int)
957 { gold_unreachable(); }
959 // Get the offset of a section--implemented by child class.
961 do_output_section_offset(unsigned int) const
962 { gold_unreachable(); }
964 // Return the Xindex structure to use.
966 do_initialize_xindex() = 0;
968 // Implement get_global_symbol_counts--implemented by child class.
970 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
972 virtual const Symbols*
973 do_get_global_symbols() const = 0;
975 // Set the number of sections.
978 { this->shnum_ = shnum; }
980 // Functions used by both Sized_relobj_file and Sized_dynobj.
982 // Read the section data into a Read_symbols_data object.
983 template<int size, bool big_endian>
985 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
988 // Find the section header with the given NAME. If HDR is non-NULL
989 // then it is a section header returned from a previous call to this
990 // function and the next section header with the same name will be
992 template<int size, bool big_endian>
994 find_shdr(const unsigned char* pshdrs, const char* name,
995 const char* names, section_size_type names_size,
996 const unsigned char* hdr) const;
998 // Let the child class initialize the xindex object directly.
1000 set_xindex(Xindex* xindex)
1002 gold_assert(this->xindex_ == NULL);
1003 this->xindex_ = xindex;
1006 // If NAME is the name of a special .gnu.warning section, arrange
1007 // for the warning to be issued. SHNDX is the section index.
1008 // Return whether it is a warning section.
1010 handle_gnu_warning_section(const char* name, unsigned int shndx,
1013 // If NAME is the name of the special section which indicates that
1014 // this object was compiled with -fsplit-stack, mark it accordingly,
1015 // and return true. Otherwise return false.
1017 handle_split_stack_section(const char* name);
1019 // Discard any buffers of decompressed sections. This is done
1020 // at the end of the Add_symbols task.
1022 do_discard_decompressed_sections()
1025 // Return the index of the first incremental relocation for symbol SYMNDX--
1026 // implemented by child class.
1027 virtual unsigned int
1028 do_get_incremental_reloc_base(unsigned int) const
1029 { gold_unreachable(); }
1031 // Return the number of incremental relocations for symbol SYMNDX--
1032 // implemented by child class.
1033 virtual unsigned int
1034 do_get_incremental_reloc_count(unsigned int) const
1035 { gold_unreachable(); }
1037 // Return the output view for a section.
1038 virtual unsigned char*
1039 do_get_output_view(unsigned int, section_size_type*) const
1040 { gold_unreachable(); }
1043 set_compressed_sections(Compressed_section_map* compressed_sections)
1044 { this->compressed_sections_ = compressed_sections; }
1046 Compressed_section_map*
1047 compressed_sections()
1048 { return this->compressed_sections_; }
1051 // This class may not be copied.
1052 Object(const Object&);
1053 Object& operator=(const Object&);
1055 // Name of object as printed to user.
1057 // For reading the file.
1058 Input_file* input_file_;
1059 // Offset within the file--0 for an object file, non-0 for an
1062 // Number of input sections.
1063 unsigned int shnum_;
1064 // Whether this is a dynamic object.
1065 bool is_dynamic_ : 1;
1066 // Whether this object is needed. This is only set for dynamic
1067 // objects, and means that the object defined a symbol which was
1068 // used by a reference from a regular object.
1069 bool is_needed_ : 1;
1070 // Whether this object was compiled with -fsplit-stack.
1071 bool uses_split_stack_ : 1;
1072 // Whether this object contains any functions compiled with the
1073 // no_split_stack attribute.
1074 bool has_no_split_stack_ : 1;
1075 // True if exclude this object from automatic symbol export.
1076 // This is used only for archive objects.
1077 bool no_export_ : 1;
1078 // True if the object was found in a system directory.
1079 bool is_in_system_directory_ : 1;
1080 // True if the object was linked with --as-needed.
1081 bool as_needed_ : 1;
1082 // Many sections for objects with more than SHN_LORESERVE sections.
1084 // For compressed debug sections, map section index to uncompressed size
1086 Compressed_section_map* compressed_sections_;
1089 // A regular object (ET_REL). This is an abstract base class itself.
1090 // The implementation is the template class Sized_relobj_file.
1092 class Relobj : public Object
1095 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
1096 : Object(name, input_file, false, offset),
1098 map_to_relocatable_relocs_(NULL),
1099 object_merge_map_(NULL),
1100 relocs_must_follow_section_writes_(false),
1102 reloc_counts_(NULL),
1104 first_dyn_reloc_(0),
1108 // During garbage collection, the Read_symbols_data pass for
1109 // each object is stored as layout needs to be done after
1110 // reloc processing.
1113 { return this->sd_; }
1115 // Decides which section names have to be included in the worklist
1118 is_section_name_included(const char* name);
1121 copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
1122 unsigned int section_header_size);
1125 set_symbols_data(Symbols_data* sd)
1128 // During garbage collection, the Read_relocs pass for all objects
1129 // is done before scanning the relocs. In that case, this->rd_ is
1130 // used to store the information from Read_relocs for each object.
1131 // This data is also used to compute the list of relevant sections.
1134 { return this->rd_; }
1137 set_relocs_data(Read_relocs_data* rd)
1141 is_output_section_offset_invalid(unsigned int shndx) const = 0;
1145 read_relocs(Read_relocs_data* rd)
1146 { return this->do_read_relocs(rd); }
1148 // Process the relocs, during garbage collection only.
1150 gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1151 { return this->do_gc_process_relocs(symtab, layout, rd); }
1153 // Scan the relocs and adjust the symbol table.
1155 scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
1156 { return this->do_scan_relocs(symtab, layout, rd); }
1158 // Return the value of the local symbol whose index is SYMNDX, plus
1159 // ADDEND. ADDEND is passed in so that we can correctly handle the
1160 // section symbol for a merge section.
1162 local_symbol_value(unsigned int symndx, uint64_t addend) const
1163 { return this->do_local_symbol_value(symndx, addend); }
1165 // Return the PLT offset for a local symbol. It is an error to call
1166 // this if it doesn't have one.
1168 local_plt_offset(unsigned int symndx) const
1169 { return this->do_local_plt_offset(symndx); }
1171 // Return whether the local symbol SYMNDX has a GOT offset of type
1174 local_has_got_offset(unsigned int symndx, unsigned int got_type) const
1175 { return this->do_local_has_got_offset(symndx, got_type, 0); }
1177 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
1178 // of type GOT_TYPE.
1180 local_has_got_offset(unsigned int symndx, unsigned int got_type,
1181 uint64_t addend) const
1182 { return this->do_local_has_got_offset(symndx, got_type, addend); }
1184 // Return the GOT offset of type GOT_TYPE of the local symbol
1185 // SYMNDX. It is an error to call this if the symbol does not have
1186 // a GOT offset of the specified type.
1188 local_got_offset(unsigned int symndx, unsigned int got_type) const
1189 { return this->do_local_got_offset(symndx, got_type, 0); }
1191 // Return the GOT offset of type GOT_TYPE of the local symbol
1192 // SYMNDX plus ADDEND. It is an error to call this if the symbol
1193 // does not have a GOT offset of the specified type.
1195 local_got_offset(unsigned int symndx, unsigned int got_type,
1196 uint64_t addend) const
1197 { return this->do_local_got_offset(symndx, got_type, addend); }
1199 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1202 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1203 unsigned int got_offset)
1204 { this->do_set_local_got_offset(symndx, got_type, got_offset, 0); }
1206 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
1207 // plus ADDEND to GOT_OFFSET.
1209 set_local_got_offset(unsigned int symndx, unsigned int got_type,
1210 unsigned int got_offset, uint64_t addend)
1211 { this->do_set_local_got_offset(symndx, got_type, got_offset, addend); }
1213 // Return whether the local symbol SYMNDX is a TLS symbol.
1215 local_is_tls(unsigned int symndx) const
1216 { return this->do_local_is_tls(symndx); }
1218 // The number of local symbols in the input symbol table.
1219 virtual unsigned int
1220 local_symbol_count() const
1221 { return this->do_local_symbol_count(); }
1223 // The number of local symbols in the output symbol table.
1224 virtual unsigned int
1225 output_local_symbol_count() const
1226 { return this->do_output_local_symbol_count(); }
1228 // The file offset for local symbols in the output symbol table.
1230 local_symbol_offset() const
1231 { return this->do_local_symbol_offset(); }
1233 // Initial local symbol processing: count the number of local symbols
1234 // in the output symbol table and dynamic symbol table; add local symbol
1235 // names to *POOL and *DYNPOOL.
1237 count_local_symbols(Stringpool_template<char>* pool,
1238 Stringpool_template<char>* dynpool)
1239 { return this->do_count_local_symbols(pool, dynpool); }
1241 // Set the values of the local symbols, set the output symbol table
1242 // indexes for the local variables, and set the offset where local
1243 // symbol information will be stored. Returns the new local symbol index.
1245 finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
1246 { return this->do_finalize_local_symbols(index, off, symtab); }
1248 // Set the output dynamic symbol table indexes for the local variables.
1250 set_local_dynsym_indexes(unsigned int index)
1251 { return this->do_set_local_dynsym_indexes(index); }
1253 // Set the offset where local dynamic symbol information will be stored.
1255 set_local_dynsym_offset(off_t off)
1256 { return this->do_set_local_dynsym_offset(off); }
1258 // Record a dynamic relocation against an input section from this object.
1260 add_dyn_reloc(unsigned int index)
1262 if (this->dyn_reloc_count_ == 0)
1263 this->first_dyn_reloc_ = index;
1264 ++this->dyn_reloc_count_;
1267 // Return the index of the first dynamic relocation.
1269 first_dyn_reloc() const
1270 { return this->first_dyn_reloc_; }
1272 // Return the count of dynamic relocations.
1274 dyn_reloc_count() const
1275 { return this->dyn_reloc_count_; }
1277 // Relocate the input sections and write out the local symbols.
1279 relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of)
1280 { return this->do_relocate(symtab, layout, of); }
1282 // Return whether an input section is being included in the link.
1284 is_section_included(unsigned int shndx) const
1286 gold_assert(shndx < this->output_sections_.size());
1287 return this->output_sections_[shndx] != NULL;
1290 // The output section of the input section with index SHNDX.
1291 // This is only used currently to remove a section from the link in
1294 set_output_section(unsigned int shndx, Output_section* os)
1296 gold_assert(shndx < this->output_sections_.size());
1297 this->output_sections_[shndx] = os;
1300 // Set the offset of an input section within its output section.
1302 set_section_offset(unsigned int shndx, uint64_t off)
1303 { this->do_set_section_offset(shndx, off); }
1305 // Return true if we need to wait for output sections to be written
1306 // before we can apply relocations. This is true if the object has
1307 // any relocations for sections which require special handling, such
1308 // as the exception frame section.
1310 relocs_must_follow_section_writes() const
1311 { return this->relocs_must_follow_section_writes_; }
1314 get_or_create_merge_map();
1318 initialize_input_to_output_map(unsigned int shndx,
1319 typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
1320 Unordered_map<section_offset_type,
1321 typename elfcpp::Elf_types<size>::Elf_Addr>* output_address) const;
1324 add_merge_mapping(Output_section_data *output_data,
1325 unsigned int shndx, section_offset_type offset,
1326 section_size_type length,
1327 section_offset_type output_offset);
1330 merge_output_offset(unsigned int shndx, section_offset_type offset,
1331 section_offset_type *poutput) const;
1333 const Output_section_data*
1334 find_merge_section(unsigned int shndx) const;
1336 // Record the relocatable reloc info for an input reloc section.
1338 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
1340 gold_assert(reloc_shndx < this->shnum());
1341 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
1344 // Get the relocatable reloc info for an input reloc section.
1346 relocatable_relocs(unsigned int reloc_shndx)
1348 gold_assert(reloc_shndx < this->shnum());
1349 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
1352 // Layout sections whose layout was deferred while waiting for
1353 // input files from a plugin.
1355 layout_deferred_sections(Layout* layout)
1356 { this->do_layout_deferred_sections(layout); }
1358 // Return the index of the first incremental relocation for symbol SYMNDX.
1359 virtual unsigned int
1360 do_get_incremental_reloc_base(unsigned int symndx) const
1361 { return this->reloc_bases_[symndx]; }
1363 // Return the number of incremental relocations for symbol SYMNDX.
1364 virtual unsigned int
1365 do_get_incremental_reloc_count(unsigned int symndx) const
1366 { return this->reloc_counts_[symndx]; }
1368 // Return the word size of the object file.
1371 { return this->do_elfsize(); }
1373 // Return TRUE if this is a big-endian object file.
1375 is_big_endian() const
1376 { return this->do_is_big_endian(); }
1379 // The output section to be used for each input section, indexed by
1380 // the input section number. The output section is NULL if the
1381 // input section is to be discarded.
1382 typedef std::vector<Output_section*> Output_sections;
1384 // Read the relocs--implemented by child class.
1386 do_read_relocs(Read_relocs_data*) = 0;
1388 // Process the relocs--implemented by child class.
1390 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
1392 // Scan the relocs--implemented by child class.
1394 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
1396 // Return the value of a local symbol.
1398 do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0;
1400 // Return the PLT offset of a local symbol.
1401 virtual unsigned int
1402 do_local_plt_offset(unsigned int symndx) const = 0;
1404 // Return whether a local symbol plus addend has a GOT offset
1407 do_local_has_got_offset(unsigned int symndx,
1408 unsigned int got_type, uint64_t addend) const = 0;
1410 // Return the GOT offset of a given type of a local symbol plus addend.
1411 virtual unsigned int
1412 do_local_got_offset(unsigned int symndx, unsigned int got_type,
1413 uint64_t addend) const = 0;
1415 // Set the GOT offset with a given type for a local symbol plus addend.
1417 do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
1418 unsigned int got_offset, uint64_t addend) = 0;
1420 // Return whether local symbol SYMNDX is a TLS symbol.
1422 do_local_is_tls(unsigned int symndx) const = 0;
1424 // Return the number of local symbols--implemented by child class.
1425 virtual unsigned int
1426 do_local_symbol_count() const = 0;
1428 // Return the number of output local symbols--implemented by child class.
1429 virtual unsigned int
1430 do_output_local_symbol_count() const = 0;
1432 // Return the file offset for local symbols--implemented by child class.
1434 do_local_symbol_offset() const = 0;
1436 // Count local symbols--implemented by child class.
1438 do_count_local_symbols(Stringpool_template<char>*,
1439 Stringpool_template<char>*) = 0;
1441 // Finalize the local symbols. Set the output symbol table indexes
1442 // for the local variables, and set the offset where local symbol
1443 // information will be stored.
1444 virtual unsigned int
1445 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
1447 // Set the output dynamic symbol table indexes for the local variables.
1448 virtual unsigned int
1449 do_set_local_dynsym_indexes(unsigned int) = 0;
1451 // Set the offset where local dynamic symbol information will be stored.
1452 virtual unsigned int
1453 do_set_local_dynsym_offset(off_t) = 0;
1455 // Relocate the input sections and write out the local
1456 // symbols--implemented by child class.
1458 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
1460 // Set the offset of a section--implemented by child class.
1462 do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
1464 // Layout sections whose layout was deferred while waiting for
1465 // input files from a plugin--implemented by child class.
1467 do_layout_deferred_sections(Layout*) = 0;
1469 // Given a section index, return the corresponding Output_section.
1470 // The return value will be NULL if the section is not included in
1473 do_output_section(unsigned int shndx) const
1475 gold_assert(shndx < this->output_sections_.size());
1476 return this->output_sections_[shndx];
1479 // Return the vector mapping input sections to output sections.
1482 { return this->output_sections_; }
1484 const Output_sections&
1485 output_sections() const
1486 { return this->output_sections_; }
1488 // Set the size of the relocatable relocs array.
1490 size_relocatable_relocs()
1492 this->map_to_relocatable_relocs_ =
1493 new std::vector<Relocatable_relocs*>(this->shnum());
1496 // Record that we must wait for the output sections to be written
1497 // before applying relocations.
1499 set_relocs_must_follow_section_writes()
1500 { this->relocs_must_follow_section_writes_ = true; }
1502 // Allocate the array for counting incremental relocations.
1504 allocate_incremental_reloc_counts()
1506 unsigned int nsyms = this->do_get_global_symbols()->size();
1507 this->reloc_counts_ = new unsigned int[nsyms];
1508 gold_assert(this->reloc_counts_ != NULL);
1509 memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int));
1512 // Record a relocation in this object referencing global symbol SYMNDX.
1513 // Used for tracking incremental link information.
1515 count_incremental_reloc(unsigned int symndx)
1517 unsigned int nsyms = this->do_get_global_symbols()->size();
1518 gold_assert(symndx < nsyms);
1519 gold_assert(this->reloc_counts_ != NULL);
1520 ++this->reloc_counts_[symndx];
1523 // Finalize the incremental relocation information.
1525 finalize_incremental_relocs(Layout* layout, bool clear_counts);
1527 // Return the index of the next relocation to be written for global symbol
1528 // SYMNDX. Only valid after finalize_incremental_relocs() has been called.
1530 next_incremental_reloc_index(unsigned int symndx)
1532 unsigned int nsyms = this->do_get_global_symbols()->size();
1534 gold_assert(this->reloc_counts_ != NULL);
1535 gold_assert(this->reloc_bases_ != NULL);
1536 gold_assert(symndx < nsyms);
1538 unsigned int counter = this->reloc_counts_[symndx]++;
1539 return this->reloc_bases_[symndx] + counter;
1542 // Return the word size of the object file--
1543 // implemented by child class.
1545 do_elfsize() const = 0;
1547 // Return TRUE if this is a big-endian object file--
1548 // implemented by child class.
1550 do_is_big_endian() const = 0;
1553 // Mapping from input sections to output section.
1554 Output_sections output_sections_;
1555 // Mapping from input section index to the information recorded for
1556 // the relocations. This is only used for a relocatable link.
1557 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
1558 // Mappings for merge sections. This is managed by the code in the
1560 Object_merge_map* object_merge_map_;
1561 // Whether we need to wait for output sections to be written before
1562 // we can apply relocations.
1563 bool relocs_must_follow_section_writes_;
1564 // Used to store the relocs data computed by the Read_relocs pass.
1565 // Used during garbage collection of unused sections.
1566 Read_relocs_data* rd_;
1567 // Used to store the symbols data computed by the Read_symbols pass.
1568 // Again used during garbage collection when laying out referenced
1570 gold::Symbols_data* sd_;
1571 // Per-symbol counts of relocations, for incremental links.
1572 unsigned int* reloc_counts_;
1573 // Per-symbol base indexes of relocations, for incremental links.
1574 unsigned int* reloc_bases_;
1575 // Index of the first dynamic relocation for this object.
1576 unsigned int first_dyn_reloc_;
1577 // Count of dynamic relocations for this object.
1578 unsigned int dyn_reloc_count_;
1581 // This class is used to handle relocations against a section symbol
1582 // in an SHF_MERGE section. For such a symbol, we need to know the
1583 // addend of the relocation before we can determine the final value.
1584 // The addend gives us the location in the input section, and we can
1585 // determine how it is mapped to the output section. For a
1586 // non-section symbol, we apply the addend to the final value of the
1587 // symbol; that is done in finalize_local_symbols, and does not use
1591 class Merged_symbol_value
1594 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1596 // We use a hash table to map offsets in the input section to output
1598 typedef Unordered_map<section_offset_type, Value> Output_addresses;
1600 Merged_symbol_value(Value input_value, Value output_start_address)
1601 : input_value_(input_value), output_start_address_(output_start_address),
1605 // Initialize the hash table.
1607 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
1609 // Release the hash table to save space.
1611 free_input_to_output_map()
1612 { this->output_addresses_.clear(); }
1614 // Get the output value corresponding to an addend. The object and
1615 // input section index are passed in because the caller will have
1616 // them; otherwise we could store them here.
1618 value(const Relobj* object, unsigned int input_shndx, Value addend) const
1620 // This is a relocation against a section symbol. ADDEND is the
1621 // offset in the section. The result should be the start of some
1622 // merge area. If the object file wants something else, it should
1623 // use a regular symbol rather than a section symbol.
1624 // Unfortunately, PR 6658 shows a case in which the object file
1625 // refers to the section symbol, but uses a negative ADDEND to
1626 // compensate for a PC relative reloc. We can't handle the
1627 // general case. However, we can handle the special case of a
1628 // negative addend, by assuming that it refers to the start of the
1629 // section. Of course, that means that we have to guess when
1630 // ADDEND is negative. It is normal to see a 32-bit value here
1631 // even when the template parameter size is 64, as 64-bit object
1632 // file formats have 32-bit relocations. We know this is a merge
1633 // section, so we know it has to fit into memory. So we assume
1634 // that we won't see a value larger than a large 32-bit unsigned
1635 // value. This will break objects with very very large merge
1636 // sections; they probably break in other ways anyhow.
1637 Value input_offset = this->input_value_;
1638 if (addend < 0xffffff00)
1640 input_offset += addend;
1643 typename Output_addresses::const_iterator p =
1644 this->output_addresses_.find(input_offset);
1645 if (p != this->output_addresses_.end())
1646 return p->second + addend;
1648 return (this->value_from_output_section(object, input_shndx, input_offset)
1653 // Get the output value for an input offset if we couldn't find it
1654 // in the hash table.
1656 value_from_output_section(const Relobj*, unsigned int input_shndx,
1657 Value input_offset) const;
1659 // The value of the section symbol in the input file. This is
1660 // normally zero, but could in principle be something else.
1662 // The start address of this merged section in the output file.
1663 Value output_start_address_;
1664 // A hash table which maps offsets in the input section to output
1665 // addresses. This only maps specific offsets, not all offsets.
1666 Output_addresses output_addresses_;
1669 // This POD class is holds the value of a symbol. This is used for
1670 // local symbols, and for all symbols during relocation processing.
1671 // For special sections, such as SHF_MERGE sections, this calls a
1672 // function to get the final symbol value.
1678 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
1681 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
1682 is_ordinary_shndx_(false), is_section_symbol_(false),
1683 is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
1684 { this->u_.value = 0; }
1688 if (!this->has_output_value_)
1689 delete this->u_.merged_symbol_value;
1692 // Get the value of this symbol. OBJECT is the object in which this
1693 // symbol is defined, and ADDEND is an addend to add to the value.
1694 template<bool big_endian>
1696 value(const Sized_relobj_file<size, big_endian>* object, Value addend) const
1698 if (this->has_output_value_)
1699 return this->u_.value + addend;
1702 gold_assert(this->is_ordinary_shndx_);
1703 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
1708 // Set the value of this symbol in the output symbol table.
1710 set_output_value(Value value)
1711 { this->u_.value = value; }
1713 // For a section symbol in a merged section, we need more
1716 set_merged_symbol_value(Merged_symbol_value<size>* msv)
1718 gold_assert(this->is_section_symbol_);
1719 this->has_output_value_ = false;
1720 this->u_.merged_symbol_value = msv;
1723 // Initialize the input to output map for a section symbol in a
1724 // merged section. We also initialize the value of a non-section
1725 // symbol in a merged section.
1727 initialize_input_to_output_map(const Relobj* object)
1729 if (!this->has_output_value_)
1731 gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
1732 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
1733 msv->initialize_input_to_output_map(object, this->input_shndx_);
1737 // Free the input to output map for a section symbol in a merged
1740 free_input_to_output_map()
1742 if (!this->has_output_value_)
1743 this->u_.merged_symbol_value->free_input_to_output_map();
1746 // Set the value of the symbol from the input file. This is only
1747 // called by count_local_symbols, to communicate the value to
1748 // finalize_local_symbols.
1750 set_input_value(Value value)
1751 { this->u_.value = value; }
1753 // Return the input value. This is only called by
1754 // finalize_local_symbols and (in special cases) relocate_section.
1757 { return this->u_.value; }
1759 // Return whether we have set the index in the output symbol table
1762 is_output_symtab_index_set() const
1764 return (this->output_symtab_index_ != 0
1765 && this->output_symtab_index_ != -2U);
1768 // Return whether this symbol may be discarded from the normal
1771 may_be_discarded_from_output_symtab() const
1773 gold_assert(!this->is_output_symtab_index_set());
1774 return this->output_symtab_index_ != -2U;
1777 // Return whether this symbol has an entry in the output symbol
1780 has_output_symtab_entry() const
1782 gold_assert(this->is_output_symtab_index_set());
1783 return this->output_symtab_index_ != -1U;
1786 // Return the index in the output symbol table.
1788 output_symtab_index() const
1790 gold_assert(this->is_output_symtab_index_set()
1791 && this->output_symtab_index_ != -1U);
1792 return this->output_symtab_index_;
1795 // Set the index in the output symbol table.
1797 set_output_symtab_index(unsigned int i)
1799 gold_assert(!this->is_output_symtab_index_set());
1800 gold_assert(i != 0 && i != -1U && i != -2U);
1801 this->output_symtab_index_ = i;
1804 // Record that this symbol should not go into the output symbol
1807 set_no_output_symtab_entry()
1809 gold_assert(this->output_symtab_index_ == 0);
1810 this->output_symtab_index_ = -1U;
1813 // Record that this symbol must go into the output symbol table,
1814 // because it there is a relocation that uses it.
1816 set_must_have_output_symtab_entry()
1818 gold_assert(!this->is_output_symtab_index_set());
1819 this->output_symtab_index_ = -2U;
1822 // Set the index in the output dynamic symbol table.
1824 set_needs_output_dynsym_entry()
1826 gold_assert(!this->is_section_symbol());
1827 this->output_dynsym_index_ = 0;
1830 // Return whether this symbol should go into the dynamic symbol
1833 needs_output_dynsym_entry() const
1835 return this->output_dynsym_index_ != -1U;
1838 // Return whether this symbol has an entry in the dynamic symbol
1841 has_output_dynsym_entry() const
1843 gold_assert(this->output_dynsym_index_ != 0);
1844 return this->output_dynsym_index_ != -1U;
1847 // Record that this symbol should go into the dynamic symbol table.
1849 set_output_dynsym_index(unsigned int i)
1851 gold_assert(this->output_dynsym_index_ == 0);
1852 gold_assert(i != 0 && i != -1U);
1853 this->output_dynsym_index_ = i;
1856 // Return the index in the output dynamic symbol table.
1858 output_dynsym_index() const
1860 gold_assert(this->output_dynsym_index_ != 0
1861 && this->output_dynsym_index_ != -1U);
1862 return this->output_dynsym_index_;
1865 // Set the index of the input section in the input file.
1867 set_input_shndx(unsigned int i, bool is_ordinary)
1869 this->input_shndx_ = i;
1870 // input_shndx_ field is a bitfield, so make sure that the value
1872 gold_assert(this->input_shndx_ == i);
1873 this->is_ordinary_shndx_ = is_ordinary;
1876 // Return the index of the input section in the input file.
1878 input_shndx(bool* is_ordinary) const
1880 *is_ordinary = this->is_ordinary_shndx_;
1881 return this->input_shndx_;
1884 // Whether this is a section symbol.
1886 is_section_symbol() const
1887 { return this->is_section_symbol_; }
1889 // Record that this is a section symbol.
1891 set_is_section_symbol()
1893 gold_assert(!this->needs_output_dynsym_entry());
1894 this->is_section_symbol_ = true;
1897 // Record that this is a TLS symbol.
1900 { this->is_tls_symbol_ = true; }
1902 // Return true if this is a TLS symbol.
1904 is_tls_symbol() const
1905 { return this->is_tls_symbol_; }
1907 // Record that this is an IFUNC symbol.
1909 set_is_ifunc_symbol()
1910 { this->is_ifunc_symbol_ = true; }
1912 // Return true if this is an IFUNC symbol.
1914 is_ifunc_symbol() const
1915 { return this->is_ifunc_symbol_; }
1917 // Return true if this has output value.
1919 has_output_value() const
1920 { return this->has_output_value_; }
1923 // The index of this local symbol in the output symbol table. This
1924 // will be 0 if no value has been assigned yet, and the symbol may
1925 // be omitted. This will be -1U if the symbol should not go into
1926 // the symbol table. This will be -2U if the symbol must go into
1927 // the symbol table, but no index has been assigned yet.
1928 unsigned int output_symtab_index_;
1929 // The index of this local symbol in the dynamic symbol table. This
1930 // will be -1U if the symbol should not go into the symbol table.
1931 unsigned int output_dynsym_index_;
1932 // The section index in the input file in which this symbol is
1934 unsigned int input_shndx_ : 27;
1935 // Whether the section index is an ordinary index, not a special
1937 bool is_ordinary_shndx_ : 1;
1938 // Whether this is a STT_SECTION symbol.
1939 bool is_section_symbol_ : 1;
1940 // Whether this is a STT_TLS symbol.
1941 bool is_tls_symbol_ : 1;
1942 // Whether this is a STT_GNU_IFUNC symbol.
1943 bool is_ifunc_symbol_ : 1;
1944 // Whether this symbol has a value for the output file. This is
1945 // normally set to true during Layout::finalize, by
1946 // finalize_local_symbols. It will be false for a section symbol in
1947 // a merge section, as for such symbols we can not determine the
1948 // value to use in a relocation until we see the addend.
1949 bool has_output_value_ : 1;
1952 // This is used if has_output_value_ is true. Between
1953 // count_local_symbols and finalize_local_symbols, this is the
1954 // value in the input file. After finalize_local_symbols, it is
1955 // the value in the output file.
1957 // This is used if has_output_value_ is false. It points to the
1958 // information we need to get the value for a merge section.
1959 Merged_symbol_value<size>* merged_symbol_value;
1963 // This type is used to modify relocations for -fsplit-stack. It is
1964 // indexed by relocation index, and means that the relocation at that
1965 // index should use the symbol from the vector, rather than the one
1966 // indicated by the relocation.
1968 class Reloc_symbol_changes
1971 Reloc_symbol_changes(size_t count)
1976 set(size_t i, Symbol* sym)
1977 { this->vec_[i] = sym; }
1980 operator[](size_t i) const
1981 { return this->vec_[i]; }
1984 std::vector<Symbol*> vec_;
1987 // Abstract base class for a regular object file, either a real object file
1988 // or an incremental (unchanged) object. This is size and endian specific.
1990 template<int size, bool big_endian>
1991 class Sized_relobj : public Relobj
1994 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1995 typedef Relobj::Symbols Symbols;
1997 static const Address invalid_address = static_cast<Address>(0) - 1;
1999 Sized_relobj(const std::string& name, Input_file* input_file)
2000 : Relobj(name, input_file), local_got_offsets_(), section_offsets_()
2003 Sized_relobj(const std::string& name, Input_file* input_file,
2005 : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_()
2011 // If this is a regular object, return a pointer to the Sized_relobj_file
2012 // object. Otherwise, return NULL.
2013 virtual Sized_relobj_file<size, big_endian>*
2017 const virtual Sized_relobj_file<size, big_endian>*
2018 sized_relobj() const
2021 // Checks if the offset of input section SHNDX within its output
2022 // section is invalid.
2024 is_output_section_offset_invalid(unsigned int shndx) const
2025 { return this->get_output_section_offset(shndx) == invalid_address; }
2027 // Get the offset of input section SHNDX within its output section.
2028 // This is -1 if the input section requires a special mapping, such
2029 // as a merge section. The output section can be found in the
2030 // output_sections_ field of the parent class Relobj.
2032 get_output_section_offset(unsigned int shndx) const
2034 gold_assert(shndx < this->section_offsets_.size());
2035 return this->section_offsets_[shndx];
2038 // Iterate over local symbols, calling a visitor class V for each GOT offset
2039 // associated with a local symbol.
2041 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
2044 typedef Relobj::Output_sections Output_sections;
2046 // Clear the local symbol information.
2049 { this->local_got_offsets_.clear(); }
2051 // Return the vector of section offsets.
2052 std::vector<Address>&
2054 { return this->section_offsets_; }
2056 // Get the address of an output section.
2058 do_output_section_address(unsigned int shndx);
2060 // Get the offset of a section.
2062 do_output_section_offset(unsigned int shndx) const
2064 Address off = this->get_output_section_offset(shndx);
2065 if (off == invalid_address)
2070 // Set the offset of a section.
2072 do_set_section_offset(unsigned int shndx, uint64_t off)
2074 gold_assert(shndx < this->section_offsets_.size());
2075 this->section_offsets_[shndx] =
2076 (off == static_cast<uint64_t>(-1)
2078 : convert_types<Address, uint64_t>(off));
2081 // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
2082 // of type GOT_TYPE.
2084 do_local_has_got_offset(unsigned int symndx, unsigned int got_type,
2085 uint64_t addend) const
2087 Local_got_entry_key key(symndx, addend);
2088 Local_got_offsets::const_iterator p =
2089 this->local_got_offsets_.find(key);
2090 return (p != this->local_got_offsets_.end()
2091 && p->second->get_offset(got_type) != -1U);
2094 // Return the GOT offset of type GOT_TYPE of the local symbol
2095 // SYMNDX plus ADDEND.
2097 do_local_got_offset(unsigned int symndx, unsigned int got_type,
2098 uint64_t addend) const
2100 Local_got_entry_key key(symndx, addend);
2101 Local_got_offsets::const_iterator p =
2102 this->local_got_offsets_.find(key);
2103 gold_assert(p != this->local_got_offsets_.end());
2104 unsigned int off = p->second->get_offset(got_type);
2105 gold_assert(off != -1U);
2109 // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
2110 // plus ADDEND to GOT_OFFSET.
2112 do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
2113 unsigned int got_offset, uint64_t addend)
2115 Local_got_entry_key key(symndx, addend);
2116 Local_got_offsets::const_iterator p =
2117 this->local_got_offsets_.find(key);
2118 if (p != this->local_got_offsets_.end())
2119 p->second->set_offset(got_type, got_offset);
2122 Got_offset_list* g = new Got_offset_list(got_type, got_offset);
2123 std::pair<Local_got_offsets::iterator, bool> ins =
2124 this->local_got_offsets_.insert(std::make_pair(key, g));
2125 gold_assert(ins.second);
2129 // Return the word size of the object file.
2134 // Return TRUE if this is a big-endian object file.
2136 do_is_big_endian() const
2137 { return big_endian; }
2140 // The GOT offsets of local symbols. This map also stores GOT offsets
2141 // for tp-relative offsets for TLS symbols.
2142 typedef Unordered_map<Local_got_entry_key, Got_offset_list*,
2143 Local_got_entry_key::hash,
2144 Local_got_entry_key::equal_to> Local_got_offsets;
2146 // GOT offsets for local non-TLS symbols, and tp-relative offsets
2147 // for TLS symbols, indexed by local got entry key class.
2148 Local_got_offsets local_got_offsets_;
2149 // For each input section, the offset of the input section in its
2150 // output section. This is INVALID_ADDRESS if the input section requires a
2152 std::vector<Address> section_offsets_;
2155 // A regular object file. This is size and endian specific.
2157 template<int size, bool big_endian>
2158 class Sized_relobj_file : public Sized_relobj<size, big_endian>
2161 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2162 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
2163 typedef std::vector<Symbol_value<size> > Local_values;
2165 static const Address invalid_address = static_cast<Address>(0) - 1;
2167 enum Compute_final_local_value_status
2171 // An error occurred.
2173 // The local symbol has no output section.
2177 Sized_relobj_file(const std::string& name,
2178 Input_file* input_file,
2180 const typename elfcpp::Ehdr<size, big_endian>&);
2182 ~Sized_relobj_file();
2184 // Set up the object file based on TARGET.
2187 { this->do_setup(); }
2189 // Return a pointer to the Sized_relobj_file object.
2190 Sized_relobj_file<size, big_endian>*
2194 const Sized_relobj_file<size, big_endian>*
2195 sized_relobj() const
2198 // Return the ELF file type.
2201 { return this->e_type_; }
2203 // Return the number of symbols. This is only valid after
2204 // Object::add_symbols has been called.
2206 symbol_count() const
2207 { return this->local_symbol_count_ + this->symbols_.size(); }
2209 // If SYM is the index of a global symbol in the object file's
2210 // symbol table, return the Symbol object. Otherwise, return NULL.
2212 global_symbol(unsigned int sym) const
2214 if (sym >= this->local_symbol_count_)
2216 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
2217 return this->symbols_[sym - this->local_symbol_count_];
2222 // Return the section index of symbol SYM. Set *VALUE to its value
2223 // in the object file. Set *IS_ORDINARY if this is an ordinary
2224 // section index, not a special code between SHN_LORESERVE and
2225 // SHN_HIRESERVE. Note that for a symbol which is not defined in
2226 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
2227 // it will not return the final value of the symbol in the link.
2229 symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
2231 // Return a pointer to the Symbol_value structure which holds the
2232 // value of a local symbol.
2233 const Symbol_value<size>*
2234 local_symbol(unsigned int sym) const
2236 gold_assert(sym < this->local_values_.size());
2237 return &this->local_values_[sym];
2240 // Return the index of local symbol SYM in the ordinary symbol
2241 // table. A value of -1U means that the symbol is not being output.
2243 symtab_index(unsigned int sym) const
2245 gold_assert(sym < this->local_values_.size());
2246 return this->local_values_[sym].output_symtab_index();
2249 // Return the index of local symbol SYM in the dynamic symbol
2250 // table. A value of -1U means that the symbol is not being output.
2252 dynsym_index(unsigned int sym) const
2254 gold_assert(sym < this->local_values_.size());
2255 return this->local_values_[sym].output_dynsym_index();
2258 // Return the input section index of local symbol SYM.
2260 local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
2262 gold_assert(sym < this->local_values_.size());
2263 return this->local_values_[sym].input_shndx(is_ordinary);
2266 // Record that local symbol SYM must be in the output symbol table.
2268 set_must_have_output_symtab_entry(unsigned int sym)
2270 gold_assert(sym < this->local_values_.size());
2271 this->local_values_[sym].set_must_have_output_symtab_entry();
2274 // Record that local symbol SYM needs a dynamic symbol entry.
2276 set_needs_output_dynsym_entry(unsigned int sym)
2278 gold_assert(sym < this->local_values_.size());
2279 this->local_values_[sym].set_needs_output_dynsym_entry();
2282 // Return whether the local symbol SYMNDX has a PLT offset.
2284 local_has_plt_offset(unsigned int symndx) const;
2286 // Set the PLT offset of the local symbol SYMNDX.
2288 set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
2290 // Adjust this local symbol value. Return false if the symbol
2291 // should be discarded from the output file.
2293 adjust_local_symbol(Symbol_value<size>* lv) const
2294 { return this->do_adjust_local_symbol(lv); }
2296 // Return the name of the symbol that spans the given offset in the
2297 // specified section in this object. This is used only for error
2298 // messages and is not particularly efficient.
2300 get_symbol_location_info(unsigned int shndx, off_t offset,
2301 Symbol_location_info* info);
2303 // Look for a kept section corresponding to the given discarded section,
2304 // and return its output address. This is used only for relocations in
2305 // debugging sections.
2307 map_to_kept_section(unsigned int shndx, bool* found) const;
2309 // Compute final local symbol value. R_SYM is the local symbol index.
2310 // LV_IN points to a local symbol value containing the input value.
2311 // LV_OUT points to a local symbol value storing the final output value,
2312 // which must not be a merged symbol value since before calling this
2313 // method to avoid memory leak. SYMTAB points to a symbol table.
2315 // The method returns a status code at return. If the return status is
2316 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2317 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2318 // *LV_OUT is not modified.
2319 Compute_final_local_value_status
2320 compute_final_local_value(unsigned int r_sym,
2321 const Symbol_value<size>* lv_in,
2322 Symbol_value<size>* lv_out,
2323 const Symbol_table* symtab);
2325 // Return true if the layout for this object was deferred.
2326 bool is_deferred_layout() const
2327 { return this->is_deferred_layout_; }
2330 typedef typename Sized_relobj<size, big_endian>::Output_sections
2337 // Read the symbols.
2339 do_read_symbols(Read_symbols_data*);
2341 // Read the symbols. This is common code for all target-specific
2342 // overrides of do_read_symbols.
2344 base_read_symbols(Read_symbols_data*);
2346 // Return the value of a local symbol.
2348 do_local_symbol_value(unsigned int symndx, uint64_t addend) const
2350 const Symbol_value<size>* symval = this->local_symbol(symndx);
2351 return symval->value(this, addend);
2354 // Return the PLT offset for a local symbol. It is an error to call
2355 // this if it doesn't have one.
2357 do_local_plt_offset(unsigned int symndx) const;
2359 // Return whether local symbol SYMNDX is a TLS symbol.
2361 do_local_is_tls(unsigned int symndx) const
2362 { return this->local_symbol(symndx)->is_tls_symbol(); }
2364 // Return the number of local symbols.
2366 do_local_symbol_count() const
2367 { return this->local_symbol_count_; }
2369 // Return the number of local symbols in the output symbol table.
2371 do_output_local_symbol_count() const
2372 { return this->output_local_symbol_count_; }
2374 // Return the number of local symbols in the output symbol table.
2376 do_local_symbol_offset() const
2377 { return this->local_symbol_offset_; }
2379 // Lay out the input sections.
2381 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
2383 // Layout sections whose layout was deferred while waiting for
2384 // input files from a plugin.
2386 do_layout_deferred_sections(Layout*);
2388 // Add the symbols to the symbol table.
2390 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
2392 Archive::Should_include
2393 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
2396 // Iterate over global symbols, calling a visitor class V for each.
2398 do_for_all_global_symbols(Read_symbols_data* sd,
2399 Library_base::Symbol_visitor_base* v);
2403 do_read_relocs(Read_relocs_data*);
2405 // Process the relocs to find list of referenced sections. Used only
2406 // during garbage collection.
2408 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
2410 // Scan the relocs and adjust the symbol table.
2412 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
2414 // Count the local symbols.
2416 do_count_local_symbols(Stringpool_template<char>*,
2417 Stringpool_template<char>*);
2419 // Finalize the local symbols.
2421 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
2423 // Set the offset where local dynamic symbol information will be stored.
2425 do_set_local_dynsym_indexes(unsigned int);
2427 // Set the offset where local dynamic symbol information will be stored.
2429 do_set_local_dynsym_offset(off_t);
2431 // Relocate the input sections and write out the local symbols.
2433 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
2435 // Get the size of a section.
2437 do_section_size(unsigned int shndx)
2438 { return this->elf_file_.section_size(shndx); }
2440 // Get the name of a section.
2442 do_section_name(unsigned int shndx) const
2443 { return this->elf_file_.section_name(shndx); }
2445 // Return the location of the contents of a section.
2446 const unsigned char*
2447 do_section_contents(unsigned int shndx, section_size_type* plen,
2450 Object::Location loc(this->elf_file_.section_contents(shndx));
2451 *plen = convert_to_section_size_type(loc.data_size);
2454 static const unsigned char empty[1] = { '\0' };
2457 return this->get_view(loc.file_offset, *plen, true, cache);
2460 // Return section flags.
2462 do_section_flags(unsigned int shndx);
2464 // Return section entsize.
2466 do_section_entsize(unsigned int shndx);
2468 // Return section address.
2470 do_section_address(unsigned int shndx)
2471 { return this->elf_file_.section_addr(shndx); }
2473 // Return section type.
2475 do_section_type(unsigned int shndx)
2476 { return this->elf_file_.section_type(shndx); }
2478 // Return the section link field.
2480 do_section_link(unsigned int shndx)
2481 { return this->elf_file_.section_link(shndx); }
2483 // Return the section info field.
2485 do_section_info(unsigned int shndx)
2486 { return this->elf_file_.section_info(shndx); }
2488 // Return the section alignment.
2490 do_section_addralign(unsigned int shndx)
2491 { return this->elf_file_.section_addralign(shndx); }
2493 // Return the Xindex structure to use.
2495 do_initialize_xindex();
2497 // Get symbol counts.
2499 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
2501 // Get the global symbols.
2503 do_get_global_symbols() const
2504 { return &this->symbols_; }
2506 // Adjust a section index if necessary.
2508 adjust_shndx(unsigned int shndx)
2510 if (shndx >= elfcpp::SHN_LORESERVE)
2511 shndx += this->elf_file_.large_shndx_offset();
2515 // Initialize input to output maps for section symbols in merged
2518 initialize_input_to_output_maps();
2520 // Free the input to output maps for section symbols in merged
2523 free_input_to_output_maps();
2525 // Return symbol table section index.
2527 symtab_shndx() const
2528 { return this->symtab_shndx_; }
2530 // Allow a child class to access the ELF file.
2531 elfcpp::Elf_file<size, big_endian, Object>*
2533 { return &this->elf_file_; }
2535 // Allow a child class to access the local values.
2538 { return &this->local_values_; }
2540 // Views and sizes when relocating.
2543 unsigned char* view;
2544 typename elfcpp::Elf_types<size>::Elf_Addr address;
2546 section_size_type view_size;
2547 bool is_input_output_view;
2548 bool is_postprocessing_view;
2549 bool is_ctors_reverse_view;
2552 typedef std::vector<View_size> Views;
2554 // Stash away info for a number of special sections.
2555 // Return true if any of the sections found require local symbols to be read.
2557 do_find_special_sections(Read_symbols_data* sd);
2559 // This may be overriden by a child class.
2561 do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
2562 const unsigned char* pshdrs, Output_file* of,
2565 // Relocate section data for a range of sections.
2567 relocate_section_range(const Symbol_table* symtab, const Layout* layout,
2568 const unsigned char* pshdrs, Output_file* of,
2569 Views* pviews, unsigned int start_shndx,
2570 unsigned int end_shndx);
2572 // Adjust this local symbol value. Return false if the symbol
2573 // should be discarded from the output file.
2575 do_adjust_local_symbol(Symbol_value<size>*) const
2578 // Allow a child to set output local symbol count.
2580 set_output_local_symbol_count(unsigned int value)
2581 { this->output_local_symbol_count_ = value; }
2583 // Return the output view for a section.
2585 do_get_output_view(unsigned int, section_size_type*) const;
2589 typedef Sized_relobj_file<size, big_endian> This;
2590 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
2591 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2592 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2593 typedef elfcpp::Shdr<size, big_endian> Shdr;
2595 // To keep track of discarded comdat sections, we need to map a member
2596 // section index to the object and section index of the corresponding
2598 struct Kept_comdat_section
2600 Kept_comdat_section(Relobj* a_object, unsigned int a_shndx)
2601 : object(a_object), shndx(a_shndx)
2606 typedef std::map<unsigned int, Kept_comdat_section>
2607 Kept_comdat_section_table;
2609 // Find the SHT_SYMTAB section, given the section headers.
2611 find_symtab(const unsigned char* pshdrs);
2613 // Return whether SHDR has the right flags for a GNU style exception
2616 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
2618 // Return whether there is a section named .eh_frame which might be
2619 // a GNU style exception frame section.
2621 find_eh_frame(const unsigned char* pshdrs, const char* names,
2622 section_size_type names_size) const;
2624 // Whether to include a section group in the link.
2626 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
2627 const unsigned char*, const char*, section_size_type,
2628 std::vector<bool>*);
2630 // Whether to include a linkonce section in the link.
2632 include_linkonce_section(Layout*, unsigned int, const char*,
2633 const elfcpp::Shdr<size, big_endian>&);
2635 // Layout an input section.
2637 layout_section(Layout* layout, unsigned int shndx, const char* name,
2638 const typename This::Shdr& shdr, unsigned int reloc_shndx,
2639 unsigned int reloc_type);
2641 // Layout an input .eh_frame section.
2643 layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data,
2644 section_size_type symbols_size,
2645 const unsigned char* symbol_names_data,
2646 section_size_type symbol_names_size,
2647 unsigned int shndx, const typename This::Shdr&,
2648 unsigned int reloc_shndx, unsigned int reloc_type);
2650 // Write section data to the output file. Record the views and
2651 // sizes in VIEWS for use when relocating.
2653 write_sections(const Layout*, const unsigned char* pshdrs, Output_file*,
2656 // Relocate the sections in the output file.
2658 relocate_sections(const Symbol_table* symtab, const Layout* layout,
2659 const unsigned char* pshdrs, Output_file* of,
2661 { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
2663 // Reverse the words in a section. Used for .ctors sections mapped
2664 // to .init_array sections.
2666 reverse_words(unsigned char*, section_size_type);
2668 // Scan the input relocations for --emit-relocs.
2670 emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
2671 const Read_relocs_data::Relocs_list::iterator&);
2673 // Scan the input relocations for --emit-relocs, templatized on the
2674 // type of the relocation section.
2675 template<int sh_type>
2677 emit_relocs_scan_reltype(Symbol_table*, Layout*,
2678 const unsigned char* plocal_syms,
2679 const Read_relocs_data::Relocs_list::iterator&,
2680 Relocatable_relocs*);
2682 // Scan the input relocations for --incremental.
2684 incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&);
2686 // Scan the input relocations for --incremental, templatized on the
2687 // type of the relocation section.
2688 template<int sh_type>
2690 incremental_relocs_scan_reltype(
2691 const Read_relocs_data::Relocs_list::iterator&);
2694 incremental_relocs_write(const Relocate_info<size, big_endian>*,
2695 unsigned int sh_type,
2696 const unsigned char* prelocs,
2699 Address output_offset,
2702 template<int sh_type>
2704 incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*,
2705 const unsigned char* prelocs,
2708 Address output_offset,
2711 // A type shared by split_stack_adjust_reltype and find_functions.
2712 typedef std::map<section_offset_type, section_size_type> Function_offsets;
2714 // Check for -fsplit-stack routines calling non-split-stack routines.
2716 split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
2717 unsigned int sh_type, unsigned int shndx,
2718 const unsigned char* prelocs, size_t reloc_count,
2719 unsigned char* view, section_size_type view_size,
2720 Reloc_symbol_changes** reloc_map,
2721 const Sized_target<size, big_endian>* target);
2723 template<int sh_type>
2725 split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
2726 unsigned int shndx, const unsigned char* prelocs,
2727 size_t reloc_count, unsigned char* view,
2728 section_size_type view_size,
2729 Reloc_symbol_changes** reloc_map,
2730 const Sized_target<size, big_endian>* target);
2732 // Find all functions in a section.
2734 find_functions(const unsigned char* pshdrs, unsigned int shndx,
2737 // Write out the local symbols.
2739 write_local_symbols(Output_file*,
2740 const Stringpool_template<char>*,
2741 const Stringpool_template<char>*,
2742 Output_symtab_xindex*,
2743 Output_symtab_xindex*,
2746 // Record a mapping from discarded section SHNDX to the corresponding
2749 set_kept_comdat_section(unsigned int shndx, Relobj* kept_object,
2750 unsigned int kept_shndx)
2752 Kept_comdat_section kept(kept_object, kept_shndx);
2753 this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
2756 // Find the kept section corresponding to the discarded section
2757 // SHNDX. Return true if found.
2759 get_kept_comdat_section(unsigned int shndx, Relobj** kept_object,
2760 unsigned int* kept_shndx) const
2762 typename Kept_comdat_section_table::const_iterator p =
2763 this->kept_comdat_sections_.find(shndx);
2764 if (p == this->kept_comdat_sections_.end())
2766 *kept_object = p->second.object;
2767 *kept_shndx = p->second.shndx;
2771 // Compute final local symbol value. R_SYM is the local symbol index.
2772 // LV_IN points to a local symbol value containing the input value.
2773 // LV_OUT points to a local symbol value storing the final output value,
2774 // which must not be a merged symbol value since before calling this
2775 // method to avoid memory leak. RELOCATABLE indicates whether we are
2776 // linking a relocatable output. OUT_SECTIONS is an array of output
2777 // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB
2778 // points to a symbol table.
2780 // The method returns a status code at return. If the return status is
2781 // CFLV_OK, *LV_OUT contains the final value. If the return status is
2782 // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
2783 // *LV_OUT is not modified.
2784 inline Compute_final_local_value_status
2785 compute_final_local_value_internal(unsigned int r_sym,
2786 const Symbol_value<size>* lv_in,
2787 Symbol_value<size>* lv_out,
2789 const Output_sections& out_sections,
2790 const std::vector<Address>& out_offsets,
2791 const Symbol_table* symtab);
2793 // The PLT offsets of local symbols.
2794 typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
2796 // Saved information for sections whose layout was deferred.
2797 struct Deferred_layout
2799 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2800 Deferred_layout(unsigned int shndx, const char* name,
2801 const unsigned char* pshdr,
2802 unsigned int reloc_shndx, unsigned int reloc_type)
2803 : shndx_(shndx), name_(name), reloc_shndx_(reloc_shndx),
2804 reloc_type_(reloc_type)
2806 memcpy(this->shdr_data_, pshdr, shdr_size);
2808 unsigned int shndx_;
2810 unsigned int reloc_shndx_;
2811 unsigned int reloc_type_;
2812 unsigned char shdr_data_[shdr_size];
2815 // General access to the ELF file.
2816 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
2817 // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed
2818 // as input files only for the --just-symbols option.
2820 // Index of SHT_SYMTAB section.
2821 unsigned int symtab_shndx_;
2822 // The number of local symbols.
2823 unsigned int local_symbol_count_;
2824 // The number of local symbols which go into the output file.
2825 unsigned int output_local_symbol_count_;
2826 // The number of local symbols which go into the output file's dynamic
2828 unsigned int output_local_dynsym_count_;
2829 // The entries in the symbol table for the external symbols.
2831 // Number of symbols defined in object file itself.
2832 size_t defined_count_;
2833 // File offset for local symbols (relative to start of symbol table).
2834 off_t local_symbol_offset_;
2835 // File offset for local dynamic symbols (absolute).
2836 off_t local_dynsym_offset_;
2837 // Values of local symbols.
2838 Local_values local_values_;
2839 // PLT offsets for local symbols.
2840 Local_plt_offsets local_plt_offsets_;
2841 // Table mapping discarded comdat sections to corresponding kept sections.
2842 Kept_comdat_section_table kept_comdat_sections_;
2843 // Whether this object has a GNU style .eh_frame section.
2845 // True if the layout of this object was deferred, waiting for plugin
2846 // replacement files.
2847 bool is_deferred_layout_;
2848 // The list of sections whose layout was deferred.
2849 std::vector<Deferred_layout> deferred_layout_;
2850 // The list of relocation sections whose layout was deferred.
2851 std::vector<Deferred_layout> deferred_layout_relocs_;
2852 // Pointer to the list of output views; valid only during do_relocate().
2853 const Views* output_views_;
2856 // A class to manage the list of all objects.
2862 : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
2865 // The type of the list of input relocateable objects.
2866 typedef std::vector<Relobj*> Relobj_list;
2867 typedef Relobj_list::const_iterator Relobj_iterator;
2869 // The type of the list of input dynamic objects.
2870 typedef std::vector<Dynobj*> Dynobj_list;
2871 typedef Dynobj_list::const_iterator Dynobj_iterator;
2873 // Add an object to the list. Return true if all is well, or false
2874 // if this object should be ignored.
2876 add_object(Object*);
2878 // Start processing an archive.
2880 archive_start(Archive*);
2882 // Stop processing an archive.
2884 archive_stop(Archive*);
2886 // For each dynamic object, check whether we've seen all of its
2887 // explicit dependencies.
2889 check_dynamic_dependencies() const;
2891 // Return whether an object was found in the system library
2894 found_in_system_library_directory(const Object*) const;
2896 // Print symbol counts.
2898 print_symbol_counts(const Symbol_table*) const;
2900 // Print a cross reference table.
2902 print_cref(const Symbol_table*, FILE*) const;
2904 // Iterate over all regular objects.
2907 relobj_begin() const
2908 { return this->relobj_list_.begin(); }
2912 { return this->relobj_list_.end(); }
2914 // Iterate over all dynamic objects.
2917 dynobj_begin() const
2918 { return this->dynobj_list_.begin(); }
2922 { return this->dynobj_list_.end(); }
2924 // Return whether we have seen any dynamic objects.
2927 { return !this->dynobj_list_.empty(); }
2929 // Return the number of non dynamic objects.
2931 number_of_relobjs() const
2932 { return this->relobj_list_.size(); }
2934 // Return the number of input objects.
2936 number_of_input_objects() const
2937 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
2940 Input_objects(const Input_objects&);
2941 Input_objects& operator=(const Input_objects&);
2943 // The list of ordinary objects included in the link.
2944 Relobj_list relobj_list_;
2945 // The list of dynamic objects included in the link.
2946 Dynobj_list dynobj_list_;
2947 // SONAMEs that we have seen.
2948 Unordered_map<std::string, Object*> sonames_;
2949 // Manage cross-references if requested.
2953 // Some of the information we pass to the relocation routines. We
2954 // group this together to avoid passing a dozen different arguments.
2956 template<int size, bool big_endian>
2957 struct Relocate_info
2960 const Symbol_table* symtab;
2962 const Layout* layout;
2963 // Object being relocated.
2964 Sized_relobj_file<size, big_endian>* object;
2965 // Section index of relocation section.
2966 unsigned int reloc_shndx;
2967 // Section header of relocation section.
2968 const unsigned char* reloc_shdr;
2969 // Info about how relocs should be handled
2970 Relocatable_relocs* rr;
2971 // Section index of section being relocated.
2972 unsigned int data_shndx;
2973 // Section header of data section.
2974 const unsigned char* data_shdr;
2976 // Return a string showing the location of a relocation. This is
2977 // only used for error messages.
2979 location(size_t relnum, off_t reloffset) const;
2982 // This is used to represent a section in an object and is used as the
2983 // key type for various section maps.
2984 typedef std::pair<Relobj*, unsigned int> Section_id;
2986 // This is similar to Section_id but is used when the section
2987 // pointers are const.
2988 typedef std::pair<const Relobj*, unsigned int> Const_section_id;
2990 // The hash value is based on the address of an object in memory during
2991 // linking. It is okay to use this for looking up sections but never use
2992 // this in an unordered container that we want to traverse in a repeatable
2995 struct Section_id_hash
2997 size_t operator()(const Section_id& loc) const
2998 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3001 struct Const_section_id_hash
3003 size_t operator()(const Const_section_id& loc) const
3004 { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
3007 // Return whether INPUT_FILE contains an ELF object start at file
3008 // offset OFFSET. This sets *START to point to a view of the start of
3009 // the file. It sets *READ_SIZE to the number of bytes in the view.
3012 is_elf_object(Input_file* input_file, off_t offset,
3013 const unsigned char** start, int* read_size);
3015 // Return an Object appropriate for the input file. P is BYTES long,
3016 // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
3017 // this sees an object the linker is not configured to support, it
3018 // sets *PUNCONFIGURED to true and returns NULL without giving an
3022 make_elf_object(const std::string& name, Input_file*,
3023 off_t offset, const unsigned char* p,
3024 section_offset_type bytes, bool* punconfigured);
3026 } // end namespace gold
3028 #endif // !defined(GOLD_OBJECT_H)