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
;
42 class Object_merge_map
;
44 template<typename Stringpool_char
>
45 class Stringpool_template
;
47 // Data to pass from read_symbols() to add_symbols().
49 struct Read_symbols_data
52 File_view
* section_headers
;
54 File_view
* section_names
;
55 // Size of section name data in bytes.
56 off_t section_names_size
;
59 // Size of symbol data in bytes.
61 // Offset of external symbols within symbol data. This structure
62 // sometimes contains only external symbols, in which case this will
63 // be zero. Sometimes it contains all symbols.
64 off_t external_symbols_offset
;
66 File_view
* symbol_names
;
67 // Size of symbol name data in bytes.
68 off_t symbol_names_size
;
70 // Version information. This is only used on dynamic objects.
71 // Version symbol data (from SHT_GNU_versym section).
74 // Version definition data (from SHT_GNU_verdef section).
77 unsigned int verdef_info
;
78 // Needed version data (from SHT_GNU_verneed section).
81 unsigned int verneed_info
;
84 // Information used to print error messages.
86 struct Symbol_location_info
88 std::string source_file
;
89 std::string enclosing_symbol_name
;
93 // Data about a single relocation section. This is read in
94 // read_relocs and processed in scan_relocs.
98 // Index of reloc section.
99 unsigned int reloc_shndx
;
100 // Index of section that relocs apply to.
101 unsigned int data_shndx
;
102 // Contents of reloc section.
104 // Reloc section type.
105 unsigned int sh_type
;
106 // Number of reloc entries.
109 Output_section
* output_section
;
110 // Whether this section has special handling for offsets.
111 bool needs_special_offset_handling
;
114 // Relocations in an object file. This is read in read_relocs and
115 // processed in scan_relocs.
117 struct Read_relocs_data
119 typedef std::vector
<Section_relocs
> Relocs_list
;
122 // The local symbols.
123 File_view
* local_symbols
;
126 // Object is an abstract base class which represents either a 32-bit
127 // or a 64-bit input object. This can be a regular object file
128 // (ET_REL) or a shared object (ET_DYN).
133 // NAME is the name of the object as we would report it to the user
134 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
135 // used to read the file. OFFSET is the offset within the input
136 // file--0 for a .o or .so file, something else for a .a file.
137 Object(const std::string
& name
, Input_file
* input_file
, bool is_dynamic
,
139 : name_(name
), input_file_(input_file
), offset_(offset
), shnum_(-1U),
140 is_dynamic_(is_dynamic
), target_(NULL
)
146 // Return the name of the object as we would report it to the tuser.
149 { return this->name_
; }
151 // Get the offset into the file.
154 { return this->offset_
; }
156 // Return whether this is a dynamic object.
159 { return this->is_dynamic_
; }
161 // Return the target structure associated with this object.
164 { return this->target_
; }
166 // Lock the underlying file.
169 { this->input_file_
->file().lock(); }
171 // Unlock the underlying file.
174 { this->input_file_
->file().unlock(); }
176 // Return whether the underlying file is locked.
179 { return this->input_file_
->file().is_locked(); }
181 // Return the sized target structure associated with this object.
182 // This is like the target method but it returns a pointer of
183 // appropriate checked type.
184 template<int size
, bool big_endian
>
185 Sized_target
<size
, big_endian
>*
186 sized_target(ACCEPT_SIZE_ENDIAN_ONLY
);
188 // Get the number of sections.
191 { return this->shnum_
; }
193 // Return a view of the contents of a section. Set *PLEN to the
194 // size. CACHE is a hint as in File_read::get_view.
196 section_contents(unsigned int shndx
, off_t
* plen
, bool cache
);
198 // Return the name of a section given a section index. This is only
199 // used for error messages.
201 section_name(unsigned int shndx
)
202 { return this->do_section_name(shndx
); }
204 // Return the section flags given a section index.
206 section_flags(unsigned int shndx
)
207 { return this->do_section_flags(shndx
); }
209 // Return the section type given a section index.
211 section_type(unsigned int shndx
)
212 { return this->do_section_type(shndx
); }
214 // Return the section link field given a section index.
216 section_link(unsigned int shndx
)
217 { return this->do_section_link(shndx
); }
219 // Return the section info field given a section index.
221 section_info(unsigned int shndx
)
222 { return this->do_section_info(shndx
); }
224 // Read the symbol information.
226 read_symbols(Read_symbols_data
* sd
)
227 { return this->do_read_symbols(sd
); }
229 // Pass sections which should be included in the link to the Layout
230 // object, and record where the sections go in the output file.
232 layout(Symbol_table
* symtab
, Layout
* layout
, Read_symbols_data
* sd
)
233 { this->do_layout(symtab
, layout
, sd
); }
235 // Add symbol information to the global symbol table.
237 add_symbols(Symbol_table
* symtab
, Read_symbols_data
* sd
)
238 { this->do_add_symbols(symtab
, sd
); }
240 // Functions and types for the elfcpp::Elf_file interface. This
241 // permit us to use Object as the File template parameter for
244 // The View class is returned by view. It must support a single
245 // method, data(). This is trivial, because get_view does what we
250 View(const unsigned char* p
)
259 const unsigned char* p_
;
264 view(off_t file_offset
, off_t data_size
)
265 { return View(this->get_view(file_offset
, data_size
, true)); }
269 error(const char* format
, ...) const ATTRIBUTE_PRINTF_2
;
271 // A location in the file.
277 Location(off_t fo
, off_t ds
)
278 : file_offset(fo
), data_size(ds
)
282 // Get a View given a Location.
283 View
view(Location loc
)
284 { return View(this->get_view(loc
.file_offset
, loc
.data_size
, true)); }
287 // Read the symbols--implemented by child class.
289 do_read_symbols(Read_symbols_data
*) = 0;
291 // Lay out sections--implemented by child class.
293 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*) = 0;
295 // Add symbol information to the global symbol table--implemented by
298 do_add_symbols(Symbol_table
*, Read_symbols_data
*) = 0;
300 // Return the location of the contents of a section. Implemented by
303 do_section_contents(unsigned int shndx
) = 0;
305 // Get the name of a section--implemented by child class.
307 do_section_name(unsigned int shndx
) = 0;
309 // Get section flags--implemented by child class.
311 do_section_flags(unsigned int shndx
) = 0;
313 // Get section type--implemented by child class.
315 do_section_type(unsigned int shndx
) = 0;
317 // Get section link field--implemented by child class.
319 do_section_link(unsigned int shndx
) = 0;
321 // Get section info field--implemented by child class.
323 do_section_info(unsigned int shndx
) = 0;
328 { return this->input_file_
; }
330 // Get a view into the underlying file.
332 get_view(off_t start
, off_t size
, bool cache
)
334 return this->input_file_
->file().get_view(start
+ this->offset_
, size
,
338 // Get a lasting view into the underlying file.
340 get_lasting_view(off_t start
, off_t size
, bool cache
)
342 return this->input_file_
->file().get_lasting_view(start
+ this->offset_
,
346 // Read data from the underlying file.
348 read(off_t start
, off_t size
, void* p
)
349 { this->input_file_
->file().read(start
+ this->offset_
, size
, p
); }
353 set_target(int machine
, int size
, bool big_endian
, int osabi
,
356 // Set the number of sections.
359 { this->shnum_
= shnum
; }
361 // Functions used by both Sized_relobj and Sized_dynobj.
363 // Read the section data into a Read_symbols_data object.
364 template<int size
, bool big_endian
>
366 read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>*,
369 // If NAME is the name of a special .gnu.warning section, arrange
370 // for the warning to be issued. SHNDX is the section index.
371 // Return whether it is a warning section.
373 handle_gnu_warning_section(const char* name
, unsigned int shndx
,
377 // This class may not be copied.
378 Object(const Object
&);
379 Object
& operator=(const Object
&);
381 // Name of object as printed to user.
383 // For reading the file.
384 Input_file
* input_file_
;
385 // Offset within the file--0 for an object file, non-0 for an
388 // Number of input sections.
390 // Whether this is a dynamic object.
392 // Target functions--may be NULL if the target is not known.
396 // Implement sized_target inline for efficiency. This approach breaks
397 // static type checking, but is made safe using asserts.
399 template<int size
, bool big_endian
>
400 inline Sized_target
<size
, big_endian
>*
401 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY
)
403 gold_assert(this->target_
->get_size() == size
);
404 gold_assert(this->target_
->is_big_endian() ? big_endian
: !big_endian
);
405 return static_cast<Sized_target
<size
, big_endian
>*>(this->target_
);
408 // A regular object (ET_REL). This is an abstract base class itself.
409 // The implementation is the template class Sized_relobj.
411 class Relobj
: public Object
414 Relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
= 0)
415 : Object(name
, input_file
, false, offset
),
417 object_merge_map_(NULL
),
418 relocs_must_follow_section_writes_(false)
423 read_relocs(Read_relocs_data
* rd
)
424 { return this->do_read_relocs(rd
); }
426 // Scan the relocs and adjust the symbol table.
428 scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
429 Layout
* layout
, Read_relocs_data
* rd
)
430 { return this->do_scan_relocs(options
, symtab
, layout
, rd
); }
432 // Initial local symbol processing: count the number of local symbols
433 // in the output symbol table and dynamic symbol table; add local symbol
434 // names to *POOL and *DYNPOOL.
436 count_local_symbols(Stringpool_template
<char>* pool
,
437 Stringpool_template
<char>* dynpool
)
438 { return this->do_count_local_symbols(pool
, dynpool
); }
440 // Set the values of the local symbols, set the output symbol table
441 // indexes for the local variables, and set the offset where local
442 // symbol information will be stored. Returns the new local symbol index.
444 finalize_local_symbols(unsigned int index
, off_t off
)
445 { return this->do_finalize_local_symbols(index
, off
); }
447 // Set the output dynamic symbol table indexes for the local variables.
449 set_local_dynsym_indexes(unsigned int index
)
450 { return this->do_set_local_dynsym_indexes(index
); }
452 // Set the offset where local dynamic symbol information will be stored.
454 set_local_dynsym_offset(off_t off
)
455 { return this->do_set_local_dynsym_offset(off
); }
457 // Relocate the input sections and write out the local symbols.
459 relocate(const General_options
& options
, const Symbol_table
* symtab
,
460 const Layout
* layout
, Output_file
* of
)
461 { return this->do_relocate(options
, symtab
, layout
, of
); }
463 // Return whether an input section is being included in the link.
465 is_section_included(unsigned int shndx
) const
467 gold_assert(shndx
< this->map_to_output_
.size());
468 return this->map_to_output_
[shndx
].output_section
!= NULL
;
471 // Return whether an input section requires special
472 // handling--whether it is not simply mapped from the input file to
475 is_section_specially_mapped(unsigned int shndx
) const
477 gold_assert(shndx
< this->map_to_output_
.size());
478 return (this->map_to_output_
[shndx
].output_section
!= NULL
479 && this->map_to_output_
[shndx
].offset
== -1);
482 // Given a section index, return the corresponding Output_section
483 // (which will be NULL if the section is not included in the link)
484 // and set *POFF to the offset within that section. *POFF will be
485 // set to -1 if the section requires special handling.
486 inline Output_section
*
487 output_section(unsigned int shndx
, off_t
* poff
) const;
489 // Set the offset of an input section within its output section.
491 set_section_offset(unsigned int shndx
, off_t off
)
493 gold_assert(shndx
< this->map_to_output_
.size());
494 this->map_to_output_
[shndx
].offset
= off
;
497 // Return true if we need to wait for output sections to be written
498 // before we can apply relocations. This is true if the object has
499 // any relocations for sections which require special handling, such
500 // as the exception frame section.
502 relocs_must_follow_section_writes()
503 { return this->relocs_must_follow_section_writes_
; }
505 // Return the object merge map.
508 { return this->object_merge_map_
; }
510 // Set the object merge map.
512 set_merge_map(Object_merge_map
* object_merge_map
)
514 gold_assert(this->object_merge_map_
== NULL
);
515 this->object_merge_map_
= object_merge_map
;
519 // What we need to know to map an input section to an output
520 // section. We keep an array of these, one for each input section,
521 // indexed by the input section number.
524 // The output section. This is NULL if the input section is to be
526 Output_section
* output_section
;
527 // The offset within the output section. This is -1 if the
528 // section requires special handling.
532 // Read the relocs--implemented by child class.
534 do_read_relocs(Read_relocs_data
*) = 0;
536 // Scan the relocs--implemented by child class.
538 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
539 Read_relocs_data
*) = 0;
541 // Count local symbols--implemented by child class.
543 do_count_local_symbols(Stringpool_template
<char>*,
544 Stringpool_template
<char>*) = 0;
546 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
547 // offset where local symbol information will be stored.
549 do_finalize_local_symbols(unsigned int, off_t
) = 0;
551 // Set the output dynamic symbol table indexes for the local variables.
553 do_set_local_dynsym_indexes(unsigned int) = 0;
555 // Set the offset where local dynamic symbol information will be stored.
557 do_set_local_dynsym_offset(off_t
) = 0;
559 // Relocate the input sections and write out the local
560 // symbols--implemented by child class.
562 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
563 const Layout
*, Output_file
* of
) = 0;
565 // Return the vector mapping input sections to output sections.
566 std::vector
<Map_to_output
>&
568 { return this->map_to_output_
; }
570 const std::vector
<Map_to_output
>&
571 map_to_output() const
572 { return this->map_to_output_
; }
574 // Record that we must wait for the output sections to be written
575 // before applying relocations.
577 set_relocs_must_follow_section_writes()
578 { this->relocs_must_follow_section_writes_
= true; }
581 // Mapping from input sections to output section.
582 std::vector
<Map_to_output
> map_to_output_
;
583 // Mappings for merge sections. This is managed by the code in the
585 Object_merge_map
* object_merge_map_
;
586 // Whether we need to wait for output sections to be written before
587 // we can apply relocations.
588 bool relocs_must_follow_section_writes_
;
591 // Implement Object::output_section inline for efficiency.
592 inline Output_section
*
593 Relobj::output_section(unsigned int shndx
, off_t
* poff
) const
595 gold_assert(shndx
< this->map_to_output_
.size());
596 const Map_to_output
& mo(this->map_to_output_
[shndx
]);
598 return mo
.output_section
;
601 // This POD class is holds the value of a symbol. This is used for
602 // local symbols, and for all symbols during relocation processing.
603 // For special sections, such as SHF_MERGE sections, this calls a
604 // function to get the final symbol value.
610 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value
;
613 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
614 is_section_symbol_(false), is_tls_symbol_(false),
615 needs_output_address_(false), value_(0)
618 // Get the value of this symbol. OBJECT is the object in which this
619 // symbol is defined, and ADDEND is an addend to add to the value.
620 template<bool big_endian
>
622 value(const Sized_relobj
<size
, big_endian
>* object
, Value addend
) const
624 if (!this->needs_output_address_
)
625 return this->value_
+ addend
;
626 return object
->local_value(this->input_shndx_
, this->value_
,
627 this->is_section_symbol_
, addend
);
630 // Set the value of this symbol in the output symbol table.
632 set_output_value(Value value
)
634 this->value_
= value
;
635 this->needs_output_address_
= false;
638 // Set the value of the symbol from the input file. This value
639 // will usually be replaced during finalization with the output
640 // value, but if the symbol is mapped to an output section which
641 // requires special handling to determine the output value, we
642 // leave the input value in place until later. This is used for
643 // SHF_MERGE sections.
645 set_input_value(Value value
)
647 this->value_
= value
;
648 this->needs_output_address_
= true;
651 // Return the input value.
655 gold_assert(this->needs_output_address_
);
659 // Return whether this symbol should go into the output symbol
662 needs_output_symtab_entry() const
663 { return this->output_symtab_index_
!= -1U; }
665 // Return the index in the output symbol table.
667 output_symtab_index() const
669 gold_assert(this->output_symtab_index_
!= 0);
670 return this->output_symtab_index_
;
673 // Set the index in the output symbol table.
675 set_output_symtab_index(unsigned int i
)
677 gold_assert(this->output_symtab_index_
== 0);
678 this->output_symtab_index_
= i
;
681 // Record that this symbol should not go into the output symbol
684 set_no_output_symtab_entry()
686 gold_assert(this->output_symtab_index_
== 0);
687 this->output_symtab_index_
= -1U;
690 // Set the index in the output dynamic symbol table.
692 set_needs_output_dynsym_entry()
694 this->output_dynsym_index_
= 0;
697 // Return whether this symbol should go into the output symbol
700 needs_output_dynsym_entry() const
702 return this->output_dynsym_index_
!= -1U;
705 // Record that this symbol should go into the dynamic symbol table.
707 set_output_dynsym_index(unsigned int i
)
709 gold_assert(this->output_dynsym_index_
== 0);
710 this->output_dynsym_index_
= i
;
713 // Return the index in the output dynamic symbol table.
715 output_dynsym_index() const
717 gold_assert(this->output_dynsym_index_
!= 0);
718 return this->output_dynsym_index_
;
721 // Set the index of the input section in the input file.
723 set_input_shndx(unsigned int i
)
725 this->input_shndx_
= i
;
726 // input_shndx_ field is a bitfield, so make sure that the value
728 gold_assert(this->input_shndx_
== i
);
731 // Return the index of the input section in the input file.
734 { return this->input_shndx_
; }
736 // Record that this is a section symbol.
738 set_is_section_symbol()
739 { this->is_section_symbol_
= true; }
741 // Record that this is a TLS symbol.
744 { this->is_tls_symbol_
= true; }
746 // Return TRUE if this is a TLS symbol.
748 is_tls_symbol() const
749 { return this->is_tls_symbol_
; }
752 // The index of this local symbol in the output symbol table. This
753 // will be -1 if the symbol should not go into the symbol table.
754 unsigned int output_symtab_index_
;
755 // The index of this local symbol in the dynamic symbol table. This
756 // will be -1 if the symbol should not go into the symbol table.
757 unsigned int output_dynsym_index_
;
758 // The section index in the input file in which this symbol is
760 unsigned int input_shndx_
: 29;
761 // Whether this is a STT_SECTION symbol.
762 bool is_section_symbol_
: 1;
763 // Whether this is a STT_TLS symbol.
764 bool is_tls_symbol_
: 1;
765 // Whether getting the value of this symbol requires calling an
766 // Output_section method. For example, this will be true of a
767 // symbol in a SHF_MERGE section.
768 bool needs_output_address_
: 1;
769 // The value of the symbol. If !needs_output_address_, this is the
770 // value in the output file. If needs_output_address_, this is the
771 // value in the input file.
775 // A regular object file. This is size and endian specific.
777 template<int size
, bool big_endian
>
778 class Sized_relobj
: public Relobj
781 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
782 typedef std::vector
<Symbol
*> Symbols
;
783 typedef std::vector
<Symbol_value
<size
> > Local_values
;
785 Sized_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
786 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
790 // Set up the object file based on the ELF header.
792 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
794 // Return the number of local symbols.
796 local_symbol_count() const
797 { return this->local_symbol_count_
; }
799 // If SYM is the index of a global symbol in the object file's
800 // symbol table, return the Symbol object. Otherwise, return NULL.
802 global_symbol(unsigned int sym
) const
804 if (sym
>= this->local_symbol_count_
)
806 gold_assert(sym
- this->local_symbol_count_
< this->symbols_
.size());
807 return this->symbols_
[sym
- this->local_symbol_count_
];
812 // Return the section index of symbol SYM. Set *VALUE to its value
813 // in the object file. Note that for a symbol which is not defined
814 // in this object file, this will set *VALUE to 0 and return
815 // SHN_UNDEF; it will not return the final value of the symbol in
818 symbol_section_and_value(unsigned int sym
, Address
* value
);
820 // Return a pointer to the Symbol_value structure which holds the
821 // value of a local symbol.
822 const Symbol_value
<size
>*
823 local_symbol(unsigned int sym
) const
825 gold_assert(sym
< this->local_values_
.size());
826 return &this->local_values_
[sym
];
829 // Return the index of local symbol SYM in the ordinary symbol
830 // table. A value of -1U means that the symbol is not being output.
832 symtab_index(unsigned int sym
) const
834 gold_assert(sym
< this->local_values_
.size());
835 return this->local_values_
[sym
].output_symtab_index();
838 // Return the index of local symbol SYM in the dynamic symbol
839 // table. A value of -1U means that the symbol is not being output.
841 dynsym_index(unsigned int sym
) const
843 gold_assert(sym
< this->local_values_
.size());
844 return this->local_values_
[sym
].output_dynsym_index();
847 // Return the appropriate Sized_target structure.
848 Sized_target
<size
, big_endian
>*
851 return this->Object::sized_target
852 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
853 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
856 // Return the value of the local symbol symndx.
858 local_symbol_value(unsigned int symndx
) const;
860 // Return the value of a local symbol defined in input section
861 // SHNDX, with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
862 // indicates whether the symbol is a section symbol. This handles
863 // SHF_MERGE sections.
865 local_value(unsigned int shndx
, Address value
, bool is_section_symbol
,
866 Address addend
) const;
869 set_needs_output_dynsym_entry(unsigned int sym
)
871 gold_assert(sym
< this->local_values_
.size());
872 this->local_values_
[sym
].set_needs_output_dynsym_entry();
875 // Return whether the local symbol SYMNDX has a GOT offset.
876 // For TLS symbols, the GOT entry will hold its tp-relative offset.
878 local_has_got_offset(unsigned int symndx
) const
880 return (this->local_got_offsets_
.find(symndx
)
881 != this->local_got_offsets_
.end());
884 // Return the GOT offset of the local symbol SYMNDX.
886 local_got_offset(unsigned int symndx
) const
888 Local_got_offsets::const_iterator p
=
889 this->local_got_offsets_
.find(symndx
);
890 gold_assert(p
!= this->local_got_offsets_
.end());
894 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
896 set_local_got_offset(unsigned int symndx
, unsigned int got_offset
)
898 std::pair
<Local_got_offsets::iterator
, bool> ins
=
899 this->local_got_offsets_
.insert(std::make_pair(symndx
, got_offset
));
900 gold_assert(ins
.second
);
903 // Return whether the local TLS symbol SYMNDX has a GOT offset.
904 // The GOT entry at this offset will contain a module index. If
905 // NEED_PAIR is true, a second entry immediately following the first
906 // will contain the dtv-relative offset.
908 local_has_tls_got_offset(unsigned int symndx
, bool need_pair
) const
910 typename
Local_tls_got_offsets::const_iterator p
=
911 this->local_tls_got_offsets_
.find(symndx
);
912 if (p
== this->local_tls_got_offsets_
.end()
913 || (need_pair
&& !p
->second
.have_pair_
))
918 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
919 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
920 // otherwise we need the offset of the GOT entry for the module index.
922 local_tls_got_offset(unsigned int symndx
, bool need_pair
) const
924 typename
Local_tls_got_offsets::const_iterator p
=
925 this->local_tls_got_offsets_
.find(symndx
);
926 gold_assert(p
!= this->local_tls_got_offsets_
.end());
927 gold_assert(!need_pair
|| p
->second
.have_pair_
);
928 return p
->second
.got_offset_
;
931 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
932 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
933 // otherwise, we have just a single entry for the module index.
935 set_local_tls_got_offset(unsigned int symndx
, unsigned int got_offset
,
938 typename
Local_tls_got_offsets::iterator p
=
939 this->local_tls_got_offsets_
.find(symndx
);
940 if (p
!= this->local_tls_got_offsets_
.end())
942 // An entry already existed for this symbol. This can happen
943 // if we see a relocation asking for the module index before
944 // a relocation asking for the pair. In that case, the original
945 // GOT entry will remain, but won't get used by any further
947 p
->second
.got_offset_
= got_offset
;
948 gold_assert(have_pair
);
949 p
->second
.have_pair_
= true;
953 std::pair
<typename
Local_tls_got_offsets::iterator
, bool> ins
=
954 this->local_tls_got_offsets_
.insert(
955 std::make_pair(symndx
, Tls_got_entry(got_offset
, have_pair
)));
956 gold_assert(ins
.second
);
960 // Return the name of the symbol that spans the given offset in the
961 // specified section in this object. This is used only for error
962 // messages and is not particularly efficient.
964 get_symbol_location_info(unsigned int shndx
, off_t offset
,
965 Symbol_location_info
* info
);
969 do_read_symbols(Read_symbols_data
*);
971 // Lay out the input sections.
973 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
975 // Add the symbols to the symbol table.
977 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
981 do_read_relocs(Read_relocs_data
*);
983 // Scan the relocs and adjust the symbol table.
985 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
988 // Count the local symbols.
990 do_count_local_symbols(Stringpool_template
<char>*,
991 Stringpool_template
<char>*);
993 // Finalize the local symbols.
995 do_finalize_local_symbols(unsigned int, off_t
);
997 // Set the offset where local dynamic symbol information will be stored.
999 do_set_local_dynsym_indexes(unsigned int);
1001 // Set the offset where local dynamic symbol information will be stored.
1003 do_set_local_dynsym_offset(off_t
);
1005 // Relocate the input sections and write out the local symbols.
1007 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
1008 const Layout
*, Output_file
* of
);
1010 // Get the name of a section.
1012 do_section_name(unsigned int shndx
)
1013 { return this->elf_file_
.section_name(shndx
); }
1015 // Return the location of the contents of a section.
1017 do_section_contents(unsigned int shndx
)
1018 { return this->elf_file_
.section_contents(shndx
); }
1020 // Return section flags.
1022 do_section_flags(unsigned int shndx
)
1023 { return this->elf_file_
.section_flags(shndx
); }
1025 // Return section type.
1027 do_section_type(unsigned int shndx
)
1028 { return this->elf_file_
.section_type(shndx
); }
1030 // Return the section link field.
1032 do_section_link(unsigned int shndx
)
1033 { return this->elf_file_
.section_link(shndx
); }
1035 // Return the section info field.
1037 do_section_info(unsigned int shndx
)
1038 { return this->elf_file_
.section_info(shndx
); }
1042 typedef Sized_relobj
<size
, big_endian
> This
;
1043 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
1044 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1045 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1046 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
1048 // Find the SHT_SYMTAB section, given the section headers.
1050 find_symtab(const unsigned char* pshdrs
);
1052 // Return whether SHDR has the right flags for a GNU style exception
1055 check_eh_frame_flags(const elfcpp::Shdr
<size
, big_endian
>* shdr
) const;
1057 // Return whether there is a section named .eh_frame which might be
1058 // a GNU style exception frame section.
1060 find_eh_frame(const unsigned char* pshdrs
, const char* names
,
1061 off_t names_size
) const;
1063 // Whether to include a section group in the link.
1065 include_section_group(Layout
*, unsigned int,
1066 const elfcpp::Shdr
<size
, big_endian
>&,
1067 std::vector
<bool>*);
1069 // Whether to include a linkonce section in the link.
1071 include_linkonce_section(Layout
*, const char*,
1072 const elfcpp::Shdr
<size
, big_endian
>&);
1074 // Views and sizes when relocating.
1077 unsigned char* view
;
1078 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
1081 bool is_input_output_view
;
1082 bool is_postprocessing_view
;
1085 typedef std::vector
<View_size
> Views
;
1087 // Write section data to the output file. Record the views and
1088 // sizes in VIEWS for use when relocating.
1090 write_sections(const unsigned char* pshdrs
, Output_file
*, Views
*);
1092 // Relocate the sections in the output file.
1094 relocate_sections(const General_options
& options
, const Symbol_table
*,
1095 const Layout
*, const unsigned char* pshdrs
, Views
*);
1097 // Write out the local symbols.
1099 write_local_symbols(Output_file
*,
1100 const Stringpool_template
<char>*,
1101 const Stringpool_template
<char>*);
1103 // The GOT offsets of local symbols. This map also stores GOT offsets
1104 // for tp-relative offsets for TLS symbols.
1105 typedef Unordered_map
<unsigned int, unsigned int> Local_got_offsets
;
1107 // The TLS GOT offsets of local symbols. The map stores the offsets
1108 // for either a single GOT entry that holds the module index of a TLS
1109 // symbol, or a pair of GOT entries containing the module index and
1110 // dtv-relative offset.
1111 struct Tls_got_entry
1113 Tls_got_entry(int got_offset
, bool have_pair
)
1114 : got_offset_(got_offset
),
1115 have_pair_(have_pair
)
1120 typedef Unordered_map
<unsigned int, Tls_got_entry
> Local_tls_got_offsets
;
1122 // General access to the ELF file.
1123 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
1124 // Index of SHT_SYMTAB section.
1125 unsigned int symtab_shndx_
;
1126 // The number of local symbols.
1127 unsigned int local_symbol_count_
;
1128 // The number of local symbols which go into the output file.
1129 unsigned int output_local_symbol_count_
;
1130 // The number of local symbols which go into the output file's dynamic
1132 unsigned int output_local_dynsym_count_
;
1133 // The entries in the symbol table for the external symbols.
1135 // File offset for local symbols.
1136 off_t local_symbol_offset_
;
1137 // File offset for local dynamic symbols.
1138 off_t local_dynsym_offset_
;
1139 // Values of local symbols.
1140 Local_values local_values_
;
1141 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1142 // for TLS symbols, indexed by symbol number.
1143 Local_got_offsets local_got_offsets_
;
1144 // GOT offsets for local TLS symbols, indexed by symbol number
1145 // and GOT entry type.
1146 Local_tls_got_offsets local_tls_got_offsets_
;
1147 // Whether this object has a GNU style .eh_frame section.
1151 // A class to manage the list of all objects.
1157 : relobj_list_(), dynobj_list_(), target_(NULL
), sonames_(),
1158 system_library_directory_()
1161 // The type of the list of input relocateable objects.
1162 typedef std::vector
<Relobj
*> Relobj_list
;
1163 typedef Relobj_list::const_iterator Relobj_iterator
;
1165 // The type of the list of input dynamic objects.
1166 typedef std::vector
<Dynobj
*> Dynobj_list
;
1167 typedef Dynobj_list::const_iterator Dynobj_iterator
;
1169 // Add an object to the list. Return true if all is well, or false
1170 // if this object should be ignored.
1172 add_object(Object
*);
1174 // Get the target we should use for the output file.
1177 { return this->target_
; }
1179 // For each dynamic object, check whether we've seen all of its
1180 // explicit dependencies.
1182 check_dynamic_dependencies() const;
1184 // Return whether an object was found in the system library
1187 found_in_system_library_directory(const Object
*) const;
1189 // Iterate over all regular objects.
1192 relobj_begin() const
1193 { return this->relobj_list_
.begin(); }
1197 { return this->relobj_list_
.end(); }
1199 // Iterate over all dynamic objects.
1202 dynobj_begin() const
1203 { return this->dynobj_list_
.begin(); }
1207 { return this->dynobj_list_
.end(); }
1209 // Return whether we have seen any dynamic objects.
1212 { return !this->dynobj_list_
.empty(); }
1214 // Return the number of input objects.
1216 number_of_input_objects() const
1217 { return this->relobj_list_
.size() + this->dynobj_list_
.size(); }
1220 Input_objects(const Input_objects
&);
1221 Input_objects
& operator=(const Input_objects
&);
1223 // The list of ordinary objects included in the link.
1224 Relobj_list relobj_list_
;
1225 // The list of dynamic objects included in the link.
1226 Dynobj_list dynobj_list_
;
1229 // SONAMEs that we have seen.
1230 Unordered_set
<std::string
> sonames_
;
1231 // The directory in which we find the libc.so.
1232 std::string system_library_directory_
;
1235 // Some of the information we pass to the relocation routines. We
1236 // group this together to avoid passing a dozen different arguments.
1238 template<int size
, bool big_endian
>
1239 struct Relocate_info
1241 // Command line options.
1242 const General_options
* options
;
1244 const Symbol_table
* symtab
;
1246 const Layout
* layout
;
1247 // Object being relocated.
1248 Sized_relobj
<size
, big_endian
>* object
;
1249 // Section index of relocation section.
1250 unsigned int reloc_shndx
;
1251 // Section index of section being relocated.
1252 unsigned int data_shndx
;
1254 // Return a string showing the location of a relocation. This is
1255 // only used for error messages.
1257 location(size_t relnum
, off_t reloffset
) const;
1260 // Return an Object appropriate for the input file. P is BYTES long,
1261 // and holds the ELF header.
1264 make_elf_object(const std::string
& name
, Input_file
*,
1265 off_t offset
, const unsigned char* p
,
1268 } // end namespace gold
1270 #endif // !defined(GOLD_OBJECT_H)