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 POD class is holds the value of a symbol. This is used for
626 // local symbols, and for all symbols during relocation processing.
627 // For special sections, such as SHF_MERGE sections, this calls a
628 // function to get the final symbol value.
634 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
637 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
638 is_section_symbol_(false), is_tls_symbol_(false),
639 needs_output_address_(false), value_(0)
642 // Get the value of this symbol. OBJECT is the object in which this
643 // symbol is defined, and ADDEND is an addend to add to the value.
644 template<bool big_endian>
646 value(const Sized_relobj<size, big_endian>* object, Value addend) const
648 if (!this->needs_output_address_)
649 return this->value_ + addend;
650 return object->local_value(this->input_shndx_, this->value_,
651 this->is_section_symbol_, addend);
654 // Set the value of this symbol in the output symbol table.
656 set_output_value(Value value)
658 this->value_ = value;
659 this->needs_output_address_ = false;
662 // Set the value of the symbol from the input file. This value
663 // will usually be replaced during finalization with the output
664 // value, but if the symbol is mapped to an output section which
665 // requires special handling to determine the output value, we
666 // leave the input value in place until later. This is used for
667 // SHF_MERGE sections.
669 set_input_value(Value value)
671 this->value_ = value;
672 this->needs_output_address_ = true;
675 // Return the input value.
679 gold_assert(this->needs_output_address_);
683 // Return whether this symbol should go into the output symbol
686 needs_output_symtab_entry() const
687 { return this->output_symtab_index_ != -1U; }
689 // Return the index in the output symbol table.
691 output_symtab_index() const
693 gold_assert(this->output_symtab_index_ != 0);
694 return this->output_symtab_index_;
697 // Set the index in the output symbol table.
699 set_output_symtab_index(unsigned int i)
701 gold_assert(this->output_symtab_index_ == 0);
702 this->output_symtab_index_ = i;
705 // Record that this symbol should not go into the output symbol
708 set_no_output_symtab_entry()
710 gold_assert(this->output_symtab_index_ == 0);
711 this->output_symtab_index_ = -1U;
714 // Set the index in the output dynamic symbol table.
716 set_needs_output_dynsym_entry()
718 this->output_dynsym_index_ = 0;
721 // Return whether this symbol should go into the output symbol
724 needs_output_dynsym_entry() const
726 return this->output_dynsym_index_ != -1U;
729 // Record that this symbol should go into the dynamic symbol table.
731 set_output_dynsym_index(unsigned int i)
733 gold_assert(this->output_dynsym_index_ == 0);
734 this->output_dynsym_index_ = i;
737 // Return the index in the output dynamic symbol table.
739 output_dynsym_index() const
741 gold_assert(this->output_dynsym_index_ != 0);
742 return this->output_dynsym_index_;
745 // Set the index of the input section in the input file.
747 set_input_shndx(unsigned int i)
749 this->input_shndx_ = i;
750 // input_shndx_ field is a bitfield, so make sure that the value
752 gold_assert(this->input_shndx_ == i);
755 // Return the index of the input section in the input file.
758 { return this->input_shndx_; }
760 // Record that this is a section symbol.
762 set_is_section_symbol()
763 { this->is_section_symbol_ = true; }
765 // Record that this is a TLS symbol.
768 { this->is_tls_symbol_ = true; }
770 // Return TRUE if this is a TLS symbol.
772 is_tls_symbol() const
773 { return this->is_tls_symbol_; }
776 // The index of this local symbol in the output symbol table. This
777 // will be -1 if the symbol should not go into the symbol table.
778 unsigned int output_symtab_index_;
779 // The index of this local symbol in the dynamic symbol table. This
780 // will be -1 if the symbol should not go into the symbol table.
781 unsigned int output_dynsym_index_;
782 // The section index in the input file in which this symbol is
784 unsigned int input_shndx_ : 29;
785 // Whether this is a STT_SECTION symbol.
786 bool is_section_symbol_ : 1;
787 // Whether this is a STT_TLS symbol.
788 bool is_tls_symbol_ : 1;
789 // Whether getting the value of this symbol requires calling an
790 // Output_section method. For example, this will be true of a
791 // symbol in a SHF_MERGE section.
792 bool needs_output_address_ : 1;
793 // The value of the symbol. If !needs_output_address_, this is the
794 // value in the output file. If needs_output_address_, this is the
795 // value in the input file.
799 // A regular object file. This is size and endian specific.
801 template<int size, bool big_endian>
802 class Sized_relobj : public Relobj
805 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
806 typedef std::vector<Symbol*> Symbols;
807 typedef std::vector<Symbol_value<size> > Local_values;
809 Sized_relobj(const std::string& name, Input_file* input_file, off_t offset,
810 const typename elfcpp::Ehdr<size, big_endian>&);
814 // Set up the object file based on the ELF header.
816 setup(const typename elfcpp::Ehdr<size, big_endian>&);
818 // If SYM is the index of a global symbol in the object file's
819 // symbol table, return the Symbol object. Otherwise, return NULL.
821 global_symbol(unsigned int sym) const
823 if (sym >= this->local_symbol_count_)
825 gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
826 return this->symbols_[sym - this->local_symbol_count_];
831 // Return the section index of symbol SYM. Set *VALUE to its value
832 // in the object file. Note that for a symbol which is not defined
833 // in this object file, this will set *VALUE to 0 and return
834 // SHN_UNDEF; it will not return the final value of the symbol in
837 symbol_section_and_value(unsigned int sym, Address* value);
839 // Return a pointer to the Symbol_value structure which holds the
840 // value of a local symbol.
841 const Symbol_value<size>*
842 local_symbol(unsigned int sym) const
844 gold_assert(sym < this->local_values_.size());
845 return &this->local_values_[sym];
848 // Return the index of local symbol SYM in the ordinary symbol
849 // table. A value of -1U means that the symbol is not being output.
851 symtab_index(unsigned int sym) const
853 gold_assert(sym < this->local_values_.size());
854 return this->local_values_[sym].output_symtab_index();
857 // Return the index of local symbol SYM in the dynamic symbol
858 // table. A value of -1U means that the symbol is not being output.
860 dynsym_index(unsigned int sym) const
862 gold_assert(sym < this->local_values_.size());
863 return this->local_values_[sym].output_dynsym_index();
866 // Return the appropriate Sized_target structure.
867 Sized_target<size, big_endian>*
870 return this->Object::sized_target
871 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
872 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
875 // Return the value of the local symbol symndx.
877 local_symbol_value(unsigned int symndx) const;
879 // Return the value of a local symbol defined in input section
880 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
881 // indicates whether the symbol is a section symbol. This handles
882 // SHF_MERGE sections.
884 local_value(unsigned int shndx, Address value, bool is_section_symbol,
885 Address addend) const;
888 set_needs_output_dynsym_entry(unsigned int sym)
890 gold_assert(sym < this->local_values_.size());
891 this->local_values_[sym].set_needs_output_dynsym_entry();
894 // Return whether the local symbol SYMNDX has a GOT offset.
895 // For TLS symbols, the GOT entry will hold its tp-relative offset.
897 local_has_got_offset(unsigned int symndx) const
899 return (this->local_got_offsets_.find(symndx)
900 != this->local_got_offsets_.end());
903 // Return the GOT offset of the local symbol SYMNDX.
905 local_got_offset(unsigned int symndx) const
907 Local_got_offsets::const_iterator p =
908 this->local_got_offsets_.find(symndx);
909 gold_assert(p != this->local_got_offsets_.end());
913 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
915 set_local_got_offset(unsigned int symndx, unsigned int got_offset)
917 std::pair<Local_got_offsets::iterator, bool> ins =
918 this->local_got_offsets_.insert(std::make_pair(symndx, got_offset));
919 gold_assert(ins.second);
922 // Return whether the local TLS symbol SYMNDX has a GOT offset.
923 // The GOT entry at this offset will contain a module index. If
924 // NEED_PAIR is true, a second entry immediately following the first
925 // will contain the dtv-relative offset.
927 local_has_tls_got_offset(unsigned int symndx, bool need_pair) const
929 typename Local_tls_got_offsets::const_iterator p =
930 this->local_tls_got_offsets_.find(symndx);
931 if (p == this->local_tls_got_offsets_.end()
932 || (need_pair && !p->second.have_pair_))
937 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
938 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
939 // otherwise we need the offset of the GOT entry for the module index.
941 local_tls_got_offset(unsigned int symndx, bool need_pair) const
943 typename Local_tls_got_offsets::const_iterator p =
944 this->local_tls_got_offsets_.find(symndx);
945 gold_assert(p != this->local_tls_got_offsets_.end());
946 gold_assert(!need_pair || p->second.have_pair_);
947 return p->second.got_offset_;
950 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
951 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
952 // otherwise, we have just a single entry for the module index.
954 set_local_tls_got_offset(unsigned int symndx, unsigned int got_offset,
957 typename Local_tls_got_offsets::iterator p =
958 this->local_tls_got_offsets_.find(symndx);
959 if (p != this->local_tls_got_offsets_.end())
961 // An entry already existed for this symbol. This can happen
962 // if we see a relocation asking for the module index before
963 // a relocation asking for the pair. In that case, the original
964 // GOT entry will remain, but won't get used by any further
966 p->second.got_offset_ = got_offset;
967 gold_assert(have_pair);
968 p->second.have_pair_ = true;
972 std::pair<typename Local_tls_got_offsets::iterator, bool> ins =
973 this->local_tls_got_offsets_.insert(
974 std::make_pair(symndx, Tls_got_entry(got_offset, have_pair)));
975 gold_assert(ins.second);
979 // Return the name of the symbol that spans the given offset in the
980 // specified section in this object. This is used only for error
981 // messages and is not particularly efficient.
983 get_symbol_location_info(unsigned int shndx, off_t offset,
984 Symbol_location_info* info);
989 do_read_symbols(Read_symbols_data*);
991 // Return the number of local symbols.
993 do_local_symbol_count() const
994 { return this->local_symbol_count_; }
996 // Lay out the input sections.
998 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1000 // Add the symbols to the symbol table.
1002 do_add_symbols(Symbol_table*, Read_symbols_data*);
1006 do_read_relocs(Read_relocs_data*);
1008 // Scan the relocs and adjust the symbol table.
1010 do_scan_relocs(const General_options&, Symbol_table*, Layout*,
1013 // Count the local symbols.
1015 do_count_local_symbols(Stringpool_template<char>*,
1016 Stringpool_template<char>*);
1018 // Finalize the local symbols.
1020 do_finalize_local_symbols(unsigned int, off_t);
1022 // Set the offset where local dynamic symbol information will be stored.
1024 do_set_local_dynsym_indexes(unsigned int);
1026 // Set the offset where local dynamic symbol information will be stored.
1028 do_set_local_dynsym_offset(off_t);
1030 // Relocate the input sections and write out the local symbols.
1032 do_relocate(const General_options& options, const Symbol_table* symtab,
1033 const Layout*, Output_file* of);
1035 // Get the name of a section.
1037 do_section_name(unsigned int shndx)
1038 { return this->elf_file_.section_name(shndx); }
1040 // Return the location of the contents of a section.
1042 do_section_contents(unsigned int shndx)
1043 { return this->elf_file_.section_contents(shndx); }
1045 // Return section flags.
1047 do_section_flags(unsigned int shndx)
1048 { return this->elf_file_.section_flags(shndx); }
1050 // Return section type.
1052 do_section_type(unsigned int shndx)
1053 { return this->elf_file_.section_type(shndx); }
1055 // Return the section link field.
1057 do_section_link(unsigned int shndx)
1058 { return this->elf_file_.section_link(shndx); }
1060 // Return the section info field.
1062 do_section_info(unsigned int shndx)
1063 { return this->elf_file_.section_info(shndx); }
1067 typedef Sized_relobj<size, big_endian> This;
1068 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
1069 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1070 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1071 typedef elfcpp::Shdr<size, big_endian> Shdr;
1073 // Find the SHT_SYMTAB section, given the section headers.
1075 find_symtab(const unsigned char* pshdrs);
1077 // Return whether SHDR has the right flags for a GNU style exception
1080 check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
1082 // Return whether there is a section named .eh_frame which might be
1083 // a GNU style exception frame section.
1085 find_eh_frame(const unsigned char* pshdrs, const char* names,
1086 section_size_type names_size) const;
1088 // Whether to include a section group in the link.
1090 include_section_group(Layout*, unsigned int,
1091 const elfcpp::Shdr<size, big_endian>&,
1092 std::vector<bool>*);
1094 // Whether to include a linkonce section in the link.
1096 include_linkonce_section(Layout*, const char*,
1097 const elfcpp::Shdr<size, big_endian>&);
1099 // Views and sizes when relocating.
1102 unsigned char* view;
1103 typename elfcpp::Elf_types<size>::Elf_Addr address;
1105 section_size_type view_size;
1106 bool is_input_output_view;
1107 bool is_postprocessing_view;
1110 typedef std::vector<View_size> Views;
1112 // Write section data to the output file. Record the views and
1113 // sizes in VIEWS for use when relocating.
1115 write_sections(const unsigned char* pshdrs, Output_file*, Views*) const;
1117 // Relocate the sections in the output file.
1119 relocate_sections(const General_options& options, const Symbol_table*,
1120 const Layout*, const unsigned char* pshdrs, Views*);
1122 // Write out the local symbols.
1124 write_local_symbols(Output_file*,
1125 const Stringpool_template<char>*,
1126 const Stringpool_template<char>*);
1128 // The GOT offsets of local symbols. This map also stores GOT offsets
1129 // for tp-relative offsets for TLS symbols.
1130 typedef Unordered_map<unsigned int, unsigned int> Local_got_offsets;
1132 // The TLS GOT offsets of local symbols. The map stores the offsets
1133 // for either a single GOT entry that holds the module index of a TLS
1134 // symbol, or a pair of GOT entries containing the module index and
1135 // dtv-relative offset.
1136 struct Tls_got_entry
1138 Tls_got_entry(int got_offset, bool have_pair)
1139 : got_offset_(got_offset),
1140 have_pair_(have_pair)
1145 typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
1147 // General access to the ELF file.
1148 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
1149 // Index of SHT_SYMTAB section.
1150 unsigned int symtab_shndx_;
1151 // The number of local symbols.
1152 unsigned int local_symbol_count_;
1153 // The number of local symbols which go into the output file.
1154 unsigned int output_local_symbol_count_;
1155 // The number of local symbols which go into the output file's dynamic
1157 unsigned int output_local_dynsym_count_;
1158 // The entries in the symbol table for the external symbols.
1160 // File offset for local symbols.
1161 off_t local_symbol_offset_;
1162 // File offset for local dynamic symbols.
1163 off_t local_dynsym_offset_;
1164 // Values of local symbols.
1165 Local_values local_values_;
1166 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1167 // for TLS symbols, indexed by symbol number.
1168 Local_got_offsets local_got_offsets_;
1169 // GOT offsets for local TLS symbols, indexed by symbol number
1170 // and GOT entry type.
1171 Local_tls_got_offsets local_tls_got_offsets_;
1172 // Whether this object has a GNU style .eh_frame section.
1176 // A class to manage the list of all objects.
1182 : relobj_list_(), dynobj_list_(), target_(NULL), sonames_(),
1183 system_library_directory_()
1186 // The type of the list of input relocateable objects.
1187 typedef std::vector<Relobj*> Relobj_list;
1188 typedef Relobj_list::const_iterator Relobj_iterator;
1190 // The type of the list of input dynamic objects.
1191 typedef std::vector<Dynobj*> Dynobj_list;
1192 typedef Dynobj_list::const_iterator Dynobj_iterator;
1194 // Add an object to the list. Return true if all is well, or false
1195 // if this object should be ignored.
1197 add_object(Object*);
1199 // Get the target we should use for the output file.
1202 { return this->target_; }
1204 // For each dynamic object, check whether we've seen all of its
1205 // explicit dependencies.
1207 check_dynamic_dependencies() const;
1209 // Return whether an object was found in the system library
1212 found_in_system_library_directory(const Object*) const;
1214 // Iterate over all regular objects.
1217 relobj_begin() const
1218 { return this->relobj_list_.begin(); }
1222 { return this->relobj_list_.end(); }
1224 // Iterate over all dynamic objects.
1227 dynobj_begin() const
1228 { return this->dynobj_list_.begin(); }
1232 { return this->dynobj_list_.end(); }
1234 // Return whether we have seen any dynamic objects.
1237 { return !this->dynobj_list_.empty(); }
1239 // Return the number of input objects.
1241 number_of_input_objects() const
1242 { return this->relobj_list_.size() + this->dynobj_list_.size(); }
1245 Input_objects(const Input_objects&);
1246 Input_objects& operator=(const Input_objects&);
1248 // The list of ordinary objects included in the link.
1249 Relobj_list relobj_list_;
1250 // The list of dynamic objects included in the link.
1251 Dynobj_list dynobj_list_;
1254 // SONAMEs that we have seen.
1255 Unordered_set<std::string> sonames_;
1256 // The directory in which we find the libc.so.
1257 std::string system_library_directory_;
1260 // Some of the information we pass to the relocation routines. We
1261 // group this together to avoid passing a dozen different arguments.
1263 template<int size, bool big_endian>
1264 struct Relocate_info
1266 // Command line options.
1267 const General_options* options;
1269 const Symbol_table* symtab;
1271 const Layout* layout;
1272 // Object being relocated.
1273 Sized_relobj<size, big_endian>* object;
1274 // Section index of relocation section.
1275 unsigned int reloc_shndx;
1276 // Section index of section being relocated.
1277 unsigned int data_shndx;
1279 // Return a string showing the location of a relocation. This is
1280 // only used for error messages.
1282 location(size_t relnum, off_t reloffset) const;
1285 // Return an Object appropriate for the input file. P is BYTES long,
1286 // and holds the ELF header.
1289 make_elf_object(const std::string& name, Input_file*,
1290 off_t offset, const unsigned char* p,
1291 section_offset_type bytes);
1293 } // end namespace gold
1295 #endif // !defined(GOLD_OBJECT_H)