Rework swapping code.
[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 7#include <list>
ead1e424 8#include <vector>
a2fb1b05
ILT
9
10#include "elfcpp.h"
54dc6425 11#include "layout.h"
a2fb1b05
ILT
12
13namespace gold
14{
15
61ba1cf9 16class General_options;
a2fb1b05
ILT
17class Object;
18class Output_file;
19
54dc6425
ILT
20template<int size, bool big_endian>
21class Sized_target;
22
23// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
24
25class Output_data
26{
27 public:
75f65a3e 28 explicit Output_data(off_t data_size = 0)
61ba1cf9 29 : address_(0), data_size_(data_size), offset_(-1)
a2fb1b05
ILT
30 { }
31
32 virtual
33 ~Output_data();
34
ead1e424
ILT
35 // Return the address. This is only valid after Layout::finalize is
36 // finished.
75f65a3e
ILT
37 uint64_t
38 address() const
39 { return this->address_; }
40
ead1e424
ILT
41 // Return the size of the data. This must be valid after
42 // Layout::finalize calls set_address, but need not be valid before
43 // then.
a2fb1b05 44 off_t
75f65a3e
ILT
45 data_size() const
46 { return this->data_size_; }
47
ead1e424
ILT
48 // Return the file offset. This is only valid after
49 // Layout::finalize is finished.
75f65a3e
ILT
50 off_t
51 offset() const
52 { return this->offset_; }
53
54 // Return the required alignment.
55 uint64_t
56 addralign() const
57 { return this->do_addralign(); }
58
59 // Return whether this is an Output_section.
60 bool
61 is_section() const
62 { return this->do_is_section(); }
63
64 // Return whether this is an Output_section of the specified type.
65 bool
66 is_section_type(elfcpp::Elf_Word stt) const
67 { return this->do_is_section_type(stt); }
68
69 // Return whether this is an Output_section with the specified flag
70 // set.
71 bool
72 is_section_flag_set(elfcpp::Elf_Xword shf) const
73 { return this->do_is_section_flag_set(shf); }
74
ead1e424
ILT
75 // Return the output section index, if there is an output section.
76 unsigned int
77 out_shndx() const
78 { return this->do_out_shndx(); }
79
80 // Set the output section index, if this is an output section.
81 void
82 set_out_shndx(unsigned int shndx)
83 { this->do_set_out_shndx(shndx); }
84
85 // Set the address and file offset of this data. This is called
86 // during Layout::finalize.
75f65a3e
ILT
87 void
88 set_address(uint64_t addr, off_t off);
89
ead1e424
ILT
90 // Write the data to the output file. This is called after
91 // Layout::finalize is complete.
75f65a3e
ILT
92 void
93 write(Output_file* file)
94 { this->do_write(file); }
a2fb1b05 95
75f65a3e
ILT
96 protected:
97 // Functions that child classes may or in some cases must implement.
98
99 // Write the data to the output file.
a2fb1b05 100 virtual void
75f65a3e
ILT
101 do_write(Output_file*) = 0;
102
103 // Return the required alignment.
104 virtual uint64_t
105 do_addralign() const = 0;
106
107 // Return whether this is an Output_section.
108 virtual bool
109 do_is_section() const
110 { return false; }
a2fb1b05 111
54dc6425 112 // Return whether this is an Output_section of the specified type.
75f65a3e 113 // This only needs to be implement by Output_section.
54dc6425 114 virtual bool
75f65a3e 115 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
116 { return false; }
117
75f65a3e
ILT
118 // Return whether this is an Output_section with the specific flag
119 // set. This only needs to be implemented by Output_section.
54dc6425 120 virtual bool
75f65a3e 121 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
122 { return false; }
123
ead1e424
ILT
124 // Return the output section index, if there is an output section.
125 virtual unsigned int
126 do_out_shndx() const
127 { abort(); }
128
129 // Set the output section index, if this is an output section.
130 virtual void
131 do_set_out_shndx(unsigned int)
132 { abort(); }
133
75f65a3e
ILT
134 // Set the address and file offset of the data. This only needs to
135 // be implemented if the child needs to know.
136 virtual void
137 do_set_address(uint64_t, off_t)
138 { }
139
140 // Functions that child classes may call.
141
a2fb1b05
ILT
142 // Set the size of the data.
143 void
75f65a3e
ILT
144 set_data_size(off_t data_size)
145 { this->data_size_ = data_size; }
146
147 // Return default alignment for a size--32 or 64.
148 static uint64_t
149 default_alignment(int size);
a2fb1b05
ILT
150
151 private:
152 Output_data(const Output_data&);
153 Output_data& operator=(const Output_data&);
154
75f65a3e
ILT
155 // Memory address in file (not always meaningful).
156 uint64_t address_;
a2fb1b05 157 // Size of data in file.
75f65a3e
ILT
158 off_t data_size_;
159 // Offset within file.
160 off_t offset_;
a2fb1b05
ILT
161};
162
54dc6425 163// A simple case of Output_data in which we have constant data to
a2fb1b05
ILT
164// output.
165
166class Output_data_const : public Output_data
167{
168 public:
75f65a3e
ILT
169 Output_data_const(const std::string& data, uint64_t addralign)
170 : Output_data(data.size()), data_(data), addralign_(addralign)
a2fb1b05
ILT
171 { }
172
75f65a3e
ILT
173 Output_data_const(const char* p, off_t len, uint64_t addralign)
174 : Output_data(len), data_(p, len), addralign_(addralign)
a2fb1b05
ILT
175 { }
176
54dc6425 177 // Write the data to the file.
a2fb1b05 178 void
75f65a3e
ILT
179 do_write(Output_file* output);
180
181 // Return the required alignment.
182 uint64_t
183 do_addralign() const
184 { return this->addralign_; }
a2fb1b05
ILT
185
186 private:
187 std::string data_;
75f65a3e 188 uint64_t addralign_;
a2fb1b05
ILT
189};
190
54dc6425
ILT
191// Output the section headers.
192
193class Output_section_headers : public Output_data
194{
195 public:
75f65a3e 196 Output_section_headers(int size,
61ba1cf9 197 bool big_endian,
75f65a3e 198 const Layout::Segment_list&,
61ba1cf9
ILT
199 const Layout::Section_list&,
200 const Stringpool*);
54dc6425
ILT
201
202 // Write the data to the file.
203 void
75f65a3e
ILT
204 do_write(Output_file*);
205
206 // Return the required alignment.
207 uint64_t
208 do_addralign() const
209 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
210
211 private:
61ba1cf9
ILT
212 // Write the data to the file with the right size and endianness.
213 template<int size, bool big_endian>
214 void
215 do_sized_write(Output_file*);
216
75f65a3e 217 int size_;
61ba1cf9 218 bool big_endian_;
54dc6425
ILT
219 const Layout::Segment_list& segment_list_;
220 const Layout::Section_list& section_list_;
61ba1cf9 221 const Stringpool* secnamepool_;
54dc6425
ILT
222};
223
224// Output the segment headers.
225
226class Output_segment_headers : public Output_data
227{
228 public:
61ba1cf9
ILT
229 Output_segment_headers(int size, bool big_endian,
230 const Layout::Segment_list& segment_list);
54dc6425
ILT
231
232 // Write the data to the file.
233 void
75f65a3e
ILT
234 do_write(Output_file*);
235
236 // Return the required alignment.
237 uint64_t
238 do_addralign() const
239 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
240
241 private:
61ba1cf9
ILT
242 // Write the data to the file with the right size and endianness.
243 template<int size, bool big_endian>
244 void
245 do_sized_write(Output_file*);
246
75f65a3e 247 int size_;
61ba1cf9 248 bool big_endian_;
54dc6425
ILT
249 const Layout::Segment_list& segment_list_;
250};
251
252// Output the ELF file header.
253
254class Output_file_header : public Output_data
255{
256 public:
75f65a3e 257 Output_file_header(int size,
61ba1cf9 258 bool big_endian,
75f65a3e 259 const General_options&,
54dc6425
ILT
260 const Target*,
261 const Symbol_table*,
75f65a3e
ILT
262 const Output_segment_headers*);
263
264 // Add information about the section headers. We lay out the ELF
265 // file header before we create the section headers.
266 void set_section_info(const Output_section_headers*,
267 const Output_section* shstrtab);
54dc6425
ILT
268
269 // Write the data to the file.
270 void
75f65a3e
ILT
271 do_write(Output_file*);
272
273 // Return the required alignment.
274 uint64_t
275 do_addralign() const
276 { return Output_data::default_alignment(this->size_); }
277
278 // Set the address and offset--we only implement this for error
279 // checking.
280 void
281 do_set_address(uint64_t, off_t off) const
282 { assert(off == 0); }
54dc6425
ILT
283
284 private:
61ba1cf9
ILT
285 // Write the data to the file with the right size and endianness.
286 template<int size, bool big_endian>
287 void
288 do_sized_write(Output_file*);
289
75f65a3e 290 int size_;
61ba1cf9 291 bool big_endian_;
54dc6425
ILT
292 const General_options& options_;
293 const Target* target_;
294 const Symbol_table* symtab_;
61ba1cf9 295 const Output_segment_headers* segment_header_;
54dc6425
ILT
296 const Output_section_headers* section_header_;
297 const Output_section* shstrtab_;
298};
299
ead1e424
ILT
300// Output sections are mainly comprised of input sections. However,
301// there are cases where we have data to write out which is not in an
302// input section. Output_section_data is used in such cases. This is
303// an abstract base class.
304
305class Output_section_data : public Output_data
306{
307 public:
308 Output_section_data(off_t data_size, uint64_t addralign)
309 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
310 { }
311
312 Output_section_data(uint64_t addralign)
313 : Output_data(0), output_section_(NULL), addralign_(addralign)
314 { }
315
316 // Record the output section.
317 void
318 set_output_section(Output_section* os)
319 {
320 assert(this->output_section_ == NULL);
321 this->output_section_ = os;
322 }
323
324 protected:
325 // The child class must implement do_write.
326
327 // Return the required alignment.
328 uint64_t
329 do_addralign() const
330 { return this->addralign_; }
331
332 // Return the section index of the output section.
333 unsigned int
334 do_out_shndx() const;
335
336 private:
337 // The output section for this section.
338 const Output_section* output_section_;
339 // The required alignment.
340 uint64_t addralign_;
341};
342
343// Output_section_common is used to handle the common symbols. This
344// is quite simple.
345
346class Output_section_common : public Output_section_data
347{
348 public:
349 Output_section_common(uint64_t addralign)
350 : Output_section_data(addralign)
351 { }
352
353 // Set the size.
354 void
355 set_common_size(off_t common_size)
356 { this->set_data_size(common_size); }
357
358 // Write out the data--there is nothing to do, as common symbols are
359 // always zero and are stored in the BSS.
360 void
361 do_write(Output_file*)
362 { }
363};
364
365// Output_section_got is used to manage a GOT. Each entry in the GOT
366// is for one symbol--either a global symbol or a local symbol in an
367// object. The target specific code adds entries to the GOT as
368// needed. The GOT code is then responsible for writing out the data
369// and for generating relocs as required.
370
371template<int size, bool big_endian>
372class Output_section_got : public Output_section_data
373{
374 public:
375 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
376
377 Output_section_got()
378 : Output_section_data(Output_data::default_alignment(size)),
379 entries_()
380 { }
381
382 // Add an entry for a global symbol to the GOT. This returns the
383 // offset of the new entry from the start of the GOT.
384 unsigned int
385 add_global(Symbol* gsym)
386 {
387 this->entries_.push_back(Got_entry(gsym));
388 this->set_got_size();
389 return this->last_got_offset();
390 }
391
392 // Add an entry for a local symbol to the GOT. This returns the
393 // offset of the new entry from the start of the GOT.
394 unsigned int
395 add_local(Object* object, unsigned int sym_index)
396 {
397 this->entries_.push_back(Got_entry(object, sym_index));
398 this->set_got_size();
399 return this->last_got_offset();
400 }
401
402 // Add a constant to the GOT. This returns the offset of the new
403 // entry from the start of the GOT.
404 unsigned int
405 add_constant(Valtype constant)
406 {
407 this->entries_.push_back(Got_entry(constant));
408 this->set_got_size();
409 return this->last_got_offset();
410 }
411
412 // Write out the GOT table.
413 void
414 do_write(Output_file*);
415
416 private:
417 // This POD class holds a single GOT entry.
418 class Got_entry
419 {
420 public:
421 // Create a zero entry.
422 Got_entry()
423 : local_sym_index_(CONSTANT_CODE)
424 { this->u_.constant = 0; }
425
426 // Create a global symbol entry.
427 Got_entry(Symbol* gsym)
428 : local_sym_index_(GSYM_CODE)
429 { this->u_.gsym = gsym; }
430
431 // Create a local symbol entry.
432 Got_entry(Object* object, unsigned int local_sym_index)
433 : local_sym_index_(local_sym_index)
434 {
435 assert(local_sym_index != GSYM_CODE
436 && local_sym_index != CONSTANT_CODE);
437 this->u_.object = object;
438 }
439
440 // Create a constant entry. The constant is a host value--it will
441 // be swapped, if necessary, when it is written out.
442 Got_entry(Valtype constant)
443 : local_sym_index_(CONSTANT_CODE)
444 { this->u_.constant = constant; }
445
446 // Write the GOT entry to an output view.
447 void
448 write(unsigned char* pov) const;
449
450 private:
451 enum
452 {
453 GSYM_CODE = -1U,
454 CONSTANT_CODE = -2U
455 };
456
457 union
458 {
459 // For a local symbol, the object.
460 Object* object;
461 // For a global symbol, the symbol.
462 Symbol* gsym;
463 // For a constant, the constant.
464 Valtype constant;
465 } u_;
466 // For a local symbol, the local symbol index. This is -1U for a
467 // global symbol, or -2U for a constant.
468 unsigned int local_sym_index_;
469 };
470
471 typedef std::vector<Got_entry> Got_entries;
472
473 // Return the offset into the GOT of GOT entry I.
474 unsigned int
475 got_offset(unsigned int i) const
476 { return i * (size / 8); }
477
478 // Return the offset into the GOT of the last entry added.
479 unsigned int
480 last_got_offset() const
481 { return this->got_offset(this->entries_.size() - 1); }
482
483 // Set the size of the section.
484 void
485 set_got_size()
486 { this->set_data_size(this->got_offset(this->entries_.size())); }
487
488 // The list of GOT entries.
489 Got_entries entries_;
490};
491
a2fb1b05
ILT
492// An output section. We don't expect to have too many output
493// sections, so we don't bother to do a template on the size.
494
54dc6425 495class Output_section : public Output_data
a2fb1b05
ILT
496{
497 public:
498 // Create an output section, giving the name, type, and flags.
61ba1cf9 499 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
ead1e424 500 bool may_add_data);
54dc6425 501 virtual ~Output_section();
a2fb1b05 502
ead1e424
ILT
503 // Add a new input section SHNDX, named NAME, with header SHDR, from
504 // object OBJECT. Return the offset within the output section.
a2fb1b05
ILT
505 template<int size, bool big_endian>
506 off_t
ead1e424 507 add_input_section(Object* object, unsigned int shndx, const char *name,
a2fb1b05
ILT
508 const elfcpp::Shdr<size, big_endian>& shdr);
509
ead1e424
ILT
510 // Add generated data ODATA to this output section.
511 virtual void
512 add_output_section_data(Output_section_data* posd);
513
a2fb1b05
ILT
514 // Return the section name.
515 const char*
516 name() const
517 { return this->name_; }
518
519 // Return the section type.
520 elfcpp::Elf_Word
521 type() const
522 { return this->type_; }
523
524 // Return the section flags.
525 elfcpp::Elf_Xword
526 flags() const
527 { return this->flags_; }
528
ead1e424 529 // Return the section index in the output file.
61ba1cf9 530 unsigned int
ead1e424
ILT
531 do_out_shndx() const
532 { return this->out_shndx_; }
533
534 // Set the output section index.
535 void
536 do_set_out_shndx(unsigned int shndx)
537 { this->out_shndx_ = shndx; }
61ba1cf9
ILT
538
539 // Set the entsize field.
540 void
541 set_entsize(uint64_t v)
542 { this->entsize_ = v; }
543
544 // Set the link field.
545 void
546 set_link(unsigned int v)
547 { this->link_ = v; }
548
549 // Set the info field.
550 void
551 set_info(unsigned int v)
552 { this->info_ = v; }
553
554 // Set the addralign field.
555 void
556 set_addralign(uint64_t v)
557 { this->addralign_ = v; }
558
ead1e424
ILT
559 // Set the address of the Output_section. For a typical
560 // Output_section, there is nothing to do, but if there are any
561 // Output_section_data objects we need to set the final addresses
562 // here.
563 void
564 do_set_address(uint64_t, off_t);
565
54dc6425 566 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
567 // does nothing: the data is written out by calling Object::Relocate
568 // on each input object. But if there are any Output_section_data
569 // objects we do need to write them out here.
54dc6425 570 virtual void
ead1e424 571 do_write(Output_file*);
54dc6425 572
75f65a3e
ILT
573 // Return the address alignment--function required by parent class.
574 uint64_t
575 do_addralign() const
576 { return this->addralign_; }
577
578 // Return whether this is an Output_section.
579 bool
580 do_is_section() const
581 { return true; }
582
54dc6425
ILT
583 // Return whether this is a section of the specified type.
584 bool
75f65a3e 585 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
586 { return this->type_ == type; }
587
588 // Return whether the specified section flag is set.
589 bool
75f65a3e 590 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
591 { return (this->flags_ & flag) != 0; }
592
61ba1cf9
ILT
593 // Write the section header into *OPHDR.
594 template<int size, bool big_endian>
595 void
596 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
597
a2fb1b05 598 private:
ead1e424
ILT
599 // In some cases we need to keep a list of the input sections
600 // associated with this output section. We only need the list if we
601 // might have to change the offsets of the input section within the
602 // output section after we add the input section. The ordinary
603 // input sections will be written out when we process the object
604 // file, and as such we don't need to track them here. We do need
605 // to track Output_section_data objects here. We store instances of
606 // this structure in a std::vector, so it must be a POD. There can
607 // be many instances of this structure, so we use a union to save
608 // some space.
609 class Input_section
610 {
611 public:
612 Input_section()
613 : shndx_(0), p2align_(0), data_size_(0)
614 { this->u_.object = NULL; }
615
616 Input_section(Object* object, unsigned int shndx, off_t data_size,
617 uint64_t addralign)
618 : shndx_(shndx),
619 p2align_(ffsll(static_cast<long long>(addralign))),
620 data_size_(data_size)
621 {
622 assert(shndx != -1U);
623 this->u_.object = object;
624 }
625
626 Input_section(Output_section_data* posd)
627 : shndx_(-1U),
628 p2align_(ffsll(static_cast<long long>(posd->addralign()))),
629 data_size_(0)
630 { this->u_.posd = posd; }
631
632 // The required alignment.
633 uint64_t
634 addralign() const
635 { return static_cast<uint64_t>(1) << this->p2align_; }
636
637 // Return the required size.
638 off_t
639 data_size() const;
640
641 // Set the address and file offset. This is called during
642 // Layout::finalize. SECOFF is the file offset of the enclosing
643 // section.
644 void
645 set_address(uint64_t addr, off_t off, off_t secoff);
646
647 // Write out the data. This does nothing for an input section.
648 void
649 write(Output_file*);
650
651 private:
652 // Whether this is an input section.
653 bool
654 is_input_section() const
655 { return this->shndx_ != -1U; }
656
657 // For an ordinary input section, this is the section index in
658 // the input file. For an Output_section_data, this is -1U.
659 unsigned int shndx_;
660 // The required alignment, stored as a power of 2.
661 unsigned int p2align_;
662 // For an ordinary input section, the section size.
663 off_t data_size_;
664 union
665 {
666 // If shndx_ != -1U, this points to the object which holds the
667 // input section.
668 Object* object;
669 // If shndx_ == -1U, this is the data to write out.
670 Output_section_data* posd;
671 } u_;
672 };
673
674 typedef std::vector<Input_section> Input_section_list;
675
a2fb1b05
ILT
676 // Most of these fields are only valid after layout.
677
678 // The name of the section. This will point into a Stringpool.
679 const char* name_;
75f65a3e 680 // The section address is in the parent class.
a2fb1b05
ILT
681 // The section alignment.
682 uint64_t addralign_;
683 // The section entry size.
684 uint64_t entsize_;
75f65a3e 685 // The file offset is in the parent class.
a2fb1b05
ILT
686 // The section link field.
687 unsigned int link_;
688 // The section info field.
689 unsigned int info_;
690 // The section type.
691 elfcpp::Elf_Word type_;
692 // The section flags.
693 elfcpp::Elf_Xword flags_;
61ba1cf9 694 // The section index.
ead1e424
ILT
695 unsigned int out_shndx_;
696 // The input sections. This will be empty in cases where we don't
697 // need to keep track of them.
698 Input_section_list input_sections_;
699 // The offset of the first entry in input_sections_.
700 off_t first_input_offset_;
701 // Whether we permit adding data.
702 bool may_add_data_;
a2fb1b05
ILT
703};
704
54dc6425 705// A special Output_section which represents the symbol table
ead1e424
ILT
706// (SHT_SYMTAB). The actual data is written out by
707// Symbol_table::write_globals.
54dc6425
ILT
708
709class Output_section_symtab : public Output_section
710{
711 public:
ead1e424
ILT
712 Output_section_symtab(const char* name, off_t size);
713
714 // The data is written out by Symbol_table::write_globals. We don't
715 // do anything here.
716 void
717 do_write(Output_file*)
718 { }
719
720 // We don't expect to see any input sections or data here.
721 void
722 add_output_section_data(Output_section_data*)
723 { abort(); }
75f65a3e
ILT
724};
725
726// A special Output_section which holds a string table.
727
728class Output_section_strtab : public Output_section
729{
730 public:
ead1e424 731 Output_section_strtab(const char* name, Stringpool* contents);
75f65a3e
ILT
732
733 // Write out the data.
734 void
735 do_write(Output_file*);
736
ead1e424
ILT
737 // We don't expect to see any input sections or data here.
738 void
739 add_output_section_data(Output_section_data*)
740 { abort(); }
741
75f65a3e
ILT
742 private:
743 Stringpool* contents_;
54dc6425
ILT
744};
745
a2fb1b05
ILT
746// An output segment. PT_LOAD segments are built from collections of
747// output sections. Other segments typically point within PT_LOAD
748// segments, and are built directly as needed.
749
750class Output_segment
751{
752 public:
753 // Create an output segment, specifying the type and flags.
754 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
755
756 // Return the virtual address.
757 uint64_t
758 vaddr() const
759 { return this->vaddr_; }
760
761 // Return the physical address.
762 uint64_t
763 paddr() const
764 { return this->paddr_; }
765
766 // Return the segment type.
767 elfcpp::Elf_Word
768 type() const
769 { return this->type_; }
770
771 // Return the segment flags.
772 elfcpp::Elf_Word
773 flags() const
774 { return this->flags_; }
775
92e059d8
ILT
776 // Return the memory size.
777 uint64_t
778 memsz() const
779 { return this->memsz_; }
780
ead1e424
ILT
781 // Return the file size.
782 off_t
783 filesz() const
784 { return this->filesz_; }
785
75f65a3e
ILT
786 // Return the maximum alignment of the Output_data.
787 uint64_t
ead1e424 788 addralign();
75f65a3e 789
a2fb1b05
ILT
790 // Add an Output_section to this segment.
791 void
75f65a3e
ILT
792 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags);
793
794 // Add an Output_data (which is not an Output_section) to the start
795 // of this segment.
796 void
797 add_initial_output_data(Output_data*);
798
799 // Set the address of the segment to ADDR and the offset to *POFF
800 // (aligned if necessary), and set the addresses and offsets of all
ead1e424
ILT
801 // contained output sections accordingly. Set the section indexes
802 // of all contained output sections starting with *PSHNDX. Return
803 // the address of the immediately following segment. Update *POFF
804 // and *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 805 uint64_t
ead1e424 806 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
75f65a3e
ILT
807
808 // Set the offset of this segment based on the section. This should
809 // only be called for a non-PT_LOAD segment.
810 void
811 set_offset();
812
813 // Return the number of output sections.
814 unsigned int
815 output_section_count() const;
a2fb1b05 816
61ba1cf9
ILT
817 // Write the segment header into *OPHDR.
818 template<int size, bool big_endian>
819 void
ead1e424 820 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
821
822 // Write the section headers of associated sections into V.
823 template<int size, bool big_endian>
824 unsigned char*
5482377d 825 write_section_headers(const Stringpool*,
ead1e424
ILT
826 unsigned char* v,
827 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 828
a2fb1b05
ILT
829 private:
830 Output_segment(const Output_segment&);
831 Output_segment& operator=(const Output_segment&);
832
54dc6425 833 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 834
ead1e424
ILT
835 // Find the maximum alignment in an Output_data_list.
836 static uint64_t
837 maximum_alignment(const Output_data_list*);
838
75f65a3e
ILT
839 // Set the section addresses in an Output_data_list.
840 uint64_t
ead1e424
ILT
841 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
842 unsigned int* pshndx);
75f65a3e
ILT
843
844 // Return the number of Output_sections in an Output_data_list.
845 unsigned int
846 output_section_count_list(const Output_data_list*) const;
847
61ba1cf9
ILT
848 // Write the section headers in the list into V.
849 template<int size, bool big_endian>
850 unsigned char*
851 write_section_headers_list(const Stringpool*, const Output_data_list*,
ead1e424
ILT
852 unsigned char* v,
853 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 854
75f65a3e 855 // The list of output data with contents attached to this segment.
54dc6425 856 Output_data_list output_data_;
75f65a3e
ILT
857 // The list of output data without contents attached to this segment.
858 Output_data_list output_bss_;
a2fb1b05
ILT
859 // The segment virtual address.
860 uint64_t vaddr_;
861 // The segment physical address.
862 uint64_t paddr_;
863 // The size of the segment in memory.
864 uint64_t memsz_;
865 // The segment alignment.
866 uint64_t align_;
867 // The offset of the segment data within the file.
868 off_t offset_;
869 // The size of the segment data in the file.
870 off_t filesz_;
871 // The segment type;
872 elfcpp::Elf_Word type_;
873 // The segment flags.
874 elfcpp::Elf_Word flags_;
ead1e424
ILT
875 // Whether we have set align_.
876 bool is_align_known_;
a2fb1b05
ILT
877};
878
61ba1cf9 879// This class represents the output file.
a2fb1b05
ILT
880
881class Output_file
882{
883 public:
61ba1cf9
ILT
884 Output_file(const General_options& options);
885
886 // Open the output file. FILE_SIZE is the final size of the file.
887 void
888 open(off_t file_size);
889
890 // Close the output file and make sure there are no error.
891 void
892 close();
893
894 // We currently always use mmap which makes the view handling quite
895 // simple. In the future we may support other approaches.
a2fb1b05
ILT
896
897 // Write data to the output file.
898 void
61ba1cf9
ILT
899 write(off_t offset, const void* data, off_t len)
900 { memcpy(this->base_ + offset, data, len); }
901
902 // Get a buffer to use to write to the file, given the offset into
903 // the file and the size.
904 unsigned char*
905 get_output_view(off_t start, off_t size)
906 {
907 assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
908 return this->base_ + start;
909 }
910
911 // VIEW must have been returned by get_output_view. Write the
912 // buffer to the file, passing in the offset and the size.
913 void
914 write_output_view(off_t, off_t, unsigned char*)
915 { }
916
917 private:
918 // General options.
919 const General_options& options_;
920 // File name.
921 const char* name_;
922 // File descriptor.
923 int o_;
924 // File size.
925 off_t file_size_;
926 // Base of file mapped into memory.
927 unsigned char* base_;
a2fb1b05
ILT
928};
929
930} // End namespace gold.
931
932#endif // !defined(GOLD_OUTPUT_H)
This page took 0.065132 seconds and 4 git commands to generate.