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