1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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
;
41 class Relocatable_relocs
;
43 template<int size
, bool big_endian
>
45 template<int size
, bool big_endian
>
48 // An abtract class for data which has to go into the output file.
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
68 gold_assert(this->is_address_valid_
);
69 return this->address_
;
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
78 gold_assert(this->is_data_size_valid_
);
79 return this->data_size_
;
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
88 gold_assert(this->is_offset_valid_
);
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
95 reset_address_and_file_offset()
97 this->is_address_valid_
= false;
98 this->is_offset_valid_
= false;
99 this->is_data_size_valid_
= false;
100 this->do_reset_address_and_file_offset();
103 // Return the required alignment.
106 { return this->do_addralign(); }
108 // Return whether this has a load address.
110 has_load_address() const
111 { return this->do_has_load_address(); }
113 // Return the load address.
116 { return this->do_load_address(); }
118 // Return whether this is an Output_section.
121 { return this->do_is_section(); }
123 // Return whether this is an Output_section of the specified type.
125 is_section_type(elfcpp::Elf_Word stt
) const
126 { return this->do_is_section_type(stt
); }
128 // Return whether this is an Output_section with the specified flag
131 is_section_flag_set(elfcpp::Elf_Xword shf
) const
132 { return this->do_is_section_flag_set(shf
); }
134 // Return the output section that this goes in, if there is one.
137 { return this->do_output_section(); }
139 // Return the output section index, if there is an output section.
142 { return this->do_out_shndx(); }
144 // Set the output section index, if this is an output section.
146 set_out_shndx(unsigned int shndx
)
147 { this->do_set_out_shndx(shndx
); }
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
153 set_address_and_file_offset(uint64_t addr
, off_t off
)
155 this->set_address(addr
);
156 this->set_file_offset(off
);
157 this->finalize_data_size();
162 set_address(uint64_t addr
)
164 gold_assert(!this->is_address_valid_
);
165 this->address_
= addr
;
166 this->is_address_valid_
= true;
169 // Set the file offset.
171 set_file_offset(off_t off
)
173 gold_assert(!this->is_offset_valid_
);
175 this->is_offset_valid_
= true;
178 // Finalize the data size.
182 if (!this->is_data_size_valid_
)
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_
);
190 // Set the TLS offset. Called only for SHT_TLS sections.
192 set_tls_offset(uint64_t tls_base
)
193 { this->do_set_tls_offset(tls_base
); }
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
199 { return this->do_tls_offset(); }
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
204 write(Output_file
* file
)
205 { this->do_write(file
); }
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
211 { Output_data::allocated_sizes_are_fixed
= true; }
213 // Used to check that layout has been done.
216 { return Output_data::allocated_sizes_are_fixed
; }
218 // Count the number of dynamic relocations applied to this section.
221 { ++this->dynamic_reloc_count_
; }
223 // Return the number of dynamic relocations applied to this section.
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_
; }
228 // Whether the address is valid.
230 is_address_valid() const
231 { return this->is_address_valid_
; }
233 // Whether the file offset is valid.
235 is_offset_valid() const
236 { return this->is_offset_valid_
; }
238 // Whether the data size is valid.
240 is_data_size_valid() const
241 { return this->is_data_size_valid_
; }
244 // Functions that child classes may or in some cases must implement.
246 // Write the data to the output file.
248 do_write(Output_file
*) = 0;
250 // Return the required alignment.
252 do_addralign() const = 0;
254 // Return whether this has a load address.
256 do_has_load_address() const
259 // Return the load address.
261 do_load_address() const
262 { gold_unreachable(); }
264 // Return whether this is an Output_section.
266 do_is_section() const
269 // Return whether this is an Output_section of the specified type.
270 // This only needs to be implement by Output_section.
272 do_is_section_type(elfcpp::Elf_Word
) const
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
278 do_is_section_flag_set(elfcpp::Elf_Xword
) const
281 // Return the output section, if there is one.
282 virtual Output_section
*
286 // Return the output section index, if there is an output section.
289 { gold_unreachable(); }
291 // Set the output section index, if this is an output section.
293 do_set_out_shndx(unsigned int)
294 { gold_unreachable(); }
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
300 set_final_data_size()
301 { gold_unreachable(); }
303 // A hook for resetting the address and file offset.
305 do_reset_address_and_file_offset()
308 // Set the TLS offset. Called only for SHT_TLS sections.
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
316 do_tls_offset() const
317 { gold_unreachable(); }
319 // Functions that child classes may call.
321 // Set the size of the data.
323 set_data_size(off_t data_size
)
325 gold_assert(!this->is_data_size_valid_
);
326 this->data_size_
= data_size
;
327 this->is_data_size_valid_
= true;
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
333 current_data_size_for_child() const
334 { return this->data_size_
; }
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
339 set_current_data_size_for_child(off_t data_size
)
341 gold_assert(!this->is_data_size_valid_
);
342 this->data_size_
= data_size
;
345 // Return default alignment for the target size.
349 // Return default alignment for a specified size--32 or 64.
351 default_alignment_for_size(int size
);
354 Output_data(const Output_data
&);
355 Output_data
& operator=(const Output_data
&);
357 // This is used for verification, to make sure that we don't try to
358 // change any sizes of allocated sections after we set the section
360 static bool allocated_sizes_are_fixed
;
362 // Memory address in output file.
364 // Size of data in output file.
366 // File offset of contents in output file.
368 // Whether address_ is valid.
369 bool is_address_valid_
;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_
;
372 // Whether offset_ is valid.
373 bool is_offset_valid_
;
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_
;
378 // Output the section headers.
380 class Output_section_headers
: public Output_data
383 Output_section_headers(const Layout
*,
384 const Layout::Segment_list
*,
385 const Layout::Section_list
*,
386 const Layout::Section_list
*,
388 const Output_section
*);
391 // Write the data to the file.
393 do_write(Output_file
*);
395 // Return the required alignment.
398 { return Output_data::default_alignment(); }
401 // Write the data to the file with the right size and endianness.
402 template<int size
, bool big_endian
>
404 do_sized_write(Output_file
*);
406 const Layout
* layout_
;
407 const Layout::Segment_list
* segment_list_
;
408 const Layout::Section_list
* section_list_
;
409 const Layout::Section_list
* unattached_section_list_
;
410 const Stringpool
* secnamepool_
;
411 const Output_section
* shstrtab_section_
;
414 // Output the segment headers.
416 class Output_segment_headers
: public Output_data
419 Output_segment_headers(const Layout::Segment_list
& segment_list
);
422 // Write the data to the file.
424 do_write(Output_file
*);
426 // Return the required alignment.
429 { return Output_data::default_alignment(); }
432 // Write the data to the file with the right size and endianness.
433 template<int size
, bool big_endian
>
435 do_sized_write(Output_file
*);
437 const Layout::Segment_list
& segment_list_
;
440 // Output the ELF file header.
442 class Output_file_header
: public Output_data
445 Output_file_header(const Target
*,
447 const Output_segment_headers
*,
450 // Add information about the section headers. We lay out the ELF
451 // file header before we create the section headers.
452 void set_section_info(const Output_section_headers
*,
453 const Output_section
* shstrtab
);
456 // Write the data to the file.
458 do_write(Output_file
*);
460 // Return the required alignment.
463 { return Output_data::default_alignment(); }
466 // Write the data to the file with the right size and endianness.
467 template<int size
, bool big_endian
>
469 do_sized_write(Output_file
*);
471 // Return the value to use for the entry address.
473 typename
elfcpp::Elf_types
<size
>::Elf_Addr
476 const Target
* target_
;
477 const Symbol_table
* symtab_
;
478 const Output_segment_headers
* segment_header_
;
479 const Output_section_headers
* section_header_
;
480 const Output_section
* shstrtab_
;
484 // Output sections are mainly comprised of input sections. However,
485 // there are cases where we have data to write out which is not in an
486 // input section. Output_section_data is used in such cases. This is
487 // an abstract base class.
489 class Output_section_data
: public Output_data
492 Output_section_data(off_t data_size
, uint64_t addralign
)
493 : Output_data(), output_section_(NULL
), addralign_(addralign
)
494 { this->set_data_size(data_size
); }
496 Output_section_data(uint64_t addralign
)
497 : Output_data(), output_section_(NULL
), addralign_(addralign
)
500 // Return the output section.
501 const Output_section
*
502 output_section() const
503 { return this->output_section_
; }
505 // Record the output section.
507 set_output_section(Output_section
* os
);
509 // Add an input section, for SHF_MERGE sections. This returns true
510 // if the section was handled.
512 add_input_section(Relobj
* object
, unsigned int shndx
)
513 { return this->do_add_input_section(object
, shndx
); }
515 // Given an input OBJECT, an input section index SHNDX within that
516 // object, and an OFFSET relative to the start of that input
517 // section, return whether or not the corresponding offset within
518 // the output section is known. If this function returns true, it
519 // sets *POUTPUT to the output offset. The value -1 indicates that
520 // this input offset is being discarded.
522 output_offset(const Relobj
* object
, unsigned int shndx
,
523 section_offset_type offset
,
524 section_offset_type
*poutput
) const
525 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
527 // Return whether this is the merge section for the input section
528 // SHNDX in OBJECT. This should return true when output_offset
529 // would return true for some values of OFFSET.
531 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
532 { return this->do_is_merge_section_for(object
, shndx
); }
534 // Write the contents to a buffer. This is used for sections which
535 // require postprocessing, such as compression.
537 write_to_buffer(unsigned char* buffer
)
538 { this->do_write_to_buffer(buffer
); }
540 // Print merge stats to stderr. This should only be called for
541 // SHF_MERGE sections.
543 print_merge_stats(const char* section_name
)
544 { this->do_print_merge_stats(section_name
); }
547 // The child class must implement do_write.
549 // The child class may implement specific adjustments to the output
552 do_adjust_output_section(Output_section
*)
555 // May be implemented by child class. Return true if the section
558 do_add_input_section(Relobj
*, unsigned int)
559 { gold_unreachable(); }
561 // The child class may implement output_offset.
563 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
564 section_offset_type
*) const
567 // The child class may implement is_merge_section_for.
569 do_is_merge_section_for(const Relobj
*, unsigned int) const
572 // The child class may implement write_to_buffer. Most child
573 // classes can not appear in a compressed section, and they do not
576 do_write_to_buffer(unsigned char*)
577 { gold_unreachable(); }
579 // Print merge statistics.
581 do_print_merge_stats(const char*)
582 { gold_unreachable(); }
584 // Return the required alignment.
587 { return this->addralign_
; }
589 // Return the output section.
592 { return this->output_section_
; }
594 // Return the section index of the output section.
596 do_out_shndx() const;
598 // Set the alignment.
600 set_addralign(uint64_t addralign
);
603 // The output section for this section.
604 Output_section
* output_section_
;
605 // The required alignment.
609 // Some Output_section_data classes build up their data step by step,
610 // rather than all at once. This class provides an interface for
613 class Output_section_data_build
: public Output_section_data
616 Output_section_data_build(uint64_t addralign
)
617 : Output_section_data(addralign
)
620 // Get the current data size.
622 current_data_size() const
623 { return this->current_data_size_for_child(); }
625 // Set the current data size.
627 set_current_data_size(off_t data_size
)
628 { this->set_current_data_size_for_child(data_size
); }
631 // Set the final data size.
633 set_final_data_size()
634 { this->set_data_size(this->current_data_size_for_child()); }
637 // A simple case of Output_data in which we have constant data to
640 class Output_data_const
: public Output_section_data
643 Output_data_const(const std::string
& data
, uint64_t addralign
)
644 : Output_section_data(data
.size(), addralign
), data_(data
)
647 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
648 : Output_section_data(len
, addralign
), data_(p
, len
)
651 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
652 : Output_section_data(len
, addralign
),
653 data_(reinterpret_cast<const char*>(p
), len
)
657 // Write the data to the output file.
659 do_write(Output_file
*);
661 // Write the data to a buffer.
663 do_write_to_buffer(unsigned char* buffer
)
664 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
670 // Another version of Output_data with constant data, in which the
671 // buffer is allocated by the caller.
673 class Output_data_const_buffer
: public Output_section_data
676 Output_data_const_buffer(const unsigned char* p
, off_t len
,
678 : Output_section_data(len
, addralign
), p_(p
)
682 // Write the data the output file.
684 do_write(Output_file
*);
686 // Write the data to a buffer.
688 do_write_to_buffer(unsigned char* buffer
)
689 { memcpy(buffer
, this->p_
, this->data_size()); }
692 const unsigned char* p_
;
695 // A place holder for a fixed amount of data written out via some
698 class Output_data_fixed_space
: public Output_section_data
701 Output_data_fixed_space(off_t data_size
, uint64_t addralign
)
702 : Output_section_data(data_size
, addralign
)
706 // Write out the data--the actual data must be written out
709 do_write(Output_file
*)
713 // A place holder for variable sized data written out via some other
716 class Output_data_space
: public Output_section_data_build
719 explicit Output_data_space(uint64_t addralign
)
720 : Output_section_data_build(addralign
)
723 // Set the alignment.
725 set_space_alignment(uint64_t align
)
726 { this->set_addralign(align
); }
729 // Write out the data--the actual data must be written out
732 do_write(Output_file
*)
736 // A string table which goes into an output section.
738 class Output_data_strtab
: public Output_section_data
741 Output_data_strtab(Stringpool
* strtab
)
742 : Output_section_data(1), strtab_(strtab
)
746 // This is called to set the address and file offset. Here we make
747 // sure that the Stringpool is finalized.
749 set_final_data_size();
751 // Write out the data.
753 do_write(Output_file
*);
755 // Write the data to a buffer.
757 do_write_to_buffer(unsigned char* buffer
)
758 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
764 // This POD class is used to represent a single reloc in the output
765 // file. This could be a private class within Output_data_reloc, but
766 // the templatization is complex enough that I broke it out into a
767 // separate class. The class is templatized on either elfcpp::SHT_REL
768 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
769 // relocation or an ordinary relocation.
771 // A relocation can be against a global symbol, a local symbol, a
772 // local section symbol, an output section, or the undefined symbol at
773 // index 0. We represent the latter by using a NULL global symbol.
775 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
778 template<bool dynamic
, int size
, bool big_endian
>
779 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
782 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
783 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
785 // An uninitialized entry. We need this because we want to put
786 // instances of this class into an STL container.
788 : local_sym_index_(INVALID_CODE
)
791 // We have a bunch of different constructors. They come in pairs
792 // depending on how the address of the relocation is specified. It
793 // can either be an offset in an Output_data or an offset in an
796 // A reloc against a global symbol.
798 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
799 Address address
, bool is_relative
);
801 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
802 unsigned int shndx
, Address address
, bool is_relative
);
804 // A reloc against a local symbol or local section symbol.
806 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
807 unsigned int local_sym_index
, unsigned int type
,
808 Output_data
* od
, Address address
, bool is_relative
,
809 bool is_section_symbol
);
811 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
812 unsigned int local_sym_index
, unsigned int type
,
813 unsigned int shndx
, Address address
, bool is_relative
,
814 bool is_section_symbol
);
816 // A reloc against the STT_SECTION symbol of an output section.
818 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
821 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
822 unsigned int shndx
, Address address
);
824 // Return TRUE if this is a RELATIVE relocation.
827 { return this->is_relative_
; }
829 // Return whether this is against a local section symbol.
831 is_local_section_symbol() const
833 return (this->local_sym_index_
!= GSYM_CODE
834 && this->local_sym_index_
!= SECTION_CODE
835 && this->local_sym_index_
!= INVALID_CODE
836 && this->is_section_symbol_
);
839 // For a local section symbol, return the offset of the input
840 // section within the output section. ADDEND is the addend being
841 // applied to the input section.
843 local_section_offset(Addend addend
) const;
845 // Get the value of the symbol referred to by a Rel relocation when
846 // we are adding the given ADDEND.
848 symbol_value(Addend addend
) const;
850 // Write the reloc entry to an output view.
852 write(unsigned char* pov
) const;
854 // Write the offset and info fields to Write_rel.
855 template<typename Write_rel
>
856 void write_rel(Write_rel
*) const;
858 // This is used when sorting dynamic relocs. Return -1 to sort this
859 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
861 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
864 // Return whether this reloc should be sorted before the argument
865 // when sorting dynamic relocs.
867 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
869 { return this->compare(r2
) < 0; }
872 // Record that we need a dynamic symbol index.
874 set_needs_dynsym_index();
876 // Return the symbol index.
878 get_symbol_index() const;
880 // Return the output address.
884 // Codes for local_sym_index_.
891 // Invalid uninitialized entry.
897 // For a local symbol or local section symbol
898 // (this->local_sym_index_ >= 0), the object. We will never
899 // generate a relocation against a local symbol in a dynamic
900 // object; that doesn't make sense. And our callers will always
901 // be templatized, so we use Sized_relobj here.
902 Sized_relobj
<size
, big_endian
>* relobj
;
903 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
904 // symbol. If this is NULL, it indicates a relocation against the
905 // undefined 0 symbol.
907 // For a relocation against an output section
908 // (this->local_sym_index_ == SECTION_CODE), the output section.
913 // If this->shndx_ is not INVALID CODE, the object which holds the
914 // input section being used to specify the reloc address.
916 // If this->shndx_ is INVALID_CODE, the output data being used to
917 // specify the reloc address. This may be NULL if the reloc
918 // address is absolute.
921 // The address offset within the input section or the Output_data.
923 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
924 // relocation against an output section, or INVALID_CODE for an
925 // uninitialized value. Otherwise, for a local symbol
926 // (this->is_section_symbol_ is false), the local symbol index. For
927 // a local section symbol (this->is_section_symbol_ is true), the
928 // section index in the input file.
929 unsigned int local_sym_index_
;
930 // The reloc type--a processor specific code.
931 unsigned int type_
: 30;
932 // True if the relocation is a RELATIVE relocation.
933 bool is_relative_
: 1;
934 // True if the relocation is against a section symbol.
935 bool is_section_symbol_
: 1;
936 // If the reloc address is an input section in an object, the
937 // section index. This is INVALID_CODE if the reloc address is
938 // specified in some other way.
942 // The SHT_RELA version of Output_reloc<>. This is just derived from
943 // the SHT_REL version of Output_reloc, but it adds an addend.
945 template<bool dynamic
, int size
, bool big_endian
>
946 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
949 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
950 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
952 // An uninitialized entry.
957 // A reloc against a global symbol.
959 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
960 Address address
, Addend addend
, bool is_relative
)
961 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
964 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
965 unsigned int shndx
, Address address
, Addend addend
,
967 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
970 // A reloc against a local symbol.
972 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
973 unsigned int local_sym_index
, unsigned int type
,
974 Output_data
* od
, Address address
,
975 Addend addend
, bool is_relative
, bool is_section_symbol
)
976 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
981 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
982 unsigned int local_sym_index
, unsigned int type
,
983 unsigned int shndx
, Address address
,
984 Addend addend
, bool is_relative
, bool is_section_symbol
)
985 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
990 // A reloc against the STT_SECTION symbol of an output section.
992 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
993 Address address
, Addend addend
)
994 : rel_(os
, type
, od
, address
), addend_(addend
)
997 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
998 unsigned int shndx
, Address address
, Addend addend
)
999 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1002 // Write the reloc entry to an output view.
1004 write(unsigned char* pov
) const;
1006 // Return whether this reloc should be sorted before the argument
1007 // when sorting dynamic relocs.
1009 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1012 int i
= this->rel_
.compare(r2
.rel_
);
1018 return this->addend_
< r2
.addend_
;
1023 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1028 // Output_data_reloc is used to manage a section containing relocs.
1029 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1030 // indicates whether this is a dynamic relocation or a normal
1031 // relocation. Output_data_reloc_base is a base class.
1032 // Output_data_reloc is the real class, which we specialize based on
1035 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1036 class Output_data_reloc_base
: public Output_section_data_build
1039 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1040 typedef typename
Output_reloc_type::Address Address
;
1041 static const int reloc_size
=
1042 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1044 // Construct the section.
1045 Output_data_reloc_base(bool sort_relocs
)
1046 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1047 sort_relocs_(sort_relocs
)
1051 // Write out the data.
1053 do_write(Output_file
*);
1055 // Set the entry size and the link.
1057 do_adjust_output_section(Output_section
*os
);
1059 // Add a relocation entry.
1061 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1063 this->relocs_
.push_back(reloc
);
1064 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1065 od
->add_dynamic_reloc();
1069 typedef std::vector
<Output_reloc_type
> Relocs
;
1071 // The class used to sort the relocations.
1072 struct Sort_relocs_comparison
1075 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1076 { return r1
.sort_before(r2
); }
1079 // The relocations in this section.
1081 // Whether to sort the relocations when writing them out, to make
1082 // the dynamic linker more efficient.
1086 // The class which callers actually create.
1088 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1089 class Output_data_reloc
;
1091 // The SHT_REL version of Output_data_reloc.
1093 template<bool dynamic
, int size
, bool big_endian
>
1094 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1095 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1098 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1102 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1103 typedef typename
Output_reloc_type::Address Address
;
1105 Output_data_reloc(bool sr
)
1106 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1109 // Add a reloc against a global symbol.
1112 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1113 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1116 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1117 unsigned int shndx
, Address address
)
1118 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1121 // These are to simplify the Copy_relocs class.
1124 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1127 gold_assert(addend
== 0);
1128 this->add_global(gsym
, type
, od
, address
);
1132 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1133 unsigned int shndx
, Address address
, Address addend
)
1135 gold_assert(addend
== 0);
1136 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1139 // Add a RELATIVE reloc against a global symbol. The final relocation
1140 // will not reference the symbol.
1143 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1145 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1148 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1149 Relobj
* relobj
, unsigned int shndx
, Address address
)
1151 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1155 // Add a reloc against a local symbol.
1158 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1159 unsigned int local_sym_index
, unsigned int type
,
1160 Output_data
* od
, Address address
)
1162 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1163 address
, false, false));
1167 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1168 unsigned int local_sym_index
, unsigned int type
,
1169 Output_data
* od
, unsigned int shndx
, Address address
)
1171 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1172 address
, false, false));
1175 // Add a RELATIVE reloc against a local symbol.
1178 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1179 unsigned int local_sym_index
, unsigned int type
,
1180 Output_data
* od
, Address address
)
1182 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1183 address
, true, false));
1187 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1188 unsigned int local_sym_index
, unsigned int type
,
1189 Output_data
* od
, unsigned int shndx
, Address address
)
1191 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1192 address
, true, false));
1195 // Add a reloc against a local section symbol. This will be
1196 // converted into a reloc against the STT_SECTION symbol of the
1200 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1201 unsigned int input_shndx
, unsigned int type
,
1202 Output_data
* od
, Address address
)
1204 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1205 address
, false, true));
1209 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1210 unsigned int input_shndx
, unsigned int type
,
1211 Output_data
* od
, unsigned int shndx
, Address address
)
1213 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1214 address
, false, true));
1217 // A reloc against the STT_SECTION symbol of an output section.
1218 // OS is the Output_section that the relocation refers to; OD is
1219 // the Output_data object being relocated.
1222 add_output_section(Output_section
* os
, unsigned int type
,
1223 Output_data
* od
, Address address
)
1224 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1227 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1228 Relobj
* relobj
, unsigned int shndx
, Address address
)
1229 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1232 // The SHT_RELA version of Output_data_reloc.
1234 template<bool dynamic
, int size
, bool big_endian
>
1235 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1236 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1239 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1243 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1244 typedef typename
Output_reloc_type::Address Address
;
1245 typedef typename
Output_reloc_type::Addend Addend
;
1247 Output_data_reloc(bool sr
)
1248 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1251 // Add a reloc against a global symbol.
1254 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1255 Address address
, Addend addend
)
1256 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1260 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1261 unsigned int shndx
, Address address
,
1263 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1266 // Add a RELATIVE reloc against a global symbol. The final output
1267 // relocation will not reference the symbol, but we must keep the symbol
1268 // information long enough to set the addend of the relocation correctly
1269 // when it is written.
1272 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1273 Address address
, Addend addend
)
1274 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1277 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1278 Relobj
* relobj
, unsigned int shndx
, Address address
,
1280 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1283 // Add a reloc against a local symbol.
1286 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1287 unsigned int local_sym_index
, unsigned int type
,
1288 Output_data
* od
, Address address
, Addend addend
)
1290 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1291 addend
, false, false));
1295 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1296 unsigned int local_sym_index
, unsigned int type
,
1297 Output_data
* od
, unsigned int shndx
, Address address
,
1300 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1301 address
, addend
, false, false));
1304 // Add a RELATIVE reloc against a local symbol.
1307 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1308 unsigned int local_sym_index
, unsigned int type
,
1309 Output_data
* od
, Address address
, Addend addend
)
1311 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1312 addend
, true, false));
1316 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1317 unsigned int local_sym_index
, unsigned int type
,
1318 Output_data
* od
, unsigned int shndx
, Address address
,
1321 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1322 address
, addend
, true, false));
1325 // Add a reloc against a local section symbol. This will be
1326 // converted into a reloc against the STT_SECTION symbol of the
1330 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1331 unsigned int input_shndx
, unsigned int type
,
1332 Output_data
* od
, Address address
, Addend addend
)
1334 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1335 addend
, false, true));
1339 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1340 unsigned int input_shndx
, unsigned int type
,
1341 Output_data
* od
, unsigned int shndx
, Address address
,
1344 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1345 address
, addend
, false, true));
1348 // A reloc against the STT_SECTION symbol of an output section.
1351 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1352 Address address
, Addend addend
)
1353 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1356 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1357 unsigned int shndx
, Address address
, Addend addend
)
1358 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1362 // Output_relocatable_relocs represents a relocation section in a
1363 // relocatable link. The actual data is written out in the target
1364 // hook relocate_for_relocatable. This just saves space for it.
1366 template<int sh_type
, int size
, bool big_endian
>
1367 class Output_relocatable_relocs
: public Output_section_data
1370 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1371 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1376 set_final_data_size();
1378 // Write out the data. There is nothing to do here.
1380 do_write(Output_file
*)
1384 // The relocs associated with this input section.
1385 Relocatable_relocs
* rr_
;
1388 // Handle a GROUP section.
1390 template<int size
, bool big_endian
>
1391 class Output_data_group
: public Output_section_data
1394 // The constructor clears *INPUT_SHNDXES.
1395 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1396 section_size_type entry_count
,
1397 elfcpp::Elf_Word flags
,
1398 std::vector
<unsigned int>* input_shndxes
);
1401 do_write(Output_file
*);
1404 // The input object.
1405 Sized_relobj
<size
, big_endian
>* relobj_
;
1406 // The group flag word.
1407 elfcpp::Elf_Word flags_
;
1408 // The section indexes of the input sections in this group.
1409 std::vector
<unsigned int> input_shndxes_
;
1412 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1413 // for one symbol--either a global symbol or a local symbol in an
1414 // object. The target specific code adds entries to the GOT as
1417 template<int size
, bool big_endian
>
1418 class Output_data_got
: public Output_section_data_build
1421 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1422 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1423 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1426 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1430 // Add an entry for a global symbol to the GOT. Return true if this
1431 // is a new GOT entry, false if the symbol was already in the GOT.
1433 add_global(Symbol
* gsym
, unsigned int got_type
);
1435 // Add an entry for a global symbol to the GOT, and add a dynamic
1436 // relocation of type R_TYPE for the GOT entry.
1438 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1439 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1442 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1443 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1445 // Add a pair of entries for a global symbol to the GOT, and add
1446 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1448 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1449 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1450 unsigned int r_type_2
);
1453 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1454 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1455 unsigned int r_type_2
);
1457 // Add an entry for a local symbol to the GOT. This returns true if
1458 // this is a new GOT entry, false if the symbol already has a GOT
1461 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1462 unsigned int got_type
);
1464 // Add an entry for a local symbol to the GOT, and add a dynamic
1465 // relocation of type R_TYPE for the GOT entry.
1467 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1468 unsigned int sym_index
, unsigned int got_type
,
1469 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1472 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1473 unsigned int sym_index
, unsigned int got_type
,
1474 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1476 // Add a pair of entries for a local symbol to the GOT, and add
1477 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1479 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1480 unsigned int sym_index
, unsigned int shndx
,
1481 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1482 unsigned int r_type_1
, unsigned int r_type_2
);
1485 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1486 unsigned int sym_index
, unsigned int shndx
,
1487 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1488 unsigned int r_type_1
, unsigned int r_type_2
);
1490 // Add a constant to the GOT. This returns the offset of the new
1491 // entry from the start of the GOT.
1493 add_constant(Valtype constant
)
1495 this->entries_
.push_back(Got_entry(constant
));
1496 this->set_got_size();
1497 return this->last_got_offset();
1501 // Write out the GOT table.
1503 do_write(Output_file
*);
1506 // This POD class holds a single GOT entry.
1510 // Create a zero entry.
1512 : local_sym_index_(CONSTANT_CODE
)
1513 { this->u_
.constant
= 0; }
1515 // Create a global symbol entry.
1516 explicit Got_entry(Symbol
* gsym
)
1517 : local_sym_index_(GSYM_CODE
)
1518 { this->u_
.gsym
= gsym
; }
1520 // Create a local symbol entry.
1521 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1522 unsigned int local_sym_index
)
1523 : local_sym_index_(local_sym_index
)
1525 gold_assert(local_sym_index
!= GSYM_CODE
1526 && local_sym_index
!= CONSTANT_CODE
);
1527 this->u_
.object
= object
;
1530 // Create a constant entry. The constant is a host value--it will
1531 // be swapped, if necessary, when it is written out.
1532 explicit Got_entry(Valtype constant
)
1533 : local_sym_index_(CONSTANT_CODE
)
1534 { this->u_
.constant
= constant
; }
1536 // Write the GOT entry to an output view.
1538 write(unsigned char* pov
) const;
1549 // For a local symbol, the object.
1550 Sized_relobj
<size
, big_endian
>* object
;
1551 // For a global symbol, the symbol.
1553 // For a constant, the constant.
1556 // For a local symbol, the local symbol index. This is GSYM_CODE
1557 // for a global symbol, or CONSTANT_CODE for a constant.
1558 unsigned int local_sym_index_
;
1561 typedef std::vector
<Got_entry
> Got_entries
;
1563 // Return the offset into the GOT of GOT entry I.
1565 got_offset(unsigned int i
) const
1566 { return i
* (size
/ 8); }
1568 // Return the offset into the GOT of the last entry added.
1570 last_got_offset() const
1571 { return this->got_offset(this->entries_
.size() - 1); }
1573 // Set the size of the section.
1576 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1578 // The list of GOT entries.
1579 Got_entries entries_
;
1582 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1585 class Output_data_dynamic
: public Output_section_data
1588 Output_data_dynamic(Stringpool
* pool
)
1589 : Output_section_data(Output_data::default_alignment()),
1590 entries_(), pool_(pool
)
1593 // Add a new dynamic entry with a fixed numeric value.
1595 add_constant(elfcpp::DT tag
, unsigned int val
)
1596 { this->add_entry(Dynamic_entry(tag
, val
)); }
1598 // Add a new dynamic entry with the address of output data.
1600 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1601 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1603 // Add a new dynamic entry with the address of output data
1604 // plus a constant offset.
1606 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
1607 unsigned int offset
)
1608 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
1610 // Add a new dynamic entry with the size of output data.
1612 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1613 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1615 // Add a new dynamic entry with the address of a symbol.
1617 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1618 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1620 // Add a new dynamic entry with a string.
1622 add_string(elfcpp::DT tag
, const char* str
)
1623 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1626 add_string(elfcpp::DT tag
, const std::string
& str
)
1627 { this->add_string(tag
, str
.c_str()); }
1630 // Adjust the output section to set the entry size.
1632 do_adjust_output_section(Output_section
*);
1634 // Set the final data size.
1636 set_final_data_size();
1638 // Write out the dynamic entries.
1640 do_write(Output_file
*);
1643 // This POD class holds a single dynamic entry.
1647 // Create an entry with a fixed numeric value.
1648 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1649 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
1650 { this->u_
.val
= val
; }
1652 // Create an entry with the size or address of a section.
1653 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1655 offset_(section_size
1656 ? DYNAMIC_SECTION_SIZE
1657 : DYNAMIC_SECTION_ADDRESS
)
1658 { this->u_
.od
= od
; }
1660 // Create an entry with the address of a section plus a constant offset.
1661 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
1664 { this->u_
.od
= od
; }
1666 // Create an entry with the address of a symbol.
1667 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1668 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
1669 { this->u_
.sym
= sym
; }
1671 // Create an entry with a string.
1672 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1673 : tag_(tag
), offset_(DYNAMIC_STRING
)
1674 { this->u_
.str
= str
; }
1676 // Write the dynamic entry to an output view.
1677 template<int size
, bool big_endian
>
1679 write(unsigned char* pov
, const Stringpool
*) const;
1682 // Classification is encoded in the OFFSET field.
1686 DYNAMIC_SECTION_ADDRESS
= 0,
1688 DYNAMIC_NUMBER
= -1U,
1690 DYNAMIC_SECTION_SIZE
= -2U,
1692 DYNAMIC_SYMBOL
= -3U,
1694 DYNAMIC_STRING
= -4U
1695 // Any other value indicates a section address plus OFFSET.
1700 // For DYNAMIC_NUMBER.
1702 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1703 const Output_data
* od
;
1704 // For DYNAMIC_SYMBOL.
1706 // For DYNAMIC_STRING.
1711 // The type of entry (Classification) or offset within a section.
1712 unsigned int offset_
;
1715 // Add an entry to the list.
1717 add_entry(const Dynamic_entry
& entry
)
1718 { this->entries_
.push_back(entry
); }
1720 // Sized version of write function.
1721 template<int size
, bool big_endian
>
1723 sized_write(Output_file
* of
);
1725 // The type of the list of entries.
1726 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1729 Dynamic_entries entries_
;
1730 // The pool used for strings.
1734 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1735 // which may be required if the object file has more than
1736 // SHN_LORESERVE sections.
1738 class Output_symtab_xindex
: public Output_section_data
1741 Output_symtab_xindex(size_t symcount
)
1742 : Output_section_data(symcount
* 4, 4),
1746 // Add an entry: symbol number SYMNDX has section SHNDX.
1748 add(unsigned int symndx
, unsigned int shndx
)
1749 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
1753 do_write(Output_file
*);
1756 template<bool big_endian
>
1758 endian_do_write(unsigned char*);
1760 // It is likely that most symbols will not require entries. Rather
1761 // than keep a vector for all symbols, we keep pairs of symbol index
1762 // and section index.
1763 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
1765 // The entries we need.
1766 Xindex_entries entries_
;
1769 // An output section. We don't expect to have too many output
1770 // sections, so we don't bother to do a template on the size.
1772 class Output_section
: public Output_data
1775 // Create an output section, giving the name, type, and flags.
1776 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1777 virtual ~Output_section();
1779 // Add a new input section SHNDX, named NAME, with header SHDR, from
1780 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1781 // which applies to this section, or 0 if none, or -1U if more than
1782 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1783 // in a linker script; in that case we need to keep track of input
1784 // sections associated with an output section. Return the offset
1785 // within the output section.
1786 template<int size
, bool big_endian
>
1788 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1790 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1791 unsigned int reloc_shndx
, bool have_sections_script
);
1793 // Add generated data POSD to this output section.
1795 add_output_section_data(Output_section_data
* posd
);
1797 // Return the section name.
1800 { return this->name_
; }
1802 // Return the section type.
1805 { return this->type_
; }
1807 // Return the section flags.
1810 { return this->flags_
; }
1812 // Set the section flags. This may only be used with the Layout
1813 // code when it is prepared to move the section to a different
1816 set_flags(elfcpp::Elf_Xword flags
)
1817 { this->flags_
= flags
; }
1819 // Update the output section flags based on input section flags.
1821 update_flags_for_input_section(elfcpp::Elf_Xword flags
)
1823 this->flags_
|= (flags
1824 & (elfcpp::SHF_WRITE
1826 | elfcpp::SHF_EXECINSTR
));
1829 // Return the entsize field.
1832 { return this->entsize_
; }
1834 // Set the entsize field.
1836 set_entsize(uint64_t v
);
1838 // Set the load address.
1840 set_load_address(uint64_t load_address
)
1842 this->load_address_
= load_address
;
1843 this->has_load_address_
= true;
1846 // Set the link field to the output section index of a section.
1848 set_link_section(const Output_data
* od
)
1850 gold_assert(this->link_
== 0
1851 && !this->should_link_to_symtab_
1852 && !this->should_link_to_dynsym_
);
1853 this->link_section_
= od
;
1856 // Set the link field to a constant.
1858 set_link(unsigned int v
)
1860 gold_assert(this->link_section_
== NULL
1861 && !this->should_link_to_symtab_
1862 && !this->should_link_to_dynsym_
);
1866 // Record that this section should link to the normal symbol table.
1868 set_should_link_to_symtab()
1870 gold_assert(this->link_section_
== NULL
1872 && !this->should_link_to_dynsym_
);
1873 this->should_link_to_symtab_
= true;
1876 // Record that this section should link to the dynamic symbol table.
1878 set_should_link_to_dynsym()
1880 gold_assert(this->link_section_
== NULL
1882 && !this->should_link_to_symtab_
);
1883 this->should_link_to_dynsym_
= true;
1886 // Return the info field.
1890 gold_assert(this->info_section_
== NULL
1891 && this->info_symndx_
== NULL
);
1895 // Set the info field to the output section index of a section.
1897 set_info_section(const Output_section
* os
)
1899 gold_assert((this->info_section_
== NULL
1900 || (this->info_section_
== os
1901 && this->info_uses_section_index_
))
1902 && this->info_symndx_
== NULL
1903 && this->info_
== 0);
1904 this->info_section_
= os
;
1905 this->info_uses_section_index_
= true;
1908 // Set the info field to the symbol table index of a symbol.
1910 set_info_symndx(const Symbol
* sym
)
1912 gold_assert(this->info_section_
== NULL
1913 && (this->info_symndx_
== NULL
1914 || this->info_symndx_
== sym
)
1915 && this->info_
== 0);
1916 this->info_symndx_
= sym
;
1919 // Set the info field to the symbol table index of a section symbol.
1921 set_info_section_symndx(const Output_section
* os
)
1923 gold_assert((this->info_section_
== NULL
1924 || (this->info_section_
== os
1925 && !this->info_uses_section_index_
))
1926 && this->info_symndx_
== NULL
1927 && this->info_
== 0);
1928 this->info_section_
= os
;
1929 this->info_uses_section_index_
= false;
1932 // Set the info field to a constant.
1934 set_info(unsigned int v
)
1936 gold_assert(this->info_section_
== NULL
1937 && this->info_symndx_
== NULL
1938 && (this->info_
== 0
1939 || this->info_
== v
));
1943 // Set the addralign field.
1945 set_addralign(uint64_t v
)
1946 { this->addralign_
= v
; }
1948 // Whether the output section index has been set.
1950 has_out_shndx() const
1951 { return this->out_shndx_
!= -1U; }
1953 // Indicate that we need a symtab index.
1955 set_needs_symtab_index()
1956 { this->needs_symtab_index_
= true; }
1958 // Return whether we need a symtab index.
1960 needs_symtab_index() const
1961 { return this->needs_symtab_index_
; }
1963 // Get the symtab index.
1965 symtab_index() const
1967 gold_assert(this->symtab_index_
!= 0);
1968 return this->symtab_index_
;
1971 // Set the symtab index.
1973 set_symtab_index(unsigned int index
)
1975 gold_assert(index
!= 0);
1976 this->symtab_index_
= index
;
1979 // Indicate that we need a dynsym index.
1981 set_needs_dynsym_index()
1982 { this->needs_dynsym_index_
= true; }
1984 // Return whether we need a dynsym index.
1986 needs_dynsym_index() const
1987 { return this->needs_dynsym_index_
; }
1989 // Get the dynsym index.
1991 dynsym_index() const
1993 gold_assert(this->dynsym_index_
!= 0);
1994 return this->dynsym_index_
;
1997 // Set the dynsym index.
1999 set_dynsym_index(unsigned int index
)
2001 gold_assert(index
!= 0);
2002 this->dynsym_index_
= index
;
2005 // Return whether the input sections sections attachd to this output
2006 // section may require sorting. This is used to handle constructor
2007 // priorities compatibly with GNU ld.
2009 may_sort_attached_input_sections() const
2010 { return this->may_sort_attached_input_sections_
; }
2012 // Record that the input sections attached to this output section
2013 // may require sorting.
2015 set_may_sort_attached_input_sections()
2016 { this->may_sort_attached_input_sections_
= true; }
2018 // Return whether the input sections attached to this output section
2019 // require sorting. This is used to handle constructor priorities
2020 // compatibly with GNU ld.
2022 must_sort_attached_input_sections() const
2023 { return this->must_sort_attached_input_sections_
; }
2025 // Record that the input sections attached to this output section
2028 set_must_sort_attached_input_sections()
2029 { this->must_sort_attached_input_sections_
= true; }
2031 // Return whether this section should be written after all the input
2032 // sections are complete.
2034 after_input_sections() const
2035 { return this->after_input_sections_
; }
2037 // Record that this section should be written after all the input
2038 // sections are complete.
2040 set_after_input_sections()
2041 { this->after_input_sections_
= true; }
2043 // Return whether this section requires postprocessing after all
2044 // relocations have been applied.
2046 requires_postprocessing() const
2047 { return this->requires_postprocessing_
; }
2049 // If a section requires postprocessing, return the buffer to use.
2051 postprocessing_buffer() const
2053 gold_assert(this->postprocessing_buffer_
!= NULL
);
2054 return this->postprocessing_buffer_
;
2057 // If a section requires postprocessing, create the buffer to use.
2059 create_postprocessing_buffer();
2061 // If a section requires postprocessing, this is the size of the
2062 // buffer to which relocations should be applied.
2064 postprocessing_buffer_size() const
2065 { return this->current_data_size_for_child(); }
2067 // Modify the section name. This is only permitted for an
2068 // unallocated section, and only before the size has been finalized.
2069 // Otherwise the name will not get into Layout::namepool_.
2071 set_name(const char* newname
)
2073 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2074 gold_assert(!this->is_data_size_valid());
2075 this->name_
= newname
;
2078 // Return whether the offset OFFSET in the input section SHNDX in
2079 // object OBJECT is being included in the link.
2081 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2082 off_t offset
) const;
2084 // Return the offset within the output section of OFFSET relative to
2085 // the start of input section SHNDX in object OBJECT.
2087 output_offset(const Relobj
* object
, unsigned int shndx
,
2088 section_offset_type offset
) const;
2090 // Return the output virtual address of OFFSET relative to the start
2091 // of input section SHNDX in object OBJECT.
2093 output_address(const Relobj
* object
, unsigned int shndx
,
2094 off_t offset
) const;
2096 // Return the output address of the start of the merged section for
2097 // input section SHNDX in object OBJECT. This is not necessarily
2098 // the offset corresponding to input offset 0 in the section, since
2099 // the section may be mapped arbitrarily.
2101 starting_output_address(const Relobj
* object
, unsigned int shndx
) const;
2103 // Record that this output section was found in the SECTIONS clause
2104 // of a linker script.
2106 set_found_in_sections_clause()
2107 { this->found_in_sections_clause_
= true; }
2109 // Return whether this output section was found in the SECTIONS
2110 // clause of a linker script.
2112 found_in_sections_clause() const
2113 { return this->found_in_sections_clause_
; }
2115 // Write the section header into *OPHDR.
2116 template<int size
, bool big_endian
>
2118 write_header(const Layout
*, const Stringpool
*,
2119 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2121 // The next few calls are for linker script support.
2123 // Store the list of input sections for this Output_section into the
2124 // list passed in. This removes the input sections, leaving only
2125 // any Output_section_data elements. This returns the size of those
2126 // Output_section_data elements. ADDRESS is the address of this
2127 // output section. FILL is the fill value to use, in case there are
2128 // any spaces between the remaining Output_section_data elements.
2130 get_input_sections(uint64_t address
, const std::string
& fill
,
2131 std::list
<std::pair
<Relobj
*, unsigned int > >*);
2133 // Add an input section from a script.
2135 add_input_section_for_script(Relobj
* object
, unsigned int shndx
,
2136 off_t data_size
, uint64_t addralign
);
2138 // Set the current size of the output section.
2140 set_current_data_size(off_t size
)
2141 { this->set_current_data_size_for_child(size
); }
2143 // Get the current size of the output section.
2145 current_data_size() const
2146 { return this->current_data_size_for_child(); }
2148 // End of linker script support.
2150 // Print merge statistics to stderr.
2152 print_merge_stats();
2155 // Return the output section--i.e., the object itself.
2160 // Return the section index in the output file.
2162 do_out_shndx() const
2164 gold_assert(this->out_shndx_
!= -1U);
2165 return this->out_shndx_
;
2168 // Set the output section index.
2170 do_set_out_shndx(unsigned int shndx
)
2172 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2173 this->out_shndx_
= shndx
;
2176 // Set the final data size of the Output_section. For a typical
2177 // Output_section, there is nothing to do, but if there are any
2178 // Output_section_data objects we need to set their final addresses
2181 set_final_data_size();
2183 // Reset the address and file offset.
2185 do_reset_address_and_file_offset();
2187 // Write the data to the file. For a typical Output_section, this
2188 // does nothing: the data is written out by calling Object::Relocate
2189 // on each input object. But if there are any Output_section_data
2190 // objects we do need to write them out here.
2192 do_write(Output_file
*);
2194 // Return the address alignment--function required by parent class.
2196 do_addralign() const
2197 { return this->addralign_
; }
2199 // Return whether there is a load address.
2201 do_has_load_address() const
2202 { return this->has_load_address_
; }
2204 // Return the load address.
2206 do_load_address() const
2208 gold_assert(this->has_load_address_
);
2209 return this->load_address_
;
2212 // Return whether this is an Output_section.
2214 do_is_section() const
2217 // Return whether this is a section of the specified type.
2219 do_is_section_type(elfcpp::Elf_Word type
) const
2220 { return this->type_
== type
; }
2222 // Return whether the specified section flag is set.
2224 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2225 { return (this->flags_
& flag
) != 0; }
2227 // Set the TLS offset. Called only for SHT_TLS sections.
2229 do_set_tls_offset(uint64_t tls_base
);
2231 // Return the TLS offset, relative to the base of the TLS segment.
2232 // Valid only for SHT_TLS sections.
2234 do_tls_offset() const
2235 { return this->tls_offset_
; }
2237 // This may be implemented by a child class.
2239 do_finalize_name(Layout
*)
2242 // Record that this section requires postprocessing after all
2243 // relocations have been applied. This is called by a child class.
2245 set_requires_postprocessing()
2247 this->requires_postprocessing_
= true;
2248 this->after_input_sections_
= true;
2251 // Write all the data of an Output_section into the postprocessing
2254 write_to_postprocessing_buffer();
2257 // In some cases we need to keep a list of the input sections
2258 // associated with this output section. We only need the list if we
2259 // might have to change the offsets of the input section within the
2260 // output section after we add the input section. The ordinary
2261 // input sections will be written out when we process the object
2262 // file, and as such we don't need to track them here. We do need
2263 // to track Output_section_data objects here. We store instances of
2264 // this structure in a std::vector, so it must be a POD. There can
2265 // be many instances of this structure, so we use a union to save
2271 : shndx_(0), p2align_(0)
2273 this->u1_
.data_size
= 0;
2274 this->u2_
.object
= NULL
;
2277 // For an ordinary input section.
2278 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2281 p2align_(ffsll(static_cast<long long>(addralign
)))
2283 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2284 && shndx
!= MERGE_DATA_SECTION_CODE
2285 && shndx
!= MERGE_STRING_SECTION_CODE
);
2286 this->u1_
.data_size
= data_size
;
2287 this->u2_
.object
= object
;
2290 // For a non-merge output section.
2291 Input_section(Output_section_data
* posd
)
2292 : shndx_(OUTPUT_SECTION_CODE
),
2293 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2295 this->u1_
.data_size
= 0;
2296 this->u2_
.posd
= posd
;
2299 // For a merge section.
2300 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2302 ? MERGE_STRING_SECTION_CODE
2303 : MERGE_DATA_SECTION_CODE
),
2304 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2306 this->u1_
.entsize
= entsize
;
2307 this->u2_
.posd
= posd
;
2310 // The required alignment.
2314 return (this->p2align_
== 0
2316 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2319 // Return the required size.
2323 // Whether this is an input section.
2325 is_input_section() const
2327 return (this->shndx_
!= OUTPUT_SECTION_CODE
2328 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2329 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
2332 // Return whether this is a merge section which matches the
2335 is_merge_section(bool is_string
, uint64_t entsize
,
2336 uint64_t addralign
) const
2338 return (this->shndx_
== (is_string
2339 ? MERGE_STRING_SECTION_CODE
2340 : MERGE_DATA_SECTION_CODE
)
2341 && this->u1_
.entsize
== entsize
2342 && this->addralign() == addralign
);
2345 // Return the object for an input section.
2349 gold_assert(this->is_input_section());
2350 return this->u2_
.object
;
2353 // Return the input section index for an input section.
2357 gold_assert(this->is_input_section());
2358 return this->shndx_
;
2361 // Set the output section.
2363 set_output_section(Output_section
* os
)
2365 gold_assert(!this->is_input_section());
2366 this->u2_
.posd
->set_output_section(os
);
2369 // Set the address and file offset. This is called during
2370 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2371 // the enclosing section.
2373 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2374 off_t section_file_offset
);
2376 // Reset the address and file offset.
2378 reset_address_and_file_offset();
2380 // Finalize the data size.
2382 finalize_data_size();
2384 // Add an input section, for SHF_MERGE sections.
2386 add_input_section(Relobj
* object
, unsigned int shndx
)
2388 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2389 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2390 return this->u2_
.posd
->add_input_section(object
, shndx
);
2393 // Given an input OBJECT, an input section index SHNDX within that
2394 // object, and an OFFSET relative to the start of that input
2395 // section, return whether or not the output offset is known. If
2396 // this function returns true, it sets *POUTPUT to the offset in
2397 // the output section, relative to the start of the input section
2398 // in the output section. *POUTPUT may be different from OFFSET
2399 // for a merged section.
2401 output_offset(const Relobj
* object
, unsigned int shndx
,
2402 section_offset_type offset
,
2403 section_offset_type
*poutput
) const;
2405 // Return whether this is the merge section for the input section
2408 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2410 // Write out the data. This does nothing for an input section.
2412 write(Output_file
*);
2414 // Write the data to a buffer. This does nothing for an input
2417 write_to_buffer(unsigned char*);
2419 // Print statistics about merge sections to stderr.
2421 print_merge_stats(const char* section_name
)
2423 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2424 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2425 this->u2_
.posd
->print_merge_stats(section_name
);
2429 // Code values which appear in shndx_. If the value is not one of
2430 // these codes, it is the input section index in the object file.
2433 // An Output_section_data.
2434 OUTPUT_SECTION_CODE
= -1U,
2435 // An Output_section_data for an SHF_MERGE section with
2436 // SHF_STRINGS not set.
2437 MERGE_DATA_SECTION_CODE
= -2U,
2438 // An Output_section_data for an SHF_MERGE section with
2440 MERGE_STRING_SECTION_CODE
= -3U
2443 // For an ordinary input section, this is the section index in the
2444 // input file. For an Output_section_data, this is
2445 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2446 // MERGE_STRING_SECTION_CODE.
2447 unsigned int shndx_
;
2448 // The required alignment, stored as a power of 2.
2449 unsigned int p2align_
;
2452 // For an ordinary input section, the section size.
2454 // For OUTPUT_SECTION_CODE, this is not used. For
2455 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2461 // For an ordinary input section, the object which holds the
2464 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2465 // MERGE_STRING_SECTION_CODE, the data.
2466 Output_section_data
* posd
;
2470 typedef std::vector
<Input_section
> Input_section_list
;
2472 // This class is used to sort the input sections.
2473 class Input_section_sort_entry
;
2475 // This is the sort comparison function.
2476 struct Input_section_sort_compare
2479 operator()(const Input_section_sort_entry
&,
2480 const Input_section_sort_entry
&) const;
2483 // Fill data. This is used to fill in data between input sections.
2484 // It is also used for data statements (BYTE, WORD, etc.) in linker
2485 // scripts. When we have to keep track of the input sections, we
2486 // can use an Output_data_const, but we don't want to have to keep
2487 // track of input sections just to implement fills.
2491 Fill(off_t section_offset
, off_t length
)
2492 : section_offset_(section_offset
),
2493 length_(convert_to_section_size_type(length
))
2496 // Return section offset.
2498 section_offset() const
2499 { return this->section_offset_
; }
2501 // Return fill length.
2504 { return this->length_
; }
2507 // The offset within the output section.
2508 off_t section_offset_
;
2509 // The length of the space to fill.
2510 section_size_type length_
;
2513 typedef std::vector
<Fill
> Fill_list
;
2515 // Add a new output section by Input_section.
2517 add_output_section_data(Input_section
*);
2519 // Add an SHF_MERGE input section. Returns true if the section was
2522 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
2523 uint64_t entsize
, uint64_t addralign
);
2525 // Add an output SHF_MERGE section POSD to this output section.
2526 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2527 // ENTSIZE is the entity size. This returns the entry added to
2530 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
2533 // Sort the attached input sections.
2535 sort_attached_input_sections();
2537 // Most of these fields are only valid after layout.
2539 // The name of the section. This will point into a Stringpool.
2541 // The section address is in the parent class.
2542 // The section alignment.
2543 uint64_t addralign_
;
2544 // The section entry size.
2546 // The load address. This is only used when using a linker script
2547 // with a SECTIONS clause. The has_load_address_ field indicates
2548 // whether this field is valid.
2549 uint64_t load_address_
;
2550 // The file offset is in the parent class.
2551 // Set the section link field to the index of this section.
2552 const Output_data
* link_section_
;
2553 // If link_section_ is NULL, this is the link field.
2555 // Set the section info field to the index of this section.
2556 const Output_section
* info_section_
;
2557 // If info_section_ is NULL, set the info field to the symbol table
2558 // index of this symbol.
2559 const Symbol
* info_symndx_
;
2560 // If info_section_ and info_symndx_ are NULL, this is the section
2563 // The section type.
2564 const elfcpp::Elf_Word type_
;
2565 // The section flags.
2566 elfcpp::Elf_Xword flags_
;
2567 // The section index.
2568 unsigned int out_shndx_
;
2569 // If there is a STT_SECTION for this output section in the normal
2570 // symbol table, this is the symbol index. This starts out as zero.
2571 // It is initialized in Layout::finalize() to be the index, or -1U
2572 // if there isn't one.
2573 unsigned int symtab_index_
;
2574 // If there is a STT_SECTION for this output section in the dynamic
2575 // symbol table, this is the symbol index. This starts out as zero.
2576 // It is initialized in Layout::finalize() to be the index, or -1U
2577 // if there isn't one.
2578 unsigned int dynsym_index_
;
2579 // The input sections. This will be empty in cases where we don't
2580 // need to keep track of them.
2581 Input_section_list input_sections_
;
2582 // The offset of the first entry in input_sections_.
2583 off_t first_input_offset_
;
2584 // The fill data. This is separate from input_sections_ because we
2585 // often will need fill sections without needing to keep track of
2588 // If the section requires postprocessing, this buffer holds the
2589 // section contents during relocation.
2590 unsigned char* postprocessing_buffer_
;
2591 // Whether this output section needs a STT_SECTION symbol in the
2592 // normal symbol table. This will be true if there is a relocation
2594 bool needs_symtab_index_
: 1;
2595 // Whether this output section needs a STT_SECTION symbol in the
2596 // dynamic symbol table. This will be true if there is a dynamic
2597 // relocation which needs it.
2598 bool needs_dynsym_index_
: 1;
2599 // Whether the link field of this output section should point to the
2600 // normal symbol table.
2601 bool should_link_to_symtab_
: 1;
2602 // Whether the link field of this output section should point to the
2603 // dynamic symbol table.
2604 bool should_link_to_dynsym_
: 1;
2605 // Whether this section should be written after all the input
2606 // sections are complete.
2607 bool after_input_sections_
: 1;
2608 // Whether this section requires post processing after all
2609 // relocations have been applied.
2610 bool requires_postprocessing_
: 1;
2611 // Whether an input section was mapped to this output section
2612 // because of a SECTIONS clause in a linker script.
2613 bool found_in_sections_clause_
: 1;
2614 // Whether this section has an explicitly specified load address.
2615 bool has_load_address_
: 1;
2616 // True if the info_section_ field means the section index of the
2617 // section, false if it means the symbol index of the corresponding
2619 bool info_uses_section_index_
: 1;
2620 // True if the input sections attached to this output section may
2622 bool may_sort_attached_input_sections_
: 1;
2623 // True if the input sections attached to this output section must
2625 bool must_sort_attached_input_sections_
: 1;
2626 // True if the input sections attached to this output section have
2627 // already been sorted.
2628 bool attached_input_sections_are_sorted_
: 1;
2629 // For SHT_TLS sections, the offset of this section relative to the base
2630 // of the TLS segment.
2631 uint64_t tls_offset_
;
2634 // An output segment. PT_LOAD segments are built from collections of
2635 // output sections. Other segments typically point within PT_LOAD
2636 // segments, and are built directly as needed.
2638 class Output_segment
2641 // Create an output segment, specifying the type and flags.
2642 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2644 // Return the virtual address.
2647 { return this->vaddr_
; }
2649 // Return the physical address.
2652 { return this->paddr_
; }
2654 // Return the segment type.
2657 { return this->type_
; }
2659 // Return the segment flags.
2662 { return this->flags_
; }
2664 // Return the memory size.
2667 { return this->memsz_
; }
2669 // Return the file size.
2672 { return this->filesz_
; }
2674 // Return the file offset.
2677 { return this->offset_
; }
2679 // Return the maximum alignment of the Output_data.
2681 maximum_alignment();
2683 // Add an Output_section to this segment.
2685 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2686 { this->add_output_section(os
, seg_flags
, false); }
2688 // Add an Output_section to the start of this segment.
2690 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2691 { this->add_output_section(os
, seg_flags
, true); }
2693 // Remove an Output_section from this segment. It is an error if it
2696 remove_output_section(Output_section
* os
);
2698 // Add an Output_data (which is not an Output_section) to the start
2701 add_initial_output_data(Output_data
*);
2703 // Return true if this segment has any sections which hold actual
2704 // data, rather than being a BSS section.
2706 has_any_data_sections() const
2707 { return !this->output_data_
.empty(); }
2709 // Return the number of dynamic relocations applied to this segment.
2711 dynamic_reloc_count() const;
2713 // Return the address of the first section.
2715 first_section_load_address() const;
2717 // Return whether the addresses have been set already.
2719 are_addresses_set() const
2720 { return this->are_addresses_set_
; }
2722 // Set the addresses.
2724 set_addresses(uint64_t vaddr
, uint64_t paddr
)
2726 this->vaddr_
= vaddr
;
2727 this->paddr_
= paddr
;
2728 this->are_addresses_set_
= true;
2731 // Set the segment flags. This is only used if we have a PHDRS
2732 // clause which explicitly specifies the flags.
2734 set_flags(elfcpp::Elf_Word flags
)
2735 { this->flags_
= flags
; }
2737 // Set the address of the segment to ADDR and the offset to *POFF
2738 // and set the addresses and offsets of all contained output
2739 // sections accordingly. Set the section indexes of all contained
2740 // output sections starting with *PSHNDX. If RESET is true, first
2741 // reset the addresses of the contained sections. Return the
2742 // address of the immediately following segment. Update *POFF and
2743 // *PSHNDX. This should only be called for a PT_LOAD segment.
2745 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
, off_t
* poff
,
2746 unsigned int* pshndx
);
2748 // Set the minimum alignment of this segment. This may be adjusted
2749 // upward based on the section alignments.
2751 set_minimum_p_align(uint64_t align
)
2752 { this->min_p_align_
= align
; }
2754 // Set the offset of this segment based on the section. This should
2755 // only be called for a non-PT_LOAD segment.
2759 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2763 // Return the number of output sections.
2765 output_section_count() const;
2767 // Return the section attached to the list segment with the lowest
2768 // load address. This is used when handling a PHDRS clause in a
2771 section_with_lowest_load_address() const;
2773 // Write the segment header into *OPHDR.
2774 template<int size
, bool big_endian
>
2776 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2778 // Write the section headers of associated sections into V.
2779 template<int size
, bool big_endian
>
2781 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2782 unsigned int* pshndx
) const;
2785 Output_segment(const Output_segment
&);
2786 Output_segment
& operator=(const Output_segment
&);
2788 typedef std::list
<Output_data
*> Output_data_list
;
2790 // Add an Output_section to this segment, specifying front or back.
2792 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2795 // Find the maximum alignment in an Output_data_list.
2797 maximum_alignment_list(const Output_data_list
*);
2799 // Set the section addresses in an Output_data_list.
2801 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
2802 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
2805 // Return the number of Output_sections in an Output_data_list.
2807 output_section_count_list(const Output_data_list
*) const;
2809 // Return the number of dynamic relocs in an Output_data_list.
2811 dynamic_reloc_count_list(const Output_data_list
*) const;
2813 // Find the section with the lowest load address in an
2814 // Output_data_list.
2816 lowest_load_address_in_list(const Output_data_list
* pdl
,
2817 Output_section
** found
,
2818 uint64_t* found_lma
) const;
2820 // Write the section headers in the list into V.
2821 template<int size
, bool big_endian
>
2823 write_section_headers_list(const Layout
*, const Stringpool
*,
2824 const Output_data_list
*, unsigned char* v
,
2825 unsigned int* pshdx
) const;
2827 // The list of output data with contents attached to this segment.
2828 Output_data_list output_data_
;
2829 // The list of output data without contents attached to this segment.
2830 Output_data_list output_bss_
;
2831 // The segment virtual address.
2833 // The segment physical address.
2835 // The size of the segment in memory.
2837 // The maximum section alignment. The is_max_align_known_ field
2838 // indicates whether this has been finalized.
2839 uint64_t max_align_
;
2840 // The required minimum value for the p_align field. This is used
2841 // for PT_LOAD segments. Note that this does not mean that
2842 // addresses should be aligned to this value; it means the p_paddr
2843 // and p_vaddr fields must be congruent modulo this value. For
2844 // non-PT_LOAD segments, the dynamic linker works more efficiently
2845 // if the p_align field has the more conventional value, although it
2846 // can align as needed.
2847 uint64_t min_p_align_
;
2848 // The offset of the segment data within the file.
2850 // The size of the segment data in the file.
2852 // The segment type;
2853 elfcpp::Elf_Word type_
;
2854 // The segment flags.
2855 elfcpp::Elf_Word flags_
;
2856 // Whether we have finalized max_align_.
2857 bool is_max_align_known_
: 1;
2858 // Whether vaddr and paddr were set by a linker script.
2859 bool are_addresses_set_
: 1;
2862 // This class represents the output file.
2867 Output_file(const char* name
);
2869 // Indicate that this is a temporary file which should not be
2873 { this->is_temporary_
= true; }
2875 // Open the output file. FILE_SIZE is the final size of the file.
2877 open(off_t file_size
);
2879 // Resize the output file.
2881 resize(off_t file_size
);
2883 // Close the output file (flushing all buffered data) and make sure
2884 // there are no errors.
2888 // We currently always use mmap which makes the view handling quite
2889 // simple. In the future we may support other approaches.
2891 // Write data to the output file.
2893 write(off_t offset
, const void* data
, size_t len
)
2894 { memcpy(this->base_
+ offset
, data
, len
); }
2896 // Get a buffer to use to write to the file, given the offset into
2897 // the file and the size.
2899 get_output_view(off_t start
, size_t size
)
2901 gold_assert(start
>= 0
2902 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
2903 return this->base_
+ start
;
2906 // VIEW must have been returned by get_output_view. Write the
2907 // buffer to the file, passing in the offset and the size.
2909 write_output_view(off_t
, size_t, unsigned char*)
2912 // Get a read/write buffer. This is used when we want to write part
2913 // of the file, read it in, and write it again.
2915 get_input_output_view(off_t start
, size_t size
)
2916 { return this->get_output_view(start
, size
); }
2918 // Write a read/write buffer back to the file.
2920 write_input_output_view(off_t
, size_t, unsigned char*)
2923 // Get a read buffer. This is used when we just want to read part
2924 // of the file back it in.
2925 const unsigned char*
2926 get_input_view(off_t start
, size_t size
)
2927 { return this->get_output_view(start
, size
); }
2929 // Release a read bfufer.
2931 free_input_view(off_t
, size_t, const unsigned char*)
2935 // Map the file into memory and return a pointer to the map.
2939 // Unmap the file from memory (and flush to disk buffers).
2949 // Base of file mapped into memory.
2950 unsigned char* base_
;
2951 // True iff base_ points to a memory buffer rather than an output file.
2952 bool map_is_anonymous_
;
2953 // True if this is a temporary file which should not be output.
2957 } // End namespace gold.
2959 #endif // !defined(GOLD_OUTPUT_H)