1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 // Copyright 2006, 2007 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;
43 class Object_merge_map;
44 class Relocatable_relocs;
46 template<typename Stringpool_char>
47 class Stringpool_template;
49 // Data to pass from read_symbols() to add_symbols().
51 struct Read_symbols_data
54 File_view* section_headers;
56 File_view* section_names;
57 // Size of section name data in bytes.
58 section_size_type section_names_size;
61 // Size of symbol data in bytes.
62 section_size_type symbols_size;
63 // Offset of external symbols within symbol data. This structure
64 // sometimes contains only external symbols, in which case this will
65 // be zero. Sometimes it contains all symbols.
66 section_offset_type external_symbols_offset;
68 File_view* symbol_names;
69 // Size of symbol name data in bytes.
70 section_size_type symbol_names_size;
72 // Version information. This is only used on dynamic objects.
73 // Version symbol data (from SHT_GNU_versym section).
75 section_size_type versym_size;
76 // Version definition data (from SHT_GNU_verdef section).
78 section_size_type verdef_size;
79 unsigned int verdef_info;
80 // Needed version data (from SHT_GNU_verneed section).
82 section_size_type verneed_size;
83 unsigned int verneed_info;
86 // Information used to print error messages.
88 struct Symbol_location_info
90 std::string source_file;
91 std::string enclosing_symbol_name;
95 // Data about a single relocation section. This is read in
96 // read_relocs and processed in scan_relocs.
100 // Index of reloc section.
101 unsigned int reloc_shndx;
102 // Index of section that relocs apply to.
103 unsigned int data_shndx;
104 // Contents of reloc section.
106 // Reloc section type.
107 unsigned int sh_type;
108 // Number of reloc entries.
111 Output_section* output_section;
112 // Whether this section has special handling for offsets.
113 bool needs_special_offset_handling;
114 // Whether the data section is allocated (has the SHF_ALLOC flag set).
115 bool is_data_section_allocated;
118 // Relocations in an object file. This is read in read_relocs and
119 // processed in scan_relocs.
121 struct Read_relocs_data
123 typedef std::vector<Section_relocs> Relocs_list;
126 // The local symbols.
127 File_view* local_symbols;
130 // Object is an abstract base class which represents either a 32-bit
131 // or a 64-bit input object. This can be a regular object file
132 // (ET_REL) or a shared object (ET_DYN).
137 // NAME is the name of the object as we would report it to the user
138 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
139 // used to read the file. OFFSET is the offset within the input
140 // file--0 for a .o or .so file, something else for a .a file.
141 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
143 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
144 is_dynamic_(is_dynamic), target_(NULL)
145 { input_file->file().add_object(); }
148 { this->input_file_->file().remove_object(); }
150 // Return the name of the object as we would report it to the tuser.
153 { return this->name_; }
155 // Get the offset into the file.
158 { return this->offset_; }
160 // Return whether this is a dynamic object.
163 { return this->is_dynamic_; }
165 // Return the target structure associated with this object.
168 { return this->target_; }
170 // Lock the underlying file.
173 { this->input_file()->file().lock(t); }
175 // Unlock the underlying file.
177 unlock(const Task* t)
178 { this->input_file()->file().unlock(t); }
180 // Return whether the underlying file is locked.
183 { return this->input_file()->file().is_locked(); }
185 // Return the token, so that the task can be queued.
188 { return this->input_file()->file().token(); }
190 // Release the underlying file.
193 { this->input_file_->file().release(); }
195 // Return whether we should just read symbols from this file.
198 { return this->input_file()->just_symbols(); }
200 // Return the sized target structure associated with this object.
201 // This is like the target method but it returns a pointer of
202 // appropriate checked type.
203 template<int size, bool big_endian>
204 Sized_target<size, big_endian>*
205 sized_target() const;
207 // Get the number of sections.
210 { return this->shnum_; }
212 // Return a view of the contents of a section. Set *PLEN to the
213 // size. CACHE is a hint as in File_read::get_view.
215 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
217 // Return the size of a section given a section index.
219 section_size(unsigned int shndx)
220 { return this->do_section_size(shndx); }
222 // Return the name of a section given a section index.
224 section_name(unsigned int shndx)
225 { return this->do_section_name(shndx); }
227 // Return the section flags given a section index.
229 section_flags(unsigned int shndx)
230 { return this->do_section_flags(shndx); }
232 // Return the section address given a section index.
234 section_address(unsigned int shndx)
235 { return this->do_section_address(shndx); }
237 // Return the section type given a section index.
239 section_type(unsigned int shndx)
240 { return this->do_section_type(shndx); }
242 // Return the section link field given a section index.
244 section_link(unsigned int shndx)
245 { return this->do_section_link(shndx); }
247 // Return the section info field given a section index.
249 section_info(unsigned int shndx)
250 { return this->do_section_info(shndx); }
252 // Return the required section alignment given a section index.
254 section_addralign(unsigned int shndx)
255 { return this->do_section_addralign(shndx); }
257 // Read the symbol information.
259 read_symbols(Read_symbols_data* sd)
260 { return this->do_read_symbols(sd); }
262 // Pass sections which should be included in the link to the Layout
263 // object, and record where the sections go in the output file.
265 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
266 { this->do_layout(symtab, layout, sd); }
268 // Add symbol information to the global symbol table.
270 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
271 { this->do_add_symbols(symtab, sd); }
273 // Functions and types for the elfcpp::Elf_file interface. This
274 // permit us to use Object as the File template parameter for
277 // The View class is returned by view. It must support a single
278 // method, data(). This is trivial, because get_view does what we
283 View(const unsigned char* p)
292 const unsigned char* p_;
297 view(off_t file_offset, section_size_type data_size)
298 { return View(this->get_view(file_offset, data_size, true)); }
302 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
304 // A location in the file.
310 Location(off_t fo, section_size_type ds)
311 : file_offset(fo), data_size(ds)
315 // Get a View given a Location.
316 View view(Location loc)
317 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
319 // Get a view into the underlying file.
321 get_view(off_t start, section_size_type size, bool cache)
323 return this->input_file()->file().get_view(start + this->offset_, size,
327 // Get a lasting view into the underlying file.
329 get_lasting_view(off_t start, section_size_type size, bool cache)
331 return this->input_file()->file().get_lasting_view(start + this->offset_,
335 // Read data from the underlying file.
337 read(off_t start, section_size_type size, void* p) const
338 { this->input_file()->file().read(start + this->offset_, size, p); }
340 // Read multiple data from the underlying file.
342 read_multiple(const File_read::Read_multiple& rm)
343 { this->input_file()->file().read_multiple(this->offset_, rm); }
345 // Stop caching views in the underlying file.
347 clear_view_cache_marks()
348 { this->input_file()->file().clear_view_cache_marks(); }
351 // Read the symbols--implemented by child class.
353 do_read_symbols(Read_symbols_data*) = 0;
355 // Lay out sections--implemented by child class.
357 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
359 // Add symbol information to the global symbol table--implemented by
362 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
364 // Return the location of the contents of a section. Implemented by
367 do_section_contents(unsigned int shndx) = 0;
369 // Get the size of a section--implemented by child class.
371 do_section_size(unsigned int shndx) = 0;
373 // Get the name of a section--implemented by child class.
375 do_section_name(unsigned int shndx) = 0;
377 // Get section flags--implemented by child class.
379 do_section_flags(unsigned int shndx) = 0;
381 // Get section address--implemented by child class.
383 do_section_address(unsigned int shndx) = 0;
385 // Get section type--implemented by child class.
387 do_section_type(unsigned int shndx) = 0;
389 // Get section link field--implemented by child class.
391 do_section_link(unsigned int shndx) = 0;
393 // Get section info field--implemented by child class.
395 do_section_info(unsigned int shndx) = 0;
397 // Get section alignment--implemented by child class.
399 do_section_addralign(unsigned int shndx) = 0;
401 // Get the file. We pass on const-ness.
404 { return this->input_file_; }
408 { return this->input_file_; }
412 set_target(int machine, int size, bool big_endian, int osabi,
415 // Set the number of sections.
418 { this->shnum_ = shnum; }
420 // Functions used by both Sized_relobj and Sized_dynobj.
422 // Read the section data into a Read_symbols_data object.
423 template<int size, bool big_endian>
425 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
428 // If NAME is the name of a special .gnu.warning section, arrange
429 // for the warning to be issued. SHNDX is the section index.
430 // Return whether it is a warning section.
432 handle_gnu_warning_section(const char* name, unsigned int shndx,
436 // This class may not be copied.
437 Object(const Object&);
438 Object& operator=(const Object&);
440 // Name of object as printed to user.
442 // For reading the file.
443 Input_file* input_file_;
444 // Offset within the file--0 for an object file, non-0 for an
447 // Number of input sections.
449 // Whether this is a dynamic object.
451 // Target functions--may be NULL if the target is not known.
455 // Implement sized_target inline for efficiency. This approach breaks
456 // static type checking, but is made safe using asserts.
458 template<int size, bool big_endian>
459 inline Sized_target<size, big_endian>*
460 Object::sized_target() const
462 gold_assert(this->target_->get_size() == size);
463 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
464 return static_cast<Sized_target<size, big_endian>*>(this->target_);
467 // A regular object (ET_REL). This is an abstract base class itself.
468 // The implementation is the template class Sized_relobj.
470 class Relobj : public Object
473 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
474 : Object(name, input_file, false, offset),
476 map_to_relocatable_relocs_(NULL),
477 object_merge_map_(NULL),
478 relocs_must_follow_section_writes_(false)
483 read_relocs(Read_relocs_data* rd)
484 { return this->do_read_relocs(rd); }
486 // Scan the relocs and adjust the symbol table.
488 scan_relocs(const General_options& options, Symbol_table* symtab,
489 Layout* layout, Read_relocs_data* rd)
490 { return this->do_scan_relocs(options, symtab, layout, rd); }
492 // The number of local symbols in the input symbol table.
494 local_symbol_count() const
495 { return this->do_local_symbol_count(); }
497 // Initial local symbol processing: count the number of local symbols
498 // in the output symbol table and dynamic symbol table; add local symbol
499 // names to *POOL and *DYNPOOL.
501 count_local_symbols(Stringpool_template<char>* pool,
502 Stringpool_template<char>* dynpool)
503 { return this->do_count_local_symbols(pool, dynpool); }
505 // Set the values of the local symbols, set the output symbol table
506 // indexes for the local variables, and set the offset where local
507 // symbol information will be stored. Returns the new local symbol index.
509 finalize_local_symbols(unsigned int index, off_t off)
510 { return this->do_finalize_local_symbols(index, off); }
512 // Set the output dynamic symbol table indexes for the local variables.
514 set_local_dynsym_indexes(unsigned int index)
515 { return this->do_set_local_dynsym_indexes(index); }
517 // Set the offset where local dynamic symbol information will be stored.
519 set_local_dynsym_offset(off_t off)
520 { return this->do_set_local_dynsym_offset(off); }
522 // Relocate the input sections and write out the local symbols.
524 relocate(const General_options& options, const Symbol_table* symtab,
525 const Layout* layout, Output_file* of)
526 { return this->do_relocate(options, symtab, layout, of); }
528 // Return whether an input section is being included in the link.
530 is_section_included(unsigned int shndx) const
532 gold_assert(shndx < this->map_to_output_.size());
533 return this->map_to_output_[shndx].output_section != NULL;
536 // Return whether an input section requires special
537 // handling--whether it is not simply mapped from the input file to
540 is_section_specially_mapped(unsigned int shndx) const
542 gold_assert(shndx < this->map_to_output_.size());
543 return (this->map_to_output_[shndx].output_section != NULL
544 && this->map_to_output_[shndx].offset == -1);
547 // Given a section index, return the corresponding Output_section
548 // (which will be NULL if the section is not included in the link)
549 // and set *POFF to the offset within that section. *POFF will be
550 // set to -1 if the section requires special handling.
551 inline Output_section*
552 output_section(unsigned int shndx, section_offset_type* poff) const;
554 // Set the offset of an input section within its output section.
556 set_section_offset(unsigned int shndx, section_offset_type off)
558 gold_assert(shndx < this->map_to_output_.size());
559 this->map_to_output_[shndx].offset = off;
562 // Return true if we need to wait for output sections to be written
563 // before we can apply relocations. This is true if the object has
564 // any relocations for sections which require special handling, such
565 // as the exception frame section.
567 relocs_must_follow_section_writes() const
568 { return this->relocs_must_follow_section_writes_; }
570 // Return the object merge map.
573 { return this->object_merge_map_; }
575 // Set the object merge map.
577 set_merge_map(Object_merge_map* object_merge_map)
579 gold_assert(this->object_merge_map_ == NULL);
580 this->object_merge_map_ = object_merge_map;
583 // Record the relocatable reloc info for an input reloc section.
585 set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
587 gold_assert(reloc_shndx < this->shnum());
588 (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
591 // Get the relocatable reloc info for an input reloc section.
593 relocatable_relocs(unsigned int reloc_shndx)
595 gold_assert(reloc_shndx < this->shnum());
596 return (*this->map_to_relocatable_relocs_)[reloc_shndx];
600 // What we need to know to map an input section to an output
601 // section. We keep an array of these, one for each input section,
602 // indexed by the input section number.
605 // The output section. This is NULL if the input section is to be
607 Output_section* output_section;
608 // The offset within the output section. This is -1 if the
609 // section requires special handling.
610 section_offset_type offset;
613 // Read the relocs--implemented by child class.
615 do_read_relocs(Read_relocs_data*) = 0;
617 // Scan the relocs--implemented by child class.
619 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
620 Read_relocs_data*) = 0;
622 // Return the number of local symbols--implemented by child class.
624 do_local_symbol_count() const = 0;
626 // Count local symbols--implemented by child class.
628 do_count_local_symbols(Stringpool_template<char>*,
629 Stringpool_template<char>*) = 0;
631 // Finalize the local symbols. Set the output symbol table indexes
632 // for the local variables, and set the offset where local symbol
633 // information will be stored.
635 do_finalize_local_symbols(unsigned int, off_t) = 0;
637 // Set the output dynamic symbol table indexes for the local variables.
639 do_set_local_dynsym_indexes(unsigned int) = 0;
641 // Set the offset where local dynamic symbol information will be stored.
643 do_set_local_dynsym_offset(off_t) = 0;
645 // Relocate the input sections and write out the local
646 // symbols--implemented by child class.
648 do_relocate(const General_options& options, const Symbol_table* symtab,
649 const Layout*, Output_file* of) = 0;
651 // Return the vector mapping input sections to output sections.
652 std::vector<Map_to_output>&
654 { return this->map_to_output_; }
656 const std::vector<Map_to_output>&
657 map_to_output() const
658 { return this->map_to_output_; }
660 // Set the size of the relocatable relocs array.
662 size_relocatable_relocs()
664 this->map_to_relocatable_relocs_ =
665 new std::vector<Relocatable_relocs*>(this->shnum());
668 // Record that we must wait for the output sections to be written
669 // before applying relocations.
671 set_relocs_must_follow_section_writes()
672 { this->relocs_must_follow_section_writes_ = true; }
675 // Mapping from input sections to output section.
676 std::vector<Map_to_output> map_to_output_;
677 // Mapping from input section index to the information recorded for
678 // the relocations. This is only used for a relocatable link.
679 std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
680 // Mappings for merge sections. This is managed by the code in the
682 Object_merge_map* object_merge_map_;
683 // Whether we need to wait for output sections to be written before
684 // we can apply relocations.
685 bool relocs_must_follow_section_writes_;
688 // Implement Object::output_section inline for efficiency.
689 inline Output_section*
690 Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
692 gold_assert(shndx < this->map_to_output_.size());
693 const Map_to_output& mo(this->map_to_output_[shndx]);
695 return mo.output_section;
698 // This class is used to handle relocations against a section symbol
699 // in an SHF_MERGE section. For such a symbol, we need to know the
700 // addend of the relocation before we can determine the final value.
701 // The addend gives us the location in the input section, and we can
702 // determine how it is mapped to the output section. For a
703 // non-section symbol, we apply the addend to the final value of the
704 // symbol; that is done in finalize_local_symbols, and does not use
708 class Merged_symbol_value
711 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
713 // We use a hash table to map offsets in the input section to output
715 typedef Unordered_map<section_offset_type, Value> Output_addresses;
717 Merged_symbol_value(Value input_value, Value output_start_address)
718 : input_value_(input_value), output_start_address_(output_start_address),
722 // Initialize the hash table.
724 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
726 // Release the hash table to save space.
728 free_input_to_output_map()
729 { this->output_addresses_.clear(); }
731 // Get the output value corresponding to an addend. The object and
732 // input section index are passed in because the caller will have
733 // them; otherwise we could store them here.
735 value(const Relobj* object, unsigned int input_shndx, Value addend) const
737 Value input_offset = this->input_value_ + addend;
738 typename Output_addresses::const_iterator p =
739 this->output_addresses_.find(input_offset);
740 if (p != this->output_addresses_.end())
743 return this->value_from_output_section(object, input_shndx, input_offset);
747 // Get the output value for an input offset if we couldn't find it
748 // in the hash table.
750 value_from_output_section(const Relobj*, unsigned int input_shndx,
751 Value input_offset) const;
753 // The value of the section symbol in the input file. This is
754 // normally zero, but could in principle be something else.
756 // The start address of this merged section in the output file.
757 Value output_start_address_;
758 // A hash table which maps offsets in the input section to output
759 // addresses. This only maps specific offsets, not all offsets.
760 Output_addresses output_addresses_;
763 // This POD class is holds the value of a symbol. This is used for
764 // local symbols, and for all symbols during relocation processing.
765 // For special sections, such as SHF_MERGE sections, this calls a
766 // function to get the final symbol value.
772 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
775 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
776 is_section_symbol_(false), is_tls_symbol_(false),
777 has_output_value_(true)
778 { this->u_.value = 0; }
780 // Get the value of this symbol. OBJECT is the object in which this
781 // symbol is defined, and ADDEND is an addend to add to the value.
782 template<bool big_endian>
784 value(const Sized_relobj<size, big_endian>* object, Value addend) const
786 if (this->has_output_value_)
787 return this->u_.value + addend;
789 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
793 // Set the value of this symbol in the output symbol table.
795 set_output_value(Value value)
796 { this->u_.value = value; }
798 // For a section symbol in a merged section, we need more
801 set_merged_symbol_value(Merged_symbol_value<size>* msv)
803 gold_assert(this->is_section_symbol_);
804 this->has_output_value_ = false;
805 this->u_.merged_symbol_value = msv;
808 // Initialize the input to output map for a section symbol in a
809 // merged section. We also initialize the value of a non-section
810 // symbol in a merged section.
812 initialize_input_to_output_map(const Relobj* object)
814 if (!this->has_output_value_)
816 gold_assert(this->is_section_symbol_);
817 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
818 msv->initialize_input_to_output_map(object, this->input_shndx_);
822 // Free the input to output map for a section symbol in a merged
825 free_input_to_output_map()
827 if (!this->has_output_value_)
828 this->u_.merged_symbol_value->free_input_to_output_map();
831 // Set the value of the symbol from the input file. This is only
832 // called by count_local_symbols, to communicate the value to
833 // finalize_local_symbols.
835 set_input_value(Value value)
836 { this->u_.value = value; }
838 // Return the input value. This is only called by
839 // finalize_local_symbols.
842 { return this->u_.value; }
844 // Return whether this symbol should go into the output symbol
847 needs_output_symtab_entry() const
848 { return this->output_symtab_index_ != -1U; }
850 // Return the index in the output symbol table.
852 output_symtab_index() const
854 gold_assert(this->output_symtab_index_ != 0);
855 return this->output_symtab_index_;
858 // Set the index in the output symbol table.
860 set_output_symtab_index(unsigned int i)
862 gold_assert(this->output_symtab_index_ == 0);
863 this->output_symtab_index_ = i;
866 // Record that this symbol should not go into the output symbol
869 set_no_output_symtab_entry()
871 gold_assert(this->output_symtab_index_ == 0);
872 this->output_symtab_index_ = -1U;
875 // Set the index in the output dynamic symbol table.
877 set_needs_output_dynsym_entry()
879 gold_assert(!this->is_section_symbol());
880 this->output_dynsym_index_ = 0;
883 // Return whether this symbol should go into the output symbol
886 needs_output_dynsym_entry() const
888 return this->output_dynsym_index_ != -1U;
891 // Record that this symbol should go into the dynamic symbol table.
893 set_output_dynsym_index(unsigned int i)
895 gold_assert(this->output_dynsym_index_ == 0);
896 this->output_dynsym_index_ = i;
899 // Return the index in the output dynamic symbol table.
901 output_dynsym_index() const
903 gold_assert(this->output_dynsym_index_ != 0
904 && this->output_dynsym_index_ != -1U);
905 return this->output_dynsym_index_;
908 // Set the index of the input section in the input file.
910 set_input_shndx(unsigned int i)
912 this->input_shndx_ = i;
913 // input_shndx_ field is a bitfield, so make sure that the value
915 gold_assert(this->input_shndx_ == i);
918 // Return the index of the input section in the input file.
921 { return this->input_shndx_; }
923 // Whether this is a section symbol.
925 is_section_symbol() const
926 { return this->is_section_symbol_; }
928 // Record that this is a section symbol.
930 set_is_section_symbol()
932 gold_assert(!this->needs_output_dynsym_entry());
933 this->is_section_symbol_ = true;
936 // Record that this is a TLS symbol.
939 { this->is_tls_symbol_ = true; }
941 // Return TRUE if this is a TLS symbol.
943 is_tls_symbol() const
944 { return this->is_tls_symbol_; }
947 // The index of this local symbol in the output symbol table. This
948 // will be -1 if the symbol should not go into the symbol table.
949 unsigned int output_symtab_index_;
950 // The index of this local symbol in the dynamic symbol table. This
951 // will be -1 if the symbol should not go into the symbol table.
952 unsigned int output_dynsym_index_;
953 // The section index in the input file in which this symbol is
955 unsigned int input_shndx_ : 29;
956 // Whether this is a STT_SECTION symbol.
957 bool is_section_symbol_ : 1;
958 // Whether this is a STT_TLS symbol.
959 bool is_tls_symbol_ : 1;
960 // Whether this symbol has a value for the output file. This is
961 // normally set to true during Layout::finalize, by
962 // finalize_local_symbols. It will be false for a section symbol in
963 // a merge section, as for such symbols we can not determine the
964 // value to use in a relocation until we see the addend.
965 bool has_output_value_ : 1;
968 // This is used if has_output_value_ is true. Between
969 // count_local_symbols and finalize_local_symbols, this is the
970 // value in the input file. After finalize_local_symbols, it is
971 // the value in the output file.
973 // This is used if has_output_value_ is false. It points to the
974 // information we need to get the value for a merge section.
975 Merged_symbol_value<size>* merged_symbol_value;
979 // A regular object file. This is size and endian specific.
981 template<int size, bool big_endian>
982 class Sized_relobj : public Relobj
985 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
986 typedef std::vector<Symbol*> Symbols;
987 typedef std::vector<Symbol_value<size> > Local_values;
989 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
990 const typename elfcpp::Ehdr<size, big_endian>&);
994 // Set up the object file based on the ELF header.
996 setup(const typename elfcpp::Ehdr<size, big_endian>&);
998 // If SYM is the index of a global symbol in the object file's
999 // symbol table, return the Symbol object. Otherwise, return NULL.
1001 global_symbol(unsigned int sym) const
1003 if (sym >= this->local_symbol_count_)
1005 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
1006 return this->symbols_[sym - this->local_symbol_count_];
1011 // Return the section index of symbol SYM. Set *VALUE to its value
1012 // in the object file. Note that for a symbol which is not defined
1013 // in this object file, this will set *VALUE to 0 and return
1014 // SHN_UNDEF; it will not return the final value of the symbol in
1017 symbol_section_and_value(unsigned int sym, Address* value);
1019 // Return a pointer to the Symbol_value structure which holds the
1020 // value of a local symbol.
1021 const Symbol_value<size>*
1022 local_symbol(unsigned int sym) const
1024 gold_assert(sym < this->local_values_.size());
1025 return &this->local_values_[sym];
1028 // Return the index of local symbol SYM in the ordinary symbol
1029 // table. A value of -1U means that the symbol is not being output.
1031 symtab_index(unsigned int sym) const
1033 gold_assert(sym < this->local_values_.size());
1034 return this->local_values_[sym].output_symtab_index();
1037 // Return the index of local symbol SYM in the dynamic symbol
1038 // table. A value of -1U means that the symbol is not being output.
1040 dynsym_index(unsigned int sym) const
1042 gold_assert(sym < this->local_values_.size());
1043 return this->local_values_[sym].output_dynsym_index();
1046 // Return the input section index of local symbol SYM.
1048 local_symbol_input_shndx(unsigned int sym) const
1050 gold_assert(sym < this->local_values_.size());
1051 return this->local_values_[sym].input_shndx();
1054 // Return the appropriate Sized_target structure.
1055 Sized_target<size, big_endian>*
1057 { return this->Object::sized_target<size, big_endian>(); }
1059 // Return the value of the local symbol symndx.
1061 local_symbol_value(unsigned int symndx) const;
1064 set_needs_output_dynsym_entry(unsigned int sym)
1066 gold_assert(sym < this->local_values_.size());
1067 this->local_values_[sym].set_needs_output_dynsym_entry();
1070 // Return whether the local symbol SYMNDX has a GOT offset.
1071 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1073 local_has_got_offset(unsigned int symndx) const
1075 return (this->local_got_offsets_.find(symndx)
1076 != this->local_got_offsets_.end());
1079 // Return the GOT offset of the local symbol SYMNDX.
1081 local_got_offset(unsigned int symndx) const
1083 Local_got_offsets::const_iterator p =
1084 this->local_got_offsets_.find(symndx);
1085 gold_assert(p != this->local_got_offsets_.end());
1089 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1091 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
1093 std::pair<Local_got_offsets::iterator, bool> ins =
1094 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
1095 gold_assert(ins.second);
1098 // Return whether the local TLS symbol SYMNDX has a GOT offset.
1099 // The GOT entry at this offset will contain a module index. If
1100 // NEED_PAIR is true, a second entry immediately following the first
1101 // will contain the dtv-relative offset.
1103 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
1105 typename Local_tls_got_offsets::const_iterator p =
1106 this->local_tls_got_offsets_.find(symndx);
1107 if (p == this->local_tls_got_offsets_.end()
1108 || (need_pair && !p->second.have_pair_))
1113 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1114 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1115 // otherwise we need the offset of the GOT entry for the module index.
1117 local_tls_got_offset(unsigned int symndx, bool need_pair) const
1119 typename Local_tls_got_offsets::const_iterator p =
1120 this->local_tls_got_offsets_.find(symndx);
1121 gold_assert(p != this->local_tls_got_offsets_.end());
1122 gold_assert(!need_pair || p->second.have_pair_);
1123 return p->second.got_offset_;
1126 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1127 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1128 // otherwise, we have just a single entry for the module index.
1130 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
1133 typename Local_tls_got_offsets::iterator p =
1134 this->local_tls_got_offsets_.find(symndx);
1135 if (p != this->local_tls_got_offsets_.end())
1137 // An entry already existed for this symbol. This can happen
1138 // if we see a relocation asking for the module index before
1139 // a relocation asking for the pair. In that case, the original
1140 // GOT entry will remain, but won't get used by any further
1142 p->second.got_offset_ = got_offset;
1143 gold_assert(have_pair);
1144 p->second.have_pair_ = true;
1148 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
1149 this->local_tls_got_offsets_.insert(
1150 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
1151 gold_assert(ins.second);
1155 // Return the name of the symbol that spans the given offset in the
1156 // specified section in this object. This is used only for error
1157 // messages and is not particularly efficient.
1159 get_symbol_location_info(unsigned int shndx, off_t offset,
1160 Symbol_location_info* info);
1163 // Read the symbols.
1165 do_read_symbols(Read_symbols_data*);
1167 // Return the number of local symbols.
1169 do_local_symbol_count() const
1170 { return this->local_symbol_count_; }
1172 // Lay out the input sections.
1174 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1176 // Add the symbols to the symbol table.
1178 do_add_symbols(Symbol_table*, Read_symbols_data*);
1182 do_read_relocs(Read_relocs_data*);
1184 // Scan the relocs and adjust the symbol table.
1186 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1189 // Count the local symbols.
1191 do_count_local_symbols(Stringpool_template<char>*,
1192 Stringpool_template<char>*);
1194 // Finalize the local symbols.
1196 do_finalize_local_symbols(unsigned int, off_t);
1198 // Set the offset where local dynamic symbol information will be stored.
1200 do_set_local_dynsym_indexes(unsigned int);
1202 // Set the offset where local dynamic symbol information will be stored.
1204 do_set_local_dynsym_offset(off_t);
1206 // Relocate the input sections and write out the local symbols.
1208 do_relocate(const General_options& options, const Symbol_table* symtab,
1209 const Layout*, Output_file* of);
1211 // Get the size of a section.
1213 do_section_size(unsigned int shndx)
1214 { return this->elf_file_.section_size(shndx); }
1216 // Get the name of a section.
1218 do_section_name(unsigned int shndx)
1219 { return this->elf_file_.section_name(shndx); }
1221 // Return the location of the contents of a section.
1223 do_section_contents(unsigned int shndx)
1224 { return this->elf_file_.section_contents(shndx); }
1226 // Return section flags.
1228 do_section_flags(unsigned int shndx)
1229 { return this->elf_file_.section_flags(shndx); }
1231 // Return section address.
1233 do_section_address(unsigned int shndx)
1234 { return this->elf_file_.section_addr(shndx); }
1236 // Return section type.
1238 do_section_type(unsigned int shndx)
1239 { return this->elf_file_.section_type(shndx); }
1241 // Return the section link field.
1243 do_section_link(unsigned int shndx)
1244 { return this->elf_file_.section_link(shndx); }
1246 // Return the section info field.
1248 do_section_info(unsigned int shndx)
1249 { return this->elf_file_.section_info(shndx); }
1251 // Return the section alignment.
1253 do_section_addralign(unsigned int shndx)
1254 { return this->elf_file_.section_addralign(shndx); }
1258 typedef Sized_relobj<size, big_endian> This;
1259 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1260 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1261 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1262 typedef elfcpp::Shdr<size, big_endian> Shdr;
1264 // Find the SHT_SYMTAB section, given the section headers.
1266 find_symtab(const unsigned char* pshdrs);
1268 // Return whether SHDR has the right flags for a GNU style exception
1271 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1273 // Return whether there is a section named .eh_frame which might be
1274 // a GNU style exception frame section.
1276 find_eh_frame(const unsigned char* pshdrs, const char* names,
1277 section_size_type names_size) const;
1279 // Whether to include a section group in the link.
1281 include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
1282 const elfcpp::Shdr<size, big_endian>&,
1283 std::vector<bool>*);
1285 // Whether to include a linkonce section in the link.
1287 include_linkonce_section(Layout*, const char*,
1288 const elfcpp::Shdr<size, big_endian>&);
1290 // Views and sizes when relocating.
1293 unsigned char* view;
1294 typename elfcpp::Elf_types<size>::Elf_Addr address;
1296 section_size_type view_size;
1297 bool is_input_output_view;
1298 bool is_postprocessing_view;
1301 typedef std::vector<View_size> Views;
1303 // Write section data to the output file. Record the views and
1304 // sizes in VIEWS for use when relocating.
1306 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1308 // Relocate the sections in the output file.
1310 relocate_sections(const General_options& options, const Symbol_table*,
1311 const Layout*, const unsigned char* pshdrs, Views*);
1313 // Scan the input relocations for --emit-relocs.
1315 emit_relocs_scan(const General_options&, Symbol_table*, Layout*,
1316 const unsigned char* plocal_syms,
1317 const Read_relocs_data::Relocs_list::iterator&);
1319 // Scan the input relocations for --emit-relocs, templatized on the
1320 // type of the relocation section.
1321 template<int sh_type>
1323 emit_relocs_scan_reltype(const General_options&, Symbol_table*, Layout*,
1324 const unsigned char* plocal_syms,
1325 const Read_relocs_data::Relocs_list::iterator&,
1326 Relocatable_relocs*);
1328 // Emit the relocs for --emit-relocs.
1330 emit_relocs(const Relocate_info<size, big_endian>*, unsigned int,
1331 unsigned int sh_type, const unsigned char* prelocs,
1332 size_t reloc_count, Output_section*, off_t output_offset,
1333 unsigned char* view, Address address,
1334 section_size_type view_size,
1335 unsigned char* reloc_view, section_size_type reloc_view_size);
1337 // Emit the relocs for --emit-relocs, templatized on the type of the
1338 // relocation section.
1339 template<int sh_type>
1341 emit_relocs_reltype(const Relocate_info<size, big_endian>*, unsigned int,
1342 const unsigned char* prelocs, size_t reloc_count,
1343 Output_section*, off_t output_offset,
1344 unsigned char* view, Address address,
1345 section_size_type view_size,
1346 unsigned char* reloc_view,
1347 section_size_type reloc_view_size);
1349 // Initialize input to output maps for section symbols in merged
1352 initialize_input_to_output_maps();
1354 // Free the input to output maps for section symbols in merged
1357 free_input_to_output_maps();
1359 // Write out the local symbols.
1361 write_local_symbols(Output_file*,
1362 const Stringpool_template<char>*,
1363 const Stringpool_template<char>*);
1365 // Clear the local symbol information.
1367 clear_local_symbols()
1369 this->local_values_.clear();
1370 this->local_got_offsets_.clear();
1371 this->local_tls_got_offsets_.clear();
1374 // The GOT offsets of local symbols. This map also stores GOT offsets
1375 // for tp-relative offsets for TLS symbols.
1376 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1378 // The TLS GOT offsets of local symbols. The map stores the offsets
1379 // for either a single GOT entry that holds the module index of a TLS
1380 // symbol, or a pair of GOT entries containing the module index and
1381 // dtv-relative offset.
1382 struct Tls_got_entry
1384 Tls_got_entry(int got_offset, bool have_pair)
1385 : got_offset_(got_offset),
1386 have_pair_(have_pair)
1391 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1393 // General access to the ELF file.
1394 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1395 // Index of SHT_SYMTAB section.
1396 unsigned int symtab_shndx_;
1397 // The number of local symbols.
1398 unsigned int local_symbol_count_;
1399 // The number of local symbols which go into the output file.
1400 unsigned int output_local_symbol_count_;
1401 // The number of local symbols which go into the output file's dynamic
1403 unsigned int output_local_dynsym_count_;
1404 // The entries in the symbol table for the external symbols.
1406 // File offset for local symbols.
1407 off_t local_symbol_offset_;
1408 // File offset for local dynamic symbols.
1409 off_t local_dynsym_offset_;
1410 // Values of local symbols.
1411 Local_values local_values_;
1412 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1413 // for TLS symbols, indexed by symbol number.
1414 Local_got_offsets local_got_offsets_;
1415 // GOT offsets for local TLS symbols, indexed by symbol number
1416 // and GOT entry type.
1417 Local_tls_got_offsets local_tls_got_offsets_;
1418 // Whether this object has a GNU style .eh_frame section.
1422 // A class to manage the list of all objects.
1428 : relobj_list_(), dynobj_list_(), sonames_(), system_library_directory_()
1431 // The type of the list of input relocateable objects.
1432 typedef std::vector<Relobj*> Relobj_list;
1433 typedef Relobj_list::const_iterator Relobj_iterator;
1435 // The type of the list of input dynamic objects.
1436 typedef std::vector<Dynobj*> Dynobj_list;
1437 typedef Dynobj_list::const_iterator Dynobj_iterator;
1439 // Add an object to the list. Return true if all is well, or false
1440 // if this object should be ignored.
1442 add_object(Object*);
1444 // For each dynamic object, check whether we've seen all of its
1445 // explicit dependencies.
1447 check_dynamic_dependencies() const;
1449 // Return whether an object was found in the system library
1452 found_in_system_library_directory(const Object*) const;
1454 // Iterate over all regular objects.
1457 relobj_begin() const
1458 { return this->relobj_list_.begin(); }
1462 { return this->relobj_list_.end(); }
1464 // Iterate over all dynamic objects.
1467 dynobj_begin() const
1468 { return this->dynobj_list_.begin(); }
1472 { return this->dynobj_list_.end(); }
1474 // Return whether we have seen any dynamic objects.
1477 { return !this->dynobj_list_.empty(); }
1479 // Return the number of input objects.
1481 number_of_input_objects() const
1482 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1485 Input_objects(const Input_objects&);
1486 Input_objects& operator=(const Input_objects&);
1488 // The list of ordinary objects included in the link.
1489 Relobj_list relobj_list_;
1490 // The list of dynamic objects included in the link.
1491 Dynobj_list dynobj_list_;
1492 // SONAMEs that we have seen.
1493 Unordered_set<std::string> sonames_;
1494 // The directory in which we find the libc.so.
1495 std::string system_library_directory_;
1498 // Some of the information we pass to the relocation routines. We
1499 // group this together to avoid passing a dozen different arguments.
1501 template<int size, bool big_endian>
1502 struct Relocate_info
1504 // Command line options.
1505 const General_options* options;
1507 const Symbol_table* symtab;
1509 const Layout* layout;
1510 // Object being relocated.
1511 Sized_relobj<size, big_endian>* object;
1512 // Section index of relocation section.
1513 unsigned int reloc_shndx;
1514 // Section index of section being relocated.
1515 unsigned int data_shndx;
1517 // Return a string showing the location of a relocation. This is
1518 // only used for error messages.
1520 location(size_t relnum, off_t reloffset) const;
1523 // Return an Object appropriate for the input file. P is BYTES long,
1524 // and holds the ELF header.
1527 make_elf_object(const std::string& name, Input_file*,
1528 off_t offset, const unsigned char* p,
1529 section_offset_type bytes);
1531 } // end namespace gold
1533 #endif // !defined(GOLD_OBJECT_H)