2006-09-29 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
3#ifndef GOLD_OUTPUT_H
4#define GOLD_OUTPUT_H
5
75f65a3e 6#include <cassert>
a2fb1b05
ILT
7#include <list>
8
9#include "elfcpp.h"
54dc6425 10#include "layout.h"
a2fb1b05
ILT
11
12namespace gold
13{
14
15class Object;
16class Output_file;
17
54dc6425
ILT
18template<int size, bool big_endian>
19class Sized_target;
20
21// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
22
23class Output_data
24{
25 public:
75f65a3e
ILT
26 explicit Output_data(off_t data_size = 0)
27 : address_(0), data_size_(data_size), offset_(0)
a2fb1b05
ILT
28 { }
29
30 virtual
31 ~Output_data();
32
75f65a3e
ILT
33 // Return the address.
34 uint64_t
35 address() const
36 { return this->address_; }
37
38 // Return the size of the data.
a2fb1b05 39 off_t
75f65a3e
ILT
40 data_size() const
41 { return this->data_size_; }
42
43 // Return the file offset.
44 off_t
45 offset() const
46 { return this->offset_; }
47
48 // Return the required alignment.
49 uint64_t
50 addralign() const
51 { return this->do_addralign(); }
52
53 // Return whether this is an Output_section.
54 bool
55 is_section() const
56 { return this->do_is_section(); }
57
58 // Return whether this is an Output_section of the specified type.
59 bool
60 is_section_type(elfcpp::Elf_Word stt) const
61 { return this->do_is_section_type(stt); }
62
63 // Return whether this is an Output_section with the specified flag
64 // set.
65 bool
66 is_section_flag_set(elfcpp::Elf_Xword shf) const
67 { return this->do_is_section_flag_set(shf); }
68
69 // Set the address and file offset of this data.
70 void
71 set_address(uint64_t addr, off_t off);
72
73 // Write the data to the output file.
74 void
75 write(Output_file* file)
76 { this->do_write(file); }
a2fb1b05 77
75f65a3e
ILT
78 protected:
79 // Functions that child classes may or in some cases must implement.
80
81 // Write the data to the output file.
a2fb1b05 82 virtual void
75f65a3e
ILT
83 do_write(Output_file*) = 0;
84
85 // Return the required alignment.
86 virtual uint64_t
87 do_addralign() const = 0;
88
89 // Return whether this is an Output_section.
90 virtual bool
91 do_is_section() const
92 { return false; }
a2fb1b05 93
54dc6425 94 // Return whether this is an Output_section of the specified type.
75f65a3e 95 // This only needs to be implement by Output_section.
54dc6425 96 virtual bool
75f65a3e 97 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
98 { return false; }
99
75f65a3e
ILT
100 // Return whether this is an Output_section with the specific flag
101 // set. This only needs to be implemented by Output_section.
54dc6425 102 virtual bool
75f65a3e 103 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
104 { return false; }
105
75f65a3e
ILT
106 // Set the address and file offset of the data. This only needs to
107 // be implemented if the child needs to know.
108 virtual void
109 do_set_address(uint64_t, off_t)
110 { }
111
112 // Functions that child classes may call.
113
a2fb1b05
ILT
114 // Set the size of the data.
115 void
75f65a3e
ILT
116 set_data_size(off_t data_size)
117 { this->data_size_ = data_size; }
118
119 // Return default alignment for a size--32 or 64.
120 static uint64_t
121 default_alignment(int size);
a2fb1b05
ILT
122
123 private:
124 Output_data(const Output_data&);
125 Output_data& operator=(const Output_data&);
126
75f65a3e
ILT
127 // Memory address in file (not always meaningful).
128 uint64_t address_;
a2fb1b05 129 // Size of data in file.
75f65a3e
ILT
130 off_t data_size_;
131 // Offset within file.
132 off_t offset_;
a2fb1b05
ILT
133};
134
54dc6425 135// A simple case of Output_data in which we have constant data to
a2fb1b05
ILT
136// output.
137
138class Output_data_const : public Output_data
139{
140 public:
75f65a3e
ILT
141 Output_data_const(const std::string& data, uint64_t addralign)
142 : Output_data(data.size()), data_(data), addralign_(addralign)
a2fb1b05
ILT
143 { }
144
75f65a3e
ILT
145 Output_data_const(const char* p, off_t len, uint64_t addralign)
146 : Output_data(len), data_(p, len), addralign_(addralign)
a2fb1b05
ILT
147 { }
148
54dc6425 149 // Write the data to the file.
a2fb1b05 150 void
75f65a3e
ILT
151 do_write(Output_file* output);
152
153 // Return the required alignment.
154 uint64_t
155 do_addralign() const
156 { return this->addralign_; }
a2fb1b05
ILT
157
158 private:
159 std::string data_;
75f65a3e 160 uint64_t addralign_;
a2fb1b05
ILT
161};
162
54dc6425
ILT
163// Output the section headers.
164
165class Output_section_headers : public Output_data
166{
167 public:
75f65a3e
ILT
168 Output_section_headers(int size,
169 const Layout::Segment_list&,
54dc6425
ILT
170 const Layout::Section_list&);
171
172 // Write the data to the file.
173 void
75f65a3e
ILT
174 do_write(Output_file*);
175
176 // Return the required alignment.
177 uint64_t
178 do_addralign() const
179 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
180
181 private:
75f65a3e 182 int size_;
54dc6425
ILT
183 const Layout::Segment_list& segment_list_;
184 const Layout::Section_list& section_list_;
185};
186
187// Output the segment headers.
188
189class Output_segment_headers : public Output_data
190{
191 public:
75f65a3e
ILT
192 Output_segment_headers(int size, const Layout::Segment_list& segment_list)
193 : size_(size), segment_list_(segment_list)
54dc6425
ILT
194 { }
195
196 // Write the data to the file.
197 void
75f65a3e
ILT
198 do_write(Output_file*);
199
200 // Return the required alignment.
201 uint64_t
202 do_addralign() const
203 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
204
205 private:
75f65a3e 206 int size_;
54dc6425
ILT
207 const Layout::Segment_list& segment_list_;
208};
209
210// Output the ELF file header.
211
212class Output_file_header : public Output_data
213{
214 public:
75f65a3e
ILT
215 Output_file_header(int size,
216 const General_options&,
54dc6425
ILT
217 const Target*,
218 const Symbol_table*,
75f65a3e
ILT
219 const Output_segment_headers*);
220
221 // Add information about the section headers. We lay out the ELF
222 // file header before we create the section headers.
223 void set_section_info(const Output_section_headers*,
224 const Output_section* shstrtab);
54dc6425
ILT
225
226 // Write the data to the file.
227 void
75f65a3e
ILT
228 do_write(Output_file*);
229
230 // Return the required alignment.
231 uint64_t
232 do_addralign() const
233 { return Output_data::default_alignment(this->size_); }
234
235 // Set the address and offset--we only implement this for error
236 // checking.
237 void
238 do_set_address(uint64_t, off_t off) const
239 { assert(off == 0); }
54dc6425
ILT
240
241 private:
75f65a3e 242 int size_;
54dc6425
ILT
243 const General_options& options_;
244 const Target* target_;
245 const Symbol_table* symtab_;
246 const Output_segment_headers* program_header_;
247 const Output_section_headers* section_header_;
248 const Output_section* shstrtab_;
249};
250
a2fb1b05
ILT
251// An output section. We don't expect to have too many output
252// sections, so we don't bother to do a template on the size.
253
54dc6425 254class Output_section : public Output_data
a2fb1b05
ILT
255{
256 public:
257 // Create an output section, giving the name, type, and flags.
258 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 259 virtual ~Output_section();
a2fb1b05
ILT
260
261 // Add a new input section named NAME with header SHDR from object
262 // OBJECT. Return the offset within the output section.
263 template<int size, bool big_endian>
264 off_t
265 add_input_section(Object* object, const char *name,
266 const elfcpp::Shdr<size, big_endian>& shdr);
267
268 // Return the section name.
269 const char*
270 name() const
271 { return this->name_; }
272
273 // Return the section type.
274 elfcpp::Elf_Word
275 type() const
276 { return this->type_; }
277
278 // Return the section flags.
279 elfcpp::Elf_Xword
280 flags() const
281 { return this->flags_; }
282
75f65a3e
ILT
283 // Return the address alignment.
284 uint64_t
285 addralign() const
286 { return this->addralign_; }
287
54dc6425
ILT
288 // Write the data to the file. For a typical Output_section, this
289 // does nothing. We write out the data by looping over all the
290 // input sections.
291 virtual void
75f65a3e 292 do_write(Output_file*)
54dc6425
ILT
293 { }
294
75f65a3e
ILT
295 // Return the address alignment--function required by parent class.
296 uint64_t
297 do_addralign() const
298 { return this->addralign_; }
299
300 // Return whether this is an Output_section.
301 bool
302 do_is_section() const
303 { return true; }
304
54dc6425
ILT
305 // Return whether this is a section of the specified type.
306 bool
75f65a3e 307 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
308 { return this->type_ == type; }
309
310 // Return whether the specified section flag is set.
311 bool
75f65a3e 312 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
313 { return (this->flags_ & flag) != 0; }
314
a2fb1b05
ILT
315 private:
316 // Most of these fields are only valid after layout.
317
318 // The name of the section. This will point into a Stringpool.
319 const char* name_;
75f65a3e 320 // The section address is in the parent class.
a2fb1b05
ILT
321 // The section alignment.
322 uint64_t addralign_;
323 // The section entry size.
324 uint64_t entsize_;
75f65a3e 325 // The file offset is in the parent class.
a2fb1b05
ILT
326 // The section link field.
327 unsigned int link_;
328 // The section info field.
329 unsigned int info_;
330 // The section type.
331 elfcpp::Elf_Word type_;
332 // The section flags.
333 elfcpp::Elf_Xword flags_;
334};
335
54dc6425
ILT
336// A special Output_section which represents the symbol table
337// (SHT_SYMTAB).
338
339class Output_section_symtab : public Output_section
340{
341 public:
75f65a3e
ILT
342 Output_section_symtab(const char* name, off_t size);
343};
344
345// A special Output_section which holds a string table.
346
347class Output_section_strtab : public Output_section
348{
349 public:
350 Output_section_strtab(const char* name, Stringpool* contents);
351
352 // Write out the data.
353 void
354 do_write(Output_file*);
355
356 private:
357 Stringpool* contents_;
54dc6425
ILT
358};
359
a2fb1b05
ILT
360// An output segment. PT_LOAD segments are built from collections of
361// output sections. Other segments typically point within PT_LOAD
362// segments, and are built directly as needed.
363
364class Output_segment
365{
366 public:
367 // Create an output segment, specifying the type and flags.
368 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
369
370 // Return the virtual address.
371 uint64_t
372 vaddr() const
373 { return this->vaddr_; }
374
375 // Return the physical address.
376 uint64_t
377 paddr() const
378 { return this->paddr_; }
379
380 // Return the segment type.
381 elfcpp::Elf_Word
382 type() const
383 { return this->type_; }
384
385 // Return the segment flags.
386 elfcpp::Elf_Word
387 flags() const
388 { return this->flags_; }
389
75f65a3e
ILT
390 // Return the maximum alignment of the Output_data.
391 uint64_t
392 max_data_align() const;
393
a2fb1b05
ILT
394 // Add an Output_section to this segment.
395 void
75f65a3e
ILT
396 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags);
397
398 // Add an Output_data (which is not an Output_section) to the start
399 // of this segment.
400 void
401 add_initial_output_data(Output_data*);
402
403 // Set the address of the segment to ADDR and the offset to *POFF
404 // (aligned if necessary), and set the addresses and offsets of all
405 // contained output sections accordingly. Return the address of the
406 // immediately following segment. Update *POFF. This should only
407 // be called for a PT_LOAD segment.
408 uint64_t
409 set_section_addresses(uint64_t addr, off_t* poff);
410
411 // Set the offset of this segment based on the section. This should
412 // only be called for a non-PT_LOAD segment.
413 void
414 set_offset();
415
416 // Return the number of output sections.
417 unsigned int
418 output_section_count() const;
a2fb1b05 419
a2fb1b05
ILT
420 private:
421 Output_segment(const Output_segment&);
422 Output_segment& operator=(const Output_segment&);
423
54dc6425 424 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 425
75f65a3e
ILT
426 // Set the section addresses in an Output_data_list.
427 uint64_t
428 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff);
429
430 // Return the number of Output_sections in an Output_data_list.
431 unsigned int
432 output_section_count_list(const Output_data_list*) const;
433
434 // The list of output data with contents attached to this segment.
54dc6425 435 Output_data_list output_data_;
75f65a3e
ILT
436 // The list of output data without contents attached to this segment.
437 Output_data_list output_bss_;
a2fb1b05
ILT
438 // The segment virtual address.
439 uint64_t vaddr_;
440 // The segment physical address.
441 uint64_t paddr_;
442 // The size of the segment in memory.
443 uint64_t memsz_;
444 // The segment alignment.
445 uint64_t align_;
446 // The offset of the segment data within the file.
447 off_t offset_;
448 // The size of the segment data in the file.
449 off_t filesz_;
450 // The segment type;
451 elfcpp::Elf_Word type_;
452 // The segment flags.
453 elfcpp::Elf_Word flags_;
454};
455
456// This class represents the output file. The output file is a
457// collection of output segments and a collection of output sections
458// which are not associated with segments.
459
460class Output_file
461{
462 public:
463 Output_file();
464 ~Output_file();
465
466 // Write data to the output file.
467 void
468 write(off_t off, const void* data, off_t len);
469};
470
471} // End namespace gold.
472
473#endif // !defined(GOLD_OUTPUT_H)
This page took 0.068 seconds and 4 git commands to generate.