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 size of a section given a section index.
211 section_size(unsigned int shndx
)
212 { return this->do_section_size(shndx
); }
214 // Return the name of a section given a section index.
216 section_name(unsigned int shndx
)
217 { return this->do_section_name(shndx
); }
219 // Return the section flags given a section index.
221 section_flags(unsigned int shndx
)
222 { return this->do_section_flags(shndx
); }
224 // Return the section type given a section index.
226 section_type(unsigned int shndx
)
227 { return this->do_section_type(shndx
); }
229 // Return the section link field given a section index.
231 section_link(unsigned int shndx
)
232 { return this->do_section_link(shndx
); }
234 // Return the section info field given a section index.
236 section_info(unsigned int shndx
)
237 { return this->do_section_info(shndx
); }
239 // Return the required section alignment given a section index.
241 section_addralign(unsigned int shndx
)
242 { return this->do_section_addralign(shndx
); }
244 // Read the symbol information.
246 read_symbols(Read_symbols_data
* sd
)
247 { return this->do_read_symbols(sd
); }
249 // Pass sections which should be included in the link to the Layout
250 // object, and record where the sections go in the output file.
252 layout(Symbol_table
* symtab
, Layout
* layout
, Read_symbols_data
* sd
)
253 { this->do_layout(symtab
, layout
, sd
); }
255 // Add symbol information to the global symbol table.
257 add_symbols(Symbol_table
* symtab
, Read_symbols_data
* sd
)
258 { this->do_add_symbols(symtab
, sd
); }
260 // Functions and types for the elfcpp::Elf_file interface. This
261 // permit us to use Object as the File template parameter for
264 // The View class is returned by view. It must support a single
265 // method, data(). This is trivial, because get_view does what we
270 View(const unsigned char* p
)
279 const unsigned char* p_
;
284 view(off_t file_offset
, section_size_type data_size
)
285 { return View(this->get_view(file_offset
, data_size
, true)); }
289 error(const char* format
, ...) const ATTRIBUTE_PRINTF_2
;
291 // A location in the file.
297 Location(off_t fo
, section_size_type ds
)
298 : file_offset(fo
), data_size(ds
)
302 // Get a View given a Location.
303 View
view(Location loc
)
304 { return View(this->get_view(loc
.file_offset
, loc
.data_size
, true)); }
306 // Get a view into the underlying file.
308 get_view(off_t start
, section_size_type size
, bool cache
)
310 return this->input_file()->file().get_view(start
+ this->offset_
, size
,
314 // Get a lasting view into the underlying file.
316 get_lasting_view(off_t start
, section_size_type size
, bool cache
)
318 return this->input_file()->file().get_lasting_view(start
+ this->offset_
,
322 // Read data from the underlying file.
324 read(off_t start
, section_size_type size
, void* p
) const
325 { this->input_file()->file().read(start
+ this->offset_
, size
, p
); }
327 // Read multiple data from the underlying file.
329 read_multiple(const File_read::Read_multiple
& rm
)
330 { this->input_file()->file().read_multiple(this->offset_
, rm
); }
332 // Stop caching views in the underlying file.
334 clear_view_cache_marks()
335 { this->input_file()->file().clear_view_cache_marks(); }
338 // Read the symbols--implemented by child class.
340 do_read_symbols(Read_symbols_data
*) = 0;
342 // Lay out sections--implemented by child class.
344 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*) = 0;
346 // Add symbol information to the global symbol table--implemented by
349 do_add_symbols(Symbol_table
*, Read_symbols_data
*) = 0;
351 // Return the location of the contents of a section. Implemented by
354 do_section_contents(unsigned int shndx
) = 0;
356 // Get the size of a section--implemented by child class.
358 do_section_size(unsigned int shndx
) = 0;
360 // Get the name of a section--implemented by child class.
362 do_section_name(unsigned int shndx
) = 0;
364 // Get section flags--implemented by child class.
366 do_section_flags(unsigned int shndx
) = 0;
368 // Get section type--implemented by child class.
370 do_section_type(unsigned int shndx
) = 0;
372 // Get section link field--implemented by child class.
374 do_section_link(unsigned int shndx
) = 0;
376 // Get section info field--implemented by child class.
378 do_section_info(unsigned int shndx
) = 0;
380 // Get section alignment--implemented by child class.
382 do_section_addralign(unsigned int shndx
) = 0;
384 // Get the file. We pass on const-ness.
387 { return this->input_file_
; }
391 { return this->input_file_
; }
395 set_target(int machine
, int size
, bool big_endian
, int osabi
,
398 // Set the number of sections.
401 { this->shnum_
= shnum
; }
403 // Functions used by both Sized_relobj and Sized_dynobj.
405 // Read the section data into a Read_symbols_data object.
406 template<int size
, bool big_endian
>
408 read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>*,
411 // If NAME is the name of a special .gnu.warning section, arrange
412 // for the warning to be issued. SHNDX is the section index.
413 // Return whether it is a warning section.
415 handle_gnu_warning_section(const char* name
, unsigned int shndx
,
419 // This class may not be copied.
420 Object(const Object
&);
421 Object
& operator=(const Object
&);
423 // Name of object as printed to user.
425 // For reading the file.
426 Input_file
* input_file_
;
427 // Offset within the file--0 for an object file, non-0 for an
430 // Number of input sections.
432 // Whether this is a dynamic object.
434 // Target functions--may be NULL if the target is not known.
438 // Implement sized_target inline for efficiency. This approach breaks
439 // static type checking, but is made safe using asserts.
441 template<int size
, bool big_endian
>
442 inline Sized_target
<size
, big_endian
>*
443 Object::sized_target(ACCEPT_SIZE_ENDIAN_ONLY
) const
445 gold_assert(this->target_
->get_size() == size
);
446 gold_assert(this->target_
->is_big_endian() ? big_endian
: !big_endian
);
447 return static_cast<Sized_target
<size
, big_endian
>*>(this->target_
);
450 // A regular object (ET_REL). This is an abstract base class itself.
451 // The implementation is the template class Sized_relobj.
453 class Relobj
: public Object
456 Relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
= 0)
457 : Object(name
, input_file
, false, offset
),
459 object_merge_map_(NULL
),
460 relocs_must_follow_section_writes_(false)
465 read_relocs(Read_relocs_data
* rd
)
466 { return this->do_read_relocs(rd
); }
468 // Scan the relocs and adjust the symbol table.
470 scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
471 Layout
* layout
, Read_relocs_data
* rd
)
472 { return this->do_scan_relocs(options
, symtab
, layout
, rd
); }
474 // The number of local symbols in the input symbol table.
476 local_symbol_count() const
477 { return this->do_local_symbol_count(); }
479 // Initial local symbol processing: count the number of local symbols
480 // in the output symbol table and dynamic symbol table; add local symbol
481 // names to *POOL and *DYNPOOL.
483 count_local_symbols(Stringpool_template
<char>* pool
,
484 Stringpool_template
<char>* dynpool
)
485 { return this->do_count_local_symbols(pool
, dynpool
); }
487 // Set the values of the local symbols, set the output symbol table
488 // indexes for the local variables, and set the offset where local
489 // symbol information will be stored. Returns the new local symbol index.
491 finalize_local_symbols(unsigned int index
, off_t off
)
492 { return this->do_finalize_local_symbols(index
, off
); }
494 // Set the output dynamic symbol table indexes for the local variables.
496 set_local_dynsym_indexes(unsigned int index
)
497 { return this->do_set_local_dynsym_indexes(index
); }
499 // Set the offset where local dynamic symbol information will be stored.
501 set_local_dynsym_offset(off_t off
)
502 { return this->do_set_local_dynsym_offset(off
); }
504 // Relocate the input sections and write out the local symbols.
506 relocate(const General_options
& options
, const Symbol_table
* symtab
,
507 const Layout
* layout
, Output_file
* of
)
508 { return this->do_relocate(options
, symtab
, layout
, of
); }
510 // Return whether an input section is being included in the link.
512 is_section_included(unsigned int shndx
) const
514 gold_assert(shndx
< this->map_to_output_
.size());
515 return this->map_to_output_
[shndx
].output_section
!= NULL
;
518 // Return whether an input section requires special
519 // handling--whether it is not simply mapped from the input file to
522 is_section_specially_mapped(unsigned int shndx
) const
524 gold_assert(shndx
< this->map_to_output_
.size());
525 return (this->map_to_output_
[shndx
].output_section
!= NULL
526 && this->map_to_output_
[shndx
].offset
== -1);
529 // Given a section index, return the corresponding Output_section
530 // (which will be NULL if the section is not included in the link)
531 // and set *POFF to the offset within that section. *POFF will be
532 // set to -1 if the section requires special handling.
533 inline Output_section
*
534 output_section(unsigned int shndx
, section_offset_type
* poff
) const;
536 // Set the offset of an input section within its output section.
538 set_section_offset(unsigned int shndx
, section_offset_type off
)
540 gold_assert(shndx
< this->map_to_output_
.size());
541 this->map_to_output_
[shndx
].offset
= off
;
544 // Return true if we need to wait for output sections to be written
545 // before we can apply relocations. This is true if the object has
546 // any relocations for sections which require special handling, such
547 // as the exception frame section.
549 relocs_must_follow_section_writes() const
550 { return this->relocs_must_follow_section_writes_
; }
552 // Return the object merge map.
555 { return this->object_merge_map_
; }
557 // Set the object merge map.
559 set_merge_map(Object_merge_map
* object_merge_map
)
561 gold_assert(this->object_merge_map_
== NULL
);
562 this->object_merge_map_
= object_merge_map
;
566 // What we need to know to map an input section to an output
567 // section. We keep an array of these, one for each input section,
568 // indexed by the input section number.
571 // The output section. This is NULL if the input section is to be
573 Output_section
* output_section
;
574 // The offset within the output section. This is -1 if the
575 // section requires special handling.
576 section_offset_type offset
;
579 // Read the relocs--implemented by child class.
581 do_read_relocs(Read_relocs_data
*) = 0;
583 // Scan the relocs--implemented by child class.
585 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
586 Read_relocs_data
*) = 0;
588 // Return the number of local symbols--implemented by child class.
590 do_local_symbol_count() const = 0;
592 // Count local symbols--implemented by child class.
594 do_count_local_symbols(Stringpool_template
<char>*,
595 Stringpool_template
<char>*) = 0;
597 // Finalize the local symbols. Set the output symbol table indexes for the local variables, and set the
598 // offset where local symbol information will be stored.
600 do_finalize_local_symbols(unsigned int, off_t
) = 0;
602 // Set the output dynamic symbol table indexes for the local variables.
604 do_set_local_dynsym_indexes(unsigned int) = 0;
606 // Set the offset where local dynamic symbol information will be stored.
608 do_set_local_dynsym_offset(off_t
) = 0;
610 // Relocate the input sections and write out the local
611 // symbols--implemented by child class.
613 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
614 const Layout
*, Output_file
* of
) = 0;
616 // Return the vector mapping input sections to output sections.
617 std::vector
<Map_to_output
>&
619 { return this->map_to_output_
; }
621 const std::vector
<Map_to_output
>&
622 map_to_output() const
623 { return this->map_to_output_
; }
625 // Record that we must wait for the output sections to be written
626 // before applying relocations.
628 set_relocs_must_follow_section_writes()
629 { this->relocs_must_follow_section_writes_
= true; }
632 // Mapping from input sections to output section.
633 std::vector
<Map_to_output
> map_to_output_
;
634 // Mappings for merge sections. This is managed by the code in the
636 Object_merge_map
* object_merge_map_
;
637 // Whether we need to wait for output sections to be written before
638 // we can apply relocations.
639 bool relocs_must_follow_section_writes_
;
642 // Implement Object::output_section inline for efficiency.
643 inline Output_section
*
644 Relobj::output_section(unsigned int shndx
, section_offset_type
* poff
) const
646 gold_assert(shndx
< this->map_to_output_
.size());
647 const Map_to_output
& mo(this->map_to_output_
[shndx
]);
649 return mo
.output_section
;
652 // This class is used to handle relocations against a section symbol
653 // in an SHF_MERGE section. For such a symbol, we need to know the
654 // addend of the relocation before we can determine the final value.
655 // The addend gives us the location in the input section, and we can
656 // determine how it is mapped to the output section. For a
657 // non-section symbol, we apply the addend to the final value of the
658 // symbol; that is done in finalize_local_symbols, and does not use
662 class Merged_symbol_value
665 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value
;
667 // We use a hash table to map offsets in the input section to output
669 typedef Unordered_map
<section_offset_type
, Value
> Output_addresses
;
671 Merged_symbol_value(Value input_value
, Value output_start_address
)
672 : input_value_(input_value
), output_start_address_(output_start_address
),
676 // Initialize the hash table.
678 initialize_input_to_output_map(const Relobj
*, unsigned int input_shndx
);
680 // Release the hash table to save space.
682 free_input_to_output_map()
683 { this->output_addresses_
.clear(); }
685 // Get the output value corresponding to an addend. The object and
686 // input section index are passed in because the caller will have
687 // them; otherwise we could store them here.
689 value(const Relobj
* object
, unsigned int input_shndx
, Value addend
) const
691 Value input_offset
= this->input_value_
+ addend
;
692 typename
Output_addresses::const_iterator p
=
693 this->output_addresses_
.find(input_offset
);
694 if (p
!= this->output_addresses_
.end())
697 return this->value_from_output_section(object
, input_shndx
, input_offset
);
701 // Get the output value for an input offset if we couldn't find it
702 // in the hash table.
704 value_from_output_section(const Relobj
*, unsigned int input_shndx
,
705 Value input_offset
) const;
707 // The value of the section symbol in the input file. This is
708 // normally zero, but could in principle be something else.
710 // The start address of this merged section in the output file.
711 Value output_start_address_
;
712 // A hash table which maps offsets in the input section to output
713 // addresses. This only maps specific offsets, not all offsets.
714 Output_addresses output_addresses_
;
717 // This POD class is holds the value of a symbol. This is used for
718 // local symbols, and for all symbols during relocation processing.
719 // For special sections, such as SHF_MERGE sections, this calls a
720 // function to get the final symbol value.
726 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value
;
729 : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
730 is_section_symbol_(false), is_tls_symbol_(false),
731 has_output_value_(true)
732 { this->u_
.value
= 0; }
734 // Get the value of this symbol. OBJECT is the object in which this
735 // symbol is defined, and ADDEND is an addend to add to the value.
736 template<bool big_endian
>
738 value(const Sized_relobj
<size
, big_endian
>* object
, Value addend
) const
740 if (this->has_output_value_
)
741 return this->u_
.value
+ addend
;
743 return this->u_
.merged_symbol_value
->value(object
, this->input_shndx_
,
747 // Set the value of this symbol in the output symbol table.
749 set_output_value(Value value
)
750 { this->u_
.value
= value
; }
752 // For a section symbol in a merged section, we need more
755 set_merged_symbol_value(Merged_symbol_value
<size
>* msv
)
757 gold_assert(this->is_section_symbol_
);
758 this->has_output_value_
= false;
759 this->u_
.merged_symbol_value
= msv
;
762 // Initialize the input to output map for a section symbol in a
763 // merged section. We also initialize the value of a non-section
764 // symbol in a merged section.
766 initialize_input_to_output_map(const Relobj
* object
)
768 if (!this->has_output_value_
)
770 gold_assert(this->is_section_symbol_
);
771 Merged_symbol_value
<size
>* msv
= this->u_
.merged_symbol_value
;
772 msv
->initialize_input_to_output_map(object
, this->input_shndx_
);
776 // Free the input to output map for a section symbol in a merged
779 free_input_to_output_map()
781 if (!this->has_output_value_
)
782 this->u_
.merged_symbol_value
->free_input_to_output_map();
785 // Set the value of the symbol from the input file. This is only
786 // called by count_local_symbols, to communicate the value to
787 // finalize_local_symbols.
789 set_input_value(Value value
)
790 { this->u_
.value
= value
; }
792 // Return the input value. This is only called by
793 // finalize_local_symbols.
796 { return this->u_
.value
; }
798 // Return whether this symbol should go into the output symbol
801 needs_output_symtab_entry() const
802 { return this->output_symtab_index_
!= -1U; }
804 // Return the index in the output symbol table.
806 output_symtab_index() const
808 gold_assert(this->output_symtab_index_
!= 0);
809 return this->output_symtab_index_
;
812 // Set the index in the output symbol table.
814 set_output_symtab_index(unsigned int i
)
816 gold_assert(this->output_symtab_index_
== 0);
817 this->output_symtab_index_
= i
;
820 // Record that this symbol should not go into the output symbol
823 set_no_output_symtab_entry()
825 gold_assert(this->output_symtab_index_
== 0);
826 this->output_symtab_index_
= -1U;
829 // Set the index in the output dynamic symbol table.
831 set_needs_output_dynsym_entry()
833 this->output_dynsym_index_
= 0;
836 // Return whether this symbol should go into the output symbol
839 needs_output_dynsym_entry() const
841 return this->output_dynsym_index_
!= -1U;
844 // Record that this symbol should go into the dynamic symbol table.
846 set_output_dynsym_index(unsigned int i
)
848 gold_assert(this->output_dynsym_index_
== 0);
849 this->output_dynsym_index_
= i
;
852 // Return the index in the output dynamic symbol table.
854 output_dynsym_index() const
856 gold_assert(this->output_dynsym_index_
!= 0);
857 return this->output_dynsym_index_
;
860 // Set the index of the input section in the input file.
862 set_input_shndx(unsigned int i
)
864 this->input_shndx_
= i
;
865 // input_shndx_ field is a bitfield, so make sure that the value
867 gold_assert(this->input_shndx_
== i
);
870 // Return the index of the input section in the input file.
873 { return this->input_shndx_
; }
875 // Whether this is a section symbol.
877 is_section_symbol() const
878 { return this->is_section_symbol_
; }
880 // Record that this is a section symbol.
882 set_is_section_symbol()
883 { this->is_section_symbol_
= true; }
885 // Record that this is a TLS symbol.
888 { this->is_tls_symbol_
= true; }
890 // Return TRUE if this is a TLS symbol.
892 is_tls_symbol() const
893 { return this->is_tls_symbol_
; }
896 // The index of this local symbol in the output symbol table. This
897 // will be -1 if the symbol should not go into the symbol table.
898 unsigned int output_symtab_index_
;
899 // The index of this local symbol in the dynamic symbol table. This
900 // will be -1 if the symbol should not go into the symbol table.
901 unsigned int output_dynsym_index_
;
902 // The section index in the input file in which this symbol is
904 unsigned int input_shndx_
: 29;
905 // Whether this is a STT_SECTION symbol.
906 bool is_section_symbol_
: 1;
907 // Whether this is a STT_TLS symbol.
908 bool is_tls_symbol_
: 1;
909 // Whether this symbol has a value for the output file. This is
910 // normally set to true during Layout::finalize, by
911 // finalize_local_symbols. It will be false for a section symbol in
912 // a merge section, as for such symbols we can not determine the
913 // value to use in a relocation until we see the addend.
914 bool has_output_value_
: 1;
917 // This is used if has_output_value_ is true. Between
918 // count_local_symbols and finalize_local_symbols, this is the
919 // value in the input file. After finalize_local_symbols, it is
920 // the value in the output file.
922 // This is used if has_output_value_ is false. It points to the
923 // information we need to get the value for a merge section.
924 Merged_symbol_value
<size
>* merged_symbol_value
;
928 // A regular object file. This is size and endian specific.
930 template<int size
, bool big_endian
>
931 class Sized_relobj
: public Relobj
934 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
935 typedef std::vector
<Symbol
*> Symbols
;
936 typedef std::vector
<Symbol_value
<size
> > Local_values
;
938 Sized_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
939 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
943 // Set up the object file based on the ELF header.
945 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
947 // If SYM is the index of a global symbol in the object file's
948 // symbol table, return the Symbol object. Otherwise, return NULL.
950 global_symbol(unsigned int sym
) const
952 if (sym
>= this->local_symbol_count_
)
954 gold_assert(sym
- this->local_symbol_count_
< this->symbols_
.size());
955 return this->symbols_
[sym
- this->local_symbol_count_
];
960 // Return the section index of symbol SYM. Set *VALUE to its value
961 // in the object file. Note that for a symbol which is not defined
962 // in this object file, this will set *VALUE to 0 and return
963 // SHN_UNDEF; it will not return the final value of the symbol in
966 symbol_section_and_value(unsigned int sym
, Address
* value
);
968 // Return a pointer to the Symbol_value structure which holds the
969 // value of a local symbol.
970 const Symbol_value
<size
>*
971 local_symbol(unsigned int sym
) const
973 gold_assert(sym
< this->local_values_
.size());
974 return &this->local_values_
[sym
];
977 // Return the index of local symbol SYM in the ordinary symbol
978 // table. A value of -1U means that the symbol is not being output.
980 symtab_index(unsigned int sym
) const
982 gold_assert(sym
< this->local_values_
.size());
983 return this->local_values_
[sym
].output_symtab_index();
986 // Return the index of local symbol SYM in the dynamic symbol
987 // table. A value of -1U means that the symbol is not being output.
989 dynsym_index(unsigned int sym
) const
991 gold_assert(sym
< this->local_values_
.size());
992 return this->local_values_
[sym
].output_dynsym_index();
995 // Return the appropriate Sized_target structure.
996 Sized_target
<size
, big_endian
>*
999 return this->Object::sized_target
1000 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1001 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
1004 // Return the value of the local symbol symndx.
1006 local_symbol_value(unsigned int symndx
) const;
1009 set_needs_output_dynsym_entry(unsigned int sym
)
1011 gold_assert(sym
< this->local_values_
.size());
1012 this->local_values_
[sym
].set_needs_output_dynsym_entry();
1015 // Return whether the local symbol SYMNDX has a GOT offset.
1016 // For TLS symbols, the GOT entry will hold its tp-relative offset.
1018 local_has_got_offset(unsigned int symndx
) const
1020 return (this->local_got_offsets_
.find(symndx
)
1021 != this->local_got_offsets_
.end());
1024 // Return the GOT offset of the local symbol SYMNDX.
1026 local_got_offset(unsigned int symndx
) const
1028 Local_got_offsets::const_iterator p
=
1029 this->local_got_offsets_
.find(symndx
);
1030 gold_assert(p
!= this->local_got_offsets_
.end());
1034 // Set the GOT offset of the local symbol SYMNDX to GOT_OFFSET.
1036 set_local_got_offset(unsigned int symndx
, unsigned int got_offset
)
1038 std::pair
<Local_got_offsets::iterator
, bool> ins
=
1039 this->local_got_offsets_
.insert(std::make_pair(symndx
, got_offset
));
1040 gold_assert(ins
.second
);
1043 // Return whether the local TLS symbol SYMNDX has a GOT offset.
1044 // The GOT entry at this offset will contain a module index. If
1045 // NEED_PAIR is true, a second entry immediately following the first
1046 // will contain the dtv-relative offset.
1048 local_has_tls_got_offset(unsigned int symndx
, bool need_pair
) const
1050 typename
Local_tls_got_offsets::const_iterator p
=
1051 this->local_tls_got_offsets_
.find(symndx
);
1052 if (p
== this->local_tls_got_offsets_
.end()
1053 || (need_pair
&& !p
->second
.have_pair_
))
1058 // Return the offset of the GOT entry for the local TLS symbol SYMNDX.
1059 // If NEED_PAIR is true, we need the offset of a pair of GOT entries;
1060 // otherwise we need the offset of the GOT entry for the module index.
1062 local_tls_got_offset(unsigned int symndx
, bool need_pair
) const
1064 typename
Local_tls_got_offsets::const_iterator p
=
1065 this->local_tls_got_offsets_
.find(symndx
);
1066 gold_assert(p
!= this->local_tls_got_offsets_
.end());
1067 gold_assert(!need_pair
|| p
->second
.have_pair_
);
1068 return p
->second
.got_offset_
;
1071 // Set the offset of the GOT entry for the local TLS symbol SYMNDX
1072 // to GOT_OFFSET. If HAVE_PAIR is true, we have a pair of GOT entries;
1073 // otherwise, we have just a single entry for the module index.
1075 set_local_tls_got_offset(unsigned int symndx
, unsigned int got_offset
,
1078 typename
Local_tls_got_offsets::iterator p
=
1079 this->local_tls_got_offsets_
.find(symndx
);
1080 if (p
!= this->local_tls_got_offsets_
.end())
1082 // An entry already existed for this symbol. This can happen
1083 // if we see a relocation asking for the module index before
1084 // a relocation asking for the pair. In that case, the original
1085 // GOT entry will remain, but won't get used by any further
1087 p
->second
.got_offset_
= got_offset
;
1088 gold_assert(have_pair
);
1089 p
->second
.have_pair_
= true;
1093 std::pair
<typename
Local_tls_got_offsets::iterator
, bool> ins
=
1094 this->local_tls_got_offsets_
.insert(
1095 std::make_pair(symndx
, Tls_got_entry(got_offset
, have_pair
)));
1096 gold_assert(ins
.second
);
1100 // Return the name of the symbol that spans the given offset in the
1101 // specified section in this object. This is used only for error
1102 // messages and is not particularly efficient.
1104 get_symbol_location_info(unsigned int shndx
, off_t offset
,
1105 Symbol_location_info
* info
);
1108 // Read the symbols.
1110 do_read_symbols(Read_symbols_data
*);
1112 // Return the number of local symbols.
1114 do_local_symbol_count() const
1115 { return this->local_symbol_count_
; }
1117 // Lay out the input sections.
1119 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
1121 // Add the symbols to the symbol table.
1123 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
1127 do_read_relocs(Read_relocs_data
*);
1129 // Scan the relocs and adjust the symbol table.
1131 do_scan_relocs(const General_options
&, Symbol_table
*, Layout
*,
1134 // Count the local symbols.
1136 do_count_local_symbols(Stringpool_template
<char>*,
1137 Stringpool_template
<char>*);
1139 // Finalize the local symbols.
1141 do_finalize_local_symbols(unsigned int, off_t
);
1143 // Set the offset where local dynamic symbol information will be stored.
1145 do_set_local_dynsym_indexes(unsigned int);
1147 // Set the offset where local dynamic symbol information will be stored.
1149 do_set_local_dynsym_offset(off_t
);
1151 // Relocate the input sections and write out the local symbols.
1153 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
1154 const Layout
*, Output_file
* of
);
1156 // Get the size of a section.
1158 do_section_size(unsigned int shndx
)
1159 { return this->elf_file_
.section_size(shndx
); }
1161 // Get the name of a section.
1163 do_section_name(unsigned int shndx
)
1164 { return this->elf_file_
.section_name(shndx
); }
1166 // Return the location of the contents of a section.
1168 do_section_contents(unsigned int shndx
)
1169 { return this->elf_file_
.section_contents(shndx
); }
1171 // Return section flags.
1173 do_section_flags(unsigned int shndx
)
1174 { return this->elf_file_
.section_flags(shndx
); }
1176 // Return section type.
1178 do_section_type(unsigned int shndx
)
1179 { return this->elf_file_
.section_type(shndx
); }
1181 // Return the section link field.
1183 do_section_link(unsigned int shndx
)
1184 { return this->elf_file_
.section_link(shndx
); }
1186 // Return the section info field.
1188 do_section_info(unsigned int shndx
)
1189 { return this->elf_file_
.section_info(shndx
); }
1191 // Return the section alignment.
1193 do_section_addralign(unsigned int shndx
)
1194 { return this->elf_file_
.section_addralign(shndx
); }
1198 typedef Sized_relobj
<size
, big_endian
> This
;
1199 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
1200 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1201 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1202 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
1204 // Find the SHT_SYMTAB section, given the section headers.
1206 find_symtab(const unsigned char* pshdrs
);
1208 // Return whether SHDR has the right flags for a GNU style exception
1211 check_eh_frame_flags(const elfcpp::Shdr
<size
, big_endian
>* shdr
) const;
1213 // Return whether there is a section named .eh_frame which might be
1214 // a GNU style exception frame section.
1216 find_eh_frame(const unsigned char* pshdrs
, const char* names
,
1217 section_size_type names_size
) const;
1219 // Whether to include a section group in the link.
1221 include_section_group(Layout
*, unsigned int,
1222 const elfcpp::Shdr
<size
, big_endian
>&,
1223 std::vector
<bool>*);
1225 // Whether to include a linkonce section in the link.
1227 include_linkonce_section(Layout
*, const char*,
1228 const elfcpp::Shdr
<size
, big_endian
>&);
1230 // Views and sizes when relocating.
1233 unsigned char* view
;
1234 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
1236 section_size_type view_size
;
1237 bool is_input_output_view
;
1238 bool is_postprocessing_view
;
1241 typedef std::vector
<View_size
> Views
;
1243 // Write section data to the output file. Record the views and
1244 // sizes in VIEWS for use when relocating.
1246 write_sections(const unsigned char* pshdrs
, Output_file
*, Views
*);
1248 // Relocate the sections in the output file.
1250 relocate_sections(const General_options
& options
, const Symbol_table
*,
1251 const Layout
*, const unsigned char* pshdrs
, Views
*);
1253 // Initialize input to output maps for section symbols in merged
1256 initialize_input_to_output_maps();
1258 // Free the input to output maps for section symbols in merged
1261 free_input_to_output_maps();
1263 // Write out the local symbols.
1265 write_local_symbols(Output_file
*,
1266 const Stringpool_template
<char>*,
1267 const Stringpool_template
<char>*);
1269 // Clear the local symbol information.
1271 clear_local_symbols()
1273 this->local_values_
.clear();
1274 this->local_got_offsets_
.clear();
1275 this->local_tls_got_offsets_
.clear();
1278 // The GOT offsets of local symbols. This map also stores GOT offsets
1279 // for tp-relative offsets for TLS symbols.
1280 typedef Unordered_map
<unsigned int, unsigned int> Local_got_offsets
;
1282 // The TLS GOT offsets of local symbols. The map stores the offsets
1283 // for either a single GOT entry that holds the module index of a TLS
1284 // symbol, or a pair of GOT entries containing the module index and
1285 // dtv-relative offset.
1286 struct Tls_got_entry
1288 Tls_got_entry(int got_offset
, bool have_pair
)
1289 : got_offset_(got_offset
),
1290 have_pair_(have_pair
)
1295 typedef Unordered_map
<unsigned int, Tls_got_entry
> Local_tls_got_offsets
;
1297 // General access to the ELF file.
1298 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
1299 // Index of SHT_SYMTAB section.
1300 unsigned int symtab_shndx_
;
1301 // The number of local symbols.
1302 unsigned int local_symbol_count_
;
1303 // The number of local symbols which go into the output file.
1304 unsigned int output_local_symbol_count_
;
1305 // The number of local symbols which go into the output file's dynamic
1307 unsigned int output_local_dynsym_count_
;
1308 // The entries in the symbol table for the external symbols.
1310 // File offset for local symbols.
1311 off_t local_symbol_offset_
;
1312 // File offset for local dynamic symbols.
1313 off_t local_dynsym_offset_
;
1314 // Values of local symbols.
1315 Local_values local_values_
;
1316 // GOT offsets for local non-TLS symbols, and tp-relative offsets
1317 // for TLS symbols, indexed by symbol number.
1318 Local_got_offsets local_got_offsets_
;
1319 // GOT offsets for local TLS symbols, indexed by symbol number
1320 // and GOT entry type.
1321 Local_tls_got_offsets local_tls_got_offsets_
;
1322 // Whether this object has a GNU style .eh_frame section.
1326 // A class to manage the list of all objects.
1332 : relobj_list_(), dynobj_list_(), target_(NULL
), sonames_(),
1333 system_library_directory_()
1336 // The type of the list of input relocateable objects.
1337 typedef std::vector
<Relobj
*> Relobj_list
;
1338 typedef Relobj_list::const_iterator Relobj_iterator
;
1340 // The type of the list of input dynamic objects.
1341 typedef std::vector
<Dynobj
*> Dynobj_list
;
1342 typedef Dynobj_list::const_iterator Dynobj_iterator
;
1344 // Add an object to the list. Return true if all is well, or false
1345 // if this object should be ignored.
1347 add_object(Object
*);
1349 // Get the target we should use for the output file.
1352 { return this->target_
; }
1354 // For each dynamic object, check whether we've seen all of its
1355 // explicit dependencies.
1357 check_dynamic_dependencies() const;
1359 // Return whether an object was found in the system library
1362 found_in_system_library_directory(const Object
*) const;
1364 // Iterate over all regular objects.
1367 relobj_begin() const
1368 { return this->relobj_list_
.begin(); }
1372 { return this->relobj_list_
.end(); }
1374 // Iterate over all dynamic objects.
1377 dynobj_begin() const
1378 { return this->dynobj_list_
.begin(); }
1382 { return this->dynobj_list_
.end(); }
1384 // Return whether we have seen any dynamic objects.
1387 { return !this->dynobj_list_
.empty(); }
1389 // Return the number of input objects.
1391 number_of_input_objects() const
1392 { return this->relobj_list_
.size() + this->dynobj_list_
.size(); }
1395 Input_objects(const Input_objects
&);
1396 Input_objects
& operator=(const Input_objects
&);
1398 // The list of ordinary objects included in the link.
1399 Relobj_list relobj_list_
;
1400 // The list of dynamic objects included in the link.
1401 Dynobj_list dynobj_list_
;
1404 // SONAMEs that we have seen.
1405 Unordered_set
<std::string
> sonames_
;
1406 // The directory in which we find the libc.so.
1407 std::string system_library_directory_
;
1410 // Some of the information we pass to the relocation routines. We
1411 // group this together to avoid passing a dozen different arguments.
1413 template<int size
, bool big_endian
>
1414 struct Relocate_info
1416 // Command line options.
1417 const General_options
* options
;
1419 const Symbol_table
* symtab
;
1421 const Layout
* layout
;
1422 // Object being relocated.
1423 Sized_relobj
<size
, big_endian
>* object
;
1424 // Section index of relocation section.
1425 unsigned int reloc_shndx
;
1426 // Section index of section being relocated.
1427 unsigned int data_shndx
;
1429 // Return a string showing the location of a relocation. This is
1430 // only used for error messages.
1432 location(size_t relnum
, off_t reloffset
) const;
1435 // Return an Object appropriate for the input file. P is BYTES long,
1436 // and holds the ELF header.
1439 make_elf_object(const std::string
& name
, Input_file
*,
1440 off_t offset
, const unsigned char* p
,
1441 section_offset_type bytes
);
1443 } // end namespace gold
1445 #endif // !defined(GOLD_OBJECT_H)