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
,
188 const Layout::Segment_list
&,
189 const Layout::Section_list
&,
192 // Write the data to the file.
194 do_write(Output_file
*);
196 // Return the required alignment.
199 { return Output_data::default_alignment(this->size_
); }
202 // Write the data to the file with the right size and endianness.
203 template<int size
, bool big_endian
>
205 do_sized_write(Output_file
*);
209 const Layout::Segment_list
& segment_list_
;
210 const Layout::Section_list
& unattached_section_list_
;
211 const Stringpool
* secnamepool_
;
214 // Output the segment headers.
216 class Output_segment_headers
: public Output_data
219 Output_segment_headers(int size
, bool big_endian
,
220 const Layout::Segment_list
& segment_list
);
222 // Write the data to the file.
224 do_write(Output_file
*);
226 // Return the required alignment.
229 { return Output_data::default_alignment(this->size_
); }
232 // Write the data to the file with the right size and endianness.
233 template<int size
, bool big_endian
>
235 do_sized_write(Output_file
*);
239 const Layout::Segment_list
& segment_list_
;
242 // Output the ELF file header.
244 class Output_file_header
: public Output_data
247 Output_file_header(int size
,
249 const General_options
&,
252 const Output_segment_headers
*);
254 // Add information about the section headers. We lay out the ELF
255 // file header before we create the section headers.
256 void set_section_info(const Output_section_headers
*,
257 const Output_section
* shstrtab
);
259 // Write the data to the file.
261 do_write(Output_file
*);
263 // Return the required alignment.
266 { return Output_data::default_alignment(this->size_
); }
268 // Set the address and offset--we only implement this for error
271 do_set_address(uint64_t, off_t off
) const
272 { gold_assert(off
== 0); }
275 // Write the data to the file with the right size and endianness.
276 template<int size
, bool big_endian
>
278 do_sized_write(Output_file
*);
282 const General_options
& options_
;
283 const Target
* target_
;
284 const Symbol_table
* symtab_
;
285 const Output_segment_headers
* segment_header_
;
286 const Output_section_headers
* section_header_
;
287 const Output_section
* shstrtab_
;
290 // Output sections are mainly comprised of input sections. However,
291 // there are cases where we have data to write out which is not in an
292 // input section. Output_section_data is used in such cases. This is
293 // an abstract base class.
295 class Output_section_data
: public Output_data
298 Output_section_data(off_t data_size
, uint64_t addralign
)
299 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
302 Output_section_data(uint64_t addralign
)
303 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
306 // Record the output section.
308 set_output_section(Output_section
* os
)
310 gold_assert(this->output_section_
== NULL
);
311 this->output_section_
= os
;
315 // The child class must implement do_write.
317 // Return the required alignment.
320 { return this->addralign_
; }
322 // Return the section index of the output section.
324 do_out_shndx() const;
327 // The output section for this section.
328 const Output_section
* output_section_
;
329 // The required alignment.
333 // A simple case of Output_data in which we have constant data to
336 class Output_data_const
: public Output_section_data
339 Output_data_const(const std::string
& data
, uint64_t addralign
)
340 : Output_section_data(data
.size(), addralign
), data_(data
)
343 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
344 : Output_section_data(len
, addralign
), data_(p
, len
)
347 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
348 : Output_section_data(len
, addralign
),
349 data_(reinterpret_cast<const char*>(p
), len
)
354 add_data(const std::string
& add
)
356 this->data_
.append(add
);
357 this->set_data_size(this->data_
.size());
360 // Write the data to the output file.
362 do_write(Output_file
*);
368 // Another version of Output_data with constant data, in which the
369 // buffer is allocated by the caller.
371 class Output_data_const_buffer
: public Output_section_data
374 Output_data_const_buffer(const unsigned char* p
, off_t len
,
376 : Output_section_data(len
, addralign
), p_(p
)
379 // Write the data the output file.
381 do_write(Output_file
*);
384 const unsigned char* p_
;
387 // A place holder for data written out via some other mechanism.
389 class Output_data_space
: public Output_section_data
392 Output_data_space(off_t data_size
, uint64_t addralign
)
393 : Output_section_data(data_size
, addralign
)
396 explicit Output_data_space(uint64_t addralign
)
397 : Output_section_data(addralign
)
402 set_space_size(off_t space_size
)
403 { this->set_data_size(space_size
); }
405 // Write out the data--this must be handled elsewhere.
407 do_write(Output_file
*)
411 // A string table which goes into an output section.
413 class Output_data_strtab
: public Output_section_data
416 Output_data_strtab(Stringpool
* strtab
)
417 : Output_section_data(1), strtab_(strtab
)
420 // This is called to set the address and file offset. Here we make
421 // sure that the Stringpool is finalized.
423 do_set_address(uint64_t, off_t
);
425 // Write out the data.
427 do_write(Output_file
*);
433 // This POD class is used to represent a single reloc in the output
434 // file. This could be a private class within Output_data_reloc, but
435 // the templatization is complex enough that I broke it out into a
436 // separate class. The class is templatized on either elfcpp::SHT_REL
437 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
438 // relocation or an ordinary relocation.
440 // A relocation can be against a global symbol, a local symbol, an
441 // output section, or the undefined symbol at index 0. We represent
442 // the latter by using a NULL global symbol.
444 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
447 template<bool dynamic
, int size
, bool big_endian
>
448 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
451 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
453 // An uninitialized entry. We need this because we want to put
454 // instances of this class into an STL container.
456 : local_sym_index_(INVALID_CODE
)
459 // A reloc against a global symbol.
460 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
462 : local_sym_index_(GSYM_CODE
), type_(type
), od_(od
), address_(address
)
463 { this->u_
.gsym
= gsym
; }
465 // A reloc against a local symbol.
466 Output_reloc(Sized_relobj
<size
, big_endian
>* object
,
467 unsigned int local_sym_index
,
471 : local_sym_index_(local_sym_index
), type_(type
), od_(od
),
474 gold_assert(local_sym_index
!= GSYM_CODE
475 && local_sym_index
!= INVALID_CODE
);
476 this->u_
.object
= object
;
479 // A reloc against the STT_SECTION symbol of an output section.
480 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
482 : local_sym_index_(SECTION_CODE
), type_(type
), od_(od
), address_(address
)
483 { this->u_
.os
= os
; }
485 // Write the reloc entry to an output view.
487 write(unsigned char* pov
) const;
489 // Write the offset and info fields to Write_rel.
490 template<typename Write_rel
>
491 void write_rel(Write_rel
*) const;
494 // Return the symbol index. We can't do a double template
495 // specialization, so we do a secondary template here.
497 get_symbol_index() const;
499 // Codes for local_sym_index_.
506 // Invalid uninitialized entry.
512 // For a local symbol, the object. We will never generate a
513 // relocation against a local symbol in a dynamic object; that
514 // doesn't make sense. And our callers will always be
515 // templatized, so we use Sized_relobj here.
516 Sized_relobj
<size
, big_endian
>* object
;
517 // For a global symbol, the symbol. If this is NULL, it indicates
518 // a relocation against the undefined 0 symbol.
520 // For a relocation against an output section, the output section.
523 // For a local symbol, the local symbol index. This is GSYM_CODE
524 // for a global symbol, or INVALID_CODE for an uninitialized value.
525 unsigned int local_sym_index_
;
526 // The reloc type--a processor specific code.
528 // If this is not NULL, then the relocation is against the contents
529 // of this output data.
531 // The reloc address--if od_ is not NULL, this is the offset from
536 // The SHT_RELA version of Output_reloc<>. This is just derived from
537 // the SHT_REL version of Output_reloc, but it adds an addend.
539 template<bool dynamic
, int size
, bool big_endian
>
540 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
543 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
544 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
546 // An uninitialized entry.
551 // A reloc against a global symbol.
552 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
553 Address address
, Addend addend
)
554 : rel_(gsym
, type
, od
, address
), addend_(addend
)
557 // A reloc against a local symbol.
558 Output_reloc(Sized_relobj
<size
, big_endian
>* object
,
559 unsigned int local_sym_index
,
560 unsigned int type
, Output_data
* od
, Address address
,
562 : rel_(object
, local_sym_index
, type
, od
, address
), addend_(addend
)
565 // A reloc against the STT_SECTION symbol of an output section.
566 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
567 Address address
, Addend addend
)
568 : rel_(os
, type
, od
, address
), addend_(addend
)
571 // Write the reloc entry to an output view.
573 write(unsigned char* pov
) const;
577 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
582 // Output_data_reloc is used to manage a section containing relocs.
583 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
584 // indicates whether this is a dynamic relocation or a normal
585 // relocation. Output_data_reloc_base is a base class.
586 // Output_data_reloc is the real class, which we specialize based on
589 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
590 class Output_data_reloc_base
: public Output_section_data
593 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
594 typedef typename
Output_reloc_type::Address Address
;
595 static const int reloc_size
=
596 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
598 // Construct the section.
599 Output_data_reloc_base()
600 : Output_section_data(Output_data::default_alignment(size
))
603 // Write out the data.
605 do_write(Output_file
*);
608 // Add a relocation entry.
610 add(const Output_reloc_type
& reloc
)
612 this->relocs_
.push_back(reloc
);
613 this->set_data_size(this->relocs_
.size() * reloc_size
);
617 typedef std::vector
<Output_reloc_type
> Relocs
;
622 // The class which callers actually create.
624 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
625 class Output_data_reloc
;
627 // The SHT_REL version of Output_data_reloc.
629 template<bool dynamic
, int size
, bool big_endian
>
630 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
631 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
634 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
638 typedef typename
Base::Output_reloc_type Output_reloc_type
;
639 typedef typename
Output_reloc_type::Address Address
;
642 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
645 // Add a reloc against a global symbol.
647 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
648 { this->add(Output_reloc_type(gsym
, type
, od
, address
)); }
650 // Add a reloc against a local symbol.
652 add_local(Sized_relobj
<size
, big_endian
>* object
,
653 unsigned int local_sym_index
, unsigned int type
,
654 Output_data
* od
, Address address
)
655 { this->add(Output_reloc_type(object
, local_sym_index
, type
, od
, address
)); }
657 // A reloc against the STT_SECTION symbol of an output section.
659 add_output_section(Output_section
* os
, unsigned int type
,
660 Output_data
* od
, Address address
)
661 { this->add(Output_reloc_type(os
, type
, od
, address
)); }
664 // The SHT_RELA version of Output_data_reloc.
666 template<bool dynamic
, int size
, bool big_endian
>
667 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
668 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
671 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
675 typedef typename
Base::Output_reloc_type Output_reloc_type
;
676 typedef typename
Output_reloc_type::Address Address
;
677 typedef typename
Output_reloc_type::Addend Addend
;
680 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
683 // Add a reloc against a global symbol.
685 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
686 Address address
, Addend addend
)
687 { this->add(Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
689 // Add a reloc against a local symbol.
691 add_local(Sized_relobj
<size
, big_endian
>* object
,
692 unsigned int local_sym_index
, unsigned int type
,
693 Output_data
* od
, Address address
, Addend addend
)
695 this->add(Output_reloc_type(object
, local_sym_index
, type
, od
, address
,
699 // A reloc against the STT_SECTION symbol of an output section.
701 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
702 Address address
, Addend addend
)
703 { this->add(Output_reloc_type(os
, type
, od
, address
, addend
)); }
706 // Output_data_got is used to manage a GOT. Each entry in the GOT is
707 // for one symbol--either a global symbol or a local symbol in an
708 // object. The target specific code adds entries to the GOT as
711 template<int size
, bool big_endian
>
712 class Output_data_got
: public Output_section_data
715 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
717 Output_data_got(const General_options
* options
)
718 : Output_section_data(Output_data::default_alignment(size
)),
719 options_(options
), entries_()
722 // Add an entry for a global symbol to the GOT. Return true if this
723 // is a new GOT entry, false if the symbol was already in the GOT.
725 add_global(Symbol
* gsym
);
727 // Add an entry for a local symbol to the GOT. This returns the
728 // offset of the new entry from the start of the GOT.
730 add_local(Object
* object
, unsigned int sym_index
)
732 this->entries_
.push_back(Got_entry(object
, sym_index
));
733 this->set_got_size();
734 return this->last_got_offset();
737 // Add a constant to the GOT. This returns the offset of the new
738 // entry from the start of the GOT.
740 add_constant(Valtype constant
)
742 this->entries_
.push_back(Got_entry(constant
));
743 this->set_got_size();
744 return this->last_got_offset();
747 // Write out the GOT table.
749 do_write(Output_file
*);
752 // This POD class holds a single GOT entry.
756 // Create a zero entry.
758 : local_sym_index_(CONSTANT_CODE
)
759 { this->u_
.constant
= 0; }
761 // Create a global symbol entry.
762 explicit Got_entry(Symbol
* gsym
)
763 : local_sym_index_(GSYM_CODE
)
764 { this->u_
.gsym
= gsym
; }
766 // Create a local symbol entry.
767 Got_entry(Object
* object
, unsigned int local_sym_index
)
768 : local_sym_index_(local_sym_index
)
770 gold_assert(local_sym_index
!= GSYM_CODE
771 && local_sym_index
!= CONSTANT_CODE
);
772 this->u_
.object
= object
;
775 // Create a constant entry. The constant is a host value--it will
776 // be swapped, if necessary, when it is written out.
777 explicit Got_entry(Valtype constant
)
778 : local_sym_index_(CONSTANT_CODE
)
779 { this->u_
.constant
= constant
; }
781 // Write the GOT entry to an output view.
783 write(const General_options
*, unsigned char* pov
) const;
794 // For a local symbol, the object.
796 // For a global symbol, the symbol.
798 // For a constant, the constant.
801 // For a local symbol, the local symbol index. This is GSYM_CODE
802 // for a global symbol, or CONSTANT_CODE for a constant.
803 unsigned int local_sym_index_
;
806 typedef std::vector
<Got_entry
> Got_entries
;
808 // Return the offset into the GOT of GOT entry I.
810 got_offset(unsigned int i
) const
811 { return i
* (size
/ 8); }
813 // Return the offset into the GOT of the last entry added.
815 last_got_offset() const
816 { return this->got_offset(this->entries_
.size() - 1); }
818 // Set the size of the section.
821 { this->set_data_size(this->got_offset(this->entries_
.size())); }
824 const General_options
* options_
;
825 // The list of GOT entries.
826 Got_entries entries_
;
829 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
832 class Output_data_dynamic
: public Output_section_data
835 Output_data_dynamic(const Target
* target
, Stringpool
* pool
)
836 : Output_section_data(Output_data::default_alignment(target
->get_size())),
837 target_(target
), entries_(), pool_(pool
)
840 // Add a new dynamic entry with a fixed numeric value.
842 add_constant(elfcpp::DT tag
, unsigned int val
)
843 { this->add_entry(Dynamic_entry(tag
, val
)); }
845 // Add a new dynamic entry with the address of a section.
847 add_section_address(elfcpp::DT tag
, Output_section
* os
)
848 { this->add_entry(Dynamic_entry(tag
, os
, false)); }
850 // Add a new dynamic entry with the size of a section.
852 add_section_size(elfcpp::DT tag
, Output_section
* os
)
853 { this->add_entry(Dynamic_entry(tag
, os
, true)); }
855 // Add a new dynamic entry with the address of a symbol.
857 add_symbol(elfcpp::DT tag
, Symbol
* sym
)
858 { this->add_entry(Dynamic_entry(tag
, sym
)); }
860 // Add a new dynamic entry with a string.
862 add_string(elfcpp::DT tag
, const char* str
)
863 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, NULL
))); }
865 // Set the final data size.
867 do_set_address(uint64_t, off_t
);
869 // Write out the dynamic entries.
871 do_write(Output_file
*);
874 // This POD class holds a single dynamic entry.
878 // Create an entry with a fixed numeric value.
879 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
880 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
881 { this->u_
.val
= val
; }
883 // Create an entry with the size or address of a section.
884 Dynamic_entry(elfcpp::DT tag
, Output_section
* os
, bool section_size
)
886 classification_(section_size
887 ? DYNAMIC_SECTION_SIZE
888 : DYNAMIC_SECTION_ADDRESS
)
889 { this->u_
.os
= os
; }
891 // Create an entry with the address of a symbol.
892 Dynamic_entry(elfcpp::DT tag
, Symbol
* sym
)
893 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
894 { this->u_
.sym
= sym
; }
896 // Create an entry with a string.
897 Dynamic_entry(elfcpp::DT tag
, const char* str
)
898 : tag_(tag
), classification_(DYNAMIC_STRING
)
899 { this->u_
.str
= str
; }
901 // Write the dynamic entry to an output view.
902 template<int size
, bool big_endian
>
904 write(unsigned char* pov
, const Stringpool
*) const;
912 DYNAMIC_SECTION_ADDRESS
,
914 DYNAMIC_SECTION_SIZE
,
923 // For DYNAMIC_NUMBER.
925 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
927 // For DYNAMIC_SYMBOL.
929 // For DYNAMIC_STRING.
934 // The type of entry.
935 Classification classification_
;
938 // Add an entry to the list.
940 add_entry(const Dynamic_entry
& entry
)
941 { this->entries_
.push_back(entry
); }
943 // Sized version of write function.
944 template<int size
, bool big_endian
>
946 sized_write(Output_file
* of
);
948 // The type of the list of entries.
949 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
952 const Target
* target_
;
954 Dynamic_entries entries_
;
955 // The pool used for strings.
959 // An output section. We don't expect to have too many output
960 // sections, so we don't bother to do a template on the size.
962 class Output_section
: public Output_data
965 // Create an output section, giving the name, type, and flags.
966 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
,
968 virtual ~Output_section();
970 // Add a new input section SHNDX, named NAME, with header SHDR, from
971 // object OBJECT. Return the offset within the output section.
972 template<int size
, bool big_endian
>
974 add_input_section(Relobj
* object
, unsigned int shndx
, const char *name
,
975 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
977 // Add generated data ODATA to this output section.
979 add_output_section_data(Output_section_data
* posd
);
981 // Return the section name.
984 { return this->name_
; }
986 // Return the section type.
989 { return this->type_
; }
991 // Return the section flags.
994 { return this->flags_
; }
996 // Return the section index in the output file.
999 { return this->out_shndx_
; }
1001 // Set the output section index.
1003 do_set_out_shndx(unsigned int shndx
)
1004 { this->out_shndx_
= shndx
; }
1006 // Return the entsize field.
1009 { return this->entsize_
; }
1011 // Set the entsize field.
1013 set_entsize(uint64_t v
)
1014 { this->entsize_
= v
; }
1016 // Set the link field.
1018 set_link(unsigned int v
)
1019 { this->link_
= v
; }
1021 // Set the info field.
1023 set_info(unsigned int v
)
1024 { this->info_
= v
; }
1026 // Set the addralign field.
1028 set_addralign(uint64_t v
)
1029 { this->addralign_
= v
; }
1031 // Indicate that we need a symtab index.
1033 set_needs_symtab_index()
1034 { this->needs_symtab_index_
= true; }
1036 // Return whether we need a symtab index.
1038 needs_symtab_index() const
1039 { return this->needs_symtab_index_
; }
1041 // Get the symtab index.
1043 symtab_index() const
1045 gold_assert(this->symtab_index_
!= 0);
1046 return this->symtab_index_
;
1049 // Set the symtab index.
1051 set_symtab_index(unsigned int index
)
1053 gold_assert(index
!= 0);
1054 this->symtab_index_
= index
;
1057 // Indicate that we need a dynsym index.
1059 set_needs_dynsym_index()
1060 { this->needs_dynsym_index_
= true; }
1062 // Return whether we need a dynsym index.
1064 needs_dynsym_index() const
1065 { return this->needs_dynsym_index_
; }
1067 // Get the dynsym index.
1069 dynsym_index() const
1071 gold_assert(this->dynsym_index_
!= 0);
1072 return this->dynsym_index_
;
1075 // Set the dynsym index.
1077 set_dynsym_index(unsigned int index
)
1079 gold_assert(index
!= 0);
1080 this->dynsym_index_
= index
;
1083 // Set the address of the Output_section. For a typical
1084 // Output_section, there is nothing to do, but if there are any
1085 // Output_section_data objects we need to set the final addresses
1088 do_set_address(uint64_t, off_t
);
1090 // Write the data to the file. For a typical Output_section, this
1091 // does nothing: the data is written out by calling Object::Relocate
1092 // on each input object. But if there are any Output_section_data
1093 // objects we do need to write them out here.
1095 do_write(Output_file
*);
1097 // Return the address alignment--function required by parent class.
1099 do_addralign() const
1100 { return this->addralign_
; }
1102 // Return whether this is an Output_section.
1104 do_is_section() const
1107 // Return whether this is a section of the specified type.
1109 do_is_section_type(elfcpp::Elf_Word type
) const
1110 { return this->type_
== type
; }
1112 // Return whether the specified section flag is set.
1114 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1115 { return (this->flags_
& flag
) != 0; }
1117 // Write the section header into *OPHDR.
1118 template<int size
, bool big_endian
>
1120 write_header(const Stringpool
*, elfcpp::Shdr_write
<size
, big_endian
>*) const;
1123 // In some cases we need to keep a list of the input sections
1124 // associated with this output section. We only need the list if we
1125 // might have to change the offsets of the input section within the
1126 // output section after we add the input section. The ordinary
1127 // input sections will be written out when we process the object
1128 // file, and as such we don't need to track them here. We do need
1129 // to track Output_section_data objects here. We store instances of
1130 // this structure in a std::vector, so it must be a POD. There can
1131 // be many instances of this structure, so we use a union to save
1137 : shndx_(0), p2align_(0), data_size_(0)
1138 { this->u_
.object
= NULL
; }
1140 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1143 p2align_(ffsll(static_cast<long long>(addralign
))),
1144 data_size_(data_size
)
1146 gold_assert(shndx
!= -1U);
1147 this->u_
.object
= object
;
1150 Input_section(Output_section_data
* posd
)
1152 p2align_(ffsll(static_cast<long long>(posd
->addralign()))),
1154 { this->u_
.posd
= posd
; }
1156 // The required alignment.
1160 return (this->p2align_
== 0
1162 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1165 // Return the required size.
1169 // Set the address and file offset. This is called during
1170 // Layout::finalize. SECOFF is the file offset of the enclosing
1173 set_address(uint64_t addr
, off_t off
, off_t secoff
);
1175 // Write out the data. This does nothing for an input section.
1177 write(Output_file
*);
1180 // Whether this is an input section.
1182 is_input_section() const
1183 { return this->shndx_
!= -1U; }
1185 // For an ordinary input section, this is the section index in
1186 // the input file. For an Output_section_data, this is -1U.
1187 unsigned int shndx_
;
1188 // The required alignment, stored as a power of 2.
1189 unsigned int p2align_
;
1190 // For an ordinary input section, the section size.
1194 // If shndx_ != -1U, this points to the object which holds the
1197 // If shndx_ == -1U, this is the data to write out.
1198 Output_section_data
* posd
;
1202 typedef std::vector
<Input_section
> Input_section_list
;
1204 // Most of these fields are only valid after layout.
1206 // The name of the section. This will point into a Stringpool.
1208 // The section address is in the parent class.
1209 // The section alignment.
1210 uint64_t addralign_
;
1211 // The section entry size.
1213 // The file offset is in the parent class.
1214 // The section link field.
1216 // The section info field.
1218 // The section type.
1219 elfcpp::Elf_Word type_
;
1220 // The section flags.
1221 elfcpp::Elf_Xword flags_
;
1222 // The section index.
1223 unsigned int out_shndx_
;
1224 // If there is a STT_SECTION for this output section in the normal
1225 // symbol table, this is the symbol index. This starts out as zero.
1226 // It is initialized in Layout::finalize() to be the index, or -1U
1227 // if there isn't one.
1228 unsigned int symtab_index_
;
1229 // If there is a STT_SECTION for this output section in the dynamic
1230 // symbol table, this is the symbol index. This starts out as zero.
1231 // It is initialized in Layout::finalize() to be the index, or -1U
1232 // if there isn't one.
1233 unsigned int dynsym_index_
;
1234 // The input sections. This will be empty in cases where we don't
1235 // need to keep track of them.
1236 Input_section_list input_sections_
;
1237 // The offset of the first entry in input_sections_.
1238 off_t first_input_offset_
;
1239 // Whether we permit adding data.
1240 bool may_add_data_
: 1;
1241 // Whether this output section needs a STT_SECTION symbol in the
1242 // normal symbol table. This will be true if there is a relocation
1244 bool needs_symtab_index_
: 1;
1245 // Whether this output section needs a STT_SECTION symbol in the
1246 // dynamic symbol table. This will be true if there is a dynamic
1247 // relocation which needs it.
1248 bool needs_dynsym_index_
: 1;
1251 // An output segment. PT_LOAD segments are built from collections of
1252 // output sections. Other segments typically point within PT_LOAD
1253 // segments, and are built directly as needed.
1255 class Output_segment
1258 // Create an output segment, specifying the type and flags.
1259 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1261 // Return the virtual address.
1264 { return this->vaddr_
; }
1266 // Return the physical address.
1269 { return this->paddr_
; }
1271 // Return the segment type.
1274 { return this->type_
; }
1276 // Return the segment flags.
1279 { return this->flags_
; }
1281 // Return the memory size.
1284 { return this->memsz_
; }
1286 // Return the file size.
1289 { return this->filesz_
; }
1291 // Return the maximum alignment of the Output_data.
1295 // Add an Output_section to this segment.
1297 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1298 { this->add_output_section(os
, seg_flags
, false); }
1300 // Add an Output_section to the start of this segment.
1302 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1303 { this->add_output_section(os
, seg_flags
, true); }
1305 // Add an Output_data (which is not an Output_section) to the start
1308 add_initial_output_data(Output_data
*);
1310 // Set the address of the segment to ADDR and the offset to *POFF
1311 // (aligned if necessary), and set the addresses and offsets of all
1312 // contained output sections accordingly. Set the section indexes
1313 // of all contained output sections starting with *PSHNDX. Return
1314 // the address of the immediately following segment. Update *POFF
1315 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1317 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
1319 // Set the offset of this segment based on the section. This should
1320 // only be called for a non-PT_LOAD segment.
1324 // Return the number of output sections.
1326 output_section_count() const;
1328 // Write the segment header into *OPHDR.
1329 template<int size
, bool big_endian
>
1331 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
1333 // Write the section headers of associated sections into V.
1334 template<int size
, bool big_endian
>
1336 write_section_headers(const Stringpool
*,
1338 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
1341 Output_segment(const Output_segment
&);
1342 Output_segment
& operator=(const Output_segment
&);
1344 typedef std::list
<Output_data
*> Output_data_list
;
1346 // Add an Output_section to this segment, specifying front or back.
1348 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
1351 // Find the maximum alignment in an Output_data_list.
1353 maximum_alignment(const Output_data_list
*);
1355 // Set the section addresses in an Output_data_list.
1357 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
1358 unsigned int* pshndx
);
1360 // Return the number of Output_sections in an Output_data_list.
1362 output_section_count_list(const Output_data_list
*) const;
1364 // Write the section headers in the list into V.
1365 template<int size
, bool big_endian
>
1367 write_section_headers_list(const Stringpool
*, const Output_data_list
*,
1369 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
1371 // The list of output data with contents attached to this segment.
1372 Output_data_list output_data_
;
1373 // The list of output data without contents attached to this segment.
1374 Output_data_list output_bss_
;
1375 // The segment virtual address.
1377 // The segment physical address.
1379 // The size of the segment in memory.
1381 // The segment alignment.
1383 // The offset of the segment data within the file.
1385 // The size of the segment data in the file.
1387 // The segment type;
1388 elfcpp::Elf_Word type_
;
1389 // The segment flags.
1390 elfcpp::Elf_Word flags_
;
1391 // Whether we have set align_.
1392 bool is_align_known_
;
1395 // This class represents the output file.
1400 Output_file(const General_options
& options
);
1402 // Open the output file. FILE_SIZE is the final size of the file.
1404 open(off_t file_size
);
1406 // Close the output file and make sure there are no error.
1410 // We currently always use mmap which makes the view handling quite
1411 // simple. In the future we may support other approaches.
1413 // Write data to the output file.
1415 write(off_t offset
, const void* data
, off_t len
)
1416 { memcpy(this->base_
+ offset
, data
, len
); }
1418 // Get a buffer to use to write to the file, given the offset into
1419 // the file and the size.
1421 get_output_view(off_t start
, off_t size
)
1423 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
1424 return this->base_
+ start
;
1427 // VIEW must have been returned by get_output_view. Write the
1428 // buffer to the file, passing in the offset and the size.
1430 write_output_view(off_t
, off_t
, unsigned char*)
1435 const General_options
& options_
;
1442 // Base of file mapped into memory.
1443 unsigned char* base_
;
1446 } // End namespace gold.
1448 #endif // !defined(GOLD_OUTPUT_H)