1 // output.h -- manage the output file for gold -*- C++ -*-
12 #include "reloc-types.h"
17 class General_options
;
21 template<int size
, bool big_endian
>
23 template<int size
, bool big_endian
>
26 // An abtract class for data which has to go into the output file.
31 explicit Output_data(off_t data_size
= 0)
32 : address_(0), data_size_(data_size
), offset_(-1)
38 // Return the address. This is only valid after Layout::finalize is
42 { return this->address_
; }
44 // Return the size of the data. This must be valid after
45 // Layout::finalize calls set_address, but need not be valid before
49 { return this->data_size_
; }
51 // Return the file offset. This is only valid after
52 // Layout::finalize is finished.
55 { return this->offset_
; }
57 // Return the required alignment.
60 { return this->do_addralign(); }
62 // Return whether this is an Output_section.
65 { return this->do_is_section(); }
67 // Return whether this is an Output_section of the specified type.
69 is_section_type(elfcpp::Elf_Word stt
) const
70 { return this->do_is_section_type(stt
); }
72 // Return whether this is an Output_section with the specified flag
75 is_section_flag_set(elfcpp::Elf_Xword shf
) const
76 { return this->do_is_section_flag_set(shf
); }
78 // Return the output section index, if there is an output section.
81 { return this->do_out_shndx(); }
83 // Set the output section index, if this is an output section.
85 set_out_shndx(unsigned int shndx
)
86 { this->do_set_out_shndx(shndx
); }
88 // Set the address and file offset of this data. This is called
89 // during Layout::finalize.
91 set_address(uint64_t addr
, off_t off
);
93 // Write the data to the output file. This is called after
94 // Layout::finalize is complete.
96 write(Output_file
* file
)
97 { this->do_write(file
); }
100 // Functions that child classes may or in some cases must implement.
102 // Write the data to the output file.
104 do_write(Output_file
*) = 0;
106 // Return the required alignment.
108 do_addralign() const = 0;
110 // Return whether this is an Output_section.
112 do_is_section() const
115 // Return whether this is an Output_section of the specified type.
116 // This only needs to be implement by Output_section.
118 do_is_section_type(elfcpp::Elf_Word
) const
121 // Return whether this is an Output_section with the specific flag
122 // set. This only needs to be implemented by Output_section.
124 do_is_section_flag_set(elfcpp::Elf_Xword
) const
127 // Return the output section index, if there is an output section.
132 // Set the output section index, if this is an output section.
134 do_set_out_shndx(unsigned int)
137 // Set the address and file offset of the data. This only needs to
138 // be implemented if the child needs to know.
140 do_set_address(uint64_t, off_t
)
143 // Functions that child classes may call.
145 // Set the size of the data.
147 set_data_size(off_t data_size
)
148 { this->data_size_
= data_size
; }
150 // Return default alignment for a size--32 or 64.
152 default_alignment(int size
);
155 Output_data(const Output_data
&);
156 Output_data
& operator=(const Output_data
&);
158 // Memory address in file (not always meaningful).
160 // Size of data in file.
162 // Offset within file.
166 // Output the section headers.
168 class Output_section_headers
: public Output_data
171 Output_section_headers(int size
,
173 const Layout::Segment_list
&,
174 const Layout::Section_list
&,
177 // Write the data to the file.
179 do_write(Output_file
*);
181 // Return the required alignment.
184 { return Output_data::default_alignment(this->size_
); }
187 // Write the data to the file with the right size and endianness.
188 template<int size
, bool big_endian
>
190 do_sized_write(Output_file
*);
194 const Layout::Segment_list
& segment_list_
;
195 const Layout::Section_list
& section_list_
;
196 const Stringpool
* secnamepool_
;
199 // Output the segment headers.
201 class Output_segment_headers
: public Output_data
204 Output_segment_headers(int size
, bool big_endian
,
205 const Layout::Segment_list
& segment_list
);
207 // Write the data to the file.
209 do_write(Output_file
*);
211 // Return the required alignment.
214 { return Output_data::default_alignment(this->size_
); }
217 // Write the data to the file with the right size and endianness.
218 template<int size
, bool big_endian
>
220 do_sized_write(Output_file
*);
224 const Layout::Segment_list
& segment_list_
;
227 // Output the ELF file header.
229 class Output_file_header
: public Output_data
232 Output_file_header(int size
,
234 const General_options
&,
237 const Output_segment_headers
*);
239 // Add information about the section headers. We lay out the ELF
240 // file header before we create the section headers.
241 void set_section_info(const Output_section_headers
*,
242 const Output_section
* shstrtab
);
244 // Write the data to the file.
246 do_write(Output_file
*);
248 // Return the required alignment.
251 { return Output_data::default_alignment(this->size_
); }
253 // Set the address and offset--we only implement this for error
256 do_set_address(uint64_t, off_t off
) const
257 { assert(off
== 0); }
260 // Write the data to the file with the right size and endianness.
261 template<int size
, bool big_endian
>
263 do_sized_write(Output_file
*);
267 const General_options
& options_
;
268 const Target
* target_
;
269 const Symbol_table
* symtab_
;
270 const Output_segment_headers
* segment_header_
;
271 const Output_section_headers
* section_header_
;
272 const Output_section
* shstrtab_
;
275 // Output sections are mainly comprised of input sections. However,
276 // there are cases where we have data to write out which is not in an
277 // input section. Output_section_data is used in such cases. This is
278 // an abstract base class.
280 class Output_section_data
: public Output_data
283 Output_section_data(off_t data_size
, uint64_t addralign
)
284 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
287 Output_section_data(uint64_t addralign
)
288 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
291 // Record the output section.
293 set_output_section(Output_section
* os
)
295 assert(this->output_section_
== NULL
);
296 this->output_section_
= os
;
300 // The child class must implement do_write.
302 // Return the required alignment.
305 { return this->addralign_
; }
307 // Return the section index of the output section.
309 do_out_shndx() const;
312 // The output section for this section.
313 const Output_section
* output_section_
;
314 // The required alignment.
318 // A simple case of Output_data in which we have constant data to
321 class Output_data_const
: public Output_section_data
324 Output_data_const(const std::string
& data
, uint64_t addralign
)
325 : Output_section_data(data
.size(), addralign
), data_(data
)
328 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
329 : Output_section_data(len
, addralign
), data_(p
, len
)
332 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
333 : Output_section_data(len
, addralign
),
334 data_(reinterpret_cast<const char*>(p
), len
)
337 // Write the data to the file.
339 do_write(Output_file
* output
);
345 // Output_data_common is used to handle the common symbols. This is
348 class Output_data_common
: public Output_section_data
351 Output_data_common(uint64_t addralign
)
352 : Output_section_data(addralign
)
357 set_common_size(off_t common_size
)
358 { this->set_data_size(common_size
); }
360 // Write out the data--there is nothing to do, as common symbols are
361 // always zero and are stored in the BSS.
363 do_write(Output_file
*)
367 // This POD class is used to represent a single reloc in the output
368 // file. This could be a private class within Output_data_reloc, but
369 // the templatization is complex enough that I broke it out into a
370 // separate class. The class is templatized on either elfcpp::SHT_REL
371 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
372 // relocation or an ordinary relocation.
374 // A relocation can be against a global symbol, a local symbol, an
375 // output section, or the undefined symbol at index 0. We represent
376 // the latter by using a NULL global symbol.
378 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
381 template<bool dynamic
, int size
, bool big_endian
>
382 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
385 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
387 // An uninitialized entry. We need this because we want to put
388 // instances of this class into an STL container.
390 : local_sym_index_(INVALID_CODE
)
393 // A reloc against a global symbol.
394 Output_reloc(Symbol
* gsym
, unsigned int type
, Address address
)
395 : local_sym_index_(GSYM_CODE
), type_(type
), address_(address
)
396 { this->u_
.gsym
= gsym
; }
398 // A reloc against a local symbol.
399 Output_reloc(Sized_relobj
<size
, big_endian
>* object
,
400 unsigned int local_sym_index
,
401 unsigned int type
, Address address
)
402 : local_sym_index_(local_sym_index
), type_(type
), address_(address
)
404 assert(local_sym_index
!= GSYM_CODE
&& local_sym_index
!= INVALID_CODE
);
405 this->u_
.object
= object
;
408 // A reloc against the STT_SECTION symbol of an output section.
409 Output_reloc(Output_section
* os
, unsigned int type
, Address address
)
410 : local_sym_index_(SECTION_CODE
), type_(type
), address_(address
)
411 { this->u_
.os
= os
; }
413 // Write the reloc entry to an output view.
415 write(unsigned char* pov
) const;
417 // Write the offset and info fields to Write_rel.
418 template<typename Write_rel
>
419 void write_rel(Write_rel
*) const;
422 // Return the symbol index. We can't do a double template
423 // specialization, so we do a secondary template here.
425 get_symbol_index() const;
427 // Codes for local_sym_index_.
434 // Invalid uninitialized entry.
440 // For a local symbol, the object. We will never generate a
441 // relocation against a local symbol in a dynamic object; that
442 // doesn't make sense. And our callers will always be
443 // templatized, so we use Sized_relobj here.
444 Sized_relobj
<size
, big_endian
>* object
;
445 // For a global symbol, the symbol. If this is NULL, it indicates
446 // a relocation against the undefined 0 symbol.
448 // For a relocation against an output section, the output section.
451 // For a local symbol, the local symbol index. This is GSYM_CODE
452 // for a global symbol, or INVALID_CODE for an uninitialized value.
453 unsigned int local_sym_index_
;
458 // The SHT_RELA version of Output_reloc<>. This is just derived from
459 // the SHT_REL version of Output_reloc, but it adds an addend.
461 template<bool dynamic
, int size
, bool big_endian
>
462 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
465 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
466 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
468 // An uninitialized entry.
473 // A reloc against a global symbol.
474 Output_reloc(Symbol
* gsym
, unsigned int type
, Address address
, Addend addend
)
475 : rel_(gsym
, type
, address
), addend_(addend
)
478 // A reloc against a local symbol.
479 Output_reloc(Sized_relobj
<size
, big_endian
>* object
,
480 unsigned int local_sym_index
,
481 unsigned int type
, Address address
, Addend addend
)
482 : rel_(object
, local_sym_index
, type
, address
), addend_(addend
)
485 // A reloc against the STT_SECTION symbol of an output section.
486 Output_reloc(Output_section
* os
, unsigned int type
, Address address
,
488 : rel_(os
, type
, address
), addend_(addend
)
491 // Write the reloc entry to an output view.
493 write(unsigned char* pov
) const;
497 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
502 // Output_data_reloc is used to manage a section containing relocs.
503 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
504 // indicates whether this is a dynamic relocation or a normal
505 // relocation. Output_data_reloc_base is a base class.
506 // Output_data_reloc is the real class, which we specialize based on
509 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
510 class Output_data_reloc_base
: public Output_section_data
513 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
514 typedef typename
Output_reloc_type::Address Address
;
515 static const int reloc_size
=
516 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
518 // Construct the section.
519 Output_data_reloc_base()
520 : Output_section_data(Output_data::default_alignment(size
))
523 // Write out the data.
525 do_write(Output_file
*);
528 // Add a relocation entry.
530 add(const Output_reloc_type
& reloc
)
532 this->relocs_
.push_back(reloc
);
533 this->set_data_size(this->relocs_
.size() * reloc_size
);
537 typedef std::vector
<Output_reloc_type
> Relocs
;
542 // The class which callers actually create.
544 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
545 class Output_data_reloc
;
547 // The SHT_REL version of Output_data_reloc.
549 template<bool dynamic
, int size
, bool big_endian
>
550 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
551 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
554 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
558 typedef typename
Base::Output_reloc_type Output_reloc_type
;
559 typedef typename
Output_reloc_type::Address Address
;
562 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
565 // Add a reloc against a global symbol.
567 add_global(Symbol
* gsym
, unsigned int type
, Address address
)
568 { this->add(Output_reloc_type(gsym
, type
, address
)); }
570 // Add a reloc against a local symbol.
572 add_local(Sized_relobj
<size
, big_endian
>* object
,
573 unsigned int local_sym_index
, unsigned int type
, Address address
)
574 { this->add(Output_reloc_type(object
, local_sym_index
, type
, address
)); }
576 // A reloc against the STT_SECTION symbol of an output section.
578 add_output_section(Output_section
* os
, unsigned int type
, Address address
)
579 { this->add(Output_reloc_type(os
, type
, address
)); }
582 // The SHT_RELA version of Output_data_reloc.
584 template<bool dynamic
, int size
, bool big_endian
>
585 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
586 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
589 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
593 typedef typename
Base::Output_reloc_type Output_reloc_type
;
594 typedef typename
Output_reloc_type::Address Address
;
595 typedef typename
Output_reloc_type::Addend Addend
;
598 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
601 // Add a reloc against a global symbol.
603 add_global(Symbol
* gsym
, unsigned int type
, Address address
, Addend addend
)
604 { this->add(Output_reloc_type(gsym
, type
, address
, addend
)); }
606 // Add a reloc against a local symbol.
608 add_local(Sized_relobj
<size
, big_endian
>* object
,
609 unsigned int local_sym_index
, unsigned int type
,
610 Address address
, Addend addend
)
612 this->add(Output_reloc_type(object
, local_sym_index
, type
, address
,
616 // A reloc against the STT_SECTION symbol of an output section.
618 add_output_section(Output_section
* os
, unsigned int type
, Address address
,
620 { this->add(Output_reloc_type(os
, type
, address
, addend
)); }
623 // Output_data_got is used to manage a GOT. Each entry in the GOT is
624 // for one symbol--either a global symbol or a local symbol in an
625 // object. The target specific code adds entries to the GOT as
628 template<int size
, bool big_endian
>
629 class Output_data_got
: public Output_section_data
632 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
635 : Output_section_data(Output_data::default_alignment(size
)),
639 // Add an entry for a global symbol to the GOT. Return true if this
640 // is a new GOT entry, false if the symbol was already in the GOT.
642 add_global(Symbol
* gsym
);
644 // Add an entry for a local symbol to the GOT. This returns the
645 // offset of the new entry from the start of the GOT.
647 add_local(Object
* object
, unsigned int sym_index
)
649 this->entries_
.push_back(Got_entry(object
, sym_index
));
650 this->set_got_size();
651 return this->last_got_offset();
654 // Add a constant to the GOT. This returns the offset of the new
655 // entry from the start of the GOT.
657 add_constant(Valtype constant
)
659 this->entries_
.push_back(Got_entry(constant
));
660 this->set_got_size();
661 return this->last_got_offset();
664 // Write out the GOT table.
666 do_write(Output_file
*);
669 // This POD class holds a single GOT entry.
673 // Create a zero entry.
675 : local_sym_index_(CONSTANT_CODE
)
676 { this->u_
.constant
= 0; }
678 // Create a global symbol entry.
679 Got_entry(Symbol
* gsym
)
680 : local_sym_index_(GSYM_CODE
)
681 { this->u_
.gsym
= gsym
; }
683 // Create a local symbol entry.
684 Got_entry(Object
* object
, unsigned int local_sym_index
)
685 : local_sym_index_(local_sym_index
)
687 assert(local_sym_index
!= GSYM_CODE
688 && local_sym_index
!= CONSTANT_CODE
);
689 this->u_
.object
= object
;
692 // Create a constant entry. The constant is a host value--it will
693 // be swapped, if necessary, when it is written out.
694 Got_entry(Valtype constant
)
695 : local_sym_index_(CONSTANT_CODE
)
696 { this->u_
.constant
= constant
; }
698 // Write the GOT entry to an output view.
700 write(unsigned char* pov
) const;
711 // For a local symbol, the object.
713 // For a global symbol, the symbol.
715 // For a constant, the constant.
718 // For a local symbol, the local symbol index. This is GSYM_CODE
719 // for a global symbol, or CONSTANT_CODE for a constant.
720 unsigned int local_sym_index_
;
723 typedef std::vector
<Got_entry
> Got_entries
;
725 // Return the offset into the GOT of GOT entry I.
727 got_offset(unsigned int i
) const
728 { return i
* (size
/ 8); }
730 // Return the offset into the GOT of the last entry added.
732 last_got_offset() const
733 { return this->got_offset(this->entries_
.size() - 1); }
735 // Set the size of the section.
738 { this->set_data_size(this->got_offset(this->entries_
.size())); }
740 // The list of GOT entries.
741 Got_entries entries_
;
744 // An output section. We don't expect to have too many output
745 // sections, so we don't bother to do a template on the size.
747 class Output_section
: public Output_data
750 // Create an output section, giving the name, type, and flags.
751 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
,
753 virtual ~Output_section();
755 // Add a new input section SHNDX, named NAME, with header SHDR, from
756 // object OBJECT. Return the offset within the output section.
757 template<int size
, bool big_endian
>
759 add_input_section(Relobj
* object
, unsigned int shndx
, const char *name
,
760 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
762 // Add generated data ODATA to this output section.
764 add_output_section_data(Output_section_data
* posd
);
766 // Return the section name.
769 { return this->name_
; }
771 // Return the section type.
774 { return this->type_
; }
776 // Return the section flags.
779 { return this->flags_
; }
781 // Return the section index in the output file.
784 { return this->out_shndx_
; }
786 // Set the output section index.
788 do_set_out_shndx(unsigned int shndx
)
789 { this->out_shndx_
= shndx
; }
791 // Set the entsize field.
793 set_entsize(uint64_t v
)
794 { this->entsize_
= v
; }
796 // Set the link field.
798 set_link(unsigned int v
)
801 // Set the info field.
803 set_info(unsigned int v
)
806 // Set the addralign field.
808 set_addralign(uint64_t v
)
809 { this->addralign_
= v
; }
811 // Indicate that we need a symtab index.
813 set_needs_symtab_index()
814 { this->needs_symtab_index_
= true; }
816 // Return whether we need a symtab index.
818 needs_symtab_index() const
819 { return this->needs_symtab_index_
; }
821 // Get the symtab index.
825 assert(this->symtab_index_
!= 0);
826 return this->symtab_index_
;
829 // Set the symtab index.
831 set_symtab_index(unsigned int index
)
834 this->symtab_index_
= index
;
837 // Indicate that we need a dynsym index.
839 set_needs_dynsym_index()
840 { this->needs_dynsym_index_
= true; }
842 // Return whether we need a dynsym index.
844 needs_dynsym_index() const
845 { return this->needs_dynsym_index_
; }
847 // Get the dynsym index.
851 assert(this->dynsym_index_
!= 0);
852 return this->dynsym_index_
;
855 // Set the dynsym index.
857 set_dynsym_index(unsigned int index
)
860 this->dynsym_index_
= index
;
863 // Set the address of the Output_section. For a typical
864 // Output_section, there is nothing to do, but if there are any
865 // Output_section_data objects we need to set the final addresses
868 do_set_address(uint64_t, off_t
);
870 // Write the data to the file. For a typical Output_section, this
871 // does nothing: the data is written out by calling Object::Relocate
872 // on each input object. But if there are any Output_section_data
873 // objects we do need to write them out here.
875 do_write(Output_file
*);
877 // Return the address alignment--function required by parent class.
880 { return this->addralign_
; }
882 // Return whether this is an Output_section.
884 do_is_section() const
887 // Return whether this is a section of the specified type.
889 do_is_section_type(elfcpp::Elf_Word type
) const
890 { return this->type_
== type
; }
892 // Return whether the specified section flag is set.
894 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
895 { return (this->flags_
& flag
) != 0; }
897 // Write the section header into *OPHDR.
898 template<int size
, bool big_endian
>
900 write_header(const Stringpool
*, elfcpp::Shdr_write
<size
, big_endian
>*) const;
903 // In some cases we need to keep a list of the input sections
904 // associated with this output section. We only need the list if we
905 // might have to change the offsets of the input section within the
906 // output section after we add the input section. The ordinary
907 // input sections will be written out when we process the object
908 // file, and as such we don't need to track them here. We do need
909 // to track Output_section_data objects here. We store instances of
910 // this structure in a std::vector, so it must be a POD. There can
911 // be many instances of this structure, so we use a union to save
917 : shndx_(0), p2align_(0), data_size_(0)
918 { this->u_
.object
= NULL
; }
920 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
923 p2align_(ffsll(static_cast<long long>(addralign
))),
924 data_size_(data_size
)
926 assert(shndx
!= -1U);
927 this->u_
.object
= object
;
930 Input_section(Output_section_data
* posd
)
932 p2align_(ffsll(static_cast<long long>(posd
->addralign()))),
934 { this->u_
.posd
= posd
; }
936 // The required alignment.
939 { return static_cast<uint64_t>(1) << this->p2align_
; }
941 // Return the required size.
945 // Set the address and file offset. This is called during
946 // Layout::finalize. SECOFF is the file offset of the enclosing
949 set_address(uint64_t addr
, off_t off
, off_t secoff
);
951 // Write out the data. This does nothing for an input section.
956 // Whether this is an input section.
958 is_input_section() const
959 { return this->shndx_
!= -1U; }
961 // For an ordinary input section, this is the section index in
962 // the input file. For an Output_section_data, this is -1U.
964 // The required alignment, stored as a power of 2.
965 unsigned int p2align_
;
966 // For an ordinary input section, the section size.
970 // If shndx_ != -1U, this points to the object which holds the
973 // If shndx_ == -1U, this is the data to write out.
974 Output_section_data
* posd
;
978 typedef std::vector
<Input_section
> Input_section_list
;
980 // Most of these fields are only valid after layout.
982 // The name of the section. This will point into a Stringpool.
984 // The section address is in the parent class.
985 // The section alignment.
987 // The section entry size.
989 // The file offset is in the parent class.
990 // The section link field.
992 // The section info field.
995 elfcpp::Elf_Word type_
;
996 // The section flags.
997 elfcpp::Elf_Xword flags_
;
998 // The section index.
999 unsigned int out_shndx_
;
1000 // If there is a STT_SECTION for this output section in the normal
1001 // symbol table, this is the symbol index. This starts out as zero.
1002 // It is initialized in Layout::finalize() to be the index, or -1U
1003 // if there isn't one.
1004 unsigned int symtab_index_
;
1005 // If there is a STT_SECTION for this output section in the dynamic
1006 // symbol table, this is the symbol index. This starts out as zero.
1007 // It is initialized in Layout::finalize() to be the index, or -1U
1008 // if there isn't one.
1009 unsigned int dynsym_index_
;
1010 // The input sections. This will be empty in cases where we don't
1011 // need to keep track of them.
1012 Input_section_list input_sections_
;
1013 // The offset of the first entry in input_sections_.
1014 off_t first_input_offset_
;
1015 // Whether we permit adding data.
1016 bool may_add_data_
: 1;
1017 // Whether this output section needs a STT_SECTION symbol in the
1018 // normal symbol table. This will be true if there is a relocation
1020 bool needs_symtab_index_
: 1;
1021 // Whether this output section needs a STT_SECTION symbol in the
1022 // dynamic symbol table. This will be true if there is a dynamic
1023 // relocation which needs it.
1024 bool needs_dynsym_index_
: 1;
1027 // A special Output_section which represents the symbol table
1028 // (SHT_SYMTAB). The actual data is written out by
1029 // Symbol_table::write_globals.
1031 class Output_section_symtab
: public Output_section
1034 Output_section_symtab(const char* name
, off_t data_size
)
1035 : Output_section(name
, elfcpp::SHT_SYMTAB
, 0, false)
1036 { this->set_data_size(data_size
); }
1038 // The data is written out by Symbol_table::write_globals. We don't
1039 // do anything here.
1041 do_write(Output_file
*)
1045 // A special Output_section which represents the dynamic symbol table
1046 // (SHT_DYNSYM). The actual data is written out by
1047 // Symbol_table::write_globals.
1049 class Output_section_dynsym
: public Output_section
1052 Output_section_dynsym(const char* name
, off_t data_size
)
1053 : Output_section(name
, elfcpp::SHT_DYNSYM
, 0, false)
1054 { this->set_data_size(data_size
); }
1056 // The data is written out by Symbol_table::write_globals. We don't
1057 // do anything here.
1059 do_write(Output_file
*)
1063 // A special Output_section which holds a string table.
1065 class Output_section_strtab
: public Output_section
1068 Output_section_strtab(const char* name
, Stringpool
* contents
);
1070 // Write out the data.
1072 do_write(Output_file
*);
1075 Stringpool
* contents_
;
1078 // An output segment. PT_LOAD segments are built from collections of
1079 // output sections. Other segments typically point within PT_LOAD
1080 // segments, and are built directly as needed.
1082 class Output_segment
1085 // Create an output segment, specifying the type and flags.
1086 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1088 // Return the virtual address.
1091 { return this->vaddr_
; }
1093 // Return the physical address.
1096 { return this->paddr_
; }
1098 // Return the segment type.
1101 { return this->type_
; }
1103 // Return the segment flags.
1106 { return this->flags_
; }
1108 // Return the memory size.
1111 { return this->memsz_
; }
1113 // Return the file size.
1116 { return this->filesz_
; }
1118 // Return the maximum alignment of the Output_data.
1122 // Add an Output_section to this segment.
1124 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1125 { this->add_output_section(os
, seg_flags
, false); }
1127 // Add an Output_section to the start of this segment.
1129 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1130 { this->add_output_section(os
, seg_flags
, true); }
1132 // Add an Output_data (which is not an Output_section) to the start
1135 add_initial_output_data(Output_data
*);
1137 // Set the address of the segment to ADDR and the offset to *POFF
1138 // (aligned if necessary), and set the addresses and offsets of all
1139 // contained output sections accordingly. Set the section indexes
1140 // of all contained output sections starting with *PSHNDX. Return
1141 // the address of the immediately following segment. Update *POFF
1142 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1144 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
1146 // Set the offset of this segment based on the section. This should
1147 // only be called for a non-PT_LOAD segment.
1151 // Return the number of output sections.
1153 output_section_count() const;
1155 // Write the segment header into *OPHDR.
1156 template<int size
, bool big_endian
>
1158 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
1160 // Write the section headers of associated sections into V.
1161 template<int size
, bool big_endian
>
1163 write_section_headers(const Stringpool
*,
1165 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
1168 Output_segment(const Output_segment
&);
1169 Output_segment
& operator=(const Output_segment
&);
1171 typedef std::list
<Output_data
*> Output_data_list
;
1173 // Add an Output_section to this segment, specifying front or back.
1175 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
1178 // Find the maximum alignment in an Output_data_list.
1180 maximum_alignment(const Output_data_list
*);
1182 // Set the section addresses in an Output_data_list.
1184 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
1185 unsigned int* pshndx
);
1187 // Return the number of Output_sections in an Output_data_list.
1189 output_section_count_list(const Output_data_list
*) const;
1191 // Write the section headers in the list into V.
1192 template<int size
, bool big_endian
>
1194 write_section_headers_list(const Stringpool
*, const Output_data_list
*,
1196 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
1198 // The list of output data with contents attached to this segment.
1199 Output_data_list output_data_
;
1200 // The list of output data without contents attached to this segment.
1201 Output_data_list output_bss_
;
1202 // The segment virtual address.
1204 // The segment physical address.
1206 // The size of the segment in memory.
1208 // The segment alignment.
1210 // The offset of the segment data within the file.
1212 // The size of the segment data in the file.
1214 // The segment type;
1215 elfcpp::Elf_Word type_
;
1216 // The segment flags.
1217 elfcpp::Elf_Word flags_
;
1218 // Whether we have set align_.
1219 bool is_align_known_
;
1222 // This class represents the output file.
1227 Output_file(const General_options
& options
);
1229 // Open the output file. FILE_SIZE is the final size of the file.
1231 open(off_t file_size
);
1233 // Close the output file and make sure there are no error.
1237 // We currently always use mmap which makes the view handling quite
1238 // simple. In the future we may support other approaches.
1240 // Write data to the output file.
1242 write(off_t offset
, const void* data
, off_t len
)
1243 { memcpy(this->base_
+ offset
, data
, len
); }
1245 // Get a buffer to use to write to the file, given the offset into
1246 // the file and the size.
1248 get_output_view(off_t start
, off_t size
)
1250 assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
1251 return this->base_
+ start
;
1254 // VIEW must have been returned by get_output_view. Write the
1255 // buffer to the file, passing in the offset and the size.
1257 write_output_view(off_t
, off_t
, unsigned char*)
1262 const General_options
& options_
;
1269 // Base of file mapped into memory.
1270 unsigned char* base_
;
1273 } // End namespace gold.
1275 #endif // !defined(GOLD_OUTPUT_H)