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