New drop, with first cut of section layout code.
[external/binutils.git] / gold / output.h
1 // output.h -- manage the output file for gold   -*- C++ -*-
2
3 #ifndef GOLD_OUTPUT_H
4 #define GOLD_OUTPUT_H
5
6 #include <list>
7
8 #include "elfcpp.h"
9
10 namespace gold
11 {
12
13 class Object;
14 class Output_file;
15
16 // An abtract class for data which has to go into the output file
17 // which is not associated with any input section.
18
19 class Output_data
20 {
21  public:
22   Output_data(off_t size = 0)
23     : size_(size)
24   { }
25
26   virtual
27   ~Output_data();
28
29   // Return the size of the data.
30   off_t
31   size()
32   { return this->size_; }
33
34   // Write the data to the output file at the specified offset.  This
35   // must be implemented by the real class.
36   virtual void
37   write(Output_file*, off_t off) = 0;
38
39  protected:
40   // Set the size of the data.
41   void
42   set_size(off_t size)
43   { this->size_ = size; }
44
45  private:
46   Output_data(const Output_data&);
47   Output_data& operator=(const Output_data&);
48
49   // Size of data in file.
50   off_t size_;
51 };
52
53 // A simple cass of Output_data in which we have constant data to
54 // output.
55
56 class Output_data_const : public Output_data
57 {
58  public:
59   Output_data_const(const std::string& data)
60     : Output_data(data.size()), data_(data)
61   { }
62
63   Output_data_const(const char* p, off_t len)
64     : Output_data(len), data_(p, len)
65   { }
66
67   void
68   write(Output_file* output, off_t off);
69
70  private:
71   std::string data_;
72 };
73
74 // An output section.  We don't expect to have too many output
75 // sections, so we don't bother to do a template on the size.
76
77 class Output_section
78 {
79  public:
80   // Create an output section, giving the name, type, and flags.
81   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
82   ~Output_section();
83
84   // Add a new input section named NAME with header SHDR from object
85   // OBJECT.  Return the offset within the output section.
86   template<int size, bool big_endian>
87   off_t
88   add_input_section(Object* object, const char *name,
89                     const elfcpp::Shdr<size, big_endian>& shdr);
90
91   // Return the section name.
92   const char*
93   name() const
94   { return this->name_; }
95
96   // Return the section type.
97   elfcpp::Elf_Word
98   type() const
99   { return this->type_; }
100
101   // Return the section flags.
102   elfcpp::Elf_Xword
103   flags() const
104   { return this->flags_; }
105
106  private:
107   // Most of these fields are only valid after layout.
108
109   // The name of the section.  This will point into a Stringpool.
110   const char* name_;
111   // The section address.
112   uint64_t addr_;
113   // The section alignment.
114   uint64_t addralign_;
115   // The section entry size.
116   uint64_t entsize_;
117   // The file offset.
118   off_t offset_;
119   // The section size.
120   off_t size_;
121   // The section link field.
122   unsigned int link_;
123   // The section info field.
124   unsigned int info_;
125   // The section type.
126   elfcpp::Elf_Word type_;
127   // The section flags.
128   elfcpp::Elf_Xword flags_;
129 };
130
131 // An output segment.  PT_LOAD segments are built from collections of
132 // output sections.  Other segments typically point within PT_LOAD
133 // segments, and are built directly as needed.
134
135 class Output_segment
136 {
137  public:
138   // Create an output segment, specifying the type and flags.
139   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
140
141   // Return the virtual address.
142   uint64_t
143   vaddr() const
144   { return this->vaddr_; }
145
146   // Return the physical address.
147   uint64_t
148   paddr() const
149   { return this->paddr_; }
150
151   // Return the segment type.
152   elfcpp::Elf_Word
153   type() const
154   { return this->type_; }
155
156   // Return the segment flags.
157   elfcpp::Elf_Word
158   flags() const
159   { return this->flags_; }
160
161   // Add an Output_section to this segment.
162   void
163   add_output_section(Output_section*);
164
165   // Update the segment flags to be compatible with FLAGS.
166   void
167   update_flags(elfcpp::Elf_Word flags)
168   { this->flags_ |= flags & (elfcpp::PF_R | elfcpp::PF_W | elfcpp::PF_X); }
169
170  private:
171   Output_segment(const Output_segment&);
172   Output_segment& operator=(const Output_segment&);
173
174   typedef std::list<Output_section*> Section_list;
175
176   // The list of output sections attached to this segment.  This is
177   // cleared after layout.
178   Section_list output_sections_;
179   // The segment virtual address.
180   uint64_t vaddr_;
181   // The segment physical address.
182   uint64_t paddr_;
183   // The size of the segment in memory.
184   uint64_t memsz_;
185   // The segment alignment.
186   uint64_t align_;
187   // The offset of the segment data within the file.
188   off_t offset_;
189   // The size of the segment data in the file.
190   off_t filesz_;
191   // The segment type;
192   elfcpp::Elf_Word type_;
193   // The segment flags.
194   elfcpp::Elf_Word flags_;
195 };
196
197 // This class represents the output file.  The output file is a
198 // collection of output segments and a collection of output sections
199 // which are not associated with segments.
200
201 class Output_file
202 {
203  public:
204   Output_file();
205   ~Output_file();
206
207   // Write data to the output file.
208   void
209   write(off_t off, const void* data, off_t len);
210 };
211
212 } // End namespace gold.
213
214 #endif // !defined(GOLD_OUTPUT_H)