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 // A simple case of Output_data in which we have constant data to
166 class Output_data_const
: public Output_data
169 Output_data_const(const std::string
& data
, uint64_t addralign
)
170 : Output_data(data
.size()), data_(data
), addralign_(addralign
)
173 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
174 : Output_data(len
), data_(p
, len
), addralign_(addralign
)
177 // Write the data to the file.
179 do_write(Output_file
* output
);
181 // Return the required alignment.
184 { return this->addralign_
; }
191 // Output the section headers.
193 class Output_section_headers
: public Output_data
196 Output_section_headers(int size
,
198 const Layout::Segment_list
&,
199 const Layout::Section_list
&,
202 // Write the data to the file.
204 do_write(Output_file
*);
206 // Return the required alignment.
209 { return Output_data::default_alignment(this->size_
); }
212 // Write the data to the file with the right size and endianness.
213 template<int size
, bool big_endian
>
215 do_sized_write(Output_file
*);
219 const Layout::Segment_list
& segment_list_
;
220 const Layout::Section_list
& section_list_
;
221 const Stringpool
* secnamepool_
;
224 // Output the segment headers.
226 class Output_segment_headers
: public Output_data
229 Output_segment_headers(int size
, bool big_endian
,
230 const Layout::Segment_list
& segment_list
);
232 // Write the data to the file.
234 do_write(Output_file
*);
236 // Return the required alignment.
239 { return Output_data::default_alignment(this->size_
); }
242 // Write the data to the file with the right size and endianness.
243 template<int size
, bool big_endian
>
245 do_sized_write(Output_file
*);
249 const Layout::Segment_list
& segment_list_
;
252 // Output the ELF file header.
254 class Output_file_header
: public Output_data
257 Output_file_header(int size
,
259 const General_options
&,
262 const Output_segment_headers
*);
264 // Add information about the section headers. We lay out the ELF
265 // file header before we create the section headers.
266 void set_section_info(const Output_section_headers
*,
267 const Output_section
* shstrtab
);
269 // Write the data to the file.
271 do_write(Output_file
*);
273 // Return the required alignment.
276 { return Output_data::default_alignment(this->size_
); }
278 // Set the address and offset--we only implement this for error
281 do_set_address(uint64_t, off_t off
) const
282 { assert(off
== 0); }
285 // Write the data to the file with the right size and endianness.
286 template<int size
, bool big_endian
>
288 do_sized_write(Output_file
*);
292 const General_options
& options_
;
293 const Target
* target_
;
294 const Symbol_table
* symtab_
;
295 const Output_segment_headers
* segment_header_
;
296 const Output_section_headers
* section_header_
;
297 const Output_section
* shstrtab_
;
300 // Output sections are mainly comprised of input sections. However,
301 // there are cases where we have data to write out which is not in an
302 // input section. Output_section_data is used in such cases. This is
303 // an abstract base class.
305 class Output_section_data
: public Output_data
308 Output_section_data(off_t data_size
, uint64_t addralign
)
309 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
312 Output_section_data(uint64_t addralign
)
313 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
316 // Record the output section.
318 set_output_section(Output_section
* os
)
320 assert(this->output_section_
== NULL
);
321 this->output_section_
= os
;
325 // The child class must implement do_write.
327 // Return the required alignment.
330 { return this->addralign_
; }
332 // Return the section index of the output section.
334 do_out_shndx() const;
337 // The output section for this section.
338 const Output_section
* output_section_
;
339 // The required alignment.
343 // Output_section_common is used to handle the common symbols. This
346 class Output_section_common
: public Output_section_data
349 Output_section_common(uint64_t addralign
)
350 : Output_section_data(addralign
)
355 set_common_size(off_t common_size
)
356 { this->set_data_size(common_size
); }
358 // Write out the data--there is nothing to do, as common symbols are
359 // always zero and are stored in the BSS.
361 do_write(Output_file
*)
365 // Output_section_got is used to manage a GOT. Each entry in the GOT
366 // is for one symbol--either a global symbol or a local symbol in an
367 // object. The target specific code adds entries to the GOT as
368 // needed. The GOT code is then responsible for writing out the data
369 // and for generating relocs as required.
371 template<int size
, bool big_endian
>
372 class Output_section_got
: public Output_section_data
375 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
378 : Output_section_data(Output_data::default_alignment(size
)),
382 // Add an entry for a global symbol to the GOT. This returns the
383 // offset of the new entry from the start of the GOT.
385 add_global(Symbol
* gsym
)
387 this->entries_
.push_back(Got_entry(gsym
));
388 this->set_got_size();
389 return this->last_got_offset();
392 // Add an entry for a local symbol to the GOT. This returns the
393 // offset of the new entry from the start of the GOT.
395 add_local(Object
* object
, unsigned int sym_index
)
397 this->entries_
.push_back(Got_entry(object
, sym_index
));
398 this->set_got_size();
399 return this->last_got_offset();
402 // Add a constant to the GOT. This returns the offset of the new
403 // entry from the start of the GOT.
405 add_constant(Valtype constant
)
407 this->entries_
.push_back(Got_entry(constant
));
408 this->set_got_size();
409 return this->last_got_offset();
412 // Write out the GOT table.
414 do_write(Output_file
*);
417 // This POD class holds a single GOT entry.
421 // Create a zero entry.
423 : local_sym_index_(CONSTANT_CODE
)
424 { this->u_
.constant
= 0; }
426 // Create a global symbol entry.
427 Got_entry(Symbol
* gsym
)
428 : local_sym_index_(GSYM_CODE
)
429 { this->u_
.gsym
= gsym
; }
431 // Create a local symbol entry.
432 Got_entry(Object
* object
, unsigned int local_sym_index
)
433 : local_sym_index_(local_sym_index
)
435 assert(local_sym_index
!= GSYM_CODE
436 && local_sym_index
!= CONSTANT_CODE
);
437 this->u_
.object
= object
;
440 // Create a constant entry. The constant is a host value--it will
441 // be swapped, if necessary, when it is written out.
442 Got_entry(Valtype constant
)
443 : local_sym_index_(CONSTANT_CODE
)
444 { this->u_
.constant
= constant
; }
446 // Write the GOT entry to an output view.
448 write(unsigned char* pov
) const;
459 // For a local symbol, the object.
461 // For a global symbol, the symbol.
463 // For a constant, the constant.
466 // For a local symbol, the local symbol index. This is -1U for a
467 // global symbol, or -2U for a constant.
468 unsigned int local_sym_index_
;
471 typedef std::vector
<Got_entry
> Got_entries
;
473 // Return the offset into the GOT of GOT entry I.
475 got_offset(unsigned int i
) const
476 { return i
* (size
/ 8); }
478 // Return the offset into the GOT of the last entry added.
480 last_got_offset() const
481 { return this->got_offset(this->entries_
.size() - 1); }
483 // Set the size of the section.
486 { this->set_data_size(this->got_offset(this->entries_
.size())); }
488 // The list of GOT entries.
489 Got_entries entries_
;
492 // An output section. We don't expect to have too many output
493 // sections, so we don't bother to do a template on the size.
495 class Output_section
: public Output_data
498 // Create an output section, giving the name, type, and flags.
499 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
,
501 virtual ~Output_section();
503 // Add a new input section SHNDX, named NAME, with header SHDR, from
504 // object OBJECT. Return the offset within the output section.
505 template<int size
, bool big_endian
>
507 add_input_section(Relobj
* object
, unsigned int shndx
, const char *name
,
508 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
510 // Add generated data ODATA to this output section.
512 add_output_section_data(Output_section_data
* posd
);
514 // Return the section name.
517 { return this->name_
; }
519 // Return the section type.
522 { return this->type_
; }
524 // Return the section flags.
527 { return this->flags_
; }
529 // Return the section index in the output file.
532 { return this->out_shndx_
; }
534 // Set the output section index.
536 do_set_out_shndx(unsigned int shndx
)
537 { this->out_shndx_
= shndx
; }
539 // Set the entsize field.
541 set_entsize(uint64_t v
)
542 { this->entsize_
= v
; }
544 // Set the link field.
546 set_link(unsigned int v
)
549 // Set the info field.
551 set_info(unsigned int v
)
554 // Set the addralign field.
556 set_addralign(uint64_t v
)
557 { this->addralign_
= v
; }
559 // Set the address of the Output_section. For a typical
560 // Output_section, there is nothing to do, but if there are any
561 // Output_section_data objects we need to set the final addresses
564 do_set_address(uint64_t, off_t
);
566 // Write the data to the file. For a typical Output_section, this
567 // does nothing: the data is written out by calling Object::Relocate
568 // on each input object. But if there are any Output_section_data
569 // objects we do need to write them out here.
571 do_write(Output_file
*);
573 // Return the address alignment--function required by parent class.
576 { return this->addralign_
; }
578 // Return whether this is an Output_section.
580 do_is_section() const
583 // Return whether this is a section of the specified type.
585 do_is_section_type(elfcpp::Elf_Word type
) const
586 { return this->type_
== type
; }
588 // Return whether the specified section flag is set.
590 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
591 { return (this->flags_
& flag
) != 0; }
593 // Write the section header into *OPHDR.
594 template<int size
, bool big_endian
>
596 write_header(const Stringpool
*, elfcpp::Shdr_write
<size
, big_endian
>*) const;
599 // In some cases we need to keep a list of the input sections
600 // associated with this output section. We only need the list if we
601 // might have to change the offsets of the input section within the
602 // output section after we add the input section. The ordinary
603 // input sections will be written out when we process the object
604 // file, and as such we don't need to track them here. We do need
605 // to track Output_section_data objects here. We store instances of
606 // this structure in a std::vector, so it must be a POD. There can
607 // be many instances of this structure, so we use a union to save
613 : shndx_(0), p2align_(0), data_size_(0)
614 { this->u_
.object
= NULL
; }
616 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
619 p2align_(ffsll(static_cast<long long>(addralign
))),
620 data_size_(data_size
)
622 assert(shndx
!= -1U);
623 this->u_
.object
= object
;
626 Input_section(Output_section_data
* posd
)
628 p2align_(ffsll(static_cast<long long>(posd
->addralign()))),
630 { this->u_
.posd
= posd
; }
632 // The required alignment.
635 { return static_cast<uint64_t>(1) << this->p2align_
; }
637 // Return the required size.
641 // Set the address and file offset. This is called during
642 // Layout::finalize. SECOFF is the file offset of the enclosing
645 set_address(uint64_t addr
, off_t off
, off_t secoff
);
647 // Write out the data. This does nothing for an input section.
652 // Whether this is an input section.
654 is_input_section() const
655 { return this->shndx_
!= -1U; }
657 // For an ordinary input section, this is the section index in
658 // the input file. For an Output_section_data, this is -1U.
660 // The required alignment, stored as a power of 2.
661 unsigned int p2align_
;
662 // For an ordinary input section, the section size.
666 // If shndx_ != -1U, this points to the object which holds the
669 // If shndx_ == -1U, this is the data to write out.
670 Output_section_data
* posd
;
674 typedef std::vector
<Input_section
> Input_section_list
;
676 // Most of these fields are only valid after layout.
678 // The name of the section. This will point into a Stringpool.
680 // The section address is in the parent class.
681 // The section alignment.
683 // The section entry size.
685 // The file offset is in the parent class.
686 // The section link field.
688 // The section info field.
691 elfcpp::Elf_Word type_
;
692 // The section flags.
693 elfcpp::Elf_Xword flags_
;
694 // The section index.
695 unsigned int out_shndx_
;
696 // The input sections. This will be empty in cases where we don't
697 // need to keep track of them.
698 Input_section_list input_sections_
;
699 // The offset of the first entry in input_sections_.
700 off_t first_input_offset_
;
701 // Whether we permit adding data.
705 // A special Output_section which represents the symbol table
706 // (SHT_SYMTAB). The actual data is written out by
707 // Symbol_table::write_globals.
709 class Output_section_symtab
: public Output_section
712 Output_section_symtab(const char* name
, off_t size
);
714 // The data is written out by Symbol_table::write_globals. We don't
717 do_write(Output_file
*)
720 // We don't expect to see any input sections or data here.
722 add_output_section_data(Output_section_data
*)
726 // A special Output_section which holds a string table.
728 class Output_section_strtab
: public Output_section
731 Output_section_strtab(const char* name
, Stringpool
* contents
);
733 // Write out the data.
735 do_write(Output_file
*);
737 // We don't expect to see any input sections or data here.
739 add_output_section_data(Output_section_data
*)
743 Stringpool
* contents_
;
746 // An output segment. PT_LOAD segments are built from collections of
747 // output sections. Other segments typically point within PT_LOAD
748 // segments, and are built directly as needed.
753 // Create an output segment, specifying the type and flags.
754 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
756 // Return the virtual address.
759 { return this->vaddr_
; }
761 // Return the physical address.
764 { return this->paddr_
; }
766 // Return the segment type.
769 { return this->type_
; }
771 // Return the segment flags.
774 { return this->flags_
; }
776 // Return the memory size.
779 { return this->memsz_
; }
781 // Return the file size.
784 { return this->filesz_
; }
786 // Return the maximum alignment of the Output_data.
790 // Add an Output_section to this segment.
792 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
);
794 // Add an Output_data (which is not an Output_section) to the start
797 add_initial_output_data(Output_data
*);
799 // Set the address of the segment to ADDR and the offset to *POFF
800 // (aligned if necessary), and set the addresses and offsets of all
801 // contained output sections accordingly. Set the section indexes
802 // of all contained output sections starting with *PSHNDX. Return
803 // the address of the immediately following segment. Update *POFF
804 // and *PSHNDX. This should only be called for a PT_LOAD segment.
806 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
808 // Set the offset of this segment based on the section. This should
809 // only be called for a non-PT_LOAD segment.
813 // Return the number of output sections.
815 output_section_count() const;
817 // Write the segment header into *OPHDR.
818 template<int size
, bool big_endian
>
820 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
822 // Write the section headers of associated sections into V.
823 template<int size
, bool big_endian
>
825 write_section_headers(const Stringpool
*,
827 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
830 Output_segment(const Output_segment
&);
831 Output_segment
& operator=(const Output_segment
&);
833 typedef std::list
<Output_data
*> Output_data_list
;
835 // Find the maximum alignment in an Output_data_list.
837 maximum_alignment(const Output_data_list
*);
839 // Set the section addresses in an Output_data_list.
841 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
842 unsigned int* pshndx
);
844 // Return the number of Output_sections in an Output_data_list.
846 output_section_count_list(const Output_data_list
*) const;
848 // Write the section headers in the list into V.
849 template<int size
, bool big_endian
>
851 write_section_headers_list(const Stringpool
*, const Output_data_list
*,
853 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
855 // The list of output data with contents attached to this segment.
856 Output_data_list output_data_
;
857 // The list of output data without contents attached to this segment.
858 Output_data_list output_bss_
;
859 // The segment virtual address.
861 // The segment physical address.
863 // The size of the segment in memory.
865 // The segment alignment.
867 // The offset of the segment data within the file.
869 // The size of the segment data in the file.
872 elfcpp::Elf_Word type_
;
873 // The segment flags.
874 elfcpp::Elf_Word flags_
;
875 // Whether we have set align_.
876 bool is_align_known_
;
879 // This class represents the output file.
884 Output_file(const General_options
& options
);
886 // Open the output file. FILE_SIZE is the final size of the file.
888 open(off_t file_size
);
890 // Close the output file and make sure there are no error.
894 // We currently always use mmap which makes the view handling quite
895 // simple. In the future we may support other approaches.
897 // Write data to the output file.
899 write(off_t offset
, const void* data
, off_t len
)
900 { memcpy(this->base_
+ offset
, data
, len
); }
902 // Get a buffer to use to write to the file, given the offset into
903 // the file and the size.
905 get_output_view(off_t start
, off_t size
)
907 assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
908 return this->base_
+ start
;
911 // VIEW must have been returned by get_output_view. Write the
912 // buffer to the file, passing in the offset and the size.
914 write_output_view(off_t
, off_t
, unsigned char*)
919 const General_options
& options_
;
926 // Base of file mapped into memory.
927 unsigned char* base_
;
930 } // End namespace gold.
932 #endif // !defined(GOLD_OUTPUT_H)