1 // output.h -- manage the output file for gold -*- C++ -*-
11 #include "reloc-types.h"
16 class General_options
;
22 template<int size
, bool big_endian
>
24 template<int size
, bool big_endian
>
27 // An abtract class for data which has to go into the output file.
32 explicit Output_data(off_t data_size
= 0)
33 : address_(0), data_size_(data_size
), offset_(-1)
39 // Return the address. This is only valid after Layout::finalize is
43 { return this->address_
; }
45 // Return the size of the data. This must be valid after
46 // Layout::finalize calls set_address, but need not be valid before
50 { return this->data_size_
; }
52 // Return the file offset. This is only valid after
53 // Layout::finalize is finished.
56 { return this->offset_
; }
58 // Return the required alignment.
61 { return this->do_addralign(); }
63 // Return whether this is an Output_section.
66 { return this->do_is_section(); }
68 // Return whether this is an Output_section of the specified type.
70 is_section_type(elfcpp::Elf_Word stt
) const
71 { return this->do_is_section_type(stt
); }
73 // Return whether this is an Output_section with the specified flag
76 is_section_flag_set(elfcpp::Elf_Xword shf
) const
77 { return this->do_is_section_flag_set(shf
); }
79 // Return the output section index, if there is an output section.
82 { return this->do_out_shndx(); }
84 // Set the output section index, if this is an output section.
86 set_out_shndx(unsigned int shndx
)
87 { this->do_set_out_shndx(shndx
); }
89 // Set the address and file offset of this data. This is called
90 // during Layout::finalize.
92 set_address(uint64_t addr
, off_t off
);
94 // Write the data to the output file. This is called after
95 // Layout::finalize is complete.
97 write(Output_file
* file
)
98 { this->do_write(file
); }
100 // This is called by Layout::finalize to note that all sizes must
104 { Output_data::sizes_are_fixed
= true; }
107 // Functions that child classes may or in some cases must implement.
109 // Write the data to the output file.
111 do_write(Output_file
*) = 0;
113 // Return the required alignment.
115 do_addralign() const = 0;
117 // Return whether this is an Output_section.
119 do_is_section() const
122 // Return whether this is an Output_section of the specified type.
123 // This only needs to be implement by Output_section.
125 do_is_section_type(elfcpp::Elf_Word
) const
128 // Return whether this is an Output_section with the specific flag
129 // set. This only needs to be implemented by Output_section.
131 do_is_section_flag_set(elfcpp::Elf_Xword
) const
134 // Return the output section index, if there is an output section.
137 { gold_unreachable(); }
139 // Set the output section index, if this is an output section.
141 do_set_out_shndx(unsigned int)
142 { gold_unreachable(); }
144 // Set the address and file offset of the data. This only needs to
145 // be implemented if the child needs to know. The child class can
146 // set its size in this call.
148 do_set_address(uint64_t, off_t
)
151 // Functions that child classes may call.
153 // Set the size of the data.
155 set_data_size(off_t data_size
)
157 gold_assert(!Output_data::sizes_are_fixed
);
158 this->data_size_
= data_size
;
161 // Return default alignment for a size--32 or 64.
163 default_alignment(int size
);
166 Output_data(const Output_data
&);
167 Output_data
& operator=(const Output_data
&);
169 // This is used for verification, to make sure that we don't try to
170 // change any sizes after we set the section addresses.
171 static bool sizes_are_fixed
;
173 // Memory address in file (not always meaningful).
175 // Size of data in file.
177 // Offset within file.
181 // Output the section headers.
183 class Output_section_headers
: public Output_data
186 Output_section_headers(int size
,
189 const Layout::Segment_list
*,
190 const Layout::Section_list
*,
193 // Write the data to the file.
195 do_write(Output_file
*);
197 // Return the required alignment.
200 { return Output_data::default_alignment(this->size_
); }
203 // Write the data to the file with the right size and endianness.
204 template<int size
, bool big_endian
>
206 do_sized_write(Output_file
*);
210 const Layout
* layout_
;
211 const Layout::Segment_list
* segment_list_
;
212 const Layout::Section_list
* unattached_section_list_
;
213 const Stringpool
* secnamepool_
;
216 // Output the segment headers.
218 class Output_segment_headers
: public Output_data
221 Output_segment_headers(int size
, bool big_endian
,
222 const Layout::Segment_list
& segment_list
);
224 // Write the data to the file.
226 do_write(Output_file
*);
228 // Return the required alignment.
231 { return Output_data::default_alignment(this->size_
); }
234 // Write the data to the file with the right size and endianness.
235 template<int size
, bool big_endian
>
237 do_sized_write(Output_file
*);
241 const Layout::Segment_list
& segment_list_
;
244 // Output the ELF file header.
246 class Output_file_header
: public Output_data
249 Output_file_header(int size
,
251 const General_options
&,
254 const Output_segment_headers
*);
256 // Add information about the section headers. We lay out the ELF
257 // file header before we create the section headers.
258 void set_section_info(const Output_section_headers
*,
259 const Output_section
* shstrtab
);
261 // Write the data to the file.
263 do_write(Output_file
*);
265 // Return the required alignment.
268 { return Output_data::default_alignment(this->size_
); }
270 // Set the address and offset--we only implement this for error
273 do_set_address(uint64_t, off_t off
) const
274 { gold_assert(off
== 0); }
277 // Write the data to the file with the right size and endianness.
278 template<int size
, bool big_endian
>
280 do_sized_write(Output_file
*);
284 const General_options
& options_
;
285 const Target
* target_
;
286 const Symbol_table
* symtab_
;
287 const Output_segment_headers
* segment_header_
;
288 const Output_section_headers
* section_header_
;
289 const Output_section
* shstrtab_
;
292 // Output sections are mainly comprised of input sections. However,
293 // there are cases where we have data to write out which is not in an
294 // input section. Output_section_data is used in such cases. This is
295 // an abstract base class.
297 class Output_section_data
: public Output_data
300 Output_section_data(off_t data_size
, uint64_t addralign
)
301 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
304 Output_section_data(uint64_t addralign
)
305 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
308 // Return the output section.
309 const Output_section
*
310 output_section() const
311 { return this->output_section_
; }
313 // Record the output section.
315 set_output_section(Output_section
* os
);
317 // Add an input section, for SHF_MERGE sections. This returns true
318 // if the section was handled.
320 add_input_section(Relobj
* object
, unsigned int shndx
)
321 { return this->do_add_input_section(object
, shndx
); }
323 // Given an input OBJECT, an input section index SHNDX within that
324 // object, and an OFFSET relative to the start of that input
325 // section, return whether or not the output address is known.
326 // OUTPUT_SECTION_ADDRESS is the address of the output section which
327 // this is a part of. If this function returns true, it sets
328 // *POUTPUT to the output address.
330 output_address(const Relobj
* object
, unsigned int shndx
, off_t offset
,
331 uint64_t output_section_address
, uint64_t *poutput
) const
333 return this->do_output_address(object
, shndx
, offset
,
334 output_section_address
, poutput
);
338 // The child class must implement do_write.
340 // The child class may implement specific adjustments to the output
343 do_adjust_output_section(Output_section
*)
346 // May be implemented by child class. Return true if the section
349 do_add_input_section(Relobj
*, unsigned int)
350 { gold_unreachable(); }
352 // The child class may implement output_address.
354 do_output_address(const Relobj
*, unsigned int, off_t
, uint64_t,
358 // Return the required alignment.
361 { return this->addralign_
; }
363 // Return the section index of the output section.
365 do_out_shndx() const;
367 // Set the alignment.
369 set_addralign(uint64_t addralign
)
370 { this->addralign_
= addralign
; }
373 // The output section for this section.
374 const Output_section
* output_section_
;
375 // The required alignment.
379 // A simple case of Output_data in which we have constant data to
382 class Output_data_const
: public Output_section_data
385 Output_data_const(const std::string
& data
, uint64_t addralign
)
386 : Output_section_data(data
.size(), addralign
), data_(data
)
389 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
390 : Output_section_data(len
, addralign
), data_(p
, len
)
393 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
394 : Output_section_data(len
, addralign
),
395 data_(reinterpret_cast<const char*>(p
), len
)
400 add_data(const std::string
& add
)
402 this->data_
.append(add
);
403 this->set_data_size(this->data_
.size());
406 // Write the data to the output file.
408 do_write(Output_file
*);
414 // Another version of Output_data with constant data, in which the
415 // buffer is allocated by the caller.
417 class Output_data_const_buffer
: public Output_section_data
420 Output_data_const_buffer(const unsigned char* p
, off_t len
,
422 : Output_section_data(len
, addralign
), p_(p
)
425 // Write the data the output file.
427 do_write(Output_file
*);
430 const unsigned char* p_
;
433 // A place holder for data written out via some other mechanism.
435 class Output_data_space
: public Output_section_data
438 Output_data_space(off_t data_size
, uint64_t addralign
)
439 : Output_section_data(data_size
, addralign
)
442 explicit Output_data_space(uint64_t addralign
)
443 : Output_section_data(addralign
)
448 set_space_size(off_t space_size
)
449 { this->set_data_size(space_size
); }
451 // Set the alignment.
453 set_space_alignment(uint64_t align
)
454 { this->set_addralign(align
); }
456 // Write out the data--this must be handled elsewhere.
458 do_write(Output_file
*)
462 // A string table which goes into an output section.
464 class Output_data_strtab
: public Output_section_data
467 Output_data_strtab(Stringpool
* strtab
)
468 : Output_section_data(1), strtab_(strtab
)
471 // This is called to set the address and file offset. Here we make
472 // sure that the Stringpool is finalized.
474 do_set_address(uint64_t, off_t
);
476 // Write out the data.
478 do_write(Output_file
*);
484 // This POD class is used to represent a single reloc in the output
485 // file. This could be a private class within Output_data_reloc, but
486 // the templatization is complex enough that I broke it out into a
487 // separate class. The class is templatized on either elfcpp::SHT_REL
488 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
489 // relocation or an ordinary relocation.
491 // A relocation can be against a global symbol, a local symbol, an
492 // output section, or the undefined symbol at index 0. We represent
493 // the latter by using a NULL global symbol.
495 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
498 template<bool dynamic
, int size
, bool big_endian
>
499 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
502 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
504 // An uninitialized entry. We need this because we want to put
505 // instances of this class into an STL container.
507 : local_sym_index_(INVALID_CODE
)
510 // A reloc against a global symbol.
512 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
514 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
517 this->u1_
.gsym
= gsym
;
521 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
522 unsigned int shndx
, Address address
)
523 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
526 gold_assert(shndx
!= INVALID_CODE
);
527 this->u1_
.gsym
= gsym
;
528 this->u2_
.relobj
= relobj
;
531 // A reloc against a local symbol.
533 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
534 unsigned int local_sym_index
,
538 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
541 gold_assert(local_sym_index
!= GSYM_CODE
542 && local_sym_index
!= INVALID_CODE
);
543 this->u1_
.relobj
= relobj
;
547 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
548 unsigned int local_sym_index
,
552 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
555 gold_assert(local_sym_index
!= GSYM_CODE
556 && local_sym_index
!= INVALID_CODE
);
557 gold_assert(shndx
!= INVALID_CODE
);
558 this->u1_
.relobj
= relobj
;
559 this->u2_
.relobj
= relobj
;
562 // A reloc against the STT_SECTION symbol of an output section.
564 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
566 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
573 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
574 unsigned int shndx
, Address address
)
575 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
578 gold_assert(shndx
!= INVALID_CODE
);
580 this->u2_
.relobj
= relobj
;
583 // Write the reloc entry to an output view.
585 write(unsigned char* pov
) const;
587 // Write the offset and info fields to Write_rel.
588 template<typename Write_rel
>
589 void write_rel(Write_rel
*) const;
592 // Return the symbol index. We can't do a double template
593 // specialization, so we do a secondary template here.
595 get_symbol_index() const;
597 // Codes for local_sym_index_.
604 // Invalid uninitialized entry.
610 // For a local symbol, the object. We will never generate a
611 // relocation against a local symbol in a dynamic object; that
612 // doesn't make sense. And our callers will always be
613 // templatized, so we use Sized_relobj here.
614 Sized_relobj
<size
, big_endian
>* relobj
;
615 // For a global symbol, the symbol. If this is NULL, it indicates
616 // a relocation against the undefined 0 symbol.
618 // For a relocation against an output section, the output section.
623 // If shndx_ is not INVALID CODE, the object which holds the input
624 // section being used to specify the reloc address.
626 // If shndx_ is INVALID_CODE, the output data being used to
627 // specify the reloc address. This may be NULL if the reloc
628 // address is absolute.
631 // The address offset within the input section or the Output_data.
633 // For a local symbol, the local symbol index. This is GSYM_CODE
634 // for a global symbol, or INVALID_CODE for an uninitialized value.
635 unsigned int local_sym_index_
;
636 // The reloc type--a processor specific code.
638 // If the reloc address is an input section in an object, the
639 // section index. This is INVALID_CODE if the reloc address is
640 // specified in some other way.
644 // The SHT_RELA version of Output_reloc<>. This is just derived from
645 // the SHT_REL version of Output_reloc, but it adds an addend.
647 template<bool dynamic
, int size
, bool big_endian
>
648 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
651 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
652 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
654 // An uninitialized entry.
659 // A reloc against a global symbol.
661 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
662 Address address
, Addend addend
)
663 : rel_(gsym
, type
, od
, address
), addend_(addend
)
666 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
667 unsigned int shndx
, Address address
, Addend addend
)
668 : rel_(gsym
, type
, relobj
, shndx
, address
), addend_(addend
)
671 // A reloc against a local symbol.
673 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
674 unsigned int local_sym_index
,
675 unsigned int type
, Output_data
* od
, Address address
,
677 : rel_(relobj
, local_sym_index
, type
, od
, address
), addend_(addend
)
680 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
681 unsigned int local_sym_index
,
686 : rel_(relobj
, local_sym_index
, type
, shndx
, address
),
690 // A reloc against the STT_SECTION symbol of an output section.
692 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
693 Address address
, Addend addend
)
694 : rel_(os
, type
, od
, address
), addend_(addend
)
697 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
698 unsigned int shndx
, Address address
, Addend addend
)
699 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
702 // Write the reloc entry to an output view.
704 write(unsigned char* pov
) const;
708 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
713 // Output_data_reloc is used to manage a section containing relocs.
714 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
715 // indicates whether this is a dynamic relocation or a normal
716 // relocation. Output_data_reloc_base is a base class.
717 // Output_data_reloc is the real class, which we specialize based on
720 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
721 class Output_data_reloc_base
: public Output_section_data
724 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
725 typedef typename
Output_reloc_type::Address Address
;
726 static const int reloc_size
=
727 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
729 // Construct the section.
730 Output_data_reloc_base()
731 : Output_section_data(Output_data::default_alignment(size
))
734 // Write out the data.
736 do_write(Output_file
*);
739 // Set the entry size and the link.
741 do_adjust_output_section(Output_section
*os
);
743 // Add a relocation entry.
745 add(const Output_reloc_type
& reloc
)
747 this->relocs_
.push_back(reloc
);
748 this->set_data_size(this->relocs_
.size() * reloc_size
);
752 typedef std::vector
<Output_reloc_type
> Relocs
;
757 // The class which callers actually create.
759 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
760 class Output_data_reloc
;
762 // The SHT_REL version of Output_data_reloc.
764 template<bool dynamic
, int size
, bool big_endian
>
765 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
766 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
769 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
773 typedef typename
Base::Output_reloc_type Output_reloc_type
;
774 typedef typename
Output_reloc_type::Address Address
;
777 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
780 // Add a reloc against a global symbol.
783 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
784 { this->add(Output_reloc_type(gsym
, type
, od
, address
)); }
787 add_global(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
788 unsigned int shndx
, Address address
)
789 { this->add(Output_reloc_type(gsym
, type
, relobj
, shndx
, address
)); }
791 // Add a reloc against a local symbol.
794 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
795 unsigned int local_sym_index
, unsigned int type
,
796 Output_data
* od
, Address address
)
797 { this->add(Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
)); }
800 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
801 unsigned int local_sym_index
, unsigned int type
,
802 unsigned int shndx
, Address address
)
803 { this->add(Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
807 // A reloc against the STT_SECTION symbol of an output section.
810 add_output_section(Output_section
* os
, unsigned int type
,
811 Output_data
* od
, Address address
)
812 { this->add(Output_reloc_type(os
, type
, od
, address
)); }
815 add_output_section(Output_section
* os
, unsigned int type
,
816 Relobj
* relobj
, unsigned int shndx
, Address address
)
817 { this->add(Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
820 // The SHT_RELA version of Output_data_reloc.
822 template<bool dynamic
, int size
, bool big_endian
>
823 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
824 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
827 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
831 typedef typename
Base::Output_reloc_type Output_reloc_type
;
832 typedef typename
Output_reloc_type::Address Address
;
833 typedef typename
Output_reloc_type::Addend Addend
;
836 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
839 // Add a reloc against a global symbol.
842 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
843 Address address
, Addend addend
)
844 { this->add(Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
847 add_global(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
848 unsigned int shndx
, Address address
, Addend addend
)
849 { this->add(Output_reloc_type(gsym
, type
, relobj
, shndx
, address
, addend
)); }
851 // Add a reloc against a local symbol.
854 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
855 unsigned int local_sym_index
, unsigned int type
,
856 Output_data
* od
, Address address
, Addend addend
)
858 this->add(Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
863 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
864 unsigned int local_sym_index
, unsigned int type
,
865 unsigned int shndx
, Address address
, Addend addend
)
867 this->add(Output_reloc_type(relobj
, local_sym_index
, type
, shndx
, address
,
871 // A reloc against the STT_SECTION symbol of an output section.
874 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
875 Address address
, Addend addend
)
876 { this->add(Output_reloc_type(os
, type
, od
, address
, addend
)); }
879 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
880 unsigned int shndx
, Address address
, Addend addend
)
881 { this->add(Output_reloc_type(os
, type
, relobj
, shndx
, address
, addend
)); }
884 // Output_data_got is used to manage a GOT. Each entry in the GOT is
885 // for one symbol--either a global symbol or a local symbol in an
886 // object. The target specific code adds entries to the GOT as
889 template<int size
, bool big_endian
>
890 class Output_data_got
: public Output_section_data
893 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
895 Output_data_got(const General_options
* options
)
896 : Output_section_data(Output_data::default_alignment(size
)),
897 options_(options
), entries_()
900 // Add an entry for a global symbol to the GOT. Return true if this
901 // is a new GOT entry, false if the symbol was already in the GOT.
903 add_global(Symbol
* gsym
);
905 // Add an entry for a local symbol to the GOT. This returns the
906 // offset of the new entry from the start of the GOT.
908 add_local(Object
* object
, unsigned int sym_index
)
910 this->entries_
.push_back(Got_entry(object
, sym_index
));
911 this->set_got_size();
912 return this->last_got_offset();
915 // Add a constant to the GOT. This returns the offset of the new
916 // entry from the start of the GOT.
918 add_constant(Valtype constant
)
920 this->entries_
.push_back(Got_entry(constant
));
921 this->set_got_size();
922 return this->last_got_offset();
925 // Write out the GOT table.
927 do_write(Output_file
*);
930 // This POD class holds a single GOT entry.
934 // Create a zero entry.
936 : local_sym_index_(CONSTANT_CODE
)
937 { this->u_
.constant
= 0; }
939 // Create a global symbol entry.
940 explicit Got_entry(Symbol
* gsym
)
941 : local_sym_index_(GSYM_CODE
)
942 { this->u_
.gsym
= gsym
; }
944 // Create a local symbol entry.
945 Got_entry(Object
* object
, unsigned int local_sym_index
)
946 : local_sym_index_(local_sym_index
)
948 gold_assert(local_sym_index
!= GSYM_CODE
949 && local_sym_index
!= CONSTANT_CODE
);
950 this->u_
.object
= object
;
953 // Create a constant entry. The constant is a host value--it will
954 // be swapped, if necessary, when it is written out.
955 explicit Got_entry(Valtype constant
)
956 : local_sym_index_(CONSTANT_CODE
)
957 { this->u_
.constant
= constant
; }
959 // Write the GOT entry to an output view.
961 write(const General_options
*, unsigned char* pov
) const;
972 // For a local symbol, the object.
974 // For a global symbol, the symbol.
976 // For a constant, the constant.
979 // For a local symbol, the local symbol index. This is GSYM_CODE
980 // for a global symbol, or CONSTANT_CODE for a constant.
981 unsigned int local_sym_index_
;
984 typedef std::vector
<Got_entry
> Got_entries
;
986 // Return the offset into the GOT of GOT entry I.
988 got_offset(unsigned int i
) const
989 { return i
* (size
/ 8); }
991 // Return the offset into the GOT of the last entry added.
993 last_got_offset() const
994 { return this->got_offset(this->entries_
.size() - 1); }
996 // Set the size of the section.
999 { this->set_data_size(this->got_offset(this->entries_
.size())); }
1002 const General_options
* options_
;
1003 // The list of GOT entries.
1004 Got_entries entries_
;
1007 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1010 class Output_data_dynamic
: public Output_section_data
1013 Output_data_dynamic(const Target
* target
, Stringpool
* pool
)
1014 : Output_section_data(Output_data::default_alignment(target
->get_size())),
1015 target_(target
), entries_(), pool_(pool
)
1018 // Add a new dynamic entry with a fixed numeric value.
1020 add_constant(elfcpp::DT tag
, unsigned int val
)
1021 { this->add_entry(Dynamic_entry(tag
, val
)); }
1023 // Add a new dynamic entry with the address of output data.
1025 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1026 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1028 // Add a new dynamic entry with the size of output data.
1030 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1031 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1033 // Add a new dynamic entry with the address of a symbol.
1035 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1036 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1038 // Add a new dynamic entry with a string.
1040 add_string(elfcpp::DT tag
, const char* str
)
1041 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, NULL
))); }
1043 // Set the final data size.
1045 do_set_address(uint64_t, off_t
);
1047 // Write out the dynamic entries.
1049 do_write(Output_file
*);
1052 // Adjust the output section to set the entry size.
1054 do_adjust_output_section(Output_section
*);
1057 // This POD class holds a single dynamic entry.
1061 // Create an entry with a fixed numeric value.
1062 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1063 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1064 { this->u_
.val
= val
; }
1066 // Create an entry with the size or address of a section.
1067 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1069 classification_(section_size
1070 ? DYNAMIC_SECTION_SIZE
1071 : DYNAMIC_SECTION_ADDRESS
)
1072 { this->u_
.od
= od
; }
1074 // Create an entry with the address of a symbol.
1075 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1076 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1077 { this->u_
.sym
= sym
; }
1079 // Create an entry with a string.
1080 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1081 : tag_(tag
), classification_(DYNAMIC_STRING
)
1082 { this->u_
.str
= str
; }
1084 // Write the dynamic entry to an output view.
1085 template<int size
, bool big_endian
>
1087 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1095 DYNAMIC_SECTION_ADDRESS
,
1097 DYNAMIC_SECTION_SIZE
,
1106 // For DYNAMIC_NUMBER.
1108 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1109 const Output_data
* od
;
1110 // For DYNAMIC_SYMBOL.
1112 // For DYNAMIC_STRING.
1117 // The type of entry.
1118 Classification classification_
;
1121 // Add an entry to the list.
1123 add_entry(const Dynamic_entry
& entry
)
1124 { this->entries_
.push_back(entry
); }
1126 // Sized version of write function.
1127 template<int size
, bool big_endian
>
1129 sized_write(Output_file
* of
);
1131 // The type of the list of entries.
1132 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1135 const Target
* target_
;
1137 Dynamic_entries entries_
;
1138 // The pool used for strings.
1142 // An output section. We don't expect to have too many output
1143 // sections, so we don't bother to do a template on the size.
1145 class Output_section
: public Output_data
1148 // Create an output section, giving the name, type, and flags.
1149 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1150 virtual ~Output_section();
1152 // Add a new input section SHNDX, named NAME, with header SHDR, from
1153 // object OBJECT. Return the offset within the output section.
1154 template<int size
, bool big_endian
>
1156 add_input_section(Relobj
* object
, unsigned int shndx
, const char *name
,
1157 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
1159 // Add generated data POSD to this output section.
1161 add_output_section_data(Output_section_data
* posd
);
1163 // Return the section name.
1166 { return this->name_
; }
1168 // Return the section type.
1171 { return this->type_
; }
1173 // Return the section flags.
1176 { return this->flags_
; }
1178 // Return the section index in the output file.
1180 do_out_shndx() const
1181 { return this->out_shndx_
; }
1183 // Set the output section index.
1185 do_set_out_shndx(unsigned int shndx
)
1186 { this->out_shndx_
= shndx
; }
1188 // Return the entsize field.
1191 { return this->entsize_
; }
1193 // Set the entsize field.
1195 set_entsize(uint64_t v
);
1197 // Set the link field to the output section index of a section.
1199 set_link_section(const Output_data
* od
)
1201 gold_assert(this->link_
== 0
1202 && !this->should_link_to_symtab_
1203 && !this->should_link_to_dynsym_
);
1204 this->link_section_
= od
;
1207 // Set the link field to a constant.
1209 set_link(unsigned int v
)
1211 gold_assert(this->link_section_
== NULL
1212 && !this->should_link_to_symtab_
1213 && !this->should_link_to_dynsym_
);
1217 // Record that this section should link to the normal symbol table.
1219 set_should_link_to_symtab()
1221 gold_assert(this->link_section_
== NULL
1223 && !this->should_link_to_dynsym_
);
1224 this->should_link_to_symtab_
= true;
1227 // Record that this section should link to the dynamic symbol table.
1229 set_should_link_to_dynsym()
1231 gold_assert(this->link_section_
== NULL
1233 && !this->should_link_to_symtab_
);
1234 this->should_link_to_dynsym_
= true;
1237 // Return the info field.
1241 gold_assert(this->info_section_
== NULL
);
1245 // Set the info field to the output section index of a section.
1247 set_info_section(const Output_data
* od
)
1249 gold_assert(this->info_
== 0);
1250 this->info_section_
= od
;
1253 // Set the info field to a constant.
1255 set_info(unsigned int v
)
1257 gold_assert(this->info_section_
== NULL
);
1261 // Set the addralign field.
1263 set_addralign(uint64_t v
)
1264 { this->addralign_
= v
; }
1266 // Indicate that we need a symtab index.
1268 set_needs_symtab_index()
1269 { this->needs_symtab_index_
= true; }
1271 // Return whether we need a symtab index.
1273 needs_symtab_index() const
1274 { return this->needs_symtab_index_
; }
1276 // Get the symtab index.
1278 symtab_index() const
1280 gold_assert(this->symtab_index_
!= 0);
1281 return this->symtab_index_
;
1284 // Set the symtab index.
1286 set_symtab_index(unsigned int index
)
1288 gold_assert(index
!= 0);
1289 this->symtab_index_
= index
;
1292 // Indicate that we need a dynsym index.
1294 set_needs_dynsym_index()
1295 { this->needs_dynsym_index_
= true; }
1297 // Return whether we need a dynsym index.
1299 needs_dynsym_index() const
1300 { return this->needs_dynsym_index_
; }
1302 // Get the dynsym index.
1304 dynsym_index() const
1306 gold_assert(this->dynsym_index_
!= 0);
1307 return this->dynsym_index_
;
1310 // Set the dynsym index.
1312 set_dynsym_index(unsigned int index
)
1314 gold_assert(index
!= 0);
1315 this->dynsym_index_
= index
;
1318 // Return the output virtual address of OFFSET relative to the start
1319 // of input section SHNDX in object OBJECT.
1321 output_address(const Relobj
* object
, unsigned int shndx
,
1322 off_t offset
) const;
1324 // Set the address of the Output_section. For a typical
1325 // Output_section, there is nothing to do, but if there are any
1326 // Output_section_data objects we need to set the final addresses
1329 do_set_address(uint64_t, off_t
);
1331 // Write the data to the file. For a typical Output_section, this
1332 // does nothing: the data is written out by calling Object::Relocate
1333 // on each input object. But if there are any Output_section_data
1334 // objects we do need to write them out here.
1336 do_write(Output_file
*);
1338 // Return the address alignment--function required by parent class.
1340 do_addralign() const
1341 { return this->addralign_
; }
1343 // Return whether this is an Output_section.
1345 do_is_section() const
1348 // Return whether this is a section of the specified type.
1350 do_is_section_type(elfcpp::Elf_Word type
) const
1351 { return this->type_
== type
; }
1353 // Return whether the specified section flag is set.
1355 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1356 { return (this->flags_
& flag
) != 0; }
1358 // Write the section header into *OPHDR.
1359 template<int size
, bool big_endian
>
1361 write_header(const Layout
*, const Stringpool
*,
1362 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1365 // In some cases we need to keep a list of the input sections
1366 // associated with this output section. We only need the list if we
1367 // might have to change the offsets of the input section within the
1368 // output section after we add the input section. The ordinary
1369 // input sections will be written out when we process the object
1370 // file, and as such we don't need to track them here. We do need
1371 // to track Output_section_data objects here. We store instances of
1372 // this structure in a std::vector, so it must be a POD. There can
1373 // be many instances of this structure, so we use a union to save
1379 : shndx_(0), p2align_(0)
1381 this->u1_
.data_size
= 0;
1382 this->u2_
.object
= NULL
;
1385 // For an ordinary input section.
1386 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1389 p2align_(ffsll(static_cast<long long>(addralign
)))
1391 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1392 && shndx
!= MERGE_DATA_SECTION_CODE
1393 && shndx
!= MERGE_STRING_SECTION_CODE
);
1394 this->u1_
.data_size
= data_size
;
1395 this->u2_
.object
= object
;
1398 // For a non-merge output section.
1399 Input_section(Output_section_data
* posd
)
1400 : shndx_(OUTPUT_SECTION_CODE
),
1401 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1403 this->u1_
.data_size
= 0;
1404 this->u2_
.posd
= posd
;
1407 // For a merge section.
1408 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1410 ? MERGE_STRING_SECTION_CODE
1411 : MERGE_DATA_SECTION_CODE
),
1412 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1414 this->u1_
.entsize
= entsize
;
1415 this->u2_
.posd
= posd
;
1418 // The required alignment.
1422 return (this->p2align_
== 0
1424 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1427 // Return the required size.
1431 // Return whether this is a merge section which matches the
1434 is_merge_section(bool is_string
, uint64_t entsize
) const
1436 return (this->shndx_
== (is_string
1437 ? MERGE_STRING_SECTION_CODE
1438 : MERGE_DATA_SECTION_CODE
)
1439 && this->u1_
.entsize
== entsize
);
1442 // Set the output section.
1444 set_output_section(Output_section
* os
)
1446 gold_assert(!this->is_input_section());
1447 this->u2_
.posd
->set_output_section(os
);
1450 // Set the address and file offset. This is called during
1451 // Layout::finalize. SECOFF is the file offset of the enclosing
1454 set_address(uint64_t addr
, off_t off
, off_t secoff
);
1456 // Add an input section, for SHF_MERGE sections.
1458 add_input_section(Relobj
* object
, unsigned int shndx
)
1460 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1461 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1462 return this->u2_
.posd
->add_input_section(object
, shndx
);
1465 // Given an input OBJECT, an input section index SHNDX within that
1466 // object, and an OFFSET relative to the start of that input
1467 // section, return whether or not the output address is known.
1468 // OUTPUT_SECTION_ADDRESS is the address of the output section
1469 // which this is a part of. If this function returns true, it
1470 // sets *POUTPUT to the output address.
1472 output_address(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1473 uint64_t output_section_address
, uint64_t *poutput
) const;
1475 // Write out the data. This does nothing for an input section.
1477 write(Output_file
*);
1480 // Code values which appear in shndx_. If the value is not one of
1481 // these codes, it is the input section index in the object file.
1484 // An Output_section_data.
1485 OUTPUT_SECTION_CODE
= -1U,
1486 // An Output_section_data for an SHF_MERGE section with
1487 // SHF_STRINGS not set.
1488 MERGE_DATA_SECTION_CODE
= -2U,
1489 // An Output_section_data for an SHF_MERGE section with
1491 MERGE_STRING_SECTION_CODE
= -3U
1494 // Whether this is an input section.
1496 is_input_section() const
1498 return (this->shndx_
!= OUTPUT_SECTION_CODE
1499 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1500 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1503 // For an ordinary input section, this is the section index in the
1504 // input file. For an Output_section_data, this is
1505 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1506 // MERGE_STRING_SECTION_CODE.
1507 unsigned int shndx_
;
1508 // The required alignment, stored as a power of 2.
1509 unsigned int p2align_
;
1512 // For an ordinary input section, the section size.
1514 // For OUTPUT_SECTION_CODE, this is not used. For
1515 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1521 // For an ordinary input section, the object which holds the
1524 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1525 // MERGE_STRING_SECTION_CODE, the data.
1526 Output_section_data
* posd
;
1530 typedef std::vector
<Input_section
> Input_section_list
;
1532 // Add a new output section by Input_section.
1534 add_output_section_data(Input_section
*);
1536 // Add an SHF_MERGE input section. Returns true if the section was
1539 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1540 uint64_t entsize
, uint64_t addralign
);
1542 // Add an output SHF_MERGE section POSD to this output section.
1543 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1544 // ENTSIZE is the entity size. This returns the entry added to
1547 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1550 // Most of these fields are only valid after layout.
1552 // The name of the section. This will point into a Stringpool.
1554 // The section address is in the parent class.
1555 // The section alignment.
1556 uint64_t addralign_
;
1557 // The section entry size.
1559 // The file offset is in the parent class.
1560 // Set the section link field to the index of this section.
1561 const Output_data
* link_section_
;
1562 // If link_section_ is NULL, this is the link field.
1564 // Set the section info field to the index of this section.
1565 const Output_data
* info_section_
;
1566 // If info_section_ is NULL, this is the section info field.
1568 // The section type.
1569 elfcpp::Elf_Word type_
;
1570 // The section flags.
1571 elfcpp::Elf_Xword flags_
;
1572 // The section index.
1573 unsigned int out_shndx_
;
1574 // If there is a STT_SECTION for this output section in the normal
1575 // symbol table, this is the symbol index. This starts out as zero.
1576 // It is initialized in Layout::finalize() to be the index, or -1U
1577 // if there isn't one.
1578 unsigned int symtab_index_
;
1579 // If there is a STT_SECTION for this output section in the dynamic
1580 // symbol table, this is the symbol index. This starts out as zero.
1581 // It is initialized in Layout::finalize() to be the index, or -1U
1582 // if there isn't one.
1583 unsigned int dynsym_index_
;
1584 // The input sections. This will be empty in cases where we don't
1585 // need to keep track of them.
1586 Input_section_list input_sections_
;
1587 // The offset of the first entry in input_sections_.
1588 off_t first_input_offset_
;
1589 // Whether this output section needs a STT_SECTION symbol in the
1590 // normal symbol table. This will be true if there is a relocation
1592 bool needs_symtab_index_
: 1;
1593 // Whether this output section needs a STT_SECTION symbol in the
1594 // dynamic symbol table. This will be true if there is a dynamic
1595 // relocation which needs it.
1596 bool needs_dynsym_index_
: 1;
1597 // Whether the link field of this output section should point to the
1598 // normal symbol table.
1599 bool should_link_to_symtab_
: 1;
1600 // Whether the link field of this output section should point to the
1601 // dynamic symbol table.
1602 bool should_link_to_dynsym_
: 1;
1605 // An output segment. PT_LOAD segments are built from collections of
1606 // output sections. Other segments typically point within PT_LOAD
1607 // segments, and are built directly as needed.
1609 class Output_segment
1612 // Create an output segment, specifying the type and flags.
1613 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1615 // Return the virtual address.
1618 { return this->vaddr_
; }
1620 // Return the physical address.
1623 { return this->paddr_
; }
1625 // Return the segment type.
1628 { return this->type_
; }
1630 // Return the segment flags.
1633 { return this->flags_
; }
1635 // Return the memory size.
1638 { return this->memsz_
; }
1640 // Return the file size.
1643 { return this->filesz_
; }
1645 // Return the maximum alignment of the Output_data.
1649 // Add an Output_section to this segment.
1651 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1652 { this->add_output_section(os
, seg_flags
, false); }
1654 // Add an Output_section to the start of this segment.
1656 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1657 { this->add_output_section(os
, seg_flags
, true); }
1659 // Add an Output_data (which is not an Output_section) to the start
1662 add_initial_output_data(Output_data
*);
1664 // Set the address of the segment to ADDR and the offset to *POFF
1665 // (aligned if necessary), and set the addresses and offsets of all
1666 // contained output sections accordingly. Set the section indexes
1667 // of all contained output sections starting with *PSHNDX. Return
1668 // the address of the immediately following segment. Update *POFF
1669 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1671 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
1673 // Set the offset of this segment based on the section. This should
1674 // only be called for a non-PT_LOAD segment.
1678 // Return the number of output sections.
1680 output_section_count() const;
1682 // Write the segment header into *OPHDR.
1683 template<int size
, bool big_endian
>
1685 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
1687 // Write the section headers of associated sections into V.
1688 template<int size
, bool big_endian
>
1690 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
1691 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
1694 Output_segment(const Output_segment
&);
1695 Output_segment
& operator=(const Output_segment
&);
1697 typedef std::list
<Output_data
*> Output_data_list
;
1699 // Add an Output_section to this segment, specifying front or back.
1701 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
1704 // Find the maximum alignment in an Output_data_list.
1706 maximum_alignment(const Output_data_list
*);
1708 // Set the section addresses in an Output_data_list.
1710 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
1711 unsigned int* pshndx
);
1713 // Return the number of Output_sections in an Output_data_list.
1715 output_section_count_list(const Output_data_list
*) const;
1717 // Write the section headers in the list into V.
1718 template<int size
, bool big_endian
>
1720 write_section_headers_list(const Layout
*, const Stringpool
*,
1721 const Output_data_list
*, unsigned char* v
,
1722 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
1724 // The list of output data with contents attached to this segment.
1725 Output_data_list output_data_
;
1726 // The list of output data without contents attached to this segment.
1727 Output_data_list output_bss_
;
1728 // The segment virtual address.
1730 // The segment physical address.
1732 // The size of the segment in memory.
1734 // The segment alignment.
1736 // The offset of the segment data within the file.
1738 // The size of the segment data in the file.
1740 // The segment type;
1741 elfcpp::Elf_Word type_
;
1742 // The segment flags.
1743 elfcpp::Elf_Word flags_
;
1744 // Whether we have set align_.
1745 bool is_align_known_
;
1748 // This class represents the output file.
1753 Output_file(const General_options
& options
);
1755 // Open the output file. FILE_SIZE is the final size of the file.
1757 open(off_t file_size
);
1759 // Close the output file and make sure there are no error.
1763 // We currently always use mmap which makes the view handling quite
1764 // simple. In the future we may support other approaches.
1766 // Write data to the output file.
1768 write(off_t offset
, const void* data
, off_t len
)
1769 { memcpy(this->base_
+ offset
, data
, len
); }
1771 // Get a buffer to use to write to the file, given the offset into
1772 // the file and the size.
1774 get_output_view(off_t start
, off_t size
)
1776 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
1777 return this->base_
+ start
;
1780 // VIEW must have been returned by get_output_view. Write the
1781 // buffer to the file, passing in the offset and the size.
1783 write_output_view(off_t
, off_t
, unsigned char*)
1788 const General_options
& options_
;
1795 // Base of file mapped into memory.
1796 unsigned char* base_
;
1799 } // End namespace gold.
1801 #endif // !defined(GOLD_OUTPUT_H)