Add Elf_file interface which can be used by both Sized_relobj and
[binutils.git] / gold / output.h
blobdc1653a987ccf9a46a252252c65799ccb49beae2
1 // output.h -- manage the output file for gold -*- C++ -*-
3 #ifndef GOLD_OUTPUT_H
4 #define GOLD_OUTPUT_H
6 #include <cassert>
7 #include <list>
8 #include <vector>
10 #include "elfcpp.h"
11 #include "layout.h"
13 namespace gold
16 class General_options;
17 class Object;
18 class Output_file;
20 template<int size, bool big_endian>
21 class Sized_target;
23 // An abtract class for data which has to go into the output file.
25 class Output_data
27 public:
28 explicit Output_data(off_t data_size = 0)
29 : address_(0), data_size_(data_size), offset_(-1)
30 { }
32 virtual
33 ~Output_data();
35 // Return the address. This is only valid after Layout::finalize is
36 // finished.
37 uint64_t
38 address() const
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
43 // then.
44 off_t
45 data_size() const
46 { return this->data_size_; }
48 // Return the file offset. This is only valid after
49 // Layout::finalize is finished.
50 off_t
51 offset() const
52 { return this->offset_; }
54 // Return the required alignment.
55 uint64_t
56 addralign() const
57 { return this->do_addralign(); }
59 // Return whether this is an Output_section.
60 bool
61 is_section() const
62 { return this->do_is_section(); }
64 // Return whether this is an Output_section of the specified type.
65 bool
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
70 // set.
71 bool
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.
76 unsigned int
77 out_shndx() const
78 { return this->do_out_shndx(); }
80 // Set the output section index, if this is an output section.
81 void
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.
87 void
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.
92 void
93 write(Output_file* file)
94 { this->do_write(file); }
96 protected:
97 // Functions that child classes may or in some cases must implement.
99 // Write the data to the output file.
100 virtual void
101 do_write(Output_file*) = 0;
103 // Return the required alignment.
104 virtual uint64_t
105 do_addralign() const = 0;
107 // Return whether this is an Output_section.
108 virtual bool
109 do_is_section() const
110 { return false; }
112 // Return whether this is an Output_section of the specified type.
113 // This only needs to be implement by Output_section.
114 virtual bool
115 do_is_section_type(elfcpp::Elf_Word) const
116 { return false; }
118 // Return whether this is an Output_section with the specific flag
119 // set. This only needs to be implemented by Output_section.
120 virtual bool
121 do_is_section_flag_set(elfcpp::Elf_Xword) const
122 { return false; }
124 // Return the output section index, if there is an output section.
125 virtual unsigned int
126 do_out_shndx() const
127 { abort(); }
129 // Set the output section index, if this is an output section.
130 virtual void
131 do_set_out_shndx(unsigned int)
132 { abort(); }
134 // Set the address and file offset of the data. This only needs to
135 // be implemented if the child needs to know.
136 virtual void
137 do_set_address(uint64_t, off_t)
140 // Functions that child classes may call.
142 // Set the size of the data.
143 void
144 set_data_size(off_t data_size)
145 { this->data_size_ = data_size; }
147 // Return default alignment for a size--32 or 64.
148 static uint64_t
149 default_alignment(int size);
151 private:
152 Output_data(const Output_data&);
153 Output_data& operator=(const Output_data&);
155 // Memory address in file (not always meaningful).
156 uint64_t address_;
157 // Size of data in file.
158 off_t data_size_;
159 // Offset within file.
160 off_t offset_;
163 // A simple case of Output_data in which we have constant data to
164 // output.
166 class Output_data_const : public Output_data
168 public:
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.
178 void
179 do_write(Output_file* output);
181 // Return the required alignment.
182 uint64_t
183 do_addralign() const
184 { return this->addralign_; }
186 private:
187 std::string data_;
188 uint64_t addralign_;
191 // Output the section headers.
193 class Output_section_headers : public Output_data
195 public:
196 Output_section_headers(int size,
197 bool big_endian,
198 const Layout::Segment_list&,
199 const Layout::Section_list&,
200 const Stringpool*);
202 // Write the data to the file.
203 void
204 do_write(Output_file*);
206 // Return the required alignment.
207 uint64_t
208 do_addralign() const
209 { return Output_data::default_alignment(this->size_); }
211 private:
212 // Write the data to the file with the right size and endianness.
213 template<int size, bool big_endian>
214 void
215 do_sized_write(Output_file*);
217 int size_;
218 bool big_endian_;
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
228 public:
229 Output_segment_headers(int size, bool big_endian,
230 const Layout::Segment_list& segment_list);
232 // Write the data to the file.
233 void
234 do_write(Output_file*);
236 // Return the required alignment.
237 uint64_t
238 do_addralign() const
239 { return Output_data::default_alignment(this->size_); }
241 private:
242 // Write the data to the file with the right size and endianness.
243 template<int size, bool big_endian>
244 void
245 do_sized_write(Output_file*);
247 int size_;
248 bool big_endian_;
249 const Layout::Segment_list& segment_list_;
252 // Output the ELF file header.
254 class Output_file_header : public Output_data
256 public:
257 Output_file_header(int size,
258 bool big_endian,
259 const General_options&,
260 const Target*,
261 const Symbol_table*,
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.
270 void
271 do_write(Output_file*);
273 // Return the required alignment.
274 uint64_t
275 do_addralign() const
276 { return Output_data::default_alignment(this->size_); }
278 // Set the address and offset--we only implement this for error
279 // checking.
280 void
281 do_set_address(uint64_t, off_t off) const
282 { assert(off == 0); }
284 private:
285 // Write the data to the file with the right size and endianness.
286 template<int size, bool big_endian>
287 void
288 do_sized_write(Output_file*);
290 int size_;
291 bool big_endian_;
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
307 public:
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.
317 void
318 set_output_section(Output_section* os)
320 assert(this->output_section_ == NULL);
321 this->output_section_ = os;
324 protected:
325 // The child class must implement do_write.
327 // Return the required alignment.
328 uint64_t
329 do_addralign() const
330 { return this->addralign_; }
332 // Return the section index of the output section.
333 unsigned int
334 do_out_shndx() const;
336 private:
337 // The output section for this section.
338 const Output_section* output_section_;
339 // The required alignment.
340 uint64_t addralign_;
343 // Output_section_common is used to handle the common symbols. This
344 // is quite simple.
346 class Output_section_common : public Output_section_data
348 public:
349 Output_section_common(uint64_t addralign)
350 : Output_section_data(addralign)
353 // Set the size.
354 void
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.
360 void
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
374 public:
375 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
377 Output_section_got()
378 : Output_section_data(Output_data::default_alignment(size)),
379 entries_()
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.
384 unsigned int
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.
394 unsigned int
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.
404 unsigned int
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.
413 void
414 do_write(Output_file*);
416 private:
417 // This POD class holds a single GOT entry.
418 class Got_entry
420 public:
421 // Create a zero entry.
422 Got_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.
447 void
448 write(unsigned char* pov) const;
450 private:
451 enum
453 GSYM_CODE = -1U,
454 CONSTANT_CODE = -2U
457 union
459 // For a local symbol, the object.
460 Object* object;
461 // For a global symbol, the symbol.
462 Symbol* gsym;
463 // For a constant, the constant.
464 Valtype constant;
465 } u_;
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.
474 unsigned int
475 got_offset(unsigned int i) const
476 { return i * (size / 8); }
478 // Return the offset into the GOT of the last entry added.
479 unsigned int
480 last_got_offset() const
481 { return this->got_offset(this->entries_.size() - 1); }
483 // Set the size of the section.
484 void
485 set_got_size()
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
497 public:
498 // Create an output section, giving the name, type, and flags.
499 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
500 bool may_add_data);
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>
506 off_t
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.
511 virtual void
512 add_output_section_data(Output_section_data* posd);
514 // Return the section name.
515 const char*
516 name() const
517 { return this->name_; }
519 // Return the section type.
520 elfcpp::Elf_Word
521 type() const
522 { return this->type_; }
524 // Return the section flags.
525 elfcpp::Elf_Xword
526 flags() const
527 { return this->flags_; }
529 // Return the section index in the output file.
530 unsigned int
531 do_out_shndx() const
532 { return this->out_shndx_; }
534 // Set the output section index.
535 void
536 do_set_out_shndx(unsigned int shndx)
537 { this->out_shndx_ = shndx; }
539 // Set the entsize field.
540 void
541 set_entsize(uint64_t v)
542 { this->entsize_ = v; }
544 // Set the link field.
545 void
546 set_link(unsigned int v)
547 { this->link_ = v; }
549 // Set the info field.
550 void
551 set_info(unsigned int v)
552 { this->info_ = v; }
554 // Set the addralign field.
555 void
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
562 // here.
563 void
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.
570 virtual void
571 do_write(Output_file*);
573 // Return the address alignment--function required by parent class.
574 uint64_t
575 do_addralign() const
576 { return this->addralign_; }
578 // Return whether this is an Output_section.
579 bool
580 do_is_section() const
581 { return true; }
583 // Return whether this is a section of the specified type.
584 bool
585 do_is_section_type(elfcpp::Elf_Word type) const
586 { return this->type_ == type; }
588 // Return whether the specified section flag is set.
589 bool
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>
595 void
596 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
598 private:
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
608 // some space.
609 class Input_section
611 public:
612 Input_section()
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,
617 uint64_t addralign)
618 : shndx_(shndx),
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)
627 : shndx_(-1U),
628 p2align_(ffsll(static_cast<long long>(posd->addralign()))),
629 data_size_(0)
630 { this->u_.posd = posd; }
632 // The required alignment.
633 uint64_t
634 addralign() const
635 { return static_cast<uint64_t>(1) << this->p2align_; }
637 // Return the required size.
638 off_t
639 data_size() const;
641 // Set the address and file offset. This is called during
642 // Layout::finalize. SECOFF is the file offset of the enclosing
643 // section.
644 void
645 set_address(uint64_t addr, off_t off, off_t secoff);
647 // Write out the data. This does nothing for an input section.
648 void
649 write(Output_file*);
651 private:
652 // Whether this is an input section.
653 bool
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.
659 unsigned int shndx_;
660 // The required alignment, stored as a power of 2.
661 unsigned int p2align_;
662 // For an ordinary input section, the section size.
663 off_t data_size_;
664 union
666 // If shndx_ != -1U, this points to the object which holds the
667 // input section.
668 Relobj* object;
669 // If shndx_ == -1U, this is the data to write out.
670 Output_section_data* posd;
671 } u_;
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.
679 const char* name_;
680 // The section address is in the parent class.
681 // The section alignment.
682 uint64_t addralign_;
683 // The section entry size.
684 uint64_t entsize_;
685 // The file offset is in the parent class.
686 // The section link field.
687 unsigned int link_;
688 // The section info field.
689 unsigned int info_;
690 // The section type.
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.
702 bool may_add_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
711 public:
712 Output_section_symtab(const char* name, off_t size);
714 // The data is written out by Symbol_table::write_globals. We don't
715 // do anything here.
716 void
717 do_write(Output_file*)
720 // We don't expect to see any input sections or data here.
721 void
722 add_output_section_data(Output_section_data*)
723 { abort(); }
726 // A special Output_section which holds a string table.
728 class Output_section_strtab : public Output_section
730 public:
731 Output_section_strtab(const char* name, Stringpool* contents);
733 // Write out the data.
734 void
735 do_write(Output_file*);
737 // We don't expect to see any input sections or data here.
738 void
739 add_output_section_data(Output_section_data*)
740 { abort(); }
742 private:
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.
750 class Output_segment
752 public:
753 // Create an output segment, specifying the type and flags.
754 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
756 // Return the virtual address.
757 uint64_t
758 vaddr() const
759 { return this->vaddr_; }
761 // Return the physical address.
762 uint64_t
763 paddr() const
764 { return this->paddr_; }
766 // Return the segment type.
767 elfcpp::Elf_Word
768 type() const
769 { return this->type_; }
771 // Return the segment flags.
772 elfcpp::Elf_Word
773 flags() const
774 { return this->flags_; }
776 // Return the memory size.
777 uint64_t
778 memsz() const
779 { return this->memsz_; }
781 // Return the file size.
782 off_t
783 filesz() const
784 { return this->filesz_; }
786 // Return the maximum alignment of the Output_data.
787 uint64_t
788 addralign();
790 // Add an Output_section to this segment.
791 void
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
795 // of this segment.
796 void
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.
805 uint64_t
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.
810 void
811 set_offset();
813 // Return the number of output sections.
814 unsigned int
815 output_section_count() const;
817 // Write the segment header into *OPHDR.
818 template<int size, bool big_endian>
819 void
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>
824 unsigned char*
825 write_section_headers(const Stringpool*,
826 unsigned char* v,
827 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
829 private:
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.
836 static uint64_t
837 maximum_alignment(const Output_data_list*);
839 // Set the section addresses in an Output_data_list.
840 uint64_t
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.
845 unsigned int
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>
850 unsigned char*
851 write_section_headers_list(const Stringpool*, const Output_data_list*,
852 unsigned char* v,
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.
860 uint64_t vaddr_;
861 // The segment physical address.
862 uint64_t paddr_;
863 // The size of the segment in memory.
864 uint64_t memsz_;
865 // The segment alignment.
866 uint64_t align_;
867 // The offset of the segment data within the file.
868 off_t offset_;
869 // The size of the segment data in the file.
870 off_t filesz_;
871 // The segment type;
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.
881 class Output_file
883 public:
884 Output_file(const General_options& options);
886 // Open the output file. FILE_SIZE is the final size of the file.
887 void
888 open(off_t file_size);
890 // Close the output file and make sure there are no error.
891 void
892 close();
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.
898 void
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.
904 unsigned char*
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.
913 void
914 write_output_view(off_t, off_t, unsigned char*)
917 private:
918 // General options.
919 const General_options& options_;
920 // File name.
921 const char* name_;
922 // File descriptor.
923 int o_;
924 // File size.
925 off_t file_size_;
926 // Base of file mapped into memory.
927 unsigned char* base_;
930 } // End namespace gold.
932 #endif // !defined(GOLD_OUTPUT_H)