* ld-sh/tlsbin-1.d: Update.
[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
61ba1cf9 15class General_options;
a2fb1b05
ILT
16class Object;
17class Output_file;
18
54dc6425
ILT
19template<int size, bool big_endian>
20class Sized_target;
21
22// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
23
24class Output_data
25{
26 public:
75f65a3e 27 explicit Output_data(off_t data_size = 0)
61ba1cf9 28 : address_(0), data_size_(data_size), offset_(-1)
a2fb1b05
ILT
29 { }
30
31 virtual
32 ~Output_data();
33
75f65a3e
ILT
34 // Return the address.
35 uint64_t
36 address() const
37 { return this->address_; }
38
39 // Return the size of the data.
a2fb1b05 40 off_t
75f65a3e
ILT
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); }
a2fb1b05 78
75f65a3e
ILT
79 protected:
80 // Functions that child classes may or in some cases must implement.
81
82 // Write the data to the output file.
a2fb1b05 83 virtual void
75f65a3e
ILT
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; }
a2fb1b05 94
54dc6425 95 // Return whether this is an Output_section of the specified type.
75f65a3e 96 // This only needs to be implement by Output_section.
54dc6425 97 virtual bool
75f65a3e 98 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
99 { return false; }
100
75f65a3e
ILT
101 // Return whether this is an Output_section with the specific flag
102 // set. This only needs to be implemented by Output_section.
54dc6425 103 virtual bool
75f65a3e 104 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
105 { return false; }
106
75f65a3e
ILT
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
a2fb1b05
ILT
115 // Set the size of the data.
116 void
75f65a3e
ILT
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);
a2fb1b05
ILT
123
124 private:
125 Output_data(const Output_data&);
126 Output_data& operator=(const Output_data&);
127
75f65a3e
ILT
128 // Memory address in file (not always meaningful).
129 uint64_t address_;
a2fb1b05 130 // Size of data in file.
75f65a3e
ILT
131 off_t data_size_;
132 // Offset within file.
133 off_t offset_;
a2fb1b05
ILT
134};
135
54dc6425 136// A simple case of Output_data in which we have constant data to
a2fb1b05
ILT
137// output.
138
139class Output_data_const : public Output_data
140{
141 public:
75f65a3e
ILT
142 Output_data_const(const std::string& data, uint64_t addralign)
143 : Output_data(data.size()), data_(data), addralign_(addralign)
a2fb1b05
ILT
144 { }
145
75f65a3e
ILT
146 Output_data_const(const char* p, off_t len, uint64_t addralign)
147 : Output_data(len), data_(p, len), addralign_(addralign)
a2fb1b05
ILT
148 { }
149
54dc6425 150 // Write the data to the file.
a2fb1b05 151 void
75f65a3e
ILT
152 do_write(Output_file* output);
153
154 // Return the required alignment.
155 uint64_t
156 do_addralign() const
157 { return this->addralign_; }
a2fb1b05
ILT
158
159 private:
160 std::string data_;
75f65a3e 161 uint64_t addralign_;
a2fb1b05
ILT
162};
163
54dc6425
ILT
164// Output the section headers.
165
166class Output_section_headers : public Output_data
167{
168 public:
75f65a3e 169 Output_section_headers(int size,
61ba1cf9 170 bool big_endian,
75f65a3e 171 const Layout::Segment_list&,
61ba1cf9
ILT
172 const Layout::Section_list&,
173 const Stringpool*);
54dc6425
ILT
174
175 // Write the data to the file.
176 void
75f65a3e
ILT
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_); }
54dc6425
ILT
183
184 private:
61ba1cf9
ILT
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
75f65a3e 190 int size_;
61ba1cf9 191 bool big_endian_;
54dc6425
ILT
192 const Layout::Segment_list& segment_list_;
193 const Layout::Section_list& section_list_;
61ba1cf9 194 const Stringpool* secnamepool_;
54dc6425
ILT
195};
196
197// Output the segment headers.
198
199class Output_segment_headers : public Output_data
200{
201 public:
61ba1cf9
ILT
202 Output_segment_headers(int size, bool big_endian,
203 const Layout::Segment_list& segment_list);
54dc6425
ILT
204
205 // Write the data to the file.
206 void
75f65a3e
ILT
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_); }
54dc6425
ILT
213
214 private:
61ba1cf9
ILT
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
75f65a3e 220 int size_;
61ba1cf9 221 bool big_endian_;
54dc6425
ILT
222 const Layout::Segment_list& segment_list_;
223};
224
225// Output the ELF file header.
226
227class Output_file_header : public Output_data
228{
229 public:
75f65a3e 230 Output_file_header(int size,
61ba1cf9 231 bool big_endian,
75f65a3e 232 const General_options&,
54dc6425
ILT
233 const Target*,
234 const Symbol_table*,
75f65a3e
ILT
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);
54dc6425
ILT
241
242 // Write the data to the file.
243 void
75f65a3e
ILT
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); }
54dc6425
ILT
256
257 private:
61ba1cf9
ILT
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
75f65a3e 263 int size_;
61ba1cf9 264 bool big_endian_;
54dc6425
ILT
265 const General_options& options_;
266 const Target* target_;
267 const Symbol_table* symtab_;
61ba1cf9 268 const Output_segment_headers* segment_header_;
54dc6425
ILT
269 const Output_section_headers* section_header_;
270 const Output_section* shstrtab_;
271};
272
a2fb1b05
ILT
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
54dc6425 276class Output_section : public Output_data
a2fb1b05
ILT
277{
278 public:
279 // Create an output section, giving the name, type, and flags.
61ba1cf9
ILT
280 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
281 unsigned int shndx);
54dc6425 282 virtual ~Output_section();
a2fb1b05
ILT
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
75f65a3e
ILT
306 // Return the address alignment.
307 uint64_t
308 addralign() const
309 { return this->addralign_; }
310
61ba1cf9
ILT
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
54dc6425
ILT
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
75f65a3e 340 do_write(Output_file*)
54dc6425
ILT
341 { }
342
75f65a3e
ILT
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
54dc6425
ILT
353 // Return whether this is a section of the specified type.
354 bool
75f65a3e 355 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
356 { return this->type_ == type; }
357
358 // Return whether the specified section flag is set.
359 bool
75f65a3e 360 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
361 { return (this->flags_ & flag) != 0; }
362
61ba1cf9
ILT
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
a2fb1b05
ILT
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_;
75f65a3e 373 // The section address is in the parent class.
a2fb1b05
ILT
374 // The section alignment.
375 uint64_t addralign_;
376 // The section entry size.
377 uint64_t entsize_;
75f65a3e 378 // The file offset is in the parent class.
a2fb1b05
ILT
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_;
61ba1cf9
ILT
387 // The section index.
388 unsigned int shndx_;
a2fb1b05
ILT
389};
390
54dc6425
ILT
391// A special Output_section which represents the symbol table
392// (SHT_SYMTAB).
393
394class Output_section_symtab : public Output_section
395{
396 public:
61ba1cf9 397 Output_section_symtab(const char* name, off_t size, unsigned int shndx);
75f65a3e
ILT
398};
399
400// A special Output_section which holds a string table.
401
402class Output_section_strtab : public Output_section
403{
404 public:
61ba1cf9
ILT
405 Output_section_strtab(const char* name, Stringpool* contents,
406 unsigned int shndx);
75f65a3e
ILT
407
408 // Write out the data.
409 void
410 do_write(Output_file*);
411
412 private:
413 Stringpool* contents_;
54dc6425
ILT
414};
415
a2fb1b05
ILT
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
420class 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
75f65a3e
ILT
446 // Return the maximum alignment of the Output_data.
447 uint64_t
448 max_data_align() const;
449
a2fb1b05
ILT
450 // Add an Output_section to this segment.
451 void
75f65a3e
ILT
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;
a2fb1b05 475
61ba1cf9
ILT
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*
5482377d
ILT
484 write_section_headers(const Stringpool*,
485 unsigned char* v ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 486
a2fb1b05
ILT
487 private:
488 Output_segment(const Output_segment&);
489 Output_segment& operator=(const Output_segment&);
490
54dc6425 491 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 492
75f65a3e
ILT
493 // Set the section addresses in an Output_data_list.
494 uint64_t
495 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff);
496
497 // Return the number of Output_sections in an Output_data_list.
498 unsigned int
499 output_section_count_list(const Output_data_list*) const;
500
61ba1cf9
ILT
501 // Write the section headers in the list into V.
502 template<int size, bool big_endian>
503 unsigned char*
504 write_section_headers_list(const Stringpool*, const Output_data_list*,
5482377d 505 unsigned char* v ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 506
75f65a3e 507 // The list of output data with contents attached to this segment.
54dc6425 508 Output_data_list output_data_;
75f65a3e
ILT
509 // The list of output data without contents attached to this segment.
510 Output_data_list output_bss_;
a2fb1b05
ILT
511 // The segment virtual address.
512 uint64_t vaddr_;
513 // The segment physical address.
514 uint64_t paddr_;
515 // The size of the segment in memory.
516 uint64_t memsz_;
517 // The segment alignment.
518 uint64_t align_;
519 // The offset of the segment data within the file.
520 off_t offset_;
521 // The size of the segment data in the file.
522 off_t filesz_;
523 // The segment type;
524 elfcpp::Elf_Word type_;
525 // The segment flags.
526 elfcpp::Elf_Word flags_;
527};
528
61ba1cf9 529// This class represents the output file.
a2fb1b05
ILT
530
531class Output_file
532{
533 public:
61ba1cf9
ILT
534 Output_file(const General_options& options);
535
536 // Open the output file. FILE_SIZE is the final size of the file.
537 void
538 open(off_t file_size);
539
540 // Close the output file and make sure there are no error.
541 void
542 close();
543
544 // We currently always use mmap which makes the view handling quite
545 // simple. In the future we may support other approaches.
a2fb1b05
ILT
546
547 // Write data to the output file.
548 void
61ba1cf9
ILT
549 write(off_t offset, const void* data, off_t len)
550 { memcpy(this->base_ + offset, data, len); }
551
552 // Get a buffer to use to write to the file, given the offset into
553 // the file and the size.
554 unsigned char*
555 get_output_view(off_t start, off_t size)
556 {
557 assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
558 return this->base_ + start;
559 }
560
561 // VIEW must have been returned by get_output_view. Write the
562 // buffer to the file, passing in the offset and the size.
563 void
564 write_output_view(off_t, off_t, unsigned char*)
565 { }
566
567 private:
568 // General options.
569 const General_options& options_;
570 // File name.
571 const char* name_;
572 // File descriptor.
573 int o_;
574 // File size.
575 off_t file_size_;
576 // Base of file mapped into memory.
577 unsigned char* base_;
a2fb1b05
ILT
578};
579
580} // End namespace gold.
581
582#endif // !defined(GOLD_OUTPUT_H)
This page took 0.074507 seconds and 4 git commands to generate.