1 // output.h -- manage the output file for 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.
31 #include "reloc-types.h"
36 class General_options
;
42 template<int size
, bool big_endian
>
44 template<int size
, bool big_endian
>
47 // An abtract class for data which has to go into the output file.
52 explicit Output_data()
53 : address_(0), data_size_(0), offset_(-1),
54 is_address_valid_(false), is_data_size_valid_(false),
55 is_offset_valid_(false),
56 dynamic_reloc_count_(0)
62 // Return the address. For allocated sections, this is only valid
63 // after Layout::finalize is finished.
67 gold_assert(this->is_address_valid_
);
68 return this->address_
;
71 // Return the size of the data. For allocated sections, this must
72 // be valid after Layout::finalize calls set_address, but need not
73 // be valid before then.
77 gold_assert(this->is_data_size_valid_
);
78 return this->data_size_
;
81 // Return the file offset. This is only valid after
82 // Layout::finalize is finished. For some non-allocated sections,
83 // it may not be valid until near the end of the link.
87 gold_assert(this->is_offset_valid_
);
91 // Reset the address and file offset. This essentially disables the
92 // sanity testing about duplicate and unknown settings.
94 reset_address_and_file_offset()
96 this->is_address_valid_
= false;
97 this->is_offset_valid_
= false;
98 this->is_data_size_valid_
= false;
99 this->do_reset_address_and_file_offset();
102 // Return the required alignment.
105 { return this->do_addralign(); }
107 // Return whether this has a load address.
109 has_load_address() const
110 { return this->do_has_load_address(); }
112 // Return the load address.
115 { return this->do_load_address(); }
117 // Return whether this is an Output_section.
120 { return this->do_is_section(); }
122 // Return whether this is an Output_section of the specified type.
124 is_section_type(elfcpp::Elf_Word stt
) const
125 { return this->do_is_section_type(stt
); }
127 // Return whether this is an Output_section with the specified flag
130 is_section_flag_set(elfcpp::Elf_Xword shf
) const
131 { return this->do_is_section_flag_set(shf
); }
133 // Return the output section index, if there is an output section.
136 { return this->do_out_shndx(); }
138 // Set the output section index, if this is an output section.
140 set_out_shndx(unsigned int shndx
)
141 { this->do_set_out_shndx(shndx
); }
143 // Set the address and file offset of this data, and finalize the
144 // size of the data. This is called during Layout::finalize for
145 // allocated sections.
147 set_address_and_file_offset(uint64_t addr
, off_t off
)
149 this->set_address(addr
);
150 this->set_file_offset(off
);
151 this->finalize_data_size();
156 set_address(uint64_t addr
)
158 gold_assert(!this->is_address_valid_
);
159 this->address_
= addr
;
160 this->is_address_valid_
= true;
163 // Set the file offset.
165 set_file_offset(off_t off
)
167 gold_assert(!this->is_offset_valid_
);
169 this->is_offset_valid_
= true;
172 // Finalize the data size.
176 if (!this->is_data_size_valid_
)
178 // Tell the child class to set the data size.
179 this->set_final_data_size();
180 gold_assert(this->is_data_size_valid_
);
184 // Set the TLS offset. Called only for SHT_TLS sections.
186 set_tls_offset(uint64_t tls_base
)
187 { this->do_set_tls_offset(tls_base
); }
189 // Return the TLS offset, relative to the base of the TLS segment.
190 // Valid only for SHT_TLS sections.
193 { return this->do_tls_offset(); }
195 // Write the data to the output file. This is called after
196 // Layout::finalize is complete.
198 write(Output_file
* file
)
199 { this->do_write(file
); }
201 // This is called by Layout::finalize to note that the sizes of
202 // allocated sections must now be fixed.
205 { Output_data::allocated_sizes_are_fixed
= true; }
207 // Used to check that layout has been done.
210 { return Output_data::allocated_sizes_are_fixed
; }
212 // Count the number of dynamic relocations applied to this section.
215 { ++this->dynamic_reloc_count_
; }
217 // Return the number of dynamic relocations applied to this section.
219 dynamic_reloc_count() const
220 { return this->dynamic_reloc_count_
; }
222 // Whether the address is valid.
224 is_address_valid() const
225 { return this->is_address_valid_
; }
227 // Whether the file offset is valid.
229 is_offset_valid() const
230 { return this->is_offset_valid_
; }
232 // Whether the data size is valid.
234 is_data_size_valid() const
235 { return this->is_data_size_valid_
; }
238 // Functions that child classes may or in some cases must implement.
240 // Write the data to the output file.
242 do_write(Output_file
*) = 0;
244 // Return the required alignment.
246 do_addralign() const = 0;
248 // Return whether this has a load address.
250 do_has_load_address() const
253 // Return the load address.
255 do_load_address() const
256 { gold_unreachable(); }
258 // Return whether this is an Output_section.
260 do_is_section() const
263 // Return whether this is an Output_section of the specified type.
264 // This only needs to be implement by Output_section.
266 do_is_section_type(elfcpp::Elf_Word
) const
269 // Return whether this is an Output_section with the specific flag
270 // set. This only needs to be implemented by Output_section.
272 do_is_section_flag_set(elfcpp::Elf_Xword
) const
275 // Return the output section index, if there is an output section.
278 { gold_unreachable(); }
280 // Set the output section index, if this is an output section.
282 do_set_out_shndx(unsigned int)
283 { gold_unreachable(); }
285 // This is a hook for derived classes to set the data size. This is
286 // called by finalize_data_size, normally called during
287 // Layout::finalize, when the section address is set.
289 set_final_data_size()
290 { gold_unreachable(); }
292 // A hook for resetting the address and file offset.
294 do_reset_address_and_file_offset()
297 // Set the TLS offset. Called only for SHT_TLS sections.
299 do_set_tls_offset(uint64_t)
300 { gold_unreachable(); }
302 // Return the TLS offset, relative to the base of the TLS segment.
303 // Valid only for SHT_TLS sections.
305 do_tls_offset() const
306 { gold_unreachable(); }
308 // Functions that child classes may call.
310 // Set the size of the data.
312 set_data_size(off_t data_size
)
314 gold_assert(!this->is_data_size_valid_
);
315 this->data_size_
= data_size
;
316 this->is_data_size_valid_
= true;
319 // Get the current data size--this is for the convenience of
320 // sections which build up their size over time.
322 current_data_size_for_child() const
323 { return this->data_size_
; }
325 // Set the current data size--this is for the convenience of
326 // sections which build up their size over time.
328 set_current_data_size_for_child(off_t data_size
)
330 gold_assert(!this->is_data_size_valid_
);
331 this->data_size_
= data_size
;
334 // Return default alignment for the target size.
338 // Return default alignment for a specified size--32 or 64.
340 default_alignment_for_size(int size
);
343 Output_data(const Output_data
&);
344 Output_data
& operator=(const Output_data
&);
346 // This is used for verification, to make sure that we don't try to
347 // change any sizes of allocated sections after we set the section
349 static bool allocated_sizes_are_fixed
;
351 // Memory address in output file.
353 // Size of data in output file.
355 // File offset of contents in output file.
357 // Whether address_ is valid.
358 bool is_address_valid_
;
359 // Whether data_size_ is valid.
360 bool is_data_size_valid_
;
361 // Whether offset_ is valid.
362 bool is_offset_valid_
;
363 // Count of dynamic relocations applied to this section.
364 unsigned int dynamic_reloc_count_
;
367 // Output the section headers.
369 class Output_section_headers
: public Output_data
372 Output_section_headers(const Layout
*,
373 const Layout::Segment_list
*,
374 const Layout::Section_list
*,
378 // Write the data to the file.
380 do_write(Output_file
*);
382 // Return the required alignment.
385 { return Output_data::default_alignment(); }
388 // Write the data to the file with the right size and endianness.
389 template<int size
, bool big_endian
>
391 do_sized_write(Output_file
*);
393 const Layout
* layout_
;
394 const Layout::Segment_list
* segment_list_
;
395 const Layout::Section_list
* unattached_section_list_
;
396 const Stringpool
* secnamepool_
;
399 // Output the segment headers.
401 class Output_segment_headers
: public Output_data
404 Output_segment_headers(const Layout::Segment_list
& segment_list
);
407 // Write the data to the file.
409 do_write(Output_file
*);
411 // Return the required alignment.
414 { return Output_data::default_alignment(); }
417 // Write the data to the file with the right size and endianness.
418 template<int size
, bool big_endian
>
420 do_sized_write(Output_file
*);
422 const Layout::Segment_list
& segment_list_
;
425 // Output the ELF file header.
427 class Output_file_header
: public Output_data
430 Output_file_header(const Target
*,
432 const Output_segment_headers
*,
435 // Add information about the section headers. We lay out the ELF
436 // file header before we create the section headers.
437 void set_section_info(const Output_section_headers
*,
438 const Output_section
* shstrtab
);
441 // Write the data to the file.
443 do_write(Output_file
*);
445 // Return the required alignment.
448 { return Output_data::default_alignment(); }
451 // Write the data to the file with the right size and endianness.
452 template<int size
, bool big_endian
>
454 do_sized_write(Output_file
*);
456 // Return the value to use for the entry address.
458 typename
elfcpp::Elf_types
<size
>::Elf_Addr
461 const Target
* target_
;
462 const Symbol_table
* symtab_
;
463 const Output_segment_headers
* segment_header_
;
464 const Output_section_headers
* section_header_
;
465 const Output_section
* shstrtab_
;
469 // Output sections are mainly comprised of input sections. However,
470 // there are cases where we have data to write out which is not in an
471 // input section. Output_section_data is used in such cases. This is
472 // an abstract base class.
474 class Output_section_data
: public Output_data
477 Output_section_data(off_t data_size
, uint64_t addralign
)
478 : Output_data(), output_section_(NULL
), addralign_(addralign
)
479 { this->set_data_size(data_size
); }
481 Output_section_data(uint64_t addralign
)
482 : Output_data(), output_section_(NULL
), addralign_(addralign
)
485 // Return the output section.
486 const Output_section
*
487 output_section() const
488 { return this->output_section_
; }
490 // Record the output section.
492 set_output_section(Output_section
* os
);
494 // Add an input section, for SHF_MERGE sections. This returns true
495 // if the section was handled.
497 add_input_section(Relobj
* object
, unsigned int shndx
)
498 { return this->do_add_input_section(object
, shndx
); }
500 // Given an input OBJECT, an input section index SHNDX within that
501 // object, and an OFFSET relative to the start of that input
502 // section, return whether or not the corresponding offset within
503 // the output section is known. If this function returns true, it
504 // sets *POUTPUT to the output offset. The value -1 indicates that
505 // this input offset is being discarded.
507 output_offset(const Relobj
* object
, unsigned int shndx
,
508 section_offset_type offset
,
509 section_offset_type
*poutput
) const
510 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
512 // Return whether this is the merge section for the input section
513 // SHNDX in OBJECT. This should return true when output_offset
514 // would return true for some values of OFFSET.
516 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
517 { return this->do_is_merge_section_for(object
, shndx
); }
519 // Write the contents to a buffer. This is used for sections which
520 // require postprocessing, such as compression.
522 write_to_buffer(unsigned char* buffer
)
523 { this->do_write_to_buffer(buffer
); }
525 // Print merge stats to stderr. This should only be called for
526 // SHF_MERGE sections.
528 print_merge_stats(const char* section_name
)
529 { this->do_print_merge_stats(section_name
); }
532 // The child class must implement do_write.
534 // The child class may implement specific adjustments to the output
537 do_adjust_output_section(Output_section
*)
540 // May be implemented by child class. Return true if the section
543 do_add_input_section(Relobj
*, unsigned int)
544 { gold_unreachable(); }
546 // The child class may implement output_offset.
548 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
549 section_offset_type
*) const
552 // The child class may implement is_merge_section_for.
554 do_is_merge_section_for(const Relobj
*, unsigned int) const
557 // The child class may implement write_to_buffer. Most child
558 // classes can not appear in a compressed section, and they do not
561 do_write_to_buffer(unsigned char*)
562 { gold_unreachable(); }
564 // Print merge statistics.
566 do_print_merge_stats(const char*)
567 { gold_unreachable(); }
569 // Return the required alignment.
572 { return this->addralign_
; }
574 // Return the section index of the output section.
576 do_out_shndx() const;
578 // Set the alignment.
580 set_addralign(uint64_t addralign
)
581 { this->addralign_
= addralign
; }
584 // The output section for this section.
585 const Output_section
* output_section_
;
586 // The required alignment.
590 // Some Output_section_data classes build up their data step by step,
591 // rather than all at once. This class provides an interface for
594 class Output_section_data_build
: public Output_section_data
597 Output_section_data_build(uint64_t addralign
)
598 : Output_section_data(addralign
)
601 // Get the current data size.
603 current_data_size() const
604 { return this->current_data_size_for_child(); }
606 // Set the current data size.
608 set_current_data_size(off_t data_size
)
609 { this->set_current_data_size_for_child(data_size
); }
612 // Set the final data size.
614 set_final_data_size()
615 { this->set_data_size(this->current_data_size_for_child()); }
618 // A simple case of Output_data in which we have constant data to
621 class Output_data_const
: public Output_section_data
624 Output_data_const(const std::string
& data
, uint64_t addralign
)
625 : Output_section_data(data
.size(), addralign
), data_(data
)
628 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
629 : Output_section_data(len
, addralign
), data_(p
, len
)
632 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
633 : Output_section_data(len
, addralign
),
634 data_(reinterpret_cast<const char*>(p
), len
)
638 // Write the data to the output file.
640 do_write(Output_file
*);
642 // Write the data to a buffer.
644 do_write_to_buffer(unsigned char* buffer
)
645 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
651 // Another version of Output_data with constant data, in which the
652 // buffer is allocated by the caller.
654 class Output_data_const_buffer
: public Output_section_data
657 Output_data_const_buffer(const unsigned char* p
, off_t len
,
659 : Output_section_data(len
, addralign
), p_(p
)
663 // Write the data the output file.
665 do_write(Output_file
*);
667 // Write the data to a buffer.
669 do_write_to_buffer(unsigned char* buffer
)
670 { memcpy(buffer
, this->p_
, this->data_size()); }
673 const unsigned char* p_
;
676 // A place holder for a fixed amount of data written out via some
679 class Output_data_fixed_space
: public Output_section_data
682 Output_data_fixed_space(off_t data_size
, uint64_t addralign
)
683 : Output_section_data(data_size
, addralign
)
687 // Write out the data--the actual data must be written out
690 do_write(Output_file
*)
694 // A place holder for variable sized data written out via some other
697 class Output_data_space
: public Output_section_data_build
700 explicit Output_data_space(uint64_t addralign
)
701 : Output_section_data_build(addralign
)
704 // Set the alignment.
706 set_space_alignment(uint64_t align
)
707 { this->set_addralign(align
); }
710 // Write out the data--the actual data must be written out
713 do_write(Output_file
*)
717 // A string table which goes into an output section.
719 class Output_data_strtab
: public Output_section_data
722 Output_data_strtab(Stringpool
* strtab
)
723 : Output_section_data(1), strtab_(strtab
)
727 // This is called to set the address and file offset. Here we make
728 // sure that the Stringpool is finalized.
730 set_final_data_size();
732 // Write out the data.
734 do_write(Output_file
*);
736 // Write the data to a buffer.
738 do_write_to_buffer(unsigned char* buffer
)
739 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
745 // This POD class is used to represent a single reloc in the output
746 // file. This could be a private class within Output_data_reloc, but
747 // the templatization is complex enough that I broke it out into a
748 // separate class. The class is templatized on either elfcpp::SHT_REL
749 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
750 // relocation or an ordinary relocation.
752 // A relocation can be against a global symbol, a local symbol, an
753 // output section, or the undefined symbol at index 0. We represent
754 // the latter by using a NULL global symbol.
756 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
759 template<bool dynamic
, int size
, bool big_endian
>
760 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
763 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
765 // An uninitialized entry. We need this because we want to put
766 // instances of this class into an STL container.
768 : local_sym_index_(INVALID_CODE
)
771 // A reloc against a global symbol.
773 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
774 Address address
, bool is_relative
);
776 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
777 unsigned int shndx
, Address address
, bool is_relative
);
779 // A reloc against a local symbol.
781 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
782 unsigned int local_sym_index
, unsigned int type
,
783 Output_data
* od
, Address address
, bool is_relative
);
785 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
786 unsigned int local_sym_index
, unsigned int type
,
787 unsigned int shndx
, Address address
, bool is_relative
);
789 // A reloc against the STT_SECTION symbol of an output section.
791 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
794 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
795 unsigned int shndx
, Address address
);
797 // Return TRUE if this is a RELATIVE relocation.
800 { return this->is_relative_
; }
802 // Get the value of the symbol referred to by a Rel relocation.
805 symbol_value() const;
807 // Write the reloc entry to an output view.
809 write(unsigned char* pov
) const;
811 // Write the offset and info fields to Write_rel.
812 template<typename Write_rel
>
813 void write_rel(Write_rel
*) const;
816 // Return the symbol index. We can't do a double template
817 // specialization, so we do a secondary template here.
819 get_symbol_index() const;
821 // Codes for local_sym_index_.
828 // Invalid uninitialized entry.
834 // For a local symbol, the object. We will never generate a
835 // relocation against a local symbol in a dynamic object; that
836 // doesn't make sense. And our callers will always be
837 // templatized, so we use Sized_relobj here.
838 Sized_relobj
<size
, big_endian
>* relobj
;
839 // For a global symbol, the symbol. If this is NULL, it indicates
840 // a relocation against the undefined 0 symbol.
842 // For a relocation against an output section, the output section.
847 // If shndx_ is not INVALID CODE, the object which holds the input
848 // section being used to specify the reloc address.
850 // If shndx_ is INVALID_CODE, the output data being used to
851 // specify the reloc address. This may be NULL if the reloc
852 // address is absolute.
855 // The address offset within the input section or the Output_data.
857 // For a local symbol, the local symbol index. This is GSYM_CODE
858 // for a global symbol, or INVALID_CODE for an uninitialized value.
859 unsigned int local_sym_index_
;
860 // The reloc type--a processor specific code.
861 unsigned int type_
: 31;
862 // True if the relocation is a RELATIVE relocation.
863 bool is_relative_
: 1;
864 // If the reloc address is an input section in an object, the
865 // section index. This is INVALID_CODE if the reloc address is
866 // specified in some other way.
870 // The SHT_RELA version of Output_reloc<>. This is just derived from
871 // the SHT_REL version of Output_reloc, but it adds an addend.
873 template<bool dynamic
, int size
, bool big_endian
>
874 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
877 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
878 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
880 // An uninitialized entry.
885 // A reloc against a global symbol.
887 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
888 Address address
, Addend addend
, bool is_relative
)
889 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
892 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
893 unsigned int shndx
, Address address
, Addend addend
,
895 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
898 // A reloc against a local symbol.
900 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
901 unsigned int local_sym_index
, unsigned int type
,
902 Output_data
* od
, Address address
,
903 Addend addend
, bool is_relative
)
904 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
),
908 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
909 unsigned int local_sym_index
, unsigned int type
,
910 unsigned int shndx
, Address address
,
911 Addend addend
, bool is_relative
)
912 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
),
916 // A reloc against the STT_SECTION symbol of an output section.
918 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
919 Address address
, Addend addend
)
920 : rel_(os
, type
, od
, address
), addend_(addend
)
923 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
924 unsigned int shndx
, Address address
, Addend addend
)
925 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
928 // Write the reloc entry to an output view.
930 write(unsigned char* pov
) const;
934 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
939 // Output_data_reloc is used to manage a section containing relocs.
940 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
941 // indicates whether this is a dynamic relocation or a normal
942 // relocation. Output_data_reloc_base is a base class.
943 // Output_data_reloc is the real class, which we specialize based on
946 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
947 class Output_data_reloc_base
: public Output_section_data_build
950 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
951 typedef typename
Output_reloc_type::Address Address
;
952 static const int reloc_size
=
953 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
955 // Construct the section.
956 Output_data_reloc_base()
957 : Output_section_data_build(Output_data::default_alignment_for_size(size
))
961 // Write out the data.
963 do_write(Output_file
*);
965 // Set the entry size and the link.
967 do_adjust_output_section(Output_section
*os
);
969 // Add a relocation entry.
971 add(Output_data
*od
, const Output_reloc_type
& reloc
)
973 this->relocs_
.push_back(reloc
);
974 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
975 od
->add_dynamic_reloc();
979 typedef std::vector
<Output_reloc_type
> Relocs
;
984 // The class which callers actually create.
986 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
987 class Output_data_reloc
;
989 // The SHT_REL version of Output_data_reloc.
991 template<bool dynamic
, int size
, bool big_endian
>
992 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
993 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
996 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1000 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1001 typedef typename
Output_reloc_type::Address Address
;
1004 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
1007 // Add a reloc against a global symbol.
1010 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1011 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1014 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1015 unsigned int shndx
, Address address
)
1016 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1019 // Add a RELATIVE reloc against a global symbol. The final relocation
1020 // will not reference the symbol.
1023 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1025 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1028 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1029 Relobj
* relobj
, unsigned int shndx
, Address address
)
1030 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1033 // Add a reloc against a local symbol.
1036 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1037 unsigned int local_sym_index
, unsigned int type
,
1038 Output_data
* od
, Address address
)
1039 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1043 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1044 unsigned int local_sym_index
, unsigned int type
,
1045 Output_data
* od
, unsigned int shndx
, Address address
)
1046 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1049 // Add a RELATIVE reloc against a local symbol.
1052 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1053 unsigned int local_sym_index
, unsigned int type
,
1054 Output_data
* od
, Address address
)
1055 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1059 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1060 unsigned int local_sym_index
, unsigned int type
,
1061 Output_data
* od
, unsigned int shndx
, Address address
)
1062 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1065 // A reloc against the STT_SECTION symbol of an output section.
1066 // OS is the Output_section that the relocation refers to; OD is
1067 // the Output_data object being relocated.
1070 add_output_section(Output_section
* os
, unsigned int type
,
1071 Output_data
* od
, Address address
)
1072 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1075 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1076 Relobj
* relobj
, unsigned int shndx
, Address address
)
1077 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1080 // The SHT_RELA version of Output_data_reloc.
1082 template<bool dynamic
, int size
, bool big_endian
>
1083 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1084 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1087 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1091 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1092 typedef typename
Output_reloc_type::Address Address
;
1093 typedef typename
Output_reloc_type::Addend Addend
;
1096 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
1099 // Add a reloc against a global symbol.
1102 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1103 Address address
, Addend addend
)
1104 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1108 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1109 unsigned int shndx
, Address address
,
1111 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1114 // Add a RELATIVE reloc against a global symbol. The final output
1115 // relocation will not reference the symbol, but we must keep the symbol
1116 // information long enough to set the addend of the relocation correctly
1117 // when it is written.
1120 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1121 Address address
, Addend addend
)
1122 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1125 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1126 Relobj
* relobj
, unsigned int shndx
, Address address
,
1128 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1131 // Add a reloc against a local symbol.
1134 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1135 unsigned int local_sym_index
, unsigned int type
,
1136 Output_data
* od
, Address address
, Addend addend
)
1138 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1143 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1144 unsigned int local_sym_index
, unsigned int type
,
1145 Output_data
* od
, unsigned int shndx
, Address address
,
1148 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1149 address
, addend
, false));
1152 // Add a RELATIVE reloc against a local symbol.
1155 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1156 unsigned int local_sym_index
, unsigned int type
,
1157 Output_data
* od
, Address address
, Addend addend
)
1159 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1164 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1165 unsigned int local_sym_index
, unsigned int type
,
1166 Output_data
* od
, unsigned int shndx
, Address address
,
1169 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1170 address
, addend
, true));
1173 // A reloc against the STT_SECTION symbol of an output section.
1176 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1177 Address address
, Addend addend
)
1178 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1181 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1182 unsigned int shndx
, Address address
, Addend addend
)
1183 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1187 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1188 // for one symbol--either a global symbol or a local symbol in an
1189 // object. The target specific code adds entries to the GOT as
1192 template<int size
, bool big_endian
>
1193 class Output_data_got
: public Output_section_data_build
1196 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1197 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1198 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1201 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1205 // Add an entry for a global symbol to the GOT. Return true if this
1206 // is a new GOT entry, false if the symbol was already in the GOT.
1208 add_global(Symbol
* gsym
);
1210 // Add an entry for a global symbol to the GOT, and add a dynamic
1211 // relocation of type R_TYPE for the GOT entry.
1213 add_global_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
, unsigned int r_type
);
1216 add_global_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
, unsigned int r_type
);
1218 // Add an entry for a local symbol to the GOT. This returns true if
1219 // this is a new GOT entry, false if the symbol already has a GOT
1222 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
1224 // Add an entry for a global symbol to the GOT, and add a dynamic
1225 // relocation of type R_TYPE for the GOT entry.
1227 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1228 unsigned int sym_index
, Rel_dyn
* rel_dyn
,
1229 unsigned int r_type
);
1232 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1233 unsigned int sym_index
, Rela_dyn
* rela_dyn
,
1234 unsigned int r_type
);
1236 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1237 // Return true if this is a new GOT entry, false if the symbol was
1238 // already in the GOT.
1240 add_global_tls(Symbol
* gsym
, bool need_pair
);
1242 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1243 // relocation of type R_TYPE.
1245 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1246 unsigned int r_type
);
1249 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1250 unsigned int r_type
);
1252 // Add a pair of entries for a global TLS symbol to the GOT, and add
1253 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1255 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1256 unsigned int mod_r_type
,
1257 unsigned int dtv_r_type
);
1260 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1261 unsigned int mod_r_type
,
1262 unsigned int dtv_r_type
);
1264 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1265 // This returns true if this is a new GOT entry, false if the symbol
1266 // already has a GOT entry.
1268 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
1269 unsigned int sym_index
, bool need_pair
);
1271 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1272 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1273 // Because this is a local symbol, the first GOT entry can be relocated
1274 // relative to a section symbol, and the second GOT entry will have an
1275 // dtv-relative value that can be computed at link time.
1277 add_local_tls_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1278 unsigned int sym_index
, unsigned int shndx
,
1279 bool need_pair
, Rel_dyn
* rel_dyn
,
1280 unsigned int r_type
);
1283 add_local_tls_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1284 unsigned int sym_index
, unsigned int shndx
,
1285 bool need_pair
, Rela_dyn
* rela_dyn
,
1286 unsigned int r_type
);
1288 // Add a constant to the GOT. This returns the offset of the new
1289 // entry from the start of the GOT.
1291 add_constant(Valtype constant
)
1293 this->entries_
.push_back(Got_entry(constant
));
1294 this->set_got_size();
1295 return this->last_got_offset();
1299 // Write out the GOT table.
1301 do_write(Output_file
*);
1304 // This POD class holds a single GOT entry.
1308 // Create a zero entry.
1310 : local_sym_index_(CONSTANT_CODE
)
1311 { this->u_
.constant
= 0; }
1313 // Create a global symbol entry.
1314 explicit Got_entry(Symbol
* gsym
)
1315 : local_sym_index_(GSYM_CODE
)
1316 { this->u_
.gsym
= gsym
; }
1318 // Create a local symbol entry.
1319 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1320 unsigned int local_sym_index
)
1321 : local_sym_index_(local_sym_index
)
1323 gold_assert(local_sym_index
!= GSYM_CODE
1324 && local_sym_index
!= CONSTANT_CODE
);
1325 this->u_
.object
= object
;
1328 // Create a constant entry. The constant is a host value--it will
1329 // be swapped, if necessary, when it is written out.
1330 explicit Got_entry(Valtype constant
)
1331 : local_sym_index_(CONSTANT_CODE
)
1332 { this->u_
.constant
= constant
; }
1334 // Write the GOT entry to an output view.
1336 write(unsigned char* pov
) const;
1347 // For a local symbol, the object.
1348 Sized_relobj
<size
, big_endian
>* object
;
1349 // For a global symbol, the symbol.
1351 // For a constant, the constant.
1354 // For a local symbol, the local symbol index. This is GSYM_CODE
1355 // for a global symbol, or CONSTANT_CODE for a constant.
1356 unsigned int local_sym_index_
;
1359 typedef std::vector
<Got_entry
> Got_entries
;
1361 // Return the offset into the GOT of GOT entry I.
1363 got_offset(unsigned int i
) const
1364 { return i
* (size
/ 8); }
1366 // Return the offset into the GOT of the last entry added.
1368 last_got_offset() const
1369 { return this->got_offset(this->entries_
.size() - 1); }
1371 // Set the size of the section.
1374 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1376 // The list of GOT entries.
1377 Got_entries entries_
;
1380 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1383 class Output_data_dynamic
: public Output_section_data
1386 Output_data_dynamic(Stringpool
* pool
)
1387 : Output_section_data(Output_data::default_alignment()),
1388 entries_(), pool_(pool
)
1391 // Add a new dynamic entry with a fixed numeric value.
1393 add_constant(elfcpp::DT tag
, unsigned int val
)
1394 { this->add_entry(Dynamic_entry(tag
, val
)); }
1396 // Add a new dynamic entry with the address of output data.
1398 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1399 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1401 // Add a new dynamic entry with the size of output data.
1403 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1404 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1406 // Add a new dynamic entry with the address of a symbol.
1408 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1409 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1411 // Add a new dynamic entry with a string.
1413 add_string(elfcpp::DT tag
, const char* str
)
1414 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1417 add_string(elfcpp::DT tag
, const std::string
& str
)
1418 { this->add_string(tag
, str
.c_str()); }
1421 // Adjust the output section to set the entry size.
1423 do_adjust_output_section(Output_section
*);
1425 // Set the final data size.
1427 set_final_data_size();
1429 // Write out the dynamic entries.
1431 do_write(Output_file
*);
1434 // This POD class holds a single dynamic entry.
1438 // Create an entry with a fixed numeric value.
1439 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1440 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1441 { this->u_
.val
= val
; }
1443 // Create an entry with the size or address of a section.
1444 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1446 classification_(section_size
1447 ? DYNAMIC_SECTION_SIZE
1448 : DYNAMIC_SECTION_ADDRESS
)
1449 { this->u_
.od
= od
; }
1451 // Create an entry with the address of a symbol.
1452 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1453 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1454 { this->u_
.sym
= sym
; }
1456 // Create an entry with a string.
1457 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1458 : tag_(tag
), classification_(DYNAMIC_STRING
)
1459 { this->u_
.str
= str
; }
1461 // Write the dynamic entry to an output view.
1462 template<int size
, bool big_endian
>
1464 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1472 DYNAMIC_SECTION_ADDRESS
,
1474 DYNAMIC_SECTION_SIZE
,
1483 // For DYNAMIC_NUMBER.
1485 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1486 const Output_data
* od
;
1487 // For DYNAMIC_SYMBOL.
1489 // For DYNAMIC_STRING.
1494 // The type of entry.
1495 Classification classification_
;
1498 // Add an entry to the list.
1500 add_entry(const Dynamic_entry
& entry
)
1501 { this->entries_
.push_back(entry
); }
1503 // Sized version of write function.
1504 template<int size
, bool big_endian
>
1506 sized_write(Output_file
* of
);
1508 // The type of the list of entries.
1509 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1512 Dynamic_entries entries_
;
1513 // The pool used for strings.
1517 // An output section. We don't expect to have too many output
1518 // sections, so we don't bother to do a template on the size.
1520 class Output_section
: public Output_data
1523 // Create an output section, giving the name, type, and flags.
1524 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1525 virtual ~Output_section();
1527 // Add a new input section SHNDX, named NAME, with header SHDR, from
1528 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1529 // which applies to this section, or 0 if none, or -1U if more than
1530 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1531 // in a linker script; in that case we need to keep track of input
1532 // sections associated with an output section. Return the offset
1533 // within the output section.
1534 template<int size
, bool big_endian
>
1536 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1538 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1539 unsigned int reloc_shndx
, bool have_sections_script
);
1541 // Add generated data POSD to this output section.
1543 add_output_section_data(Output_section_data
* posd
);
1545 // Return the section name.
1548 { return this->name_
; }
1550 // Return the section type.
1553 { return this->type_
; }
1555 // Return the section flags.
1558 { return this->flags_
; }
1560 // Return the entsize field.
1563 { return this->entsize_
; }
1565 // Set the entsize field.
1567 set_entsize(uint64_t v
);
1569 // Set the load address.
1571 set_load_address(uint64_t load_address
)
1573 this->load_address_
= load_address
;
1574 this->has_load_address_
= true;
1577 // Set the link field to the output section index of a section.
1579 set_link_section(const Output_data
* od
)
1581 gold_assert(this->link_
== 0
1582 && !this->should_link_to_symtab_
1583 && !this->should_link_to_dynsym_
);
1584 this->link_section_
= od
;
1587 // Set the link field to a constant.
1589 set_link(unsigned int v
)
1591 gold_assert(this->link_section_
== NULL
1592 && !this->should_link_to_symtab_
1593 && !this->should_link_to_dynsym_
);
1597 // Record that this section should link to the normal symbol table.
1599 set_should_link_to_symtab()
1601 gold_assert(this->link_section_
== NULL
1603 && !this->should_link_to_dynsym_
);
1604 this->should_link_to_symtab_
= true;
1607 // Record that this section should link to the dynamic symbol table.
1609 set_should_link_to_dynsym()
1611 gold_assert(this->link_section_
== NULL
1613 && !this->should_link_to_symtab_
);
1614 this->should_link_to_dynsym_
= true;
1617 // Return the info field.
1621 gold_assert(this->info_section_
== NULL
);
1625 // Set the info field to the output section index of a section.
1627 set_info_section(const Output_data
* od
)
1629 gold_assert(this->info_
== 0);
1630 this->info_section_
= od
;
1633 // Set the info field to a constant.
1635 set_info(unsigned int v
)
1637 gold_assert(this->info_section_
== NULL
);
1641 // Set the addralign field.
1643 set_addralign(uint64_t v
)
1644 { this->addralign_
= v
; }
1646 // Indicate that we need a symtab index.
1648 set_needs_symtab_index()
1649 { this->needs_symtab_index_
= true; }
1651 // Return whether we need a symtab index.
1653 needs_symtab_index() const
1654 { return this->needs_symtab_index_
; }
1656 // Get the symtab index.
1658 symtab_index() const
1660 gold_assert(this->symtab_index_
!= 0);
1661 return this->symtab_index_
;
1664 // Set the symtab index.
1666 set_symtab_index(unsigned int index
)
1668 gold_assert(index
!= 0);
1669 this->symtab_index_
= index
;
1672 // Indicate that we need a dynsym index.
1674 set_needs_dynsym_index()
1675 { this->needs_dynsym_index_
= true; }
1677 // Return whether we need a dynsym index.
1679 needs_dynsym_index() const
1680 { return this->needs_dynsym_index_
; }
1682 // Get the dynsym index.
1684 dynsym_index() const
1686 gold_assert(this->dynsym_index_
!= 0);
1687 return this->dynsym_index_
;
1690 // Set the dynsym index.
1692 set_dynsym_index(unsigned int index
)
1694 gold_assert(index
!= 0);
1695 this->dynsym_index_
= index
;
1698 // Return whether this section should be written after all the input
1699 // sections are complete.
1701 after_input_sections() const
1702 { return this->after_input_sections_
; }
1704 // Record that this section should be written after all the input
1705 // sections are complete.
1707 set_after_input_sections()
1708 { this->after_input_sections_
= true; }
1710 // Return whether this section requires postprocessing after all
1711 // relocations have been applied.
1713 requires_postprocessing() const
1714 { return this->requires_postprocessing_
; }
1716 // If a section requires postprocessing, return the buffer to use.
1718 postprocessing_buffer() const
1720 gold_assert(this->postprocessing_buffer_
!= NULL
);
1721 return this->postprocessing_buffer_
;
1724 // If a section requires postprocessing, create the buffer to use.
1726 create_postprocessing_buffer();
1728 // If a section requires postprocessing, this is the size of the
1729 // buffer to which relocations should be applied.
1731 postprocessing_buffer_size() const
1732 { return this->current_data_size_for_child(); }
1734 // Return whether the offset OFFSET in the input section SHNDX in
1735 // object OBJECT is being included in the link.
1737 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1738 off_t offset
) const;
1740 // Return the offset within the output section of OFFSET relative to
1741 // the start of input section SHNDX in object OBJECT.
1743 output_offset(const Relobj
* object
, unsigned int shndx
,
1744 section_offset_type offset
) const;
1746 // Return the output virtual address of OFFSET relative to the start
1747 // of input section SHNDX in object OBJECT.
1749 output_address(const Relobj
* object
, unsigned int shndx
,
1750 off_t offset
) const;
1752 // Return the output address of the start of the merged section for
1753 // input section SHNDX in object OBJECT. This is not necessarily
1754 // the offset corresponding to input offset 0 in the section, since
1755 // the section may be mapped arbitrarily.
1757 starting_output_address(const Relobj
* object
, unsigned int shndx
) const;
1759 // Record that this output section was found in the SECTIONS clause
1760 // of a linker script.
1762 set_found_in_sections_clause()
1763 { this->found_in_sections_clause_
= true; }
1765 // Return whether this output section was found in the SECTIONS
1766 // clause of a linker script.
1768 found_in_sections_clause() const
1769 { return this->found_in_sections_clause_
; }
1771 // Write the section header into *OPHDR.
1772 template<int size
, bool big_endian
>
1774 write_header(const Layout
*, const Stringpool
*,
1775 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1777 // The next few calls are for linker script support.
1779 // Store the list of input sections for this Output_section into the
1780 // list passed in. This removes the input sections, leaving only
1781 // any Output_section_data elements. This returns the size of those
1782 // Output_section_data elements. ADDRESS is the address of this
1783 // output section. FILL is the fill value to use, in case there are
1784 // any spaces between the remaining Output_section_data elements.
1786 get_input_sections(uint64_t address
, const std::string
& fill
,
1787 std::list
<std::pair
<Relobj
*, unsigned int > >*);
1789 // Add an input section from a script.
1791 add_input_section_for_script(Relobj
* object
, unsigned int shndx
,
1792 off_t data_size
, uint64_t addralign
);
1794 // Set the current size of the output section.
1796 set_current_data_size(off_t size
)
1797 { this->set_current_data_size_for_child(size
); }
1799 // Get the current size of the output section.
1801 current_data_size() const
1802 { return this->current_data_size_for_child(); }
1804 // End of linker script support.
1806 // Print merge statistics to stderr.
1808 print_merge_stats();
1811 // Return the section index in the output file.
1813 do_out_shndx() const
1815 gold_assert(this->out_shndx_
!= -1U);
1816 return this->out_shndx_
;
1819 // Set the output section index.
1821 do_set_out_shndx(unsigned int shndx
)
1823 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
1824 this->out_shndx_
= shndx
;
1827 // Set the final data size of the Output_section. For a typical
1828 // Output_section, there is nothing to do, but if there are any
1829 // Output_section_data objects we need to set their final addresses
1832 set_final_data_size();
1834 // Reset the address and file offset.
1836 do_reset_address_and_file_offset();
1838 // Write the data to the file. For a typical Output_section, this
1839 // does nothing: the data is written out by calling Object::Relocate
1840 // on each input object. But if there are any Output_section_data
1841 // objects we do need to write them out here.
1843 do_write(Output_file
*);
1845 // Return the address alignment--function required by parent class.
1847 do_addralign() const
1848 { return this->addralign_
; }
1850 // Return whether there is a load address.
1852 do_has_load_address() const
1853 { return this->has_load_address_
; }
1855 // Return the load address.
1857 do_load_address() const
1859 gold_assert(this->has_load_address_
);
1860 return this->load_address_
;
1863 // Return whether this is an Output_section.
1865 do_is_section() const
1868 // Return whether this is a section of the specified type.
1870 do_is_section_type(elfcpp::Elf_Word type
) const
1871 { return this->type_
== type
; }
1873 // Return whether the specified section flag is set.
1875 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1876 { return (this->flags_
& flag
) != 0; }
1878 // Set the TLS offset. Called only for SHT_TLS sections.
1880 do_set_tls_offset(uint64_t tls_base
);
1882 // Return the TLS offset, relative to the base of the TLS segment.
1883 // Valid only for SHT_TLS sections.
1885 do_tls_offset() const
1886 { return this->tls_offset_
; }
1888 // Modify the section name. This is only permitted for an
1889 // unallocated section, and only before the size has been finalized.
1890 // Otherwise the name will not get into Layout::namepool_.
1892 set_name(const char* newname
)
1894 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
1895 gold_assert(!this->is_data_size_valid());
1896 this->name_
= newname
;
1899 // This may be implemented by a child class.
1901 do_finalize_name(Layout
*)
1904 // Record that this section requires postprocessing after all
1905 // relocations have been applied. This is called by a child class.
1907 set_requires_postprocessing()
1909 this->requires_postprocessing_
= true;
1910 this->after_input_sections_
= true;
1913 // Write all the data of an Output_section into the postprocessing
1916 write_to_postprocessing_buffer();
1919 // In some cases we need to keep a list of the input sections
1920 // associated with this output section. We only need the list if we
1921 // might have to change the offsets of the input section within the
1922 // output section after we add the input section. The ordinary
1923 // input sections will be written out when we process the object
1924 // file, and as such we don't need to track them here. We do need
1925 // to track Output_section_data objects here. We store instances of
1926 // this structure in a std::vector, so it must be a POD. There can
1927 // be many instances of this structure, so we use a union to save
1933 : shndx_(0), p2align_(0)
1935 this->u1_
.data_size
= 0;
1936 this->u2_
.object
= NULL
;
1939 // For an ordinary input section.
1940 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1943 p2align_(ffsll(static_cast<long long>(addralign
)))
1945 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1946 && shndx
!= MERGE_DATA_SECTION_CODE
1947 && shndx
!= MERGE_STRING_SECTION_CODE
);
1948 this->u1_
.data_size
= data_size
;
1949 this->u2_
.object
= object
;
1952 // For a non-merge output section.
1953 Input_section(Output_section_data
* posd
)
1954 : shndx_(OUTPUT_SECTION_CODE
),
1955 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1957 this->u1_
.data_size
= 0;
1958 this->u2_
.posd
= posd
;
1961 // For a merge section.
1962 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1964 ? MERGE_STRING_SECTION_CODE
1965 : MERGE_DATA_SECTION_CODE
),
1966 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1968 this->u1_
.entsize
= entsize
;
1969 this->u2_
.posd
= posd
;
1972 // The required alignment.
1976 return (this->p2align_
== 0
1978 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1981 // Return the required size.
1985 // Whether this is an input section.
1987 is_input_section() const
1989 return (this->shndx_
!= OUTPUT_SECTION_CODE
1990 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1991 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1994 // Return whether this is a merge section which matches the
1997 is_merge_section(bool is_string
, uint64_t entsize
,
1998 uint64_t addralign
) const
2000 return (this->shndx_
== (is_string
2001 ? MERGE_STRING_SECTION_CODE
2002 : MERGE_DATA_SECTION_CODE
)
2003 && this->u1_
.entsize
== entsize
2004 && this->addralign() == addralign
);
2007 // Return the object for an input section.
2011 gold_assert(this->is_input_section());
2012 return this->u2_
.object
;
2015 // Return the input section index for an input section.
2019 gold_assert(this->is_input_section());
2020 return this->shndx_
;
2023 // Set the output section.
2025 set_output_section(Output_section
* os
)
2027 gold_assert(!this->is_input_section());
2028 this->u2_
.posd
->set_output_section(os
);
2031 // Set the address and file offset. This is called during
2032 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2033 // the enclosing section.
2035 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2036 off_t section_file_offset
);
2038 // Reset the address and file offset.
2040 reset_address_and_file_offset();
2042 // Finalize the data size.
2044 finalize_data_size();
2046 // Add an input section, for SHF_MERGE sections.
2048 add_input_section(Relobj
* object
, unsigned int shndx
)
2050 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2051 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2052 return this->u2_
.posd
->add_input_section(object
, shndx
);
2055 // Given an input OBJECT, an input section index SHNDX within that
2056 // object, and an OFFSET relative to the start of that input
2057 // section, return whether or not the output offset is known. If
2058 // this function returns true, it sets *POUTPUT to the offset in
2059 // the output section, relative to the start of the input section
2060 // in the output section. *POUTPUT may be different from OFFSET
2061 // for a merged section.
2063 output_offset(const Relobj
* object
, unsigned int shndx
,
2064 section_offset_type offset
,
2065 section_offset_type
*poutput
) const;
2067 // Return whether this is the merge section for the input section
2070 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2072 // Write out the data. This does nothing for an input section.
2074 write(Output_file
*);
2076 // Write the data to a buffer. This does nothing for an input
2079 write_to_buffer(unsigned char*);
2081 // Print statistics about merge sections to stderr.
2083 print_merge_stats(const char* section_name
)
2085 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2086 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2087 this->u2_
.posd
->print_merge_stats(section_name
);
2091 // Code values which appear in shndx_. If the value is not one of
2092 // these codes, it is the input section index in the object file.
2095 // An Output_section_data.
2096 OUTPUT_SECTION_CODE
= -1U,
2097 // An Output_section_data for an SHF_MERGE section with
2098 // SHF_STRINGS not set.
2099 MERGE_DATA_SECTION_CODE
= -2U,
2100 // An Output_section_data for an SHF_MERGE section with
2102 MERGE_STRING_SECTION_CODE
= -3U
2105 // For an ordinary input section, this is the section index in the
2106 // input file. For an Output_section_data, this is
2107 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2108 // MERGE_STRING_SECTION_CODE.
2109 unsigned int shndx_
;
2110 // The required alignment, stored as a power of 2.
2111 unsigned int p2align_
;
2114 // For an ordinary input section, the section size.
2116 // For OUTPUT_SECTION_CODE, this is not used. For
2117 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2123 // For an ordinary input section, the object which holds the
2126 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2127 // MERGE_STRING_SECTION_CODE, the data.
2128 Output_section_data
* posd
;
2132 typedef std::vector
<Input_section
> Input_section_list
;
2134 // Fill data. This is used to fill in data between input sections.
2135 // It is also used for data statements (BYTE, WORD, etc.) in linker
2136 // scripts. When we have to keep track of the input sections, we
2137 // can use an Output_data_const, but we don't want to have to keep
2138 // track of input sections just to implement fills.
2142 Fill(off_t section_offset
, off_t length
)
2143 : section_offset_(section_offset
),
2144 length_(convert_to_section_size_type(length
))
2147 // Return section offset.
2149 section_offset() const
2150 { return this->section_offset_
; }
2152 // Return fill length.
2155 { return this->length_
; }
2158 // The offset within the output section.
2159 off_t section_offset_
;
2160 // The length of the space to fill.
2161 section_size_type length_
;
2164 typedef std::vector
<Fill
> Fill_list
;
2166 // Add a new output section by Input_section.
2168 add_output_section_data(Input_section
*);
2170 // Add an SHF_MERGE input section. Returns true if the section was
2173 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
2174 uint64_t entsize
, uint64_t addralign
);
2176 // Add an output SHF_MERGE section POSD to this output section.
2177 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2178 // ENTSIZE is the entity size. This returns the entry added to
2181 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
2184 // Most of these fields are only valid after layout.
2186 // The name of the section. This will point into a Stringpool.
2188 // The section address is in the parent class.
2189 // The section alignment.
2190 uint64_t addralign_
;
2191 // The section entry size.
2193 // The load address. This is only used when using a linker script
2194 // with a SECTIONS clause. The has_load_address_ field indicates
2195 // whether this field is valid.
2196 uint64_t load_address_
;
2197 // The file offset is in the parent class.
2198 // Set the section link field to the index of this section.
2199 const Output_data
* link_section_
;
2200 // If link_section_ is NULL, this is the link field.
2202 // Set the section info field to the index of this section.
2203 const Output_data
* info_section_
;
2204 // If info_section_ is NULL, this is the section info field.
2206 // The section type.
2207 const elfcpp::Elf_Word type_
;
2208 // The section flags.
2209 elfcpp::Elf_Xword flags_
;
2210 // The section index.
2211 unsigned int out_shndx_
;
2212 // If there is a STT_SECTION for this output section in the normal
2213 // symbol table, this is the symbol index. This starts out as zero.
2214 // It is initialized in Layout::finalize() to be the index, or -1U
2215 // if there isn't one.
2216 unsigned int symtab_index_
;
2217 // If there is a STT_SECTION for this output section in the dynamic
2218 // symbol table, this is the symbol index. This starts out as zero.
2219 // It is initialized in Layout::finalize() to be the index, or -1U
2220 // if there isn't one.
2221 unsigned int dynsym_index_
;
2222 // The input sections. This will be empty in cases where we don't
2223 // need to keep track of them.
2224 Input_section_list input_sections_
;
2225 // The offset of the first entry in input_sections_.
2226 off_t first_input_offset_
;
2227 // The fill data. This is separate from input_sections_ because we
2228 // often will need fill sections without needing to keep track of
2231 // If the section requires postprocessing, this buffer holds the
2232 // section contents during relocation.
2233 unsigned char* postprocessing_buffer_
;
2234 // Whether this output section needs a STT_SECTION symbol in the
2235 // normal symbol table. This will be true if there is a relocation
2237 bool needs_symtab_index_
: 1;
2238 // Whether this output section needs a STT_SECTION symbol in the
2239 // dynamic symbol table. This will be true if there is a dynamic
2240 // relocation which needs it.
2241 bool needs_dynsym_index_
: 1;
2242 // Whether the link field of this output section should point to the
2243 // normal symbol table.
2244 bool should_link_to_symtab_
: 1;
2245 // Whether the link field of this output section should point to the
2246 // dynamic symbol table.
2247 bool should_link_to_dynsym_
: 1;
2248 // Whether this section should be written after all the input
2249 // sections are complete.
2250 bool after_input_sections_
: 1;
2251 // Whether this section requires post processing after all
2252 // relocations have been applied.
2253 bool requires_postprocessing_
: 1;
2254 // Whether an input section was mapped to this output section
2255 // because of a SECTIONS clause in a linker script.
2256 bool found_in_sections_clause_
: 1;
2257 // Whether this section has an explicitly specified load address.
2258 bool has_load_address_
: 1;
2259 // For SHT_TLS sections, the offset of this section relative to the base
2260 // of the TLS segment.
2261 uint64_t tls_offset_
;
2264 // An output segment. PT_LOAD segments are built from collections of
2265 // output sections. Other segments typically point within PT_LOAD
2266 // segments, and are built directly as needed.
2268 class Output_segment
2271 // Create an output segment, specifying the type and flags.
2272 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2274 // Return the virtual address.
2277 { return this->vaddr_
; }
2279 // Return the physical address.
2282 { return this->paddr_
; }
2284 // Return the segment type.
2287 { return this->type_
; }
2289 // Return the segment flags.
2292 { return this->flags_
; }
2294 // Return the memory size.
2297 { return this->memsz_
; }
2299 // Return the file size.
2302 { return this->filesz_
; }
2304 // Return the maximum alignment of the Output_data.
2306 maximum_alignment();
2308 // Add an Output_section to this segment.
2310 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2311 { this->add_output_section(os
, seg_flags
, false); }
2313 // Add an Output_section to the start of this segment.
2315 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2316 { this->add_output_section(os
, seg_flags
, true); }
2318 // Add an Output_data (which is not an Output_section) to the start
2321 add_initial_output_data(Output_data
*);
2323 // Return the number of dynamic relocations applied to this segment.
2325 dynamic_reloc_count() const;
2327 // Return the address of the first section.
2329 first_section_load_address() const;
2331 // Return whether the addresses have been set already.
2333 are_addresses_set() const
2334 { return this->are_addresses_set_
; }
2336 // Set the addresses.
2338 set_addresses(uint64_t vaddr
, uint64_t paddr
)
2340 this->vaddr_
= vaddr
;
2341 this->paddr_
= paddr
;
2342 this->are_addresses_set_
= true;
2345 // Set the address of the segment to ADDR and the offset to *POFF
2346 // and set the addresses and offsets of all contained output
2347 // sections accordingly. Set the section indexes of all contained
2348 // output sections starting with *PSHNDX. If RESET is true, first
2349 // reset the addresses of the contained sections. Return the
2350 // address of the immediately following segment. Update *POFF and
2351 // *PSHNDX. This should only be called for a PT_LOAD segment.
2353 set_section_addresses(bool reset
, uint64_t addr
, off_t
* poff
,
2354 unsigned int* pshndx
);
2356 // Set the minimum alignment of this segment. This may be adjusted
2357 // upward based on the section alignments.
2359 set_minimum_p_align(uint64_t align
)
2360 { this->min_p_align_
= align
; }
2362 // Set the offset of this segment based on the section. This should
2363 // only be called for a non-PT_LOAD segment.
2367 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2371 // Return the number of output sections.
2373 output_section_count() const;
2375 // Write the segment header into *OPHDR.
2376 template<int size
, bool big_endian
>
2378 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2380 // Write the section headers of associated sections into V.
2381 template<int size
, bool big_endian
>
2383 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2384 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
2387 Output_segment(const Output_segment
&);
2388 Output_segment
& operator=(const Output_segment
&);
2390 typedef std::list
<Output_data
*> Output_data_list
;
2392 // Add an Output_section to this segment, specifying front or back.
2394 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2397 // Find the maximum alignment in an Output_data_list.
2399 maximum_alignment_list(const Output_data_list
*);
2401 // Set the section addresses in an Output_data_list.
2403 set_section_list_addresses(bool reset
, Output_data_list
*, uint64_t addr
,
2404 off_t
* poff
, unsigned int* pshndx
);
2406 // Return the number of Output_sections in an Output_data_list.
2408 output_section_count_list(const Output_data_list
*) const;
2410 // Return the number of dynamic relocs in an Output_data_list.
2412 dynamic_reloc_count_list(const Output_data_list
*) const;
2414 // Write the section headers in the list into V.
2415 template<int size
, bool big_endian
>
2417 write_section_headers_list(const Layout
*, const Stringpool
*,
2418 const Output_data_list
*, unsigned char* v
,
2419 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
2421 // The list of output data with contents attached to this segment.
2422 Output_data_list output_data_
;
2423 // The list of output data without contents attached to this segment.
2424 Output_data_list output_bss_
;
2425 // The segment virtual address.
2427 // The segment physical address.
2429 // The size of the segment in memory.
2431 // The maximum section alignment. The is_max_align_known_ field
2432 // indicates whether this has been finalized.
2433 uint64_t max_align_
;
2434 // The required minimum value for the p_align field. This is used
2435 // for PT_LOAD segments. Note that this does not mean that
2436 // addresses should be aligned to this value; it means the p_paddr
2437 // and p_vaddr fields must be congruent modulo this value. For
2438 // non-PT_LOAD segments, the dynamic linker works more efficiently
2439 // if the p_align field has the more conventional value, although it
2440 // can align as needed.
2441 uint64_t min_p_align_
;
2442 // The offset of the segment data within the file.
2444 // The size of the segment data in the file.
2446 // The segment type;
2447 elfcpp::Elf_Word type_
;
2448 // The segment flags.
2449 elfcpp::Elf_Word flags_
;
2450 // Whether we have finalized max_align_.
2451 bool is_max_align_known_
: 1;
2452 // Whether vaddr and paddr were set by a linker script.
2453 bool are_addresses_set_
: 1;
2456 // This class represents the output file.
2461 Output_file(const char* name
);
2463 // Open the output file. FILE_SIZE is the final size of the file.
2465 open(off_t file_size
);
2467 // Resize the output file.
2469 resize(off_t file_size
);
2471 // Close the output file (flushing all buffered data) and make sure
2472 // there are no errors.
2476 // We currently always use mmap which makes the view handling quite
2477 // simple. In the future we may support other approaches.
2479 // Write data to the output file.
2481 write(off_t offset
, const void* data
, size_t len
)
2482 { memcpy(this->base_
+ offset
, data
, len
); }
2484 // Get a buffer to use to write to the file, given the offset into
2485 // the file and the size.
2487 get_output_view(off_t start
, size_t size
)
2489 gold_assert(start
>= 0
2490 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
2491 return this->base_
+ start
;
2494 // VIEW must have been returned by get_output_view. Write the
2495 // buffer to the file, passing in the offset and the size.
2497 write_output_view(off_t
, size_t, unsigned char*)
2500 // Get a read/write buffer. This is used when we want to write part
2501 // of the file, read it in, and write it again.
2503 get_input_output_view(off_t start
, size_t size
)
2504 { return this->get_output_view(start
, size
); }
2506 // Write a read/write buffer back to the file.
2508 write_input_output_view(off_t
, size_t, unsigned char*)
2511 // Get a read buffer. This is used when we just want to read part
2512 // of the file back it in.
2513 const unsigned char*
2514 get_input_view(off_t start
, size_t size
)
2515 { return this->get_output_view(start
, size
); }
2517 // Release a read bfufer.
2519 free_input_view(off_t
, size_t, const unsigned char*)
2523 // Map the file into memory and return a pointer to the map.
2527 // Unmap the file from memory (and flush to disk buffers).
2537 // Base of file mapped into memory.
2538 unsigned char* base_
;
2539 // True iff base_ points to a memory buffer rather than an output file.
2540 bool map_is_anonymous_
;
2543 } // End namespace gold.
2545 #endif // !defined(GOLD_OUTPUT_H)