Fix a ChangeLog formatting problem.
[deliverable/binutils-gdb.git] / gold / output.h
1 // output.h -- manage the output file for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
25
26 #include <list>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "layout.h"
31 #include "reloc-types.h"
32
33 namespace gold
34 {
35
36 class General_options;
37 class Object;
38 class Symbol;
39 class Output_file;
40 class Output_section;
41 class Relocatable_relocs;
42 class Target;
43 template<int size, bool big_endian>
44 class Sized_target;
45 template<int size, bool big_endian>
46 class Sized_relobj;
47
48 // An abtract class for data which has to go into the output file.
49
50 class Output_data
51 {
52 public:
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
58 { }
59
60 virtual
61 ~Output_data();
62
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
65 uint64_t
66 address() const
67 {
68 gold_assert(this->is_address_valid_);
69 return this->address_;
70 }
71
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
75 off_t
76 data_size() const
77 {
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
80 }
81
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
85 off_t
86 offset() const
87 {
88 gold_assert(this->is_offset_valid_);
89 return this->offset_;
90 }
91
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
94 void
95 reset_address_and_file_offset()
96 {
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
101 }
102
103 // Return the required alignment.
104 uint64_t
105 addralign() const
106 { return this->do_addralign(); }
107
108 // Return whether this has a load address.
109 bool
110 has_load_address() const
111 { return this->do_has_load_address(); }
112
113 // Return the load address.
114 uint64_t
115 load_address() const
116 { return this->do_load_address(); }
117
118 // Return whether this is an Output_section.
119 bool
120 is_section() const
121 { return this->do_is_section(); }
122
123 // Return whether this is an Output_section of the specified type.
124 bool
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
127
128 // Return whether this is an Output_section with the specified flag
129 // set.
130 bool
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
133
134 // Return the output section that this goes in, if there is one.
135 Output_section*
136 output_section()
137 { return this->do_output_section(); }
138
139 // Return the output section index, if there is an output section.
140 unsigned int
141 out_shndx() const
142 { return this->do_out_shndx(); }
143
144 // Set the output section index, if this is an output section.
145 void
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
148
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
152 void
153 set_address_and_file_offset(uint64_t addr, off_t off)
154 {
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
158 }
159
160 // Set the address.
161 void
162 set_address(uint64_t addr)
163 {
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
167 }
168
169 // Set the file offset.
170 void
171 set_file_offset(off_t off)
172 {
173 gold_assert(!this->is_offset_valid_);
174 this->offset_ = off;
175 this->is_offset_valid_ = true;
176 }
177
178 // Finalize the data size.
179 void
180 finalize_data_size()
181 {
182 if (!this->is_data_size_valid_)
183 {
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
187 }
188 }
189
190 // Set the TLS offset. Called only for SHT_TLS sections.
191 void
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
194
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
197 uint64_t
198 tls_offset() const
199 { return this->do_tls_offset(); }
200
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
203 void
204 write(Output_file* file)
205 { this->do_write(file); }
206
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
209 static void
210 layout_complete()
211 { Output_data::allocated_sizes_are_fixed = true; }
212
213 // Used to check that layout has been done.
214 static bool
215 is_layout_complete()
216 { return Output_data::allocated_sizes_are_fixed; }
217
218 // Count the number of dynamic relocations applied to this section.
219 void
220 add_dynamic_reloc()
221 { ++this->dynamic_reloc_count_; }
222
223 // Return the number of dynamic relocations applied to this section.
224 unsigned int
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
227
228 // Whether the address is valid.
229 bool
230 is_address_valid() const
231 { return this->is_address_valid_; }
232
233 // Whether the file offset is valid.
234 bool
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
237
238 // Whether the data size is valid.
239 bool
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
242
243 protected:
244 // Functions that child classes may or in some cases must implement.
245
246 // Write the data to the output file.
247 virtual void
248 do_write(Output_file*) = 0;
249
250 // Return the required alignment.
251 virtual uint64_t
252 do_addralign() const = 0;
253
254 // Return whether this has a load address.
255 virtual bool
256 do_has_load_address() const
257 { return false; }
258
259 // Return the load address.
260 virtual uint64_t
261 do_load_address() const
262 { gold_unreachable(); }
263
264 // Return whether this is an Output_section.
265 virtual bool
266 do_is_section() const
267 { return false; }
268
269 // Return whether this is an Output_section of the specified type.
270 // This only needs to be implement by Output_section.
271 virtual bool
272 do_is_section_type(elfcpp::Elf_Word) const
273 { return false; }
274
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
277 virtual bool
278 do_is_section_flag_set(elfcpp::Elf_Xword) const
279 { return false; }
280
281 // Return the output section, if there is one.
282 virtual Output_section*
283 do_output_section()
284 { return NULL; }
285
286 // Return the output section index, if there is an output section.
287 virtual unsigned int
288 do_out_shndx() const
289 { gold_unreachable(); }
290
291 // Set the output section index, if this is an output section.
292 virtual void
293 do_set_out_shndx(unsigned int)
294 { gold_unreachable(); }
295
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
299 virtual void
300 set_final_data_size()
301 { gold_unreachable(); }
302
303 // A hook for resetting the address and file offset.
304 virtual void
305 do_reset_address_and_file_offset()
306 { }
307
308 // Set the TLS offset. Called only for SHT_TLS sections.
309 virtual void
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
312
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
315 virtual uint64_t
316 do_tls_offset() const
317 { gold_unreachable(); }
318
319 // Functions that child classes may call.
320
321 // Set the size of the data.
322 void
323 set_data_size(off_t data_size)
324 {
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
328 }
329
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
332 off_t
333 current_data_size_for_child() const
334 { return this->data_size_; }
335
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
338 void
339 set_current_data_size_for_child(off_t data_size)
340 {
341 gold_assert(!this->is_data_size_valid_);
342 this->data_size_ = data_size;
343 }
344
345 // Return default alignment for the target size.
346 static uint64_t
347 default_alignment();
348
349 // Return default alignment for a specified size--32 or 64.
350 static uint64_t
351 default_alignment_for_size(int size);
352
353 private:
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
356
357 // This is used for verification, to make sure that we don't try to
358 // change any sizes of allocated sections after we set the section
359 // addresses.
360 static bool allocated_sizes_are_fixed;
361
362 // Memory address in output file.
363 uint64_t address_;
364 // Size of data in output file.
365 off_t data_size_;
366 // File offset of contents in output file.
367 off_t offset_;
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
376 };
377
378 // Output the section headers.
379
380 class Output_section_headers : public Output_data
381 {
382 public:
383 Output_section_headers(const Layout*,
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
386 const Layout::Section_list*,
387 const Stringpool*);
388
389 protected:
390 // Write the data to the file.
391 void
392 do_write(Output_file*);
393
394 // Return the required alignment.
395 uint64_t
396 do_addralign() const
397 { return Output_data::default_alignment(); }
398
399 private:
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
402 void
403 do_sized_write(Output_file*);
404
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
407 const Layout::Section_list* section_list_;
408 const Layout::Section_list* unattached_section_list_;
409 const Stringpool* secnamepool_;
410 };
411
412 // Output the segment headers.
413
414 class Output_segment_headers : public Output_data
415 {
416 public:
417 Output_segment_headers(const Layout::Segment_list& segment_list);
418
419 protected:
420 // Write the data to the file.
421 void
422 do_write(Output_file*);
423
424 // Return the required alignment.
425 uint64_t
426 do_addralign() const
427 { return Output_data::default_alignment(); }
428
429 private:
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
432 void
433 do_sized_write(Output_file*);
434
435 const Layout::Segment_list& segment_list_;
436 };
437
438 // Output the ELF file header.
439
440 class Output_file_header : public Output_data
441 {
442 public:
443 Output_file_header(const Target*,
444 const Symbol_table*,
445 const Output_segment_headers*,
446 const char* entry);
447
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
452
453 protected:
454 // Write the data to the file.
455 void
456 do_write(Output_file*);
457
458 // Return the required alignment.
459 uint64_t
460 do_addralign() const
461 { return Output_data::default_alignment(); }
462
463 private:
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
466 void
467 do_sized_write(Output_file*);
468
469 // Return the value to use for the entry address.
470 template<int size>
471 typename elfcpp::Elf_types<size>::Elf_Addr
472 entry();
473
474 const Target* target_;
475 const Symbol_table* symtab_;
476 const Output_segment_headers* segment_header_;
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
479 const char* entry_;
480 };
481
482 // Output sections are mainly comprised of input sections. However,
483 // there are cases where we have data to write out which is not in an
484 // input section. Output_section_data is used in such cases. This is
485 // an abstract base class.
486
487 class Output_section_data : public Output_data
488 {
489 public:
490 Output_section_data(off_t data_size, uint64_t addralign)
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
493
494 Output_section_data(uint64_t addralign)
495 : Output_data(), output_section_(NULL), addralign_(addralign)
496 { }
497
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
502
503 // Record the output section.
504 void
505 set_output_section(Output_section* os);
506
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
509 bool
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
512
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
519 bool
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
523 { return this->do_output_offset(object, shndx, offset, poutput); }
524
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
528 bool
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
531
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
534 void
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
537
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
540 void
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
543
544 protected:
545 // The child class must implement do_write.
546
547 // The child class may implement specific adjustments to the output
548 // section.
549 virtual void
550 do_adjust_output_section(Output_section*)
551 { }
552
553 // May be implemented by child class. Return true if the section
554 // was handled.
555 virtual bool
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
558
559 // The child class may implement output_offset.
560 virtual bool
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
563 { return false; }
564
565 // The child class may implement is_merge_section_for.
566 virtual bool
567 do_is_merge_section_for(const Relobj*, unsigned int) const
568 { return false; }
569
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
572 // implement this.
573 virtual void
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
576
577 // Print merge statistics.
578 virtual void
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
581
582 // Return the required alignment.
583 uint64_t
584 do_addralign() const
585 { return this->addralign_; }
586
587 // Return the output section.
588 Output_section*
589 do_output_section()
590 { return this->output_section_; }
591
592 // Return the section index of the output section.
593 unsigned int
594 do_out_shndx() const;
595
596 // Set the alignment.
597 void
598 set_addralign(uint64_t addralign)
599 { this->addralign_ = addralign; }
600
601 private:
602 // The output section for this section.
603 Output_section* output_section_;
604 // The required alignment.
605 uint64_t addralign_;
606 };
607
608 // Some Output_section_data classes build up their data step by step,
609 // rather than all at once. This class provides an interface for
610 // them.
611
612 class Output_section_data_build : public Output_section_data
613 {
614 public:
615 Output_section_data_build(uint64_t addralign)
616 : Output_section_data(addralign)
617 { }
618
619 // Get the current data size.
620 off_t
621 current_data_size() const
622 { return this->current_data_size_for_child(); }
623
624 // Set the current data size.
625 void
626 set_current_data_size(off_t data_size)
627 { this->set_current_data_size_for_child(data_size); }
628
629 protected:
630 // Set the final data size.
631 virtual void
632 set_final_data_size()
633 { this->set_data_size(this->current_data_size_for_child()); }
634 };
635
636 // A simple case of Output_data in which we have constant data to
637 // output.
638
639 class Output_data_const : public Output_section_data
640 {
641 public:
642 Output_data_const(const std::string& data, uint64_t addralign)
643 : Output_section_data(data.size(), addralign), data_(data)
644 { }
645
646 Output_data_const(const char* p, off_t len, uint64_t addralign)
647 : Output_section_data(len, addralign), data_(p, len)
648 { }
649
650 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
651 : Output_section_data(len, addralign),
652 data_(reinterpret_cast<const char*>(p), len)
653 { }
654
655 protected:
656 // Write the data to the output file.
657 void
658 do_write(Output_file*);
659
660 // Write the data to a buffer.
661 void
662 do_write_to_buffer(unsigned char* buffer)
663 { memcpy(buffer, this->data_.data(), this->data_.size()); }
664
665 private:
666 std::string data_;
667 };
668
669 // Another version of Output_data with constant data, in which the
670 // buffer is allocated by the caller.
671
672 class Output_data_const_buffer : public Output_section_data
673 {
674 public:
675 Output_data_const_buffer(const unsigned char* p, off_t len,
676 uint64_t addralign)
677 : Output_section_data(len, addralign), p_(p)
678 { }
679
680 protected:
681 // Write the data the output file.
682 void
683 do_write(Output_file*);
684
685 // Write the data to a buffer.
686 void
687 do_write_to_buffer(unsigned char* buffer)
688 { memcpy(buffer, this->p_, this->data_size()); }
689
690 private:
691 const unsigned char* p_;
692 };
693
694 // A place holder for a fixed amount of data written out via some
695 // other mechanism.
696
697 class Output_data_fixed_space : public Output_section_data
698 {
699 public:
700 Output_data_fixed_space(off_t data_size, uint64_t addralign)
701 : Output_section_data(data_size, addralign)
702 { }
703
704 protected:
705 // Write out the data--the actual data must be written out
706 // elsewhere.
707 void
708 do_write(Output_file*)
709 { }
710 };
711
712 // A place holder for variable sized data written out via some other
713 // mechanism.
714
715 class Output_data_space : public Output_section_data_build
716 {
717 public:
718 explicit Output_data_space(uint64_t addralign)
719 : Output_section_data_build(addralign)
720 { }
721
722 // Set the alignment.
723 void
724 set_space_alignment(uint64_t align)
725 { this->set_addralign(align); }
726
727 protected:
728 // Write out the data--the actual data must be written out
729 // elsewhere.
730 void
731 do_write(Output_file*)
732 { }
733 };
734
735 // A string table which goes into an output section.
736
737 class Output_data_strtab : public Output_section_data
738 {
739 public:
740 Output_data_strtab(Stringpool* strtab)
741 : Output_section_data(1), strtab_(strtab)
742 { }
743
744 protected:
745 // This is called to set the address and file offset. Here we make
746 // sure that the Stringpool is finalized.
747 void
748 set_final_data_size();
749
750 // Write out the data.
751 void
752 do_write(Output_file*);
753
754 // Write the data to a buffer.
755 void
756 do_write_to_buffer(unsigned char* buffer)
757 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
758
759 private:
760 Stringpool* strtab_;
761 };
762
763 // This POD class is used to represent a single reloc in the output
764 // file. This could be a private class within Output_data_reloc, but
765 // the templatization is complex enough that I broke it out into a
766 // separate class. The class is templatized on either elfcpp::SHT_REL
767 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
768 // relocation or an ordinary relocation.
769
770 // A relocation can be against a global symbol, a local symbol, a
771 // local section symbol, an output section, or the undefined symbol at
772 // index 0. We represent the latter by using a NULL global symbol.
773
774 template<int sh_type, bool dynamic, int size, bool big_endian>
775 class Output_reloc;
776
777 template<bool dynamic, int size, bool big_endian>
778 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
779 {
780 public:
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
782
783 // An uninitialized entry. We need this because we want to put
784 // instances of this class into an STL container.
785 Output_reloc()
786 : local_sym_index_(INVALID_CODE)
787 { }
788
789 // We have a bunch of different constructors. They come in pairs
790 // depending on how the address of the relocation is specified. It
791 // can either be an offset in an Output_data or an offset in an
792 // input section.
793
794 // A reloc against a global symbol.
795
796 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
797 Address address, bool is_relative);
798
799 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
800 unsigned int shndx, Address address, bool is_relative);
801
802 // A reloc against a local symbol or local section symbol.
803
804 Output_reloc(Sized_relobj<size, big_endian>* relobj,
805 unsigned int local_sym_index, unsigned int type,
806 Output_data* od, Address address, bool is_relative,
807 bool is_section_symbol);
808
809 Output_reloc(Sized_relobj<size, big_endian>* relobj,
810 unsigned int local_sym_index, unsigned int type,
811 unsigned int shndx, Address address, bool is_relative,
812 bool is_section_symbol);
813
814 // A reloc against the STT_SECTION symbol of an output section.
815
816 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
817 Address address);
818
819 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
820 unsigned int shndx, Address address);
821
822 // Return TRUE if this is a RELATIVE relocation.
823 bool
824 is_relative() const
825 { return this->is_relative_; }
826
827 // Return whether this is against a local section symbol.
828 bool
829 is_local_section_symbol() const
830 {
831 return (this->local_sym_index_ != GSYM_CODE
832 && this->local_sym_index_ != SECTION_CODE
833 && this->local_sym_index_ != INVALID_CODE
834 && this->is_section_symbol_);
835 }
836
837 // For a local section symbol, return the offset of the input
838 // section within the output section.
839 section_offset_type
840 local_section_offset() const;
841
842 // Get the value of the symbol referred to by a Rel relocation when
843 // we are adding the given ADDEND.
844 Address
845 symbol_value(Address addend) const;
846
847 // Write the reloc entry to an output view.
848 void
849 write(unsigned char* pov) const;
850
851 // Write the offset and info fields to Write_rel.
852 template<typename Write_rel>
853 void write_rel(Write_rel*) const;
854
855 private:
856 // Record that we need a dynamic symbol index.
857 void
858 set_needs_dynsym_index();
859
860 // Return the symbol index.
861 unsigned int
862 get_symbol_index() const;
863
864 // Codes for local_sym_index_.
865 enum
866 {
867 // Global symbol.
868 GSYM_CODE = -1U,
869 // Output section.
870 SECTION_CODE = -2U,
871 // Invalid uninitialized entry.
872 INVALID_CODE = -3U
873 };
874
875 union
876 {
877 // For a local symbol or local section symbol
878 // (this->local_sym_index_ >= 0), the object. We will never
879 // generate a relocation against a local symbol in a dynamic
880 // object; that doesn't make sense. And our callers will always
881 // be templatized, so we use Sized_relobj here.
882 Sized_relobj<size, big_endian>* relobj;
883 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
884 // symbol. If this is NULL, it indicates a relocation against the
885 // undefined 0 symbol.
886 Symbol* gsym;
887 // For a relocation against an output section
888 // (this->local_sym_index_ == SECTION_CODE), the output section.
889 Output_section* os;
890 } u1_;
891 union
892 {
893 // If this->shndx_ is not INVALID CODE, the object which holds the
894 // input section being used to specify the reloc address.
895 Relobj* relobj;
896 // If this->shndx_ is INVALID_CODE, the output data being used to
897 // specify the reloc address. This may be NULL if the reloc
898 // address is absolute.
899 Output_data* od;
900 } u2_;
901 // The address offset within the input section or the Output_data.
902 Address address_;
903 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
904 // relocation against an output section, or INVALID_CODE for an
905 // uninitialized value. Otherwise, for a local symbol
906 // (this->is_section_symbol_ is false), the local symbol index. For
907 // a local section symbol (this->is_section_symbol_ is true), the
908 // section index in the input file.
909 unsigned int local_sym_index_;
910 // The reloc type--a processor specific code.
911 unsigned int type_ : 30;
912 // True if the relocation is a RELATIVE relocation.
913 bool is_relative_ : 1;
914 // True if the relocation is against a section symbol.
915 bool is_section_symbol_ : 1;
916 // If the reloc address is an input section in an object, the
917 // section index. This is INVALID_CODE if the reloc address is
918 // specified in some other way.
919 unsigned int shndx_;
920 };
921
922 // The SHT_RELA version of Output_reloc<>. This is just derived from
923 // the SHT_REL version of Output_reloc, but it adds an addend.
924
925 template<bool dynamic, int size, bool big_endian>
926 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
927 {
928 public:
929 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
930 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
931
932 // An uninitialized entry.
933 Output_reloc()
934 : rel_()
935 { }
936
937 // A reloc against a global symbol.
938
939 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
940 Address address, Addend addend, bool is_relative)
941 : rel_(gsym, type, od, address, is_relative), addend_(addend)
942 { }
943
944 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
945 unsigned int shndx, Address address, Addend addend,
946 bool is_relative)
947 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
948 { }
949
950 // A reloc against a local symbol.
951
952 Output_reloc(Sized_relobj<size, big_endian>* relobj,
953 unsigned int local_sym_index, unsigned int type,
954 Output_data* od, Address address,
955 Addend addend, bool is_relative, bool is_section_symbol)
956 : rel_(relobj, local_sym_index, type, od, address, is_relative,
957 is_section_symbol),
958 addend_(addend)
959 { }
960
961 Output_reloc(Sized_relobj<size, big_endian>* relobj,
962 unsigned int local_sym_index, unsigned int type,
963 unsigned int shndx, Address address,
964 Addend addend, bool is_relative, bool is_section_symbol)
965 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
966 is_section_symbol),
967 addend_(addend)
968 { }
969
970 // A reloc against the STT_SECTION symbol of an output section.
971
972 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
973 Address address, Addend addend)
974 : rel_(os, type, od, address), addend_(addend)
975 { }
976
977 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
978 unsigned int shndx, Address address, Addend addend)
979 : rel_(os, type, relobj, shndx, address), addend_(addend)
980 { }
981
982 // Write the reloc entry to an output view.
983 void
984 write(unsigned char* pov) const;
985
986 private:
987 // The basic reloc.
988 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
989 // The addend.
990 Addend addend_;
991 };
992
993 // Output_data_reloc is used to manage a section containing relocs.
994 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
995 // indicates whether this is a dynamic relocation or a normal
996 // relocation. Output_data_reloc_base is a base class.
997 // Output_data_reloc is the real class, which we specialize based on
998 // the reloc type.
999
1000 template<int sh_type, bool dynamic, int size, bool big_endian>
1001 class Output_data_reloc_base : public Output_section_data_build
1002 {
1003 public:
1004 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1005 typedef typename Output_reloc_type::Address Address;
1006 static const int reloc_size =
1007 Reloc_types<sh_type, size, big_endian>::reloc_size;
1008
1009 // Construct the section.
1010 Output_data_reloc_base()
1011 : Output_section_data_build(Output_data::default_alignment_for_size(size))
1012 { }
1013
1014 protected:
1015 // Write out the data.
1016 void
1017 do_write(Output_file*);
1018
1019 // Set the entry size and the link.
1020 void
1021 do_adjust_output_section(Output_section *os);
1022
1023 // Add a relocation entry.
1024 void
1025 add(Output_data *od, const Output_reloc_type& reloc)
1026 {
1027 this->relocs_.push_back(reloc);
1028 this->set_current_data_size(this->relocs_.size() * reloc_size);
1029 od->add_dynamic_reloc();
1030 }
1031
1032 private:
1033 typedef std::vector<Output_reloc_type> Relocs;
1034
1035 Relocs relocs_;
1036 };
1037
1038 // The class which callers actually create.
1039
1040 template<int sh_type, bool dynamic, int size, bool big_endian>
1041 class Output_data_reloc;
1042
1043 // The SHT_REL version of Output_data_reloc.
1044
1045 template<bool dynamic, int size, bool big_endian>
1046 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1047 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1048 {
1049 private:
1050 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1051 big_endian> Base;
1052
1053 public:
1054 typedef typename Base::Output_reloc_type Output_reloc_type;
1055 typedef typename Output_reloc_type::Address Address;
1056
1057 Output_data_reloc()
1058 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1059 { }
1060
1061 // Add a reloc against a global symbol.
1062
1063 void
1064 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1065 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1066
1067 void
1068 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1069 unsigned int shndx, Address address)
1070 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1071 false)); }
1072
1073 // Add a RELATIVE reloc against a global symbol. The final relocation
1074 // will not reference the symbol.
1075
1076 void
1077 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1078 Address address)
1079 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1080
1081 void
1082 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1083 Relobj* relobj, unsigned int shndx, Address address)
1084 {
1085 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1086 true));
1087 }
1088
1089 // Add a reloc against a local symbol.
1090
1091 void
1092 add_local(Sized_relobj<size, big_endian>* relobj,
1093 unsigned int local_sym_index, unsigned int type,
1094 Output_data* od, Address address)
1095 {
1096 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1097 address, false, false));
1098 }
1099
1100 void
1101 add_local(Sized_relobj<size, big_endian>* relobj,
1102 unsigned int local_sym_index, unsigned int type,
1103 Output_data* od, unsigned int shndx, Address address)
1104 {
1105 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1106 address, false, false));
1107 }
1108
1109 // Add a RELATIVE reloc against a local symbol.
1110
1111 void
1112 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1113 unsigned int local_sym_index, unsigned int type,
1114 Output_data* od, Address address)
1115 {
1116 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1117 address, true, false));
1118 }
1119
1120 void
1121 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1122 unsigned int local_sym_index, unsigned int type,
1123 Output_data* od, unsigned int shndx, Address address)
1124 {
1125 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1126 address, true, false));
1127 }
1128
1129 // Add a reloc against a local section symbol. This will be
1130 // converted into a reloc against the STT_SECTION symbol of the
1131 // output section.
1132
1133 void
1134 add_local_section(Sized_relobj<size, big_endian>* relobj,
1135 unsigned int input_shndx, unsigned int type,
1136 Output_data* od, Address address)
1137 {
1138 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1139 address, false, true));
1140 }
1141
1142 void
1143 add_local_section(Sized_relobj<size, big_endian>* relobj,
1144 unsigned int input_shndx, unsigned int type,
1145 Output_data* od, unsigned int shndx, Address address)
1146 {
1147 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1148 address, false, true));
1149 }
1150
1151 // A reloc against the STT_SECTION symbol of an output section.
1152 // OS is the Output_section that the relocation refers to; OD is
1153 // the Output_data object being relocated.
1154
1155 void
1156 add_output_section(Output_section* os, unsigned int type,
1157 Output_data* od, Address address)
1158 { this->add(od, Output_reloc_type(os, type, od, address)); }
1159
1160 void
1161 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1162 Relobj* relobj, unsigned int shndx, Address address)
1163 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1164 };
1165
1166 // The SHT_RELA version of Output_data_reloc.
1167
1168 template<bool dynamic, int size, bool big_endian>
1169 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1170 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1171 {
1172 private:
1173 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1174 big_endian> Base;
1175
1176 public:
1177 typedef typename Base::Output_reloc_type Output_reloc_type;
1178 typedef typename Output_reloc_type::Address Address;
1179 typedef typename Output_reloc_type::Addend Addend;
1180
1181 Output_data_reloc()
1182 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1183 { }
1184
1185 // Add a reloc against a global symbol.
1186
1187 void
1188 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1189 Address address, Addend addend)
1190 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1191 false)); }
1192
1193 void
1194 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1195 unsigned int shndx, Address address,
1196 Addend addend)
1197 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1198 addend, false)); }
1199
1200 // Add a RELATIVE reloc against a global symbol. The final output
1201 // relocation will not reference the symbol, but we must keep the symbol
1202 // information long enough to set the addend of the relocation correctly
1203 // when it is written.
1204
1205 void
1206 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1207 Address address, Addend addend)
1208 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1209
1210 void
1211 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1212 Relobj* relobj, unsigned int shndx, Address address,
1213 Addend addend)
1214 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1215 addend, true)); }
1216
1217 // Add a reloc against a local symbol.
1218
1219 void
1220 add_local(Sized_relobj<size, big_endian>* relobj,
1221 unsigned int local_sym_index, unsigned int type,
1222 Output_data* od, Address address, Addend addend)
1223 {
1224 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1225 addend, false, false));
1226 }
1227
1228 void
1229 add_local(Sized_relobj<size, big_endian>* relobj,
1230 unsigned int local_sym_index, unsigned int type,
1231 Output_data* od, unsigned int shndx, Address address,
1232 Addend addend)
1233 {
1234 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1235 address, addend, false, false));
1236 }
1237
1238 // Add a RELATIVE reloc against a local symbol.
1239
1240 void
1241 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1242 unsigned int local_sym_index, unsigned int type,
1243 Output_data* od, Address address, Addend addend)
1244 {
1245 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1246 addend, true, false));
1247 }
1248
1249 void
1250 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1251 unsigned int local_sym_index, unsigned int type,
1252 Output_data* od, unsigned int shndx, Address address,
1253 Addend addend)
1254 {
1255 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1256 address, addend, true, false));
1257 }
1258
1259 // Add a reloc against a local section symbol. This will be
1260 // converted into a reloc against the STT_SECTION symbol of the
1261 // output section.
1262
1263 void
1264 add_local_section(Sized_relobj<size, big_endian>* relobj,
1265 unsigned int input_shndx, unsigned int type,
1266 Output_data* od, Address address, Addend addend)
1267 {
1268 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1269 addend, false, true));
1270 }
1271
1272 void
1273 add_local_section(Sized_relobj<size, big_endian>* relobj,
1274 unsigned int input_shndx, unsigned int type,
1275 Output_data* od, unsigned int shndx, Address address,
1276 Addend addend)
1277 {
1278 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1279 address, addend, false, true));
1280 }
1281
1282 // A reloc against the STT_SECTION symbol of an output section.
1283
1284 void
1285 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1286 Address address, Addend addend)
1287 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1288
1289 void
1290 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1291 unsigned int shndx, Address address, Addend addend)
1292 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1293 addend)); }
1294 };
1295
1296 // Output_relocatable_relocs represents a relocation section in a
1297 // relocatable link. The actual data is written out in the target
1298 // hook relocate_for_relocatable. This just saves space for it.
1299
1300 template<int sh_type, int size, bool big_endian>
1301 class Output_relocatable_relocs : public Output_section_data
1302 {
1303 public:
1304 Output_relocatable_relocs(Relocatable_relocs* rr)
1305 : Output_section_data(Output_data::default_alignment_for_size(size)),
1306 rr_(rr)
1307 { }
1308
1309 void
1310 set_final_data_size();
1311
1312 // Write out the data. There is nothing to do here.
1313 void
1314 do_write(Output_file*)
1315 { }
1316
1317 private:
1318 // The relocs associated with this input section.
1319 Relocatable_relocs* rr_;
1320 };
1321
1322 // Handle a GROUP section.
1323
1324 template<int size, bool big_endian>
1325 class Output_data_group : public Output_section_data
1326 {
1327 public:
1328 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1329 section_size_type entry_count,
1330 const elfcpp::Elf_Word* contents);
1331
1332 void
1333 do_write(Output_file*);
1334
1335 private:
1336 // The input object.
1337 Sized_relobj<size, big_endian>* relobj_;
1338 // The group flag word.
1339 elfcpp::Elf_Word flags_;
1340 // The section indexes of the input sections in this group.
1341 std::vector<unsigned int> input_sections_;
1342 };
1343
1344 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1345 // for one symbol--either a global symbol or a local symbol in an
1346 // object. The target specific code adds entries to the GOT as
1347 // needed.
1348
1349 template<int size, bool big_endian>
1350 class Output_data_got : public Output_section_data_build
1351 {
1352 public:
1353 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1354 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1355 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1356
1357 Output_data_got()
1358 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1359 entries_()
1360 { }
1361
1362 // Add an entry for a global symbol to the GOT. Return true if this
1363 // is a new GOT entry, false if the symbol was already in the GOT.
1364 bool
1365 add_global(Symbol* gsym, unsigned int got_type);
1366
1367 // Add an entry for a global symbol to the GOT, and add a dynamic
1368 // relocation of type R_TYPE for the GOT entry.
1369 void
1370 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1371 Rel_dyn* rel_dyn, unsigned int r_type);
1372
1373 void
1374 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1375 Rela_dyn* rela_dyn, unsigned int r_type);
1376
1377 // Add a pair of entries for a global symbol to the GOT, and add
1378 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1379 void
1380 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1381 Rel_dyn* rel_dyn, unsigned int r_type_1,
1382 unsigned int r_type_2);
1383
1384 void
1385 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1386 Rela_dyn* rela_dyn, unsigned int r_type_1,
1387 unsigned int r_type_2);
1388
1389 // Add an entry for a local symbol to the GOT. This returns true if
1390 // this is a new GOT entry, false if the symbol already has a GOT
1391 // entry.
1392 bool
1393 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1394 unsigned int got_type);
1395
1396 // Add an entry for a local symbol to the GOT, and add a dynamic
1397 // relocation of type R_TYPE for the GOT entry.
1398 void
1399 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1400 unsigned int sym_index, unsigned int got_type,
1401 Rel_dyn* rel_dyn, unsigned int r_type);
1402
1403 void
1404 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1405 unsigned int sym_index, unsigned int got_type,
1406 Rela_dyn* rela_dyn, unsigned int r_type);
1407
1408 // Add a pair of entries for a local symbol to the GOT, and add
1409 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1410 void
1411 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1412 unsigned int sym_index, unsigned int shndx,
1413 unsigned int got_type, Rel_dyn* rel_dyn,
1414 unsigned int r_type_1, unsigned int r_type_2);
1415
1416 void
1417 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1418 unsigned int sym_index, unsigned int shndx,
1419 unsigned int got_type, Rela_dyn* rela_dyn,
1420 unsigned int r_type_1, unsigned int r_type_2);
1421
1422 // Add a constant to the GOT. This returns the offset of the new
1423 // entry from the start of the GOT.
1424 unsigned int
1425 add_constant(Valtype constant)
1426 {
1427 this->entries_.push_back(Got_entry(constant));
1428 this->set_got_size();
1429 return this->last_got_offset();
1430 }
1431
1432 protected:
1433 // Write out the GOT table.
1434 void
1435 do_write(Output_file*);
1436
1437 private:
1438 // This POD class holds a single GOT entry.
1439 class Got_entry
1440 {
1441 public:
1442 // Create a zero entry.
1443 Got_entry()
1444 : local_sym_index_(CONSTANT_CODE)
1445 { this->u_.constant = 0; }
1446
1447 // Create a global symbol entry.
1448 explicit Got_entry(Symbol* gsym)
1449 : local_sym_index_(GSYM_CODE)
1450 { this->u_.gsym = gsym; }
1451
1452 // Create a local symbol entry.
1453 Got_entry(Sized_relobj<size, big_endian>* object,
1454 unsigned int local_sym_index)
1455 : local_sym_index_(local_sym_index)
1456 {
1457 gold_assert(local_sym_index != GSYM_CODE
1458 && local_sym_index != CONSTANT_CODE);
1459 this->u_.object = object;
1460 }
1461
1462 // Create a constant entry. The constant is a host value--it will
1463 // be swapped, if necessary, when it is written out.
1464 explicit Got_entry(Valtype constant)
1465 : local_sym_index_(CONSTANT_CODE)
1466 { this->u_.constant = constant; }
1467
1468 // Write the GOT entry to an output view.
1469 void
1470 write(unsigned char* pov) const;
1471
1472 private:
1473 enum
1474 {
1475 GSYM_CODE = -1U,
1476 CONSTANT_CODE = -2U
1477 };
1478
1479 union
1480 {
1481 // For a local symbol, the object.
1482 Sized_relobj<size, big_endian>* object;
1483 // For a global symbol, the symbol.
1484 Symbol* gsym;
1485 // For a constant, the constant.
1486 Valtype constant;
1487 } u_;
1488 // For a local symbol, the local symbol index. This is GSYM_CODE
1489 // for a global symbol, or CONSTANT_CODE for a constant.
1490 unsigned int local_sym_index_;
1491 };
1492
1493 typedef std::vector<Got_entry> Got_entries;
1494
1495 // Return the offset into the GOT of GOT entry I.
1496 unsigned int
1497 got_offset(unsigned int i) const
1498 { return i * (size / 8); }
1499
1500 // Return the offset into the GOT of the last entry added.
1501 unsigned int
1502 last_got_offset() const
1503 { return this->got_offset(this->entries_.size() - 1); }
1504
1505 // Set the size of the section.
1506 void
1507 set_got_size()
1508 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1509
1510 // The list of GOT entries.
1511 Got_entries entries_;
1512 };
1513
1514 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1515 // section.
1516
1517 class Output_data_dynamic : public Output_section_data
1518 {
1519 public:
1520 Output_data_dynamic(Stringpool* pool)
1521 : Output_section_data(Output_data::default_alignment()),
1522 entries_(), pool_(pool)
1523 { }
1524
1525 // Add a new dynamic entry with a fixed numeric value.
1526 void
1527 add_constant(elfcpp::DT tag, unsigned int val)
1528 { this->add_entry(Dynamic_entry(tag, val)); }
1529
1530 // Add a new dynamic entry with the address of output data.
1531 void
1532 add_section_address(elfcpp::DT tag, const Output_data* od)
1533 { this->add_entry(Dynamic_entry(tag, od, false)); }
1534
1535 // Add a new dynamic entry with the size of output data.
1536 void
1537 add_section_size(elfcpp::DT tag, const Output_data* od)
1538 { this->add_entry(Dynamic_entry(tag, od, true)); }
1539
1540 // Add a new dynamic entry with the address of a symbol.
1541 void
1542 add_symbol(elfcpp::DT tag, const Symbol* sym)
1543 { this->add_entry(Dynamic_entry(tag, sym)); }
1544
1545 // Add a new dynamic entry with a string.
1546 void
1547 add_string(elfcpp::DT tag, const char* str)
1548 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1549
1550 void
1551 add_string(elfcpp::DT tag, const std::string& str)
1552 { this->add_string(tag, str.c_str()); }
1553
1554 protected:
1555 // Adjust the output section to set the entry size.
1556 void
1557 do_adjust_output_section(Output_section*);
1558
1559 // Set the final data size.
1560 void
1561 set_final_data_size();
1562
1563 // Write out the dynamic entries.
1564 void
1565 do_write(Output_file*);
1566
1567 private:
1568 // This POD class holds a single dynamic entry.
1569 class Dynamic_entry
1570 {
1571 public:
1572 // Create an entry with a fixed numeric value.
1573 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1574 : tag_(tag), classification_(DYNAMIC_NUMBER)
1575 { this->u_.val = val; }
1576
1577 // Create an entry with the size or address of a section.
1578 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1579 : tag_(tag),
1580 classification_(section_size
1581 ? DYNAMIC_SECTION_SIZE
1582 : DYNAMIC_SECTION_ADDRESS)
1583 { this->u_.od = od; }
1584
1585 // Create an entry with the address of a symbol.
1586 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1587 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1588 { this->u_.sym = sym; }
1589
1590 // Create an entry with a string.
1591 Dynamic_entry(elfcpp::DT tag, const char* str)
1592 : tag_(tag), classification_(DYNAMIC_STRING)
1593 { this->u_.str = str; }
1594
1595 // Write the dynamic entry to an output view.
1596 template<int size, bool big_endian>
1597 void
1598 write(unsigned char* pov, const Stringpool*) const;
1599
1600 private:
1601 enum Classification
1602 {
1603 // Number.
1604 DYNAMIC_NUMBER,
1605 // Section address.
1606 DYNAMIC_SECTION_ADDRESS,
1607 // Section size.
1608 DYNAMIC_SECTION_SIZE,
1609 // Symbol adress.
1610 DYNAMIC_SYMBOL,
1611 // String.
1612 DYNAMIC_STRING
1613 };
1614
1615 union
1616 {
1617 // For DYNAMIC_NUMBER.
1618 unsigned int val;
1619 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1620 const Output_data* od;
1621 // For DYNAMIC_SYMBOL.
1622 const Symbol* sym;
1623 // For DYNAMIC_STRING.
1624 const char* str;
1625 } u_;
1626 // The dynamic tag.
1627 elfcpp::DT tag_;
1628 // The type of entry.
1629 Classification classification_;
1630 };
1631
1632 // Add an entry to the list.
1633 void
1634 add_entry(const Dynamic_entry& entry)
1635 { this->entries_.push_back(entry); }
1636
1637 // Sized version of write function.
1638 template<int size, bool big_endian>
1639 void
1640 sized_write(Output_file* of);
1641
1642 // The type of the list of entries.
1643 typedef std::vector<Dynamic_entry> Dynamic_entries;
1644
1645 // The entries.
1646 Dynamic_entries entries_;
1647 // The pool used for strings.
1648 Stringpool* pool_;
1649 };
1650
1651 // An output section. We don't expect to have too many output
1652 // sections, so we don't bother to do a template on the size.
1653
1654 class Output_section : public Output_data
1655 {
1656 public:
1657 // Create an output section, giving the name, type, and flags.
1658 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1659 virtual ~Output_section();
1660
1661 // Add a new input section SHNDX, named NAME, with header SHDR, from
1662 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1663 // which applies to this section, or 0 if none, or -1U if more than
1664 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1665 // in a linker script; in that case we need to keep track of input
1666 // sections associated with an output section. Return the offset
1667 // within the output section.
1668 template<int size, bool big_endian>
1669 off_t
1670 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1671 const char *name,
1672 const elfcpp::Shdr<size, big_endian>& shdr,
1673 unsigned int reloc_shndx, bool have_sections_script);
1674
1675 // Add generated data POSD to this output section.
1676 void
1677 add_output_section_data(Output_section_data* posd);
1678
1679 // Return the section name.
1680 const char*
1681 name() const
1682 { return this->name_; }
1683
1684 // Return the section type.
1685 elfcpp::Elf_Word
1686 type() const
1687 { return this->type_; }
1688
1689 // Return the section flags.
1690 elfcpp::Elf_Xword
1691 flags() const
1692 { return this->flags_; }
1693
1694 // Set the section flags. This may only be used with the Layout
1695 // code when it is prepared to move the section to a different
1696 // segment.
1697 void
1698 set_flags(elfcpp::Elf_Xword flags)
1699 { this->flags_ = flags; }
1700
1701 // Return the entsize field.
1702 uint64_t
1703 entsize() const
1704 { return this->entsize_; }
1705
1706 // Set the entsize field.
1707 void
1708 set_entsize(uint64_t v);
1709
1710 // Set the load address.
1711 void
1712 set_load_address(uint64_t load_address)
1713 {
1714 this->load_address_ = load_address;
1715 this->has_load_address_ = true;
1716 }
1717
1718 // Set the link field to the output section index of a section.
1719 void
1720 set_link_section(const Output_data* od)
1721 {
1722 gold_assert(this->link_ == 0
1723 && !this->should_link_to_symtab_
1724 && !this->should_link_to_dynsym_);
1725 this->link_section_ = od;
1726 }
1727
1728 // Set the link field to a constant.
1729 void
1730 set_link(unsigned int v)
1731 {
1732 gold_assert(this->link_section_ == NULL
1733 && !this->should_link_to_symtab_
1734 && !this->should_link_to_dynsym_);
1735 this->link_ = v;
1736 }
1737
1738 // Record that this section should link to the normal symbol table.
1739 void
1740 set_should_link_to_symtab()
1741 {
1742 gold_assert(this->link_section_ == NULL
1743 && this->link_ == 0
1744 && !this->should_link_to_dynsym_);
1745 this->should_link_to_symtab_ = true;
1746 }
1747
1748 // Record that this section should link to the dynamic symbol table.
1749 void
1750 set_should_link_to_dynsym()
1751 {
1752 gold_assert(this->link_section_ == NULL
1753 && this->link_ == 0
1754 && !this->should_link_to_symtab_);
1755 this->should_link_to_dynsym_ = true;
1756 }
1757
1758 // Return the info field.
1759 unsigned int
1760 info() const
1761 {
1762 gold_assert(this->info_section_ == NULL
1763 && this->info_symndx_ == NULL);
1764 return this->info_;
1765 }
1766
1767 // Set the info field to the output section index of a section.
1768 void
1769 set_info_section(const Output_section* os)
1770 {
1771 gold_assert((this->info_section_ == NULL
1772 || (this->info_section_ == os
1773 && this->info_uses_section_index_))
1774 && this->info_symndx_ == NULL
1775 && this->info_ == 0);
1776 this->info_section_ = os;
1777 this->info_uses_section_index_= true;
1778 }
1779
1780 // Set the info field to the symbol table index of a symbol.
1781 void
1782 set_info_symndx(const Symbol* sym)
1783 {
1784 gold_assert(this->info_section_ == NULL
1785 && (this->info_symndx_ == NULL
1786 || this->info_symndx_ == sym)
1787 && this->info_ == 0);
1788 this->info_symndx_ = sym;
1789 }
1790
1791 // Set the info field to the symbol table index of a section symbol.
1792 void
1793 set_info_section_symndx(const Output_section* os)
1794 {
1795 gold_assert((this->info_section_ == NULL
1796 || (this->info_section_ == os
1797 && !this->info_uses_section_index_))
1798 && this->info_symndx_ == NULL
1799 && this->info_ == 0);
1800 this->info_section_ = os;
1801 this->info_uses_section_index_ = false;
1802 }
1803
1804 // Set the info field to a constant.
1805 void
1806 set_info(unsigned int v)
1807 {
1808 gold_assert(this->info_section_ == NULL
1809 && this->info_symndx_ == NULL
1810 && (this->info_ == 0
1811 || this->info_ == v));
1812 this->info_ = v;
1813 }
1814
1815 // Set the addralign field.
1816 void
1817 set_addralign(uint64_t v)
1818 { this->addralign_ = v; }
1819
1820 // Indicate that we need a symtab index.
1821 void
1822 set_needs_symtab_index()
1823 { this->needs_symtab_index_ = true; }
1824
1825 // Return whether we need a symtab index.
1826 bool
1827 needs_symtab_index() const
1828 { return this->needs_symtab_index_; }
1829
1830 // Get the symtab index.
1831 unsigned int
1832 symtab_index() const
1833 {
1834 gold_assert(this->symtab_index_ != 0);
1835 return this->symtab_index_;
1836 }
1837
1838 // Set the symtab index.
1839 void
1840 set_symtab_index(unsigned int index)
1841 {
1842 gold_assert(index != 0);
1843 this->symtab_index_ = index;
1844 }
1845
1846 // Indicate that we need a dynsym index.
1847 void
1848 set_needs_dynsym_index()
1849 { this->needs_dynsym_index_ = true; }
1850
1851 // Return whether we need a dynsym index.
1852 bool
1853 needs_dynsym_index() const
1854 { return this->needs_dynsym_index_; }
1855
1856 // Get the dynsym index.
1857 unsigned int
1858 dynsym_index() const
1859 {
1860 gold_assert(this->dynsym_index_ != 0);
1861 return this->dynsym_index_;
1862 }
1863
1864 // Set the dynsym index.
1865 void
1866 set_dynsym_index(unsigned int index)
1867 {
1868 gold_assert(index != 0);
1869 this->dynsym_index_ = index;
1870 }
1871
1872 // Return whether this section should be written after all the input
1873 // sections are complete.
1874 bool
1875 after_input_sections() const
1876 { return this->after_input_sections_; }
1877
1878 // Record that this section should be written after all the input
1879 // sections are complete.
1880 void
1881 set_after_input_sections()
1882 { this->after_input_sections_ = true; }
1883
1884 // Return whether this section requires postprocessing after all
1885 // relocations have been applied.
1886 bool
1887 requires_postprocessing() const
1888 { return this->requires_postprocessing_; }
1889
1890 // If a section requires postprocessing, return the buffer to use.
1891 unsigned char*
1892 postprocessing_buffer() const
1893 {
1894 gold_assert(this->postprocessing_buffer_ != NULL);
1895 return this->postprocessing_buffer_;
1896 }
1897
1898 // If a section requires postprocessing, create the buffer to use.
1899 void
1900 create_postprocessing_buffer();
1901
1902 // If a section requires postprocessing, this is the size of the
1903 // buffer to which relocations should be applied.
1904 off_t
1905 postprocessing_buffer_size() const
1906 { return this->current_data_size_for_child(); }
1907
1908 // Modify the section name. This is only permitted for an
1909 // unallocated section, and only before the size has been finalized.
1910 // Otherwise the name will not get into Layout::namepool_.
1911 void
1912 set_name(const char* newname)
1913 {
1914 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1915 gold_assert(!this->is_data_size_valid());
1916 this->name_ = newname;
1917 }
1918
1919 // Return whether the offset OFFSET in the input section SHNDX in
1920 // object OBJECT is being included in the link.
1921 bool
1922 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1923 off_t offset) const;
1924
1925 // Return the offset within the output section of OFFSET relative to
1926 // the start of input section SHNDX in object OBJECT.
1927 section_offset_type
1928 output_offset(const Relobj* object, unsigned int shndx,
1929 section_offset_type offset) const;
1930
1931 // Return the output virtual address of OFFSET relative to the start
1932 // of input section SHNDX in object OBJECT.
1933 uint64_t
1934 output_address(const Relobj* object, unsigned int shndx,
1935 off_t offset) const;
1936
1937 // Return the output address of the start of the merged section for
1938 // input section SHNDX in object OBJECT. This is not necessarily
1939 // the offset corresponding to input offset 0 in the section, since
1940 // the section may be mapped arbitrarily.
1941 uint64_t
1942 starting_output_address(const Relobj* object, unsigned int shndx) const;
1943
1944 // Record that this output section was found in the SECTIONS clause
1945 // of a linker script.
1946 void
1947 set_found_in_sections_clause()
1948 { this->found_in_sections_clause_ = true; }
1949
1950 // Return whether this output section was found in the SECTIONS
1951 // clause of a linker script.
1952 bool
1953 found_in_sections_clause() const
1954 { return this->found_in_sections_clause_; }
1955
1956 // Write the section header into *OPHDR.
1957 template<int size, bool big_endian>
1958 void
1959 write_header(const Layout*, const Stringpool*,
1960 elfcpp::Shdr_write<size, big_endian>*) const;
1961
1962 // The next few calls are for linker script support.
1963
1964 // Store the list of input sections for this Output_section into the
1965 // list passed in. This removes the input sections, leaving only
1966 // any Output_section_data elements. This returns the size of those
1967 // Output_section_data elements. ADDRESS is the address of this
1968 // output section. FILL is the fill value to use, in case there are
1969 // any spaces between the remaining Output_section_data elements.
1970 uint64_t
1971 get_input_sections(uint64_t address, const std::string& fill,
1972 std::list<std::pair<Relobj*, unsigned int > >*);
1973
1974 // Add an input section from a script.
1975 void
1976 add_input_section_for_script(Relobj* object, unsigned int shndx,
1977 off_t data_size, uint64_t addralign);
1978
1979 // Set the current size of the output section.
1980 void
1981 set_current_data_size(off_t size)
1982 { this->set_current_data_size_for_child(size); }
1983
1984 // Get the current size of the output section.
1985 off_t
1986 current_data_size() const
1987 { return this->current_data_size_for_child(); }
1988
1989 // End of linker script support.
1990
1991 // Print merge statistics to stderr.
1992 void
1993 print_merge_stats();
1994
1995 protected:
1996 // Return the output section--i.e., the object itself.
1997 Output_section*
1998 do_output_section()
1999 { return this; }
2000
2001 // Return the section index in the output file.
2002 unsigned int
2003 do_out_shndx() const
2004 {
2005 gold_assert(this->out_shndx_ != -1U);
2006 return this->out_shndx_;
2007 }
2008
2009 // Set the output section index.
2010 void
2011 do_set_out_shndx(unsigned int shndx)
2012 {
2013 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2014 this->out_shndx_ = shndx;
2015 }
2016
2017 // Set the final data size of the Output_section. For a typical
2018 // Output_section, there is nothing to do, but if there are any
2019 // Output_section_data objects we need to set their final addresses
2020 // here.
2021 virtual void
2022 set_final_data_size();
2023
2024 // Reset the address and file offset.
2025 void
2026 do_reset_address_and_file_offset();
2027
2028 // Write the data to the file. For a typical Output_section, this
2029 // does nothing: the data is written out by calling Object::Relocate
2030 // on each input object. But if there are any Output_section_data
2031 // objects we do need to write them out here.
2032 virtual void
2033 do_write(Output_file*);
2034
2035 // Return the address alignment--function required by parent class.
2036 uint64_t
2037 do_addralign() const
2038 { return this->addralign_; }
2039
2040 // Return whether there is a load address.
2041 bool
2042 do_has_load_address() const
2043 { return this->has_load_address_; }
2044
2045 // Return the load address.
2046 uint64_t
2047 do_load_address() const
2048 {
2049 gold_assert(this->has_load_address_);
2050 return this->load_address_;
2051 }
2052
2053 // Return whether this is an Output_section.
2054 bool
2055 do_is_section() const
2056 { return true; }
2057
2058 // Return whether this is a section of the specified type.
2059 bool
2060 do_is_section_type(elfcpp::Elf_Word type) const
2061 { return this->type_ == type; }
2062
2063 // Return whether the specified section flag is set.
2064 bool
2065 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2066 { return (this->flags_ & flag) != 0; }
2067
2068 // Set the TLS offset. Called only for SHT_TLS sections.
2069 void
2070 do_set_tls_offset(uint64_t tls_base);
2071
2072 // Return the TLS offset, relative to the base of the TLS segment.
2073 // Valid only for SHT_TLS sections.
2074 uint64_t
2075 do_tls_offset() const
2076 { return this->tls_offset_; }
2077
2078 // This may be implemented by a child class.
2079 virtual void
2080 do_finalize_name(Layout*)
2081 { }
2082
2083 // Record that this section requires postprocessing after all
2084 // relocations have been applied. This is called by a child class.
2085 void
2086 set_requires_postprocessing()
2087 {
2088 this->requires_postprocessing_ = true;
2089 this->after_input_sections_ = true;
2090 }
2091
2092 // Write all the data of an Output_section into the postprocessing
2093 // buffer.
2094 void
2095 write_to_postprocessing_buffer();
2096
2097 private:
2098 // In some cases we need to keep a list of the input sections
2099 // associated with this output section. We only need the list if we
2100 // might have to change the offsets of the input section within the
2101 // output section after we add the input section. The ordinary
2102 // input sections will be written out when we process the object
2103 // file, and as such we don't need to track them here. We do need
2104 // to track Output_section_data objects here. We store instances of
2105 // this structure in a std::vector, so it must be a POD. There can
2106 // be many instances of this structure, so we use a union to save
2107 // some space.
2108 class Input_section
2109 {
2110 public:
2111 Input_section()
2112 : shndx_(0), p2align_(0)
2113 {
2114 this->u1_.data_size = 0;
2115 this->u2_.object = NULL;
2116 }
2117
2118 // For an ordinary input section.
2119 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2120 uint64_t addralign)
2121 : shndx_(shndx),
2122 p2align_(ffsll(static_cast<long long>(addralign)))
2123 {
2124 gold_assert(shndx != OUTPUT_SECTION_CODE
2125 && shndx != MERGE_DATA_SECTION_CODE
2126 && shndx != MERGE_STRING_SECTION_CODE);
2127 this->u1_.data_size = data_size;
2128 this->u2_.object = object;
2129 }
2130
2131 // For a non-merge output section.
2132 Input_section(Output_section_data* posd)
2133 : shndx_(OUTPUT_SECTION_CODE),
2134 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2135 {
2136 this->u1_.data_size = 0;
2137 this->u2_.posd = posd;
2138 }
2139
2140 // For a merge section.
2141 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2142 : shndx_(is_string
2143 ? MERGE_STRING_SECTION_CODE
2144 : MERGE_DATA_SECTION_CODE),
2145 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2146 {
2147 this->u1_.entsize = entsize;
2148 this->u2_.posd = posd;
2149 }
2150
2151 // The required alignment.
2152 uint64_t
2153 addralign() const
2154 {
2155 return (this->p2align_ == 0
2156 ? 0
2157 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2158 }
2159
2160 // Return the required size.
2161 off_t
2162 data_size() const;
2163
2164 // Whether this is an input section.
2165 bool
2166 is_input_section() const
2167 {
2168 return (this->shndx_ != OUTPUT_SECTION_CODE
2169 && this->shndx_ != MERGE_DATA_SECTION_CODE
2170 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2171 }
2172
2173 // Return whether this is a merge section which matches the
2174 // parameters.
2175 bool
2176 is_merge_section(bool is_string, uint64_t entsize,
2177 uint64_t addralign) const
2178 {
2179 return (this->shndx_ == (is_string
2180 ? MERGE_STRING_SECTION_CODE
2181 : MERGE_DATA_SECTION_CODE)
2182 && this->u1_.entsize == entsize
2183 && this->addralign() == addralign);
2184 }
2185
2186 // Return the object for an input section.
2187 Relobj*
2188 relobj() const
2189 {
2190 gold_assert(this->is_input_section());
2191 return this->u2_.object;
2192 }
2193
2194 // Return the input section index for an input section.
2195 unsigned int
2196 shndx() const
2197 {
2198 gold_assert(this->is_input_section());
2199 return this->shndx_;
2200 }
2201
2202 // Set the output section.
2203 void
2204 set_output_section(Output_section* os)
2205 {
2206 gold_assert(!this->is_input_section());
2207 this->u2_.posd->set_output_section(os);
2208 }
2209
2210 // Set the address and file offset. This is called during
2211 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2212 // the enclosing section.
2213 void
2214 set_address_and_file_offset(uint64_t address, off_t file_offset,
2215 off_t section_file_offset);
2216
2217 // Reset the address and file offset.
2218 void
2219 reset_address_and_file_offset();
2220
2221 // Finalize the data size.
2222 void
2223 finalize_data_size();
2224
2225 // Add an input section, for SHF_MERGE sections.
2226 bool
2227 add_input_section(Relobj* object, unsigned int shndx)
2228 {
2229 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2230 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2231 return this->u2_.posd->add_input_section(object, shndx);
2232 }
2233
2234 // Given an input OBJECT, an input section index SHNDX within that
2235 // object, and an OFFSET relative to the start of that input
2236 // section, return whether or not the output offset is known. If
2237 // this function returns true, it sets *POUTPUT to the offset in
2238 // the output section, relative to the start of the input section
2239 // in the output section. *POUTPUT may be different from OFFSET
2240 // for a merged section.
2241 bool
2242 output_offset(const Relobj* object, unsigned int shndx,
2243 section_offset_type offset,
2244 section_offset_type *poutput) const;
2245
2246 // Return whether this is the merge section for the input section
2247 // SHNDX in OBJECT.
2248 bool
2249 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2250
2251 // Write out the data. This does nothing for an input section.
2252 void
2253 write(Output_file*);
2254
2255 // Write the data to a buffer. This does nothing for an input
2256 // section.
2257 void
2258 write_to_buffer(unsigned char*);
2259
2260 // Print statistics about merge sections to stderr.
2261 void
2262 print_merge_stats(const char* section_name)
2263 {
2264 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2265 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2266 this->u2_.posd->print_merge_stats(section_name);
2267 }
2268
2269 private:
2270 // Code values which appear in shndx_. If the value is not one of
2271 // these codes, it is the input section index in the object file.
2272 enum
2273 {
2274 // An Output_section_data.
2275 OUTPUT_SECTION_CODE = -1U,
2276 // An Output_section_data for an SHF_MERGE section with
2277 // SHF_STRINGS not set.
2278 MERGE_DATA_SECTION_CODE = -2U,
2279 // An Output_section_data for an SHF_MERGE section with
2280 // SHF_STRINGS set.
2281 MERGE_STRING_SECTION_CODE = -3U
2282 };
2283
2284 // For an ordinary input section, this is the section index in the
2285 // input file. For an Output_section_data, this is
2286 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2287 // MERGE_STRING_SECTION_CODE.
2288 unsigned int shndx_;
2289 // The required alignment, stored as a power of 2.
2290 unsigned int p2align_;
2291 union
2292 {
2293 // For an ordinary input section, the section size.
2294 off_t data_size;
2295 // For OUTPUT_SECTION_CODE, this is not used. For
2296 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2297 // entity size.
2298 uint64_t entsize;
2299 } u1_;
2300 union
2301 {
2302 // For an ordinary input section, the object which holds the
2303 // input section.
2304 Relobj* object;
2305 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2306 // MERGE_STRING_SECTION_CODE, the data.
2307 Output_section_data* posd;
2308 } u2_;
2309 };
2310
2311 typedef std::vector<Input_section> Input_section_list;
2312
2313 // Fill data. This is used to fill in data between input sections.
2314 // It is also used for data statements (BYTE, WORD, etc.) in linker
2315 // scripts. When we have to keep track of the input sections, we
2316 // can use an Output_data_const, but we don't want to have to keep
2317 // track of input sections just to implement fills.
2318 class Fill
2319 {
2320 public:
2321 Fill(off_t section_offset, off_t length)
2322 : section_offset_(section_offset),
2323 length_(convert_to_section_size_type(length))
2324 { }
2325
2326 // Return section offset.
2327 off_t
2328 section_offset() const
2329 { return this->section_offset_; }
2330
2331 // Return fill length.
2332 section_size_type
2333 length() const
2334 { return this->length_; }
2335
2336 private:
2337 // The offset within the output section.
2338 off_t section_offset_;
2339 // The length of the space to fill.
2340 section_size_type length_;
2341 };
2342
2343 typedef std::vector<Fill> Fill_list;
2344
2345 // Add a new output section by Input_section.
2346 void
2347 add_output_section_data(Input_section*);
2348
2349 // Add an SHF_MERGE input section. Returns true if the section was
2350 // handled.
2351 bool
2352 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2353 uint64_t entsize, uint64_t addralign);
2354
2355 // Add an output SHF_MERGE section POSD to this output section.
2356 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2357 // ENTSIZE is the entity size. This returns the entry added to
2358 // input_sections_.
2359 void
2360 add_output_merge_section(Output_section_data* posd, bool is_string,
2361 uint64_t entsize);
2362
2363 // Most of these fields are only valid after layout.
2364
2365 // The name of the section. This will point into a Stringpool.
2366 const char* name_;
2367 // The section address is in the parent class.
2368 // The section alignment.
2369 uint64_t addralign_;
2370 // The section entry size.
2371 uint64_t entsize_;
2372 // The load address. This is only used when using a linker script
2373 // with a SECTIONS clause. The has_load_address_ field indicates
2374 // whether this field is valid.
2375 uint64_t load_address_;
2376 // The file offset is in the parent class.
2377 // Set the section link field to the index of this section.
2378 const Output_data* link_section_;
2379 // If link_section_ is NULL, this is the link field.
2380 unsigned int link_;
2381 // Set the section info field to the index of this section.
2382 const Output_section* info_section_;
2383 // If info_section_ is NULL, set the info field to the symbol table
2384 // index of this symbol.
2385 const Symbol* info_symndx_;
2386 // If info_section_ and info_symndx_ are NULL, this is the section
2387 // info field.
2388 unsigned int info_;
2389 // The section type.
2390 const elfcpp::Elf_Word type_;
2391 // The section flags.
2392 elfcpp::Elf_Xword flags_;
2393 // The section index.
2394 unsigned int out_shndx_;
2395 // If there is a STT_SECTION for this output section in the normal
2396 // symbol table, this is the symbol index. This starts out as zero.
2397 // It is initialized in Layout::finalize() to be the index, or -1U
2398 // if there isn't one.
2399 unsigned int symtab_index_;
2400 // If there is a STT_SECTION for this output section in the dynamic
2401 // symbol table, this is the symbol index. This starts out as zero.
2402 // It is initialized in Layout::finalize() to be the index, or -1U
2403 // if there isn't one.
2404 unsigned int dynsym_index_;
2405 // The input sections. This will be empty in cases where we don't
2406 // need to keep track of them.
2407 Input_section_list input_sections_;
2408 // The offset of the first entry in input_sections_.
2409 off_t first_input_offset_;
2410 // The fill data. This is separate from input_sections_ because we
2411 // often will need fill sections without needing to keep track of
2412 // input sections.
2413 Fill_list fills_;
2414 // If the section requires postprocessing, this buffer holds the
2415 // section contents during relocation.
2416 unsigned char* postprocessing_buffer_;
2417 // Whether this output section needs a STT_SECTION symbol in the
2418 // normal symbol table. This will be true if there is a relocation
2419 // which needs it.
2420 bool needs_symtab_index_ : 1;
2421 // Whether this output section needs a STT_SECTION symbol in the
2422 // dynamic symbol table. This will be true if there is a dynamic
2423 // relocation which needs it.
2424 bool needs_dynsym_index_ : 1;
2425 // Whether the link field of this output section should point to the
2426 // normal symbol table.
2427 bool should_link_to_symtab_ : 1;
2428 // Whether the link field of this output section should point to the
2429 // dynamic symbol table.
2430 bool should_link_to_dynsym_ : 1;
2431 // Whether this section should be written after all the input
2432 // sections are complete.
2433 bool after_input_sections_ : 1;
2434 // Whether this section requires post processing after all
2435 // relocations have been applied.
2436 bool requires_postprocessing_ : 1;
2437 // Whether an input section was mapped to this output section
2438 // because of a SECTIONS clause in a linker script.
2439 bool found_in_sections_clause_ : 1;
2440 // Whether this section has an explicitly specified load address.
2441 bool has_load_address_ : 1;
2442 // True if the info_section_ field means the section index of the
2443 // section, false if it means the symbol index of the corresponding
2444 // section symbol.
2445 bool info_uses_section_index_ : 1;
2446 // For SHT_TLS sections, the offset of this section relative to the base
2447 // of the TLS segment.
2448 uint64_t tls_offset_;
2449 };
2450
2451 // An output segment. PT_LOAD segments are built from collections of
2452 // output sections. Other segments typically point within PT_LOAD
2453 // segments, and are built directly as needed.
2454
2455 class Output_segment
2456 {
2457 public:
2458 // Create an output segment, specifying the type and flags.
2459 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2460
2461 // Return the virtual address.
2462 uint64_t
2463 vaddr() const
2464 { return this->vaddr_; }
2465
2466 // Return the physical address.
2467 uint64_t
2468 paddr() const
2469 { return this->paddr_; }
2470
2471 // Return the segment type.
2472 elfcpp::Elf_Word
2473 type() const
2474 { return this->type_; }
2475
2476 // Return the segment flags.
2477 elfcpp::Elf_Word
2478 flags() const
2479 { return this->flags_; }
2480
2481 // Return the memory size.
2482 uint64_t
2483 memsz() const
2484 { return this->memsz_; }
2485
2486 // Return the file size.
2487 off_t
2488 filesz() const
2489 { return this->filesz_; }
2490
2491 // Return the file offset.
2492 off_t
2493 offset() const
2494 { return this->offset_; }
2495
2496 // Return the maximum alignment of the Output_data.
2497 uint64_t
2498 maximum_alignment();
2499
2500 // Add an Output_section to this segment.
2501 void
2502 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2503 { this->add_output_section(os, seg_flags, false); }
2504
2505 // Add an Output_section to the start of this segment.
2506 void
2507 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2508 { this->add_output_section(os, seg_flags, true); }
2509
2510 // Remove an Output_section from this segment. It is an error if it
2511 // is not present.
2512 void
2513 remove_output_section(Output_section* os);
2514
2515 // Add an Output_data (which is not an Output_section) to the start
2516 // of this segment.
2517 void
2518 add_initial_output_data(Output_data*);
2519
2520 // Return true if this segment has any sections which hold actual
2521 // data, rather than being a BSS section.
2522 bool
2523 has_any_data_sections() const
2524 { return !this->output_data_.empty(); }
2525
2526 // Return the number of dynamic relocations applied to this segment.
2527 unsigned int
2528 dynamic_reloc_count() const;
2529
2530 // Return the address of the first section.
2531 uint64_t
2532 first_section_load_address() const;
2533
2534 // Return whether the addresses have been set already.
2535 bool
2536 are_addresses_set() const
2537 { return this->are_addresses_set_; }
2538
2539 // Set the addresses.
2540 void
2541 set_addresses(uint64_t vaddr, uint64_t paddr)
2542 {
2543 this->vaddr_ = vaddr;
2544 this->paddr_ = paddr;
2545 this->are_addresses_set_ = true;
2546 }
2547
2548 // Set the segment flags. This is only used if we have a PHDRS
2549 // clause which explicitly specifies the flags.
2550 void
2551 set_flags(elfcpp::Elf_Word flags)
2552 { this->flags_ = flags; }
2553
2554 // Set the address of the segment to ADDR and the offset to *POFF
2555 // and set the addresses and offsets of all contained output
2556 // sections accordingly. Set the section indexes of all contained
2557 // output sections starting with *PSHNDX. If RESET is true, first
2558 // reset the addresses of the contained sections. Return the
2559 // address of the immediately following segment. Update *POFF and
2560 // *PSHNDX. This should only be called for a PT_LOAD segment.
2561 uint64_t
2562 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
2563 unsigned int* pshndx);
2564
2565 // Set the minimum alignment of this segment. This may be adjusted
2566 // upward based on the section alignments.
2567 void
2568 set_minimum_p_align(uint64_t align)
2569 { this->min_p_align_ = align; }
2570
2571 // Set the offset of this segment based on the section. This should
2572 // only be called for a non-PT_LOAD segment.
2573 void
2574 set_offset();
2575
2576 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2577 void
2578 set_tls_offsets();
2579
2580 // Return the number of output sections.
2581 unsigned int
2582 output_section_count() const;
2583
2584 // Return the section attached to the list segment with the lowest
2585 // load address. This is used when handling a PHDRS clause in a
2586 // linker script.
2587 Output_section*
2588 section_with_lowest_load_address() const;
2589
2590 // Write the segment header into *OPHDR.
2591 template<int size, bool big_endian>
2592 void
2593 write_header(elfcpp::Phdr_write<size, big_endian>*);
2594
2595 // Write the section headers of associated sections into V.
2596 template<int size, bool big_endian>
2597 unsigned char*
2598 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2599 unsigned int* pshndx) const;
2600
2601 private:
2602 Output_segment(const Output_segment&);
2603 Output_segment& operator=(const Output_segment&);
2604
2605 typedef std::list<Output_data*> Output_data_list;
2606
2607 // Add an Output_section to this segment, specifying front or back.
2608 void
2609 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2610 bool front);
2611
2612 // Find the maximum alignment in an Output_data_list.
2613 static uint64_t
2614 maximum_alignment_list(const Output_data_list*);
2615
2616 // Set the section addresses in an Output_data_list.
2617 uint64_t
2618 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2619 uint64_t addr, off_t* poff, unsigned int* pshndx,
2620 bool* in_tls);
2621
2622 // Return the number of Output_sections in an Output_data_list.
2623 unsigned int
2624 output_section_count_list(const Output_data_list*) const;
2625
2626 // Return the number of dynamic relocs in an Output_data_list.
2627 unsigned int
2628 dynamic_reloc_count_list(const Output_data_list*) const;
2629
2630 // Find the section with the lowest load address in an
2631 // Output_data_list.
2632 void
2633 lowest_load_address_in_list(const Output_data_list* pdl,
2634 Output_section** found,
2635 uint64_t* found_lma) const;
2636
2637 // Write the section headers in the list into V.
2638 template<int size, bool big_endian>
2639 unsigned char*
2640 write_section_headers_list(const Layout*, const Stringpool*,
2641 const Output_data_list*, unsigned char* v,
2642 unsigned int* pshdx) const;
2643
2644 // The list of output data with contents attached to this segment.
2645 Output_data_list output_data_;
2646 // The list of output data without contents attached to this segment.
2647 Output_data_list output_bss_;
2648 // The segment virtual address.
2649 uint64_t vaddr_;
2650 // The segment physical address.
2651 uint64_t paddr_;
2652 // The size of the segment in memory.
2653 uint64_t memsz_;
2654 // The maximum section alignment. The is_max_align_known_ field
2655 // indicates whether this has been finalized.
2656 uint64_t max_align_;
2657 // The required minimum value for the p_align field. This is used
2658 // for PT_LOAD segments. Note that this does not mean that
2659 // addresses should be aligned to this value; it means the p_paddr
2660 // and p_vaddr fields must be congruent modulo this value. For
2661 // non-PT_LOAD segments, the dynamic linker works more efficiently
2662 // if the p_align field has the more conventional value, although it
2663 // can align as needed.
2664 uint64_t min_p_align_;
2665 // The offset of the segment data within the file.
2666 off_t offset_;
2667 // The size of the segment data in the file.
2668 off_t filesz_;
2669 // The segment type;
2670 elfcpp::Elf_Word type_;
2671 // The segment flags.
2672 elfcpp::Elf_Word flags_;
2673 // Whether we have finalized max_align_.
2674 bool is_max_align_known_ : 1;
2675 // Whether vaddr and paddr were set by a linker script.
2676 bool are_addresses_set_ : 1;
2677 };
2678
2679 // This class represents the output file.
2680
2681 class Output_file
2682 {
2683 public:
2684 Output_file(const char* name);
2685
2686 // Indicate that this is a temporary file which should not be
2687 // output.
2688 void
2689 set_is_temporary()
2690 { this->is_temporary_ = true; }
2691
2692 // Open the output file. FILE_SIZE is the final size of the file.
2693 void
2694 open(off_t file_size);
2695
2696 // Resize the output file.
2697 void
2698 resize(off_t file_size);
2699
2700 // Close the output file (flushing all buffered data) and make sure
2701 // there are no errors.
2702 void
2703 close();
2704
2705 // We currently always use mmap which makes the view handling quite
2706 // simple. In the future we may support other approaches.
2707
2708 // Write data to the output file.
2709 void
2710 write(off_t offset, const void* data, size_t len)
2711 { memcpy(this->base_ + offset, data, len); }
2712
2713 // Get a buffer to use to write to the file, given the offset into
2714 // the file and the size.
2715 unsigned char*
2716 get_output_view(off_t start, size_t size)
2717 {
2718 gold_assert(start >= 0
2719 && start + static_cast<off_t>(size) <= this->file_size_);
2720 return this->base_ + start;
2721 }
2722
2723 // VIEW must have been returned by get_output_view. Write the
2724 // buffer to the file, passing in the offset and the size.
2725 void
2726 write_output_view(off_t, size_t, unsigned char*)
2727 { }
2728
2729 // Get a read/write buffer. This is used when we want to write part
2730 // of the file, read it in, and write it again.
2731 unsigned char*
2732 get_input_output_view(off_t start, size_t size)
2733 { return this->get_output_view(start, size); }
2734
2735 // Write a read/write buffer back to the file.
2736 void
2737 write_input_output_view(off_t, size_t, unsigned char*)
2738 { }
2739
2740 // Get a read buffer. This is used when we just want to read part
2741 // of the file back it in.
2742 const unsigned char*
2743 get_input_view(off_t start, size_t size)
2744 { return this->get_output_view(start, size); }
2745
2746 // Release a read bfufer.
2747 void
2748 free_input_view(off_t, size_t, const unsigned char*)
2749 { }
2750
2751 private:
2752 // Map the file into memory and return a pointer to the map.
2753 void
2754 map();
2755
2756 // Unmap the file from memory (and flush to disk buffers).
2757 void
2758 unmap();
2759
2760 // File name.
2761 const char* name_;
2762 // File descriptor.
2763 int o_;
2764 // File size.
2765 off_t file_size_;
2766 // Base of file mapped into memory.
2767 unsigned char* base_;
2768 // True iff base_ points to a memory buffer rather than an output file.
2769 bool map_is_anonymous_;
2770 // True if this is a temporary file which should not be output.
2771 bool is_temporary_;
2772 };
2773
2774 } // End namespace gold.
2775
2776 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.118362 seconds and 4 git commands to generate.