2006-11-29 Paul Brook <paul@codesourcery.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 7#include <list>
ead1e424 8#include <vector>
a2fb1b05
ILT
9
10#include "elfcpp.h"
54dc6425 11#include "layout.h"
c06b7b0b 12#include "reloc-types.h"
a2fb1b05
ILT
13
14namespace gold
15{
16
61ba1cf9 17class General_options;
a2fb1b05
ILT
18class Object;
19class Output_file;
c06b7b0b 20class Output_section;
54dc6425
ILT
21template<int size, bool big_endian>
22class Sized_target;
c06b7b0b
ILT
23template<int size, bool big_endian>
24class Sized_relobj;
54dc6425
ILT
25
26// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
27
28class Output_data
29{
30 public:
75f65a3e 31 explicit Output_data(off_t data_size = 0)
61ba1cf9 32 : address_(0), data_size_(data_size), offset_(-1)
a2fb1b05
ILT
33 { }
34
35 virtual
36 ~Output_data();
37
ead1e424
ILT
38 // Return the address. This is only valid after Layout::finalize is
39 // finished.
75f65a3e
ILT
40 uint64_t
41 address() const
42 { return this->address_; }
43
ead1e424
ILT
44 // Return the size of the data. This must be valid after
45 // Layout::finalize calls set_address, but need not be valid before
46 // then.
a2fb1b05 47 off_t
75f65a3e
ILT
48 data_size() const
49 { return this->data_size_; }
50
ead1e424
ILT
51 // Return the file offset. This is only valid after
52 // Layout::finalize is finished.
75f65a3e
ILT
53 off_t
54 offset() const
55 { return this->offset_; }
56
57 // Return the required alignment.
58 uint64_t
59 addralign() const
60 { return this->do_addralign(); }
61
62 // Return whether this is an Output_section.
63 bool
64 is_section() const
65 { return this->do_is_section(); }
66
67 // Return whether this is an Output_section of the specified type.
68 bool
69 is_section_type(elfcpp::Elf_Word stt) const
70 { return this->do_is_section_type(stt); }
71
72 // Return whether this is an Output_section with the specified flag
73 // set.
74 bool
75 is_section_flag_set(elfcpp::Elf_Xword shf) const
76 { return this->do_is_section_flag_set(shf); }
77
ead1e424
ILT
78 // Return the output section index, if there is an output section.
79 unsigned int
80 out_shndx() const
81 { return this->do_out_shndx(); }
82
83 // Set the output section index, if this is an output section.
84 void
85 set_out_shndx(unsigned int shndx)
86 { this->do_set_out_shndx(shndx); }
87
88 // Set the address and file offset of this data. This is called
89 // during Layout::finalize.
75f65a3e
ILT
90 void
91 set_address(uint64_t addr, off_t off);
92
ead1e424
ILT
93 // Write the data to the output file. This is called after
94 // Layout::finalize is complete.
75f65a3e
ILT
95 void
96 write(Output_file* file)
97 { this->do_write(file); }
a2fb1b05 98
75f65a3e
ILT
99 protected:
100 // Functions that child classes may or in some cases must implement.
101
102 // Write the data to the output file.
a2fb1b05 103 virtual void
75f65a3e
ILT
104 do_write(Output_file*) = 0;
105
106 // Return the required alignment.
107 virtual uint64_t
108 do_addralign() const = 0;
109
110 // Return whether this is an Output_section.
111 virtual bool
112 do_is_section() const
113 { return false; }
a2fb1b05 114
54dc6425 115 // Return whether this is an Output_section of the specified type.
75f65a3e 116 // This only needs to be implement by Output_section.
54dc6425 117 virtual bool
75f65a3e 118 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
119 { return false; }
120
75f65a3e
ILT
121 // Return whether this is an Output_section with the specific flag
122 // set. This only needs to be implemented by Output_section.
54dc6425 123 virtual bool
75f65a3e 124 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
125 { return false; }
126
ead1e424
ILT
127 // Return the output section index, if there is an output section.
128 virtual unsigned int
129 do_out_shndx() const
130 { abort(); }
131
132 // Set the output section index, if this is an output section.
133 virtual void
134 do_set_out_shndx(unsigned int)
135 { abort(); }
136
75f65a3e
ILT
137 // Set the address and file offset of the data. This only needs to
138 // be implemented if the child needs to know.
139 virtual void
140 do_set_address(uint64_t, off_t)
141 { }
142
143 // Functions that child classes may call.
144
a2fb1b05
ILT
145 // Set the size of the data.
146 void
75f65a3e
ILT
147 set_data_size(off_t data_size)
148 { this->data_size_ = data_size; }
149
150 // Return default alignment for a size--32 or 64.
151 static uint64_t
152 default_alignment(int size);
a2fb1b05
ILT
153
154 private:
155 Output_data(const Output_data&);
156 Output_data& operator=(const Output_data&);
157
75f65a3e
ILT
158 // Memory address in file (not always meaningful).
159 uint64_t address_;
a2fb1b05 160 // Size of data in file.
75f65a3e
ILT
161 off_t data_size_;
162 // Offset within file.
163 off_t offset_;
a2fb1b05
ILT
164};
165
54dc6425
ILT
166// Output the section headers.
167
168class Output_section_headers : public Output_data
169{
170 public:
75f65a3e 171 Output_section_headers(int size,
61ba1cf9 172 bool big_endian,
75f65a3e 173 const Layout::Segment_list&,
61ba1cf9
ILT
174 const Layout::Section_list&,
175 const Stringpool*);
54dc6425
ILT
176
177 // Write the data to the file.
178 void
75f65a3e
ILT
179 do_write(Output_file*);
180
181 // Return the required alignment.
182 uint64_t
183 do_addralign() const
184 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
185
186 private:
61ba1cf9
ILT
187 // Write the data to the file with the right size and endianness.
188 template<int size, bool big_endian>
189 void
190 do_sized_write(Output_file*);
191
75f65a3e 192 int size_;
61ba1cf9 193 bool big_endian_;
54dc6425
ILT
194 const Layout::Segment_list& segment_list_;
195 const Layout::Section_list& section_list_;
61ba1cf9 196 const Stringpool* secnamepool_;
54dc6425
ILT
197};
198
199// Output the segment headers.
200
201class Output_segment_headers : public Output_data
202{
203 public:
61ba1cf9
ILT
204 Output_segment_headers(int size, bool big_endian,
205 const Layout::Segment_list& segment_list);
54dc6425
ILT
206
207 // Write the data to the file.
208 void
75f65a3e
ILT
209 do_write(Output_file*);
210
211 // Return the required alignment.
212 uint64_t
213 do_addralign() const
214 { return Output_data::default_alignment(this->size_); }
54dc6425
ILT
215
216 private:
61ba1cf9
ILT
217 // Write the data to the file with the right size and endianness.
218 template<int size, bool big_endian>
219 void
220 do_sized_write(Output_file*);
221
75f65a3e 222 int size_;
61ba1cf9 223 bool big_endian_;
54dc6425
ILT
224 const Layout::Segment_list& segment_list_;
225};
226
227// Output the ELF file header.
228
229class Output_file_header : public Output_data
230{
231 public:
75f65a3e 232 Output_file_header(int size,
61ba1cf9 233 bool big_endian,
75f65a3e 234 const General_options&,
54dc6425
ILT
235 const Target*,
236 const Symbol_table*,
75f65a3e
ILT
237 const Output_segment_headers*);
238
239 // Add information about the section headers. We lay out the ELF
240 // file header before we create the section headers.
241 void set_section_info(const Output_section_headers*,
242 const Output_section* shstrtab);
54dc6425
ILT
243
244 // Write the data to the file.
245 void
75f65a3e
ILT
246 do_write(Output_file*);
247
248 // Return the required alignment.
249 uint64_t
250 do_addralign() const
251 { return Output_data::default_alignment(this->size_); }
252
253 // Set the address and offset--we only implement this for error
254 // checking.
255 void
256 do_set_address(uint64_t, off_t off) const
257 { assert(off == 0); }
54dc6425
ILT
258
259 private:
61ba1cf9
ILT
260 // Write the data to the file with the right size and endianness.
261 template<int size, bool big_endian>
262 void
263 do_sized_write(Output_file*);
264
75f65a3e 265 int size_;
61ba1cf9 266 bool big_endian_;
54dc6425
ILT
267 const General_options& options_;
268 const Target* target_;
269 const Symbol_table* symtab_;
61ba1cf9 270 const Output_segment_headers* segment_header_;
54dc6425
ILT
271 const Output_section_headers* section_header_;
272 const Output_section* shstrtab_;
273};
274
ead1e424
ILT
275// Output sections are mainly comprised of input sections. However,
276// there are cases where we have data to write out which is not in an
277// input section. Output_section_data is used in such cases. This is
278// an abstract base class.
279
280class Output_section_data : public Output_data
281{
282 public:
283 Output_section_data(off_t data_size, uint64_t addralign)
284 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
285 { }
286
287 Output_section_data(uint64_t addralign)
288 : Output_data(0), output_section_(NULL), addralign_(addralign)
289 { }
290
291 // Record the output section.
292 void
293 set_output_section(Output_section* os)
294 {
295 assert(this->output_section_ == NULL);
296 this->output_section_ = os;
297 }
298
299 protected:
300 // The child class must implement do_write.
301
302 // Return the required alignment.
303 uint64_t
304 do_addralign() const
305 { return this->addralign_; }
306
307 // Return the section index of the output section.
308 unsigned int
309 do_out_shndx() const;
310
311 private:
312 // The output section for this section.
313 const Output_section* output_section_;
314 // The required alignment.
315 uint64_t addralign_;
316};
317
dbe717ef
ILT
318// A simple case of Output_data in which we have constant data to
319// output.
ead1e424 320
dbe717ef 321class Output_data_const : public Output_section_data
ead1e424
ILT
322{
323 public:
dbe717ef
ILT
324 Output_data_const(const std::string& data, uint64_t addralign)
325 : Output_section_data(data.size(), addralign), data_(data)
326 { }
327
328 Output_data_const(const char* p, off_t len, uint64_t addralign)
329 : Output_section_data(len, addralign), data_(p, len)
330 { }
331
332 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
333 : Output_section_data(len, addralign),
334 data_(reinterpret_cast<const char*>(p), len)
335 { }
336
337 // Write the data to the file.
338 void
339 do_write(Output_file* output);
340
341 private:
342 std::string data_;
343};
344
345// Output_data_common is used to handle the common symbols. This is
346// quite simple.
347
348class Output_data_common : public Output_section_data
349{
350 public:
351 Output_data_common(uint64_t addralign)
ead1e424
ILT
352 : Output_section_data(addralign)
353 { }
354
355 // Set the size.
356 void
357 set_common_size(off_t common_size)
358 { this->set_data_size(common_size); }
359
360 // Write out the data--there is nothing to do, as common symbols are
361 // always zero and are stored in the BSS.
362 void
363 do_write(Output_file*)
364 { }
365};
366
c06b7b0b
ILT
367// This POD class is used to represent a single reloc in the output
368// file. This could be a private class within Output_data_reloc, but
369// the templatization is complex enough that I broke it out into a
370// separate class. The class is templatized on either elfcpp::SHT_REL
371// or elfcpp::SHT_RELA, and also on whether this is a dynamic
372// relocation or an ordinary relocation.
373
374// A relocation can be against a global symbol, a local symbol, an
375// output section, or the undefined symbol at index 0. We represent
376// the latter by using a NULL global symbol.
377
378template<int sh_type, bool dynamic, int size, bool big_endian>
379class Output_reloc;
380
381template<bool dynamic, int size, bool big_endian>
382class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
383{
384 public:
385 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
386
387 // An uninitialized entry. We need this because we want to put
388 // instances of this class into an STL container.
389 Output_reloc()
390 : local_sym_index_(INVALID_CODE)
391 { }
392
393 // A reloc against a global symbol.
394 Output_reloc(Symbol* gsym, unsigned int type, Address address)
395 : local_sym_index_(GSYM_CODE), type_(type), address_(address)
396 { this->u_.gsym = gsym; }
397
398 // A reloc against a local symbol.
399 Output_reloc(Sized_relobj<size, big_endian>* object,
400 unsigned int local_sym_index,
401 unsigned int type, Address address)
402 : local_sym_index_(local_sym_index), type_(type), address_(address)
403 {
404 assert(local_sym_index != GSYM_CODE && local_sym_index != INVALID_CODE);
405 this->u_.object = object;
406 }
407
408 // A reloc against the STT_SECTION symbol of an output section.
409 Output_reloc(Output_section* os, unsigned int type, Address address)
410 : local_sym_index_(SECTION_CODE), type_(type), address_(address)
411 { this->u_.os = os; }
412
413 // Write the reloc entry to an output view.
414 void
415 write(unsigned char* pov) const;
416
417 // Write the offset and info fields to Write_rel.
418 template<typename Write_rel>
419 void write_rel(Write_rel*) const;
420
421 private:
422 // Return the symbol index. We can't do a double template
423 // specialization, so we do a secondary template here.
424 unsigned int
425 get_symbol_index() const;
426
427 // Codes for local_sym_index_.
428 enum
429 {
430 // Global symbol.
431 GSYM_CODE = -1U,
432 // Output section.
433 SECTION_CODE = -2U,
434 // Invalid uninitialized entry.
435 INVALID_CODE = -3U
436 };
437
438 union
439 {
440 // For a local symbol, the object. We will never generate a
441 // relocation against a local symbol in a dynamic object; that
442 // doesn't make sense. And our callers will always be
443 // templatized, so we use Sized_relobj here.
444 Sized_relobj<size, big_endian>* object;
445 // For a global symbol, the symbol. If this is NULL, it indicates
446 // a relocation against the undefined 0 symbol.
447 Symbol* gsym;
448 // For a relocation against an output section, the output section.
449 Output_section* os;
450 } u_;
451 // For a local symbol, the local symbol index. This is GSYM_CODE
452 // for a global symbol, or INVALID_CODE for an uninitialized value.
453 unsigned int local_sym_index_;
454 unsigned int type_;
455 Address address_;
456};
457
458// The SHT_RELA version of Output_reloc<>. This is just derived from
459// the SHT_REL version of Output_reloc, but it adds an addend.
460
461template<bool dynamic, int size, bool big_endian>
462class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
463{
464 public:
465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
466 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
467
468 // An uninitialized entry.
469 Output_reloc()
470 : rel_()
471 { }
472
473 // A reloc against a global symbol.
474 Output_reloc(Symbol* gsym, unsigned int type, Address address, Addend addend)
475 : rel_(gsym, type, address), addend_(addend)
476 { }
477
478 // A reloc against a local symbol.
479 Output_reloc(Sized_relobj<size, big_endian>* object,
480 unsigned int local_sym_index,
481 unsigned int type, Address address, Addend addend)
482 : rel_(object, local_sym_index, type, address), addend_(addend)
483 { }
484
485 // A reloc against the STT_SECTION symbol of an output section.
486 Output_reloc(Output_section* os, unsigned int type, Address address,
487 Addend addend)
488 : rel_(os, type, address), addend_(addend)
489 { }
490
491 // Write the reloc entry to an output view.
492 void
493 write(unsigned char* pov) const;
494
495 private:
496 // The basic reloc.
497 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
498 // The addend.
499 Addend addend_;
500};
501
502// Output_data_reloc is used to manage a section containing relocs.
503// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
504// indicates whether this is a dynamic relocation or a normal
505// relocation. Output_data_reloc_base is a base class.
506// Output_data_reloc is the real class, which we specialize based on
507// the reloc type.
508
509template<int sh_type, bool dynamic, int size, bool big_endian>
510class Output_data_reloc_base : public Output_section_data
511{
512 public:
513 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
514 typedef typename Output_reloc_type::Address Address;
515 static const int reloc_size =
516 Reloc_types<sh_type, size, big_endian>::reloc_size;
517
518 // Construct the section.
519 Output_data_reloc_base()
520 : Output_section_data(Output_data::default_alignment(size))
521 { }
522
523 // Write out the data.
524 void
525 do_write(Output_file*);
526
527 protected:
528 // Add a relocation entry.
529 void
530 add(const Output_reloc_type& reloc)
531 {
532 this->relocs_.push_back(reloc);
533 this->set_data_size(this->relocs_.size() * reloc_size);
534 }
535
536 private:
537 typedef std::vector<Output_reloc_type> Relocs;
538
539 Relocs relocs_;
540};
541
542// The class which callers actually create.
543
544template<int sh_type, bool dynamic, int size, bool big_endian>
545class Output_data_reloc;
546
547// The SHT_REL version of Output_data_reloc.
548
549template<bool dynamic, int size, bool big_endian>
550class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
551 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
552{
553 private:
554 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
555 big_endian> Base;
556
557 public:
558 typedef typename Base::Output_reloc_type Output_reloc_type;
559 typedef typename Output_reloc_type::Address Address;
560
561 Output_data_reloc()
562 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
563 { }
564
565 // Add a reloc against a global symbol.
566 void
567 add_global(Symbol* gsym, unsigned int type, Address address)
568 { this->add(Output_reloc_type(gsym, type, address)); }
569
570 // Add a reloc against a local symbol.
571 void
572 add_local(Sized_relobj<size, big_endian>* object,
573 unsigned int local_sym_index, unsigned int type, Address address)
574 { this->add(Output_reloc_type(object, local_sym_index, type, address)); }
575
576 // A reloc against the STT_SECTION symbol of an output section.
577 void
578 add_output_section(Output_section* os, unsigned int type, Address address)
579 { this->add(Output_reloc_type(os, type, address)); }
580};
581
582// The SHT_RELA version of Output_data_reloc.
583
584template<bool dynamic, int size, bool big_endian>
585class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
586 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
587{
588 private:
589 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
590 big_endian> Base;
591
592 public:
593 typedef typename Base::Output_reloc_type Output_reloc_type;
594 typedef typename Output_reloc_type::Address Address;
595 typedef typename Output_reloc_type::Addend Addend;
596
597 Output_data_reloc()
598 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
599 { }
600
601 // Add a reloc against a global symbol.
602 void
603 add_global(Symbol* gsym, unsigned int type, Address address, Addend addend)
604 { this->add(Output_reloc_type(gsym, type, address, addend)); }
605
606 // Add a reloc against a local symbol.
607 void
608 add_local(Sized_relobj<size, big_endian>* object,
609 unsigned int local_sym_index, unsigned int type,
610 Address address, Addend addend)
611 {
612 this->add(Output_reloc_type(object, local_sym_index, type, address,
613 addend));
614 }
615
616 // A reloc against the STT_SECTION symbol of an output section.
617 void
618 add_output_section(Output_section* os, unsigned int type, Address address,
619 Addend addend)
620 { this->add(Output_reloc_type(os, type, address, addend)); }
621};
622
dbe717ef
ILT
623// Output_data_got is used to manage a GOT. Each entry in the GOT is
624// for one symbol--either a global symbol or a local symbol in an
ead1e424 625// object. The target specific code adds entries to the GOT as
dbe717ef 626// needed.
ead1e424
ILT
627
628template<int size, bool big_endian>
dbe717ef 629class Output_data_got : public Output_section_data
ead1e424
ILT
630{
631 public:
632 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
633
dbe717ef 634 Output_data_got()
ead1e424
ILT
635 : Output_section_data(Output_data::default_alignment(size)),
636 entries_()
637 { }
638
dbe717ef
ILT
639 // Add an entry for a global symbol to the GOT. Return true if this
640 // is a new GOT entry, false if the symbol was already in the GOT.
641 bool
642 add_global(Symbol* gsym);
ead1e424
ILT
643
644 // Add an entry for a local symbol to the GOT. This returns the
645 // offset of the new entry from the start of the GOT.
646 unsigned int
647 add_local(Object* object, unsigned int sym_index)
648 {
649 this->entries_.push_back(Got_entry(object, sym_index));
650 this->set_got_size();
651 return this->last_got_offset();
652 }
653
654 // Add a constant to the GOT. This returns the offset of the new
655 // entry from the start of the GOT.
656 unsigned int
657 add_constant(Valtype constant)
658 {
659 this->entries_.push_back(Got_entry(constant));
660 this->set_got_size();
661 return this->last_got_offset();
662 }
663
664 // Write out the GOT table.
665 void
666 do_write(Output_file*);
667
668 private:
669 // This POD class holds a single GOT entry.
670 class Got_entry
671 {
672 public:
673 // Create a zero entry.
674 Got_entry()
675 : local_sym_index_(CONSTANT_CODE)
676 { this->u_.constant = 0; }
677
678 // Create a global symbol entry.
679 Got_entry(Symbol* gsym)
680 : local_sym_index_(GSYM_CODE)
681 { this->u_.gsym = gsym; }
682
683 // Create a local symbol entry.
684 Got_entry(Object* object, unsigned int local_sym_index)
685 : local_sym_index_(local_sym_index)
686 {
687 assert(local_sym_index != GSYM_CODE
688 && local_sym_index != CONSTANT_CODE);
689 this->u_.object = object;
690 }
691
692 // Create a constant entry. The constant is a host value--it will
693 // be swapped, if necessary, when it is written out.
694 Got_entry(Valtype constant)
695 : local_sym_index_(CONSTANT_CODE)
696 { this->u_.constant = constant; }
697
698 // Write the GOT entry to an output view.
699 void
700 write(unsigned char* pov) const;
701
702 private:
703 enum
704 {
705 GSYM_CODE = -1U,
706 CONSTANT_CODE = -2U
707 };
708
709 union
710 {
711 // For a local symbol, the object.
712 Object* object;
713 // For a global symbol, the symbol.
714 Symbol* gsym;
715 // For a constant, the constant.
716 Valtype constant;
717 } u_;
c06b7b0b
ILT
718 // For a local symbol, the local symbol index. This is GSYM_CODE
719 // for a global symbol, or CONSTANT_CODE for a constant.
ead1e424
ILT
720 unsigned int local_sym_index_;
721 };
722
723 typedef std::vector<Got_entry> Got_entries;
724
725 // Return the offset into the GOT of GOT entry I.
726 unsigned int
727 got_offset(unsigned int i) const
728 { return i * (size / 8); }
729
730 // Return the offset into the GOT of the last entry added.
731 unsigned int
732 last_got_offset() const
733 { return this->got_offset(this->entries_.size() - 1); }
734
735 // Set the size of the section.
736 void
737 set_got_size()
738 { this->set_data_size(this->got_offset(this->entries_.size())); }
739
740 // The list of GOT entries.
741 Got_entries entries_;
742};
743
a2fb1b05
ILT
744// An output section. We don't expect to have too many output
745// sections, so we don't bother to do a template on the size.
746
54dc6425 747class Output_section : public Output_data
a2fb1b05
ILT
748{
749 public:
750 // Create an output section, giving the name, type, and flags.
61ba1cf9 751 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
ead1e424 752 bool may_add_data);
54dc6425 753 virtual ~Output_section();
a2fb1b05 754
ead1e424
ILT
755 // Add a new input section SHNDX, named NAME, with header SHDR, from
756 // object OBJECT. Return the offset within the output section.
a2fb1b05
ILT
757 template<int size, bool big_endian>
758 off_t
f6ce93d6 759 add_input_section(Relobj* object, unsigned int shndx, const char *name,
a2fb1b05
ILT
760 const elfcpp::Shdr<size, big_endian>& shdr);
761
ead1e424 762 // Add generated data ODATA to this output section.
c06b7b0b 763 void
ead1e424
ILT
764 add_output_section_data(Output_section_data* posd);
765
a2fb1b05
ILT
766 // Return the section name.
767 const char*
768 name() const
769 { return this->name_; }
770
771 // Return the section type.
772 elfcpp::Elf_Word
773 type() const
774 { return this->type_; }
775
776 // Return the section flags.
777 elfcpp::Elf_Xword
778 flags() const
779 { return this->flags_; }
780
ead1e424 781 // Return the section index in the output file.
61ba1cf9 782 unsigned int
ead1e424
ILT
783 do_out_shndx() const
784 { return this->out_shndx_; }
785
786 // Set the output section index.
787 void
788 do_set_out_shndx(unsigned int shndx)
789 { this->out_shndx_ = shndx; }
61ba1cf9
ILT
790
791 // Set the entsize field.
792 void
793 set_entsize(uint64_t v)
794 { this->entsize_ = v; }
795
796 // Set the link field.
797 void
798 set_link(unsigned int v)
799 { this->link_ = v; }
800
801 // Set the info field.
802 void
803 set_info(unsigned int v)
804 { this->info_ = v; }
805
806 // Set the addralign field.
807 void
808 set_addralign(uint64_t v)
809 { this->addralign_ = v; }
810
c06b7b0b
ILT
811 // Indicate that we need a symtab index.
812 void
813 set_needs_symtab_index()
814 { this->needs_symtab_index_ = true; }
815
816 // Return whether we need a symtab index.
817 bool
818 needs_symtab_index() const
819 { return this->needs_symtab_index_; }
820
821 // Get the symtab index.
822 unsigned int
823 symtab_index() const
824 {
825 assert(this->symtab_index_ != 0);
826 return this->symtab_index_;
827 }
828
829 // Set the symtab index.
830 void
831 set_symtab_index(unsigned int index)
832 {
833 assert(index != 0);
834 this->symtab_index_ = index;
835 }
836
837 // Indicate that we need a dynsym index.
838 void
839 set_needs_dynsym_index()
840 { this->needs_dynsym_index_ = true; }
841
842 // Return whether we need a dynsym index.
843 bool
844 needs_dynsym_index() const
845 { return this->needs_dynsym_index_; }
846
847 // Get the dynsym index.
848 unsigned int
849 dynsym_index() const
850 {
851 assert(this->dynsym_index_ != 0);
852 return this->dynsym_index_;
853 }
854
855 // Set the dynsym index.
856 void
857 set_dynsym_index(unsigned int index)
858 {
859 assert(index != 0);
860 this->dynsym_index_ = index;
861 }
862
ead1e424
ILT
863 // Set the address of the Output_section. For a typical
864 // Output_section, there is nothing to do, but if there are any
865 // Output_section_data objects we need to set the final addresses
866 // here.
867 void
868 do_set_address(uint64_t, off_t);
869
54dc6425 870 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
871 // does nothing: the data is written out by calling Object::Relocate
872 // on each input object. But if there are any Output_section_data
873 // objects we do need to write them out here.
54dc6425 874 virtual void
ead1e424 875 do_write(Output_file*);
54dc6425 876
75f65a3e
ILT
877 // Return the address alignment--function required by parent class.
878 uint64_t
879 do_addralign() const
880 { return this->addralign_; }
881
882 // Return whether this is an Output_section.
883 bool
884 do_is_section() const
885 { return true; }
886
54dc6425
ILT
887 // Return whether this is a section of the specified type.
888 bool
75f65a3e 889 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
890 { return this->type_ == type; }
891
892 // Return whether the specified section flag is set.
893 bool
75f65a3e 894 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
895 { return (this->flags_ & flag) != 0; }
896
61ba1cf9
ILT
897 // Write the section header into *OPHDR.
898 template<int size, bool big_endian>
899 void
900 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
901
a2fb1b05 902 private:
ead1e424
ILT
903 // In some cases we need to keep a list of the input sections
904 // associated with this output section. We only need the list if we
905 // might have to change the offsets of the input section within the
906 // output section after we add the input section. The ordinary
907 // input sections will be written out when we process the object
908 // file, and as such we don't need to track them here. We do need
909 // to track Output_section_data objects here. We store instances of
910 // this structure in a std::vector, so it must be a POD. There can
911 // be many instances of this structure, so we use a union to save
912 // some space.
913 class Input_section
914 {
915 public:
916 Input_section()
917 : shndx_(0), p2align_(0), data_size_(0)
918 { this->u_.object = NULL; }
919
f6ce93d6 920 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
ead1e424
ILT
921 uint64_t addralign)
922 : shndx_(shndx),
923 p2align_(ffsll(static_cast<long long>(addralign))),
924 data_size_(data_size)
925 {
926 assert(shndx != -1U);
927 this->u_.object = object;
928 }
929
930 Input_section(Output_section_data* posd)
931 : shndx_(-1U),
932 p2align_(ffsll(static_cast<long long>(posd->addralign()))),
933 data_size_(0)
934 { this->u_.posd = posd; }
935
936 // The required alignment.
937 uint64_t
938 addralign() const
939 { return static_cast<uint64_t>(1) << this->p2align_; }
940
941 // Return the required size.
942 off_t
943 data_size() const;
944
945 // Set the address and file offset. This is called during
946 // Layout::finalize. SECOFF is the file offset of the enclosing
947 // section.
948 void
949 set_address(uint64_t addr, off_t off, off_t secoff);
950
951 // Write out the data. This does nothing for an input section.
952 void
953 write(Output_file*);
954
955 private:
956 // Whether this is an input section.
957 bool
958 is_input_section() const
959 { return this->shndx_ != -1U; }
960
961 // For an ordinary input section, this is the section index in
962 // the input file. For an Output_section_data, this is -1U.
963 unsigned int shndx_;
964 // The required alignment, stored as a power of 2.
965 unsigned int p2align_;
966 // For an ordinary input section, the section size.
967 off_t data_size_;
968 union
969 {
970 // If shndx_ != -1U, this points to the object which holds the
971 // input section.
f6ce93d6 972 Relobj* object;
ead1e424
ILT
973 // If shndx_ == -1U, this is the data to write out.
974 Output_section_data* posd;
975 } u_;
976 };
977
978 typedef std::vector<Input_section> Input_section_list;
979
a2fb1b05
ILT
980 // Most of these fields are only valid after layout.
981
982 // The name of the section. This will point into a Stringpool.
983 const char* name_;
75f65a3e 984 // The section address is in the parent class.
a2fb1b05
ILT
985 // The section alignment.
986 uint64_t addralign_;
987 // The section entry size.
988 uint64_t entsize_;
75f65a3e 989 // The file offset is in the parent class.
a2fb1b05
ILT
990 // The section link field.
991 unsigned int link_;
992 // The section info field.
993 unsigned int info_;
994 // The section type.
995 elfcpp::Elf_Word type_;
996 // The section flags.
997 elfcpp::Elf_Xword flags_;
61ba1cf9 998 // The section index.
ead1e424 999 unsigned int out_shndx_;
c06b7b0b
ILT
1000 // If there is a STT_SECTION for this output section in the normal
1001 // symbol table, this is the symbol index. This starts out as zero.
1002 // It is initialized in Layout::finalize() to be the index, or -1U
1003 // if there isn't one.
1004 unsigned int symtab_index_;
1005 // If there is a STT_SECTION for this output section in the dynamic
1006 // symbol table, this is the symbol index. This starts out as zero.
1007 // It is initialized in Layout::finalize() to be the index, or -1U
1008 // if there isn't one.
1009 unsigned int dynsym_index_;
ead1e424
ILT
1010 // The input sections. This will be empty in cases where we don't
1011 // need to keep track of them.
1012 Input_section_list input_sections_;
1013 // The offset of the first entry in input_sections_.
1014 off_t first_input_offset_;
1015 // Whether we permit adding data.
c06b7b0b
ILT
1016 bool may_add_data_ : 1;
1017 // Whether this output section needs a STT_SECTION symbol in the
1018 // normal symbol table. This will be true if there is a relocation
1019 // which needs it.
1020 bool needs_symtab_index_ : 1;
1021 // Whether this output section needs a STT_SECTION symbol in the
1022 // dynamic symbol table. This will be true if there is a dynamic
1023 // relocation which needs it.
1024 bool needs_dynsym_index_ : 1;
a2fb1b05
ILT
1025};
1026
54dc6425 1027// A special Output_section which represents the symbol table
ead1e424
ILT
1028// (SHT_SYMTAB). The actual data is written out by
1029// Symbol_table::write_globals.
54dc6425
ILT
1030
1031class Output_section_symtab : public Output_section
1032{
1033 public:
c06b7b0b
ILT
1034 Output_section_symtab(const char* name, off_t data_size)
1035 : Output_section(name, elfcpp::SHT_SYMTAB, 0, false)
1036 { this->set_data_size(data_size); }
ead1e424
ILT
1037
1038 // The data is written out by Symbol_table::write_globals. We don't
1039 // do anything here.
1040 void
1041 do_write(Output_file*)
1042 { }
c06b7b0b
ILT
1043};
1044
1045// A special Output_section which represents the dynamic symbol table
1046// (SHT_DYNSYM). The actual data is written out by
1047// Symbol_table::write_globals.
1048
1049class Output_section_dynsym : public Output_section
1050{
1051 public:
1052 Output_section_dynsym(const char* name, off_t data_size)
1053 : Output_section(name, elfcpp::SHT_DYNSYM, 0, false)
1054 { this->set_data_size(data_size); }
ead1e424 1055
c06b7b0b
ILT
1056 // The data is written out by Symbol_table::write_globals. We don't
1057 // do anything here.
ead1e424 1058 void
c06b7b0b
ILT
1059 do_write(Output_file*)
1060 { }
75f65a3e
ILT
1061};
1062
1063// A special Output_section which holds a string table.
1064
1065class Output_section_strtab : public Output_section
1066{
1067 public:
ead1e424 1068 Output_section_strtab(const char* name, Stringpool* contents);
75f65a3e
ILT
1069
1070 // Write out the data.
1071 void
1072 do_write(Output_file*);
1073
1074 private:
1075 Stringpool* contents_;
54dc6425
ILT
1076};
1077
a2fb1b05
ILT
1078// An output segment. PT_LOAD segments are built from collections of
1079// output sections. Other segments typically point within PT_LOAD
1080// segments, and are built directly as needed.
1081
1082class Output_segment
1083{
1084 public:
1085 // Create an output segment, specifying the type and flags.
1086 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
1087
1088 // Return the virtual address.
1089 uint64_t
1090 vaddr() const
1091 { return this->vaddr_; }
1092
1093 // Return the physical address.
1094 uint64_t
1095 paddr() const
1096 { return this->paddr_; }
1097
1098 // Return the segment type.
1099 elfcpp::Elf_Word
1100 type() const
1101 { return this->type_; }
1102
1103 // Return the segment flags.
1104 elfcpp::Elf_Word
1105 flags() const
1106 { return this->flags_; }
1107
92e059d8
ILT
1108 // Return the memory size.
1109 uint64_t
1110 memsz() const
1111 { return this->memsz_; }
1112
ead1e424
ILT
1113 // Return the file size.
1114 off_t
1115 filesz() const
1116 { return this->filesz_; }
1117
75f65a3e
ILT
1118 // Return the maximum alignment of the Output_data.
1119 uint64_t
ead1e424 1120 addralign();
75f65a3e 1121
a2fb1b05
ILT
1122 // Add an Output_section to this segment.
1123 void
dbe717ef
ILT
1124 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1125 { this->add_output_section(os, seg_flags, false); }
1126
1127 // Add an Output_section to the start of this segment.
1128 void
1129 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1130 { this->add_output_section(os, seg_flags, true); }
75f65a3e
ILT
1131
1132 // Add an Output_data (which is not an Output_section) to the start
1133 // of this segment.
1134 void
1135 add_initial_output_data(Output_data*);
1136
1137 // Set the address of the segment to ADDR and the offset to *POFF
1138 // (aligned if necessary), and set the addresses and offsets of all
ead1e424
ILT
1139 // contained output sections accordingly. Set the section indexes
1140 // of all contained output sections starting with *PSHNDX. Return
1141 // the address of the immediately following segment. Update *POFF
1142 // and *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 1143 uint64_t
ead1e424 1144 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
75f65a3e
ILT
1145
1146 // Set the offset of this segment based on the section. This should
1147 // only be called for a non-PT_LOAD segment.
1148 void
1149 set_offset();
1150
1151 // Return the number of output sections.
1152 unsigned int
1153 output_section_count() const;
a2fb1b05 1154
61ba1cf9
ILT
1155 // Write the segment header into *OPHDR.
1156 template<int size, bool big_endian>
1157 void
ead1e424 1158 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
1159
1160 // Write the section headers of associated sections into V.
1161 template<int size, bool big_endian>
1162 unsigned char*
5482377d 1163 write_section_headers(const Stringpool*,
ead1e424
ILT
1164 unsigned char* v,
1165 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 1166
a2fb1b05
ILT
1167 private:
1168 Output_segment(const Output_segment&);
1169 Output_segment& operator=(const Output_segment&);
1170
54dc6425 1171 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 1172
dbe717ef
ILT
1173 // Add an Output_section to this segment, specifying front or back.
1174 void
1175 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
1176 bool front);
1177
ead1e424
ILT
1178 // Find the maximum alignment in an Output_data_list.
1179 static uint64_t
1180 maximum_alignment(const Output_data_list*);
1181
75f65a3e
ILT
1182 // Set the section addresses in an Output_data_list.
1183 uint64_t
ead1e424
ILT
1184 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
1185 unsigned int* pshndx);
75f65a3e
ILT
1186
1187 // Return the number of Output_sections in an Output_data_list.
1188 unsigned int
1189 output_section_count_list(const Output_data_list*) const;
1190
61ba1cf9
ILT
1191 // Write the section headers in the list into V.
1192 template<int size, bool big_endian>
1193 unsigned char*
1194 write_section_headers_list(const Stringpool*, const Output_data_list*,
ead1e424
ILT
1195 unsigned char* v,
1196 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 1197
75f65a3e 1198 // The list of output data with contents attached to this segment.
54dc6425 1199 Output_data_list output_data_;
75f65a3e
ILT
1200 // The list of output data without contents attached to this segment.
1201 Output_data_list output_bss_;
a2fb1b05
ILT
1202 // The segment virtual address.
1203 uint64_t vaddr_;
1204 // The segment physical address.
1205 uint64_t paddr_;
1206 // The size of the segment in memory.
1207 uint64_t memsz_;
1208 // The segment alignment.
1209 uint64_t align_;
1210 // The offset of the segment data within the file.
1211 off_t offset_;
1212 // The size of the segment data in the file.
1213 off_t filesz_;
1214 // The segment type;
1215 elfcpp::Elf_Word type_;
1216 // The segment flags.
1217 elfcpp::Elf_Word flags_;
ead1e424
ILT
1218 // Whether we have set align_.
1219 bool is_align_known_;
a2fb1b05
ILT
1220};
1221
61ba1cf9 1222// This class represents the output file.
a2fb1b05
ILT
1223
1224class Output_file
1225{
1226 public:
61ba1cf9
ILT
1227 Output_file(const General_options& options);
1228
1229 // Open the output file. FILE_SIZE is the final size of the file.
1230 void
1231 open(off_t file_size);
1232
1233 // Close the output file and make sure there are no error.
1234 void
1235 close();
1236
1237 // We currently always use mmap which makes the view handling quite
1238 // simple. In the future we may support other approaches.
a2fb1b05
ILT
1239
1240 // Write data to the output file.
1241 void
61ba1cf9
ILT
1242 write(off_t offset, const void* data, off_t len)
1243 { memcpy(this->base_ + offset, data, len); }
1244
1245 // Get a buffer to use to write to the file, given the offset into
1246 // the file and the size.
1247 unsigned char*
1248 get_output_view(off_t start, off_t size)
1249 {
1250 assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
1251 return this->base_ + start;
1252 }
1253
1254 // VIEW must have been returned by get_output_view. Write the
1255 // buffer to the file, passing in the offset and the size.
1256 void
1257 write_output_view(off_t, off_t, unsigned char*)
1258 { }
1259
1260 private:
1261 // General options.
1262 const General_options& options_;
1263 // File name.
1264 const char* name_;
1265 // File descriptor.
1266 int o_;
1267 // File size.
1268 off_t file_size_;
1269 // Base of file mapped into memory.
1270 unsigned char* base_;
a2fb1b05
ILT
1271};
1272
1273} // End namespace gold.
1274
1275#endif // !defined(GOLD_OUTPUT_H)
This page took 0.080604 seconds and 4 git commands to generate.