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 holds relro data--data which has
2032 // dynamic relocations but which may be marked read-only after the
2033 // dynamic relocations have been completed.
2036 { return this->is_relro_
; }
2038 // Record that this section holds relro data.
2041 { this->is_relro_
= true; }
2043 // True if this section holds relro local data--relro data for which
2044 // the dynamic relocations are all RELATIVE relocations.
2046 is_relro_local() const
2047 { return this->is_relro_local_
; }
2049 // Record that this section holds relro local data.
2051 set_is_relro_local()
2052 { this->is_relro_local_
= true; }
2054 // Return whether this section should be written after all the input
2055 // sections are complete.
2057 after_input_sections() const
2058 { return this->after_input_sections_
; }
2060 // Record that this section should be written after all the input
2061 // sections are complete.
2063 set_after_input_sections()
2064 { this->after_input_sections_
= true; }
2066 // Return whether this section requires postprocessing after all
2067 // relocations have been applied.
2069 requires_postprocessing() const
2070 { return this->requires_postprocessing_
; }
2072 // If a section requires postprocessing, return the buffer to use.
2074 postprocessing_buffer() const
2076 gold_assert(this->postprocessing_buffer_
!= NULL
);
2077 return this->postprocessing_buffer_
;
2080 // If a section requires postprocessing, create the buffer to use.
2082 create_postprocessing_buffer();
2084 // If a section requires postprocessing, this is the size of the
2085 // buffer to which relocations should be applied.
2087 postprocessing_buffer_size() const
2088 { return this->current_data_size_for_child(); }
2090 // Modify the section name. This is only permitted for an
2091 // unallocated section, and only before the size has been finalized.
2092 // Otherwise the name will not get into Layout::namepool_.
2094 set_name(const char* newname
)
2096 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2097 gold_assert(!this->is_data_size_valid());
2098 this->name_
= newname
;
2101 // Return whether the offset OFFSET in the input section SHNDX in
2102 // object OBJECT is being included in the link.
2104 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2105 off_t offset
) const;
2107 // Return the offset within the output section of OFFSET relative to
2108 // the start of input section SHNDX in object OBJECT.
2110 output_offset(const Relobj
* object
, unsigned int shndx
,
2111 section_offset_type offset
) const;
2113 // Return the output virtual address of OFFSET relative to the start
2114 // of input section SHNDX in object OBJECT.
2116 output_address(const Relobj
* object
, unsigned int shndx
,
2117 off_t offset
) const;
2119 // Return the output address of the start of the merged section for
2120 // input section SHNDX in object OBJECT. This is not necessarily
2121 // the offset corresponding to input offset 0 in the section, since
2122 // the section may be mapped arbitrarily.
2124 starting_output_address(const Relobj
* object
, unsigned int shndx
) const;
2126 // Record that this output section was found in the SECTIONS clause
2127 // of a linker script.
2129 set_found_in_sections_clause()
2130 { this->found_in_sections_clause_
= true; }
2132 // Return whether this output section was found in the SECTIONS
2133 // clause of a linker script.
2135 found_in_sections_clause() const
2136 { return this->found_in_sections_clause_
; }
2138 // Write the section header into *OPHDR.
2139 template<int size
, bool big_endian
>
2141 write_header(const Layout
*, const Stringpool
*,
2142 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2144 // The next few calls are for linker script support.
2146 // Store the list of input sections for this Output_section into the
2147 // list passed in. This removes the input sections, leaving only
2148 // any Output_section_data elements. This returns the size of those
2149 // Output_section_data elements. ADDRESS is the address of this
2150 // output section. FILL is the fill value to use, in case there are
2151 // any spaces between the remaining Output_section_data elements.
2153 get_input_sections(uint64_t address
, const std::string
& fill
,
2154 std::list
<std::pair
<Relobj
*, unsigned int > >*);
2156 // Add an input section from a script.
2158 add_input_section_for_script(Relobj
* object
, unsigned int shndx
,
2159 off_t data_size
, uint64_t addralign
);
2161 // Set the current size of the output section.
2163 set_current_data_size(off_t size
)
2164 { this->set_current_data_size_for_child(size
); }
2166 // Get the current size of the output section.
2168 current_data_size() const
2169 { return this->current_data_size_for_child(); }
2171 // End of linker script support.
2173 // Print merge statistics to stderr.
2175 print_merge_stats();
2178 // Return the output section--i.e., the object itself.
2183 // Return the section index in the output file.
2185 do_out_shndx() const
2187 gold_assert(this->out_shndx_
!= -1U);
2188 return this->out_shndx_
;
2191 // Set the output section index.
2193 do_set_out_shndx(unsigned int shndx
)
2195 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2196 this->out_shndx_
= shndx
;
2199 // Set the final data size of the Output_section. For a typical
2200 // Output_section, there is nothing to do, but if there are any
2201 // Output_section_data objects we need to set their final addresses
2204 set_final_data_size();
2206 // Reset the address and file offset.
2208 do_reset_address_and_file_offset();
2210 // Write the data to the file. For a typical Output_section, this
2211 // does nothing: the data is written out by calling Object::Relocate
2212 // on each input object. But if there are any Output_section_data
2213 // objects we do need to write them out here.
2215 do_write(Output_file
*);
2217 // Return the address alignment--function required by parent class.
2219 do_addralign() const
2220 { return this->addralign_
; }
2222 // Return whether there is a load address.
2224 do_has_load_address() const
2225 { return this->has_load_address_
; }
2227 // Return the load address.
2229 do_load_address() const
2231 gold_assert(this->has_load_address_
);
2232 return this->load_address_
;
2235 // Return whether this is an Output_section.
2237 do_is_section() const
2240 // Return whether this is a section of the specified type.
2242 do_is_section_type(elfcpp::Elf_Word type
) const
2243 { return this->type_
== type
; }
2245 // Return whether the specified section flag is set.
2247 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2248 { return (this->flags_
& flag
) != 0; }
2250 // Set the TLS offset. Called only for SHT_TLS sections.
2252 do_set_tls_offset(uint64_t tls_base
);
2254 // Return the TLS offset, relative to the base of the TLS segment.
2255 // Valid only for SHT_TLS sections.
2257 do_tls_offset() const
2258 { return this->tls_offset_
; }
2260 // This may be implemented by a child class.
2262 do_finalize_name(Layout
*)
2265 // Record that this section requires postprocessing after all
2266 // relocations have been applied. This is called by a child class.
2268 set_requires_postprocessing()
2270 this->requires_postprocessing_
= true;
2271 this->after_input_sections_
= true;
2274 // Write all the data of an Output_section into the postprocessing
2277 write_to_postprocessing_buffer();
2280 // In some cases we need to keep a list of the input sections
2281 // associated with this output section. We only need the list if we
2282 // might have to change the offsets of the input section within the
2283 // output section after we add the input section. The ordinary
2284 // input sections will be written out when we process the object
2285 // file, and as such we don't need to track them here. We do need
2286 // to track Output_section_data objects here. We store instances of
2287 // this structure in a std::vector, so it must be a POD. There can
2288 // be many instances of this structure, so we use a union to save
2294 : shndx_(0), p2align_(0)
2296 this->u1_
.data_size
= 0;
2297 this->u2_
.object
= NULL
;
2300 // For an ordinary input section.
2301 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2304 p2align_(ffsll(static_cast<long long>(addralign
)))
2306 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2307 && shndx
!= MERGE_DATA_SECTION_CODE
2308 && shndx
!= MERGE_STRING_SECTION_CODE
);
2309 this->u1_
.data_size
= data_size
;
2310 this->u2_
.object
= object
;
2313 // For a non-merge output section.
2314 Input_section(Output_section_data
* posd
)
2315 : shndx_(OUTPUT_SECTION_CODE
),
2316 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2318 this->u1_
.data_size
= 0;
2319 this->u2_
.posd
= posd
;
2322 // For a merge section.
2323 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2325 ? MERGE_STRING_SECTION_CODE
2326 : MERGE_DATA_SECTION_CODE
),
2327 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2329 this->u1_
.entsize
= entsize
;
2330 this->u2_
.posd
= posd
;
2333 // The required alignment.
2337 return (this->p2align_
== 0
2339 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2342 // Return the required size.
2346 // Whether this is an input section.
2348 is_input_section() const
2350 return (this->shndx_
!= OUTPUT_SECTION_CODE
2351 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2352 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
2355 // Return whether this is a merge section which matches the
2358 is_merge_section(bool is_string
, uint64_t entsize
,
2359 uint64_t addralign
) const
2361 return (this->shndx_
== (is_string
2362 ? MERGE_STRING_SECTION_CODE
2363 : MERGE_DATA_SECTION_CODE
)
2364 && this->u1_
.entsize
== entsize
2365 && this->addralign() == addralign
);
2368 // Return the object for an input section.
2372 gold_assert(this->is_input_section());
2373 return this->u2_
.object
;
2376 // Return the input section index for an input section.
2380 gold_assert(this->is_input_section());
2381 return this->shndx_
;
2384 // Set the output section.
2386 set_output_section(Output_section
* os
)
2388 gold_assert(!this->is_input_section());
2389 this->u2_
.posd
->set_output_section(os
);
2392 // Set the address and file offset. This is called during
2393 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2394 // the enclosing section.
2396 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2397 off_t section_file_offset
);
2399 // Reset the address and file offset.
2401 reset_address_and_file_offset();
2403 // Finalize the data size.
2405 finalize_data_size();
2407 // Add an input section, for SHF_MERGE sections.
2409 add_input_section(Relobj
* object
, unsigned int shndx
)
2411 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2412 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2413 return this->u2_
.posd
->add_input_section(object
, shndx
);
2416 // Given an input OBJECT, an input section index SHNDX within that
2417 // object, and an OFFSET relative to the start of that input
2418 // section, return whether or not the output offset is known. If
2419 // this function returns true, it sets *POUTPUT to the offset in
2420 // the output section, relative to the start of the input section
2421 // in the output section. *POUTPUT may be different from OFFSET
2422 // for a merged section.
2424 output_offset(const Relobj
* object
, unsigned int shndx
,
2425 section_offset_type offset
,
2426 section_offset_type
*poutput
) const;
2428 // Return whether this is the merge section for the input section
2431 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2433 // Write out the data. This does nothing for an input section.
2435 write(Output_file
*);
2437 // Write the data to a buffer. This does nothing for an input
2440 write_to_buffer(unsigned char*);
2442 // Print statistics about merge sections to stderr.
2444 print_merge_stats(const char* section_name
)
2446 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2447 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2448 this->u2_
.posd
->print_merge_stats(section_name
);
2452 // Code values which appear in shndx_. If the value is not one of
2453 // these codes, it is the input section index in the object file.
2456 // An Output_section_data.
2457 OUTPUT_SECTION_CODE
= -1U,
2458 // An Output_section_data for an SHF_MERGE section with
2459 // SHF_STRINGS not set.
2460 MERGE_DATA_SECTION_CODE
= -2U,
2461 // An Output_section_data for an SHF_MERGE section with
2463 MERGE_STRING_SECTION_CODE
= -3U
2466 // For an ordinary input section, this is the section index in the
2467 // input file. For an Output_section_data, this is
2468 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2469 // MERGE_STRING_SECTION_CODE.
2470 unsigned int shndx_
;
2471 // The required alignment, stored as a power of 2.
2472 unsigned int p2align_
;
2475 // For an ordinary input section, the section size.
2477 // For OUTPUT_SECTION_CODE, this is not used. For
2478 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2484 // For an ordinary input section, the object which holds the
2487 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2488 // MERGE_STRING_SECTION_CODE, the data.
2489 Output_section_data
* posd
;
2493 typedef std::vector
<Input_section
> Input_section_list
;
2495 // This class is used to sort the input sections.
2496 class Input_section_sort_entry
;
2498 // This is the sort comparison function.
2499 struct Input_section_sort_compare
2502 operator()(const Input_section_sort_entry
&,
2503 const Input_section_sort_entry
&) const;
2506 // Fill data. This is used to fill in data between input sections.
2507 // It is also used for data statements (BYTE, WORD, etc.) in linker
2508 // scripts. When we have to keep track of the input sections, we
2509 // can use an Output_data_const, but we don't want to have to keep
2510 // track of input sections just to implement fills.
2514 Fill(off_t section_offset
, off_t length
)
2515 : section_offset_(section_offset
),
2516 length_(convert_to_section_size_type(length
))
2519 // Return section offset.
2521 section_offset() const
2522 { return this->section_offset_
; }
2524 // Return fill length.
2527 { return this->length_
; }
2530 // The offset within the output section.
2531 off_t section_offset_
;
2532 // The length of the space to fill.
2533 section_size_type length_
;
2536 typedef std::vector
<Fill
> Fill_list
;
2538 // Add a new output section by Input_section.
2540 add_output_section_data(Input_section
*);
2542 // Add an SHF_MERGE input section. Returns true if the section was
2545 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
2546 uint64_t entsize
, uint64_t addralign
);
2548 // Add an output SHF_MERGE section POSD to this output section.
2549 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2550 // ENTSIZE is the entity size. This returns the entry added to
2553 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
2556 // Sort the attached input sections.
2558 sort_attached_input_sections();
2560 // Most of these fields are only valid after layout.
2562 // The name of the section. This will point into a Stringpool.
2564 // The section address is in the parent class.
2565 // The section alignment.
2566 uint64_t addralign_
;
2567 // The section entry size.
2569 // The load address. This is only used when using a linker script
2570 // with a SECTIONS clause. The has_load_address_ field indicates
2571 // whether this field is valid.
2572 uint64_t load_address_
;
2573 // The file offset is in the parent class.
2574 // Set the section link field to the index of this section.
2575 const Output_data
* link_section_
;
2576 // If link_section_ is NULL, this is the link field.
2578 // Set the section info field to the index of this section.
2579 const Output_section
* info_section_
;
2580 // If info_section_ is NULL, set the info field to the symbol table
2581 // index of this symbol.
2582 const Symbol
* info_symndx_
;
2583 // If info_section_ and info_symndx_ are NULL, this is the section
2586 // The section type.
2587 const elfcpp::Elf_Word type_
;
2588 // The section flags.
2589 elfcpp::Elf_Xword flags_
;
2590 // The section index.
2591 unsigned int out_shndx_
;
2592 // If there is a STT_SECTION for this output section in the normal
2593 // symbol table, this is the symbol index. This starts out as zero.
2594 // It is initialized in Layout::finalize() to be the index, or -1U
2595 // if there isn't one.
2596 unsigned int symtab_index_
;
2597 // If there is a STT_SECTION for this output section in the dynamic
2598 // symbol table, this is the symbol index. This starts out as zero.
2599 // It is initialized in Layout::finalize() to be the index, or -1U
2600 // if there isn't one.
2601 unsigned int dynsym_index_
;
2602 // The input sections. This will be empty in cases where we don't
2603 // need to keep track of them.
2604 Input_section_list input_sections_
;
2605 // The offset of the first entry in input_sections_.
2606 off_t first_input_offset_
;
2607 // The fill data. This is separate from input_sections_ because we
2608 // often will need fill sections without needing to keep track of
2611 // If the section requires postprocessing, this buffer holds the
2612 // section contents during relocation.
2613 unsigned char* postprocessing_buffer_
;
2614 // Whether this output section needs a STT_SECTION symbol in the
2615 // normal symbol table. This will be true if there is a relocation
2617 bool needs_symtab_index_
: 1;
2618 // Whether this output section needs a STT_SECTION symbol in the
2619 // dynamic symbol table. This will be true if there is a dynamic
2620 // relocation which needs it.
2621 bool needs_dynsym_index_
: 1;
2622 // Whether the link field of this output section should point to the
2623 // normal symbol table.
2624 bool should_link_to_symtab_
: 1;
2625 // Whether the link field of this output section should point to the
2626 // dynamic symbol table.
2627 bool should_link_to_dynsym_
: 1;
2628 // Whether this section should be written after all the input
2629 // sections are complete.
2630 bool after_input_sections_
: 1;
2631 // Whether this section requires post processing after all
2632 // relocations have been applied.
2633 bool requires_postprocessing_
: 1;
2634 // Whether an input section was mapped to this output section
2635 // because of a SECTIONS clause in a linker script.
2636 bool found_in_sections_clause_
: 1;
2637 // Whether this section has an explicitly specified load address.
2638 bool has_load_address_
: 1;
2639 // True if the info_section_ field means the section index of the
2640 // section, false if it means the symbol index of the corresponding
2642 bool info_uses_section_index_
: 1;
2643 // True if the input sections attached to this output section may
2645 bool may_sort_attached_input_sections_
: 1;
2646 // True if the input sections attached to this output section must
2648 bool must_sort_attached_input_sections_
: 1;
2649 // True if the input sections attached to this output section have
2650 // already been sorted.
2651 bool attached_input_sections_are_sorted_
: 1;
2652 // True if this section holds relro data.
2654 // True if this section holds relro local data.
2655 bool is_relro_local_
: 1;
2656 // For SHT_TLS sections, the offset of this section relative to the base
2657 // of the TLS segment.
2658 uint64_t tls_offset_
;
2661 // An output segment. PT_LOAD segments are built from collections of
2662 // output sections. Other segments typically point within PT_LOAD
2663 // segments, and are built directly as needed.
2665 class Output_segment
2668 // Create an output segment, specifying the type and flags.
2669 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2671 // Return the virtual address.
2674 { return this->vaddr_
; }
2676 // Return the physical address.
2679 { return this->paddr_
; }
2681 // Return the segment type.
2684 { return this->type_
; }
2686 // Return the segment flags.
2689 { return this->flags_
; }
2691 // Return the memory size.
2694 { return this->memsz_
; }
2696 // Return the file size.
2699 { return this->filesz_
; }
2701 // Return the file offset.
2704 { return this->offset_
; }
2706 // Return the maximum alignment of the Output_data.
2708 maximum_alignment();
2710 // Add an Output_section to this segment.
2712 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
);
2714 // Remove an Output_section from this segment. It is an error if it
2717 remove_output_section(Output_section
* os
);
2719 // Add an Output_data (which is not an Output_section) to the start
2722 add_initial_output_data(Output_data
*);
2724 // Return true if this segment has any sections which hold actual
2725 // data, rather than being a BSS section.
2727 has_any_data_sections() const
2728 { return !this->output_data_
.empty(); }
2730 // Return the number of dynamic relocations applied to this segment.
2732 dynamic_reloc_count() const;
2734 // Return the address of the first section.
2736 first_section_load_address() const;
2738 // Return whether the addresses have been set already.
2740 are_addresses_set() const
2741 { return this->are_addresses_set_
; }
2743 // Set the addresses.
2745 set_addresses(uint64_t vaddr
, uint64_t paddr
)
2747 this->vaddr_
= vaddr
;
2748 this->paddr_
= paddr
;
2749 this->are_addresses_set_
= true;
2752 // Set the segment flags. This is only used if we have a PHDRS
2753 // clause which explicitly specifies the flags.
2755 set_flags(elfcpp::Elf_Word flags
)
2756 { this->flags_
= flags
; }
2758 // Set the address of the segment to ADDR and the offset to *POFF
2759 // and set the addresses and offsets of all contained output
2760 // sections accordingly. Set the section indexes of all contained
2761 // output sections starting with *PSHNDX. If RESET is true, first
2762 // reset the addresses of the contained sections. Return the
2763 // address of the immediately following segment. Update *POFF and
2764 // *PSHNDX. This should only be called for a PT_LOAD segment.
2766 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
, off_t
* poff
,
2767 unsigned int* pshndx
);
2769 // Set the minimum alignment of this segment. This may be adjusted
2770 // upward based on the section alignments.
2772 set_minimum_p_align(uint64_t align
)
2773 { this->min_p_align_
= align
; }
2775 // Set the offset of this segment based on the section. This should
2776 // only be called for a non-PT_LOAD segment.
2780 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2784 // Return the number of output sections.
2786 output_section_count() const;
2788 // Return the section attached to the list segment with the lowest
2789 // load address. This is used when handling a PHDRS clause in a
2792 section_with_lowest_load_address() const;
2794 // Write the segment header into *OPHDR.
2795 template<int size
, bool big_endian
>
2797 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2799 // Write the section headers of associated sections into V.
2800 template<int size
, bool big_endian
>
2802 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2803 unsigned int* pshndx
) const;
2806 Output_segment(const Output_segment
&);
2807 Output_segment
& operator=(const Output_segment
&);
2809 typedef std::list
<Output_data
*> Output_data_list
;
2811 // Find the maximum alignment in an Output_data_list.
2813 maximum_alignment_list(const Output_data_list
*);
2815 // Return whether the first data section is a relro section.
2817 is_first_section_relro() const;
2819 // Set the section addresses in an Output_data_list.
2821 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
2822 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
2823 bool* in_tls
, bool* in_relro
);
2825 // Return the number of Output_sections in an Output_data_list.
2827 output_section_count_list(const Output_data_list
*) const;
2829 // Return the number of dynamic relocs in an Output_data_list.
2831 dynamic_reloc_count_list(const Output_data_list
*) const;
2833 // Find the section with the lowest load address in an
2834 // Output_data_list.
2836 lowest_load_address_in_list(const Output_data_list
* pdl
,
2837 Output_section
** found
,
2838 uint64_t* found_lma
) const;
2840 // Write the section headers in the list into V.
2841 template<int size
, bool big_endian
>
2843 write_section_headers_list(const Layout
*, const Stringpool
*,
2844 const Output_data_list
*, unsigned char* v
,
2845 unsigned int* pshdx
) const;
2847 // The list of output data with contents attached to this segment.
2848 Output_data_list output_data_
;
2849 // The list of output data without contents attached to this segment.
2850 Output_data_list output_bss_
;
2851 // The segment virtual address.
2853 // The segment physical address.
2855 // The size of the segment in memory.
2857 // The maximum section alignment. The is_max_align_known_ field
2858 // indicates whether this has been finalized.
2859 uint64_t max_align_
;
2860 // The required minimum value for the p_align field. This is used
2861 // for PT_LOAD segments. Note that this does not mean that
2862 // addresses should be aligned to this value; it means the p_paddr
2863 // and p_vaddr fields must be congruent modulo this value. For
2864 // non-PT_LOAD segments, the dynamic linker works more efficiently
2865 // if the p_align field has the more conventional value, although it
2866 // can align as needed.
2867 uint64_t min_p_align_
;
2868 // The offset of the segment data within the file.
2870 // The size of the segment data in the file.
2872 // The segment type;
2873 elfcpp::Elf_Word type_
;
2874 // The segment flags.
2875 elfcpp::Elf_Word flags_
;
2876 // Whether we have finalized max_align_.
2877 bool is_max_align_known_
: 1;
2878 // Whether vaddr and paddr were set by a linker script.
2879 bool are_addresses_set_
: 1;
2882 // This class represents the output file.
2887 Output_file(const char* name
);
2889 // Indicate that this is a temporary file which should not be
2893 { this->is_temporary_
= true; }
2895 // Open the output file. FILE_SIZE is the final size of the file.
2897 open(off_t file_size
);
2899 // Resize the output file.
2901 resize(off_t file_size
);
2903 // Close the output file (flushing all buffered data) and make sure
2904 // there are no errors.
2908 // We currently always use mmap which makes the view handling quite
2909 // simple. In the future we may support other approaches.
2911 // Write data to the output file.
2913 write(off_t offset
, const void* data
, size_t len
)
2914 { memcpy(this->base_
+ offset
, data
, len
); }
2916 // Get a buffer to use to write to the file, given the offset into
2917 // the file and the size.
2919 get_output_view(off_t start
, size_t size
)
2921 gold_assert(start
>= 0
2922 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
2923 return this->base_
+ start
;
2926 // VIEW must have been returned by get_output_view. Write the
2927 // buffer to the file, passing in the offset and the size.
2929 write_output_view(off_t
, size_t, unsigned char*)
2932 // Get a read/write buffer. This is used when we want to write part
2933 // of the file, read it in, and write it again.
2935 get_input_output_view(off_t start
, size_t size
)
2936 { return this->get_output_view(start
, size
); }
2938 // Write a read/write buffer back to the file.
2940 write_input_output_view(off_t
, size_t, unsigned char*)
2943 // Get a read buffer. This is used when we just want to read part
2944 // of the file back it in.
2945 const unsigned char*
2946 get_input_view(off_t start
, size_t size
)
2947 { return this->get_output_view(start
, size
); }
2949 // Release a read bfufer.
2951 free_input_view(off_t
, size_t, const unsigned char*)
2955 // Map the file into memory and return a pointer to the map.
2959 // Unmap the file from memory (and flush to disk buffers).
2969 // Base of file mapped into memory.
2970 unsigned char* base_
;
2971 // True iff base_ points to a memory buffer rather than an output file.
2972 bool map_is_anonymous_
;
2973 // True if this is a temporary file which should not be output.
2977 } // End namespace gold.
2979 #endif // !defined(GOLD_OUTPUT_H)