Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
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" | |
54dc6425 | 9 | #include "layout.h" |
a2fb1b05 ILT |
10 | |
11 | namespace gold | |
12 | { | |
13 | ||
14 | class Object; | |
15 | class Output_file; | |
16 | ||
54dc6425 ILT |
17 | template<int size, bool big_endian> |
18 | class Sized_target; | |
19 | ||
20 | // An abtract class for data which has to go into the output file. | |
a2fb1b05 ILT |
21 | |
22 | class Output_data | |
23 | { | |
24 | public: | |
25 | Output_data(off_t size = 0) | |
26 | : size_(size) | |
27 | { } | |
28 | ||
29 | virtual | |
30 | ~Output_data(); | |
31 | ||
54dc6425 ILT |
32 | // Return the size of the data. This can't be called "size" since |
33 | // that interferes with the widely used template parameter name. | |
a2fb1b05 | 34 | off_t |
54dc6425 | 35 | get_size() |
a2fb1b05 ILT |
36 | { return this->size_; } |
37 | ||
38 | // Write the data to the output file at the specified offset. This | |
39 | // must be implemented by the real class. | |
40 | virtual void | |
41 | write(Output_file*, off_t off) = 0; | |
42 | ||
54dc6425 ILT |
43 | // Return whether this is an Output_section of the specified type. |
44 | virtual bool | |
45 | is_section_type(elfcpp::Elf_Word) | |
46 | { return false; } | |
47 | ||
48 | // Return whether this is an Output_section with the specified flag | |
49 | // set. | |
50 | virtual bool | |
51 | is_section_flag_set(elfcpp::Elf_Xword) | |
52 | { return false; } | |
53 | ||
a2fb1b05 ILT |
54 | protected: |
55 | // Set the size of the data. | |
56 | void | |
57 | set_size(off_t size) | |
58 | { this->size_ = size; } | |
59 | ||
60 | private: | |
61 | Output_data(const Output_data&); | |
62 | Output_data& operator=(const Output_data&); | |
63 | ||
64 | // Size of data in file. | |
65 | off_t size_; | |
66 | }; | |
67 | ||
54dc6425 | 68 | // A simple case of Output_data in which we have constant data to |
a2fb1b05 ILT |
69 | // output. |
70 | ||
71 | class Output_data_const : public Output_data | |
72 | { | |
73 | public: | |
74 | Output_data_const(const std::string& data) | |
75 | : Output_data(data.size()), data_(data) | |
76 | { } | |
77 | ||
78 | Output_data_const(const char* p, off_t len) | |
79 | : Output_data(len), data_(p, len) | |
80 | { } | |
81 | ||
54dc6425 | 82 | // Write the data to the file. |
a2fb1b05 ILT |
83 | void |
84 | write(Output_file* output, off_t off); | |
85 | ||
86 | private: | |
87 | std::string data_; | |
88 | }; | |
89 | ||
54dc6425 ILT |
90 | // Output the section headers. |
91 | ||
92 | class Output_section_headers : public Output_data | |
93 | { | |
94 | public: | |
95 | Output_section_headers(const Layout::Segment_list&, | |
96 | const Layout::Section_list&); | |
97 | ||
98 | // Write the data to the file. | |
99 | void | |
100 | write(Output_file*, off_t); | |
101 | ||
102 | private: | |
103 | const Layout::Segment_list& segment_list_; | |
104 | const Layout::Section_list& section_list_; | |
105 | }; | |
106 | ||
107 | // Output the segment headers. | |
108 | ||
109 | class Output_segment_headers : public Output_data | |
110 | { | |
111 | public: | |
112 | Output_segment_headers(const Layout::Segment_list& segment_list) | |
113 | : segment_list_(segment_list) | |
114 | { } | |
115 | ||
116 | // Write the data to the file. | |
117 | void | |
118 | write(Output_file*, off_t); | |
119 | ||
120 | private: | |
121 | const Layout::Segment_list& segment_list_; | |
122 | }; | |
123 | ||
124 | // Output the ELF file header. | |
125 | ||
126 | class Output_file_header : public Output_data | |
127 | { | |
128 | public: | |
129 | Output_file_header(const General_options&, | |
130 | const Target*, | |
131 | const Symbol_table*, | |
132 | const Output_segment_headers*, | |
133 | const Output_section_headers*, | |
134 | const Output_section* shstrtab); | |
135 | ||
136 | // Write the data to the file. | |
137 | void | |
138 | write(Output_file*, off_t); | |
139 | ||
140 | private: | |
141 | const General_options& options_; | |
142 | const Target* target_; | |
143 | const Symbol_table* symtab_; | |
144 | const Output_segment_headers* program_header_; | |
145 | const Output_section_headers* section_header_; | |
146 | const Output_section* shstrtab_; | |
147 | }; | |
148 | ||
a2fb1b05 ILT |
149 | // An output section. We don't expect to have too many output |
150 | // sections, so we don't bother to do a template on the size. | |
151 | ||
54dc6425 | 152 | class Output_section : public Output_data |
a2fb1b05 ILT |
153 | { |
154 | public: | |
155 | // Create an output section, giving the name, type, and flags. | |
156 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); | |
54dc6425 | 157 | virtual ~Output_section(); |
a2fb1b05 ILT |
158 | |
159 | // Add a new input section named NAME with header SHDR from object | |
160 | // OBJECT. Return the offset within the output section. | |
161 | template<int size, bool big_endian> | |
162 | off_t | |
163 | add_input_section(Object* object, const char *name, | |
164 | const elfcpp::Shdr<size, big_endian>& shdr); | |
165 | ||
166 | // Return the section name. | |
167 | const char* | |
168 | name() const | |
169 | { return this->name_; } | |
170 | ||
171 | // Return the section type. | |
172 | elfcpp::Elf_Word | |
173 | type() const | |
174 | { return this->type_; } | |
175 | ||
176 | // Return the section flags. | |
177 | elfcpp::Elf_Xword | |
178 | flags() const | |
179 | { return this->flags_; } | |
180 | ||
54dc6425 ILT |
181 | // Write the data to the file. For a typical Output_section, this |
182 | // does nothing. We write out the data by looping over all the | |
183 | // input sections. | |
184 | virtual void | |
185 | write(Output_file*, off_t) | |
186 | { } | |
187 | ||
188 | // Return whether this is a section of the specified type. | |
189 | bool | |
190 | is_section_type(elfcpp::Elf_Word type) | |
191 | { return this->type_ == type; } | |
192 | ||
193 | // Return whether the specified section flag is set. | |
194 | bool | |
195 | is_section_flag_set(elfcpp::Elf_Xword flag) | |
196 | { return (this->flags_ & flag) != 0; } | |
197 | ||
a2fb1b05 ILT |
198 | private: |
199 | // Most of these fields are only valid after layout. | |
200 | ||
201 | // The name of the section. This will point into a Stringpool. | |
202 | const char* name_; | |
203 | // The section address. | |
204 | uint64_t addr_; | |
205 | // The section alignment. | |
206 | uint64_t addralign_; | |
207 | // The section entry size. | |
208 | uint64_t entsize_; | |
209 | // The file offset. | |
210 | off_t offset_; | |
a2fb1b05 ILT |
211 | // The section link field. |
212 | unsigned int link_; | |
213 | // The section info field. | |
214 | unsigned int info_; | |
215 | // The section type. | |
216 | elfcpp::Elf_Word type_; | |
217 | // The section flags. | |
218 | elfcpp::Elf_Xword flags_; | |
219 | }; | |
220 | ||
54dc6425 ILT |
221 | // A special Output_section which represents the symbol table |
222 | // (SHT_SYMTAB). | |
223 | ||
224 | class Output_section_symtab : public Output_section | |
225 | { | |
226 | public: | |
227 | Output_section_symtab(); | |
228 | ~Output_section_symtab(); | |
229 | }; | |
230 | ||
a2fb1b05 ILT |
231 | // An output segment. PT_LOAD segments are built from collections of |
232 | // output sections. Other segments typically point within PT_LOAD | |
233 | // segments, and are built directly as needed. | |
234 | ||
235 | class Output_segment | |
236 | { | |
237 | public: | |
238 | // Create an output segment, specifying the type and flags. | |
239 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
240 | ||
241 | // Return the virtual address. | |
242 | uint64_t | |
243 | vaddr() const | |
244 | { return this->vaddr_; } | |
245 | ||
246 | // Return the physical address. | |
247 | uint64_t | |
248 | paddr() const | |
249 | { return this->paddr_; } | |
250 | ||
251 | // Return the segment type. | |
252 | elfcpp::Elf_Word | |
253 | type() const | |
254 | { return this->type_; } | |
255 | ||
256 | // Return the segment flags. | |
257 | elfcpp::Elf_Word | |
258 | flags() const | |
259 | { return this->flags_; } | |
260 | ||
261 | // Add an Output_section to this segment. | |
262 | void | |
263 | add_output_section(Output_section*); | |
264 | ||
a2fb1b05 ILT |
265 | private: |
266 | Output_segment(const Output_segment&); | |
267 | Output_segment& operator=(const Output_segment&); | |
268 | ||
54dc6425 | 269 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 ILT |
270 | |
271 | // The list of output sections attached to this segment. This is | |
272 | // cleared after layout. | |
54dc6425 | 273 | Output_data_list output_data_; |
a2fb1b05 ILT |
274 | // The segment virtual address. |
275 | uint64_t vaddr_; | |
276 | // The segment physical address. | |
277 | uint64_t paddr_; | |
278 | // The size of the segment in memory. | |
279 | uint64_t memsz_; | |
280 | // The segment alignment. | |
281 | uint64_t align_; | |
282 | // The offset of the segment data within the file. | |
283 | off_t offset_; | |
284 | // The size of the segment data in the file. | |
285 | off_t filesz_; | |
286 | // The segment type; | |
287 | elfcpp::Elf_Word type_; | |
288 | // The segment flags. | |
289 | elfcpp::Elf_Word flags_; | |
290 | }; | |
291 | ||
292 | // This class represents the output file. The output file is a | |
293 | // collection of output segments and a collection of output sections | |
294 | // which are not associated with segments. | |
295 | ||
296 | class Output_file | |
297 | { | |
298 | public: | |
299 | Output_file(); | |
300 | ~Output_file(); | |
301 | ||
302 | // Write data to the output file. | |
303 | void | |
304 | write(off_t off, const void* data, off_t len); | |
305 | }; | |
306 | ||
307 | } // End namespace gold. | |
308 | ||
309 | #endif // !defined(GOLD_OUTPUT_H) |