1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "reloc-types.h"
36 class General_options
;
42 template<int size
, bool big_endian
>
44 template<int size
, bool big_endian
>
47 // An abtract class for data which has to go into the output file.
52 explicit Output_data()
53 : address_(0), data_size_(0), offset_(-1),
54 is_address_valid_(false), is_data_size_valid_(false),
55 is_offset_valid_(false),
56 dynamic_reloc_count_(0)
62 // Return the address. For allocated sections, this is only valid
63 // after Layout::finalize is finished.
67 gold_assert(this->is_address_valid_
);
68 return this->address_
;
71 // Return the size of the data. For allocated sections, this must
72 // be valid after Layout::finalize calls set_address, but need not
73 // be valid before then.
77 gold_assert(this->is_data_size_valid_
);
78 return this->data_size_
;
81 // Return the file offset. This is only valid after
82 // Layout::finalize is finished. For some non-allocated sections,
83 // it may not be valid until near the end of the link.
87 gold_assert(this->is_offset_valid_
);
91 // Return the required alignment.
94 { return this->do_addralign(); }
96 // Return whether this is an Output_section.
99 { return this->do_is_section(); }
101 // Return whether this is an Output_section of the specified type.
103 is_section_type(elfcpp::Elf_Word stt
) const
104 { return this->do_is_section_type(stt
); }
106 // Return whether this is an Output_section with the specified flag
109 is_section_flag_set(elfcpp::Elf_Xword shf
) const
110 { return this->do_is_section_flag_set(shf
); }
112 // Return the output section index, if there is an output section.
115 { return this->do_out_shndx(); }
117 // Set the output section index, if this is an output section.
119 set_out_shndx(unsigned int shndx
)
120 { this->do_set_out_shndx(shndx
); }
122 // Set the address and file offset of this data, and finalize the
123 // size of the data. This is called during Layout::finalize for
124 // allocated sections.
126 set_address_and_file_offset(uint64_t addr
, off_t off
)
128 this->set_address(addr
);
129 this->set_file_offset(off
);
130 this->finalize_data_size();
135 set_address(uint64_t addr
)
137 gold_assert(!this->is_address_valid_
);
138 this->address_
= addr
;
139 this->is_address_valid_
= true;
142 // Set the file offset.
144 set_file_offset(off_t off
)
146 gold_assert(!this->is_offset_valid_
);
148 this->is_offset_valid_
= true;
151 // Finalize the data size.
155 if (!this->is_data_size_valid_
)
157 // Tell the child class to set the data size.
158 this->set_final_data_size();
159 gold_assert(this->is_data_size_valid_
);
163 // Write the data to the output file. This is called after
164 // Layout::finalize is complete.
166 write(Output_file
* file
)
167 { this->do_write(file
); }
169 // This is called by Layout::finalize to note that the sizes of
170 // allocated sections must now be fixed.
173 { Output_data::allocated_sizes_are_fixed
= true; }
175 // Used to check that layout has been done.
178 { return Output_data::allocated_sizes_are_fixed
; }
180 // Count the number of dynamic relocations applied to this section.
183 { ++this->dynamic_reloc_count_
; }
185 // Return the number of dynamic relocations applied to this section.
187 dynamic_reloc_count() const
188 { return this->dynamic_reloc_count_
; }
191 // Functions that child classes may or in some cases must implement.
193 // Write the data to the output file.
195 do_write(Output_file
*) = 0;
197 // Return the required alignment.
199 do_addralign() const = 0;
201 // Return whether this is an Output_section.
203 do_is_section() const
206 // Return whether this is an Output_section of the specified type.
207 // This only needs to be implement by Output_section.
209 do_is_section_type(elfcpp::Elf_Word
) const
212 // Return whether this is an Output_section with the specific flag
213 // set. This only needs to be implemented by Output_section.
215 do_is_section_flag_set(elfcpp::Elf_Xword
) const
218 // Return the output section index, if there is an output section.
221 { gold_unreachable(); }
223 // Set the output section index, if this is an output section.
225 do_set_out_shndx(unsigned int)
226 { gold_unreachable(); }
228 // This is a hook for derived classes to set the data size. This is
229 // called by finalize_data_size, normally called during
230 // Layout::finalize, when the section address is set.
232 set_final_data_size()
233 { gold_unreachable(); }
235 // Functions that child classes may call.
237 // Whether the address is valid.
239 is_address_valid() const
240 { return this->is_address_valid_
; }
242 // Whether the file offset is valid.
244 is_offset_valid() const
245 { return this->is_offset_valid_
; }
247 // Whether the data size is valid.
249 is_data_size_valid() const
250 { return this->is_data_size_valid_
; }
252 // Set the size of the data.
254 set_data_size(off_t data_size
)
256 gold_assert(!this->is_data_size_valid_
);
257 this->data_size_
= data_size
;
258 this->is_data_size_valid_
= true;
261 // Get the current data size--this is for the convenience of
262 // sections which build up their size over time.
264 current_data_size_for_child() const
265 { return this->data_size_
; }
267 // Set the current data size--this is for the convenience of
268 // sections which build up their size over time.
270 set_current_data_size_for_child(off_t data_size
)
272 gold_assert(!this->is_data_size_valid_
);
273 this->data_size_
= data_size
;
276 // Return default alignment for the target size.
280 // Return default alignment for a specified size--32 or 64.
282 default_alignment_for_size(int size
);
285 Output_data(const Output_data
&);
286 Output_data
& operator=(const Output_data
&);
288 // This is used for verification, to make sure that we don't try to
289 // change any sizes of allocated sections after we set the section
291 static bool allocated_sizes_are_fixed
;
293 // Memory address in output file.
295 // Size of data in output file.
297 // File offset of contents in output file.
299 // Whether address_ is valid.
300 bool is_address_valid_
;
301 // Whether data_size_ is valid.
302 bool is_data_size_valid_
;
303 // Whether offset_ is valid.
304 bool is_offset_valid_
;
305 // Count of dynamic relocations applied to this section.
306 unsigned int dynamic_reloc_count_
;
309 // Output the section headers.
311 class Output_section_headers
: public Output_data
314 Output_section_headers(const Layout
*,
315 const Layout::Segment_list
*,
316 const Layout::Section_list
*,
320 // Write the data to the file.
322 do_write(Output_file
*);
324 // Return the required alignment.
327 { return Output_data::default_alignment(); }
330 // Write the data to the file with the right size and endianness.
331 template<int size
, bool big_endian
>
333 do_sized_write(Output_file
*);
335 const Layout
* layout_
;
336 const Layout::Segment_list
* segment_list_
;
337 const Layout::Section_list
* unattached_section_list_
;
338 const Stringpool
* secnamepool_
;
341 // Output the segment headers.
343 class Output_segment_headers
: public Output_data
346 Output_segment_headers(const Layout::Segment_list
& segment_list
);
349 // Write the data to the file.
351 do_write(Output_file
*);
353 // Return the required alignment.
356 { return Output_data::default_alignment(); }
359 // Write the data to the file with the right size and endianness.
360 template<int size
, bool big_endian
>
362 do_sized_write(Output_file
*);
364 const Layout::Segment_list
& segment_list_
;
367 // Output the ELF file header.
369 class Output_file_header
: public Output_data
372 Output_file_header(const Target
*,
374 const Output_segment_headers
*);
376 // Add information about the section headers. We lay out the ELF
377 // file header before we create the section headers.
378 void set_section_info(const Output_section_headers
*,
379 const Output_section
* shstrtab
);
382 // Write the data to the file.
384 do_write(Output_file
*);
386 // Return the required alignment.
389 { return Output_data::default_alignment(); }
392 // Write the data to the file with the right size and endianness.
393 template<int size
, bool big_endian
>
395 do_sized_write(Output_file
*);
397 const Target
* target_
;
398 const Symbol_table
* symtab_
;
399 const Output_segment_headers
* segment_header_
;
400 const Output_section_headers
* section_header_
;
401 const Output_section
* shstrtab_
;
404 // Output sections are mainly comprised of input sections. However,
405 // there are cases where we have data to write out which is not in an
406 // input section. Output_section_data is used in such cases. This is
407 // an abstract base class.
409 class Output_section_data
: public Output_data
412 Output_section_data(off_t data_size
, uint64_t addralign
)
413 : Output_data(), output_section_(NULL
), addralign_(addralign
)
414 { this->set_data_size(data_size
); }
416 Output_section_data(uint64_t addralign
)
417 : Output_data(), output_section_(NULL
), addralign_(addralign
)
420 // Return the output section.
421 const Output_section
*
422 output_section() const
423 { return this->output_section_
; }
425 // Record the output section.
427 set_output_section(Output_section
* os
);
429 // Add an input section, for SHF_MERGE sections. This returns true
430 // if the section was handled.
432 add_input_section(Relobj
* object
, unsigned int shndx
)
433 { return this->do_add_input_section(object
, shndx
); }
435 // Given an input OBJECT, an input section index SHNDX within that
436 // object, and an OFFSET relative to the start of that input
437 // section, return whether or not the corresponding offset within
438 // the output section is known. If this function returns true, it
439 // sets *POUTPUT to the output offset. The value -1 indicates that
440 // this input offset is being discarded.
442 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
443 off_t
*poutput
) const
444 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
446 // Write the contents to a buffer. This is used for sections which
447 // require postprocessing, such as compression.
449 write_to_buffer(unsigned char* buffer
)
450 { this->do_write_to_buffer(buffer
); }
453 // The child class must implement do_write.
455 // The child class may implement specific adjustments to the output
458 do_adjust_output_section(Output_section
*)
461 // May be implemented by child class. Return true if the section
464 do_add_input_section(Relobj
*, unsigned int)
465 { gold_unreachable(); }
467 // The child class may implement output_offset.
469 do_output_offset(const Relobj
*, unsigned int, off_t
, off_t
*) const
472 // The child class may implement write_to_buffer. Most child
473 // classes can not appear in a compressed section, and they do not
476 do_write_to_buffer(unsigned char*)
477 { gold_unreachable(); }
479 // Return the required alignment.
482 { return this->addralign_
; }
484 // Return the section index of the output section.
486 do_out_shndx() const;
488 // Set the alignment.
490 set_addralign(uint64_t addralign
)
491 { this->addralign_
= addralign
; }
494 // The output section for this section.
495 const Output_section
* output_section_
;
496 // The required alignment.
500 // Some Output_section_data classes build up their data step by step,
501 // rather than all at once. This class provides an interface for
504 class Output_section_data_build
: public Output_section_data
507 Output_section_data_build(uint64_t addralign
)
508 : Output_section_data(addralign
)
511 // Get the current data size.
513 current_data_size() const
514 { return this->current_data_size_for_child(); }
516 // Set the current data size.
518 set_current_data_size(off_t data_size
)
519 { this->set_current_data_size_for_child(data_size
); }
522 // Set the final data size.
524 set_final_data_size()
525 { this->set_data_size(this->current_data_size_for_child()); }
528 // A simple case of Output_data in which we have constant data to
531 class Output_data_const
: public Output_section_data
534 Output_data_const(const std::string
& data
, uint64_t addralign
)
535 : Output_section_data(data
.size(), addralign
), data_(data
)
538 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
539 : Output_section_data(len
, addralign
), data_(p
, len
)
542 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
543 : Output_section_data(len
, addralign
),
544 data_(reinterpret_cast<const char*>(p
), len
)
548 // Write the data to the output file.
550 do_write(Output_file
*);
552 // Write the data to a buffer.
554 do_write_to_buffer(unsigned char* buffer
)
555 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
561 // Another version of Output_data with constant data, in which the
562 // buffer is allocated by the caller.
564 class Output_data_const_buffer
: public Output_section_data
567 Output_data_const_buffer(const unsigned char* p
, off_t len
,
569 : Output_section_data(len
, addralign
), p_(p
)
573 // Write the data the output file.
575 do_write(Output_file
*);
577 // Write the data to a buffer.
579 do_write_to_buffer(unsigned char* buffer
)
580 { memcpy(buffer
, this->p_
, this->data_size()); }
583 const unsigned char* p_
;
586 // A place holder for a fixed amount of data written out via some
589 class Output_data_fixed_space
: public Output_section_data
592 Output_data_fixed_space(off_t data_size
, uint64_t addralign
)
593 : Output_section_data(data_size
, addralign
)
597 // Write out the data--the actual data must be written out
600 do_write(Output_file
*)
604 // A place holder for variable sized data written out via some other
607 class Output_data_space
: public Output_section_data_build
610 explicit Output_data_space(uint64_t addralign
)
611 : Output_section_data_build(addralign
)
614 // Set the alignment.
616 set_space_alignment(uint64_t align
)
617 { this->set_addralign(align
); }
620 // Write out the data--the actual data must be written out
623 do_write(Output_file
*)
627 // A string table which goes into an output section.
629 class Output_data_strtab
: public Output_section_data
632 Output_data_strtab(Stringpool
* strtab
)
633 : Output_section_data(1), strtab_(strtab
)
637 // This is called to set the address and file offset. Here we make
638 // sure that the Stringpool is finalized.
640 set_final_data_size();
642 // Write out the data.
644 do_write(Output_file
*);
646 // Write the data to a buffer.
648 do_write_to_buffer(unsigned char* buffer
)
649 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
655 // This POD class is used to represent a single reloc in the output
656 // file. This could be a private class within Output_data_reloc, but
657 // the templatization is complex enough that I broke it out into a
658 // separate class. The class is templatized on either elfcpp::SHT_REL
659 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
660 // relocation or an ordinary relocation.
662 // A relocation can be against a global symbol, a local symbol, an
663 // output section, or the undefined symbol at index 0. We represent
664 // the latter by using a NULL global symbol.
666 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
669 template<bool dynamic
, int size
, bool big_endian
>
670 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
673 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
675 // An uninitialized entry. We need this because we want to put
676 // instances of this class into an STL container.
678 : local_sym_index_(INVALID_CODE
)
681 // A reloc against a global symbol.
683 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
685 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
688 this->u1_
.gsym
= gsym
;
692 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
693 unsigned int shndx
, Address address
)
694 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
697 gold_assert(shndx
!= INVALID_CODE
);
698 this->u1_
.gsym
= gsym
;
699 this->u2_
.relobj
= relobj
;
702 // A reloc against a local symbol.
704 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
705 unsigned int local_sym_index
,
709 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
712 gold_assert(local_sym_index
!= GSYM_CODE
713 && local_sym_index
!= INVALID_CODE
);
714 this->u1_
.relobj
= relobj
;
718 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
719 unsigned int local_sym_index
,
723 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
726 gold_assert(local_sym_index
!= GSYM_CODE
727 && local_sym_index
!= INVALID_CODE
);
728 gold_assert(shndx
!= INVALID_CODE
);
729 this->u1_
.relobj
= relobj
;
730 this->u2_
.relobj
= relobj
;
733 // A reloc against the STT_SECTION symbol of an output section.
735 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
737 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
744 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
745 unsigned int shndx
, Address address
)
746 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
749 gold_assert(shndx
!= INVALID_CODE
);
751 this->u2_
.relobj
= relobj
;
754 // Write the reloc entry to an output view.
756 write(unsigned char* pov
) const;
758 // Write the offset and info fields to Write_rel.
759 template<typename Write_rel
>
760 void write_rel(Write_rel
*) const;
763 // Return the symbol index. We can't do a double template
764 // specialization, so we do a secondary template here.
766 get_symbol_index() const;
768 // Codes for local_sym_index_.
775 // Invalid uninitialized entry.
781 // For a local symbol, the object. We will never generate a
782 // relocation against a local symbol in a dynamic object; that
783 // doesn't make sense. And our callers will always be
784 // templatized, so we use Sized_relobj here.
785 Sized_relobj
<size
, big_endian
>* relobj
;
786 // For a global symbol, the symbol. If this is NULL, it indicates
787 // a relocation against the undefined 0 symbol.
789 // For a relocation against an output section, the output section.
794 // If shndx_ is not INVALID CODE, the object which holds the input
795 // section being used to specify the reloc address.
797 // If shndx_ is INVALID_CODE, the output data being used to
798 // specify the reloc address. This may be NULL if the reloc
799 // address is absolute.
802 // The address offset within the input section or the Output_data.
804 // For a local symbol, the local symbol index. This is GSYM_CODE
805 // for a global symbol, or INVALID_CODE for an uninitialized value.
806 unsigned int local_sym_index_
;
807 // The reloc type--a processor specific code.
809 // If the reloc address is an input section in an object, the
810 // section index. This is INVALID_CODE if the reloc address is
811 // specified in some other way.
815 // The SHT_RELA version of Output_reloc<>. This is just derived from
816 // the SHT_REL version of Output_reloc, but it adds an addend.
818 template<bool dynamic
, int size
, bool big_endian
>
819 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
822 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
823 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
825 // An uninitialized entry.
830 // A reloc against a global symbol.
832 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
833 Address address
, Addend addend
)
834 : rel_(gsym
, type
, od
, address
), addend_(addend
)
837 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
838 unsigned int shndx
, Address address
, Addend addend
)
839 : rel_(gsym
, type
, relobj
, shndx
, address
), addend_(addend
)
842 // A reloc against a local symbol.
844 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
845 unsigned int local_sym_index
,
846 unsigned int type
, Output_data
* od
, Address address
,
848 : rel_(relobj
, local_sym_index
, type
, od
, address
), addend_(addend
)
851 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
852 unsigned int local_sym_index
,
857 : rel_(relobj
, local_sym_index
, type
, shndx
, address
),
861 // A reloc against the STT_SECTION symbol of an output section.
863 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
864 Address address
, Addend addend
)
865 : rel_(os
, type
, od
, address
), addend_(addend
)
868 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
869 unsigned int shndx
, Address address
, Addend addend
)
870 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
873 // Write the reloc entry to an output view.
875 write(unsigned char* pov
) const;
879 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
884 // Output_data_reloc is used to manage a section containing relocs.
885 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
886 // indicates whether this is a dynamic relocation or a normal
887 // relocation. Output_data_reloc_base is a base class.
888 // Output_data_reloc is the real class, which we specialize based on
891 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
892 class Output_data_reloc_base
: public Output_section_data_build
895 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
896 typedef typename
Output_reloc_type::Address Address
;
897 static const int reloc_size
=
898 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
900 // Construct the section.
901 Output_data_reloc_base()
902 : Output_section_data_build(Output_data::default_alignment_for_size(size
))
906 // Write out the data.
908 do_write(Output_file
*);
910 // Set the entry size and the link.
912 do_adjust_output_section(Output_section
*os
);
914 // Add a relocation entry.
916 add(Output_data
*od
, const Output_reloc_type
& reloc
)
918 this->relocs_
.push_back(reloc
);
919 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
920 od
->add_dynamic_reloc();
924 typedef std::vector
<Output_reloc_type
> Relocs
;
929 // The class which callers actually create.
931 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
932 class Output_data_reloc
;
934 // The SHT_REL version of Output_data_reloc.
936 template<bool dynamic
, int size
, bool big_endian
>
937 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
938 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
941 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
945 typedef typename
Base::Output_reloc_type Output_reloc_type
;
946 typedef typename
Output_reloc_type::Address Address
;
949 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
952 // Add a reloc against a global symbol.
955 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
956 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
)); }
959 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
960 unsigned int shndx
, Address address
)
961 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
)); }
963 // Add a reloc against a local symbol.
966 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
967 unsigned int local_sym_index
, unsigned int type
,
968 Output_data
* od
, Address address
)
969 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
973 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
974 unsigned int local_sym_index
, unsigned int type
,
975 Output_data
* od
, unsigned int shndx
, Address address
)
976 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
980 // A reloc against the STT_SECTION symbol of an output section.
981 // OS is the Output_section that the relocation refers to; OD is
982 // the Output_data object being relocated.
985 add_output_section(Output_section
* os
, unsigned int type
,
986 Output_data
* od
, Address address
)
987 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
990 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
991 Relobj
* relobj
, unsigned int shndx
, Address address
)
992 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
995 // The SHT_RELA version of Output_data_reloc.
997 template<bool dynamic
, int size
, bool big_endian
>
998 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
999 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1002 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1006 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1007 typedef typename
Output_reloc_type::Address Address
;
1008 typedef typename
Output_reloc_type::Addend Addend
;
1011 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
1014 // Add a reloc against a global symbol.
1017 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1018 Address address
, Addend addend
)
1019 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
1022 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1023 unsigned int shndx
, Address address
,
1025 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1028 // Add a reloc against a local symbol.
1031 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1032 unsigned int local_sym_index
, unsigned int type
,
1033 Output_data
* od
, Address address
, Addend addend
)
1035 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1040 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1041 unsigned int local_sym_index
, unsigned int type
,
1042 Output_data
* od
, unsigned int shndx
, Address address
,
1045 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1049 // A reloc against the STT_SECTION symbol of an output section.
1052 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1053 Address address
, Addend addend
)
1054 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1057 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1058 unsigned int shndx
, Address address
, Addend addend
)
1059 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1063 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1064 // for one symbol--either a global symbol or a local symbol in an
1065 // object. The target specific code adds entries to the GOT as
1068 template<int size
, bool big_endian
>
1069 class Output_data_got
: public Output_section_data_build
1072 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1075 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1079 // Add an entry for a global symbol to the GOT. Return true if this
1080 // is a new GOT entry, false if the symbol was already in the GOT.
1082 add_global(Symbol
* gsym
);
1084 // Add an entry for a local symbol to the GOT. This returns true if
1085 // this is a new GOT entry, false if the symbol already has a GOT
1088 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
1090 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1091 // Return true if this is a new GOT entry, false if the symbol was
1092 // already in the GOT.
1094 add_global_tls(Symbol
* gsym
, bool need_pair
);
1096 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1097 // This returns true if this is a new GOT entry, false if the symbol
1098 // already has a GOT entry.
1100 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
1101 unsigned int sym_index
, bool need_pair
);
1103 // Add a constant to the GOT. This returns the offset of the new
1104 // entry from the start of the GOT.
1106 add_constant(Valtype constant
)
1108 this->entries_
.push_back(Got_entry(constant
));
1109 this->set_got_size();
1110 return this->last_got_offset();
1114 // Write out the GOT table.
1116 do_write(Output_file
*);
1119 // This POD class holds a single GOT entry.
1123 // Create a zero entry.
1125 : local_sym_index_(CONSTANT_CODE
)
1126 { this->u_
.constant
= 0; }
1128 // Create a global symbol entry.
1129 explicit Got_entry(Symbol
* gsym
)
1130 : local_sym_index_(GSYM_CODE
)
1131 { this->u_
.gsym
= gsym
; }
1133 // Create a local symbol entry.
1134 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1135 unsigned int local_sym_index
)
1136 : local_sym_index_(local_sym_index
)
1138 gold_assert(local_sym_index
!= GSYM_CODE
1139 && local_sym_index
!= CONSTANT_CODE
);
1140 this->u_
.object
= object
;
1143 // Create a constant entry. The constant is a host value--it will
1144 // be swapped, if necessary, when it is written out.
1145 explicit Got_entry(Valtype constant
)
1146 : local_sym_index_(CONSTANT_CODE
)
1147 { this->u_
.constant
= constant
; }
1149 // Write the GOT entry to an output view.
1151 write(unsigned char* pov
) const;
1162 // For a local symbol, the object.
1163 Sized_relobj
<size
, big_endian
>* object
;
1164 // For a global symbol, the symbol.
1166 // For a constant, the constant.
1169 // For a local symbol, the local symbol index. This is GSYM_CODE
1170 // for a global symbol, or CONSTANT_CODE for a constant.
1171 unsigned int local_sym_index_
;
1174 typedef std::vector
<Got_entry
> Got_entries
;
1176 // Return the offset into the GOT of GOT entry I.
1178 got_offset(unsigned int i
) const
1179 { return i
* (size
/ 8); }
1181 // Return the offset into the GOT of the last entry added.
1183 last_got_offset() const
1184 { return this->got_offset(this->entries_
.size() - 1); }
1186 // Set the size of the section.
1189 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1191 // The list of GOT entries.
1192 Got_entries entries_
;
1195 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1198 class Output_data_dynamic
: public Output_section_data
1201 Output_data_dynamic(Stringpool
* pool
)
1202 : Output_section_data(Output_data::default_alignment()),
1203 entries_(), pool_(pool
)
1206 // Add a new dynamic entry with a fixed numeric value.
1208 add_constant(elfcpp::DT tag
, unsigned int val
)
1209 { this->add_entry(Dynamic_entry(tag
, val
)); }
1211 // Add a new dynamic entry with the address of output data.
1213 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1214 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1216 // Add a new dynamic entry with the size of output data.
1218 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1219 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1221 // Add a new dynamic entry with the address of a symbol.
1223 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1224 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1226 // Add a new dynamic entry with a string.
1228 add_string(elfcpp::DT tag
, const char* str
)
1229 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1232 add_string(elfcpp::DT tag
, const std::string
& str
)
1233 { this->add_string(tag
, str
.c_str()); }
1236 // Adjust the output section to set the entry size.
1238 do_adjust_output_section(Output_section
*);
1240 // Set the final data size.
1242 set_final_data_size();
1244 // Write out the dynamic entries.
1246 do_write(Output_file
*);
1249 // This POD class holds a single dynamic entry.
1253 // Create an entry with a fixed numeric value.
1254 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1255 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1256 { this->u_
.val
= val
; }
1258 // Create an entry with the size or address of a section.
1259 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1261 classification_(section_size
1262 ? DYNAMIC_SECTION_SIZE
1263 : DYNAMIC_SECTION_ADDRESS
)
1264 { this->u_
.od
= od
; }
1266 // Create an entry with the address of a symbol.
1267 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1268 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1269 { this->u_
.sym
= sym
; }
1271 // Create an entry with a string.
1272 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1273 : tag_(tag
), classification_(DYNAMIC_STRING
)
1274 { this->u_
.str
= str
; }
1276 // Write the dynamic entry to an output view.
1277 template<int size
, bool big_endian
>
1279 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1287 DYNAMIC_SECTION_ADDRESS
,
1289 DYNAMIC_SECTION_SIZE
,
1298 // For DYNAMIC_NUMBER.
1300 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1301 const Output_data
* od
;
1302 // For DYNAMIC_SYMBOL.
1304 // For DYNAMIC_STRING.
1309 // The type of entry.
1310 Classification classification_
;
1313 // Add an entry to the list.
1315 add_entry(const Dynamic_entry
& entry
)
1316 { this->entries_
.push_back(entry
); }
1318 // Sized version of write function.
1319 template<int size
, bool big_endian
>
1321 sized_write(Output_file
* of
);
1323 // The type of the list of entries.
1324 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1327 Dynamic_entries entries_
;
1328 // The pool used for strings.
1332 // An output section. We don't expect to have too many output
1333 // sections, so we don't bother to do a template on the size.
1335 class Output_section
: public Output_data
1338 // Create an output section, giving the name, type, and flags.
1339 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1340 virtual ~Output_section();
1342 // Add a new input section SHNDX, named NAME, with header SHDR, from
1343 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1344 // which applies to this section, or 0 if none, or -1U if more than
1345 // one. Return the offset within the output section.
1346 template<int size
, bool big_endian
>
1348 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1350 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1351 unsigned int reloc_shndx
);
1353 // Add generated data POSD to this output section.
1355 add_output_section_data(Output_section_data
* posd
);
1357 // Return the section name.
1360 { return this->name_
; }
1362 // Return the section type.
1365 { return this->type_
; }
1367 // Return the section flags.
1370 { return this->flags_
; }
1372 // Return the entsize field.
1375 { return this->entsize_
; }
1377 // Set the entsize field.
1379 set_entsize(uint64_t v
);
1381 // Set the link field to the output section index of a section.
1383 set_link_section(const Output_data
* od
)
1385 gold_assert(this->link_
== 0
1386 && !this->should_link_to_symtab_
1387 && !this->should_link_to_dynsym_
);
1388 this->link_section_
= od
;
1391 // Set the link field to a constant.
1393 set_link(unsigned int v
)
1395 gold_assert(this->link_section_
== NULL
1396 && !this->should_link_to_symtab_
1397 && !this->should_link_to_dynsym_
);
1401 // Record that this section should link to the normal symbol table.
1403 set_should_link_to_symtab()
1405 gold_assert(this->link_section_
== NULL
1407 && !this->should_link_to_dynsym_
);
1408 this->should_link_to_symtab_
= true;
1411 // Record that this section should link to the dynamic symbol table.
1413 set_should_link_to_dynsym()
1415 gold_assert(this->link_section_
== NULL
1417 && !this->should_link_to_symtab_
);
1418 this->should_link_to_dynsym_
= true;
1421 // Return the info field.
1425 gold_assert(this->info_section_
== NULL
);
1429 // Set the info field to the output section index of a section.
1431 set_info_section(const Output_data
* od
)
1433 gold_assert(this->info_
== 0);
1434 this->info_section_
= od
;
1437 // Set the info field to a constant.
1439 set_info(unsigned int v
)
1441 gold_assert(this->info_section_
== NULL
);
1445 // Set the addralign field.
1447 set_addralign(uint64_t v
)
1448 { this->addralign_
= v
; }
1450 // Indicate that we need a symtab index.
1452 set_needs_symtab_index()
1453 { this->needs_symtab_index_
= true; }
1455 // Return whether we need a symtab index.
1457 needs_symtab_index() const
1458 { return this->needs_symtab_index_
; }
1460 // Get the symtab index.
1462 symtab_index() const
1464 gold_assert(this->symtab_index_
!= 0);
1465 return this->symtab_index_
;
1468 // Set the symtab index.
1470 set_symtab_index(unsigned int index
)
1472 gold_assert(index
!= 0);
1473 this->symtab_index_
= index
;
1476 // Indicate that we need a dynsym index.
1478 set_needs_dynsym_index()
1479 { this->needs_dynsym_index_
= true; }
1481 // Return whether we need a dynsym index.
1483 needs_dynsym_index() const
1484 { return this->needs_dynsym_index_
; }
1486 // Get the dynsym index.
1488 dynsym_index() const
1490 gold_assert(this->dynsym_index_
!= 0);
1491 return this->dynsym_index_
;
1494 // Set the dynsym index.
1496 set_dynsym_index(unsigned int index
)
1498 gold_assert(index
!= 0);
1499 this->dynsym_index_
= index
;
1502 // Return whether this section should be written after all the input
1503 // sections are complete.
1505 after_input_sections() const
1506 { return this->after_input_sections_
; }
1508 // Record that this section should be written after all the input
1509 // sections are complete.
1511 set_after_input_sections()
1512 { this->after_input_sections_
= true; }
1514 // Return whether this section requires postprocessing after all
1515 // relocations have been applied.
1517 requires_postprocessing() const
1518 { return this->requires_postprocessing_
; }
1520 // If a section requires postprocessing, return the buffer to use.
1522 postprocessing_buffer() const
1524 gold_assert(this->postprocessing_buffer_
!= NULL
);
1525 return this->postprocessing_buffer_
;
1528 // If a section requires postprocessing, create the buffer to use.
1530 create_postprocessing_buffer();
1532 // If a section requires postprocessing, this is the size of the
1533 // buffer to which relocations should be applied.
1535 postprocessing_buffer_size() const
1536 { return this->current_data_size_for_child(); }
1538 // Return whether the offset OFFSET in the input section SHNDX in
1539 // object OBJECT is being included in the link.
1541 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1542 off_t offset
) const;
1544 // Return the offset within the output section of OFFSET relative to
1545 // the start of input section SHNDX in object OBJECT.
1547 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
) const;
1549 // Return the output virtual address of OFFSET relative to the start
1550 // of input section SHNDX in object OBJECT.
1552 output_address(const Relobj
* object
, unsigned int shndx
,
1553 off_t offset
) const;
1555 // Write the section header into *OPHDR.
1556 template<int size
, bool big_endian
>
1558 write_header(const Layout
*, const Stringpool
*,
1559 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1562 // Return the section index in the output file.
1564 do_out_shndx() const
1566 gold_assert(this->out_shndx_
!= -1U);
1567 return this->out_shndx_
;
1570 // Set the output section index.
1572 do_set_out_shndx(unsigned int shndx
)
1574 gold_assert(this->out_shndx_
== -1U);
1575 this->out_shndx_
= shndx
;
1578 // Set the final data size of the Output_section. For a typical
1579 // Output_section, there is nothing to do, but if there are any
1580 // Output_section_data objects we need to set their final addresses
1583 set_final_data_size();
1585 // Write the data to the file. For a typical Output_section, this
1586 // does nothing: the data is written out by calling Object::Relocate
1587 // on each input object. But if there are any Output_section_data
1588 // objects we do need to write them out here.
1590 do_write(Output_file
*);
1592 // Return the address alignment--function required by parent class.
1594 do_addralign() const
1595 { return this->addralign_
; }
1597 // Return whether this is an Output_section.
1599 do_is_section() const
1602 // Return whether this is a section of the specified type.
1604 do_is_section_type(elfcpp::Elf_Word type
) const
1605 { return this->type_
== type
; }
1607 // Return whether the specified section flag is set.
1609 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1610 { return (this->flags_
& flag
) != 0; }
1612 // Modify the section name. This is only permitted for an
1613 // unallocated section, and only before the size has been finalized.
1614 // Otherwise the name will not get into Layout::namepool_.
1616 set_name(const char* newname
)
1618 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
1619 gold_assert(!this->is_data_size_valid());
1620 this->name_
= newname
;
1623 // This may be implemented by a child class.
1625 do_finalize_name(Layout
*)
1628 // Record that this section requires postprocessing after all
1629 // relocations have been applied. This is called by a child class.
1631 set_requires_postprocessing()
1633 this->requires_postprocessing_
= true;
1634 this->after_input_sections_
= true;
1637 // Write all the data of an Output_section into the postprocessing
1640 write_to_postprocessing_buffer();
1643 // In some cases we need to keep a list of the input sections
1644 // associated with this output section. We only need the list if we
1645 // might have to change the offsets of the input section within the
1646 // output section after we add the input section. The ordinary
1647 // input sections will be written out when we process the object
1648 // file, and as such we don't need to track them here. We do need
1649 // to track Output_section_data objects here. We store instances of
1650 // this structure in a std::vector, so it must be a POD. There can
1651 // be many instances of this structure, so we use a union to save
1657 : shndx_(0), p2align_(0)
1659 this->u1_
.data_size
= 0;
1660 this->u2_
.object
= NULL
;
1663 // For an ordinary input section.
1664 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1667 p2align_(ffsll(static_cast<long long>(addralign
)))
1669 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1670 && shndx
!= MERGE_DATA_SECTION_CODE
1671 && shndx
!= MERGE_STRING_SECTION_CODE
);
1672 this->u1_
.data_size
= data_size
;
1673 this->u2_
.object
= object
;
1676 // For a non-merge output section.
1677 Input_section(Output_section_data
* posd
)
1678 : shndx_(OUTPUT_SECTION_CODE
),
1679 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1681 this->u1_
.data_size
= 0;
1682 this->u2_
.posd
= posd
;
1685 // For a merge section.
1686 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1688 ? MERGE_STRING_SECTION_CODE
1689 : MERGE_DATA_SECTION_CODE
),
1690 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1692 this->u1_
.entsize
= entsize
;
1693 this->u2_
.posd
= posd
;
1696 // The required alignment.
1700 return (this->p2align_
== 0
1702 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1705 // Return the required size.
1709 // Return whether this is a merge section which matches the
1712 is_merge_section(bool is_string
, uint64_t entsize
,
1713 uint64_t addralign
) const
1715 return (this->shndx_
== (is_string
1716 ? MERGE_STRING_SECTION_CODE
1717 : MERGE_DATA_SECTION_CODE
)
1718 && this->u1_
.entsize
== entsize
1719 && this->addralign() == addralign
);
1722 // Set the output section.
1724 set_output_section(Output_section
* os
)
1726 gold_assert(!this->is_input_section());
1727 this->u2_
.posd
->set_output_section(os
);
1730 // Set the address and file offset. This is called during
1731 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
1732 // the enclosing section.
1734 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
1735 off_t section_file_offset
);
1737 // Finalize the data size.
1739 finalize_data_size();
1741 // Add an input section, for SHF_MERGE sections.
1743 add_input_section(Relobj
* object
, unsigned int shndx
)
1745 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1746 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1747 return this->u2_
.posd
->add_input_section(object
, shndx
);
1750 // Given an input OBJECT, an input section index SHNDX within that
1751 // object, and an OFFSET relative to the start of that input
1752 // section, return whether or not the output offset is known. If
1753 // this function returns true, it sets *POUTPUT to the output
1756 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1757 off_t
*poutput
) const;
1759 // Write out the data. This does nothing for an input section.
1761 write(Output_file
*);
1763 // Write the data to a buffer. This does nothing for an input
1766 write_to_buffer(unsigned char*);
1769 // Code values which appear in shndx_. If the value is not one of
1770 // these codes, it is the input section index in the object file.
1773 // An Output_section_data.
1774 OUTPUT_SECTION_CODE
= -1U,
1775 // An Output_section_data for an SHF_MERGE section with
1776 // SHF_STRINGS not set.
1777 MERGE_DATA_SECTION_CODE
= -2U,
1778 // An Output_section_data for an SHF_MERGE section with
1780 MERGE_STRING_SECTION_CODE
= -3U
1783 // Whether this is an input section.
1785 is_input_section() const
1787 return (this->shndx_
!= OUTPUT_SECTION_CODE
1788 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1789 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1792 // For an ordinary input section, this is the section index in the
1793 // input file. For an Output_section_data, this is
1794 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1795 // MERGE_STRING_SECTION_CODE.
1796 unsigned int shndx_
;
1797 // The required alignment, stored as a power of 2.
1798 unsigned int p2align_
;
1801 // For an ordinary input section, the section size.
1803 // For OUTPUT_SECTION_CODE, this is not used. For
1804 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1810 // For an ordinary input section, the object which holds the
1813 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1814 // MERGE_STRING_SECTION_CODE, the data.
1815 Output_section_data
* posd
;
1819 typedef std::vector
<Input_section
> Input_section_list
;
1821 // Fill data. This is used to fill in data between input sections.
1822 // When we have to keep track of the input sections, we can use an
1823 // Output_data_const, but we don't want to have to keep track of
1824 // input sections just to implement fills. For a fill we record the
1825 // offset, and the actual data to be written out.
1829 Fill(off_t section_offset
, off_t length
)
1830 : section_offset_(section_offset
), length_(length
)
1833 // Return section offset.
1835 section_offset() const
1836 { return this->section_offset_
; }
1838 // Return fill length.
1841 { return this->length_
; }
1844 // The offset within the output section.
1845 off_t section_offset_
;
1846 // The length of the space to fill.
1850 typedef std::vector
<Fill
> Fill_list
;
1852 // Add a new output section by Input_section.
1854 add_output_section_data(Input_section
*);
1856 // Add an SHF_MERGE input section. Returns true if the section was
1859 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1860 uint64_t entsize
, uint64_t addralign
);
1862 // Add an output SHF_MERGE section POSD to this output section.
1863 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1864 // ENTSIZE is the entity size. This returns the entry added to
1867 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1870 // Most of these fields are only valid after layout.
1872 // The name of the section. This will point into a Stringpool.
1874 // The section address is in the parent class.
1875 // The section alignment.
1876 uint64_t addralign_
;
1877 // The section entry size.
1879 // The file offset is in the parent class.
1880 // Set the section link field to the index of this section.
1881 const Output_data
* link_section_
;
1882 // If link_section_ is NULL, this is the link field.
1884 // Set the section info field to the index of this section.
1885 const Output_data
* info_section_
;
1886 // If info_section_ is NULL, this is the section info field.
1888 // The section type.
1889 const elfcpp::Elf_Word type_
;
1890 // The section flags.
1891 const elfcpp::Elf_Xword flags_
;
1892 // The section index.
1893 unsigned int out_shndx_
;
1894 // If there is a STT_SECTION for this output section in the normal
1895 // symbol table, this is the symbol index. This starts out as zero.
1896 // It is initialized in Layout::finalize() to be the index, or -1U
1897 // if there isn't one.
1898 unsigned int symtab_index_
;
1899 // If there is a STT_SECTION for this output section in the dynamic
1900 // symbol table, this is the symbol index. This starts out as zero.
1901 // It is initialized in Layout::finalize() to be the index, or -1U
1902 // if there isn't one.
1903 unsigned int dynsym_index_
;
1904 // The input sections. This will be empty in cases where we don't
1905 // need to keep track of them.
1906 Input_section_list input_sections_
;
1907 // The offset of the first entry in input_sections_.
1908 off_t first_input_offset_
;
1909 // The fill data. This is separate from input_sections_ because we
1910 // often will need fill sections without needing to keep track of
1913 // If the section requires postprocessing, this buffer holds the
1914 // section contents during relocation.
1915 unsigned char* postprocessing_buffer_
;
1916 // Whether this output section needs a STT_SECTION symbol in the
1917 // normal symbol table. This will be true if there is a relocation
1919 bool needs_symtab_index_
: 1;
1920 // Whether this output section needs a STT_SECTION symbol in the
1921 // dynamic symbol table. This will be true if there is a dynamic
1922 // relocation which needs it.
1923 bool needs_dynsym_index_
: 1;
1924 // Whether the link field of this output section should point to the
1925 // normal symbol table.
1926 bool should_link_to_symtab_
: 1;
1927 // Whether the link field of this output section should point to the
1928 // dynamic symbol table.
1929 bool should_link_to_dynsym_
: 1;
1930 // Whether this section should be written after all the input
1931 // sections are complete.
1932 bool after_input_sections_
: 1;
1933 // Whether this section requires post processing after all
1934 // relocations have been applied.
1935 bool requires_postprocessing_
: 1;
1938 // An output segment. PT_LOAD segments are built from collections of
1939 // output sections. Other segments typically point within PT_LOAD
1940 // segments, and are built directly as needed.
1942 class Output_segment
1945 // Create an output segment, specifying the type and flags.
1946 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1948 // Return the virtual address.
1951 { return this->vaddr_
; }
1953 // Return the physical address.
1956 { return this->paddr_
; }
1958 // Return the segment type.
1961 { return this->type_
; }
1963 // Return the segment flags.
1966 { return this->flags_
; }
1968 // Return the memory size.
1971 { return this->memsz_
; }
1973 // Return the file size.
1976 { return this->filesz_
; }
1978 // Return the maximum alignment of the Output_data.
1982 // Add an Output_section to this segment.
1984 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1985 { this->add_output_section(os
, seg_flags
, false); }
1987 // Add an Output_section to the start of this segment.
1989 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1990 { this->add_output_section(os
, seg_flags
, true); }
1992 // Add an Output_data (which is not an Output_section) to the start
1995 add_initial_output_data(Output_data
*);
1997 // Return the number of dynamic relocations applied to this segment.
1999 dynamic_reloc_count() const;
2001 // Set the address of the segment to ADDR and the offset to *POFF
2002 // (aligned if necessary), and set the addresses and offsets of all
2003 // contained output sections accordingly. Set the section indexes
2004 // of all contained output sections starting with *PSHNDX. Return
2005 // the address of the immediately following segment. Update *POFF
2006 // and *PSHNDX. This should only be called for a PT_LOAD segment.
2008 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
2010 // Set the minimum alignment of this segment. This may be adjusted
2011 // upward based on the section alignments.
2013 set_minimum_addralign(uint64_t align
)
2015 gold_assert(!this->is_align_known_
);
2016 this->align_
= align
;
2019 // Set the offset of this segment based on the section. This should
2020 // only be called for a non-PT_LOAD segment.
2024 // Return the number of output sections.
2026 output_section_count() const;
2028 // Write the segment header into *OPHDR.
2029 template<int size
, bool big_endian
>
2031 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2033 // Write the section headers of associated sections into V.
2034 template<int size
, bool big_endian
>
2036 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2037 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
2040 Output_segment(const Output_segment
&);
2041 Output_segment
& operator=(const Output_segment
&);
2043 typedef std::list
<Output_data
*> Output_data_list
;
2045 // Add an Output_section to this segment, specifying front or back.
2047 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2050 // Find the maximum alignment in an Output_data_list.
2052 maximum_alignment(const Output_data_list
*);
2054 // Set the section addresses in an Output_data_list.
2056 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
2057 unsigned int* pshndx
);
2059 // Return the number of Output_sections in an Output_data_list.
2061 output_section_count_list(const Output_data_list
*) const;
2063 // Return the number of dynamic relocs in an Output_data_list.
2065 dynamic_reloc_count_list(const Output_data_list
*) const;
2067 // Write the section headers in the list into V.
2068 template<int size
, bool big_endian
>
2070 write_section_headers_list(const Layout
*, const Stringpool
*,
2071 const Output_data_list
*, unsigned char* v
,
2072 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
2074 // The list of output data with contents attached to this segment.
2075 Output_data_list output_data_
;
2076 // The list of output data without contents attached to this segment.
2077 Output_data_list output_bss_
;
2078 // The segment virtual address.
2080 // The segment physical address.
2082 // The size of the segment in memory.
2084 // The segment alignment. The is_align_known_ field indicates
2085 // whether this has been finalized. It can be set to a minimum
2086 // value before it is finalized.
2088 // The offset of the segment data within the file.
2090 // The size of the segment data in the file.
2092 // The segment type;
2093 elfcpp::Elf_Word type_
;
2094 // The segment flags.
2095 elfcpp::Elf_Word flags_
;
2096 // Whether we have finalized align_.
2097 bool is_align_known_
;
2100 // This class represents the output file.
2105 Output_file(const General_options
& options
, Target
*);
2107 // Get a pointer to the target.
2110 { return this->target_
; }
2112 // Open the output file. FILE_SIZE is the final size of the file.
2114 open(off_t file_size
);
2116 // Resize the output file.
2118 resize(off_t file_size
);
2120 // Close the output file (flushing all buffered data) and make sure
2121 // there are no errors.
2125 // We currently always use mmap which makes the view handling quite
2126 // simple. In the future we may support other approaches.
2128 // Write data to the output file.
2130 write(off_t offset
, const void* data
, off_t len
)
2131 { memcpy(this->base_
+ offset
, data
, len
); }
2133 // Get a buffer to use to write to the file, given the offset into
2134 // the file and the size.
2136 get_output_view(off_t start
, off_t size
)
2138 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
2139 return this->base_
+ start
;
2142 // VIEW must have been returned by get_output_view. Write the
2143 // buffer to the file, passing in the offset and the size.
2145 write_output_view(off_t
, off_t
, unsigned char*)
2148 // Get a read/write buffer. This is used when we want to write part
2149 // of the file, read it in, and write it again.
2151 get_input_output_view(off_t start
, off_t size
)
2152 { return this->get_output_view(start
, size
); }
2154 // Write a read/write buffer back to the file.
2156 write_input_output_view(off_t
, off_t
, unsigned char*)
2159 // Get a read buffer. This is used when we just want to read part
2160 // of the file back it in.
2161 const unsigned char*
2162 get_input_view(off_t start
, off_t size
)
2163 { return this->get_output_view(start
, size
); }
2165 // Release a read bfufer.
2167 free_input_view(off_t
, off_t
, const unsigned char*)
2171 // Map the file into memory and return a pointer to the map.
2175 // Unmap the file from memory (and flush to disk buffers).
2181 const General_options
& options_
;
2190 // Base of file mapped into memory.
2191 unsigned char* base_
;
2192 // True iff base_ points to a memory buffer rather than an output file.
2193 bool map_is_anonymous_
;
2196 } // End namespace gold.
2198 #endif // !defined(GOLD_OUTPUT_H)