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;
45 template<typename Stringpool_char>
46 class Stringpool_template;
48 // Data to pass from read_symbols() to add_symbols().
50 struct Read_symbols_data
53 File_view* section_headers;
55 File_view* section_names;
56 // Size of section name data in bytes.
57 section_size_type section_names_size;
60 // Size of symbol data in bytes.
61 section_size_type symbols_size;
62 // Offset of external symbols within symbol data. This structure
63 // sometimes contains only external symbols, in which case this will
64 // be zero. Sometimes it contains all symbols.
65 section_offset_type external_symbols_offset;
67 File_view* symbol_names;
68 // Size of symbol name data in bytes.
69 section_size_type symbol_names_size;
71 // Version information. This is only used on dynamic objects.
72 // Version symbol data (from SHT_GNU_versym section).
74 section_size_type versym_size;
75 // Version definition data (from SHT_GNU_verdef section).
77 section_size_type verdef_size;
78 unsigned int verdef_info;
79 // Needed version data (from SHT_GNU_verneed section).
81 section_size_type verneed_size;
82 unsigned int verneed_info;
85 // Information used to print error messages.
87 struct Symbol_location_info
89 std::string source_file;
90 std::string enclosing_symbol_name;
94 // Data about a single relocation section. This is read in
95 // read_relocs and processed in scan_relocs.
99 // Index of reloc section.
100 unsigned int reloc_shndx;
101 // Index of section that relocs apply to.
102 unsigned int data_shndx;
103 // Contents of reloc section.
105 // Reloc section type.
106 unsigned int sh_type;
107 // Number of reloc entries.
110 Output_section* output_section;
111 // Whether this section has special handling for offsets.
112 bool needs_special_offset_handling;
115 // Relocations in an object file. This is read in read_relocs and
116 // processed in scan_relocs.
118 struct Read_relocs_data
120 typedef std::vector<Section_relocs> Relocs_list;
123 // The local symbols.
124 File_view* local_symbols;
127 // Object is an abstract base class which represents either a 32-bit
128 // or a 64-bit input object. This can be a regular object file
129 // (ET_REL) or a shared object (ET_DYN).
134 // NAME is the name of the object as we would report it to the user
135 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
136 // used to read the file. OFFSET is the offset within the input
137 // file--0 for a .o or .so file, something else for a .a file.
138 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
140 : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
141 is_dynamic_(is_dynamic), target_(NULL)
147 // Return the name of the object as we would report it to the tuser.
150 { return this->name_; }
152 // Get the offset into the file.
155 { return this->offset_; }
157 // Return whether this is a dynamic object.
160 { return this->is_dynamic_; }
162 // Return the target structure associated with this object.
165 { return this->target_; }
167 // Lock the underlying file.
170 { this->input_file()->file().lock(t); }
172 // Unlock the underlying file.
174 unlock(const Task* t)
175 { this->input_file()->file().unlock(t); }
177 // Return whether the underlying file is locked.
180 { return this->input_file()->file().is_locked(); }
182 // Return the token, so that the task can be queued.
185 { return this->input_file()->file().token(); }
187 // Release the underlying file.
190 { this->input_file_->file().release(); }
192 // Return the sized target structure associated with this object.
193 // This is like the target method but it returns a pointer of
194 // appropriate checked type.
195 template<int size, bool big_endian>
196 Sized_target<size, big_endian>*
197 sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const;
199 // Get the number of sections.
202 { return this->shnum_; }
204 // Return a view of the contents of a section. Set *PLEN to the
205 // size. CACHE is a hint as in File_read::get_view.
207 section_contents(unsigned int shndx, section_size_type* plen, bool cache);
209 // Return the name of a section given a section index. This is only
210 // used for error messages.
212 section_name(unsigned int shndx)
213 { return this->do_section_name(shndx); }
215 // Return the section flags given a section index.
217 section_flags(unsigned int shndx)
218 { return this->do_section_flags(shndx); }
220 // Return the section type given a section index.
222 section_type(unsigned int shndx)
223 { return this->do_section_type(shndx); }
225 // Return the section link field given a section index.
227 section_link(unsigned int shndx)
228 { return this->do_section_link(shndx); }
230 // Return the section info field given a section index.
232 section_info(unsigned int shndx)
233 { return this->do_section_info(shndx); }
235 // Read the symbol information.
237 read_symbols(Read_symbols_data* sd)
238 { return this->do_read_symbols(sd); }
240 // Pass sections which should be included in the link to the Layout
241 // object, and record where the sections go in the output file.
243 layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
244 { this->do_layout(symtab, layout, sd); }
246 // Add symbol information to the global symbol table.
248 add_symbols(Symbol_table* symtab, Read_symbols_data* sd)
249 { this->do_add_symbols(symtab, sd); }
251 // Functions and types for the elfcpp::Elf_file interface. This
252 // permit us to use Object as the File template parameter for
255 // The View class is returned by view. It must support a single
256 // method, data(). This is trivial, because get_view does what we
261 View(const unsigned char* p)
270 const unsigned char* p_;
275 view(off_t file_offset, section_size_type data_size)
276 { return View(this->get_view(file_offset, data_size, true)); }
280 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
282 // A location in the file.
288 Location(off_t fo, section_size_type ds)
289 : file_offset(fo), data_size(ds)
293 // Get a View given a Location.
294 View view(Location loc)
295 { return View(this->get_view(loc.file_offset, loc.data_size, true)); }
298 // Read the symbols--implemented by child class.
300 do_read_symbols(Read_symbols_data*) = 0;
302 // Lay out sections--implemented by child class.
304 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
306 // Add symbol information to the global symbol table--implemented by
309 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
311 // Return the location of the contents of a section. Implemented by
314 do_section_contents(unsigned int shndx) = 0;
316 // Get the name of a section--implemented by child class.
318 do_section_name(unsigned int shndx) = 0;
320 // Get section flags--implemented by child class.
322 do_section_flags(unsigned int shndx) = 0;
324 // Get section type--implemented by child class.
326 do_section_type(unsigned int shndx) = 0;
328 // Get section link field--implemented by child class.
330 do_section_link(unsigned int shndx) = 0;
332 // Get section info field--implemented by child class.
334 do_section_info(unsigned int shndx) = 0;
336 // Get the file. We pass on const-ness.
339 { return this->input_file_; }
343 { return this->input_file_; }
345 // Get a view into the underlying file.
347 get_view(off_t start, section_size_type size, bool cache)
349 return this->input_file()->file().get_view(start + this->offset_, size,
353 // Get a lasting view into the underlying file.
355 get_lasting_view(off_t start, section_size_type size, bool cache)
357 return this->input_file()->file().get_lasting_view(start + this->offset_,
361 // Read data from the underlying file.
363 read(off_t start, section_size_type size, void* p) const
364 { this->input_file()->file().read(start + this->offset_, size, p); }
368 set_target(int machine, int size, bool big_endian, int osabi,
371 // Set the number of sections.
374 { this->shnum_ = shnum; }
376 // Functions used by both Sized_relobj and Sized_dynobj.
378 // Read the section data into a Read_symbols_data object.
379 template<int size, bool big_endian>
381 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
384 // If NAME is the name of a special .gnu.warning section, arrange
385 // for the warning to be issued. SHNDX is the section index.
386 // Return whether it is a warning section.
388 handle_gnu_warning_section(const char* name, unsigned int shndx,
392 // This class may not be copied.
393 Object(const Object&);
394 Object& operator=(const Object&);
396 // Name of object as printed to user.
398 // For reading the file.
399 Input_file* input_file_;
400 // Offset within the file--0 for an object file, non-0 for an
403 // Number of input sections.
405 // Whether this is a dynamic object.
407 // Target functions--may be NULL if the target is not known.
411 // Implement sized_target inline for efficiency. This approach breaks
412 // static type checking, but is made safe using asserts.
414 template<int size, bool big_endian>
415 inline Sized_target<size, big_endian>*
416 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const
418 gold_assert(this->target_->get_size() == size);
419 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
420 return static_cast<Sized_target<size, big_endian>*>(this->target_);
423 // A regular object (ET_REL). This is an abstract base class itself.
424 // The implementation is the template class Sized_relobj.
426 class Relobj : public Object
429 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
430 : Object(name, input_file, false, offset),
432 object_merge_map_(NULL),
433 relocs_must_follow_section_writes_(false)
438 read_relocs(Read_relocs_data* rd)
439 { return this->do_read_relocs(rd); }
441 // Scan the relocs and adjust the symbol table.
443 scan_relocs(const General_options& options, Symbol_table* symtab,
444 Layout* layout, Read_relocs_data* rd)
445 { return this->do_scan_relocs(options, symtab, layout, rd); }
447 // The number of local symbols in the input symbol table.
449 local_symbol_count() const
450 { return this->do_local_symbol_count(); }
452 // Initial local symbol processing: count the number of local symbols
453 // in the output symbol table and dynamic symbol table; add local symbol
454 // names to *POOL and *DYNPOOL.
456 count_local_symbols(Stringpool_template<char>* pool,
457 Stringpool_template<char>* dynpool)
458 { return this->do_count_local_symbols(pool, dynpool); }
460 // Set the values of the local symbols, set the output symbol table
461 // indexes for the local variables, and set the offset where local
462 // symbol information will be stored. Returns the new local symbol index.
464 finalize_local_symbols(unsigned int index, off_t off)
465 { return this->do_finalize_local_symbols(index, off); }
467 // Set the output dynamic symbol table indexes for the local variables.
469 set_local_dynsym_indexes(unsigned int index)
470 { return this->do_set_local_dynsym_indexes(index); }
472 // Set the offset where local dynamic symbol information will be stored.
474 set_local_dynsym_offset(off_t off)
475 { return this->do_set_local_dynsym_offset(off); }
477 // Relocate the input sections and write out the local symbols.
479 relocate(const General_options& options, const Symbol_table* symtab,
480 const Layout* layout, Output_file* of)
481 { return this->do_relocate(options, symtab, layout, of); }
483 // Return whether an input section is being included in the link.
485 is_section_included(unsigned int shndx) const
487 gold_assert(shndx < this->map_to_output_.size());
488 return this->map_to_output_[shndx].output_section != NULL;
491 // Return whether an input section requires special
492 // handling--whether it is not simply mapped from the input file to
495 is_section_specially_mapped(unsigned int shndx) const
497 gold_assert(shndx < this->map_to_output_.size());
498 return (this->map_to_output_[shndx].output_section != NULL
499 && this->map_to_output_[shndx].offset == -1);
502 // Given a section index, return the corresponding Output_section
503 // (which will be NULL if the section is not included in the link)
504 // and set *POFF to the offset within that section. *POFF will be
505 // set to -1 if the section requires special handling.
506 inline Output_section*
507 output_section(unsigned int shndx, section_offset_type* poff) const;
509 // Set the offset of an input section within its output section.
511 set_section_offset(unsigned int shndx, section_offset_type off)
513 gold_assert(shndx < this->map_to_output_.size());
514 this->map_to_output_[shndx].offset = off;
517 // Return true if we need to wait for output sections to be written
518 // before we can apply relocations. This is true if the object has
519 // any relocations for sections which require special handling, such
520 // as the exception frame section.
522 relocs_must_follow_section_writes() const
523 { return this->relocs_must_follow_section_writes_; }
525 // Return the object merge map.
528 { return this->object_merge_map_; }
530 // Set the object merge map.
532 set_merge_map(Object_merge_map* object_merge_map)
534 gold_assert(this->object_merge_map_ == NULL);
535 this->object_merge_map_ = object_merge_map;
539 // What we need to know to map an input section to an output
540 // section. We keep an array of these, one for each input section,
541 // indexed by the input section number.
544 // The output section. This is NULL if the input section is to be
546 Output_section* output_section;
547 // The offset within the output section. This is -1 if the
548 // section requires special handling.
549 section_offset_type offset;
552 // Read the relocs--implemented by child class.
554 do_read_relocs(Read_relocs_data*) = 0;
556 // Scan the relocs--implemented by child class.
558 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
559 Read_relocs_data*) = 0;
561 // Return the number of local symbols--implemented by child class.
563 do_local_symbol_count() const = 0;
565 // Count local symbols--implemented by child class.
567 do_count_local_symbols(Stringpool_template<char>*,
568 Stringpool_template<char>*) = 0;
570 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
571 // offset where local symbol information will be stored.
573 do_finalize_local_symbols(unsigned int, off_t) = 0;
575 // Set the output dynamic symbol table indexes for the local variables.
577 do_set_local_dynsym_indexes(unsigned int) = 0;
579 // Set the offset where local dynamic symbol information will be stored.
581 do_set_local_dynsym_offset(off_t) = 0;
583 // Relocate the input sections and write out the local
584 // symbols--implemented by child class.
586 do_relocate(const General_options& options, const Symbol_table* symtab,
587 const Layout*, Output_file* of) = 0;
589 // Return the vector mapping input sections to output sections.
590 std::vector<Map_to_output>&
592 { return this->map_to_output_; }
594 const std::vector<Map_to_output>&
595 map_to_output() const
596 { return this->map_to_output_; }
598 // Record that we must wait for the output sections to be written
599 // before applying relocations.
601 set_relocs_must_follow_section_writes()
602 { this->relocs_must_follow_section_writes_ = true; }
605 // Mapping from input sections to output section.
606 std::vector<Map_to_output> map_to_output_;
607 // Mappings for merge sections. This is managed by the code in the
609 Object_merge_map* object_merge_map_;
610 // Whether we need to wait for output sections to be written before
611 // we can apply relocations.
612 bool relocs_must_follow_section_writes_;
615 // Implement Object::output_section inline for efficiency.
616 inline Output_section*
617 Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
619 gold_assert(shndx < this->map_to_output_.size());
620 const Map_to_output& mo(this->map_to_output_[shndx]);
622 return mo.output_section;
625 // This class is used to handle relocations against a section symbol
626 // in an SHF_MERGE section. For such a symbol, we need to know the
627 // addend of the relocation before we can determine the final value.
628 // The addend gives us the location in the input section, and we can
629 // determine how it is mapped to the output section. For a
630 // non-section symbol, we apply the addend to the final value of the
631 // symbol; that is done in finalize_local_symbols, and does not use
635 class Merged_symbol_value
638 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
640 // We use a hash table to map offsets in the input section to output
642 typedef Unordered_map<section_offset_type, Value> Output_addresses;
644 Merged_symbol_value(Value input_value, Value output_start_address)
645 : input_value_(input_value), output_start_address_(output_start_address),
649 // Initialize the hash table.
651 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
653 // Release the hash table to save space.
655 free_input_to_output_map()
656 { this->output_addresses_.clear(); }
658 // Get the output value corresponding to an addend. The object and
659 // input section index are passed in because the caller will have
660 // them; otherwise we could store them here.
662 value(const Relobj* object, unsigned int input_shndx, Value addend) const
664 Value input_offset = this->input_value_ + addend;
665 typename Output_addresses::const_iterator p =
666 this->output_addresses_.find(input_offset);
667 if (p != this->output_addresses_.end())
670 return this->value_from_output_section(object, input_shndx, input_offset);
674 // Get the output value for an input offset if we couldn't find it
675 // in the hash table.
677 value_from_output_section(const Relobj*, unsigned int input_shndx,
678 Value input_offset) const;
680 // The value of the section symbol in the input file. This is
681 // normally zero, but could in principle be something else.
683 // The start address of this merged section in the output file.
684 Value output_start_address_;
685 // A hash table which maps offsets in the input section to output
686 // addresses. This only maps specific offsets, not all offsets.
687 Output_addresses output_addresses_;
690 // This POD class is holds the value of a symbol. This is used for
691 // local symbols, and for all symbols during relocation processing.
692 // For special sections, such as SHF_MERGE sections, this calls a
693 // function to get the final symbol value.
699 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
702 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
703 is_section_symbol_(false), is_tls_symbol_(false),
704 has_output_value_(true)
705 { this->u_.value = 0; }
707 // Get the value of this symbol. OBJECT is the object in which this
708 // symbol is defined, and ADDEND is an addend to add to the value.
709 template<bool big_endian>
711 value(const Sized_relobj<size, big_endian>* object, Value addend) const
713 if (this->has_output_value_)
714 return this->u_.value + addend;
716 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
720 // Set the value of this symbol in the output symbol table.
722 set_output_value(Value value)
723 { this->u_.value = value; }
725 // For a section symbol in a merged section, we need more
728 set_merged_symbol_value(Merged_symbol_value<size>* msv)
730 gold_assert(this->is_section_symbol_);
731 this->has_output_value_ = false;
732 this->u_.merged_symbol_value = msv;
735 // Initialize the input to output map for a section symbol in a
736 // merged section. We also initialize the value of a non-section
737 // symbol in a merged section.
739 initialize_input_to_output_map(const Relobj* object)
741 if (!this->has_output_value_)
743 gold_assert(this->is_section_symbol_);
744 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
745 msv->initialize_input_to_output_map(object, this->input_shndx_);
749 // Free the input to output map for a section symbol in a merged
752 free_input_to_output_map()
754 if (!this->has_output_value_)
755 this->u_.merged_symbol_value->free_input_to_output_map();
758 // Set the value of the symbol from the input file. This is only
759 // called by count_local_symbols, to communicate the value to
760 // finalize_local_symbols.
762 set_input_value(Value value)
763 { this->u_.value = value; }
765 // Return the input value. This is only called by
766 // finalize_local_symbols.
769 { return this->u_.value; }
771 // Return whether this symbol should go into the output symbol
774 needs_output_symtab_entry() const
775 { return this->output_symtab_index_ != -1U; }
777 // Return the index in the output symbol table.
779 output_symtab_index() const
781 gold_assert(this->output_symtab_index_ != 0);
782 return this->output_symtab_index_;
785 // Set the index in the output symbol table.
787 set_output_symtab_index(unsigned int i)
789 gold_assert(this->output_symtab_index_ == 0);
790 this->output_symtab_index_ = i;
793 // Record that this symbol should not go into the output symbol
796 set_no_output_symtab_entry()
798 gold_assert(this->output_symtab_index_ == 0);
799 this->output_symtab_index_ = -1U;
802 // Set the index in the output dynamic symbol table.
804 set_needs_output_dynsym_entry()
806 this->output_dynsym_index_ = 0;
809 // Return whether this symbol should go into the output symbol
812 needs_output_dynsym_entry() const
814 return this->output_dynsym_index_ != -1U;
817 // Record that this symbol should go into the dynamic symbol table.
819 set_output_dynsym_index(unsigned int i)
821 gold_assert(this->output_dynsym_index_ == 0);
822 this->output_dynsym_index_ = i;
825 // Return the index in the output dynamic symbol table.
827 output_dynsym_index() const
829 gold_assert(this->output_dynsym_index_ != 0);
830 return this->output_dynsym_index_;
833 // Set the index of the input section in the input file.
835 set_input_shndx(unsigned int i)
837 this->input_shndx_ = i;
838 // input_shndx_ field is a bitfield, so make sure that the value
840 gold_assert(this->input_shndx_ == i);
843 // Return the index of the input section in the input file.
846 { return this->input_shndx_; }
848 // Whether this is a section symbol.
850 is_section_symbol() const
851 { return this->is_section_symbol_; }
853 // Record that this is a section symbol.
855 set_is_section_symbol()
856 { this->is_section_symbol_ = true; }
858 // Record that this is a TLS symbol.
861 { this->is_tls_symbol_ = true; }
863 // Return TRUE if this is a TLS symbol.
865 is_tls_symbol() const
866 { return this->is_tls_symbol_; }
869 // The index of this local symbol in the output symbol table. This
870 // will be -1 if the symbol should not go into the symbol table.
871 unsigned int output_symtab_index_;
872 // The index of this local symbol in the dynamic symbol table. This
873 // will be -1 if the symbol should not go into the symbol table.
874 unsigned int output_dynsym_index_;
875 // The section index in the input file in which this symbol is
877 unsigned int input_shndx_ : 29;
878 // Whether this is a STT_SECTION symbol.
879 bool is_section_symbol_ : 1;
880 // Whether this is a STT_TLS symbol.
881 bool is_tls_symbol_ : 1;
882 // Whether this symbol has a value for the output file. This is
883 // normally set to true during Layout::finalize, by
884 // finalize_local_symbols. It will be false for a section symbol in
885 // a merge section, as for such symbols we can not determine the
886 // value to use in a relocation until we see the addend.
887 bool has_output_value_ : 1;
890 // This is used if has_output_value_ is true. Between
891 // count_local_symbols and finalize_local_symbols, this is the
892 // value in the input file. After finalize_local_symbols, it is
893 // the value in the output file.
895 // This is used if has_output_value_ is false. It points to the
896 // information we need to get the value for a merge section.
897 Merged_symbol_value<size>* merged_symbol_value;
901 // A regular object file. This is size and endian specific.
903 template<int size, bool big_endian>
904 class Sized_relobj : public Relobj
907 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
908 typedef std::vector<Symbol*> Symbols;
909 typedef std::vector<Symbol_value<size> > Local_values;
911 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
912 const typename elfcpp::Ehdr<size, big_endian>&);
916 // Set up the object file based on the ELF header.
918 setup(const typename elfcpp::Ehdr<size, big_endian>&);
920 // If SYM is the index of a global symbol in the object file's
921 // symbol table, return the Symbol object. Otherwise, return NULL.
923 global_symbol(unsigned int sym) const
925 if (sym >= this->local_symbol_count_)
927 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
928 return this->symbols_[sym - this->local_symbol_count_];
933 // Return the section index of symbol SYM. Set *VALUE to its value
934 // in the object file. Note that for a symbol which is not defined
935 // in this object file, this will set *VALUE to 0 and return
936 // SHN_UNDEF; it will not return the final value of the symbol in
939 symbol_section_and_value(unsigned int sym, Address* value);
941 // Return a pointer to the Symbol_value structure which holds the
942 // value of a local symbol.
943 const Symbol_value<size>*
944 local_symbol(unsigned int sym) const
946 gold_assert(sym < this->local_values_.size());
947 return &this->local_values_[sym];
950 // Return the index of local symbol SYM in the ordinary symbol
951 // table. A value of -1U means that the symbol is not being output.
953 symtab_index(unsigned int sym) const
955 gold_assert(sym < this->local_values_.size());
956 return this->local_values_[sym].output_symtab_index();
959 // Return the index of local symbol SYM in the dynamic symbol
960 // table. A value of -1U means that the symbol is not being output.
962 dynsym_index(unsigned int sym) const
964 gold_assert(sym < this->local_values_.size());
965 return this->local_values_[sym].output_dynsym_index();
968 // Return the appropriate Sized_target structure.
969 Sized_target<size, big_endian>*
972 return this->Object::sized_target
973 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
974 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
977 // Return the value of the local symbol symndx.
979 local_symbol_value(unsigned int symndx) const;
982 set_needs_output_dynsym_entry(unsigned int sym)
984 gold_assert(sym < this->local_values_.size());
985 this->local_values_[sym].set_needs_output_dynsym_entry();
988 // Return whether the local symbol SYMNDX has a GOT offset.
989 // For TLS symbols, the GOT entry will hold its tp-relative offset.
991 local_has_got_offset(unsigned int symndx) const
993 return (this->local_got_offsets_.find(symndx)
994 != this->local_got_offsets_.end());
997 // Return the GOT offset of the local symbol SYMNDX.
999 local_got_offset(unsigned int symndx) const
1001 Local_got_offsets::const_iterator p =
1002 this->local_got_offsets_.find(symndx);
1003 gold_assert(p != this->local_got_offsets_.end());
1007 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1009 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
1011 std::pair<Local_got_offsets::iterator, bool> ins =
1012 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
1013 gold_assert(ins.second);
1016 // Return whether the local TLS symbol SYMNDX has a GOT offset.
1017 // The GOT entry at this offset will contain a module index. If
1018 // NEED_PAIR is true, a second entry immediately following the first
1019 // will contain the dtv-relative offset.
1021 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
1023 typename Local_tls_got_offsets::const_iterator p =
1024 this->local_tls_got_offsets_.find(symndx);
1025 if (p == this->local_tls_got_offsets_.end()
1026 || (need_pair && !p->second.have_pair_))
1031 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1032 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1033 // otherwise we need the offset of the GOT entry for the module index.
1035 local_tls_got_offset(unsigned int symndx, bool need_pair) const
1037 typename Local_tls_got_offsets::const_iterator p =
1038 this->local_tls_got_offsets_.find(symndx);
1039 gold_assert(p != this->local_tls_got_offsets_.end());
1040 gold_assert(!need_pair || p->second.have_pair_);
1041 return p->second.got_offset_;
1044 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1045 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1046 // otherwise, we have just a single entry for the module index.
1048 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
1051 typename Local_tls_got_offsets::iterator p =
1052 this->local_tls_got_offsets_.find(symndx);
1053 if (p != this->local_tls_got_offsets_.end())
1055 // An entry already existed for this symbol. This can happen
1056 // if we see a relocation asking for the module index before
1057 // a relocation asking for the pair. In that case, the original
1058 // GOT entry will remain, but won't get used by any further
1060 p->second.got_offset_ = got_offset;
1061 gold_assert(have_pair);
1062 p->second.have_pair_ = true;
1066 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
1067 this->local_tls_got_offsets_.insert(
1068 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
1069 gold_assert(ins.second);
1073 // Return the name of the symbol that spans the given offset in the
1074 // specified section in this object. This is used only for error
1075 // messages and is not particularly efficient.
1077 get_symbol_location_info(unsigned int shndx, off_t offset,
1078 Symbol_location_info* info);
1081 // Read the symbols.
1083 do_read_symbols(Read_symbols_data*);
1085 // Return the number of local symbols.
1087 do_local_symbol_count() const
1088 { return this->local_symbol_count_; }
1090 // Lay out the input sections.
1092 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1094 // Add the symbols to the symbol table.
1096 do_add_symbols(Symbol_table*, Read_symbols_data*);
1100 do_read_relocs(Read_relocs_data*);
1102 // Scan the relocs and adjust the symbol table.
1104 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1107 // Count the local symbols.
1109 do_count_local_symbols(Stringpool_template<char>*,
1110 Stringpool_template<char>*);
1112 // Finalize the local symbols.
1114 do_finalize_local_symbols(unsigned int, off_t);
1116 // Set the offset where local dynamic symbol information will be stored.
1118 do_set_local_dynsym_indexes(unsigned int);
1120 // Set the offset where local dynamic symbol information will be stored.
1122 do_set_local_dynsym_offset(off_t);
1124 // Relocate the input sections and write out the local symbols.
1126 do_relocate(const General_options& options, const Symbol_table* symtab,
1127 const Layout*, Output_file* of);
1129 // Get the name of a section.
1131 do_section_name(unsigned int shndx)
1132 { return this->elf_file_.section_name(shndx); }
1134 // Return the location of the contents of a section.
1136 do_section_contents(unsigned int shndx)
1137 { return this->elf_file_.section_contents(shndx); }
1139 // Return section flags.
1141 do_section_flags(unsigned int shndx)
1142 { return this->elf_file_.section_flags(shndx); }
1144 // Return section type.
1146 do_section_type(unsigned int shndx)
1147 { return this->elf_file_.section_type(shndx); }
1149 // Return the section link field.
1151 do_section_link(unsigned int shndx)
1152 { return this->elf_file_.section_link(shndx); }
1154 // Return the section info field.
1156 do_section_info(unsigned int shndx)
1157 { return this->elf_file_.section_info(shndx); }
1161 typedef Sized_relobj<size, big_endian> This;
1162 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1163 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1164 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1165 typedef elfcpp::Shdr<size, big_endian> Shdr;
1167 // Find the SHT_SYMTAB section, given the section headers.
1169 find_symtab(const unsigned char* pshdrs);
1171 // Return whether SHDR has the right flags for a GNU style exception
1174 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1176 // Return whether there is a section named .eh_frame which might be
1177 // a GNU style exception frame section.
1179 find_eh_frame(const unsigned char* pshdrs, const char* names,
1180 section_size_type names_size) const;
1182 // Whether to include a section group in the link.
1184 include_section_group(Layout*, unsigned int,
1185 const elfcpp::Shdr<size, big_endian>&,
1186 std::vector<bool>*);
1188 // Whether to include a linkonce section in the link.
1190 include_linkonce_section(Layout*, const char*,
1191 const elfcpp::Shdr<size, big_endian>&);
1193 // Views and sizes when relocating.
1196 unsigned char* view;
1197 typename elfcpp::Elf_types<size>::Elf_Addr address;
1199 section_size_type view_size;
1200 bool is_input_output_view;
1201 bool is_postprocessing_view;
1204 typedef std::vector<View_size> Views;
1206 // Write section data to the output file. Record the views and
1207 // sizes in VIEWS for use when relocating.
1209 write_sections(const unsigned char* pshdrs, Output_file*, Views*) const;
1211 // Relocate the sections in the output file.
1213 relocate_sections(const General_options& options, const Symbol_table*,
1214 const Layout*, const unsigned char* pshdrs, Views*);
1216 // Initialize input to output maps for section symbols in merged
1219 initialize_input_to_output_maps();
1221 // Free the input to output maps for section symbols in merged
1224 free_input_to_output_maps();
1226 // Write out the local symbols.
1228 write_local_symbols(Output_file*,
1229 const Stringpool_template<char>*,
1230 const Stringpool_template<char>*);
1232 // The GOT offsets of local symbols. This map also stores GOT offsets
1233 // for tp-relative offsets for TLS symbols.
1234 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1236 // The TLS GOT offsets of local symbols. The map stores the offsets
1237 // for either a single GOT entry that holds the module index of a TLS
1238 // symbol, or a pair of GOT entries containing the module index and
1239 // dtv-relative offset.
1240 struct Tls_got_entry
1242 Tls_got_entry(int got_offset, bool have_pair)
1243 : got_offset_(got_offset),
1244 have_pair_(have_pair)
1249 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1251 // General access to the ELF file.
1252 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1253 // Index of SHT_SYMTAB section.
1254 unsigned int symtab_shndx_;
1255 // The number of local symbols.
1256 unsigned int local_symbol_count_;
1257 // The number of local symbols which go into the output file.
1258 unsigned int output_local_symbol_count_;
1259 // The number of local symbols which go into the output file's dynamic
1261 unsigned int output_local_dynsym_count_;
1262 // The entries in the symbol table for the external symbols.
1264 // File offset for local symbols.
1265 off_t local_symbol_offset_;
1266 // File offset for local dynamic symbols.
1267 off_t local_dynsym_offset_;
1268 // Values of local symbols.
1269 Local_values local_values_;
1270 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1271 // for TLS symbols, indexed by symbol number.
1272 Local_got_offsets local_got_offsets_;
1273 // GOT offsets for local TLS symbols, indexed by symbol number
1274 // and GOT entry type.
1275 Local_tls_got_offsets local_tls_got_offsets_;
1276 // Whether this object has a GNU style .eh_frame section.
1280 // A class to manage the list of all objects.
1286 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1287 system_library_directory_()
1290 // The type of the list of input relocateable objects.
1291 typedef std::vector<Relobj*> Relobj_list;
1292 typedef Relobj_list::const_iterator Relobj_iterator;
1294 // The type of the list of input dynamic objects.
1295 typedef std::vector<Dynobj*> Dynobj_list;
1296 typedef Dynobj_list::const_iterator Dynobj_iterator;
1298 // Add an object to the list. Return true if all is well, or false
1299 // if this object should be ignored.
1301 add_object(Object*);
1303 // Get the target we should use for the output file.
1306 { return this->target_; }
1308 // For each dynamic object, check whether we've seen all of its
1309 // explicit dependencies.
1311 check_dynamic_dependencies() const;
1313 // Return whether an object was found in the system library
1316 found_in_system_library_directory(const Object*) const;
1318 // Iterate over all regular objects.
1321 relobj_begin() const
1322 { return this->relobj_list_.begin(); }
1326 { return this->relobj_list_.end(); }
1328 // Iterate over all dynamic objects.
1331 dynobj_begin() const
1332 { return this->dynobj_list_.begin(); }
1336 { return this->dynobj_list_.end(); }
1338 // Return whether we have seen any dynamic objects.
1341 { return !this->dynobj_list_.empty(); }
1343 // Return the number of input objects.
1345 number_of_input_objects() const
1346 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1349 Input_objects(const Input_objects&);
1350 Input_objects& operator=(const Input_objects&);
1352 // The list of ordinary objects included in the link.
1353 Relobj_list relobj_list_;
1354 // The list of dynamic objects included in the link.
1355 Dynobj_list dynobj_list_;
1358 // SONAMEs that we have seen.
1359 Unordered_set<std::string> sonames_;
1360 // The directory in which we find the libc.so.
1361 std::string system_library_directory_;
1364 // Some of the information we pass to the relocation routines. We
1365 // group this together to avoid passing a dozen different arguments.
1367 template<int size, bool big_endian>
1368 struct Relocate_info
1370 // Command line options.
1371 const General_options* options;
1373 const Symbol_table* symtab;
1375 const Layout* layout;
1376 // Object being relocated.
1377 Sized_relobj<size, big_endian>* object;
1378 // Section index of relocation section.
1379 unsigned int reloc_shndx;
1380 // Section index of section being relocated.
1381 unsigned int data_shndx;
1383 // Return a string showing the location of a relocation. This is
1384 // only used for error messages.
1386 location(size_t relnum, off_t reloffset) const;
1389 // Return an Object appropriate for the input file. P is BYTES long,
1390 // and holds the ELF header.
1393 make_elf_object(const std::string& name, Input_file*,
1394 off_t offset, const unsigned char* p,
1395 section_offset_type bytes);
1397 } // end namespace gold
1399 #endif // !defined(GOLD_OBJECT_H)