Snapshot. Now able to produce a minimal executable which actually
[deliverable/binutils-gdb.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 <cassert>
7 #include <list>
8
9 #include "elfcpp.h"
10 #include "layout.h"
11
12 namespace gold
13 {
14
15 class General_options;
16 class Object;
17 class Output_file;
18
19 template<int size, bool big_endian>
20 class Sized_target;
21
22 // An abtract class for data which has to go into the output file.
23
24 class Output_data
25 {
26 public:
27 explicit Output_data(off_t data_size = 0)
28 : address_(0), data_size_(data_size), offset_(-1)
29 { }
30
31 virtual
32 ~Output_data();
33
34 // Return the address.
35 uint64_t
36 address() const
37 { return this->address_; }
38
39 // Return the size of the data.
40 off_t
41 data_size() const
42 { return this->data_size_; }
43
44 // Return the file offset.
45 off_t
46 offset() const
47 { return this->offset_; }
48
49 // Return the required alignment.
50 uint64_t
51 addralign() const
52 { return this->do_addralign(); }
53
54 // Return whether this is an Output_section.
55 bool
56 is_section() const
57 { return this->do_is_section(); }
58
59 // Return whether this is an Output_section of the specified type.
60 bool
61 is_section_type(elfcpp::Elf_Word stt) const
62 { return this->do_is_section_type(stt); }
63
64 // Return whether this is an Output_section with the specified flag
65 // set.
66 bool
67 is_section_flag_set(elfcpp::Elf_Xword shf) const
68 { return this->do_is_section_flag_set(shf); }
69
70 // Set the address and file offset of this data.
71 void
72 set_address(uint64_t addr, off_t off);
73
74 // Write the data to the output file.
75 void
76 write(Output_file* file)
77 { this->do_write(file); }
78
79 protected:
80 // Functions that child classes may or in some cases must implement.
81
82 // Write the data to the output file.
83 virtual void
84 do_write(Output_file*) = 0;
85
86 // Return the required alignment.
87 virtual uint64_t
88 do_addralign() const = 0;
89
90 // Return whether this is an Output_section.
91 virtual bool
92 do_is_section() const
93 { return false; }
94
95 // Return whether this is an Output_section of the specified type.
96 // This only needs to be implement by Output_section.
97 virtual bool
98 do_is_section_type(elfcpp::Elf_Word) const
99 { return false; }
100
101 // Return whether this is an Output_section with the specific flag
102 // set. This only needs to be implemented by Output_section.
103 virtual bool
104 do_is_section_flag_set(elfcpp::Elf_Xword) const
105 { return false; }
106
107 // Set the address and file offset of the data. This only needs to
108 // be implemented if the child needs to know.
109 virtual void
110 do_set_address(uint64_t, off_t)
111 { }
112
113 // Functions that child classes may call.
114
115 // Set the size of the data.
116 void
117 set_data_size(off_t data_size)
118 { this->data_size_ = data_size; }
119
120 // Return default alignment for a size--32 or 64.
121 static uint64_t
122 default_alignment(int size);
123
124 private:
125 Output_data(const Output_data&);
126 Output_data& operator=(const Output_data&);
127
128 // Memory address in file (not always meaningful).
129 uint64_t address_;
130 // Size of data in file.
131 off_t data_size_;
132 // Offset within file.
133 off_t offset_;
134 };
135
136 // A simple case of Output_data in which we have constant data to
137 // output.
138
139 class Output_data_const : public Output_data
140 {
141 public:
142 Output_data_const(const std::string& data, uint64_t addralign)
143 : Output_data(data.size()), data_(data), addralign_(addralign)
144 { }
145
146 Output_data_const(const char* p, off_t len, uint64_t addralign)
147 : Output_data(len), data_(p, len), addralign_(addralign)
148 { }
149
150 // Write the data to the file.
151 void
152 do_write(Output_file* output);
153
154 // Return the required alignment.
155 uint64_t
156 do_addralign() const
157 { return this->addralign_; }
158
159 private:
160 std::string data_;
161 uint64_t addralign_;
162 };
163
164 // Output the section headers.
165
166 class Output_section_headers : public Output_data
167 {
168 public:
169 Output_section_headers(int size,
170 bool big_endian,
171 const Layout::Segment_list&,
172 const Layout::Section_list&,
173 const Stringpool*);
174
175 // Write the data to the file.
176 void
177 do_write(Output_file*);
178
179 // Return the required alignment.
180 uint64_t
181 do_addralign() const
182 { return Output_data::default_alignment(this->size_); }
183
184 private:
185 // Write the data to the file with the right size and endianness.
186 template<int size, bool big_endian>
187 void
188 do_sized_write(Output_file*);
189
190 int size_;
191 bool big_endian_;
192 const Layout::Segment_list& segment_list_;
193 const Layout::Section_list& section_list_;
194 const Stringpool* secnamepool_;
195 };
196
197 // Output the segment headers.
198
199 class Output_segment_headers : public Output_data
200 {
201 public:
202 Output_segment_headers(int size, bool big_endian,
203 const Layout::Segment_list& segment_list);
204
205 // Write the data to the file.
206 void
207 do_write(Output_file*);
208
209 // Return the required alignment.
210 uint64_t
211 do_addralign() const
212 { return Output_data::default_alignment(this->size_); }
213
214 private:
215 // Write the data to the file with the right size and endianness.
216 template<int size, bool big_endian>
217 void
218 do_sized_write(Output_file*);
219
220 int size_;
221 bool big_endian_;
222 const Layout::Segment_list& segment_list_;
223 };
224
225 // Output the ELF file header.
226
227 class Output_file_header : public Output_data
228 {
229 public:
230 Output_file_header(int size,
231 bool big_endian,
232 const General_options&,
233 const Target*,
234 const Symbol_table*,
235 const Output_segment_headers*);
236
237 // Add information about the section headers. We lay out the ELF
238 // file header before we create the section headers.
239 void set_section_info(const Output_section_headers*,
240 const Output_section* shstrtab);
241
242 // Write the data to the file.
243 void
244 do_write(Output_file*);
245
246 // Return the required alignment.
247 uint64_t
248 do_addralign() const
249 { return Output_data::default_alignment(this->size_); }
250
251 // Set the address and offset--we only implement this for error
252 // checking.
253 void
254 do_set_address(uint64_t, off_t off) const
255 { assert(off == 0); }
256
257 private:
258 // Write the data to the file with the right size and endianness.
259 template<int size, bool big_endian>
260 void
261 do_sized_write(Output_file*);
262
263 int size_;
264 bool big_endian_;
265 const General_options& options_;
266 const Target* target_;
267 const Symbol_table* symtab_;
268 const Output_segment_headers* segment_header_;
269 const Output_section_headers* section_header_;
270 const Output_section* shstrtab_;
271 };
272
273 // An output section. We don't expect to have too many output
274 // sections, so we don't bother to do a template on the size.
275
276 class Output_section : public Output_data
277 {
278 public:
279 // Create an output section, giving the name, type, and flags.
280 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
281 unsigned int shndx);
282 virtual ~Output_section();
283
284 // Add a new input section named NAME with header SHDR from object
285 // OBJECT. Return the offset within the output section.
286 template<int size, bool big_endian>
287 off_t
288 add_input_section(Object* object, const char *name,
289 const elfcpp::Shdr<size, big_endian>& shdr);
290
291 // Return the section name.
292 const char*
293 name() const
294 { return this->name_; }
295
296 // Return the section type.
297 elfcpp::Elf_Word
298 type() const
299 { return this->type_; }
300
301 // Return the section flags.
302 elfcpp::Elf_Xword
303 flags() const
304 { return this->flags_; }
305
306 // Return the address alignment.
307 uint64_t
308 addralign() const
309 { return this->addralign_; }
310
311 // Return the section index.
312 unsigned int
313 shndx() const
314 { return this->shndx_; }
315
316 // Set the entsize field.
317 void
318 set_entsize(uint64_t v)
319 { this->entsize_ = v; }
320
321 // Set the link field.
322 void
323 set_link(unsigned int v)
324 { this->link_ = v; }
325
326 // Set the info field.
327 void
328 set_info(unsigned int v)
329 { this->info_ = v; }
330
331 // Set the addralign field.
332 void
333 set_addralign(uint64_t v)
334 { this->addralign_ = v; }
335
336 // Write the data to the file. For a typical Output_section, this
337 // does nothing. We write out the data by looping over all the
338 // input sections.
339 virtual void
340 do_write(Output_file*)
341 { }
342
343 // Return the address alignment--function required by parent class.
344 uint64_t
345 do_addralign() const
346 { return this->addralign_; }
347
348 // Return whether this is an Output_section.
349 bool
350 do_is_section() const
351 { return true; }
352
353 // Return whether this is a section of the specified type.
354 bool
355 do_is_section_type(elfcpp::Elf_Word type) const
356 { return this->type_ == type; }
357
358 // Return whether the specified section flag is set.
359 bool
360 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
361 { return (this->flags_ & flag) != 0; }
362
363 // Write the section header into *OPHDR.
364 template<int size, bool big_endian>
365 void
366 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
367
368 private:
369 // Most of these fields are only valid after layout.
370
371 // The name of the section. This will point into a Stringpool.
372 const char* name_;
373 // The section address is in the parent class.
374 // The section alignment.
375 uint64_t addralign_;
376 // The section entry size.
377 uint64_t entsize_;
378 // The file offset is in the parent class.
379 // The section link field.
380 unsigned int link_;
381 // The section info field.
382 unsigned int info_;
383 // The section type.
384 elfcpp::Elf_Word type_;
385 // The section flags.
386 elfcpp::Elf_Xword flags_;
387 // The section index.
388 unsigned int shndx_;
389 };
390
391 // A special Output_section which represents the symbol table
392 // (SHT_SYMTAB).
393
394 class Output_section_symtab : public Output_section
395 {
396 public:
397 Output_section_symtab(const char* name, off_t size, unsigned int shndx);
398 };
399
400 // A special Output_section which holds a string table.
401
402 class Output_section_strtab : public Output_section
403 {
404 public:
405 Output_section_strtab(const char* name, Stringpool* contents,
406 unsigned int shndx);
407
408 // Write out the data.
409 void
410 do_write(Output_file*);
411
412 private:
413 Stringpool* contents_;
414 };
415
416 // An output segment. PT_LOAD segments are built from collections of
417 // output sections. Other segments typically point within PT_LOAD
418 // segments, and are built directly as needed.
419
420 class Output_segment
421 {
422 public:
423 // Create an output segment, specifying the type and flags.
424 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
425
426 // Return the virtual address.
427 uint64_t
428 vaddr() const
429 { return this->vaddr_; }
430
431 // Return the physical address.
432 uint64_t
433 paddr() const
434 { return this->paddr_; }
435
436 // Return the segment type.
437 elfcpp::Elf_Word
438 type() const
439 { return this->type_; }
440
441 // Return the segment flags.
442 elfcpp::Elf_Word
443 flags() const
444 { return this->flags_; }
445
446 // Return the maximum alignment of the Output_data.
447 uint64_t
448 max_data_align() const;
449
450 // Add an Output_section to this segment.
451 void
452 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags);
453
454 // Add an Output_data (which is not an Output_section) to the start
455 // of this segment.
456 void
457 add_initial_output_data(Output_data*);
458
459 // Set the address of the segment to ADDR and the offset to *POFF
460 // (aligned if necessary), and set the addresses and offsets of all
461 // contained output sections accordingly. Return the address of the
462 // immediately following segment. Update *POFF. This should only
463 // be called for a PT_LOAD segment.
464 uint64_t
465 set_section_addresses(uint64_t addr, off_t* poff);
466
467 // Set the offset of this segment based on the section. This should
468 // only be called for a non-PT_LOAD segment.
469 void
470 set_offset();
471
472 // Return the number of output sections.
473 unsigned int
474 output_section_count() const;
475
476 // Write the segment header into *OPHDR.
477 template<int size, bool big_endian>
478 void
479 write_header(elfcpp::Phdr_write<size, big_endian>*) const;
480
481 // Write the section headers of associated sections into V.
482 template<int size, bool big_endian>
483 unsigned char*
484 write_section_headers(const Stringpool*, unsigned char* v) const;
485
486 private:
487 Output_segment(const Output_segment&);
488 Output_segment& operator=(const Output_segment&);
489
490 typedef std::list<Output_data*> Output_data_list;
491
492 // Set the section addresses in an Output_data_list.
493 uint64_t
494 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff);
495
496 // Return the number of Output_sections in an Output_data_list.
497 unsigned int
498 output_section_count_list(const Output_data_list*) const;
499
500 // Write the section headers in the list into V.
501 template<int size, bool big_endian>
502 unsigned char*
503 write_section_headers_list(const Stringpool*, const Output_data_list*,
504 unsigned char* v) const;
505
506 // The list of output data with contents attached to this segment.
507 Output_data_list output_data_;
508 // The list of output data without contents attached to this segment.
509 Output_data_list output_bss_;
510 // The segment virtual address.
511 uint64_t vaddr_;
512 // The segment physical address.
513 uint64_t paddr_;
514 // The size of the segment in memory.
515 uint64_t memsz_;
516 // The segment alignment.
517 uint64_t align_;
518 // The offset of the segment data within the file.
519 off_t offset_;
520 // The size of the segment data in the file.
521 off_t filesz_;
522 // The segment type;
523 elfcpp::Elf_Word type_;
524 // The segment flags.
525 elfcpp::Elf_Word flags_;
526 };
527
528 // This class represents the output file.
529
530 class Output_file
531 {
532 public:
533 Output_file(const General_options& options);
534
535 // Open the output file. FILE_SIZE is the final size of the file.
536 void
537 open(off_t file_size);
538
539 // Close the output file and make sure there are no error.
540 void
541 close();
542
543 // We currently always use mmap which makes the view handling quite
544 // simple. In the future we may support other approaches.
545
546 // Write data to the output file.
547 void
548 write(off_t offset, const void* data, off_t len)
549 { memcpy(this->base_ + offset, data, len); }
550
551 // Get a buffer to use to write to the file, given the offset into
552 // the file and the size.
553 unsigned char*
554 get_output_view(off_t start, off_t size)
555 {
556 assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
557 return this->base_ + start;
558 }
559
560 // VIEW must have been returned by get_output_view. Write the
561 // buffer to the file, passing in the offset and the size.
562 void
563 write_output_view(off_t, off_t, unsigned char*)
564 { }
565
566 private:
567 // General options.
568 const General_options& options_;
569 // File name.
570 const char* name_;
571 // File descriptor.
572 int o_;
573 // File size.
574 off_t file_size_;
575 // Base of file mapped into memory.
576 unsigned char* base_;
577 };
578
579 } // End namespace gold.
580
581 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.043213 seconds and 5 git commands to generate.