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)
142 { input_file->file().add_object(); }
145 { this->input_file_->file().remove_object(); }
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)); }
297 // Get a view into the underlying file.
299 get_view(off_t start, section_size_type size, bool cache)
301 return this->input_file()->file().get_view(start + this->offset_, size,
305 // Get a lasting view into the underlying file.
307 get_lasting_view(off_t start, section_size_type size, bool cache)
309 return this->input_file()->file().get_lasting_view(start + this->offset_,
313 // Read data from the underlying file.
315 read(off_t start, section_size_type size, void* p) const
316 { this->input_file()->file().read(start + this->offset_, size, p); }
318 // Read multiple data from the underlying file.
320 read_multiple(const File_read::Read_multiple& rm)
321 { this->input_file()->file().read_multiple(this->offset_, rm); }
323 // Stop caching views in the underlying file.
325 clear_view_cache_marks()
326 { this->input_file()->file().clear_view_cache_marks(); }
329 // Read the symbols--implemented by child class.
331 do_read_symbols(Read_symbols_data*) = 0;
333 // Lay out sections--implemented by child class.
335 do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
337 // Add symbol information to the global symbol table--implemented by
340 do_add_symbols(Symbol_table*, Read_symbols_data*) = 0;
342 // Return the location of the contents of a section. Implemented by
345 do_section_contents(unsigned int shndx) = 0;
347 // Get the name of a section--implemented by child class.
349 do_section_name(unsigned int shndx) = 0;
351 // Get section flags--implemented by child class.
353 do_section_flags(unsigned int shndx) = 0;
355 // Get section type--implemented by child class.
357 do_section_type(unsigned int shndx) = 0;
359 // Get section link field--implemented by child class.
361 do_section_link(unsigned int shndx) = 0;
363 // Get section info field--implemented by child class.
365 do_section_info(unsigned int shndx) = 0;
367 // Get the file. We pass on const-ness.
370 { return this->input_file_; }
374 { return this->input_file_; }
378 set_target(int machine, int size, bool big_endian, int osabi,
381 // Set the number of sections.
384 { this->shnum_ = shnum; }
386 // Functions used by both Sized_relobj and Sized_dynobj.
388 // Read the section data into a Read_symbols_data object.
389 template<int size, bool big_endian>
391 read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
394 // If NAME is the name of a special .gnu.warning section, arrange
395 // for the warning to be issued. SHNDX is the section index.
396 // Return whether it is a warning section.
398 handle_gnu_warning_section(const char* name, unsigned int shndx,
402 // This class may not be copied.
403 Object(const Object&);
404 Object& operator=(const Object&);
406 // Name of object as printed to user.
408 // For reading the file.
409 Input_file* input_file_;
410 // Offset within the file--0 for an object file, non-0 for an
413 // Number of input sections.
415 // Whether this is a dynamic object.
417 // Target functions--may be NULL if the target is not known.
421 // Implement sized_target inline for efficiency. This approach breaks
422 // static type checking, but is made safe using asserts.
424 template<int size, bool big_endian>
425 inline Sized_target<size, big_endian>*
426 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY) const
428 gold_assert(this->target_->get_size() == size);
429 gold_assert(this->target_->is_big_endian() ? big_endian : !big_endian);
430 return static_cast<Sized_target<size, big_endian>*>(this->target_);
433 // A regular object (ET_REL). This is an abstract base class itself.
434 // The implementation is the template class Sized_relobj.
436 class Relobj : public Object
439 Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
440 : Object(name, input_file, false, offset),
442 object_merge_map_(NULL),
443 relocs_must_follow_section_writes_(false)
448 read_relocs(Read_relocs_data* rd)
449 { return this->do_read_relocs(rd); }
451 // Scan the relocs and adjust the symbol table.
453 scan_relocs(const General_options& options, Symbol_table* symtab,
454 Layout* layout, Read_relocs_data* rd)
455 { return this->do_scan_relocs(options, symtab, layout, rd); }
457 // The number of local symbols in the input symbol table.
459 local_symbol_count() const
460 { return this->do_local_symbol_count(); }
462 // Initial local symbol processing: count the number of local symbols
463 // in the output symbol table and dynamic symbol table; add local symbol
464 // names to *POOL and *DYNPOOL.
466 count_local_symbols(Stringpool_template<char>* pool,
467 Stringpool_template<char>* dynpool)
468 { return this->do_count_local_symbols(pool, dynpool); }
470 // Set the values of the local symbols, set the output symbol table
471 // indexes for the local variables, and set the offset where local
472 // symbol information will be stored. Returns the new local symbol index.
474 finalize_local_symbols(unsigned int index, off_t off)
475 { return this->do_finalize_local_symbols(index, off); }
477 // Set the output dynamic symbol table indexes for the local variables.
479 set_local_dynsym_indexes(unsigned int index)
480 { return this->do_set_local_dynsym_indexes(index); }
482 // Set the offset where local dynamic symbol information will be stored.
484 set_local_dynsym_offset(off_t off)
485 { return this->do_set_local_dynsym_offset(off); }
487 // Relocate the input sections and write out the local symbols.
489 relocate(const General_options& options, const Symbol_table* symtab,
490 const Layout* layout, Output_file* of)
491 { return this->do_relocate(options, symtab, layout, of); }
493 // Return whether an input section is being included in the link.
495 is_section_included(unsigned int shndx) const
497 gold_assert(shndx < this->map_to_output_.size());
498 return this->map_to_output_[shndx].output_section != NULL;
501 // Return whether an input section requires special
502 // handling--whether it is not simply mapped from the input file to
505 is_section_specially_mapped(unsigned int shndx) const
507 gold_assert(shndx < this->map_to_output_.size());
508 return (this->map_to_output_[shndx].output_section != NULL
509 && this->map_to_output_[shndx].offset == -1);
512 // Given a section index, return the corresponding Output_section
513 // (which will be NULL if the section is not included in the link)
514 // and set *POFF to the offset within that section. *POFF will be
515 // set to -1 if the section requires special handling.
516 inline Output_section*
517 output_section(unsigned int shndx, section_offset_type* poff) const;
519 // Set the offset of an input section within its output section.
521 set_section_offset(unsigned int shndx, section_offset_type off)
523 gold_assert(shndx < this->map_to_output_.size());
524 this->map_to_output_[shndx].offset = off;
527 // Return true if we need to wait for output sections to be written
528 // before we can apply relocations. This is true if the object has
529 // any relocations for sections which require special handling, such
530 // as the exception frame section.
532 relocs_must_follow_section_writes() const
533 { return this->relocs_must_follow_section_writes_; }
535 // Return the object merge map.
538 { return this->object_merge_map_; }
540 // Set the object merge map.
542 set_merge_map(Object_merge_map* object_merge_map)
544 gold_assert(this->object_merge_map_ == NULL);
545 this->object_merge_map_ = object_merge_map;
549 // What we need to know to map an input section to an output
550 // section. We keep an array of these, one for each input section,
551 // indexed by the input section number.
554 // The output section. This is NULL if the input section is to be
556 Output_section* output_section;
557 // The offset within the output section. This is -1 if the
558 // section requires special handling.
559 section_offset_type offset;
562 // Read the relocs--implemented by child class.
564 do_read_relocs(Read_relocs_data*) = 0;
566 // Scan the relocs--implemented by child class.
568 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
569 Read_relocs_data*) = 0;
571 // Return the number of local symbols--implemented by child class.
573 do_local_symbol_count() const = 0;
575 // Count local symbols--implemented by child class.
577 do_count_local_symbols(Stringpool_template<char>*,
578 Stringpool_template<char>*) = 0;
580 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
581 // offset where local symbol information will be stored.
583 do_finalize_local_symbols(unsigned int, off_t) = 0;
585 // Set the output dynamic symbol table indexes for the local variables.
587 do_set_local_dynsym_indexes(unsigned int) = 0;
589 // Set the offset where local dynamic symbol information will be stored.
591 do_set_local_dynsym_offset(off_t) = 0;
593 // Relocate the input sections and write out the local
594 // symbols--implemented by child class.
596 do_relocate(const General_options& options, const Symbol_table* symtab,
597 const Layout*, Output_file* of) = 0;
599 // Return the vector mapping input sections to output sections.
600 std::vector<Map_to_output>&
602 { return this->map_to_output_; }
604 const std::vector<Map_to_output>&
605 map_to_output() const
606 { return this->map_to_output_; }
608 // Record that we must wait for the output sections to be written
609 // before applying relocations.
611 set_relocs_must_follow_section_writes()
612 { this->relocs_must_follow_section_writes_ = true; }
615 // Mapping from input sections to output section.
616 std::vector<Map_to_output> map_to_output_;
617 // Mappings for merge sections. This is managed by the code in the
619 Object_merge_map* object_merge_map_;
620 // Whether we need to wait for output sections to be written before
621 // we can apply relocations.
622 bool relocs_must_follow_section_writes_;
625 // Implement Object::output_section inline for efficiency.
626 inline Output_section*
627 Relobj::output_section(unsigned int shndx, section_offset_type* poff) const
629 gold_assert(shndx < this->map_to_output_.size());
630 const Map_to_output& mo(this->map_to_output_[shndx]);
632 return mo.output_section;
635 // This class is used to handle relocations against a section symbol
636 // in an SHF_MERGE section. For such a symbol, we need to know the
637 // addend of the relocation before we can determine the final value.
638 // The addend gives us the location in the input section, and we can
639 // determine how it is mapped to the output section. For a
640 // non-section symbol, we apply the addend to the final value of the
641 // symbol; that is done in finalize_local_symbols, and does not use
645 class Merged_symbol_value
648 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
650 // We use a hash table to map offsets in the input section to output
652 typedef Unordered_map<section_offset_type, Value> Output_addresses;
654 Merged_symbol_value(Value input_value, Value output_start_address)
655 : input_value_(input_value), output_start_address_(output_start_address),
659 // Initialize the hash table.
661 initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
663 // Release the hash table to save space.
665 free_input_to_output_map()
666 { this->output_addresses_.clear(); }
668 // Get the output value corresponding to an addend. The object and
669 // input section index are passed in because the caller will have
670 // them; otherwise we could store them here.
672 value(const Relobj* object, unsigned int input_shndx, Value addend) const
674 Value input_offset = this->input_value_ + addend;
675 typename Output_addresses::const_iterator p =
676 this->output_addresses_.find(input_offset);
677 if (p != this->output_addresses_.end())
680 return this->value_from_output_section(object, input_shndx, input_offset);
684 // Get the output value for an input offset if we couldn't find it
685 // in the hash table.
687 value_from_output_section(const Relobj*, unsigned int input_shndx,
688 Value input_offset) const;
690 // The value of the section symbol in the input file. This is
691 // normally zero, but could in principle be something else.
693 // The start address of this merged section in the output file.
694 Value output_start_address_;
695 // A hash table which maps offsets in the input section to output
696 // addresses. This only maps specific offsets, not all offsets.
697 Output_addresses output_addresses_;
700 // This POD class is holds the value of a symbol. This is used for
701 // local symbols, and for all symbols during relocation processing.
702 // For special sections, such as SHF_MERGE sections, this calls a
703 // function to get the final symbol value.
709 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
712 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
713 is_section_symbol_(false), is_tls_symbol_(false),
714 has_output_value_(true)
715 { this->u_.value = 0; }
717 // Get the value of this symbol. OBJECT is the object in which this
718 // symbol is defined, and ADDEND is an addend to add to the value.
719 template<bool big_endian>
721 value(const Sized_relobj<size, big_endian>* object, Value addend) const
723 if (this->has_output_value_)
724 return this->u_.value + addend;
726 return this->u_.merged_symbol_value->value(object, this->input_shndx_,
730 // Set the value of this symbol in the output symbol table.
732 set_output_value(Value value)
733 { this->u_.value = value; }
735 // For a section symbol in a merged section, we need more
738 set_merged_symbol_value(Merged_symbol_value<size>* msv)
740 gold_assert(this->is_section_symbol_);
741 this->has_output_value_ = false;
742 this->u_.merged_symbol_value = msv;
745 // Initialize the input to output map for a section symbol in a
746 // merged section. We also initialize the value of a non-section
747 // symbol in a merged section.
749 initialize_input_to_output_map(const Relobj* object)
751 if (!this->has_output_value_)
753 gold_assert(this->is_section_symbol_);
754 Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
755 msv->initialize_input_to_output_map(object, this->input_shndx_);
759 // Free the input to output map for a section symbol in a merged
762 free_input_to_output_map()
764 if (!this->has_output_value_)
765 this->u_.merged_symbol_value->free_input_to_output_map();
768 // Set the value of the symbol from the input file. This is only
769 // called by count_local_symbols, to communicate the value to
770 // finalize_local_symbols.
772 set_input_value(Value value)
773 { this->u_.value = value; }
775 // Return the input value. This is only called by
776 // finalize_local_symbols.
779 { return this->u_.value; }
781 // Return whether this symbol should go into the output symbol
784 needs_output_symtab_entry() const
785 { return this->output_symtab_index_ != -1U; }
787 // Return the index in the output symbol table.
789 output_symtab_index() const
791 gold_assert(this->output_symtab_index_ != 0);
792 return this->output_symtab_index_;
795 // Set the index in the output symbol table.
797 set_output_symtab_index(unsigned int i)
799 gold_assert(this->output_symtab_index_ == 0);
800 this->output_symtab_index_ = i;
803 // Record that this symbol should not go into the output symbol
806 set_no_output_symtab_entry()
808 gold_assert(this->output_symtab_index_ == 0);
809 this->output_symtab_index_ = -1U;
812 // Set the index in the output dynamic symbol table.
814 set_needs_output_dynsym_entry()
816 this->output_dynsym_index_ = 0;
819 // Return whether this symbol should go into the output symbol
822 needs_output_dynsym_entry() const
824 return this->output_dynsym_index_ != -1U;
827 // Record that this symbol should go into the dynamic symbol table.
829 set_output_dynsym_index(unsigned int i)
831 gold_assert(this->output_dynsym_index_ == 0);
832 this->output_dynsym_index_ = i;
835 // Return the index in the output dynamic symbol table.
837 output_dynsym_index() const
839 gold_assert(this->output_dynsym_index_ != 0);
840 return this->output_dynsym_index_;
843 // Set the index of the input section in the input file.
845 set_input_shndx(unsigned int i)
847 this->input_shndx_ = i;
848 // input_shndx_ field is a bitfield, so make sure that the value
850 gold_assert(this->input_shndx_ == i);
853 // Return the index of the input section in the input file.
856 { return this->input_shndx_; }
858 // Whether this is a section symbol.
860 is_section_symbol() const
861 { return this->is_section_symbol_; }
863 // Record that this is a section symbol.
865 set_is_section_symbol()
866 { this->is_section_symbol_ = true; }
868 // Record that this is a TLS symbol.
871 { this->is_tls_symbol_ = true; }
873 // Return TRUE if this is a TLS symbol.
875 is_tls_symbol() const
876 { return this->is_tls_symbol_; }
879 // The index of this local symbol in the output symbol table. This
880 // will be -1 if the symbol should not go into the symbol table.
881 unsigned int output_symtab_index_;
882 // The index of this local symbol in the dynamic symbol table. This
883 // will be -1 if the symbol should not go into the symbol table.
884 unsigned int output_dynsym_index_;
885 // The section index in the input file in which this symbol is
887 unsigned int input_shndx_ : 29;
888 // Whether this is a STT_SECTION symbol.
889 bool is_section_symbol_ : 1;
890 // Whether this is a STT_TLS symbol.
891 bool is_tls_symbol_ : 1;
892 // Whether this symbol has a value for the output file. This is
893 // normally set to true during Layout::finalize, by
894 // finalize_local_symbols. It will be false for a section symbol in
895 // a merge section, as for such symbols we can not determine the
896 // value to use in a relocation until we see the addend.
897 bool has_output_value_ : 1;
900 // This is used if has_output_value_ is true. Between
901 // count_local_symbols and finalize_local_symbols, this is the
902 // value in the input file. After finalize_local_symbols, it is
903 // the value in the output file.
905 // This is used if has_output_value_ is false. It points to the
906 // information we need to get the value for a merge section.
907 Merged_symbol_value<size>* merged_symbol_value;
911 // A regular object file. This is size and endian specific.
913 template<int size, bool big_endian>
914 class Sized_relobj : public Relobj
917 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
918 typedef std::vector<Symbol*> Symbols;
919 typedef std::vector<Symbol_value<size> > Local_values;
921 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
922 const typename elfcpp::Ehdr<size, big_endian>&);
926 // Set up the object file based on the ELF header.
928 setup(const typename elfcpp::Ehdr<size, big_endian>&);
930 // If SYM is the index of a global symbol in the object file's
931 // symbol table, return the Symbol object. Otherwise, return NULL.
933 global_symbol(unsigned int sym) const
935 if (sym >= this->local_symbol_count_)
937 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
938 return this->symbols_[sym - this->local_symbol_count_];
943 // Return the section index of symbol SYM. Set *VALUE to its value
944 // in the object file. Note that for a symbol which is not defined
945 // in this object file, this will set *VALUE to 0 and return
946 // SHN_UNDEF; it will not return the final value of the symbol in
949 symbol_section_and_value(unsigned int sym, Address* value);
951 // Return a pointer to the Symbol_value structure which holds the
952 // value of a local symbol.
953 const Symbol_value<size>*
954 local_symbol(unsigned int sym) const
956 gold_assert(sym < this->local_values_.size());
957 return &this->local_values_[sym];
960 // Return the index of local symbol SYM in the ordinary symbol
961 // table. A value of -1U means that the symbol is not being output.
963 symtab_index(unsigned int sym) const
965 gold_assert(sym < this->local_values_.size());
966 return this->local_values_[sym].output_symtab_index();
969 // Return the index of local symbol SYM in the dynamic symbol
970 // table. A value of -1U means that the symbol is not being output.
972 dynsym_index(unsigned int sym) const
974 gold_assert(sym < this->local_values_.size());
975 return this->local_values_[sym].output_dynsym_index();
978 // Return the appropriate Sized_target structure.
979 Sized_target<size, big_endian>*
982 return this->Object::sized_target
983 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
984 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
987 // Return the value of the local symbol symndx.
989 local_symbol_value(unsigned int symndx) const;
992 set_needs_output_dynsym_entry(unsigned int sym)
994 gold_assert(sym < this->local_values_.size());
995 this->local_values_[sym].set_needs_output_dynsym_entry();
998 // Return whether the local symbol SYMNDX has a GOT offset.
999 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1001 local_has_got_offset(unsigned int symndx) const
1003 return (this->local_got_offsets_.find(symndx)
1004 != this->local_got_offsets_.end());
1007 // Return the GOT offset of the local symbol SYMNDX.
1009 local_got_offset(unsigned int symndx) const
1011 Local_got_offsets::const_iterator p =
1012 this->local_got_offsets_.find(symndx);
1013 gold_assert(p != this->local_got_offsets_.end());
1017 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1019 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
1021 std::pair<Local_got_offsets::iterator, bool> ins =
1022 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
1023 gold_assert(ins.second);
1026 // Return whether the local TLS symbol SYMNDX has a GOT offset.
1027 // The GOT entry at this offset will contain a module index. If
1028 // NEED_PAIR is true, a second entry immediately following the first
1029 // will contain the dtv-relative offset.
1031 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
1033 typename Local_tls_got_offsets::const_iterator p =
1034 this->local_tls_got_offsets_.find(symndx);
1035 if (p == this->local_tls_got_offsets_.end()
1036 || (need_pair && !p->second.have_pair_))
1041 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1042 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1043 // otherwise we need the offset of the GOT entry for the module index.
1045 local_tls_got_offset(unsigned int symndx, bool need_pair) const
1047 typename Local_tls_got_offsets::const_iterator p =
1048 this->local_tls_got_offsets_.find(symndx);
1049 gold_assert(p != this->local_tls_got_offsets_.end());
1050 gold_assert(!need_pair || p->second.have_pair_);
1051 return p->second.got_offset_;
1054 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1055 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1056 // otherwise, we have just a single entry for the module index.
1058 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
1061 typename Local_tls_got_offsets::iterator p =
1062 this->local_tls_got_offsets_.find(symndx);
1063 if (p != this->local_tls_got_offsets_.end())
1065 // An entry already existed for this symbol. This can happen
1066 // if we see a relocation asking for the module index before
1067 // a relocation asking for the pair. In that case, the original
1068 // GOT entry will remain, but won't get used by any further
1070 p->second.got_offset_ = got_offset;
1071 gold_assert(have_pair);
1072 p->second.have_pair_ = true;
1076 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
1077 this->local_tls_got_offsets_.insert(
1078 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
1079 gold_assert(ins.second);
1083 // Return the name of the symbol that spans the given offset in the
1084 // specified section in this object. This is used only for error
1085 // messages and is not particularly efficient.
1087 get_symbol_location_info(unsigned int shndx, off_t offset,
1088 Symbol_location_info* info);
1091 // Read the symbols.
1093 do_read_symbols(Read_symbols_data*);
1095 // Return the number of local symbols.
1097 do_local_symbol_count() const
1098 { return this->local_symbol_count_; }
1100 // Lay out the input sections.
1102 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1104 // Add the symbols to the symbol table.
1106 do_add_symbols(Symbol_table*, Read_symbols_data*);
1110 do_read_relocs(Read_relocs_data*);
1112 // Scan the relocs and adjust the symbol table.
1114 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1117 // Count the local symbols.
1119 do_count_local_symbols(Stringpool_template<char>*,
1120 Stringpool_template<char>*);
1122 // Finalize the local symbols.
1124 do_finalize_local_symbols(unsigned int, off_t);
1126 // Set the offset where local dynamic symbol information will be stored.
1128 do_set_local_dynsym_indexes(unsigned int);
1130 // Set the offset where local dynamic symbol information will be stored.
1132 do_set_local_dynsym_offset(off_t);
1134 // Relocate the input sections and write out the local symbols.
1136 do_relocate(const General_options& options, const Symbol_table* symtab,
1137 const Layout*, Output_file* of);
1139 // Get the name of a section.
1141 do_section_name(unsigned int shndx)
1142 { return this->elf_file_.section_name(shndx); }
1144 // Return the location of the contents of a section.
1146 do_section_contents(unsigned int shndx)
1147 { return this->elf_file_.section_contents(shndx); }
1149 // Return section flags.
1151 do_section_flags(unsigned int shndx)
1152 { return this->elf_file_.section_flags(shndx); }
1154 // Return section type.
1156 do_section_type(unsigned int shndx)
1157 { return this->elf_file_.section_type(shndx); }
1159 // Return the section link field.
1161 do_section_link(unsigned int shndx)
1162 { return this->elf_file_.section_link(shndx); }
1164 // Return the section info field.
1166 do_section_info(unsigned int shndx)
1167 { return this->elf_file_.section_info(shndx); }
1171 typedef Sized_relobj<size, big_endian> This;
1172 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1173 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1174 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1175 typedef elfcpp::Shdr<size, big_endian> Shdr;
1177 // Find the SHT_SYMTAB section, given the section headers.
1179 find_symtab(const unsigned char* pshdrs);
1181 // Return whether SHDR has the right flags for a GNU style exception
1184 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1186 // Return whether there is a section named .eh_frame which might be
1187 // a GNU style exception frame section.
1189 find_eh_frame(const unsigned char* pshdrs, const char* names,
1190 section_size_type names_size) const;
1192 // Whether to include a section group in the link.
1194 include_section_group(Layout*, unsigned int,
1195 const elfcpp::Shdr<size, big_endian>&,
1196 std::vector<bool>*);
1198 // Whether to include a linkonce section in the link.
1200 include_linkonce_section(Layout*, const char*,
1201 const elfcpp::Shdr<size, big_endian>&);
1203 // Views and sizes when relocating.
1206 unsigned char* view;
1207 typename elfcpp::Elf_types<size>::Elf_Addr address;
1209 section_size_type view_size;
1210 bool is_input_output_view;
1211 bool is_postprocessing_view;
1214 typedef std::vector<View_size> Views;
1216 // Write section data to the output file. Record the views and
1217 // sizes in VIEWS for use when relocating.
1219 write_sections(const unsigned char* pshdrs, Output_file*, Views*);
1221 // Relocate the sections in the output file.
1223 relocate_sections(const General_options& options, const Symbol_table*,
1224 const Layout*, const unsigned char* pshdrs, Views*);
1226 // Initialize input to output maps for section symbols in merged
1229 initialize_input_to_output_maps();
1231 // Free the input to output maps for section symbols in merged
1234 free_input_to_output_maps();
1236 // Write out the local symbols.
1238 write_local_symbols(Output_file*,
1239 const Stringpool_template<char>*,
1240 const Stringpool_template<char>*);
1242 // Clear the local symbol information.
1244 clear_local_symbols()
1246 this->local_values_.clear();
1247 this->local_got_offsets_.clear();
1248 this->local_tls_got_offsets_.clear();
1251 // The GOT offsets of local symbols. This map also stores GOT offsets
1252 // for tp-relative offsets for TLS symbols.
1253 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1255 // The TLS GOT offsets of local symbols. The map stores the offsets
1256 // for either a single GOT entry that holds the module index of a TLS
1257 // symbol, or a pair of GOT entries containing the module index and
1258 // dtv-relative offset.
1259 struct Tls_got_entry
1261 Tls_got_entry(int got_offset, bool have_pair)
1262 : got_offset_(got_offset),
1263 have_pair_(have_pair)
1268 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1270 // General access to the ELF file.
1271 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1272 // Index of SHT_SYMTAB section.
1273 unsigned int symtab_shndx_;
1274 // The number of local symbols.
1275 unsigned int local_symbol_count_;
1276 // The number of local symbols which go into the output file.
1277 unsigned int output_local_symbol_count_;
1278 // The number of local symbols which go into the output file's dynamic
1280 unsigned int output_local_dynsym_count_;
1281 // The entries in the symbol table for the external symbols.
1283 // File offset for local symbols.
1284 off_t local_symbol_offset_;
1285 // File offset for local dynamic symbols.
1286 off_t local_dynsym_offset_;
1287 // Values of local symbols.
1288 Local_values local_values_;
1289 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1290 // for TLS symbols, indexed by symbol number.
1291 Local_got_offsets local_got_offsets_;
1292 // GOT offsets for local TLS symbols, indexed by symbol number
1293 // and GOT entry type.
1294 Local_tls_got_offsets local_tls_got_offsets_;
1295 // Whether this object has a GNU style .eh_frame section.
1299 // A class to manage the list of all objects.
1305 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1306 system_library_directory_()
1309 // The type of the list of input relocateable objects.
1310 typedef std::vector<Relobj*> Relobj_list;
1311 typedef Relobj_list::const_iterator Relobj_iterator;
1313 // The type of the list of input dynamic objects.
1314 typedef std::vector<Dynobj*> Dynobj_list;
1315 typedef Dynobj_list::const_iterator Dynobj_iterator;
1317 // Add an object to the list. Return true if all is well, or false
1318 // if this object should be ignored.
1320 add_object(Object*);
1322 // Get the target we should use for the output file.
1325 { return this->target_; }
1327 // For each dynamic object, check whether we've seen all of its
1328 // explicit dependencies.
1330 check_dynamic_dependencies() const;
1332 // Return whether an object was found in the system library
1335 found_in_system_library_directory(const Object*) const;
1337 // Iterate over all regular objects.
1340 relobj_begin() const
1341 { return this->relobj_list_.begin(); }
1345 { return this->relobj_list_.end(); }
1347 // Iterate over all dynamic objects.
1350 dynobj_begin() const
1351 { return this->dynobj_list_.begin(); }
1355 { return this->dynobj_list_.end(); }
1357 // Return whether we have seen any dynamic objects.
1360 { return !this->dynobj_list_.empty(); }
1362 // Return the number of input objects.
1364 number_of_input_objects() const
1365 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1368 Input_objects(const Input_objects&);
1369 Input_objects& operator=(const Input_objects&);
1371 // The list of ordinary objects included in the link.
1372 Relobj_list relobj_list_;
1373 // The list of dynamic objects included in the link.
1374 Dynobj_list dynobj_list_;
1377 // SONAMEs that we have seen.
1378 Unordered_set<std::string> sonames_;
1379 // The directory in which we find the libc.so.
1380 std::string system_library_directory_;
1383 // Some of the information we pass to the relocation routines. We
1384 // group this together to avoid passing a dozen different arguments.
1386 template<int size, bool big_endian>
1387 struct Relocate_info
1389 // Command line options.
1390 const General_options* options;
1392 const Symbol_table* symtab;
1394 const Layout* layout;
1395 // Object being relocated.
1396 Sized_relobj<size, big_endian>* object;
1397 // Section index of relocation section.
1398 unsigned int reloc_shndx;
1399 // Section index of section being relocated.
1400 unsigned int data_shndx;
1402 // Return a string showing the location of a relocation. This is
1403 // only used for error messages.
1405 location(size_t relnum, off_t reloffset) const;
1408 // Return an Object appropriate for the input file. P is BYTES long,
1409 // and holds the ELF header.
1412 make_elf_object(const std::string& name, Input_file*,
1413 off_t offset, const unsigned char* p,
1414 section_offset_type bytes);
1416 } // end namespace gold
1418 #endif // !defined(GOLD_OBJECT_H)