* copy-relocs.cc: New file.
[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
600 private:
601 // The output section for this section.
602 Output_section* output_section_;
603 // The required alignment.
604 uint64_t addralign_;
605 };
606
607 // Some Output_section_data classes build up their data step by step,
608 // rather than all at once. This class provides an interface for
609 // them.
610
611 class Output_section_data_build : public Output_section_data
612 {
613 public:
614 Output_section_data_build(uint64_t addralign)
615 : Output_section_data(addralign)
616 { }
617
618 // Get the current data size.
619 off_t
620 current_data_size() const
621 { return this->current_data_size_for_child(); }
622
623 // Set the current data size.
624 void
625 set_current_data_size(off_t data_size)
626 { this->set_current_data_size_for_child(data_size); }
627
628 protected:
629 // Set the final data size.
630 virtual void
631 set_final_data_size()
632 { this->set_data_size(this->current_data_size_for_child()); }
633 };
634
635 // A simple case of Output_data in which we have constant data to
636 // output.
637
638 class Output_data_const : public Output_section_data
639 {
640 public:
641 Output_data_const(const std::string& data, uint64_t addralign)
642 : Output_section_data(data.size(), addralign), data_(data)
643 { }
644
645 Output_data_const(const char* p, off_t len, uint64_t addralign)
646 : Output_section_data(len, addralign), data_(p, len)
647 { }
648
649 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
650 : Output_section_data(len, addralign),
651 data_(reinterpret_cast<const char*>(p), len)
652 { }
653
654 protected:
655 // Write the data to the output file.
656 void
657 do_write(Output_file*);
658
659 // Write the data to a buffer.
660 void
661 do_write_to_buffer(unsigned char* buffer)
662 { memcpy(buffer, this->data_.data(), this->data_.size()); }
663
664 private:
665 std::string data_;
666 };
667
668 // Another version of Output_data with constant data, in which the
669 // buffer is allocated by the caller.
670
671 class Output_data_const_buffer : public Output_section_data
672 {
673 public:
674 Output_data_const_buffer(const unsigned char* p, off_t len,
675 uint64_t addralign)
676 : Output_section_data(len, addralign), p_(p)
677 { }
678
679 protected:
680 // Write the data the output file.
681 void
682 do_write(Output_file*);
683
684 // Write the data to a buffer.
685 void
686 do_write_to_buffer(unsigned char* buffer)
687 { memcpy(buffer, this->p_, this->data_size()); }
688
689 private:
690 const unsigned char* p_;
691 };
692
693 // A place holder for a fixed amount of data written out via some
694 // other mechanism.
695
696 class Output_data_fixed_space : public Output_section_data
697 {
698 public:
699 Output_data_fixed_space(off_t data_size, uint64_t addralign)
700 : Output_section_data(data_size, addralign)
701 { }
702
703 protected:
704 // Write out the data--the actual data must be written out
705 // elsewhere.
706 void
707 do_write(Output_file*)
708 { }
709 };
710
711 // A place holder for variable sized data written out via some other
712 // mechanism.
713
714 class Output_data_space : public Output_section_data_build
715 {
716 public:
717 explicit Output_data_space(uint64_t addralign)
718 : Output_section_data_build(addralign)
719 { }
720
721 // Set the alignment.
722 void
723 set_space_alignment(uint64_t align)
724 { this->set_addralign(align); }
725
726 protected:
727 // Write out the data--the actual data must be written out
728 // elsewhere.
729 void
730 do_write(Output_file*)
731 { }
732 };
733
734 // A string table which goes into an output section.
735
736 class Output_data_strtab : public Output_section_data
737 {
738 public:
739 Output_data_strtab(Stringpool* strtab)
740 : Output_section_data(1), strtab_(strtab)
741 { }
742
743 protected:
744 // This is called to set the address and file offset. Here we make
745 // sure that the Stringpool is finalized.
746 void
747 set_final_data_size();
748
749 // Write out the data.
750 void
751 do_write(Output_file*);
752
753 // Write the data to a buffer.
754 void
755 do_write_to_buffer(unsigned char* buffer)
756 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
757
758 private:
759 Stringpool* strtab_;
760 };
761
762 // This POD class is used to represent a single reloc in the output
763 // file. This could be a private class within Output_data_reloc, but
764 // the templatization is complex enough that I broke it out into a
765 // separate class. The class is templatized on either elfcpp::SHT_REL
766 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
767 // relocation or an ordinary relocation.
768
769 // A relocation can be against a global symbol, a local symbol, a
770 // local section symbol, an output section, or the undefined symbol at
771 // index 0. We represent the latter by using a NULL global symbol.
772
773 template<int sh_type, bool dynamic, int size, bool big_endian>
774 class Output_reloc;
775
776 template<bool dynamic, int size, bool big_endian>
777 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
778 {
779 public:
780 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
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. ADDEND is the addend being
839 // applied to the input section.
840 section_offset_type
841 local_section_offset(Addend addend) const;
842
843 // Get the value of the symbol referred to by a Rel relocation when
844 // we are adding the given ADDEND.
845 Address
846 symbol_value(Addend addend) const;
847
848 // Write the reloc entry to an output view.
849 void
850 write(unsigned char* pov) const;
851
852 // Write the offset and info fields to Write_rel.
853 template<typename Write_rel>
854 void write_rel(Write_rel*) const;
855
856 private:
857 // Record that we need a dynamic symbol index.
858 void
859 set_needs_dynsym_index();
860
861 // Return the symbol index.
862 unsigned int
863 get_symbol_index() const;
864
865 // Codes for local_sym_index_.
866 enum
867 {
868 // Global symbol.
869 GSYM_CODE = -1U,
870 // Output section.
871 SECTION_CODE = -2U,
872 // Invalid uninitialized entry.
873 INVALID_CODE = -3U
874 };
875
876 union
877 {
878 // For a local symbol or local section symbol
879 // (this->local_sym_index_ >= 0), the object. We will never
880 // generate a relocation against a local symbol in a dynamic
881 // object; that doesn't make sense. And our callers will always
882 // be templatized, so we use Sized_relobj here.
883 Sized_relobj<size, big_endian>* relobj;
884 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
885 // symbol. If this is NULL, it indicates a relocation against the
886 // undefined 0 symbol.
887 Symbol* gsym;
888 // For a relocation against an output section
889 // (this->local_sym_index_ == SECTION_CODE), the output section.
890 Output_section* os;
891 } u1_;
892 union
893 {
894 // If this->shndx_ is not INVALID CODE, the object which holds the
895 // input section being used to specify the reloc address.
896 Relobj* relobj;
897 // If this->shndx_ is INVALID_CODE, the output data being used to
898 // specify the reloc address. This may be NULL if the reloc
899 // address is absolute.
900 Output_data* od;
901 } u2_;
902 // The address offset within the input section or the Output_data.
903 Address address_;
904 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
905 // relocation against an output section, or INVALID_CODE for an
906 // uninitialized value. Otherwise, for a local symbol
907 // (this->is_section_symbol_ is false), the local symbol index. For
908 // a local section symbol (this->is_section_symbol_ is true), the
909 // section index in the input file.
910 unsigned int local_sym_index_;
911 // The reloc type--a processor specific code.
912 unsigned int type_ : 30;
913 // True if the relocation is a RELATIVE relocation.
914 bool is_relative_ : 1;
915 // True if the relocation is against a section symbol.
916 bool is_section_symbol_ : 1;
917 // If the reloc address is an input section in an object, the
918 // section index. This is INVALID_CODE if the reloc address is
919 // specified in some other way.
920 unsigned int shndx_;
921 };
922
923 // The SHT_RELA version of Output_reloc<>. This is just derived from
924 // the SHT_REL version of Output_reloc, but it adds an addend.
925
926 template<bool dynamic, int size, bool big_endian>
927 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
928 {
929 public:
930 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
931 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
932
933 // An uninitialized entry.
934 Output_reloc()
935 : rel_()
936 { }
937
938 // A reloc against a global symbol.
939
940 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
941 Address address, Addend addend, bool is_relative)
942 : rel_(gsym, type, od, address, is_relative), addend_(addend)
943 { }
944
945 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
946 unsigned int shndx, Address address, Addend addend,
947 bool is_relative)
948 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
949 { }
950
951 // A reloc against a local symbol.
952
953 Output_reloc(Sized_relobj<size, big_endian>* relobj,
954 unsigned int local_sym_index, unsigned int type,
955 Output_data* od, Address address,
956 Addend addend, bool is_relative, bool is_section_symbol)
957 : rel_(relobj, local_sym_index, type, od, address, is_relative,
958 is_section_symbol),
959 addend_(addend)
960 { }
961
962 Output_reloc(Sized_relobj<size, big_endian>* relobj,
963 unsigned int local_sym_index, unsigned int type,
964 unsigned int shndx, Address address,
965 Addend addend, bool is_relative, bool is_section_symbol)
966 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
967 is_section_symbol),
968 addend_(addend)
969 { }
970
971 // A reloc against the STT_SECTION symbol of an output section.
972
973 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
974 Address address, Addend addend)
975 : rel_(os, type, od, address), addend_(addend)
976 { }
977
978 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
979 unsigned int shndx, Address address, Addend addend)
980 : rel_(os, type, relobj, shndx, address), addend_(addend)
981 { }
982
983 // Write the reloc entry to an output view.
984 void
985 write(unsigned char* pov) const;
986
987 private:
988 // The basic reloc.
989 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
990 // The addend.
991 Addend addend_;
992 };
993
994 // Output_data_reloc is used to manage a section containing relocs.
995 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
996 // indicates whether this is a dynamic relocation or a normal
997 // relocation. Output_data_reloc_base is a base class.
998 // Output_data_reloc is the real class, which we specialize based on
999 // the reloc type.
1000
1001 template<int sh_type, bool dynamic, int size, bool big_endian>
1002 class Output_data_reloc_base : public Output_section_data_build
1003 {
1004 public:
1005 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1006 typedef typename Output_reloc_type::Address Address;
1007 static const int reloc_size =
1008 Reloc_types<sh_type, size, big_endian>::reloc_size;
1009
1010 // Construct the section.
1011 Output_data_reloc_base()
1012 : Output_section_data_build(Output_data::default_alignment_for_size(size))
1013 { }
1014
1015 protected:
1016 // Write out the data.
1017 void
1018 do_write(Output_file*);
1019
1020 // Set the entry size and the link.
1021 void
1022 do_adjust_output_section(Output_section *os);
1023
1024 // Add a relocation entry.
1025 void
1026 add(Output_data *od, const Output_reloc_type& reloc)
1027 {
1028 this->relocs_.push_back(reloc);
1029 this->set_current_data_size(this->relocs_.size() * reloc_size);
1030 od->add_dynamic_reloc();
1031 }
1032
1033 private:
1034 typedef std::vector<Output_reloc_type> Relocs;
1035
1036 Relocs relocs_;
1037 };
1038
1039 // The class which callers actually create.
1040
1041 template<int sh_type, bool dynamic, int size, bool big_endian>
1042 class Output_data_reloc;
1043
1044 // The SHT_REL version of Output_data_reloc.
1045
1046 template<bool dynamic, int size, bool big_endian>
1047 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1048 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1049 {
1050 private:
1051 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1052 big_endian> Base;
1053
1054 public:
1055 typedef typename Base::Output_reloc_type Output_reloc_type;
1056 typedef typename Output_reloc_type::Address Address;
1057
1058 Output_data_reloc()
1059 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1060 { }
1061
1062 // Add a reloc against a global symbol.
1063
1064 void
1065 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1066 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1067
1068 void
1069 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1070 unsigned int shndx, Address address)
1071 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1072 false)); }
1073
1074 // These are to simplify the Copy_relocs class.
1075
1076 void
1077 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1078 Address addend)
1079 {
1080 gold_assert(addend == 0);
1081 this->add_global(gsym, type, od, address);
1082 }
1083
1084 void
1085 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1086 unsigned int shndx, Address address, Address addend)
1087 {
1088 gold_assert(addend == 0);
1089 this->add_global(gsym, type, od, relobj, shndx, address);
1090 }
1091
1092 // Add a RELATIVE reloc against a global symbol. The final relocation
1093 // will not reference the symbol.
1094
1095 void
1096 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1097 Address address)
1098 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1099
1100 void
1101 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1102 Relobj* relobj, unsigned int shndx, Address address)
1103 {
1104 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1105 true));
1106 }
1107
1108 // Add a reloc against a local symbol.
1109
1110 void
1111 add_local(Sized_relobj<size, big_endian>* relobj,
1112 unsigned int local_sym_index, unsigned int type,
1113 Output_data* od, Address address)
1114 {
1115 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1116 address, false, false));
1117 }
1118
1119 void
1120 add_local(Sized_relobj<size, big_endian>* relobj,
1121 unsigned int local_sym_index, unsigned int type,
1122 Output_data* od, unsigned int shndx, Address address)
1123 {
1124 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1125 address, false, false));
1126 }
1127
1128 // Add a RELATIVE reloc against a local symbol.
1129
1130 void
1131 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1132 unsigned int local_sym_index, unsigned int type,
1133 Output_data* od, Address address)
1134 {
1135 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1136 address, true, false));
1137 }
1138
1139 void
1140 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1141 unsigned int local_sym_index, unsigned int type,
1142 Output_data* od, unsigned int shndx, Address address)
1143 {
1144 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1145 address, true, false));
1146 }
1147
1148 // Add a reloc against a local section symbol. This will be
1149 // converted into a reloc against the STT_SECTION symbol of the
1150 // output section.
1151
1152 void
1153 add_local_section(Sized_relobj<size, big_endian>* relobj,
1154 unsigned int input_shndx, unsigned int type,
1155 Output_data* od, Address address)
1156 {
1157 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1158 address, false, true));
1159 }
1160
1161 void
1162 add_local_section(Sized_relobj<size, big_endian>* relobj,
1163 unsigned int input_shndx, unsigned int type,
1164 Output_data* od, unsigned int shndx, Address address)
1165 {
1166 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1167 address, false, true));
1168 }
1169
1170 // A reloc against the STT_SECTION symbol of an output section.
1171 // OS is the Output_section that the relocation refers to; OD is
1172 // the Output_data object being relocated.
1173
1174 void
1175 add_output_section(Output_section* os, unsigned int type,
1176 Output_data* od, Address address)
1177 { this->add(od, Output_reloc_type(os, type, od, address)); }
1178
1179 void
1180 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1181 Relobj* relobj, unsigned int shndx, Address address)
1182 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1183 };
1184
1185 // The SHT_RELA version of Output_data_reloc.
1186
1187 template<bool dynamic, int size, bool big_endian>
1188 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1189 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1190 {
1191 private:
1192 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1193 big_endian> Base;
1194
1195 public:
1196 typedef typename Base::Output_reloc_type Output_reloc_type;
1197 typedef typename Output_reloc_type::Address Address;
1198 typedef typename Output_reloc_type::Addend Addend;
1199
1200 Output_data_reloc()
1201 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1202 { }
1203
1204 // Add a reloc against a global symbol.
1205
1206 void
1207 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1208 Address address, Addend addend)
1209 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1210 false)); }
1211
1212 void
1213 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1214 unsigned int shndx, Address address,
1215 Addend addend)
1216 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1217 addend, false)); }
1218
1219 // Add a RELATIVE reloc against a global symbol. The final output
1220 // relocation will not reference the symbol, but we must keep the symbol
1221 // information long enough to set the addend of the relocation correctly
1222 // when it is written.
1223
1224 void
1225 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1226 Address address, Addend addend)
1227 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1228
1229 void
1230 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1231 Relobj* relobj, unsigned int shndx, Address address,
1232 Addend addend)
1233 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1234 addend, true)); }
1235
1236 // Add a reloc against a local symbol.
1237
1238 void
1239 add_local(Sized_relobj<size, big_endian>* relobj,
1240 unsigned int local_sym_index, unsigned int type,
1241 Output_data* od, Address address, Addend addend)
1242 {
1243 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1244 addend, false, false));
1245 }
1246
1247 void
1248 add_local(Sized_relobj<size, big_endian>* relobj,
1249 unsigned int local_sym_index, unsigned int type,
1250 Output_data* od, unsigned int shndx, Address address,
1251 Addend addend)
1252 {
1253 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1254 address, addend, false, false));
1255 }
1256
1257 // Add a RELATIVE reloc against a local symbol.
1258
1259 void
1260 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1261 unsigned int local_sym_index, unsigned int type,
1262 Output_data* od, Address address, Addend addend)
1263 {
1264 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1265 addend, true, false));
1266 }
1267
1268 void
1269 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1270 unsigned int local_sym_index, unsigned int type,
1271 Output_data* od, unsigned int shndx, Address address,
1272 Addend addend)
1273 {
1274 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1275 address, addend, true, false));
1276 }
1277
1278 // Add a reloc against a local section symbol. This will be
1279 // converted into a reloc against the STT_SECTION symbol of the
1280 // output section.
1281
1282 void
1283 add_local_section(Sized_relobj<size, big_endian>* relobj,
1284 unsigned int input_shndx, unsigned int type,
1285 Output_data* od, Address address, Addend addend)
1286 {
1287 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1288 addend, false, true));
1289 }
1290
1291 void
1292 add_local_section(Sized_relobj<size, big_endian>* relobj,
1293 unsigned int input_shndx, unsigned int type,
1294 Output_data* od, unsigned int shndx, Address address,
1295 Addend addend)
1296 {
1297 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1298 address, addend, false, true));
1299 }
1300
1301 // A reloc against the STT_SECTION symbol of an output section.
1302
1303 void
1304 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1305 Address address, Addend addend)
1306 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1307
1308 void
1309 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1310 unsigned int shndx, Address address, Addend addend)
1311 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1312 addend)); }
1313 };
1314
1315 // Output_relocatable_relocs represents a relocation section in a
1316 // relocatable link. The actual data is written out in the target
1317 // hook relocate_for_relocatable. This just saves space for it.
1318
1319 template<int sh_type, int size, bool big_endian>
1320 class Output_relocatable_relocs : public Output_section_data
1321 {
1322 public:
1323 Output_relocatable_relocs(Relocatable_relocs* rr)
1324 : Output_section_data(Output_data::default_alignment_for_size(size)),
1325 rr_(rr)
1326 { }
1327
1328 void
1329 set_final_data_size();
1330
1331 // Write out the data. There is nothing to do here.
1332 void
1333 do_write(Output_file*)
1334 { }
1335
1336 private:
1337 // The relocs associated with this input section.
1338 Relocatable_relocs* rr_;
1339 };
1340
1341 // Handle a GROUP section.
1342
1343 template<int size, bool big_endian>
1344 class Output_data_group : public Output_section_data
1345 {
1346 public:
1347 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1348 section_size_type entry_count,
1349 const elfcpp::Elf_Word* contents);
1350
1351 void
1352 do_write(Output_file*);
1353
1354 private:
1355 // The input object.
1356 Sized_relobj<size, big_endian>* relobj_;
1357 // The group flag word.
1358 elfcpp::Elf_Word flags_;
1359 // The section indexes of the input sections in this group.
1360 std::vector<unsigned int> input_sections_;
1361 };
1362
1363 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1364 // for one symbol--either a global symbol or a local symbol in an
1365 // object. The target specific code adds entries to the GOT as
1366 // needed.
1367
1368 template<int size, bool big_endian>
1369 class Output_data_got : public Output_section_data_build
1370 {
1371 public:
1372 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1373 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1374 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1375
1376 Output_data_got()
1377 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1378 entries_()
1379 { }
1380
1381 // Add an entry for a global symbol to the GOT. Return true if this
1382 // is a new GOT entry, false if the symbol was already in the GOT.
1383 bool
1384 add_global(Symbol* gsym, unsigned int got_type);
1385
1386 // Add an entry for a global symbol to the GOT, and add a dynamic
1387 // relocation of type R_TYPE for the GOT entry.
1388 void
1389 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1390 Rel_dyn* rel_dyn, unsigned int r_type);
1391
1392 void
1393 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1394 Rela_dyn* rela_dyn, unsigned int r_type);
1395
1396 // Add a pair of entries for a global symbol to the GOT, and add
1397 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1398 void
1399 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1400 Rel_dyn* rel_dyn, unsigned int r_type_1,
1401 unsigned int r_type_2);
1402
1403 void
1404 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1405 Rela_dyn* rela_dyn, unsigned int r_type_1,
1406 unsigned int r_type_2);
1407
1408 // Add an entry for a local symbol to the GOT. This returns true if
1409 // this is a new GOT entry, false if the symbol already has a GOT
1410 // entry.
1411 bool
1412 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1413 unsigned int got_type);
1414
1415 // Add an entry for a local symbol to the GOT, and add a dynamic
1416 // relocation of type R_TYPE for the GOT entry.
1417 void
1418 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1419 unsigned int sym_index, unsigned int got_type,
1420 Rel_dyn* rel_dyn, unsigned int r_type);
1421
1422 void
1423 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1424 unsigned int sym_index, unsigned int got_type,
1425 Rela_dyn* rela_dyn, unsigned int r_type);
1426
1427 // Add a pair of entries for a local symbol to the GOT, and add
1428 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1429 void
1430 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1431 unsigned int sym_index, unsigned int shndx,
1432 unsigned int got_type, Rel_dyn* rel_dyn,
1433 unsigned int r_type_1, unsigned int r_type_2);
1434
1435 void
1436 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1437 unsigned int sym_index, unsigned int shndx,
1438 unsigned int got_type, Rela_dyn* rela_dyn,
1439 unsigned int r_type_1, unsigned int r_type_2);
1440
1441 // Add a constant to the GOT. This returns the offset of the new
1442 // entry from the start of the GOT.
1443 unsigned int
1444 add_constant(Valtype constant)
1445 {
1446 this->entries_.push_back(Got_entry(constant));
1447 this->set_got_size();
1448 return this->last_got_offset();
1449 }
1450
1451 protected:
1452 // Write out the GOT table.
1453 void
1454 do_write(Output_file*);
1455
1456 private:
1457 // This POD class holds a single GOT entry.
1458 class Got_entry
1459 {
1460 public:
1461 // Create a zero entry.
1462 Got_entry()
1463 : local_sym_index_(CONSTANT_CODE)
1464 { this->u_.constant = 0; }
1465
1466 // Create a global symbol entry.
1467 explicit Got_entry(Symbol* gsym)
1468 : local_sym_index_(GSYM_CODE)
1469 { this->u_.gsym = gsym; }
1470
1471 // Create a local symbol entry.
1472 Got_entry(Sized_relobj<size, big_endian>* object,
1473 unsigned int local_sym_index)
1474 : local_sym_index_(local_sym_index)
1475 {
1476 gold_assert(local_sym_index != GSYM_CODE
1477 && local_sym_index != CONSTANT_CODE);
1478 this->u_.object = object;
1479 }
1480
1481 // Create a constant entry. The constant is a host value--it will
1482 // be swapped, if necessary, when it is written out.
1483 explicit Got_entry(Valtype constant)
1484 : local_sym_index_(CONSTANT_CODE)
1485 { this->u_.constant = constant; }
1486
1487 // Write the GOT entry to an output view.
1488 void
1489 write(unsigned char* pov) const;
1490
1491 private:
1492 enum
1493 {
1494 GSYM_CODE = -1U,
1495 CONSTANT_CODE = -2U
1496 };
1497
1498 union
1499 {
1500 // For a local symbol, the object.
1501 Sized_relobj<size, big_endian>* object;
1502 // For a global symbol, the symbol.
1503 Symbol* gsym;
1504 // For a constant, the constant.
1505 Valtype constant;
1506 } u_;
1507 // For a local symbol, the local symbol index. This is GSYM_CODE
1508 // for a global symbol, or CONSTANT_CODE for a constant.
1509 unsigned int local_sym_index_;
1510 };
1511
1512 typedef std::vector<Got_entry> Got_entries;
1513
1514 // Return the offset into the GOT of GOT entry I.
1515 unsigned int
1516 got_offset(unsigned int i) const
1517 { return i * (size / 8); }
1518
1519 // Return the offset into the GOT of the last entry added.
1520 unsigned int
1521 last_got_offset() const
1522 { return this->got_offset(this->entries_.size() - 1); }
1523
1524 // Set the size of the section.
1525 void
1526 set_got_size()
1527 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1528
1529 // The list of GOT entries.
1530 Got_entries entries_;
1531 };
1532
1533 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1534 // section.
1535
1536 class Output_data_dynamic : public Output_section_data
1537 {
1538 public:
1539 Output_data_dynamic(Stringpool* pool)
1540 : Output_section_data(Output_data::default_alignment()),
1541 entries_(), pool_(pool)
1542 { }
1543
1544 // Add a new dynamic entry with a fixed numeric value.
1545 void
1546 add_constant(elfcpp::DT tag, unsigned int val)
1547 { this->add_entry(Dynamic_entry(tag, val)); }
1548
1549 // Add a new dynamic entry with the address of output data.
1550 void
1551 add_section_address(elfcpp::DT tag, const Output_data* od)
1552 { this->add_entry(Dynamic_entry(tag, od, false)); }
1553
1554 // Add a new dynamic entry with the address of output data
1555 // plus a constant offset.
1556 void
1557 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1558 unsigned int offset)
1559 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1560
1561 // Add a new dynamic entry with the size of output data.
1562 void
1563 add_section_size(elfcpp::DT tag, const Output_data* od)
1564 { this->add_entry(Dynamic_entry(tag, od, true)); }
1565
1566 // Add a new dynamic entry with the address of a symbol.
1567 void
1568 add_symbol(elfcpp::DT tag, const Symbol* sym)
1569 { this->add_entry(Dynamic_entry(tag, sym)); }
1570
1571 // Add a new dynamic entry with a string.
1572 void
1573 add_string(elfcpp::DT tag, const char* str)
1574 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1575
1576 void
1577 add_string(elfcpp::DT tag, const std::string& str)
1578 { this->add_string(tag, str.c_str()); }
1579
1580 protected:
1581 // Adjust the output section to set the entry size.
1582 void
1583 do_adjust_output_section(Output_section*);
1584
1585 // Set the final data size.
1586 void
1587 set_final_data_size();
1588
1589 // Write out the dynamic entries.
1590 void
1591 do_write(Output_file*);
1592
1593 private:
1594 // This POD class holds a single dynamic entry.
1595 class Dynamic_entry
1596 {
1597 public:
1598 // Create an entry with a fixed numeric value.
1599 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1600 : tag_(tag), offset_(DYNAMIC_NUMBER)
1601 { this->u_.val = val; }
1602
1603 // Create an entry with the size or address of a section.
1604 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1605 : tag_(tag),
1606 offset_(section_size
1607 ? DYNAMIC_SECTION_SIZE
1608 : DYNAMIC_SECTION_ADDRESS)
1609 { this->u_.od = od; }
1610
1611 // Create an entry with the address of a section plus a constant offset.
1612 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1613 : tag_(tag),
1614 offset_(offset)
1615 { this->u_.od = od; }
1616
1617 // Create an entry with the address of a symbol.
1618 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1619 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1620 { this->u_.sym = sym; }
1621
1622 // Create an entry with a string.
1623 Dynamic_entry(elfcpp::DT tag, const char* str)
1624 : tag_(tag), offset_(DYNAMIC_STRING)
1625 { this->u_.str = str; }
1626
1627 // Write the dynamic entry to an output view.
1628 template<int size, bool big_endian>
1629 void
1630 write(unsigned char* pov, const Stringpool*) const;
1631
1632 private:
1633 // Classification is encoded in the OFFSET field.
1634 enum Classification
1635 {
1636 // Section address.
1637 DYNAMIC_SECTION_ADDRESS = 0,
1638 // Number.
1639 DYNAMIC_NUMBER = -1U,
1640 // Section size.
1641 DYNAMIC_SECTION_SIZE = -2U,
1642 // Symbol adress.
1643 DYNAMIC_SYMBOL = -3U,
1644 // String.
1645 DYNAMIC_STRING = -4U
1646 // Any other value indicates a section address plus OFFSET.
1647 };
1648
1649 union
1650 {
1651 // For DYNAMIC_NUMBER.
1652 unsigned int val;
1653 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1654 const Output_data* od;
1655 // For DYNAMIC_SYMBOL.
1656 const Symbol* sym;
1657 // For DYNAMIC_STRING.
1658 const char* str;
1659 } u_;
1660 // The dynamic tag.
1661 elfcpp::DT tag_;
1662 // The type of entry (Classification) or offset within a section.
1663 unsigned int offset_;
1664 };
1665
1666 // Add an entry to the list.
1667 void
1668 add_entry(const Dynamic_entry& entry)
1669 { this->entries_.push_back(entry); }
1670
1671 // Sized version of write function.
1672 template<int size, bool big_endian>
1673 void
1674 sized_write(Output_file* of);
1675
1676 // The type of the list of entries.
1677 typedef std::vector<Dynamic_entry> Dynamic_entries;
1678
1679 // The entries.
1680 Dynamic_entries entries_;
1681 // The pool used for strings.
1682 Stringpool* pool_;
1683 };
1684
1685 // An output section. We don't expect to have too many output
1686 // sections, so we don't bother to do a template on the size.
1687
1688 class Output_section : public Output_data
1689 {
1690 public:
1691 // Create an output section, giving the name, type, and flags.
1692 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1693 virtual ~Output_section();
1694
1695 // Add a new input section SHNDX, named NAME, with header SHDR, from
1696 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1697 // which applies to this section, or 0 if none, or -1U if more than
1698 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1699 // in a linker script; in that case we need to keep track of input
1700 // sections associated with an output section. Return the offset
1701 // within the output section.
1702 template<int size, bool big_endian>
1703 off_t
1704 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1705 const char *name,
1706 const elfcpp::Shdr<size, big_endian>& shdr,
1707 unsigned int reloc_shndx, bool have_sections_script);
1708
1709 // Add generated data POSD to this output section.
1710 void
1711 add_output_section_data(Output_section_data* posd);
1712
1713 // Return the section name.
1714 const char*
1715 name() const
1716 { return this->name_; }
1717
1718 // Return the section type.
1719 elfcpp::Elf_Word
1720 type() const
1721 { return this->type_; }
1722
1723 // Return the section flags.
1724 elfcpp::Elf_Xword
1725 flags() const
1726 { return this->flags_; }
1727
1728 // Set the section flags. This may only be used with the Layout
1729 // code when it is prepared to move the section to a different
1730 // segment.
1731 void
1732 set_flags(elfcpp::Elf_Xword flags)
1733 { this->flags_ = flags; }
1734
1735 // Update the output section flags based on input section flags.
1736 void
1737 update_flags_for_input_section(elfcpp::Elf_Xword flags)
1738 {
1739 this->flags_ |= (flags
1740 & (elfcpp::SHF_WRITE
1741 | elfcpp::SHF_ALLOC
1742 | elfcpp::SHF_EXECINSTR));
1743 }
1744
1745 // Return the entsize field.
1746 uint64_t
1747 entsize() const
1748 { return this->entsize_; }
1749
1750 // Set the entsize field.
1751 void
1752 set_entsize(uint64_t v);
1753
1754 // Set the load address.
1755 void
1756 set_load_address(uint64_t load_address)
1757 {
1758 this->load_address_ = load_address;
1759 this->has_load_address_ = true;
1760 }
1761
1762 // Set the link field to the output section index of a section.
1763 void
1764 set_link_section(const Output_data* od)
1765 {
1766 gold_assert(this->link_ == 0
1767 && !this->should_link_to_symtab_
1768 && !this->should_link_to_dynsym_);
1769 this->link_section_ = od;
1770 }
1771
1772 // Set the link field to a constant.
1773 void
1774 set_link(unsigned int v)
1775 {
1776 gold_assert(this->link_section_ == NULL
1777 && !this->should_link_to_symtab_
1778 && !this->should_link_to_dynsym_);
1779 this->link_ = v;
1780 }
1781
1782 // Record that this section should link to the normal symbol table.
1783 void
1784 set_should_link_to_symtab()
1785 {
1786 gold_assert(this->link_section_ == NULL
1787 && this->link_ == 0
1788 && !this->should_link_to_dynsym_);
1789 this->should_link_to_symtab_ = true;
1790 }
1791
1792 // Record that this section should link to the dynamic symbol table.
1793 void
1794 set_should_link_to_dynsym()
1795 {
1796 gold_assert(this->link_section_ == NULL
1797 && this->link_ == 0
1798 && !this->should_link_to_symtab_);
1799 this->should_link_to_dynsym_ = true;
1800 }
1801
1802 // Return the info field.
1803 unsigned int
1804 info() const
1805 {
1806 gold_assert(this->info_section_ == NULL
1807 && this->info_symndx_ == NULL);
1808 return this->info_;
1809 }
1810
1811 // Set the info field to the output section index of a section.
1812 void
1813 set_info_section(const Output_section* os)
1814 {
1815 gold_assert((this->info_section_ == NULL
1816 || (this->info_section_ == os
1817 && this->info_uses_section_index_))
1818 && this->info_symndx_ == NULL
1819 && this->info_ == 0);
1820 this->info_section_ = os;
1821 this->info_uses_section_index_= true;
1822 }
1823
1824 // Set the info field to the symbol table index of a symbol.
1825 void
1826 set_info_symndx(const Symbol* sym)
1827 {
1828 gold_assert(this->info_section_ == NULL
1829 && (this->info_symndx_ == NULL
1830 || this->info_symndx_ == sym)
1831 && this->info_ == 0);
1832 this->info_symndx_ = sym;
1833 }
1834
1835 // Set the info field to the symbol table index of a section symbol.
1836 void
1837 set_info_section_symndx(const Output_section* os)
1838 {
1839 gold_assert((this->info_section_ == NULL
1840 || (this->info_section_ == os
1841 && !this->info_uses_section_index_))
1842 && this->info_symndx_ == NULL
1843 && this->info_ == 0);
1844 this->info_section_ = os;
1845 this->info_uses_section_index_ = false;
1846 }
1847
1848 // Set the info field to a constant.
1849 void
1850 set_info(unsigned int v)
1851 {
1852 gold_assert(this->info_section_ == NULL
1853 && this->info_symndx_ == NULL
1854 && (this->info_ == 0
1855 || this->info_ == v));
1856 this->info_ = v;
1857 }
1858
1859 // Set the addralign field.
1860 void
1861 set_addralign(uint64_t v)
1862 { this->addralign_ = v; }
1863
1864 // Indicate that we need a symtab index.
1865 void
1866 set_needs_symtab_index()
1867 { this->needs_symtab_index_ = true; }
1868
1869 // Return whether we need a symtab index.
1870 bool
1871 needs_symtab_index() const
1872 { return this->needs_symtab_index_; }
1873
1874 // Get the symtab index.
1875 unsigned int
1876 symtab_index() const
1877 {
1878 gold_assert(this->symtab_index_ != 0);
1879 return this->symtab_index_;
1880 }
1881
1882 // Set the symtab index.
1883 void
1884 set_symtab_index(unsigned int index)
1885 {
1886 gold_assert(index != 0);
1887 this->symtab_index_ = index;
1888 }
1889
1890 // Indicate that we need a dynsym index.
1891 void
1892 set_needs_dynsym_index()
1893 { this->needs_dynsym_index_ = true; }
1894
1895 // Return whether we need a dynsym index.
1896 bool
1897 needs_dynsym_index() const
1898 { return this->needs_dynsym_index_; }
1899
1900 // Get the dynsym index.
1901 unsigned int
1902 dynsym_index() const
1903 {
1904 gold_assert(this->dynsym_index_ != 0);
1905 return this->dynsym_index_;
1906 }
1907
1908 // Set the dynsym index.
1909 void
1910 set_dynsym_index(unsigned int index)
1911 {
1912 gold_assert(index != 0);
1913 this->dynsym_index_ = index;
1914 }
1915
1916 // Return whether the input sections sections attachd to this output
1917 // section may require sorting. This is used to handle constructor
1918 // priorities compatibly with GNU ld.
1919 bool
1920 may_sort_attached_input_sections() const
1921 { return this->may_sort_attached_input_sections_; }
1922
1923 // Record that the input sections attached to this output section
1924 // may require sorting.
1925 void
1926 set_may_sort_attached_input_sections()
1927 { this->may_sort_attached_input_sections_ = true; }
1928
1929 // Return whether the input sections attached to this output section
1930 // require sorting. This is used to handle constructor priorities
1931 // compatibly with GNU ld.
1932 bool
1933 must_sort_attached_input_sections() const
1934 { return this->must_sort_attached_input_sections_; }
1935
1936 // Record that the input sections attached to this output section
1937 // require sorting.
1938 void
1939 set_must_sort_attached_input_sections()
1940 { this->must_sort_attached_input_sections_ = true; }
1941
1942 // Return whether this section should be written after all the input
1943 // sections are complete.
1944 bool
1945 after_input_sections() const
1946 { return this->after_input_sections_; }
1947
1948 // Record that this section should be written after all the input
1949 // sections are complete.
1950 void
1951 set_after_input_sections()
1952 { this->after_input_sections_ = true; }
1953
1954 // Return whether this section requires postprocessing after all
1955 // relocations have been applied.
1956 bool
1957 requires_postprocessing() const
1958 { return this->requires_postprocessing_; }
1959
1960 // If a section requires postprocessing, return the buffer to use.
1961 unsigned char*
1962 postprocessing_buffer() const
1963 {
1964 gold_assert(this->postprocessing_buffer_ != NULL);
1965 return this->postprocessing_buffer_;
1966 }
1967
1968 // If a section requires postprocessing, create the buffer to use.
1969 void
1970 create_postprocessing_buffer();
1971
1972 // If a section requires postprocessing, this is the size of the
1973 // buffer to which relocations should be applied.
1974 off_t
1975 postprocessing_buffer_size() const
1976 { return this->current_data_size_for_child(); }
1977
1978 // Modify the section name. This is only permitted for an
1979 // unallocated section, and only before the size has been finalized.
1980 // Otherwise the name will not get into Layout::namepool_.
1981 void
1982 set_name(const char* newname)
1983 {
1984 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1985 gold_assert(!this->is_data_size_valid());
1986 this->name_ = newname;
1987 }
1988
1989 // Return whether the offset OFFSET in the input section SHNDX in
1990 // object OBJECT is being included in the link.
1991 bool
1992 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1993 off_t offset) const;
1994
1995 // Return the offset within the output section of OFFSET relative to
1996 // the start of input section SHNDX in object OBJECT.
1997 section_offset_type
1998 output_offset(const Relobj* object, unsigned int shndx,
1999 section_offset_type offset) const;
2000
2001 // Return the output virtual address of OFFSET relative to the start
2002 // of input section SHNDX in object OBJECT.
2003 uint64_t
2004 output_address(const Relobj* object, unsigned int shndx,
2005 off_t offset) const;
2006
2007 // Return the output address of the start of the merged section for
2008 // input section SHNDX in object OBJECT. This is not necessarily
2009 // the offset corresponding to input offset 0 in the section, since
2010 // the section may be mapped arbitrarily.
2011 uint64_t
2012 starting_output_address(const Relobj* object, unsigned int shndx) const;
2013
2014 // Record that this output section was found in the SECTIONS clause
2015 // of a linker script.
2016 void
2017 set_found_in_sections_clause()
2018 { this->found_in_sections_clause_ = true; }
2019
2020 // Return whether this output section was found in the SECTIONS
2021 // clause of a linker script.
2022 bool
2023 found_in_sections_clause() const
2024 { return this->found_in_sections_clause_; }
2025
2026 // Write the section header into *OPHDR.
2027 template<int size, bool big_endian>
2028 void
2029 write_header(const Layout*, const Stringpool*,
2030 elfcpp::Shdr_write<size, big_endian>*) const;
2031
2032 // The next few calls are for linker script support.
2033
2034 // Store the list of input sections for this Output_section into the
2035 // list passed in. This removes the input sections, leaving only
2036 // any Output_section_data elements. This returns the size of those
2037 // Output_section_data elements. ADDRESS is the address of this
2038 // output section. FILL is the fill value to use, in case there are
2039 // any spaces between the remaining Output_section_data elements.
2040 uint64_t
2041 get_input_sections(uint64_t address, const std::string& fill,
2042 std::list<std::pair<Relobj*, unsigned int > >*);
2043
2044 // Add an input section from a script.
2045 void
2046 add_input_section_for_script(Relobj* object, unsigned int shndx,
2047 off_t data_size, uint64_t addralign);
2048
2049 // Set the current size of the output section.
2050 void
2051 set_current_data_size(off_t size)
2052 { this->set_current_data_size_for_child(size); }
2053
2054 // Get the current size of the output section.
2055 off_t
2056 current_data_size() const
2057 { return this->current_data_size_for_child(); }
2058
2059 // End of linker script support.
2060
2061 // Print merge statistics to stderr.
2062 void
2063 print_merge_stats();
2064
2065 protected:
2066 // Return the output section--i.e., the object itself.
2067 Output_section*
2068 do_output_section()
2069 { return this; }
2070
2071 // Return the section index in the output file.
2072 unsigned int
2073 do_out_shndx() const
2074 {
2075 gold_assert(this->out_shndx_ != -1U);
2076 return this->out_shndx_;
2077 }
2078
2079 // Set the output section index.
2080 void
2081 do_set_out_shndx(unsigned int shndx)
2082 {
2083 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2084 this->out_shndx_ = shndx;
2085 }
2086
2087 // Set the final data size of the Output_section. For a typical
2088 // Output_section, there is nothing to do, but if there are any
2089 // Output_section_data objects we need to set their final addresses
2090 // here.
2091 virtual void
2092 set_final_data_size();
2093
2094 // Reset the address and file offset.
2095 void
2096 do_reset_address_and_file_offset();
2097
2098 // Write the data to the file. For a typical Output_section, this
2099 // does nothing: the data is written out by calling Object::Relocate
2100 // on each input object. But if there are any Output_section_data
2101 // objects we do need to write them out here.
2102 virtual void
2103 do_write(Output_file*);
2104
2105 // Return the address alignment--function required by parent class.
2106 uint64_t
2107 do_addralign() const
2108 { return this->addralign_; }
2109
2110 // Return whether there is a load address.
2111 bool
2112 do_has_load_address() const
2113 { return this->has_load_address_; }
2114
2115 // Return the load address.
2116 uint64_t
2117 do_load_address() const
2118 {
2119 gold_assert(this->has_load_address_);
2120 return this->load_address_;
2121 }
2122
2123 // Return whether this is an Output_section.
2124 bool
2125 do_is_section() const
2126 { return true; }
2127
2128 // Return whether this is a section of the specified type.
2129 bool
2130 do_is_section_type(elfcpp::Elf_Word type) const
2131 { return this->type_ == type; }
2132
2133 // Return whether the specified section flag is set.
2134 bool
2135 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2136 { return (this->flags_ & flag) != 0; }
2137
2138 // Set the TLS offset. Called only for SHT_TLS sections.
2139 void
2140 do_set_tls_offset(uint64_t tls_base);
2141
2142 // Return the TLS offset, relative to the base of the TLS segment.
2143 // Valid only for SHT_TLS sections.
2144 uint64_t
2145 do_tls_offset() const
2146 { return this->tls_offset_; }
2147
2148 // This may be implemented by a child class.
2149 virtual void
2150 do_finalize_name(Layout*)
2151 { }
2152
2153 // Record that this section requires postprocessing after all
2154 // relocations have been applied. This is called by a child class.
2155 void
2156 set_requires_postprocessing()
2157 {
2158 this->requires_postprocessing_ = true;
2159 this->after_input_sections_ = true;
2160 }
2161
2162 // Write all the data of an Output_section into the postprocessing
2163 // buffer.
2164 void
2165 write_to_postprocessing_buffer();
2166
2167 private:
2168 // In some cases we need to keep a list of the input sections
2169 // associated with this output section. We only need the list if we
2170 // might have to change the offsets of the input section within the
2171 // output section after we add the input section. The ordinary
2172 // input sections will be written out when we process the object
2173 // file, and as such we don't need to track them here. We do need
2174 // to track Output_section_data objects here. We store instances of
2175 // this structure in a std::vector, so it must be a POD. There can
2176 // be many instances of this structure, so we use a union to save
2177 // some space.
2178 class Input_section
2179 {
2180 public:
2181 Input_section()
2182 : shndx_(0), p2align_(0)
2183 {
2184 this->u1_.data_size = 0;
2185 this->u2_.object = NULL;
2186 }
2187
2188 // For an ordinary input section.
2189 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2190 uint64_t addralign)
2191 : shndx_(shndx),
2192 p2align_(ffsll(static_cast<long long>(addralign)))
2193 {
2194 gold_assert(shndx != OUTPUT_SECTION_CODE
2195 && shndx != MERGE_DATA_SECTION_CODE
2196 && shndx != MERGE_STRING_SECTION_CODE);
2197 this->u1_.data_size = data_size;
2198 this->u2_.object = object;
2199 }
2200
2201 // For a non-merge output section.
2202 Input_section(Output_section_data* posd)
2203 : shndx_(OUTPUT_SECTION_CODE),
2204 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2205 {
2206 this->u1_.data_size = 0;
2207 this->u2_.posd = posd;
2208 }
2209
2210 // For a merge section.
2211 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2212 : shndx_(is_string
2213 ? MERGE_STRING_SECTION_CODE
2214 : MERGE_DATA_SECTION_CODE),
2215 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2216 {
2217 this->u1_.entsize = entsize;
2218 this->u2_.posd = posd;
2219 }
2220
2221 // The required alignment.
2222 uint64_t
2223 addralign() const
2224 {
2225 return (this->p2align_ == 0
2226 ? 0
2227 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2228 }
2229
2230 // Return the required size.
2231 off_t
2232 data_size() const;
2233
2234 // Whether this is an input section.
2235 bool
2236 is_input_section() const
2237 {
2238 return (this->shndx_ != OUTPUT_SECTION_CODE
2239 && this->shndx_ != MERGE_DATA_SECTION_CODE
2240 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2241 }
2242
2243 // Return whether this is a merge section which matches the
2244 // parameters.
2245 bool
2246 is_merge_section(bool is_string, uint64_t entsize,
2247 uint64_t addralign) const
2248 {
2249 return (this->shndx_ == (is_string
2250 ? MERGE_STRING_SECTION_CODE
2251 : MERGE_DATA_SECTION_CODE)
2252 && this->u1_.entsize == entsize
2253 && this->addralign() == addralign);
2254 }
2255
2256 // Return the object for an input section.
2257 Relobj*
2258 relobj() const
2259 {
2260 gold_assert(this->is_input_section());
2261 return this->u2_.object;
2262 }
2263
2264 // Return the input section index for an input section.
2265 unsigned int
2266 shndx() const
2267 {
2268 gold_assert(this->is_input_section());
2269 return this->shndx_;
2270 }
2271
2272 // Set the output section.
2273 void
2274 set_output_section(Output_section* os)
2275 {
2276 gold_assert(!this->is_input_section());
2277 this->u2_.posd->set_output_section(os);
2278 }
2279
2280 // Set the address and file offset. This is called during
2281 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2282 // the enclosing section.
2283 void
2284 set_address_and_file_offset(uint64_t address, off_t file_offset,
2285 off_t section_file_offset);
2286
2287 // Reset the address and file offset.
2288 void
2289 reset_address_and_file_offset();
2290
2291 // Finalize the data size.
2292 void
2293 finalize_data_size();
2294
2295 // Add an input section, for SHF_MERGE sections.
2296 bool
2297 add_input_section(Relobj* object, unsigned int shndx)
2298 {
2299 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2300 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2301 return this->u2_.posd->add_input_section(object, shndx);
2302 }
2303
2304 // Given an input OBJECT, an input section index SHNDX within that
2305 // object, and an OFFSET relative to the start of that input
2306 // section, return whether or not the output offset is known. If
2307 // this function returns true, it sets *POUTPUT to the offset in
2308 // the output section, relative to the start of the input section
2309 // in the output section. *POUTPUT may be different from OFFSET
2310 // for a merged section.
2311 bool
2312 output_offset(const Relobj* object, unsigned int shndx,
2313 section_offset_type offset,
2314 section_offset_type *poutput) const;
2315
2316 // Return whether this is the merge section for the input section
2317 // SHNDX in OBJECT.
2318 bool
2319 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2320
2321 // Write out the data. This does nothing for an input section.
2322 void
2323 write(Output_file*);
2324
2325 // Write the data to a buffer. This does nothing for an input
2326 // section.
2327 void
2328 write_to_buffer(unsigned char*);
2329
2330 // Print statistics about merge sections to stderr.
2331 void
2332 print_merge_stats(const char* section_name)
2333 {
2334 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2335 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2336 this->u2_.posd->print_merge_stats(section_name);
2337 }
2338
2339 private:
2340 // Code values which appear in shndx_. If the value is not one of
2341 // these codes, it is the input section index in the object file.
2342 enum
2343 {
2344 // An Output_section_data.
2345 OUTPUT_SECTION_CODE = -1U,
2346 // An Output_section_data for an SHF_MERGE section with
2347 // SHF_STRINGS not set.
2348 MERGE_DATA_SECTION_CODE = -2U,
2349 // An Output_section_data for an SHF_MERGE section with
2350 // SHF_STRINGS set.
2351 MERGE_STRING_SECTION_CODE = -3U
2352 };
2353
2354 // For an ordinary input section, this is the section index in the
2355 // input file. For an Output_section_data, this is
2356 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2357 // MERGE_STRING_SECTION_CODE.
2358 unsigned int shndx_;
2359 // The required alignment, stored as a power of 2.
2360 unsigned int p2align_;
2361 union
2362 {
2363 // For an ordinary input section, the section size.
2364 off_t data_size;
2365 // For OUTPUT_SECTION_CODE, this is not used. For
2366 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2367 // entity size.
2368 uint64_t entsize;
2369 } u1_;
2370 union
2371 {
2372 // For an ordinary input section, the object which holds the
2373 // input section.
2374 Relobj* object;
2375 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2376 // MERGE_STRING_SECTION_CODE, the data.
2377 Output_section_data* posd;
2378 } u2_;
2379 };
2380
2381 typedef std::vector<Input_section> Input_section_list;
2382
2383 // This class is used to sort the input sections.
2384 class Input_section_sort_entry;
2385
2386 // This is the sort comparison function.
2387 struct Input_section_sort_compare
2388 {
2389 bool
2390 operator()(const Input_section_sort_entry&,
2391 const Input_section_sort_entry&) const;
2392 };
2393
2394 // Fill data. This is used to fill in data between input sections.
2395 // It is also used for data statements (BYTE, WORD, etc.) in linker
2396 // scripts. When we have to keep track of the input sections, we
2397 // can use an Output_data_const, but we don't want to have to keep
2398 // track of input sections just to implement fills.
2399 class Fill
2400 {
2401 public:
2402 Fill(off_t section_offset, off_t length)
2403 : section_offset_(section_offset),
2404 length_(convert_to_section_size_type(length))
2405 { }
2406
2407 // Return section offset.
2408 off_t
2409 section_offset() const
2410 { return this->section_offset_; }
2411
2412 // Return fill length.
2413 section_size_type
2414 length() const
2415 { return this->length_; }
2416
2417 private:
2418 // The offset within the output section.
2419 off_t section_offset_;
2420 // The length of the space to fill.
2421 section_size_type length_;
2422 };
2423
2424 typedef std::vector<Fill> Fill_list;
2425
2426 // Add a new output section by Input_section.
2427 void
2428 add_output_section_data(Input_section*);
2429
2430 // Add an SHF_MERGE input section. Returns true if the section was
2431 // handled.
2432 bool
2433 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2434 uint64_t entsize, uint64_t addralign);
2435
2436 // Add an output SHF_MERGE section POSD to this output section.
2437 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2438 // ENTSIZE is the entity size. This returns the entry added to
2439 // input_sections_.
2440 void
2441 add_output_merge_section(Output_section_data* posd, bool is_string,
2442 uint64_t entsize);
2443
2444 // Sort the attached input sections.
2445 void
2446 sort_attached_input_sections();
2447
2448 // Most of these fields are only valid after layout.
2449
2450 // The name of the section. This will point into a Stringpool.
2451 const char* name_;
2452 // The section address is in the parent class.
2453 // The section alignment.
2454 uint64_t addralign_;
2455 // The section entry size.
2456 uint64_t entsize_;
2457 // The load address. This is only used when using a linker script
2458 // with a SECTIONS clause. The has_load_address_ field indicates
2459 // whether this field is valid.
2460 uint64_t load_address_;
2461 // The file offset is in the parent class.
2462 // Set the section link field to the index of this section.
2463 const Output_data* link_section_;
2464 // If link_section_ is NULL, this is the link field.
2465 unsigned int link_;
2466 // Set the section info field to the index of this section.
2467 const Output_section* info_section_;
2468 // If info_section_ is NULL, set the info field to the symbol table
2469 // index of this symbol.
2470 const Symbol* info_symndx_;
2471 // If info_section_ and info_symndx_ are NULL, this is the section
2472 // info field.
2473 unsigned int info_;
2474 // The section type.
2475 const elfcpp::Elf_Word type_;
2476 // The section flags.
2477 elfcpp::Elf_Xword flags_;
2478 // The section index.
2479 unsigned int out_shndx_;
2480 // If there is a STT_SECTION for this output section in the normal
2481 // symbol table, this is the symbol index. This starts out as zero.
2482 // It is initialized in Layout::finalize() to be the index, or -1U
2483 // if there isn't one.
2484 unsigned int symtab_index_;
2485 // If there is a STT_SECTION for this output section in the dynamic
2486 // symbol table, this is the symbol index. This starts out as zero.
2487 // It is initialized in Layout::finalize() to be the index, or -1U
2488 // if there isn't one.
2489 unsigned int dynsym_index_;
2490 // The input sections. This will be empty in cases where we don't
2491 // need to keep track of them.
2492 Input_section_list input_sections_;
2493 // The offset of the first entry in input_sections_.
2494 off_t first_input_offset_;
2495 // The fill data. This is separate from input_sections_ because we
2496 // often will need fill sections without needing to keep track of
2497 // input sections.
2498 Fill_list fills_;
2499 // If the section requires postprocessing, this buffer holds the
2500 // section contents during relocation.
2501 unsigned char* postprocessing_buffer_;
2502 // Whether this output section needs a STT_SECTION symbol in the
2503 // normal symbol table. This will be true if there is a relocation
2504 // which needs it.
2505 bool needs_symtab_index_ : 1;
2506 // Whether this output section needs a STT_SECTION symbol in the
2507 // dynamic symbol table. This will be true if there is a dynamic
2508 // relocation which needs it.
2509 bool needs_dynsym_index_ : 1;
2510 // Whether the link field of this output section should point to the
2511 // normal symbol table.
2512 bool should_link_to_symtab_ : 1;
2513 // Whether the link field of this output section should point to the
2514 // dynamic symbol table.
2515 bool should_link_to_dynsym_ : 1;
2516 // Whether this section should be written after all the input
2517 // sections are complete.
2518 bool after_input_sections_ : 1;
2519 // Whether this section requires post processing after all
2520 // relocations have been applied.
2521 bool requires_postprocessing_ : 1;
2522 // Whether an input section was mapped to this output section
2523 // because of a SECTIONS clause in a linker script.
2524 bool found_in_sections_clause_ : 1;
2525 // Whether this section has an explicitly specified load address.
2526 bool has_load_address_ : 1;
2527 // True if the info_section_ field means the section index of the
2528 // section, false if it means the symbol index of the corresponding
2529 // section symbol.
2530 bool info_uses_section_index_ : 1;
2531 // True if the input sections attached to this output section may
2532 // need sorting.
2533 bool may_sort_attached_input_sections_ : 1;
2534 // True if the input sections attached to this output section must
2535 // be sorted.
2536 bool must_sort_attached_input_sections_ : 1;
2537 // True if the input sections attached to this output section have
2538 // already been sorted.
2539 bool attached_input_sections_are_sorted_ : 1;
2540 // For SHT_TLS sections, the offset of this section relative to the base
2541 // of the TLS segment.
2542 uint64_t tls_offset_;
2543 };
2544
2545 // An output segment. PT_LOAD segments are built from collections of
2546 // output sections. Other segments typically point within PT_LOAD
2547 // segments, and are built directly as needed.
2548
2549 class Output_segment
2550 {
2551 public:
2552 // Create an output segment, specifying the type and flags.
2553 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2554
2555 // Return the virtual address.
2556 uint64_t
2557 vaddr() const
2558 { return this->vaddr_; }
2559
2560 // Return the physical address.
2561 uint64_t
2562 paddr() const
2563 { return this->paddr_; }
2564
2565 // Return the segment type.
2566 elfcpp::Elf_Word
2567 type() const
2568 { return this->type_; }
2569
2570 // Return the segment flags.
2571 elfcpp::Elf_Word
2572 flags() const
2573 { return this->flags_; }
2574
2575 // Return the memory size.
2576 uint64_t
2577 memsz() const
2578 { return this->memsz_; }
2579
2580 // Return the file size.
2581 off_t
2582 filesz() const
2583 { return this->filesz_; }
2584
2585 // Return the file offset.
2586 off_t
2587 offset() const
2588 { return this->offset_; }
2589
2590 // Return the maximum alignment of the Output_data.
2591 uint64_t
2592 maximum_alignment();
2593
2594 // Add an Output_section to this segment.
2595 void
2596 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2597 { this->add_output_section(os, seg_flags, false); }
2598
2599 // Add an Output_section to the start of this segment.
2600 void
2601 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2602 { this->add_output_section(os, seg_flags, true); }
2603
2604 // Remove an Output_section from this segment. It is an error if it
2605 // is not present.
2606 void
2607 remove_output_section(Output_section* os);
2608
2609 // Add an Output_data (which is not an Output_section) to the start
2610 // of this segment.
2611 void
2612 add_initial_output_data(Output_data*);
2613
2614 // Return true if this segment has any sections which hold actual
2615 // data, rather than being a BSS section.
2616 bool
2617 has_any_data_sections() const
2618 { return !this->output_data_.empty(); }
2619
2620 // Return the number of dynamic relocations applied to this segment.
2621 unsigned int
2622 dynamic_reloc_count() const;
2623
2624 // Return the address of the first section.
2625 uint64_t
2626 first_section_load_address() const;
2627
2628 // Return whether the addresses have been set already.
2629 bool
2630 are_addresses_set() const
2631 { return this->are_addresses_set_; }
2632
2633 // Set the addresses.
2634 void
2635 set_addresses(uint64_t vaddr, uint64_t paddr)
2636 {
2637 this->vaddr_ = vaddr;
2638 this->paddr_ = paddr;
2639 this->are_addresses_set_ = true;
2640 }
2641
2642 // Set the segment flags. This is only used if we have a PHDRS
2643 // clause which explicitly specifies the flags.
2644 void
2645 set_flags(elfcpp::Elf_Word flags)
2646 { this->flags_ = flags; }
2647
2648 // Set the address of the segment to ADDR and the offset to *POFF
2649 // and set the addresses and offsets of all contained output
2650 // sections accordingly. Set the section indexes of all contained
2651 // output sections starting with *PSHNDX. If RESET is true, first
2652 // reset the addresses of the contained sections. Return the
2653 // address of the immediately following segment. Update *POFF and
2654 // *PSHNDX. This should only be called for a PT_LOAD segment.
2655 uint64_t
2656 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
2657 unsigned int* pshndx);
2658
2659 // Set the minimum alignment of this segment. This may be adjusted
2660 // upward based on the section alignments.
2661 void
2662 set_minimum_p_align(uint64_t align)
2663 { this->min_p_align_ = align; }
2664
2665 // Set the offset of this segment based on the section. This should
2666 // only be called for a non-PT_LOAD segment.
2667 void
2668 set_offset();
2669
2670 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2671 void
2672 set_tls_offsets();
2673
2674 // Return the number of output sections.
2675 unsigned int
2676 output_section_count() const;
2677
2678 // Return the section attached to the list segment with the lowest
2679 // load address. This is used when handling a PHDRS clause in a
2680 // linker script.
2681 Output_section*
2682 section_with_lowest_load_address() const;
2683
2684 // Write the segment header into *OPHDR.
2685 template<int size, bool big_endian>
2686 void
2687 write_header(elfcpp::Phdr_write<size, big_endian>*);
2688
2689 // Write the section headers of associated sections into V.
2690 template<int size, bool big_endian>
2691 unsigned char*
2692 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2693 unsigned int* pshndx) const;
2694
2695 private:
2696 Output_segment(const Output_segment&);
2697 Output_segment& operator=(const Output_segment&);
2698
2699 typedef std::list<Output_data*> Output_data_list;
2700
2701 // Add an Output_section to this segment, specifying front or back.
2702 void
2703 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2704 bool front);
2705
2706 // Find the maximum alignment in an Output_data_list.
2707 static uint64_t
2708 maximum_alignment_list(const Output_data_list*);
2709
2710 // Set the section addresses in an Output_data_list.
2711 uint64_t
2712 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2713 uint64_t addr, off_t* poff, unsigned int* pshndx,
2714 bool* in_tls);
2715
2716 // Return the number of Output_sections in an Output_data_list.
2717 unsigned int
2718 output_section_count_list(const Output_data_list*) const;
2719
2720 // Return the number of dynamic relocs in an Output_data_list.
2721 unsigned int
2722 dynamic_reloc_count_list(const Output_data_list*) const;
2723
2724 // Find the section with the lowest load address in an
2725 // Output_data_list.
2726 void
2727 lowest_load_address_in_list(const Output_data_list* pdl,
2728 Output_section** found,
2729 uint64_t* found_lma) const;
2730
2731 // Write the section headers in the list into V.
2732 template<int size, bool big_endian>
2733 unsigned char*
2734 write_section_headers_list(const Layout*, const Stringpool*,
2735 const Output_data_list*, unsigned char* v,
2736 unsigned int* pshdx) const;
2737
2738 // The list of output data with contents attached to this segment.
2739 Output_data_list output_data_;
2740 // The list of output data without contents attached to this segment.
2741 Output_data_list output_bss_;
2742 // The segment virtual address.
2743 uint64_t vaddr_;
2744 // The segment physical address.
2745 uint64_t paddr_;
2746 // The size of the segment in memory.
2747 uint64_t memsz_;
2748 // The maximum section alignment. The is_max_align_known_ field
2749 // indicates whether this has been finalized.
2750 uint64_t max_align_;
2751 // The required minimum value for the p_align field. This is used
2752 // for PT_LOAD segments. Note that this does not mean that
2753 // addresses should be aligned to this value; it means the p_paddr
2754 // and p_vaddr fields must be congruent modulo this value. For
2755 // non-PT_LOAD segments, the dynamic linker works more efficiently
2756 // if the p_align field has the more conventional value, although it
2757 // can align as needed.
2758 uint64_t min_p_align_;
2759 // The offset of the segment data within the file.
2760 off_t offset_;
2761 // The size of the segment data in the file.
2762 off_t filesz_;
2763 // The segment type;
2764 elfcpp::Elf_Word type_;
2765 // The segment flags.
2766 elfcpp::Elf_Word flags_;
2767 // Whether we have finalized max_align_.
2768 bool is_max_align_known_ : 1;
2769 // Whether vaddr and paddr were set by a linker script.
2770 bool are_addresses_set_ : 1;
2771 };
2772
2773 // This class represents the output file.
2774
2775 class Output_file
2776 {
2777 public:
2778 Output_file(const char* name);
2779
2780 // Indicate that this is a temporary file which should not be
2781 // output.
2782 void
2783 set_is_temporary()
2784 { this->is_temporary_ = true; }
2785
2786 // Open the output file. FILE_SIZE is the final size of the file.
2787 void
2788 open(off_t file_size);
2789
2790 // Resize the output file.
2791 void
2792 resize(off_t file_size);
2793
2794 // Close the output file (flushing all buffered data) and make sure
2795 // there are no errors.
2796 void
2797 close();
2798
2799 // We currently always use mmap which makes the view handling quite
2800 // simple. In the future we may support other approaches.
2801
2802 // Write data to the output file.
2803 void
2804 write(off_t offset, const void* data, size_t len)
2805 { memcpy(this->base_ + offset, data, len); }
2806
2807 // Get a buffer to use to write to the file, given the offset into
2808 // the file and the size.
2809 unsigned char*
2810 get_output_view(off_t start, size_t size)
2811 {
2812 gold_assert(start >= 0
2813 && start + static_cast<off_t>(size) <= this->file_size_);
2814 return this->base_ + start;
2815 }
2816
2817 // VIEW must have been returned by get_output_view. Write the
2818 // buffer to the file, passing in the offset and the size.
2819 void
2820 write_output_view(off_t, size_t, unsigned char*)
2821 { }
2822
2823 // Get a read/write buffer. This is used when we want to write part
2824 // of the file, read it in, and write it again.
2825 unsigned char*
2826 get_input_output_view(off_t start, size_t size)
2827 { return this->get_output_view(start, size); }
2828
2829 // Write a read/write buffer back to the file.
2830 void
2831 write_input_output_view(off_t, size_t, unsigned char*)
2832 { }
2833
2834 // Get a read buffer. This is used when we just want to read part
2835 // of the file back it in.
2836 const unsigned char*
2837 get_input_view(off_t start, size_t size)
2838 { return this->get_output_view(start, size); }
2839
2840 // Release a read bfufer.
2841 void
2842 free_input_view(off_t, size_t, const unsigned char*)
2843 { }
2844
2845 private:
2846 // Map the file into memory and return a pointer to the map.
2847 void
2848 map();
2849
2850 // Unmap the file from memory (and flush to disk buffers).
2851 void
2852 unmap();
2853
2854 // File name.
2855 const char* name_;
2856 // File descriptor.
2857 int o_;
2858 // File size.
2859 off_t file_size_;
2860 // Base of file mapped into memory.
2861 unsigned char* base_;
2862 // True iff base_ points to a memory buffer rather than an output file.
2863 bool map_is_anonymous_;
2864 // True if this is a temporary file which should not be output.
2865 bool is_temporary_;
2866 };
2867
2868 } // End namespace gold.
2869
2870 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.13899 seconds and 4 git commands to generate.