1 // output.h -- manage the output file for gold -*- C++ -*-
16 class General_options
;
20 template<int size
, bool big_endian
>
23 // An abtract class for data which has to go into the output file.
28 explicit Output_data(off_t data_size
= 0)
29 : address_(0), data_size_(data_size
), offset_(-1)
35 // Return the address. This is only valid after Layout::finalize is
39 { return this->address_
; }
41 // Return the size of the data. This must be valid after
42 // Layout::finalize calls set_address, but need not be valid before
46 { return this->data_size_
; }
48 // Return the file offset. This is only valid after
49 // Layout::finalize is finished.
52 { return this->offset_
; }
54 // Return the required alignment.
57 { return this->do_addralign(); }
59 // Return whether this is an Output_section.
62 { return this->do_is_section(); }
64 // Return whether this is an Output_section of the specified type.
66 is_section_type(elfcpp::Elf_Word stt
) const
67 { return this->do_is_section_type(stt
); }
69 // Return whether this is an Output_section with the specified flag
72 is_section_flag_set(elfcpp::Elf_Xword shf
) const
73 { return this->do_is_section_flag_set(shf
); }
75 // Return the output section index, if there is an output section.
78 { return this->do_out_shndx(); }
80 // Set the output section index, if this is an output section.
82 set_out_shndx(unsigned int shndx
)
83 { this->do_set_out_shndx(shndx
); }
85 // Set the address and file offset of this data. This is called
86 // during Layout::finalize.
88 set_address(uint64_t addr
, off_t off
);
90 // Write the data to the output file. This is called after
91 // Layout::finalize is complete.
93 write(Output_file
* file
)
94 { this->do_write(file
); }
97 // Functions that child classes may or in some cases must implement.
99 // Write the data to the output file.
101 do_write(Output_file
*) = 0;
103 // Return the required alignment.
105 do_addralign() const = 0;
107 // Return whether this is an Output_section.
109 do_is_section() const
112 // Return whether this is an Output_section of the specified type.
113 // This only needs to be implement by Output_section.
115 do_is_section_type(elfcpp::Elf_Word
) const
118 // Return whether this is an Output_section with the specific flag
119 // set. This only needs to be implemented by Output_section.
121 do_is_section_flag_set(elfcpp::Elf_Xword
) const
124 // Return the output section index, if there is an output section.
129 // Set the output section index, if this is an output section.
131 do_set_out_shndx(unsigned int)
134 // Set the address and file offset of the data. This only needs to
135 // be implemented if the child needs to know.
137 do_set_address(uint64_t, off_t
)
140 // Functions that child classes may call.
142 // Set the size of the data.
144 set_data_size(off_t data_size
)
145 { this->data_size_
= data_size
; }
147 // Return default alignment for a size--32 or 64.
149 default_alignment(int size
);
152 Output_data(const Output_data
&);
153 Output_data
& operator=(const Output_data
&);
155 // Memory address in file (not always meaningful).
157 // Size of data in file.
159 // Offset within file.
163 // Output the section headers.
165 class Output_section_headers
: public Output_data
168 Output_section_headers(int size
,
170 const Layout::Segment_list
&,
171 const Layout::Section_list
&,
174 // Write the data to the file.
176 do_write(Output_file
*);
178 // Return the required alignment.
181 { return Output_data::default_alignment(this->size_
); }
184 // Write the data to the file with the right size and endianness.
185 template<int size
, bool big_endian
>
187 do_sized_write(Output_file
*);
191 const Layout::Segment_list
& segment_list_
;
192 const Layout::Section_list
& section_list_
;
193 const Stringpool
* secnamepool_
;
196 // Output the segment headers.
198 class Output_segment_headers
: public Output_data
201 Output_segment_headers(int size
, bool big_endian
,
202 const Layout::Segment_list
& segment_list
);
204 // Write the data to the file.
206 do_write(Output_file
*);
208 // Return the required alignment.
211 { return Output_data::default_alignment(this->size_
); }
214 // Write the data to the file with the right size and endianness.
215 template<int size
, bool big_endian
>
217 do_sized_write(Output_file
*);
221 const Layout::Segment_list
& segment_list_
;
224 // Output the ELF file header.
226 class Output_file_header
: public Output_data
229 Output_file_header(int size
,
231 const General_options
&,
234 const Output_segment_headers
*);
236 // Add information about the section headers. We lay out the ELF
237 // file header before we create the section headers.
238 void set_section_info(const Output_section_headers
*,
239 const Output_section
* shstrtab
);
241 // Write the data to the file.
243 do_write(Output_file
*);
245 // Return the required alignment.
248 { return Output_data::default_alignment(this->size_
); }
250 // Set the address and offset--we only implement this for error
253 do_set_address(uint64_t, off_t off
) const
254 { assert(off
== 0); }
257 // Write the data to the file with the right size and endianness.
258 template<int size
, bool big_endian
>
260 do_sized_write(Output_file
*);
264 const General_options
& options_
;
265 const Target
* target_
;
266 const Symbol_table
* symtab_
;
267 const Output_segment_headers
* segment_header_
;
268 const Output_section_headers
* section_header_
;
269 const Output_section
* shstrtab_
;
272 // Output sections are mainly comprised of input sections. However,
273 // there are cases where we have data to write out which is not in an
274 // input section. Output_section_data is used in such cases. This is
275 // an abstract base class.
277 class Output_section_data
: public Output_data
280 Output_section_data(off_t data_size
, uint64_t addralign
)
281 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
284 Output_section_data(uint64_t addralign
)
285 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
288 // Record the output section.
290 set_output_section(Output_section
* os
)
292 assert(this->output_section_
== NULL
);
293 this->output_section_
= os
;
297 // The child class must implement do_write.
299 // Return the required alignment.
302 { return this->addralign_
; }
304 // Return the section index of the output section.
306 do_out_shndx() const;
309 // The output section for this section.
310 const Output_section
* output_section_
;
311 // The required alignment.
315 // A simple case of Output_data in which we have constant data to
318 class Output_data_const
: public Output_section_data
321 Output_data_const(const std::string
& data
, uint64_t addralign
)
322 : Output_section_data(data
.size(), addralign
), data_(data
)
325 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
326 : Output_section_data(len
, addralign
), data_(p
, len
)
329 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
330 : Output_section_data(len
, addralign
),
331 data_(reinterpret_cast<const char*>(p
), len
)
334 // Write the data to the file.
336 do_write(Output_file
* output
);
342 // Output_data_common is used to handle the common symbols. This is
345 class Output_data_common
: public Output_section_data
348 Output_data_common(uint64_t addralign
)
349 : Output_section_data(addralign
)
354 set_common_size(off_t common_size
)
355 { this->set_data_size(common_size
); }
357 // Write out the data--there is nothing to do, as common symbols are
358 // always zero and are stored in the BSS.
360 do_write(Output_file
*)
364 // Output_data_got is used to manage a GOT. Each entry in the GOT is
365 // for one symbol--either a global symbol or a local symbol in an
366 // object. The target specific code adds entries to the GOT as
369 template<int size
, bool big_endian
>
370 class Output_data_got
: public Output_section_data
373 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
376 : Output_section_data(Output_data::default_alignment(size
)),
380 // Add an entry for a global symbol to the GOT. Return true if this
381 // is a new GOT entry, false if the symbol was already in the GOT.
383 add_global(Symbol
* gsym
);
385 // Add an entry for a local symbol to the GOT. This returns the
386 // offset of the new entry from the start of the GOT.
388 add_local(Object
* object
, unsigned int sym_index
)
390 this->entries_
.push_back(Got_entry(object
, sym_index
));
391 this->set_got_size();
392 return this->last_got_offset();
395 // Add a constant to the GOT. This returns the offset of the new
396 // entry from the start of the GOT.
398 add_constant(Valtype constant
)
400 this->entries_
.push_back(Got_entry(constant
));
401 this->set_got_size();
402 return this->last_got_offset();
405 // Write out the GOT table.
407 do_write(Output_file
*);
410 // This POD class holds a single GOT entry.
414 // Create a zero entry.
416 : local_sym_index_(CONSTANT_CODE
)
417 { this->u_
.constant
= 0; }
419 // Create a global symbol entry.
420 Got_entry(Symbol
* gsym
)
421 : local_sym_index_(GSYM_CODE
)
422 { this->u_
.gsym
= gsym
; }
424 // Create a local symbol entry.
425 Got_entry(Object
* object
, unsigned int local_sym_index
)
426 : local_sym_index_(local_sym_index
)
428 assert(local_sym_index
!= GSYM_CODE
429 && local_sym_index
!= CONSTANT_CODE
);
430 this->u_
.object
= object
;
433 // Create a constant entry. The constant is a host value--it will
434 // be swapped, if necessary, when it is written out.
435 Got_entry(Valtype constant
)
436 : local_sym_index_(CONSTANT_CODE
)
437 { this->u_
.constant
= constant
; }
439 // Write the GOT entry to an output view.
441 write(unsigned char* pov
) const;
452 // For a local symbol, the object.
454 // For a global symbol, the symbol.
456 // For a constant, the constant.
459 // For a local symbol, the local symbol index. This is -1U for a
460 // global symbol, or -2U for a constant.
461 unsigned int local_sym_index_
;
464 typedef std::vector
<Got_entry
> Got_entries
;
466 // Return the offset into the GOT of GOT entry I.
468 got_offset(unsigned int i
) const
469 { return i
* (size
/ 8); }
471 // Return the offset into the GOT of the last entry added.
473 last_got_offset() const
474 { return this->got_offset(this->entries_
.size() - 1); }
476 // Set the size of the section.
479 { this->set_data_size(this->got_offset(this->entries_
.size())); }
481 // The list of GOT entries.
482 Got_entries entries_
;
485 // An output section. We don't expect to have too many output
486 // sections, so we don't bother to do a template on the size.
488 class Output_section
: public Output_data
491 // Create an output section, giving the name, type, and flags.
492 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
,
494 virtual ~Output_section();
496 // Add a new input section SHNDX, named NAME, with header SHDR, from
497 // object OBJECT. Return the offset within the output section.
498 template<int size
, bool big_endian
>
500 add_input_section(Relobj
* object
, unsigned int shndx
, const char *name
,
501 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
503 // Add generated data ODATA to this output section.
505 add_output_section_data(Output_section_data
* posd
);
507 // Return the section name.
510 { return this->name_
; }
512 // Return the section type.
515 { return this->type_
; }
517 // Return the section flags.
520 { return this->flags_
; }
522 // Return the section index in the output file.
525 { return this->out_shndx_
; }
527 // Set the output section index.
529 do_set_out_shndx(unsigned int shndx
)
530 { this->out_shndx_
= shndx
; }
532 // Set the entsize field.
534 set_entsize(uint64_t v
)
535 { this->entsize_
= v
; }
537 // Set the link field.
539 set_link(unsigned int v
)
542 // Set the info field.
544 set_info(unsigned int v
)
547 // Set the addralign field.
549 set_addralign(uint64_t v
)
550 { this->addralign_
= v
; }
552 // Set the address of the Output_section. For a typical
553 // Output_section, there is nothing to do, but if there are any
554 // Output_section_data objects we need to set the final addresses
557 do_set_address(uint64_t, off_t
);
559 // Write the data to the file. For a typical Output_section, this
560 // does nothing: the data is written out by calling Object::Relocate
561 // on each input object. But if there are any Output_section_data
562 // objects we do need to write them out here.
564 do_write(Output_file
*);
566 // Return the address alignment--function required by parent class.
569 { return this->addralign_
; }
571 // Return whether this is an Output_section.
573 do_is_section() const
576 // Return whether this is a section of the specified type.
578 do_is_section_type(elfcpp::Elf_Word type
) const
579 { return this->type_
== type
; }
581 // Return whether the specified section flag is set.
583 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
584 { return (this->flags_
& flag
) != 0; }
586 // Write the section header into *OPHDR.
587 template<int size
, bool big_endian
>
589 write_header(const Stringpool
*, elfcpp::Shdr_write
<size
, big_endian
>*) const;
592 // In some cases we need to keep a list of the input sections
593 // associated with this output section. We only need the list if we
594 // might have to change the offsets of the input section within the
595 // output section after we add the input section. The ordinary
596 // input sections will be written out when we process the object
597 // file, and as such we don't need to track them here. We do need
598 // to track Output_section_data objects here. We store instances of
599 // this structure in a std::vector, so it must be a POD. There can
600 // be many instances of this structure, so we use a union to save
606 : shndx_(0), p2align_(0), data_size_(0)
607 { this->u_
.object
= NULL
; }
609 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
612 p2align_(ffsll(static_cast<long long>(addralign
))),
613 data_size_(data_size
)
615 assert(shndx
!= -1U);
616 this->u_
.object
= object
;
619 Input_section(Output_section_data
* posd
)
621 p2align_(ffsll(static_cast<long long>(posd
->addralign()))),
623 { this->u_
.posd
= posd
; }
625 // The required alignment.
628 { return static_cast<uint64_t>(1) << this->p2align_
; }
630 // Return the required size.
634 // Set the address and file offset. This is called during
635 // Layout::finalize. SECOFF is the file offset of the enclosing
638 set_address(uint64_t addr
, off_t off
, off_t secoff
);
640 // Write out the data. This does nothing for an input section.
645 // Whether this is an input section.
647 is_input_section() const
648 { return this->shndx_
!= -1U; }
650 // For an ordinary input section, this is the section index in
651 // the input file. For an Output_section_data, this is -1U.
653 // The required alignment, stored as a power of 2.
654 unsigned int p2align_
;
655 // For an ordinary input section, the section size.
659 // If shndx_ != -1U, this points to the object which holds the
662 // If shndx_ == -1U, this is the data to write out.
663 Output_section_data
* posd
;
667 typedef std::vector
<Input_section
> Input_section_list
;
669 // Most of these fields are only valid after layout.
671 // The name of the section. This will point into a Stringpool.
673 // The section address is in the parent class.
674 // The section alignment.
676 // The section entry size.
678 // The file offset is in the parent class.
679 // The section link field.
681 // The section info field.
684 elfcpp::Elf_Word type_
;
685 // The section flags.
686 elfcpp::Elf_Xword flags_
;
687 // The section index.
688 unsigned int out_shndx_
;
689 // The input sections. This will be empty in cases where we don't
690 // need to keep track of them.
691 Input_section_list input_sections_
;
692 // The offset of the first entry in input_sections_.
693 off_t first_input_offset_
;
694 // Whether we permit adding data.
698 // A special Output_section which represents the symbol table
699 // (SHT_SYMTAB). The actual data is written out by
700 // Symbol_table::write_globals.
702 class Output_section_symtab
: public Output_section
705 Output_section_symtab(const char* name
, off_t size
);
707 // The data is written out by Symbol_table::write_globals. We don't
710 do_write(Output_file
*)
713 // We don't expect to see any input sections or data here.
715 add_output_section_data(Output_section_data
*)
719 // A special Output_section which holds a string table.
721 class Output_section_strtab
: public Output_section
724 Output_section_strtab(const char* name
, Stringpool
* contents
);
726 // Write out the data.
728 do_write(Output_file
*);
730 // We don't expect to see any input sections or data here.
732 add_output_section_data(Output_section_data
*)
736 Stringpool
* contents_
;
739 // An output segment. PT_LOAD segments are built from collections of
740 // output sections. Other segments typically point within PT_LOAD
741 // segments, and are built directly as needed.
746 // Create an output segment, specifying the type and flags.
747 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
749 // Return the virtual address.
752 { return this->vaddr_
; }
754 // Return the physical address.
757 { return this->paddr_
; }
759 // Return the segment type.
762 { return this->type_
; }
764 // Return the segment flags.
767 { return this->flags_
; }
769 // Return the memory size.
772 { return this->memsz_
; }
774 // Return the file size.
777 { return this->filesz_
; }
779 // Return the maximum alignment of the Output_data.
783 // Add an Output_section to this segment.
785 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
786 { this->add_output_section(os
, seg_flags
, false); }
788 // Add an Output_section to the start of this segment.
790 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
791 { this->add_output_section(os
, seg_flags
, true); }
793 // Add an Output_data (which is not an Output_section) to the start
796 add_initial_output_data(Output_data
*);
798 // Set the address of the segment to ADDR and the offset to *POFF
799 // (aligned if necessary), and set the addresses and offsets of all
800 // contained output sections accordingly. Set the section indexes
801 // of all contained output sections starting with *PSHNDX. Return
802 // the address of the immediately following segment. Update *POFF
803 // and *PSHNDX. This should only be called for a PT_LOAD segment.
805 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
807 // Set the offset of this segment based on the section. This should
808 // only be called for a non-PT_LOAD segment.
812 // Return the number of output sections.
814 output_section_count() const;
816 // Write the segment header into *OPHDR.
817 template<int size
, bool big_endian
>
819 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
821 // Write the section headers of associated sections into V.
822 template<int size
, bool big_endian
>
824 write_section_headers(const Stringpool
*,
826 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
829 Output_segment(const Output_segment
&);
830 Output_segment
& operator=(const Output_segment
&);
832 typedef std::list
<Output_data
*> Output_data_list
;
834 // Add an Output_section to this segment, specifying front or back.
836 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
839 // Find the maximum alignment in an Output_data_list.
841 maximum_alignment(const Output_data_list
*);
843 // Set the section addresses in an Output_data_list.
845 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
846 unsigned int* pshndx
);
848 // Return the number of Output_sections in an Output_data_list.
850 output_section_count_list(const Output_data_list
*) const;
852 // Write the section headers in the list into V.
853 template<int size
, bool big_endian
>
855 write_section_headers_list(const Stringpool
*, const Output_data_list
*,
857 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
859 // The list of output data with contents attached to this segment.
860 Output_data_list output_data_
;
861 // The list of output data without contents attached to this segment.
862 Output_data_list output_bss_
;
863 // The segment virtual address.
865 // The segment physical address.
867 // The size of the segment in memory.
869 // The segment alignment.
871 // The offset of the segment data within the file.
873 // The size of the segment data in the file.
876 elfcpp::Elf_Word type_
;
877 // The segment flags.
878 elfcpp::Elf_Word flags_
;
879 // Whether we have set align_.
880 bool is_align_known_
;
883 // This class represents the output file.
888 Output_file(const General_options
& options
);
890 // Open the output file. FILE_SIZE is the final size of the file.
892 open(off_t file_size
);
894 // Close the output file and make sure there are no error.
898 // We currently always use mmap which makes the view handling quite
899 // simple. In the future we may support other approaches.
901 // Write data to the output file.
903 write(off_t offset
, const void* data
, off_t len
)
904 { memcpy(this->base_
+ offset
, data
, len
); }
906 // Get a buffer to use to write to the file, given the offset into
907 // the file and the size.
909 get_output_view(off_t start
, off_t size
)
911 assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
912 return this->base_
+ start
;
915 // VIEW must have been returned by get_output_view. Write the
916 // buffer to the file, passing in the offset and the size.
918 write_output_view(off_t
, off_t
, unsigned char*)
923 const General_options
& options_
;
930 // Base of file mapped into memory.
931 unsigned char* base_
;
934 } // End namespace gold.
936 #endif // !defined(GOLD_OUTPUT_H)