Copyright update for binutils
[deliverable/binutils-gdb.git] / gold / output.h
1 // output.h -- manage the output file for gold -*- C++ -*-
2
3 // Copyright (C) 2006-2016 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 "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
33
34 namespace gold
35 {
36
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_merge_base;
42 class Output_section;
43 class Relocatable_relocs;
44 class Target;
45 template<int size, bool big_endian>
46 class Sized_target;
47 template<int size, bool big_endian>
48 class Sized_relobj;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
51
52 // An abtract class for data which has to go into the output file.
53
54 class Output_data
55 {
56 public:
57 explicit Output_data()
58 : address_(0), data_size_(0), offset_(-1),
59 is_address_valid_(false), is_data_size_valid_(false),
60 is_offset_valid_(false), is_data_size_fixed_(false),
61 has_dynamic_reloc_(false)
62 { }
63
64 virtual
65 ~Output_data();
66
67 // Return the address. For allocated sections, this is only valid
68 // after Layout::finalize is finished.
69 uint64_t
70 address() const
71 {
72 gold_assert(this->is_address_valid_);
73 return this->address_;
74 }
75
76 // Return the size of the data. For allocated sections, this must
77 // be valid after Layout::finalize calls set_address, but need not
78 // be valid before then.
79 off_t
80 data_size() const
81 {
82 gold_assert(this->is_data_size_valid_);
83 return this->data_size_;
84 }
85
86 // Get the current data size.
87 off_t
88 current_data_size() const
89 { return this->current_data_size_for_child(); }
90
91 // Return true if data size is fixed.
92 bool
93 is_data_size_fixed() const
94 { return this->is_data_size_fixed_; }
95
96 // Return the file offset. This is only valid after
97 // Layout::finalize is finished. For some non-allocated sections,
98 // it may not be valid until near the end of the link.
99 off_t
100 offset() const
101 {
102 gold_assert(this->is_offset_valid_);
103 return this->offset_;
104 }
105
106 // Reset the address, file offset and data size. This essentially
107 // disables the sanity testing about duplicate and unknown settings.
108 void
109 reset_address_and_file_offset()
110 {
111 this->is_address_valid_ = false;
112 this->is_offset_valid_ = false;
113 if (!this->is_data_size_fixed_)
114 this->is_data_size_valid_ = false;
115 this->do_reset_address_and_file_offset();
116 }
117
118 // As above, but just for data size.
119 void
120 reset_data_size()
121 {
122 if (!this->is_data_size_fixed_)
123 this->is_data_size_valid_ = false;
124 }
125
126 // Return true if address and file offset already have reset values. In
127 // other words, calling reset_address_and_file_offset will not change them.
128 bool
129 address_and_file_offset_have_reset_values() const
130 { return this->do_address_and_file_offset_have_reset_values(); }
131
132 // Return the required alignment.
133 uint64_t
134 addralign() const
135 { return this->do_addralign(); }
136
137 // Return whether this has a load address.
138 bool
139 has_load_address() const
140 { return this->do_has_load_address(); }
141
142 // Return the load address.
143 uint64_t
144 load_address() const
145 { return this->do_load_address(); }
146
147 // Return whether this is an Output_section.
148 bool
149 is_section() const
150 { return this->do_is_section(); }
151
152 // Return whether this is an Output_section of the specified type.
153 bool
154 is_section_type(elfcpp::Elf_Word stt) const
155 { return this->do_is_section_type(stt); }
156
157 // Return whether this is an Output_section with the specified flag
158 // set.
159 bool
160 is_section_flag_set(elfcpp::Elf_Xword shf) const
161 { return this->do_is_section_flag_set(shf); }
162
163 // Return the output section that this goes in, if there is one.
164 Output_section*
165 output_section()
166 { return this->do_output_section(); }
167
168 const Output_section*
169 output_section() const
170 { return this->do_output_section(); }
171
172 // Return the output section index, if there is an output section.
173 unsigned int
174 out_shndx() const
175 { return this->do_out_shndx(); }
176
177 // Set the output section index, if this is an output section.
178 void
179 set_out_shndx(unsigned int shndx)
180 { this->do_set_out_shndx(shndx); }
181
182 // Set the address and file offset of this data, and finalize the
183 // size of the data. This is called during Layout::finalize for
184 // allocated sections.
185 void
186 set_address_and_file_offset(uint64_t addr, off_t off)
187 {
188 this->set_address(addr);
189 this->set_file_offset(off);
190 this->finalize_data_size();
191 }
192
193 // Set the address.
194 void
195 set_address(uint64_t addr)
196 {
197 gold_assert(!this->is_address_valid_);
198 this->address_ = addr;
199 this->is_address_valid_ = true;
200 }
201
202 // Set the file offset.
203 void
204 set_file_offset(off_t off)
205 {
206 gold_assert(!this->is_offset_valid_);
207 this->offset_ = off;
208 this->is_offset_valid_ = true;
209 }
210
211 // Update the data size without finalizing it.
212 void
213 pre_finalize_data_size()
214 {
215 if (!this->is_data_size_valid_)
216 {
217 // Tell the child class to update the data size.
218 this->update_data_size();
219 }
220 }
221
222 // Finalize the data size.
223 void
224 finalize_data_size()
225 {
226 if (!this->is_data_size_valid_)
227 {
228 // Tell the child class to set the data size.
229 this->set_final_data_size();
230 gold_assert(this->is_data_size_valid_);
231 }
232 }
233
234 // Set the TLS offset. Called only for SHT_TLS sections.
235 void
236 set_tls_offset(uint64_t tls_base)
237 { this->do_set_tls_offset(tls_base); }
238
239 // Return the TLS offset, relative to the base of the TLS segment.
240 // Valid only for SHT_TLS sections.
241 uint64_t
242 tls_offset() const
243 { return this->do_tls_offset(); }
244
245 // Write the data to the output file. This is called after
246 // Layout::finalize is complete.
247 void
248 write(Output_file* file)
249 { this->do_write(file); }
250
251 // This is called by Layout::finalize to note that the sizes of
252 // allocated sections must now be fixed.
253 static void
254 layout_complete()
255 { Output_data::allocated_sizes_are_fixed = true; }
256
257 // Used to check that layout has been done.
258 static bool
259 is_layout_complete()
260 { return Output_data::allocated_sizes_are_fixed; }
261
262 // Note that a dynamic reloc has been applied to this data.
263 void
264 add_dynamic_reloc()
265 { this->has_dynamic_reloc_ = true; }
266
267 // Return whether a dynamic reloc has been applied.
268 bool
269 has_dynamic_reloc() const
270 { return this->has_dynamic_reloc_; }
271
272 // Whether the address is valid.
273 bool
274 is_address_valid() const
275 { return this->is_address_valid_; }
276
277 // Whether the file offset is valid.
278 bool
279 is_offset_valid() const
280 { return this->is_offset_valid_; }
281
282 // Whether the data size is valid.
283 bool
284 is_data_size_valid() const
285 { return this->is_data_size_valid_; }
286
287 // Print information to the map file.
288 void
289 print_to_mapfile(Mapfile* mapfile) const
290 { return this->do_print_to_mapfile(mapfile); }
291
292 protected:
293 // Functions that child classes may or in some cases must implement.
294
295 // Write the data to the output file.
296 virtual void
297 do_write(Output_file*) = 0;
298
299 // Return the required alignment.
300 virtual uint64_t
301 do_addralign() const = 0;
302
303 // Return whether this has a load address.
304 virtual bool
305 do_has_load_address() const
306 { return false; }
307
308 // Return the load address.
309 virtual uint64_t
310 do_load_address() const
311 { gold_unreachable(); }
312
313 // Return whether this is an Output_section.
314 virtual bool
315 do_is_section() const
316 { return false; }
317
318 // Return whether this is an Output_section of the specified type.
319 // This only needs to be implement by Output_section.
320 virtual bool
321 do_is_section_type(elfcpp::Elf_Word) const
322 { return false; }
323
324 // Return whether this is an Output_section with the specific flag
325 // set. This only needs to be implemented by Output_section.
326 virtual bool
327 do_is_section_flag_set(elfcpp::Elf_Xword) const
328 { return false; }
329
330 // Return the output section, if there is one.
331 virtual Output_section*
332 do_output_section()
333 { return NULL; }
334
335 virtual const Output_section*
336 do_output_section() const
337 { return NULL; }
338
339 // Return the output section index, if there is an output section.
340 virtual unsigned int
341 do_out_shndx() const
342 { gold_unreachable(); }
343
344 // Set the output section index, if this is an output section.
345 virtual void
346 do_set_out_shndx(unsigned int)
347 { gold_unreachable(); }
348
349 // This is a hook for derived classes to set the preliminary data size.
350 // This is called by pre_finalize_data_size, normally called during
351 // Layout::finalize, before the section address is set, and is used
352 // during an incremental update, when we need to know the size of a
353 // section before allocating space in the output file. For classes
354 // where the current data size is up to date, this default version of
355 // the method can be inherited.
356 virtual void
357 update_data_size()
358 { }
359
360 // This is a hook for derived classes to set the data size. This is
361 // called by finalize_data_size, normally called during
362 // Layout::finalize, when the section address is set.
363 virtual void
364 set_final_data_size()
365 { gold_unreachable(); }
366
367 // A hook for resetting the address and file offset.
368 virtual void
369 do_reset_address_and_file_offset()
370 { }
371
372 // Return true if address and file offset already have reset values. In
373 // other words, calling reset_address_and_file_offset will not change them.
374 // A child class overriding do_reset_address_and_file_offset may need to
375 // also override this.
376 virtual bool
377 do_address_and_file_offset_have_reset_values() const
378 { return !this->is_address_valid_ && !this->is_offset_valid_; }
379
380 // Set the TLS offset. Called only for SHT_TLS sections.
381 virtual void
382 do_set_tls_offset(uint64_t)
383 { gold_unreachable(); }
384
385 // Return the TLS offset, relative to the base of the TLS segment.
386 // Valid only for SHT_TLS sections.
387 virtual uint64_t
388 do_tls_offset() const
389 { gold_unreachable(); }
390
391 // Print to the map file. This only needs to be implemented by
392 // classes which may appear in a PT_LOAD segment.
393 virtual void
394 do_print_to_mapfile(Mapfile*) const
395 { gold_unreachable(); }
396
397 // Functions that child classes may call.
398
399 // Reset the address. The Output_section class needs this when an
400 // SHF_ALLOC input section is added to an output section which was
401 // formerly not SHF_ALLOC.
402 void
403 mark_address_invalid()
404 { this->is_address_valid_ = false; }
405
406 // Set the size of the data.
407 void
408 set_data_size(off_t data_size)
409 {
410 gold_assert(!this->is_data_size_valid_
411 && !this->is_data_size_fixed_);
412 this->data_size_ = data_size;
413 this->is_data_size_valid_ = true;
414 }
415
416 // Fix the data size. Once it is fixed, it cannot be changed
417 // and the data size remains always valid.
418 void
419 fix_data_size()
420 {
421 gold_assert(this->is_data_size_valid_);
422 this->is_data_size_fixed_ = true;
423 }
424
425 // Get the current data size--this is for the convenience of
426 // sections which build up their size over time.
427 off_t
428 current_data_size_for_child() const
429 { return this->data_size_; }
430
431 // Set the current data size--this is for the convenience of
432 // sections which build up their size over time.
433 void
434 set_current_data_size_for_child(off_t data_size)
435 {
436 gold_assert(!this->is_data_size_valid_);
437 this->data_size_ = data_size;
438 }
439
440 // Return default alignment for the target size.
441 static uint64_t
442 default_alignment();
443
444 // Return default alignment for a specified size--32 or 64.
445 static uint64_t
446 default_alignment_for_size(int size);
447
448 private:
449 Output_data(const Output_data&);
450 Output_data& operator=(const Output_data&);
451
452 // This is used for verification, to make sure that we don't try to
453 // change any sizes of allocated sections after we set the section
454 // addresses.
455 static bool allocated_sizes_are_fixed;
456
457 // Memory address in output file.
458 uint64_t address_;
459 // Size of data in output file.
460 off_t data_size_;
461 // File offset of contents in output file.
462 off_t offset_;
463 // Whether address_ is valid.
464 bool is_address_valid_ : 1;
465 // Whether data_size_ is valid.
466 bool is_data_size_valid_ : 1;
467 // Whether offset_ is valid.
468 bool is_offset_valid_ : 1;
469 // Whether data size is fixed.
470 bool is_data_size_fixed_ : 1;
471 // Whether any dynamic relocs have been applied to this section.
472 bool has_dynamic_reloc_ : 1;
473 };
474
475 // Output the section headers.
476
477 class Output_section_headers : public Output_data
478 {
479 public:
480 Output_section_headers(const Layout*,
481 const Layout::Segment_list*,
482 const Layout::Section_list*,
483 const Layout::Section_list*,
484 const Stringpool*,
485 const Output_section*);
486
487 protected:
488 // Write the data to the file.
489 void
490 do_write(Output_file*);
491
492 // Return the required alignment.
493 uint64_t
494 do_addralign() const
495 { return Output_data::default_alignment(); }
496
497 // Write to a map file.
498 void
499 do_print_to_mapfile(Mapfile* mapfile) const
500 { mapfile->print_output_data(this, _("** section headers")); }
501
502 // Update the data size.
503 void
504 update_data_size()
505 { this->set_data_size(this->do_size()); }
506
507 // Set final data size.
508 void
509 set_final_data_size()
510 { this->set_data_size(this->do_size()); }
511
512 private:
513 // Write the data to the file with the right size and endianness.
514 template<int size, bool big_endian>
515 void
516 do_sized_write(Output_file*);
517
518 // Compute data size.
519 off_t
520 do_size() const;
521
522 const Layout* layout_;
523 const Layout::Segment_list* segment_list_;
524 const Layout::Section_list* section_list_;
525 const Layout::Section_list* unattached_section_list_;
526 const Stringpool* secnamepool_;
527 const Output_section* shstrtab_section_;
528 };
529
530 // Output the segment headers.
531
532 class Output_segment_headers : public Output_data
533 {
534 public:
535 Output_segment_headers(const Layout::Segment_list& segment_list);
536
537 protected:
538 // Write the data to the file.
539 void
540 do_write(Output_file*);
541
542 // Return the required alignment.
543 uint64_t
544 do_addralign() const
545 { return Output_data::default_alignment(); }
546
547 // Write to a map file.
548 void
549 do_print_to_mapfile(Mapfile* mapfile) const
550 { mapfile->print_output_data(this, _("** segment headers")); }
551
552 // Set final data size.
553 void
554 set_final_data_size()
555 { this->set_data_size(this->do_size()); }
556
557 private:
558 // Write the data to the file with the right size and endianness.
559 template<int size, bool big_endian>
560 void
561 do_sized_write(Output_file*);
562
563 // Compute the current size.
564 off_t
565 do_size() const;
566
567 const Layout::Segment_list& segment_list_;
568 };
569
570 // Output the ELF file header.
571
572 class Output_file_header : public Output_data
573 {
574 public:
575 Output_file_header(Target*,
576 const Symbol_table*,
577 const Output_segment_headers*);
578
579 // Add information about the section headers. We lay out the ELF
580 // file header before we create the section headers.
581 void set_section_info(const Output_section_headers*,
582 const Output_section* shstrtab);
583
584 protected:
585 // Write the data to the file.
586 void
587 do_write(Output_file*);
588
589 // Return the required alignment.
590 uint64_t
591 do_addralign() const
592 { return Output_data::default_alignment(); }
593
594 // Write to a map file.
595 void
596 do_print_to_mapfile(Mapfile* mapfile) const
597 { mapfile->print_output_data(this, _("** file header")); }
598
599 // Set final data size.
600 void
601 set_final_data_size(void)
602 { this->set_data_size(this->do_size()); }
603
604 private:
605 // Write the data to the file with the right size and endianness.
606 template<int size, bool big_endian>
607 void
608 do_sized_write(Output_file*);
609
610 // Return the value to use for the entry address.
611 template<int size>
612 typename elfcpp::Elf_types<size>::Elf_Addr
613 entry();
614
615 // Compute the current data size.
616 off_t
617 do_size() const;
618
619 Target* target_;
620 const Symbol_table* symtab_;
621 const Output_segment_headers* segment_header_;
622 const Output_section_headers* section_header_;
623 const Output_section* shstrtab_;
624 };
625
626 // Output sections are mainly comprised of input sections. However,
627 // there are cases where we have data to write out which is not in an
628 // input section. Output_section_data is used in such cases. This is
629 // an abstract base class.
630
631 class Output_section_data : public Output_data
632 {
633 public:
634 Output_section_data(off_t data_size, uint64_t addralign,
635 bool is_data_size_fixed)
636 : Output_data(), output_section_(NULL), addralign_(addralign)
637 {
638 this->set_data_size(data_size);
639 if (is_data_size_fixed)
640 this->fix_data_size();
641 }
642
643 Output_section_data(uint64_t addralign)
644 : Output_data(), output_section_(NULL), addralign_(addralign)
645 { }
646
647 // Return the output section.
648 Output_section*
649 output_section()
650 { return this->output_section_; }
651
652 const Output_section*
653 output_section() const
654 { return this->output_section_; }
655
656 // Record the output section.
657 void
658 set_output_section(Output_section* os);
659
660 // Add an input section, for SHF_MERGE sections. This returns true
661 // if the section was handled.
662 bool
663 add_input_section(Relobj* object, unsigned int shndx)
664 { return this->do_add_input_section(object, shndx); }
665
666 // Given an input OBJECT, an input section index SHNDX within that
667 // object, and an OFFSET relative to the start of that input
668 // section, return whether or not the corresponding offset within
669 // the output section is known. If this function returns true, it
670 // sets *POUTPUT to the output offset. The value -1 indicates that
671 // this input offset is being discarded.
672 bool
673 output_offset(const Relobj* object, unsigned int shndx,
674 section_offset_type offset,
675 section_offset_type* poutput) const
676 { return this->do_output_offset(object, shndx, offset, poutput); }
677
678 // Write the contents to a buffer. This is used for sections which
679 // require postprocessing, such as compression.
680 void
681 write_to_buffer(unsigned char* buffer)
682 { this->do_write_to_buffer(buffer); }
683
684 // Print merge stats to stderr. This should only be called for
685 // SHF_MERGE sections.
686 void
687 print_merge_stats(const char* section_name)
688 { this->do_print_merge_stats(section_name); }
689
690 protected:
691 // The child class must implement do_write.
692
693 // The child class may implement specific adjustments to the output
694 // section.
695 virtual void
696 do_adjust_output_section(Output_section*)
697 { }
698
699 // May be implemented by child class. Return true if the section
700 // was handled.
701 virtual bool
702 do_add_input_section(Relobj*, unsigned int)
703 { gold_unreachable(); }
704
705 // The child class may implement output_offset.
706 virtual bool
707 do_output_offset(const Relobj*, unsigned int, section_offset_type,
708 section_offset_type*) const
709 { return false; }
710
711 // The child class may implement write_to_buffer. Most child
712 // classes can not appear in a compressed section, and they do not
713 // implement this.
714 virtual void
715 do_write_to_buffer(unsigned char*)
716 { gold_unreachable(); }
717
718 // Print merge statistics.
719 virtual void
720 do_print_merge_stats(const char*)
721 { gold_unreachable(); }
722
723 // Return the required alignment.
724 uint64_t
725 do_addralign() const
726 { return this->addralign_; }
727
728 // Return the output section.
729 Output_section*
730 do_output_section()
731 { return this->output_section_; }
732
733 const Output_section*
734 do_output_section() const
735 { return this->output_section_; }
736
737 // Return the section index of the output section.
738 unsigned int
739 do_out_shndx() const;
740
741 // Set the alignment.
742 void
743 set_addralign(uint64_t addralign);
744
745 private:
746 // The output section for this section.
747 Output_section* output_section_;
748 // The required alignment.
749 uint64_t addralign_;
750 };
751
752 // Some Output_section_data classes build up their data step by step,
753 // rather than all at once. This class provides an interface for
754 // them.
755
756 class Output_section_data_build : public Output_section_data
757 {
758 public:
759 Output_section_data_build(uint64_t addralign)
760 : Output_section_data(addralign)
761 { }
762
763 Output_section_data_build(off_t data_size, uint64_t addralign)
764 : Output_section_data(data_size, addralign, false)
765 { }
766
767 // Set the current data size.
768 void
769 set_current_data_size(off_t data_size)
770 { this->set_current_data_size_for_child(data_size); }
771
772 protected:
773 // Set the final data size.
774 virtual void
775 set_final_data_size()
776 { this->set_data_size(this->current_data_size_for_child()); }
777 };
778
779 // A simple case of Output_data in which we have constant data to
780 // output.
781
782 class Output_data_const : public Output_section_data
783 {
784 public:
785 Output_data_const(const std::string& data, uint64_t addralign)
786 : Output_section_data(data.size(), addralign, true), data_(data)
787 { }
788
789 Output_data_const(const char* p, off_t len, uint64_t addralign)
790 : Output_section_data(len, addralign, true), data_(p, len)
791 { }
792
793 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
794 : Output_section_data(len, addralign, true),
795 data_(reinterpret_cast<const char*>(p), len)
796 { }
797
798 protected:
799 // Write the data to the output file.
800 void
801 do_write(Output_file*);
802
803 // Write the data to a buffer.
804 void
805 do_write_to_buffer(unsigned char* buffer)
806 { memcpy(buffer, this->data_.data(), this->data_.size()); }
807
808 // Write to a map file.
809 void
810 do_print_to_mapfile(Mapfile* mapfile) const
811 { mapfile->print_output_data(this, _("** fill")); }
812
813 private:
814 std::string data_;
815 };
816
817 // Another version of Output_data with constant data, in which the
818 // buffer is allocated by the caller.
819
820 class Output_data_const_buffer : public Output_section_data
821 {
822 public:
823 Output_data_const_buffer(const unsigned char* p, off_t len,
824 uint64_t addralign, const char* map_name)
825 : Output_section_data(len, addralign, true),
826 p_(p), map_name_(map_name)
827 { }
828
829 protected:
830 // Write the data the output file.
831 void
832 do_write(Output_file*);
833
834 // Write the data to a buffer.
835 void
836 do_write_to_buffer(unsigned char* buffer)
837 { memcpy(buffer, this->p_, this->data_size()); }
838
839 // Write to a map file.
840 void
841 do_print_to_mapfile(Mapfile* mapfile) const
842 { mapfile->print_output_data(this, _(this->map_name_)); }
843
844 private:
845 // The data to output.
846 const unsigned char* p_;
847 // Name to use in a map file. Maps are a rarely used feature, but
848 // the space usage is minor as aren't very many of these objects.
849 const char* map_name_;
850 };
851
852 // A place holder for a fixed amount of data written out via some
853 // other mechanism.
854
855 class Output_data_fixed_space : public Output_section_data
856 {
857 public:
858 Output_data_fixed_space(off_t data_size, uint64_t addralign,
859 const char* map_name)
860 : Output_section_data(data_size, addralign, true),
861 map_name_(map_name)
862 { }
863
864 protected:
865 // Write out the data--the actual data must be written out
866 // elsewhere.
867 void
868 do_write(Output_file*)
869 { }
870
871 // Write to a map file.
872 void
873 do_print_to_mapfile(Mapfile* mapfile) const
874 { mapfile->print_output_data(this, _(this->map_name_)); }
875
876 private:
877 // Name to use in a map file. Maps are a rarely used feature, but
878 // the space usage is minor as aren't very many of these objects.
879 const char* map_name_;
880 };
881
882 // A place holder for variable sized data written out via some other
883 // mechanism.
884
885 class Output_data_space : public Output_section_data_build
886 {
887 public:
888 explicit Output_data_space(uint64_t addralign, const char* map_name)
889 : Output_section_data_build(addralign),
890 map_name_(map_name)
891 { }
892
893 explicit Output_data_space(off_t data_size, uint64_t addralign,
894 const char* map_name)
895 : Output_section_data_build(data_size, addralign),
896 map_name_(map_name)
897 { }
898
899 // Set the alignment.
900 void
901 set_space_alignment(uint64_t align)
902 { this->set_addralign(align); }
903
904 protected:
905 // Write out the data--the actual data must be written out
906 // elsewhere.
907 void
908 do_write(Output_file*)
909 { }
910
911 // Write to a map file.
912 void
913 do_print_to_mapfile(Mapfile* mapfile) const
914 { mapfile->print_output_data(this, _(this->map_name_)); }
915
916 private:
917 // Name to use in a map file. Maps are a rarely used feature, but
918 // the space usage is minor as aren't very many of these objects.
919 const char* map_name_;
920 };
921
922 // Fill fixed space with zeroes. This is just like
923 // Output_data_fixed_space, except that the map name is known.
924
925 class Output_data_zero_fill : public Output_section_data
926 {
927 public:
928 Output_data_zero_fill(off_t data_size, uint64_t addralign)
929 : Output_section_data(data_size, addralign, true)
930 { }
931
932 protected:
933 // There is no data to write out.
934 void
935 do_write(Output_file*)
936 { }
937
938 // Write to a map file.
939 void
940 do_print_to_mapfile(Mapfile* mapfile) const
941 { mapfile->print_output_data(this, "** zero fill"); }
942 };
943
944 // A string table which goes into an output section.
945
946 class Output_data_strtab : public Output_section_data
947 {
948 public:
949 Output_data_strtab(Stringpool* strtab)
950 : Output_section_data(1), strtab_(strtab)
951 { }
952
953 protected:
954 // This is called to update the section size prior to assigning
955 // the address and file offset.
956 void
957 update_data_size()
958 { this->set_final_data_size(); }
959
960 // This is called to set the address and file offset. Here we make
961 // sure that the Stringpool is finalized.
962 void
963 set_final_data_size();
964
965 // Write out the data.
966 void
967 do_write(Output_file*);
968
969 // Write the data to a buffer.
970 void
971 do_write_to_buffer(unsigned char* buffer)
972 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
973
974 // Write to a map file.
975 void
976 do_print_to_mapfile(Mapfile* mapfile) const
977 { mapfile->print_output_data(this, _("** string table")); }
978
979 private:
980 Stringpool* strtab_;
981 };
982
983 // This POD class is used to represent a single reloc in the output
984 // file. This could be a private class within Output_data_reloc, but
985 // the templatization is complex enough that I broke it out into a
986 // separate class. The class is templatized on either elfcpp::SHT_REL
987 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
988 // relocation or an ordinary relocation.
989
990 // A relocation can be against a global symbol, a local symbol, a
991 // local section symbol, an output section, or the undefined symbol at
992 // index 0. We represent the latter by using a NULL global symbol.
993
994 template<int sh_type, bool dynamic, int size, bool big_endian>
995 class Output_reloc;
996
997 template<bool dynamic, int size, bool big_endian>
998 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
999 {
1000 public:
1001 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1002 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1003
1004 static const Address invalid_address = static_cast<Address>(0) - 1;
1005
1006 // An uninitialized entry. We need this because we want to put
1007 // instances of this class into an STL container.
1008 Output_reloc()
1009 : local_sym_index_(INVALID_CODE)
1010 { }
1011
1012 // We have a bunch of different constructors. They come in pairs
1013 // depending on how the address of the relocation is specified. It
1014 // can either be an offset in an Output_data or an offset in an
1015 // input section.
1016
1017 // A reloc against a global symbol.
1018
1019 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1020 Address address, bool is_relative, bool is_symbolless,
1021 bool use_plt_offset);
1022
1023 Output_reloc(Symbol* gsym, unsigned int type,
1024 Sized_relobj<size, big_endian>* relobj,
1025 unsigned int shndx, Address address, bool is_relative,
1026 bool is_symbolless, bool use_plt_offset);
1027
1028 // A reloc against a local symbol or local section symbol.
1029
1030 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1031 unsigned int local_sym_index, unsigned int type,
1032 Output_data* od, Address address, bool is_relative,
1033 bool is_symbolless, bool is_section_symbol,
1034 bool use_plt_offset);
1035
1036 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1037 unsigned int local_sym_index, unsigned int type,
1038 unsigned int shndx, Address address, bool is_relative,
1039 bool is_symbolless, bool is_section_symbol,
1040 bool use_plt_offset);
1041
1042 // A reloc against the STT_SECTION symbol of an output section.
1043
1044 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1045 Address address, bool is_relative);
1046
1047 Output_reloc(Output_section* os, unsigned int type,
1048 Sized_relobj<size, big_endian>* relobj, unsigned int shndx,
1049 Address address, bool is_relative);
1050
1051 // An absolute or relative relocation with no symbol.
1052
1053 Output_reloc(unsigned int type, Output_data* od, Address address,
1054 bool is_relative);
1055
1056 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1057 unsigned int shndx, Address address, bool is_relative);
1058
1059 // A target specific relocation. The target will be called to get
1060 // the symbol index, passing ARG. The type and offset will be set
1061 // as for other relocation types.
1062
1063 Output_reloc(unsigned int type, void* arg, Output_data* od,
1064 Address address);
1065
1066 Output_reloc(unsigned int type, void* arg,
1067 Sized_relobj<size, big_endian>* relobj,
1068 unsigned int shndx, Address address);
1069
1070 // Return the reloc type.
1071 unsigned int
1072 type() const
1073 { return this->type_; }
1074
1075 // Return whether this is a RELATIVE relocation.
1076 bool
1077 is_relative() const
1078 { return this->is_relative_; }
1079
1080 // Return whether this is a relocation which should not use
1081 // a symbol, but which obtains its addend from a symbol.
1082 bool
1083 is_symbolless() const
1084 { return this->is_symbolless_; }
1085
1086 // Return whether this is against a local section symbol.
1087 bool
1088 is_local_section_symbol() const
1089 {
1090 return (this->local_sym_index_ != GSYM_CODE
1091 && this->local_sym_index_ != SECTION_CODE
1092 && this->local_sym_index_ != INVALID_CODE
1093 && this->local_sym_index_ != TARGET_CODE
1094 && this->is_section_symbol_);
1095 }
1096
1097 // Return whether this is a target specific relocation.
1098 bool
1099 is_target_specific() const
1100 { return this->local_sym_index_ == TARGET_CODE; }
1101
1102 // Return the argument to pass to the target for a target specific
1103 // relocation.
1104 void*
1105 target_arg() const
1106 {
1107 gold_assert(this->local_sym_index_ == TARGET_CODE);
1108 return this->u1_.arg;
1109 }
1110
1111 // For a local section symbol, return the offset of the input
1112 // section within the output section. ADDEND is the addend being
1113 // applied to the input section.
1114 Address
1115 local_section_offset(Addend addend) const;
1116
1117 // Get the value of the symbol referred to by a Rel relocation when
1118 // we are adding the given ADDEND.
1119 Address
1120 symbol_value(Addend addend) const;
1121
1122 // If this relocation is against an input section, return the
1123 // relocatable object containing the input section.
1124 Sized_relobj<size, big_endian>*
1125 get_relobj() const
1126 {
1127 if (this->shndx_ == INVALID_CODE)
1128 return NULL;
1129 return this->u2_.relobj;
1130 }
1131
1132 // Write the reloc entry to an output view.
1133 void
1134 write(unsigned char* pov) const;
1135
1136 // Write the offset and info fields to Write_rel.
1137 template<typename Write_rel>
1138 void write_rel(Write_rel*) const;
1139
1140 // This is used when sorting dynamic relocs. Return -1 to sort this
1141 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1142 int
1143 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1144 const;
1145
1146 // Return whether this reloc should be sorted before the argument
1147 // when sorting dynamic relocs.
1148 bool
1149 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1150 r2) const
1151 { return this->compare(r2) < 0; }
1152
1153 private:
1154 // Record that we need a dynamic symbol index.
1155 void
1156 set_needs_dynsym_index();
1157
1158 // Return the symbol index.
1159 unsigned int
1160 get_symbol_index() const;
1161
1162 // Return the output address.
1163 Address
1164 get_address() const;
1165
1166 // Codes for local_sym_index_.
1167 enum
1168 {
1169 // Global symbol.
1170 GSYM_CODE = -1U,
1171 // Output section.
1172 SECTION_CODE = -2U,
1173 // Target specific.
1174 TARGET_CODE = -3U,
1175 // Invalid uninitialized entry.
1176 INVALID_CODE = -4U
1177 };
1178
1179 union
1180 {
1181 // For a local symbol or local section symbol
1182 // (this->local_sym_index_ >= 0), the object. We will never
1183 // generate a relocation against a local symbol in a dynamic
1184 // object; that doesn't make sense. And our callers will always
1185 // be templatized, so we use Sized_relobj here.
1186 Sized_relobj<size, big_endian>* relobj;
1187 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1188 // symbol. If this is NULL, it indicates a relocation against the
1189 // undefined 0 symbol.
1190 Symbol* gsym;
1191 // For a relocation against an output section
1192 // (this->local_sym_index_ == SECTION_CODE), the output section.
1193 Output_section* os;
1194 // For a target specific relocation, an argument to pass to the
1195 // target.
1196 void* arg;
1197 } u1_;
1198 union
1199 {
1200 // If this->shndx_ is not INVALID CODE, the object which holds the
1201 // input section being used to specify the reloc address.
1202 Sized_relobj<size, big_endian>* relobj;
1203 // If this->shndx_ is INVALID_CODE, the output data being used to
1204 // specify the reloc address. This may be NULL if the reloc
1205 // address is absolute.
1206 Output_data* od;
1207 } u2_;
1208 // The address offset within the input section or the Output_data.
1209 Address address_;
1210 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1211 // relocation against an output section, or TARGET_CODE for a target
1212 // specific relocation, or INVALID_CODE for an uninitialized value.
1213 // Otherwise, for a local symbol (this->is_section_symbol_ is
1214 // false), the local symbol index. For a local section symbol
1215 // (this->is_section_symbol_ is true), the section index in the
1216 // input file.
1217 unsigned int local_sym_index_;
1218 // The reloc type--a processor specific code.
1219 unsigned int type_ : 28;
1220 // True if the relocation is a RELATIVE relocation.
1221 bool is_relative_ : 1;
1222 // True if the relocation is one which should not use
1223 // a symbol, but which obtains its addend from a symbol.
1224 bool is_symbolless_ : 1;
1225 // True if the relocation is against a section symbol.
1226 bool is_section_symbol_ : 1;
1227 // True if the addend should be the PLT offset.
1228 // (Used only for RELA, but stored here for space.)
1229 bool use_plt_offset_ : 1;
1230 // If the reloc address is an input section in an object, the
1231 // section index. This is INVALID_CODE if the reloc address is
1232 // specified in some other way.
1233 unsigned int shndx_;
1234 };
1235
1236 // The SHT_RELA version of Output_reloc<>. This is just derived from
1237 // the SHT_REL version of Output_reloc, but it adds an addend.
1238
1239 template<bool dynamic, int size, bool big_endian>
1240 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1241 {
1242 public:
1243 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1244 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1245
1246 // An uninitialized entry.
1247 Output_reloc()
1248 : rel_()
1249 { }
1250
1251 // A reloc against a global symbol.
1252
1253 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1254 Address address, Addend addend, bool is_relative,
1255 bool is_symbolless, bool use_plt_offset)
1256 : rel_(gsym, type, od, address, is_relative, is_symbolless,
1257 use_plt_offset),
1258 addend_(addend)
1259 { }
1260
1261 Output_reloc(Symbol* gsym, unsigned int type,
1262 Sized_relobj<size, big_endian>* relobj,
1263 unsigned int shndx, Address address, Addend addend,
1264 bool is_relative, bool is_symbolless, bool use_plt_offset)
1265 : rel_(gsym, type, relobj, shndx, address, is_relative,
1266 is_symbolless, use_plt_offset), addend_(addend)
1267 { }
1268
1269 // A reloc against a local symbol.
1270
1271 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1272 unsigned int local_sym_index, unsigned int type,
1273 Output_data* od, Address address,
1274 Addend addend, bool is_relative,
1275 bool is_symbolless, bool is_section_symbol,
1276 bool use_plt_offset)
1277 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1278 is_symbolless, is_section_symbol, use_plt_offset),
1279 addend_(addend)
1280 { }
1281
1282 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1283 unsigned int local_sym_index, unsigned int type,
1284 unsigned int shndx, Address address,
1285 Addend addend, bool is_relative,
1286 bool is_symbolless, bool is_section_symbol,
1287 bool use_plt_offset)
1288 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1289 is_symbolless, is_section_symbol, use_plt_offset),
1290 addend_(addend)
1291 { }
1292
1293 // A reloc against the STT_SECTION symbol of an output section.
1294
1295 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1296 Address address, Addend addend, bool is_relative)
1297 : rel_(os, type, od, address, is_relative), addend_(addend)
1298 { }
1299
1300 Output_reloc(Output_section* os, unsigned int type,
1301 Sized_relobj<size, big_endian>* relobj,
1302 unsigned int shndx, Address address, Addend addend,
1303 bool is_relative)
1304 : rel_(os, type, relobj, shndx, address, is_relative), addend_(addend)
1305 { }
1306
1307 // An absolute or relative relocation with no symbol.
1308
1309 Output_reloc(unsigned int type, Output_data* od, Address address,
1310 Addend addend, bool is_relative)
1311 : rel_(type, od, address, is_relative), addend_(addend)
1312 { }
1313
1314 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1315 unsigned int shndx, Address address, Addend addend,
1316 bool is_relative)
1317 : rel_(type, relobj, shndx, address, is_relative), addend_(addend)
1318 { }
1319
1320 // A target specific relocation. The target will be called to get
1321 // the symbol index and the addend, passing ARG. The type and
1322 // offset will be set as for other relocation types.
1323
1324 Output_reloc(unsigned int type, void* arg, Output_data* od,
1325 Address address, Addend addend)
1326 : rel_(type, arg, od, address), addend_(addend)
1327 { }
1328
1329 Output_reloc(unsigned int type, void* arg,
1330 Sized_relobj<size, big_endian>* relobj,
1331 unsigned int shndx, Address address, Addend addend)
1332 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1333 { }
1334
1335 // Return whether this is a RELATIVE relocation.
1336 bool
1337 is_relative() const
1338 { return this->rel_.is_relative(); }
1339
1340 // Return whether this is a relocation which should not use
1341 // a symbol, but which obtains its addend from a symbol.
1342 bool
1343 is_symbolless() const
1344 { return this->rel_.is_symbolless(); }
1345
1346 // If this relocation is against an input section, return the
1347 // relocatable object containing the input section.
1348 Sized_relobj<size, big_endian>*
1349 get_relobj() const
1350 { return this->rel_.get_relobj(); }
1351
1352 // Write the reloc entry to an output view.
1353 void
1354 write(unsigned char* pov) const;
1355
1356 // Return whether this reloc should be sorted before the argument
1357 // when sorting dynamic relocs.
1358 bool
1359 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1360 r2) const
1361 {
1362 int i = this->rel_.compare(r2.rel_);
1363 if (i < 0)
1364 return true;
1365 else if (i > 0)
1366 return false;
1367 else
1368 return this->addend_ < r2.addend_;
1369 }
1370
1371 private:
1372 // The basic reloc.
1373 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1374 // The addend.
1375 Addend addend_;
1376 };
1377
1378 // Output_data_reloc_generic is a non-template base class for
1379 // Output_data_reloc_base. This gives the generic code a way to hold
1380 // a pointer to a reloc section.
1381
1382 class Output_data_reloc_generic : public Output_section_data_build
1383 {
1384 public:
1385 Output_data_reloc_generic(int size, bool sort_relocs)
1386 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1387 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1388 { }
1389
1390 // Return the number of relative relocs in this section.
1391 size_t
1392 relative_reloc_count() const
1393 { return this->relative_reloc_count_; }
1394
1395 // Whether we should sort the relocs.
1396 bool
1397 sort_relocs() const
1398 { return this->sort_relocs_; }
1399
1400 // Add a reloc of type TYPE against the global symbol GSYM. The
1401 // relocation applies to the data at offset ADDRESS within OD.
1402 virtual void
1403 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1404 uint64_t address, uint64_t addend) = 0;
1405
1406 // Add a reloc of type TYPE against the global symbol GSYM. The
1407 // relocation applies to data at offset ADDRESS within section SHNDX
1408 // of object file RELOBJ. OD is the associated output section.
1409 virtual void
1410 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1411 Relobj* relobj, unsigned int shndx, uint64_t address,
1412 uint64_t addend) = 0;
1413
1414 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1415 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1416 // within OD.
1417 virtual void
1418 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1419 unsigned int type, Output_data* od, uint64_t address,
1420 uint64_t addend) = 0;
1421
1422 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1423 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1424 // within section SHNDX of RELOBJ. OD is the associated output
1425 // section.
1426 virtual void
1427 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1428 unsigned int type, Output_data* od, unsigned int shndx,
1429 uint64_t address, uint64_t addend) = 0;
1430
1431 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1432 // output section OS. The relocation applies to the data at offset
1433 // ADDRESS within OD.
1434 virtual void
1435 add_output_section_generic(Output_section *os, unsigned int type,
1436 Output_data* od, uint64_t address,
1437 uint64_t addend) = 0;
1438
1439 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1440 // output section OS. The relocation applies to the data at offset
1441 // ADDRESS within section SHNDX of RELOBJ. OD is the associated
1442 // output section.
1443 virtual void
1444 add_output_section_generic(Output_section* os, unsigned int type,
1445 Output_data* od, Relobj* relobj,
1446 unsigned int shndx, uint64_t address,
1447 uint64_t addend) = 0;
1448
1449 protected:
1450 // Note that we've added another relative reloc.
1451 void
1452 bump_relative_reloc_count()
1453 { ++this->relative_reloc_count_; }
1454
1455 private:
1456 // The number of relative relocs added to this section. This is to
1457 // support DT_RELCOUNT.
1458 size_t relative_reloc_count_;
1459 // Whether to sort the relocations when writing them out, to make
1460 // the dynamic linker more efficient.
1461 bool sort_relocs_;
1462 };
1463
1464 // Output_data_reloc is used to manage a section containing relocs.
1465 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1466 // indicates whether this is a dynamic relocation or a normal
1467 // relocation. Output_data_reloc_base is a base class.
1468 // Output_data_reloc is the real class, which we specialize based on
1469 // the reloc type.
1470
1471 template<int sh_type, bool dynamic, int size, bool big_endian>
1472 class Output_data_reloc_base : public Output_data_reloc_generic
1473 {
1474 public:
1475 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1476 typedef typename Output_reloc_type::Address Address;
1477 static const int reloc_size =
1478 Reloc_types<sh_type, size, big_endian>::reloc_size;
1479
1480 // Construct the section.
1481 Output_data_reloc_base(bool sort_relocs)
1482 : Output_data_reloc_generic(size, sort_relocs)
1483 { }
1484
1485 protected:
1486 // Write out the data.
1487 void
1488 do_write(Output_file*);
1489
1490 // Set the entry size and the link.
1491 void
1492 do_adjust_output_section(Output_section* os);
1493
1494 // Write to a map file.
1495 void
1496 do_print_to_mapfile(Mapfile* mapfile) const
1497 {
1498 mapfile->print_output_data(this,
1499 (dynamic
1500 ? _("** dynamic relocs")
1501 : _("** relocs")));
1502 }
1503
1504 // Add a relocation entry.
1505 void
1506 add(Output_data* od, const Output_reloc_type& reloc)
1507 {
1508 this->relocs_.push_back(reloc);
1509 this->set_current_data_size(this->relocs_.size() * reloc_size);
1510 if (dynamic)
1511 od->add_dynamic_reloc();
1512 if (reloc.is_relative())
1513 this->bump_relative_reloc_count();
1514 Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1515 if (relobj != NULL)
1516 relobj->add_dyn_reloc(this->relocs_.size() - 1);
1517 }
1518
1519 private:
1520 typedef std::vector<Output_reloc_type> Relocs;
1521
1522 // The class used to sort the relocations.
1523 struct Sort_relocs_comparison
1524 {
1525 bool
1526 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1527 { return r1.sort_before(r2); }
1528 };
1529
1530 // The relocations in this section.
1531 Relocs relocs_;
1532 };
1533
1534 // The class which callers actually create.
1535
1536 template<int sh_type, bool dynamic, int size, bool big_endian>
1537 class Output_data_reloc;
1538
1539 // The SHT_REL version of Output_data_reloc.
1540
1541 template<bool dynamic, int size, bool big_endian>
1542 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1543 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1544 {
1545 private:
1546 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1547 big_endian> Base;
1548
1549 public:
1550 typedef typename Base::Output_reloc_type Output_reloc_type;
1551 typedef typename Output_reloc_type::Address Address;
1552
1553 Output_data_reloc(bool sr)
1554 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1555 { }
1556
1557 // Add a reloc against a global symbol.
1558
1559 void
1560 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1561 {
1562 this->add(od, Output_reloc_type(gsym, type, od, address,
1563 false, false, false));
1564 }
1565
1566 void
1567 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1568 Sized_relobj<size, big_endian>* relobj,
1569 unsigned int shndx, Address address)
1570 {
1571 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1572 false, false, false));
1573 }
1574
1575 void
1576 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1577 uint64_t address, uint64_t addend)
1578 {
1579 gold_assert(addend == 0);
1580 this->add(od, Output_reloc_type(gsym, type, od,
1581 convert_types<Address, uint64_t>(address),
1582 false, false, false));
1583 }
1584
1585 void
1586 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1587 Relobj* relobj, unsigned int shndx, uint64_t address,
1588 uint64_t addend)
1589 {
1590 gold_assert(addend == 0);
1591 Sized_relobj<size, big_endian>* sized_relobj =
1592 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1593 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1594 convert_types<Address, uint64_t>(address),
1595 false, false, false));
1596 }
1597
1598 // Add a RELATIVE reloc against a global symbol. The final relocation
1599 // will not reference the symbol.
1600
1601 void
1602 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1603 Address address)
1604 {
1605 this->add(od, Output_reloc_type(gsym, type, od, address, true, true,
1606 false));
1607 }
1608
1609 void
1610 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1611 Sized_relobj<size, big_endian>* relobj,
1612 unsigned int shndx, Address address)
1613 {
1614 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1615 true, true, false));
1616 }
1617
1618 // Add a global relocation which does not use a symbol for the relocation,
1619 // but which gets its addend from a symbol.
1620
1621 void
1622 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1623 Output_data* od, Address address)
1624 {
1625 this->add(od, Output_reloc_type(gsym, type, od, address, false, true,
1626 false));
1627 }
1628
1629 void
1630 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1631 Output_data* od,
1632 Sized_relobj<size, big_endian>* relobj,
1633 unsigned int shndx, Address address)
1634 {
1635 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1636 false, true, false));
1637 }
1638
1639 // Add a reloc against a local symbol.
1640
1641 void
1642 add_local(Sized_relobj<size, big_endian>* relobj,
1643 unsigned int local_sym_index, unsigned int type,
1644 Output_data* od, Address address)
1645 {
1646 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1647 address, false, false, false, false));
1648 }
1649
1650 void
1651 add_local(Sized_relobj<size, big_endian>* relobj,
1652 unsigned int local_sym_index, unsigned int type,
1653 Output_data* od, unsigned int shndx, Address address)
1654 {
1655 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1656 address, false, false, false, false));
1657 }
1658
1659 void
1660 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1661 unsigned int type, Output_data* od, uint64_t address,
1662 uint64_t addend)
1663 {
1664 gold_assert(addend == 0);
1665 Sized_relobj<size, big_endian>* sized_relobj =
1666 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1667 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1668 convert_types<Address, uint64_t>(address),
1669 false, false, false, false));
1670 }
1671
1672 void
1673 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1674 unsigned int type, Output_data* od, unsigned int shndx,
1675 uint64_t address, uint64_t addend)
1676 {
1677 gold_assert(addend == 0);
1678 Sized_relobj<size, big_endian>* sized_relobj =
1679 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1680 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1681 convert_types<Address, uint64_t>(address),
1682 false, false, false, false));
1683 }
1684
1685 // Add a RELATIVE reloc against a local symbol.
1686
1687 void
1688 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1689 unsigned int local_sym_index, unsigned int type,
1690 Output_data* od, Address address)
1691 {
1692 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1693 address, true, true, false, false));
1694 }
1695
1696 void
1697 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1698 unsigned int local_sym_index, unsigned int type,
1699 Output_data* od, unsigned int shndx, Address address)
1700 {
1701 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1702 address, true, true, false, false));
1703 }
1704
1705 void
1706 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1707 unsigned int local_sym_index, unsigned int type,
1708 Output_data* od, unsigned int shndx, Address address,
1709 bool use_plt_offset)
1710 {
1711 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1712 address, true, true, false,
1713 use_plt_offset));
1714 }
1715
1716 // Add a local relocation which does not use a symbol for the relocation,
1717 // but which gets its addend from a symbol.
1718
1719 void
1720 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1721 unsigned int local_sym_index, unsigned int type,
1722 Output_data* od, Address address)
1723 {
1724 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1725 address, false, true, false, false));
1726 }
1727
1728 void
1729 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1730 unsigned int local_sym_index, unsigned int type,
1731 Output_data* od, unsigned int shndx,
1732 Address address)
1733 {
1734 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1735 address, false, true, false, false));
1736 }
1737
1738 // Add a reloc against a local section symbol. This will be
1739 // converted into a reloc against the STT_SECTION symbol of the
1740 // output section.
1741
1742 void
1743 add_local_section(Sized_relobj<size, big_endian>* relobj,
1744 unsigned int input_shndx, unsigned int type,
1745 Output_data* od, Address address)
1746 {
1747 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1748 address, false, false, true, false));
1749 }
1750
1751 void
1752 add_local_section(Sized_relobj<size, big_endian>* relobj,
1753 unsigned int input_shndx, unsigned int type,
1754 Output_data* od, unsigned int shndx, Address address)
1755 {
1756 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1757 address, false, false, true, false));
1758 }
1759
1760 // A reloc against the STT_SECTION symbol of an output section.
1761 // OS is the Output_section that the relocation refers to; OD is
1762 // the Output_data object being relocated.
1763
1764 void
1765 add_output_section(Output_section* os, unsigned int type,
1766 Output_data* od, Address address)
1767 { this->add(od, Output_reloc_type(os, type, od, address, false)); }
1768
1769 void
1770 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1771 Sized_relobj<size, big_endian>* relobj,
1772 unsigned int shndx, Address address)
1773 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, false)); }
1774
1775 void
1776 add_output_section_generic(Output_section* os, unsigned int type,
1777 Output_data* od, uint64_t address,
1778 uint64_t addend)
1779 {
1780 gold_assert(addend == 0);
1781 this->add(od, Output_reloc_type(os, type, od,
1782 convert_types<Address, uint64_t>(address),
1783 false));
1784 }
1785
1786 void
1787 add_output_section_generic(Output_section* os, unsigned int type,
1788 Output_data* od, Relobj* relobj,
1789 unsigned int shndx, uint64_t address,
1790 uint64_t addend)
1791 {
1792 gold_assert(addend == 0);
1793 Sized_relobj<size, big_endian>* sized_relobj =
1794 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1795 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
1796 convert_types<Address, uint64_t>(address),
1797 false));
1798 }
1799
1800 // As above, but the reloc TYPE is relative
1801
1802 void
1803 add_output_section_relative(Output_section* os, unsigned int type,
1804 Output_data* od, Address address)
1805 { this->add(od, Output_reloc_type(os, type, od, address, true)); }
1806
1807 void
1808 add_output_section_relative(Output_section* os, unsigned int type,
1809 Output_data* od,
1810 Sized_relobj<size, big_endian>* relobj,
1811 unsigned int shndx, Address address)
1812 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, true)); }
1813
1814 // Add an absolute relocation.
1815
1816 void
1817 add_absolute(unsigned int type, Output_data* od, Address address)
1818 { this->add(od, Output_reloc_type(type, od, address, false)); }
1819
1820 void
1821 add_absolute(unsigned int type, Output_data* od,
1822 Sized_relobj<size, big_endian>* relobj,
1823 unsigned int shndx, Address address)
1824 { this->add(od, Output_reloc_type(type, relobj, shndx, address, false)); }
1825
1826 // Add a relative relocation
1827
1828 void
1829 add_relative(unsigned int type, Output_data* od, Address address)
1830 { this->add(od, Output_reloc_type(type, od, address, true)); }
1831
1832 void
1833 add_relative(unsigned int type, Output_data* od,
1834 Sized_relobj<size, big_endian>* relobj,
1835 unsigned int shndx, Address address)
1836 { this->add(od, Output_reloc_type(type, relobj, shndx, address, true)); }
1837
1838 // Add a target specific relocation. A target which calls this must
1839 // define the reloc_symbol_index and reloc_addend virtual functions.
1840
1841 void
1842 add_target_specific(unsigned int type, void* arg, Output_data* od,
1843 Address address)
1844 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1845
1846 void
1847 add_target_specific(unsigned int type, void* arg, Output_data* od,
1848 Sized_relobj<size, big_endian>* relobj,
1849 unsigned int shndx, Address address)
1850 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1851 };
1852
1853 // The SHT_RELA version of Output_data_reloc.
1854
1855 template<bool dynamic, int size, bool big_endian>
1856 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1857 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1858 {
1859 private:
1860 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1861 big_endian> Base;
1862
1863 public:
1864 typedef typename Base::Output_reloc_type Output_reloc_type;
1865 typedef typename Output_reloc_type::Address Address;
1866 typedef typename Output_reloc_type::Addend Addend;
1867
1868 Output_data_reloc(bool sr)
1869 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1870 { }
1871
1872 // Add a reloc against a global symbol.
1873
1874 void
1875 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1876 Address address, Addend addend)
1877 {
1878 this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1879 false, false, false));
1880 }
1881
1882 void
1883 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1884 Sized_relobj<size, big_endian>* relobj,
1885 unsigned int shndx, Address address,
1886 Addend addend)
1887 {
1888 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1889 addend, false, false, false));
1890 }
1891
1892 void
1893 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1894 uint64_t address, uint64_t addend)
1895 {
1896 this->add(od, Output_reloc_type(gsym, type, od,
1897 convert_types<Address, uint64_t>(address),
1898 convert_types<Addend, uint64_t>(addend),
1899 false, false, false));
1900 }
1901
1902 void
1903 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1904 Relobj* relobj, unsigned int shndx, uint64_t address,
1905 uint64_t addend)
1906 {
1907 Sized_relobj<size, big_endian>* sized_relobj =
1908 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1909 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1910 convert_types<Address, uint64_t>(address),
1911 convert_types<Addend, uint64_t>(addend),
1912 false, false, false));
1913 }
1914
1915 // Add a RELATIVE reloc against a global symbol. The final output
1916 // relocation will not reference the symbol, but we must keep the symbol
1917 // information long enough to set the addend of the relocation correctly
1918 // when it is written.
1919
1920 void
1921 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1922 Address address, Addend addend, bool use_plt_offset)
1923 {
1924 this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1925 true, use_plt_offset));
1926 }
1927
1928 void
1929 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1930 Sized_relobj<size, big_endian>* relobj,
1931 unsigned int shndx, Address address, Addend addend,
1932 bool use_plt_offset)
1933 {
1934 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1935 addend, true, true, use_plt_offset));
1936 }
1937
1938 // Add a global relocation which does not use a symbol for the relocation,
1939 // but which gets its addend from a symbol.
1940
1941 void
1942 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1943 Address address, Addend addend)
1944 {
1945 this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1946 false, true, false));
1947 }
1948
1949 void
1950 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1951 Output_data* od,
1952 Sized_relobj<size, big_endian>* relobj,
1953 unsigned int shndx, Address address,
1954 Addend addend)
1955 {
1956 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1957 addend, false, true, false));
1958 }
1959
1960 // Add a reloc against a local symbol.
1961
1962 void
1963 add_local(Sized_relobj<size, big_endian>* relobj,
1964 unsigned int local_sym_index, unsigned int type,
1965 Output_data* od, Address address, Addend addend)
1966 {
1967 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1968 addend, false, false, false, false));
1969 }
1970
1971 void
1972 add_local(Sized_relobj<size, big_endian>* relobj,
1973 unsigned int local_sym_index, unsigned int type,
1974 Output_data* od, unsigned int shndx, Address address,
1975 Addend addend)
1976 {
1977 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1978 address, addend, false, false, false,
1979 false));
1980 }
1981
1982 void
1983 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1984 unsigned int type, Output_data* od, uint64_t address,
1985 uint64_t addend)
1986 {
1987 Sized_relobj<size, big_endian>* sized_relobj =
1988 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1989 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1990 convert_types<Address, uint64_t>(address),
1991 convert_types<Addend, uint64_t>(addend),
1992 false, false, false, false));
1993 }
1994
1995 void
1996 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1997 unsigned int type, Output_data* od, unsigned int shndx,
1998 uint64_t address, uint64_t addend)
1999 {
2000 Sized_relobj<size, big_endian>* sized_relobj =
2001 static_cast<Sized_relobj<size, big_endian>*>(relobj);
2002 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
2003 convert_types<Address, uint64_t>(address),
2004 convert_types<Addend, uint64_t>(addend),
2005 false, false, false, false));
2006 }
2007
2008 // Add a RELATIVE reloc against a local symbol.
2009
2010 void
2011 add_local_relative(Sized_relobj<size, big_endian>* relobj,
2012 unsigned int local_sym_index, unsigned int type,
2013 Output_data* od, Address address, Addend addend,
2014 bool use_plt_offset)
2015 {
2016 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2017 addend, true, true, false,
2018 use_plt_offset));
2019 }
2020
2021 void
2022 add_local_relative(Sized_relobj<size, big_endian>* relobj,
2023 unsigned int local_sym_index, unsigned int type,
2024 Output_data* od, unsigned int shndx, Address address,
2025 Addend addend, bool use_plt_offset)
2026 {
2027 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2028 address, addend, true, true, false,
2029 use_plt_offset));
2030 }
2031
2032 // Add a local relocation which does not use a symbol for the relocation,
2033 // but which gets it's addend from a symbol.
2034
2035 void
2036 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2037 unsigned int local_sym_index, unsigned int type,
2038 Output_data* od, Address address, Addend addend)
2039 {
2040 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2041 addend, false, true, false, false));
2042 }
2043
2044 void
2045 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2046 unsigned int local_sym_index, unsigned int type,
2047 Output_data* od, unsigned int shndx,
2048 Address address, Addend addend)
2049 {
2050 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2051 address, addend, false, true, false,
2052 false));
2053 }
2054
2055 // Add a reloc against a local section symbol. This will be
2056 // converted into a reloc against the STT_SECTION symbol of the
2057 // output section.
2058
2059 void
2060 add_local_section(Sized_relobj<size, big_endian>* relobj,
2061 unsigned int input_shndx, unsigned int type,
2062 Output_data* od, Address address, Addend addend)
2063 {
2064 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
2065 addend, false, false, true, false));
2066 }
2067
2068 void
2069 add_local_section(Sized_relobj<size, big_endian>* relobj,
2070 unsigned int input_shndx, unsigned int type,
2071 Output_data* od, unsigned int shndx, Address address,
2072 Addend addend)
2073 {
2074 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
2075 address, addend, false, false, true,
2076 false));
2077 }
2078
2079 // A reloc against the STT_SECTION symbol of an output section.
2080
2081 void
2082 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2083 Address address, Addend addend)
2084 { this->add(od, Output_reloc_type(os, type, od, address, addend, false)); }
2085
2086 void
2087 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2088 Sized_relobj<size, big_endian>* relobj,
2089 unsigned int shndx, Address address, Addend addend)
2090 {
2091 this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
2092 addend, false));
2093 }
2094
2095 void
2096 add_output_section_generic(Output_section* os, unsigned int type,
2097 Output_data* od, uint64_t address,
2098 uint64_t addend)
2099 {
2100 this->add(od, Output_reloc_type(os, type, od,
2101 convert_types<Address, uint64_t>(address),
2102 convert_types<Addend, uint64_t>(addend),
2103 false));
2104 }
2105
2106 void
2107 add_output_section_generic(Output_section* os, unsigned int type,
2108 Output_data* od, Relobj* relobj,
2109 unsigned int shndx, uint64_t address,
2110 uint64_t addend)
2111 {
2112 Sized_relobj<size, big_endian>* sized_relobj =
2113 static_cast<Sized_relobj<size, big_endian>*>(relobj);
2114 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
2115 convert_types<Address, uint64_t>(address),
2116 convert_types<Addend, uint64_t>(addend),
2117 false));
2118 }
2119
2120 // As above, but the reloc TYPE is relative
2121
2122 void
2123 add_output_section_relative(Output_section* os, unsigned int type,
2124 Output_data* od, Address address, Addend addend)
2125 { this->add(od, Output_reloc_type(os, type, od, address, addend, true)); }
2126
2127 void
2128 add_output_section_relative(Output_section* os, unsigned int type,
2129 Output_data* od,
2130 Sized_relobj<size, big_endian>* relobj,
2131 unsigned int shndx, Address address,
2132 Addend addend)
2133 {
2134 this->add(od, Output_reloc_type(os, type, relobj, shndx,
2135 address, addend, true));
2136 }
2137
2138 // Add an absolute relocation.
2139
2140 void
2141 add_absolute(unsigned int type, Output_data* od, Address address,
2142 Addend addend)
2143 { this->add(od, Output_reloc_type(type, od, address, addend, false)); }
2144
2145 void
2146 add_absolute(unsigned int type, Output_data* od,
2147 Sized_relobj<size, big_endian>* relobj,
2148 unsigned int shndx, Address address, Addend addend)
2149 {
2150 this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2151 false));
2152 }
2153
2154 // Add a relative relocation
2155
2156 void
2157 add_relative(unsigned int type, Output_data* od, Address address,
2158 Addend addend)
2159 { this->add(od, Output_reloc_type(type, od, address, addend, true)); }
2160
2161 void
2162 add_relative(unsigned int type, Output_data* od,
2163 Sized_relobj<size, big_endian>* relobj,
2164 unsigned int shndx, Address address, Addend addend)
2165 {
2166 this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2167 true));
2168 }
2169
2170 // Add a target specific relocation. A target which calls this must
2171 // define the reloc_symbol_index and reloc_addend virtual functions.
2172
2173 void
2174 add_target_specific(unsigned int type, void* arg, Output_data* od,
2175 Address address, Addend addend)
2176 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
2177
2178 void
2179 add_target_specific(unsigned int type, void* arg, Output_data* od,
2180 Sized_relobj<size, big_endian>* relobj,
2181 unsigned int shndx, Address address, Addend addend)
2182 {
2183 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
2184 addend));
2185 }
2186 };
2187
2188 // Output_relocatable_relocs represents a relocation section in a
2189 // relocatable link. The actual data is written out in the target
2190 // hook relocate_relocs. This just saves space for it.
2191
2192 template<int sh_type, int size, bool big_endian>
2193 class Output_relocatable_relocs : public Output_section_data
2194 {
2195 public:
2196 Output_relocatable_relocs(Relocatable_relocs* rr)
2197 : Output_section_data(Output_data::default_alignment_for_size(size)),
2198 rr_(rr)
2199 { }
2200
2201 void
2202 set_final_data_size();
2203
2204 // Write out the data. There is nothing to do here.
2205 void
2206 do_write(Output_file*)
2207 { }
2208
2209 // Write to a map file.
2210 void
2211 do_print_to_mapfile(Mapfile* mapfile) const
2212 { mapfile->print_output_data(this, _("** relocs")); }
2213
2214 private:
2215 // The relocs associated with this input section.
2216 Relocatable_relocs* rr_;
2217 };
2218
2219 // Handle a GROUP section.
2220
2221 template<int size, bool big_endian>
2222 class Output_data_group : public Output_section_data
2223 {
2224 public:
2225 // The constructor clears *INPUT_SHNDXES.
2226 Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
2227 section_size_type entry_count,
2228 elfcpp::Elf_Word flags,
2229 std::vector<unsigned int>* input_shndxes);
2230
2231 void
2232 do_write(Output_file*);
2233
2234 // Write to a map file.
2235 void
2236 do_print_to_mapfile(Mapfile* mapfile) const
2237 { mapfile->print_output_data(this, _("** group")); }
2238
2239 // Set final data size.
2240 void
2241 set_final_data_size()
2242 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
2243
2244 private:
2245 // The input object.
2246 Sized_relobj_file<size, big_endian>* relobj_;
2247 // The group flag word.
2248 elfcpp::Elf_Word flags_;
2249 // The section indexes of the input sections in this group.
2250 std::vector<unsigned int> input_shndxes_;
2251 };
2252
2253 // Output_data_got is used to manage a GOT. Each entry in the GOT is
2254 // for one symbol--either a global symbol or a local symbol in an
2255 // object. The target specific code adds entries to the GOT as
2256 // needed. The GOT_SIZE template parameter is the size in bits of a
2257 // GOT entry, typically 32 or 64.
2258
2259 class Output_data_got_base : public Output_section_data_build
2260 {
2261 public:
2262 Output_data_got_base(uint64_t align)
2263 : Output_section_data_build(align)
2264 { }
2265
2266 Output_data_got_base(off_t data_size, uint64_t align)
2267 : Output_section_data_build(data_size, align)
2268 { }
2269
2270 // Reserve the slot at index I in the GOT.
2271 void
2272 reserve_slot(unsigned int i)
2273 { this->do_reserve_slot(i); }
2274
2275 protected:
2276 // Reserve the slot at index I in the GOT.
2277 virtual void
2278 do_reserve_slot(unsigned int i) = 0;
2279 };
2280
2281 template<int got_size, bool big_endian>
2282 class Output_data_got : public Output_data_got_base
2283 {
2284 public:
2285 typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
2286
2287 Output_data_got()
2288 : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
2289 entries_(), free_list_()
2290 { }
2291
2292 Output_data_got(off_t data_size)
2293 : Output_data_got_base(data_size,
2294 Output_data::default_alignment_for_size(got_size)),
2295 entries_(), free_list_()
2296 {
2297 // For an incremental update, we have an existing GOT section.
2298 // Initialize the list of entries and the free list.
2299 this->entries_.resize(data_size / (got_size / 8));
2300 this->free_list_.init(data_size, false);
2301 }
2302
2303 // Add an entry for a global symbol to the GOT. Return true if this
2304 // is a new GOT entry, false if the symbol was already in the GOT.
2305 bool
2306 add_global(Symbol* gsym, unsigned int got_type);
2307
2308 // Like add_global, but use the PLT offset of the global symbol if
2309 // it has one.
2310 bool
2311 add_global_plt(Symbol* gsym, unsigned int got_type);
2312
2313 // Like add_global, but for a TLS symbol where the value will be
2314 // offset using Target::tls_offset_for_global.
2315 bool
2316 add_global_tls(Symbol* gsym, unsigned int got_type)
2317 { return add_global_plt(gsym, got_type); }
2318
2319 // Add an entry for a global symbol to the GOT, and add a dynamic
2320 // relocation of type R_TYPE for the GOT entry.
2321 void
2322 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2323 Output_data_reloc_generic* rel_dyn, unsigned int r_type);
2324
2325 // Add a pair of entries for a global symbol to the GOT, and add
2326 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2327 void
2328 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2329 Output_data_reloc_generic* rel_dyn,
2330 unsigned int r_type_1, unsigned int r_type_2);
2331
2332 // Add an entry for a local symbol to the GOT. This returns true if
2333 // this is a new GOT entry, false if the symbol already has a GOT
2334 // entry.
2335 bool
2336 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type);
2337
2338 // Add an entry for a local symbol plus ADDEND to the GOT. This returns
2339 // true if this is a new GOT entry, false if the symbol already has a GOT
2340 // entry.
2341 bool
2342 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
2343 uint64_t addend);
2344
2345 // Like add_local, but use the PLT offset of the local symbol if it
2346 // has one.
2347 bool
2348 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type);
2349
2350 // Like add_local, but for a TLS symbol where the value will be
2351 // offset using Target::tls_offset_for_local.
2352 bool
2353 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2354 { return add_local_plt(object, sym_index, got_type); }
2355
2356 // Add an entry for a local symbol to the GOT, and add a dynamic
2357 // relocation of type R_TYPE for the GOT entry.
2358 void
2359 add_local_with_rel(Relobj* object, unsigned int sym_index,
2360 unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2361 unsigned int r_type);
2362
2363 // Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
2364 // relocation of type R_TYPE for the GOT entry.
2365 void
2366 add_local_with_rel(Relobj* object, unsigned int sym_index,
2367 unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2368 unsigned int r_type, uint64_t addend);
2369
2370 // Add a pair of entries for a local symbol to the GOT, and add
2371 // a dynamic relocation of type R_TYPE using the section symbol of
2372 // the output section to which input section SHNDX maps, on the first.
2373 // The first got entry will have a value of zero, the second the
2374 // value of the local symbol.
2375 void
2376 add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2377 unsigned int shndx, unsigned int got_type,
2378 Output_data_reloc_generic* rel_dyn,
2379 unsigned int r_type);
2380
2381 // Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
2382 // a dynamic relocation of type R_TYPE using the section symbol of
2383 // the output section to which input section SHNDX maps, on the first.
2384 // The first got entry will have a value of zero, the second the
2385 // value of the local symbol.
2386 void
2387 add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2388 unsigned int shndx, unsigned int got_type,
2389 Output_data_reloc_generic* rel_dyn,
2390 unsigned int r_type, uint64_t addend);
2391
2392 // Add a pair of entries for a local symbol to the GOT, and add
2393 // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
2394 // The first got entry will have a value of zero, the second the
2395 // value of the local symbol offset by Target::tls_offset_for_local.
2396 void
2397 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2398 unsigned int got_type,
2399 Output_data_reloc_generic* rel_dyn,
2400 unsigned int r_type);
2401
2402 // Add a constant to the GOT. This returns the offset of the new
2403 // entry from the start of the GOT.
2404 unsigned int
2405 add_constant(Valtype constant)
2406 { return this->add_got_entry(Got_entry(constant)); }
2407
2408 // Add a pair of constants to the GOT. This returns the offset of
2409 // the new entry from the start of the GOT.
2410 unsigned int
2411 add_constant_pair(Valtype c1, Valtype c2)
2412 { return this->add_got_entry_pair(Got_entry(c1), Got_entry(c2)); }
2413
2414 // Replace GOT entry I with a new constant.
2415 void
2416 replace_constant(unsigned int i, Valtype constant)
2417 {
2418 this->replace_got_entry(i, Got_entry(constant));
2419 }
2420
2421 // Reserve a slot in the GOT for a local symbol.
2422 void
2423 reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2424 unsigned int got_type);
2425
2426 // Reserve a slot in the GOT for a global symbol.
2427 void
2428 reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2429
2430 protected:
2431 // Write out the GOT table.
2432 void
2433 do_write(Output_file*);
2434
2435 // Write to a map file.
2436 void
2437 do_print_to_mapfile(Mapfile* mapfile) const
2438 { mapfile->print_output_data(this, _("** GOT")); }
2439
2440 // Reserve the slot at index I in the GOT.
2441 virtual void
2442 do_reserve_slot(unsigned int i)
2443 { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2444
2445 // Return the number of words in the GOT.
2446 unsigned int
2447 num_entries () const
2448 { return this->entries_.size(); }
2449
2450 // Return the offset into the GOT of GOT entry I.
2451 unsigned int
2452 got_offset(unsigned int i) const
2453 { return i * (got_size / 8); }
2454
2455 private:
2456 // This POD class holds a single GOT entry.
2457 class Got_entry
2458 {
2459 public:
2460 // Create a zero entry.
2461 Got_entry()
2462 : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false),
2463 addend_(0)
2464 { this->u_.constant = 0; }
2465
2466 // Create a global symbol entry.
2467 Got_entry(Symbol* gsym, bool use_plt_or_tls_offset)
2468 : local_sym_index_(GSYM_CODE),
2469 use_plt_or_tls_offset_(use_plt_or_tls_offset), addend_(0)
2470 { this->u_.gsym = gsym; }
2471
2472 // Create a local symbol entry.
2473 Got_entry(Relobj* object, unsigned int local_sym_index,
2474 bool use_plt_or_tls_offset)
2475 : local_sym_index_(local_sym_index),
2476 use_plt_or_tls_offset_(use_plt_or_tls_offset), addend_(0)
2477 {
2478 gold_assert(local_sym_index != GSYM_CODE
2479 && local_sym_index != CONSTANT_CODE
2480 && local_sym_index != RESERVED_CODE
2481 && local_sym_index == this->local_sym_index_);
2482 this->u_.object = object;
2483 }
2484
2485 // Create a local symbol entry plus addend.
2486 Got_entry(Relobj* object, unsigned int local_sym_index,
2487 bool use_plt_or_tls_offset, uint64_t addend)
2488 : local_sym_index_(local_sym_index),
2489 use_plt_or_tls_offset_(use_plt_or_tls_offset), addend_(addend)
2490 {
2491 gold_assert(local_sym_index != GSYM_CODE
2492 && local_sym_index != CONSTANT_CODE
2493 && local_sym_index != RESERVED_CODE
2494 && local_sym_index == this->local_sym_index_);
2495 this->u_.object = object;
2496 }
2497
2498 // Create a constant entry. The constant is a host value--it will
2499 // be swapped, if necessary, when it is written out.
2500 explicit Got_entry(Valtype constant)
2501 : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
2502 { this->u_.constant = constant; }
2503
2504 // Write the GOT entry to an output view.
2505 void
2506 write(unsigned int got_indx, unsigned char* pov) const;
2507
2508 private:
2509 enum
2510 {
2511 GSYM_CODE = 0x7fffffff,
2512 CONSTANT_CODE = 0x7ffffffe,
2513 RESERVED_CODE = 0x7ffffffd
2514 };
2515
2516 union
2517 {
2518 // For a local symbol, the object.
2519 Relobj* object;
2520 // For a global symbol, the symbol.
2521 Symbol* gsym;
2522 // For a constant, the constant.
2523 Valtype constant;
2524 } u_;
2525 // For a local symbol, the local symbol index. This is GSYM_CODE
2526 // for a global symbol, or CONSTANT_CODE for a constant.
2527 unsigned int local_sym_index_ : 31;
2528 // Whether to use the PLT offset of the symbol if it has one.
2529 // For TLS symbols, whether to offset the symbol value.
2530 bool use_plt_or_tls_offset_ : 1;
2531 // The addend.
2532 uint64_t addend_;
2533 };
2534
2535 typedef std::vector<Got_entry> Got_entries;
2536
2537 // Create a new GOT entry and return its offset.
2538 unsigned int
2539 add_got_entry(Got_entry got_entry);
2540
2541 // Create a pair of new GOT entries and return the offset of the first.
2542 unsigned int
2543 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2544
2545 // Replace GOT entry I with a new value.
2546 void
2547 replace_got_entry(unsigned int i, Got_entry got_entry);
2548
2549 // Return the offset into the GOT of the last entry added.
2550 unsigned int
2551 last_got_offset() const
2552 { return this->got_offset(this->num_entries() - 1); }
2553
2554 // Set the size of the section.
2555 void
2556 set_got_size()
2557 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2558
2559 // The list of GOT entries.
2560 Got_entries entries_;
2561
2562 // List of available regions within the section, for incremental
2563 // update links.
2564 Free_list free_list_;
2565 };
2566
2567 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2568 // section.
2569
2570 class Output_data_dynamic : public Output_section_data
2571 {
2572 public:
2573 Output_data_dynamic(Stringpool* pool)
2574 : Output_section_data(Output_data::default_alignment()),
2575 entries_(), pool_(pool)
2576 { }
2577
2578 // Add a new dynamic entry with a fixed numeric value.
2579 void
2580 add_constant(elfcpp::DT tag, unsigned int val)
2581 { this->add_entry(Dynamic_entry(tag, val)); }
2582
2583 // Add a new dynamic entry with the address of output data.
2584 void
2585 add_section_address(elfcpp::DT tag, const Output_data* od)
2586 { this->add_entry(Dynamic_entry(tag, od, false)); }
2587
2588 // Add a new dynamic entry with the address of output data
2589 // plus a constant offset.
2590 void
2591 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2592 unsigned int offset)
2593 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2594
2595 // Add a new dynamic entry with the size of output data.
2596 void
2597 add_section_size(elfcpp::DT tag, const Output_data* od)
2598 { this->add_entry(Dynamic_entry(tag, od, true)); }
2599
2600 // Add a new dynamic entry with the total size of two output datas.
2601 void
2602 add_section_size(elfcpp::DT tag, const Output_data* od,
2603 const Output_data* od2)
2604 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2605
2606 // Add a new dynamic entry with the address of a symbol.
2607 void
2608 add_symbol(elfcpp::DT tag, const Symbol* sym)
2609 { this->add_entry(Dynamic_entry(tag, sym)); }
2610
2611 // Add a new dynamic entry with a string.
2612 void
2613 add_string(elfcpp::DT tag, const char* str)
2614 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2615
2616 void
2617 add_string(elfcpp::DT tag, const std::string& str)
2618 { this->add_string(tag, str.c_str()); }
2619
2620 // Add a new dynamic entry with custom value.
2621 void
2622 add_custom(elfcpp::DT tag)
2623 { this->add_entry(Dynamic_entry(tag)); }
2624
2625 protected:
2626 // Adjust the output section to set the entry size.
2627 void
2628 do_adjust_output_section(Output_section*);
2629
2630 // Set the final data size.
2631 void
2632 set_final_data_size();
2633
2634 // Write out the dynamic entries.
2635 void
2636 do_write(Output_file*);
2637
2638 // Write to a map file.
2639 void
2640 do_print_to_mapfile(Mapfile* mapfile) const
2641 { mapfile->print_output_data(this, _("** dynamic")); }
2642
2643 private:
2644 // This POD class holds a single dynamic entry.
2645 class Dynamic_entry
2646 {
2647 public:
2648 // Create an entry with a fixed numeric value.
2649 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2650 : tag_(tag), offset_(DYNAMIC_NUMBER)
2651 { this->u_.val = val; }
2652
2653 // Create an entry with the size or address of a section.
2654 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2655 : tag_(tag),
2656 offset_(section_size
2657 ? DYNAMIC_SECTION_SIZE
2658 : DYNAMIC_SECTION_ADDRESS)
2659 {
2660 this->u_.od = od;
2661 this->od2 = NULL;
2662 }
2663
2664 // Create an entry with the size of two sections.
2665 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2666 : tag_(tag),
2667 offset_(DYNAMIC_SECTION_SIZE)
2668 {
2669 this->u_.od = od;
2670 this->od2 = od2;
2671 }
2672
2673 // Create an entry with the address of a section plus a constant offset.
2674 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2675 : tag_(tag),
2676 offset_(offset)
2677 { this->u_.od = od; }
2678
2679 // Create an entry with the address of a symbol.
2680 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2681 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2682 { this->u_.sym = sym; }
2683
2684 // Create an entry with a string.
2685 Dynamic_entry(elfcpp::DT tag, const char* str)
2686 : tag_(tag), offset_(DYNAMIC_STRING)
2687 { this->u_.str = str; }
2688
2689 // Create an entry with a custom value.
2690 Dynamic_entry(elfcpp::DT tag)
2691 : tag_(tag), offset_(DYNAMIC_CUSTOM)
2692 { }
2693
2694 // Return the tag of this entry.
2695 elfcpp::DT
2696 tag() const
2697 { return this->tag_; }
2698
2699 // Write the dynamic entry to an output view.
2700 template<int size, bool big_endian>
2701 void
2702 write(unsigned char* pov, const Stringpool*) const;
2703
2704 private:
2705 // Classification is encoded in the OFFSET field.
2706 enum Classification
2707 {
2708 // Section address.
2709 DYNAMIC_SECTION_ADDRESS = 0,
2710 // Number.
2711 DYNAMIC_NUMBER = -1U,
2712 // Section size.
2713 DYNAMIC_SECTION_SIZE = -2U,
2714 // Symbol adress.
2715 DYNAMIC_SYMBOL = -3U,
2716 // String.
2717 DYNAMIC_STRING = -4U,
2718 // Custom value.
2719 DYNAMIC_CUSTOM = -5U
2720 // Any other value indicates a section address plus OFFSET.
2721 };
2722
2723 union
2724 {
2725 // For DYNAMIC_NUMBER.
2726 unsigned int val;
2727 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2728 const Output_data* od;
2729 // For DYNAMIC_SYMBOL.
2730 const Symbol* sym;
2731 // For DYNAMIC_STRING.
2732 const char* str;
2733 } u_;
2734 // For DYNAMIC_SYMBOL with two sections.
2735 const Output_data* od2;
2736 // The dynamic tag.
2737 elfcpp::DT tag_;
2738 // The type of entry (Classification) or offset within a section.
2739 unsigned int offset_;
2740 };
2741
2742 // Add an entry to the list.
2743 void
2744 add_entry(const Dynamic_entry& entry)
2745 { this->entries_.push_back(entry); }
2746
2747 // Sized version of write function.
2748 template<int size, bool big_endian>
2749 void
2750 sized_write(Output_file* of);
2751
2752 // The type of the list of entries.
2753 typedef std::vector<Dynamic_entry> Dynamic_entries;
2754
2755 // The entries.
2756 Dynamic_entries entries_;
2757 // The pool used for strings.
2758 Stringpool* pool_;
2759 };
2760
2761 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2762 // which may be required if the object file has more than
2763 // SHN_LORESERVE sections.
2764
2765 class Output_symtab_xindex : public Output_section_data
2766 {
2767 public:
2768 Output_symtab_xindex(size_t symcount)
2769 : Output_section_data(symcount * 4, 4, true),
2770 entries_()
2771 { }
2772
2773 // Add an entry: symbol number SYMNDX has section SHNDX.
2774 void
2775 add(unsigned int symndx, unsigned int shndx)
2776 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2777
2778 protected:
2779 void
2780 do_write(Output_file*);
2781
2782 // Write to a map file.
2783 void
2784 do_print_to_mapfile(Mapfile* mapfile) const
2785 { mapfile->print_output_data(this, _("** symtab xindex")); }
2786
2787 private:
2788 template<bool big_endian>
2789 void
2790 endian_do_write(unsigned char*);
2791
2792 // It is likely that most symbols will not require entries. Rather
2793 // than keep a vector for all symbols, we keep pairs of symbol index
2794 // and section index.
2795 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2796
2797 // The entries we need.
2798 Xindex_entries entries_;
2799 };
2800
2801 // A relaxed input section.
2802 class Output_relaxed_input_section : public Output_section_data_build
2803 {
2804 public:
2805 // We would like to call relobj->section_addralign(shndx) to get the
2806 // alignment but we do not want the constructor to fail. So callers
2807 // are repsonsible for ensuring that.
2808 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2809 uint64_t addralign)
2810 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2811 { }
2812
2813 // Return the Relobj of this relaxed input section.
2814 Relobj*
2815 relobj() const
2816 { return this->relobj_; }
2817
2818 // Return the section index of this relaxed input section.
2819 unsigned int
2820 shndx() const
2821 { return this->shndx_; }
2822
2823 protected:
2824 void
2825 set_relobj(Relobj* relobj)
2826 { this->relobj_ = relobj; }
2827
2828 void
2829 set_shndx(unsigned int shndx)
2830 { this->shndx_ = shndx; }
2831
2832 private:
2833 Relobj* relobj_;
2834 unsigned int shndx_;
2835 };
2836
2837 // This class describes properties of merge data sections. It is used
2838 // as a key type for maps.
2839 class Merge_section_properties
2840 {
2841 public:
2842 Merge_section_properties(bool is_string, uint64_t entsize,
2843 uint64_t addralign)
2844 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2845 { }
2846
2847 // Whether this equals to another Merge_section_properties MSP.
2848 bool
2849 eq(const Merge_section_properties& msp) const
2850 {
2851 return ((this->is_string_ == msp.is_string_)
2852 && (this->entsize_ == msp.entsize_)
2853 && (this->addralign_ == msp.addralign_));
2854 }
2855
2856 // Compute a hash value for this using 64-bit FNV-1a hash.
2857 size_t
2858 hash_value() const
2859 {
2860 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2861 uint64_t prime = 1099511628211ULL;
2862 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2863 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2864 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2865 return h;
2866 }
2867
2868 // Functors for associative containers.
2869 struct equal_to
2870 {
2871 bool
2872 operator()(const Merge_section_properties& msp1,
2873 const Merge_section_properties& msp2) const
2874 { return msp1.eq(msp2); }
2875 };
2876
2877 struct hash
2878 {
2879 size_t
2880 operator()(const Merge_section_properties& msp) const
2881 { return msp.hash_value(); }
2882 };
2883
2884 private:
2885 // Whether this merge data section is for strings.
2886 bool is_string_;
2887 // Entsize of this merge data section.
2888 uint64_t entsize_;
2889 // Address alignment.
2890 uint64_t addralign_;
2891 };
2892
2893 // This class is used to speed up look up of special input sections in an
2894 // Output_section.
2895
2896 class Output_section_lookup_maps
2897 {
2898 public:
2899 Output_section_lookup_maps()
2900 : is_valid_(true), merge_sections_by_properties_(),
2901 relaxed_input_sections_by_id_()
2902 { }
2903
2904 // Whether the maps are valid.
2905 bool
2906 is_valid() const
2907 { return this->is_valid_; }
2908
2909 // Invalidate the maps.
2910 void
2911 invalidate()
2912 { this->is_valid_ = false; }
2913
2914 // Clear the maps.
2915 void
2916 clear()
2917 {
2918 this->merge_sections_by_properties_.clear();
2919 this->relaxed_input_sections_by_id_.clear();
2920 // A cleared map is valid.
2921 this->is_valid_ = true;
2922 }
2923
2924 // Find a merge section by merge section properties. Return NULL if none
2925 // is found.
2926 Output_merge_base*
2927 find_merge_section(const Merge_section_properties& msp) const
2928 {
2929 gold_assert(this->is_valid_);
2930 Merge_sections_by_properties::const_iterator p =
2931 this->merge_sections_by_properties_.find(msp);
2932 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2933 }
2934
2935 // Add a merge section pointed by POMB with properties MSP.
2936 void
2937 add_merge_section(const Merge_section_properties& msp,
2938 Output_merge_base* pomb)
2939 {
2940 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2941 std::pair<Merge_sections_by_properties::iterator, bool> result =
2942 this->merge_sections_by_properties_.insert(value);
2943 gold_assert(result.second);
2944 }
2945
2946 // Find a relaxed input section of OBJECT with index SHNDX.
2947 Output_relaxed_input_section*
2948 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const
2949 {
2950 gold_assert(this->is_valid_);
2951 Relaxed_input_sections_by_id::const_iterator p =
2952 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2953 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2954 }
2955
2956 // Add a relaxed input section pointed by POMB and whose original input
2957 // section is in OBJECT with index SHNDX.
2958 void
2959 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2960 Output_relaxed_input_section* poris)
2961 {
2962 Const_section_id csid(relobj, shndx);
2963 std::pair<Const_section_id, Output_relaxed_input_section*>
2964 value(csid, poris);
2965 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2966 this->relaxed_input_sections_by_id_.insert(value);
2967 gold_assert(result.second);
2968 }
2969
2970 private:
2971 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2972 Merge_section_properties::hash,
2973 Merge_section_properties::equal_to>
2974 Merge_sections_by_properties;
2975
2976 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2977 Const_section_id_hash>
2978 Relaxed_input_sections_by_id;
2979
2980 // Whether this is valid
2981 bool is_valid_;
2982 // Merge sections by merge section properties.
2983 Merge_sections_by_properties merge_sections_by_properties_;
2984 // Relaxed sections by section IDs.
2985 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2986 };
2987
2988 // This abstract base class defines the interface for the
2989 // types of methods used to fill free space left in an output
2990 // section during an incremental link. These methods are used
2991 // to insert dummy compilation units into debug info so that
2992 // debug info consumers can scan the debug info serially.
2993
2994 class Output_fill
2995 {
2996 public:
2997 Output_fill()
2998 : is_big_endian_(parameters->target().is_big_endian())
2999 { }
3000
3001 virtual
3002 ~Output_fill()
3003 { }
3004
3005 // Return the smallest size chunk of free space that can be
3006 // filled with a dummy compilation unit.
3007 size_t
3008 minimum_hole_size() const
3009 { return this->do_minimum_hole_size(); }
3010
3011 // Write a fill pattern of length LEN at offset OFF in the file.
3012 void
3013 write(Output_file* of, off_t off, size_t len) const
3014 { this->do_write(of, off, len); }
3015
3016 protected:
3017 virtual size_t
3018 do_minimum_hole_size() const = 0;
3019
3020 virtual void
3021 do_write(Output_file* of, off_t off, size_t len) const = 0;
3022
3023 bool
3024 is_big_endian() const
3025 { return this->is_big_endian_; }
3026
3027 private:
3028 bool is_big_endian_;
3029 };
3030
3031 // Fill method that introduces a dummy compilation unit in
3032 // a .debug_info or .debug_types section.
3033
3034 class Output_fill_debug_info : public Output_fill
3035 {
3036 public:
3037 Output_fill_debug_info(bool is_debug_types)
3038 : is_debug_types_(is_debug_types)
3039 { }
3040
3041 protected:
3042 virtual size_t
3043 do_minimum_hole_size() const;
3044
3045 virtual void
3046 do_write(Output_file* of, off_t off, size_t len) const;
3047
3048 private:
3049 // Version of the header.
3050 static const int version = 4;
3051 // True if this is a .debug_types section.
3052 bool is_debug_types_;
3053 };
3054
3055 // Fill method that introduces a dummy compilation unit in
3056 // a .debug_line section.
3057
3058 class Output_fill_debug_line : public Output_fill
3059 {
3060 public:
3061 Output_fill_debug_line()
3062 { }
3063
3064 protected:
3065 virtual size_t
3066 do_minimum_hole_size() const;
3067
3068 virtual void
3069 do_write(Output_file* of, off_t off, size_t len) const;
3070
3071 private:
3072 // Version of the header. We write a DWARF-3 header because it's smaller
3073 // and many tools have not yet been updated to understand the DWARF-4 header.
3074 static const int version = 3;
3075 // Length of the portion of the header that follows the header_length
3076 // field. This includes the following fields:
3077 // minimum_instruction_length, default_is_stmt, line_base, line_range,
3078 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3079 // The standard_opcode_lengths array is 12 bytes long, and the
3080 // include_directories and filenames fields each contain only a single
3081 // null byte.
3082 static const size_t header_length = 19;
3083 };
3084
3085 // An output section. We don't expect to have too many output
3086 // sections, so we don't bother to do a template on the size.
3087
3088 class Output_section : public Output_data
3089 {
3090 public:
3091 // Create an output section, giving the name, type, and flags.
3092 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
3093 virtual ~Output_section();
3094
3095 // Add a new input section SHNDX, named NAME, with header SHDR, from
3096 // object OBJECT. RELOC_SHNDX is the index of a relocation section
3097 // which applies to this section, or 0 if none, or -1 if more than
3098 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3099 // in a linker script; in that case we need to keep track of input
3100 // sections associated with an output section. Return the offset
3101 // within the output section.
3102 template<int size, bool big_endian>
3103 off_t
3104 add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
3105 unsigned int shndx, const char* name,
3106 const elfcpp::Shdr<size, big_endian>& shdr,
3107 unsigned int reloc_shndx, bool have_sections_script);
3108
3109 // Add generated data POSD to this output section.
3110 void
3111 add_output_section_data(Output_section_data* posd);
3112
3113 // Add a relaxed input section PORIS called NAME to this output section
3114 // with LAYOUT.
3115 void
3116 add_relaxed_input_section(Layout* layout,
3117 Output_relaxed_input_section* poris,
3118 const std::string& name);
3119
3120 // Return the section name.
3121 const char*
3122 name() const
3123 { return this->name_; }
3124
3125 // Return the section type.
3126 elfcpp::Elf_Word
3127 type() const
3128 { return this->type_; }
3129
3130 // Return the section flags.
3131 elfcpp::Elf_Xword
3132 flags() const
3133 { return this->flags_; }
3134
3135 typedef std::map<Section_id, unsigned int> Section_layout_order;
3136
3137 void
3138 update_section_layout(const Section_layout_order* order_map);
3139
3140 // Update the output section flags based on input section flags.
3141 void
3142 update_flags_for_input_section(elfcpp::Elf_Xword flags);
3143
3144 // Set the output section flags.
3145 void
3146 set_flags(elfcpp::Elf_Xword flags)
3147 { this->flags_ = flags; }
3148
3149 // Return the entsize field.
3150 uint64_t
3151 entsize() const
3152 { return this->entsize_; }
3153
3154 // Set the entsize field.
3155 void
3156 set_entsize(uint64_t v);
3157
3158 // Set the load address.
3159 void
3160 set_load_address(uint64_t load_address)
3161 {
3162 this->load_address_ = load_address;
3163 this->has_load_address_ = true;
3164 }
3165
3166 // Set the link field to the output section index of a section.
3167 void
3168 set_link_section(const Output_data* od)
3169 {
3170 gold_assert(this->link_ == 0
3171 && !this->should_link_to_symtab_
3172 && !this->should_link_to_dynsym_);
3173 this->link_section_ = od;
3174 }
3175
3176 // Set the link field to a constant.
3177 void
3178 set_link(unsigned int v)
3179 {
3180 gold_assert(this->link_section_ == NULL
3181 && !this->should_link_to_symtab_
3182 && !this->should_link_to_dynsym_);
3183 this->link_ = v;
3184 }
3185
3186 // Record that this section should link to the normal symbol table.
3187 void
3188 set_should_link_to_symtab()
3189 {
3190 gold_assert(this->link_section_ == NULL
3191 && this->link_ == 0
3192 && !this->should_link_to_dynsym_);
3193 this->should_link_to_symtab_ = true;
3194 }
3195
3196 // Record that this section should link to the dynamic symbol table.
3197 void
3198 set_should_link_to_dynsym()
3199 {
3200 gold_assert(this->link_section_ == NULL
3201 && this->link_ == 0
3202 && !this->should_link_to_symtab_);
3203 this->should_link_to_dynsym_ = true;
3204 }
3205
3206 // Return the info field.
3207 unsigned int
3208 info() const
3209 {
3210 gold_assert(this->info_section_ == NULL
3211 && this->info_symndx_ == NULL);
3212 return this->info_;
3213 }
3214
3215 // Set the info field to the output section index of a section.
3216 void
3217 set_info_section(const Output_section* os)
3218 {
3219 gold_assert((this->info_section_ == NULL
3220 || (this->info_section_ == os
3221 && this->info_uses_section_index_))
3222 && this->info_symndx_ == NULL
3223 && this->info_ == 0);
3224 this->info_section_ = os;
3225 this->info_uses_section_index_= true;
3226 }
3227
3228 // Set the info field to the symbol table index of a symbol.
3229 void
3230 set_info_symndx(const Symbol* sym)
3231 {
3232 gold_assert(this->info_section_ == NULL
3233 && (this->info_symndx_ == NULL
3234 || this->info_symndx_ == sym)
3235 && this->info_ == 0);
3236 this->info_symndx_ = sym;
3237 }
3238
3239 // Set the info field to the symbol table index of a section symbol.
3240 void
3241 set_info_section_symndx(const Output_section* os)
3242 {
3243 gold_assert((this->info_section_ == NULL
3244 || (this->info_section_ == os
3245 && !this->info_uses_section_index_))
3246 && this->info_symndx_ == NULL
3247 && this->info_ == 0);
3248 this->info_section_ = os;
3249 this->info_uses_section_index_ = false;
3250 }
3251
3252 // Set the info field to a constant.
3253 void
3254 set_info(unsigned int v)
3255 {
3256 gold_assert(this->info_section_ == NULL
3257 && this->info_symndx_ == NULL
3258 && (this->info_ == 0
3259 || this->info_ == v));
3260 this->info_ = v;
3261 }
3262
3263 // Set the addralign field.
3264 void
3265 set_addralign(uint64_t v)
3266 { this->addralign_ = v; }
3267
3268 void
3269 checkpoint_set_addralign(uint64_t val)
3270 {
3271 if (this->checkpoint_ != NULL)
3272 this->checkpoint_->set_addralign(val);
3273 }
3274
3275 // Whether the output section index has been set.
3276 bool
3277 has_out_shndx() const
3278 { return this->out_shndx_ != -1U; }
3279
3280 // Indicate that we need a symtab index.
3281 void
3282 set_needs_symtab_index()
3283 { this->needs_symtab_index_ = true; }
3284
3285 // Return whether we need a symtab index.
3286 bool
3287 needs_symtab_index() const
3288 { return this->needs_symtab_index_; }
3289
3290 // Get the symtab index.
3291 unsigned int
3292 symtab_index() const
3293 {
3294 gold_assert(this->symtab_index_ != 0);
3295 return this->symtab_index_;
3296 }
3297
3298 // Set the symtab index.
3299 void
3300 set_symtab_index(unsigned int index)
3301 {
3302 gold_assert(index != 0);
3303 this->symtab_index_ = index;
3304 }
3305
3306 // Indicate that we need a dynsym index.
3307 void
3308 set_needs_dynsym_index()
3309 { this->needs_dynsym_index_ = true; }
3310
3311 // Return whether we need a dynsym index.
3312 bool
3313 needs_dynsym_index() const
3314 { return this->needs_dynsym_index_; }
3315
3316 // Get the dynsym index.
3317 unsigned int
3318 dynsym_index() const
3319 {
3320 gold_assert(this->dynsym_index_ != 0);
3321 return this->dynsym_index_;
3322 }
3323
3324 // Set the dynsym index.
3325 void
3326 set_dynsym_index(unsigned int index)
3327 {
3328 gold_assert(index != 0);
3329 this->dynsym_index_ = index;
3330 }
3331
3332 // Sort the attached input sections.
3333 void
3334 sort_attached_input_sections();
3335
3336 // Return whether the input sections sections attachd to this output
3337 // section may require sorting. This is used to handle constructor
3338 // priorities compatibly with GNU ld.
3339 bool
3340 may_sort_attached_input_sections() const
3341 { return this->may_sort_attached_input_sections_; }
3342
3343 // Record that the input sections attached to this output section
3344 // may require sorting.
3345 void
3346 set_may_sort_attached_input_sections()
3347 { this->may_sort_attached_input_sections_ = true; }
3348
3349 // Returns true if input sections must be sorted according to the
3350 // order in which their name appear in the --section-ordering-file.
3351 bool
3352 input_section_order_specified()
3353 { return this->input_section_order_specified_; }
3354
3355 // Record that input sections must be sorted as some of their names
3356 // match the patterns specified through --section-ordering-file.
3357 void
3358 set_input_section_order_specified()
3359 { this->input_section_order_specified_ = true; }
3360
3361 // Return whether the input sections attached to this output section
3362 // require sorting. This is used to handle constructor priorities
3363 // compatibly with GNU ld.
3364 bool
3365 must_sort_attached_input_sections() const
3366 { return this->must_sort_attached_input_sections_; }
3367
3368 // Record that the input sections attached to this output section
3369 // require sorting.
3370 void
3371 set_must_sort_attached_input_sections()
3372 { this->must_sort_attached_input_sections_ = true; }
3373
3374 // Get the order in which this section appears in the PT_LOAD output
3375 // segment.
3376 Output_section_order
3377 order() const
3378 { return this->order_; }
3379
3380 // Set the order for this section.
3381 void
3382 set_order(Output_section_order order)
3383 { this->order_ = order; }
3384
3385 // Return whether this section holds relro data--data which has
3386 // dynamic relocations but which may be marked read-only after the
3387 // dynamic relocations have been completed.
3388 bool
3389 is_relro() const
3390 { return this->is_relro_; }
3391
3392 // Record that this section holds relro data.
3393 void
3394 set_is_relro()
3395 { this->is_relro_ = true; }
3396
3397 // Record that this section does not hold relro data.
3398 void
3399 clear_is_relro()
3400 { this->is_relro_ = false; }
3401
3402 // True if this is a small section: a section which holds small
3403 // variables.
3404 bool
3405 is_small_section() const
3406 { return this->is_small_section_; }
3407
3408 // Record that this is a small section.
3409 void
3410 set_is_small_section()
3411 { this->is_small_section_ = true; }
3412
3413 // True if this is a large section: a section which holds large
3414 // variables.
3415 bool
3416 is_large_section() const
3417 { return this->is_large_section_; }
3418
3419 // Record that this is a large section.
3420 void
3421 set_is_large_section()
3422 { this->is_large_section_ = true; }
3423
3424 // True if this is a large data (not BSS) section.
3425 bool
3426 is_large_data_section()
3427 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3428
3429 // Return whether this section should be written after all the input
3430 // sections are complete.
3431 bool
3432 after_input_sections() const
3433 { return this->after_input_sections_; }
3434
3435 // Record that this section should be written after all the input
3436 // sections are complete.
3437 void
3438 set_after_input_sections()
3439 { this->after_input_sections_ = true; }
3440
3441 // Return whether this section requires postprocessing after all
3442 // relocations have been applied.
3443 bool
3444 requires_postprocessing() const
3445 { return this->requires_postprocessing_; }
3446
3447 bool
3448 is_unique_segment() const
3449 { return this->is_unique_segment_; }
3450
3451 void
3452 set_is_unique_segment()
3453 { this->is_unique_segment_ = true; }
3454
3455 uint64_t extra_segment_flags() const
3456 { return this->extra_segment_flags_; }
3457
3458 void
3459 set_extra_segment_flags(uint64_t flags)
3460 { this->extra_segment_flags_ = flags; }
3461
3462 uint64_t segment_alignment() const
3463 { return this->segment_alignment_; }
3464
3465 void
3466 set_segment_alignment(uint64_t align)
3467 { this->segment_alignment_ = align; }
3468
3469 // If a section requires postprocessing, return the buffer to use.
3470 unsigned char*
3471 postprocessing_buffer() const
3472 {
3473 gold_assert(this->postprocessing_buffer_ != NULL);
3474 return this->postprocessing_buffer_;
3475 }
3476
3477 // If a section requires postprocessing, create the buffer to use.
3478 void
3479 create_postprocessing_buffer();
3480
3481 // If a section requires postprocessing, this is the size of the
3482 // buffer to which relocations should be applied.
3483 off_t
3484 postprocessing_buffer_size() const
3485 { return this->current_data_size_for_child(); }
3486
3487 // Modify the section name. This is only permitted for an
3488 // unallocated section, and only before the size has been finalized.
3489 // Otherwise the name will not get into Layout::namepool_.
3490 void
3491 set_name(const char* newname)
3492 {
3493 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3494 gold_assert(!this->is_data_size_valid());
3495 this->name_ = newname;
3496 }
3497
3498 // Return whether the offset OFFSET in the input section SHNDX in
3499 // object OBJECT is being included in the link.
3500 bool
3501 is_input_address_mapped(const Relobj* object, unsigned int shndx,
3502 off_t offset) const;
3503
3504 // Return the offset within the output section of OFFSET relative to
3505 // the start of input section SHNDX in object OBJECT.
3506 section_offset_type
3507 output_offset(const Relobj* object, unsigned int shndx,
3508 section_offset_type offset) const;
3509
3510 // Return the output virtual address of OFFSET relative to the start
3511 // of input section SHNDX in object OBJECT.
3512 uint64_t
3513 output_address(const Relobj* object, unsigned int shndx,
3514 off_t offset) const;
3515
3516 // Look for the merged section for input section SHNDX in object
3517 // OBJECT. If found, return true, and set *ADDR to the address of
3518 // the start of the merged section. This is not necessary the
3519 // output offset corresponding to input offset 0 in the section,
3520 // since the section may be mapped arbitrarily.
3521 bool
3522 find_starting_output_address(const Relobj* object, unsigned int shndx,
3523 uint64_t* addr) const;
3524
3525 // Record that this output section was found in the SECTIONS clause
3526 // of a linker script.
3527 void
3528 set_found_in_sections_clause()
3529 { this->found_in_sections_clause_ = true; }
3530
3531 // Return whether this output section was found in the SECTIONS
3532 // clause of a linker script.
3533 bool
3534 found_in_sections_clause() const
3535 { return this->found_in_sections_clause_; }
3536
3537 // Write the section header into *OPHDR.
3538 template<int size, bool big_endian>
3539 void
3540 write_header(const Layout*, const Stringpool*,
3541 elfcpp::Shdr_write<size, big_endian>*) const;
3542
3543 // The next few calls are for linker script support.
3544
3545 // In some cases we need to keep a list of the input sections
3546 // associated with this output section. We only need the list if we
3547 // might have to change the offsets of the input section within the
3548 // output section after we add the input section. The ordinary
3549 // input sections will be written out when we process the object
3550 // file, and as such we don't need to track them here. We do need
3551 // to track Output_section_data objects here. We store instances of
3552 // this structure in a std::vector, so it must be a POD. There can
3553 // be many instances of this structure, so we use a union to save
3554 // some space.
3555 class Input_section
3556 {
3557 public:
3558 Input_section()
3559 : shndx_(0), p2align_(0)
3560 {
3561 this->u1_.data_size = 0;
3562 this->u2_.object = NULL;
3563 }
3564
3565 // For an ordinary input section.
3566 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3567 uint64_t addralign)
3568 : shndx_(shndx),
3569 p2align_(ffsll(static_cast<long long>(addralign))),
3570 section_order_index_(0)
3571 {
3572 gold_assert(shndx != OUTPUT_SECTION_CODE
3573 && shndx != MERGE_DATA_SECTION_CODE
3574 && shndx != MERGE_STRING_SECTION_CODE
3575 && shndx != RELAXED_INPUT_SECTION_CODE);
3576 this->u1_.data_size = data_size;
3577 this->u2_.object = object;
3578 }
3579
3580 // For a non-merge output section.
3581 Input_section(Output_section_data* posd)
3582 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3583 section_order_index_(0)
3584 {
3585 this->u1_.data_size = 0;
3586 this->u2_.posd = posd;
3587 }
3588
3589 // For a merge section.
3590 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3591 : shndx_(is_string
3592 ? MERGE_STRING_SECTION_CODE
3593 : MERGE_DATA_SECTION_CODE),
3594 p2align_(0),
3595 section_order_index_(0)
3596 {
3597 this->u1_.entsize = entsize;
3598 this->u2_.posd = posd;
3599 }
3600
3601 // For a relaxed input section.
3602 Input_section(Output_relaxed_input_section* psection)
3603 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3604 section_order_index_(0)
3605 {
3606 this->u1_.data_size = 0;
3607 this->u2_.poris = psection;
3608 }
3609
3610 unsigned int
3611 section_order_index() const
3612 {
3613 return this->section_order_index_;
3614 }
3615
3616 void
3617 set_section_order_index(unsigned int number)
3618 {
3619 this->section_order_index_ = number;
3620 }
3621
3622 // The required alignment.
3623 uint64_t
3624 addralign() const
3625 {
3626 if (this->p2align_ != 0)
3627 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3628 else if (!this->is_input_section())
3629 return this->u2_.posd->addralign();
3630 else
3631 return 0;
3632 }
3633
3634 // Set the required alignment, which must be either 0 or a power of 2.
3635 // For input sections that are sub-classes of Output_section_data, a
3636 // alignment of zero means asking the underlying object for alignment.
3637 void
3638 set_addralign(uint64_t addralign)
3639 {
3640 if (addralign == 0)
3641 this->p2align_ = 0;
3642 else
3643 {
3644 gold_assert((addralign & (addralign - 1)) == 0);
3645 this->p2align_ = ffsll(static_cast<long long>(addralign));
3646 }
3647 }
3648
3649 // Return the current required size, without finalization.
3650 off_t
3651 current_data_size() const;
3652
3653 // Return the required size.
3654 off_t
3655 data_size() const;
3656
3657 // Whether this is an input section.
3658 bool
3659 is_input_section() const
3660 {
3661 return (this->shndx_ != OUTPUT_SECTION_CODE
3662 && this->shndx_ != MERGE_DATA_SECTION_CODE
3663 && this->shndx_ != MERGE_STRING_SECTION_CODE
3664 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3665 }
3666
3667 // Return whether this is a merge section which matches the
3668 // parameters.
3669 bool
3670 is_merge_section(bool is_string, uint64_t entsize,
3671 uint64_t addralign) const
3672 {
3673 return (this->shndx_ == (is_string
3674 ? MERGE_STRING_SECTION_CODE
3675 : MERGE_DATA_SECTION_CODE)
3676 && this->u1_.entsize == entsize
3677 && this->addralign() == addralign);
3678 }
3679
3680 // Return whether this is a merge section for some input section.
3681 bool
3682 is_merge_section() const
3683 {
3684 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3685 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3686 }
3687
3688 // Return whether this is a relaxed input section.
3689 bool
3690 is_relaxed_input_section() const
3691 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3692
3693 // Return whether this is a generic Output_section_data.
3694 bool
3695 is_output_section_data() const
3696 {
3697 return this->shndx_ == OUTPUT_SECTION_CODE;
3698 }
3699
3700 // Return the object for an input section.
3701 Relobj*
3702 relobj() const;
3703
3704 // Return the input section index for an input section.
3705 unsigned int
3706 shndx() const;
3707
3708 // For non-input-sections, return the associated Output_section_data
3709 // object.
3710 Output_section_data*
3711 output_section_data() const
3712 {
3713 gold_assert(!this->is_input_section());
3714 return this->u2_.posd;
3715 }
3716
3717 // For a merge section, return the Output_merge_base pointer.
3718 Output_merge_base*
3719 output_merge_base() const
3720 {
3721 gold_assert(this->is_merge_section());
3722 return this->u2_.pomb;
3723 }
3724
3725 // Return the Output_relaxed_input_section object.
3726 Output_relaxed_input_section*
3727 relaxed_input_section() const
3728 {
3729 gold_assert(this->is_relaxed_input_section());
3730 return this->u2_.poris;
3731 }
3732
3733 // Set the output section.
3734 void
3735 set_output_section(Output_section* os)
3736 {
3737 gold_assert(!this->is_input_section());
3738 Output_section_data* posd =
3739 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3740 posd->set_output_section(os);
3741 }
3742
3743 // Set the address and file offset. This is called during
3744 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3745 // the enclosing section.
3746 void
3747 set_address_and_file_offset(uint64_t address, off_t file_offset,
3748 off_t section_file_offset);
3749
3750 // Reset the address and file offset.
3751 void
3752 reset_address_and_file_offset();
3753
3754 // Finalize the data size.
3755 void
3756 finalize_data_size();
3757
3758 // Add an input section, for SHF_MERGE sections.
3759 bool
3760 add_input_section(Relobj* object, unsigned int shndx)
3761 {
3762 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3763 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3764 return this->u2_.posd->add_input_section(object, shndx);
3765 }
3766
3767 // Given an input OBJECT, an input section index SHNDX within that
3768 // object, and an OFFSET relative to the start of that input
3769 // section, return whether or not the output offset is known. If
3770 // this function returns true, it sets *POUTPUT to the offset in
3771 // the output section, relative to the start of the input section
3772 // in the output section. *POUTPUT may be different from OFFSET
3773 // for a merged section.
3774 bool
3775 output_offset(const Relobj* object, unsigned int shndx,
3776 section_offset_type offset,
3777 section_offset_type* poutput) const;
3778
3779 // Write out the data. This does nothing for an input section.
3780 void
3781 write(Output_file*);
3782
3783 // Write the data to a buffer. This does nothing for an input
3784 // section.
3785 void
3786 write_to_buffer(unsigned char*);
3787
3788 // Print to a map file.
3789 void
3790 print_to_mapfile(Mapfile*) const;
3791
3792 // Print statistics about merge sections to stderr.
3793 void
3794 print_merge_stats(const char* section_name)
3795 {
3796 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3797 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3798 this->u2_.posd->print_merge_stats(section_name);
3799 }
3800
3801 private:
3802 // Code values which appear in shndx_. If the value is not one of
3803 // these codes, it is the input section index in the object file.
3804 enum
3805 {
3806 // An Output_section_data.
3807 OUTPUT_SECTION_CODE = -1U,
3808 // An Output_section_data for an SHF_MERGE section with
3809 // SHF_STRINGS not set.
3810 MERGE_DATA_SECTION_CODE = -2U,
3811 // An Output_section_data for an SHF_MERGE section with
3812 // SHF_STRINGS set.
3813 MERGE_STRING_SECTION_CODE = -3U,
3814 // An Output_section_data for a relaxed input section.
3815 RELAXED_INPUT_SECTION_CODE = -4U
3816 };
3817
3818 // For an ordinary input section, this is the section index in the
3819 // input file. For an Output_section_data, this is
3820 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3821 // MERGE_STRING_SECTION_CODE.
3822 unsigned int shndx_;
3823 // The required alignment, stored as a power of 2.
3824 unsigned int p2align_;
3825 union
3826 {
3827 // For an ordinary input section, the section size.
3828 off_t data_size;
3829 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3830 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3831 // entity size.
3832 uint64_t entsize;
3833 } u1_;
3834 union
3835 {
3836 // For an ordinary input section, the object which holds the
3837 // input section.
3838 Relobj* object;
3839 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3840 // MERGE_STRING_SECTION_CODE, the data.
3841 Output_section_data* posd;
3842 Output_merge_base* pomb;
3843 // For RELAXED_INPUT_SECTION_CODE, the data.
3844 Output_relaxed_input_section* poris;
3845 } u2_;
3846 // The line number of the pattern it matches in the --section-ordering-file
3847 // file. It is 0 if does not match any pattern.
3848 unsigned int section_order_index_;
3849 };
3850
3851 // Store the list of input sections for this Output_section into the
3852 // list passed in. This removes the input sections, leaving only
3853 // any Output_section_data elements. This returns the size of those
3854 // Output_section_data elements. ADDRESS is the address of this
3855 // output section. FILL is the fill value to use, in case there are
3856 // any spaces between the remaining Output_section_data elements.
3857 uint64_t
3858 get_input_sections(uint64_t address, const std::string& fill,
3859 std::list<Input_section>*);
3860
3861 // Add a script input section. A script input section can either be
3862 // a plain input section or a sub-class of Output_section_data.
3863 void
3864 add_script_input_section(const Input_section& input_section);
3865
3866 // Set the current size of the output section.
3867 void
3868 set_current_data_size(off_t size)
3869 { this->set_current_data_size_for_child(size); }
3870
3871 // End of linker script support.
3872
3873 // Save states before doing section layout.
3874 // This is used for relaxation.
3875 void
3876 save_states();
3877
3878 // Restore states prior to section layout.
3879 void
3880 restore_states();
3881
3882 // Discard states.
3883 void
3884 discard_states();
3885
3886 // Convert existing input sections to relaxed input sections.
3887 void
3888 convert_input_sections_to_relaxed_sections(
3889 const std::vector<Output_relaxed_input_section*>& sections);
3890
3891 // Find a relaxed input section to an input section in OBJECT
3892 // with index SHNDX. Return NULL if none is found.
3893 const Output_relaxed_input_section*
3894 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3895
3896 // Whether section offsets need adjustment due to relaxation.
3897 bool
3898 section_offsets_need_adjustment() const
3899 { return this->section_offsets_need_adjustment_; }
3900
3901 // Set section_offsets_need_adjustment to be true.
3902 void
3903 set_section_offsets_need_adjustment()
3904 { this->section_offsets_need_adjustment_ = true; }
3905
3906 // Set section_offsets_need_adjustment to be false.
3907 void
3908 clear_section_offsets_need_adjustment()
3909 { this->section_offsets_need_adjustment_ = false; }
3910
3911 // Adjust section offsets of input sections in this. This is
3912 // requires if relaxation caused some input sections to change sizes.
3913 void
3914 adjust_section_offsets();
3915
3916 // Whether this is a NOLOAD section.
3917 bool
3918 is_noload() const
3919 { return this->is_noload_; }
3920
3921 // Set NOLOAD flag.
3922 void
3923 set_is_noload()
3924 { this->is_noload_ = true; }
3925
3926 // Print merge statistics to stderr.
3927 void
3928 print_merge_stats();
3929
3930 // Set a fixed layout for the section. Used for incremental update links.
3931 void
3932 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3933 uint64_t sh_addralign);
3934
3935 // Return TRUE if the section has a fixed layout.
3936 bool
3937 has_fixed_layout() const
3938 { return this->has_fixed_layout_; }
3939
3940 // Set flag to allow patch space for this section. Used for full
3941 // incremental links.
3942 void
3943 set_is_patch_space_allowed()
3944 { this->is_patch_space_allowed_ = true; }
3945
3946 // Set a fill method to use for free space left in the output section
3947 // during incremental links.
3948 void
3949 set_free_space_fill(Output_fill* free_space_fill)
3950 {
3951 this->free_space_fill_ = free_space_fill;
3952 this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3953 }
3954
3955 // Reserve space within the fixed layout for the section. Used for
3956 // incremental update links.
3957 void
3958 reserve(uint64_t sh_offset, uint64_t sh_size);
3959
3960 // Allocate space from the free list for the section. Used for
3961 // incremental update links.
3962 off_t
3963 allocate(off_t len, uint64_t addralign);
3964
3965 typedef std::vector<Input_section> Input_section_list;
3966
3967 // Allow access to the input sections.
3968 const Input_section_list&
3969 input_sections() const
3970 { return this->input_sections_; }
3971
3972 Input_section_list&
3973 input_sections()
3974 { return this->input_sections_; }
3975
3976 protected:
3977 // Return the output section--i.e., the object itself.
3978 Output_section*
3979 do_output_section()
3980 { return this; }
3981
3982 const Output_section*
3983 do_output_section() const
3984 { return this; }
3985
3986 // Return the section index in the output file.
3987 unsigned int
3988 do_out_shndx() const
3989 {
3990 gold_assert(this->out_shndx_ != -1U);
3991 return this->out_shndx_;
3992 }
3993
3994 // Set the output section index.
3995 void
3996 do_set_out_shndx(unsigned int shndx)
3997 {
3998 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3999 this->out_shndx_ = shndx;
4000 }
4001
4002 // Update the data size of the Output_section. For a typical
4003 // Output_section, there is nothing to do, but if there are any
4004 // Output_section_data objects we need to do a trial layout
4005 // here.
4006 virtual void
4007 update_data_size();
4008
4009 // Set the final data size of the Output_section. For a typical
4010 // Output_section, there is nothing to do, but if there are any
4011 // Output_section_data objects we need to set their final addresses
4012 // here.
4013 virtual void
4014 set_final_data_size();
4015
4016 // Reset the address and file offset.
4017 void
4018 do_reset_address_and_file_offset();
4019
4020 // Return true if address and file offset already have reset values. In
4021 // other words, calling reset_address_and_file_offset will not change them.
4022 bool
4023 do_address_and_file_offset_have_reset_values() const;
4024
4025 // Write the data to the file. For a typical Output_section, this
4026 // does nothing: the data is written out by calling Object::Relocate
4027 // on each input object. But if there are any Output_section_data
4028 // objects we do need to write them out here.
4029 virtual void
4030 do_write(Output_file*);
4031
4032 // Return the address alignment--function required by parent class.
4033 uint64_t
4034 do_addralign() const
4035 { return this->addralign_; }
4036
4037 // Return whether there is a load address.
4038 bool
4039 do_has_load_address() const
4040 { return this->has_load_address_; }
4041
4042 // Return the load address.
4043 uint64_t
4044 do_load_address() const
4045 {
4046 gold_assert(this->has_load_address_);
4047 return this->load_address_;
4048 }
4049
4050 // Return whether this is an Output_section.
4051 bool
4052 do_is_section() const
4053 { return true; }
4054
4055 // Return whether this is a section of the specified type.
4056 bool
4057 do_is_section_type(elfcpp::Elf_Word type) const
4058 { return this->type_ == type; }
4059
4060 // Return whether the specified section flag is set.
4061 bool
4062 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
4063 { return (this->flags_ & flag) != 0; }
4064
4065 // Set the TLS offset. Called only for SHT_TLS sections.
4066 void
4067 do_set_tls_offset(uint64_t tls_base);
4068
4069 // Return the TLS offset, relative to the base of the TLS segment.
4070 // Valid only for SHT_TLS sections.
4071 uint64_t
4072 do_tls_offset() const
4073 { return this->tls_offset_; }
4074
4075 // This may be implemented by a child class.
4076 virtual void
4077 do_finalize_name(Layout*)
4078 { }
4079
4080 // Print to the map file.
4081 virtual void
4082 do_print_to_mapfile(Mapfile*) const;
4083
4084 // Record that this section requires postprocessing after all
4085 // relocations have been applied. This is called by a child class.
4086 void
4087 set_requires_postprocessing()
4088 {
4089 this->requires_postprocessing_ = true;
4090 this->after_input_sections_ = true;
4091 }
4092
4093 // Write all the data of an Output_section into the postprocessing
4094 // buffer.
4095 void
4096 write_to_postprocessing_buffer();
4097
4098 // Whether this always keeps an input section list
4099 bool
4100 always_keeps_input_sections() const
4101 { return this->always_keeps_input_sections_; }
4102
4103 // Always keep an input section list.
4104 void
4105 set_always_keeps_input_sections()
4106 {
4107 gold_assert(this->current_data_size_for_child() == 0);
4108 this->always_keeps_input_sections_ = true;
4109 }
4110
4111 private:
4112 // We only save enough information to undo the effects of section layout.
4113 class Checkpoint_output_section
4114 {
4115 public:
4116 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
4117 const Input_section_list& input_sections,
4118 off_t first_input_offset,
4119 bool attached_input_sections_are_sorted)
4120 : addralign_(addralign), flags_(flags),
4121 input_sections_(input_sections),
4122 input_sections_size_(input_sections_.size()),
4123 input_sections_copy_(), first_input_offset_(first_input_offset),
4124 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
4125 { }
4126
4127 virtual
4128 ~Checkpoint_output_section()
4129 { }
4130
4131 // Return the address alignment.
4132 uint64_t
4133 addralign() const
4134 { return this->addralign_; }
4135
4136 void
4137 set_addralign(uint64_t val)
4138 { this->addralign_ = val; }
4139
4140 // Return the section flags.
4141 elfcpp::Elf_Xword
4142 flags() const
4143 { return this->flags_; }
4144
4145 // Return a reference to the input section list copy.
4146 Input_section_list*
4147 input_sections()
4148 { return &this->input_sections_copy_; }
4149
4150 // Return the size of input_sections at the time when checkpoint is
4151 // taken.
4152 size_t
4153 input_sections_size() const
4154 { return this->input_sections_size_; }
4155
4156 // Whether input sections are copied.
4157 bool
4158 input_sections_saved() const
4159 { return this->input_sections_copy_.size() == this->input_sections_size_; }
4160
4161 off_t
4162 first_input_offset() const
4163 { return this->first_input_offset_; }
4164
4165 bool
4166 attached_input_sections_are_sorted() const
4167 { return this->attached_input_sections_are_sorted_; }
4168
4169 // Save input sections.
4170 void
4171 save_input_sections()
4172 {
4173 this->input_sections_copy_.reserve(this->input_sections_size_);
4174 this->input_sections_copy_.clear();
4175 Input_section_list::const_iterator p = this->input_sections_.begin();
4176 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
4177 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
4178 this->input_sections_copy_.push_back(*p);
4179 }
4180
4181 private:
4182 // The section alignment.
4183 uint64_t addralign_;
4184 // The section flags.
4185 elfcpp::Elf_Xword flags_;
4186 // Reference to the input sections to be checkpointed.
4187 const Input_section_list& input_sections_;
4188 // Size of the checkpointed portion of input_sections_;
4189 size_t input_sections_size_;
4190 // Copy of input sections.
4191 Input_section_list input_sections_copy_;
4192 // The offset of the first entry in input_sections_.
4193 off_t first_input_offset_;
4194 // True if the input sections attached to this output section have
4195 // already been sorted.
4196 bool attached_input_sections_are_sorted_;
4197 };
4198
4199 // This class is used to sort the input sections.
4200 class Input_section_sort_entry;
4201
4202 // This is the sort comparison function for ctors and dtors.
4203 struct Input_section_sort_compare
4204 {
4205 bool
4206 operator()(const Input_section_sort_entry&,
4207 const Input_section_sort_entry&) const;
4208 };
4209
4210 // This is the sort comparison function for .init_array and .fini_array.
4211 struct Input_section_sort_init_fini_compare
4212 {
4213 bool
4214 operator()(const Input_section_sort_entry&,
4215 const Input_section_sort_entry&) const;
4216 };
4217
4218 // This is the sort comparison function when a section order is specified
4219 // from an input file.
4220 struct Input_section_sort_section_order_index_compare
4221 {
4222 bool
4223 operator()(const Input_section_sort_entry&,
4224 const Input_section_sort_entry&) const;
4225 };
4226
4227 // This is the sort comparison function for .text to sort sections with
4228 // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4229 struct Input_section_sort_section_prefix_special_ordering_compare
4230 {
4231 bool
4232 operator()(const Input_section_sort_entry&,
4233 const Input_section_sort_entry&) const;
4234 };
4235
4236 // This is the sort comparison function for sorting sections by name.
4237 struct Input_section_sort_section_name_compare
4238 {
4239 bool
4240 operator()(const Input_section_sort_entry&,
4241 const Input_section_sort_entry&) const;
4242 };
4243
4244 // Fill data. This is used to fill in data between input sections.
4245 // It is also used for data statements (BYTE, WORD, etc.) in linker
4246 // scripts. When we have to keep track of the input sections, we
4247 // can use an Output_data_const, but we don't want to have to keep
4248 // track of input sections just to implement fills.
4249 class Fill
4250 {
4251 public:
4252 Fill(off_t section_offset, off_t length)
4253 : section_offset_(section_offset),
4254 length_(convert_to_section_size_type(length))
4255 { }
4256
4257 // Return section offset.
4258 off_t
4259 section_offset() const
4260 { return this->section_offset_; }
4261
4262 // Return fill length.
4263 section_size_type
4264 length() const
4265 { return this->length_; }
4266
4267 private:
4268 // The offset within the output section.
4269 off_t section_offset_;
4270 // The length of the space to fill.
4271 section_size_type length_;
4272 };
4273
4274 typedef std::vector<Fill> Fill_list;
4275
4276 // Map used during relaxation of existing sections. This map
4277 // a section id an input section list index. We assume that
4278 // Input_section_list is a vector.
4279 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4280
4281 // Add a new output section by Input_section.
4282 void
4283 add_output_section_data(Input_section*);
4284
4285 // Add an SHF_MERGE input section. Returns true if the section was
4286 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4287 // stores information about the merged input sections.
4288 bool
4289 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4290 uint64_t entsize, uint64_t addralign,
4291 bool keeps_input_sections);
4292
4293 // Add an output SHF_MERGE section POSD to this output section.
4294 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4295 // ENTSIZE is the entity size. This returns the entry added to
4296 // input_sections_.
4297 void
4298 add_output_merge_section(Output_section_data* posd, bool is_string,
4299 uint64_t entsize);
4300
4301 // Find the merge section into which an input section with index SHNDX in
4302 // OBJECT has been added. Return NULL if none found.
4303 const Output_section_data*
4304 find_merge_section(const Relobj* object, unsigned int shndx) const;
4305
4306 // Build a relaxation map.
4307 void
4308 build_relaxation_map(
4309 const Input_section_list& input_sections,
4310 size_t limit,
4311 Relaxation_map* map) const;
4312
4313 // Convert input sections in an input section list into relaxed sections.
4314 void
4315 convert_input_sections_in_list_to_relaxed_sections(
4316 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4317 const Relaxation_map& map,
4318 Input_section_list* input_sections);
4319
4320 // Build the lookup maps for merge and relaxed input sections.
4321 void
4322 build_lookup_maps() const;
4323
4324 // Most of these fields are only valid after layout.
4325
4326 // The name of the section. This will point into a Stringpool.
4327 const char* name_;
4328 // The section address is in the parent class.
4329 // The section alignment.
4330 uint64_t addralign_;
4331 // The section entry size.
4332 uint64_t entsize_;
4333 // The load address. This is only used when using a linker script
4334 // with a SECTIONS clause. The has_load_address_ field indicates
4335 // whether this field is valid.
4336 uint64_t load_address_;
4337 // The file offset is in the parent class.
4338 // Set the section link field to the index of this section.
4339 const Output_data* link_section_;
4340 // If link_section_ is NULL, this is the link field.
4341 unsigned int link_;
4342 // Set the section info field to the index of this section.
4343 const Output_section* info_section_;
4344 // If info_section_ is NULL, set the info field to the symbol table
4345 // index of this symbol.
4346 const Symbol* info_symndx_;
4347 // If info_section_ and info_symndx_ are NULL, this is the section
4348 // info field.
4349 unsigned int info_;
4350 // The section type.
4351 const elfcpp::Elf_Word type_;
4352 // The section flags.
4353 elfcpp::Elf_Xword flags_;
4354 // The order of this section in the output segment.
4355 Output_section_order order_;
4356 // The section index.
4357 unsigned int out_shndx_;
4358 // If there is a STT_SECTION for this output section in the normal
4359 // symbol table, this is the symbol index. This starts out as zero.
4360 // It is initialized in Layout::finalize() to be the index, or -1U
4361 // if there isn't one.
4362 unsigned int symtab_index_;
4363 // If there is a STT_SECTION for this output section in the dynamic
4364 // symbol table, this is the symbol index. This starts out as zero.
4365 // It is initialized in Layout::finalize() to be the index, or -1U
4366 // if there isn't one.
4367 unsigned int dynsym_index_;
4368 // The input sections. This will be empty in cases where we don't
4369 // need to keep track of them.
4370 Input_section_list input_sections_;
4371 // The offset of the first entry in input_sections_.
4372 off_t first_input_offset_;
4373 // The fill data. This is separate from input_sections_ because we
4374 // often will need fill sections without needing to keep track of
4375 // input sections.
4376 Fill_list fills_;
4377 // If the section requires postprocessing, this buffer holds the
4378 // section contents during relocation.
4379 unsigned char* postprocessing_buffer_;
4380 // Whether this output section needs a STT_SECTION symbol in the
4381 // normal symbol table. This will be true if there is a relocation
4382 // which needs it.
4383 bool needs_symtab_index_ : 1;
4384 // Whether this output section needs a STT_SECTION symbol in the
4385 // dynamic symbol table. This will be true if there is a dynamic
4386 // relocation which needs it.
4387 bool needs_dynsym_index_ : 1;
4388 // Whether the link field of this output section should point to the
4389 // normal symbol table.
4390 bool should_link_to_symtab_ : 1;
4391 // Whether the link field of this output section should point to the
4392 // dynamic symbol table.
4393 bool should_link_to_dynsym_ : 1;
4394 // Whether this section should be written after all the input
4395 // sections are complete.
4396 bool after_input_sections_ : 1;
4397 // Whether this section requires post processing after all
4398 // relocations have been applied.
4399 bool requires_postprocessing_ : 1;
4400 // Whether an input section was mapped to this output section
4401 // because of a SECTIONS clause in a linker script.
4402 bool found_in_sections_clause_ : 1;
4403 // Whether this section has an explicitly specified load address.
4404 bool has_load_address_ : 1;
4405 // True if the info_section_ field means the section index of the
4406 // section, false if it means the symbol index of the corresponding
4407 // section symbol.
4408 bool info_uses_section_index_ : 1;
4409 // True if input sections attached to this output section have to be
4410 // sorted according to a specified order.
4411 bool input_section_order_specified_ : 1;
4412 // True if the input sections attached to this output section may
4413 // need sorting.
4414 bool may_sort_attached_input_sections_ : 1;
4415 // True if the input sections attached to this output section must
4416 // be sorted.
4417 bool must_sort_attached_input_sections_ : 1;
4418 // True if the input sections attached to this output section have
4419 // already been sorted.
4420 bool attached_input_sections_are_sorted_ : 1;
4421 // True if this section holds relro data.
4422 bool is_relro_ : 1;
4423 // True if this is a small section.
4424 bool is_small_section_ : 1;
4425 // True if this is a large section.
4426 bool is_large_section_ : 1;
4427 // Whether code-fills are generated at write.
4428 bool generate_code_fills_at_write_ : 1;
4429 // Whether the entry size field should be zero.
4430 bool is_entsize_zero_ : 1;
4431 // Whether section offsets need adjustment due to relaxation.
4432 bool section_offsets_need_adjustment_ : 1;
4433 // Whether this is a NOLOAD section.
4434 bool is_noload_ : 1;
4435 // Whether this always keeps input section.
4436 bool always_keeps_input_sections_ : 1;
4437 // Whether this section has a fixed layout, for incremental update links.
4438 bool has_fixed_layout_ : 1;
4439 // True if we can add patch space to this section.
4440 bool is_patch_space_allowed_ : 1;
4441 // True if this output section goes into a unique segment.
4442 bool is_unique_segment_ : 1;
4443 // For SHT_TLS sections, the offset of this section relative to the base
4444 // of the TLS segment.
4445 uint64_t tls_offset_;
4446 // Additional segment flags, specified via linker plugin, when mapping some
4447 // input sections to unique segments.
4448 uint64_t extra_segment_flags_;
4449 // Segment alignment specified via linker plugin, when mapping some
4450 // input sections to unique segments.
4451 uint64_t segment_alignment_;
4452 // Saved checkpoint.
4453 Checkpoint_output_section* checkpoint_;
4454 // Fast lookup maps for merged and relaxed input sections.
4455 Output_section_lookup_maps* lookup_maps_;
4456 // List of available regions within the section, for incremental
4457 // update links.
4458 Free_list free_list_;
4459 // Method for filling chunks of free space.
4460 Output_fill* free_space_fill_;
4461 // Amount added as patch space for incremental linking.
4462 off_t patch_space_;
4463 };
4464
4465 // An output segment. PT_LOAD segments are built from collections of
4466 // output sections. Other segments typically point within PT_LOAD
4467 // segments, and are built directly as needed.
4468 //
4469 // NOTE: We want to use the copy constructor for this class. During
4470 // relaxation, we may try built the segments multiple times. We do
4471 // that by copying the original segment list before lay-out, doing
4472 // a trial lay-out and roll-back to the saved copied if we need to
4473 // to the lay-out again.
4474
4475 class Output_segment
4476 {
4477 public:
4478 // Create an output segment, specifying the type and flags.
4479 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4480
4481 // Return the virtual address.
4482 uint64_t
4483 vaddr() const
4484 { return this->vaddr_; }
4485
4486 // Return the physical address.
4487 uint64_t
4488 paddr() const
4489 { return this->paddr_; }
4490
4491 // Return the segment type.
4492 elfcpp::Elf_Word
4493 type() const
4494 { return this->type_; }
4495
4496 // Return the segment flags.
4497 elfcpp::Elf_Word
4498 flags() const
4499 { return this->flags_; }
4500
4501 // Return the memory size.
4502 uint64_t
4503 memsz() const
4504 { return this->memsz_; }
4505
4506 // Return the file size.
4507 off_t
4508 filesz() const
4509 { return this->filesz_; }
4510
4511 // Return the file offset.
4512 off_t
4513 offset() const
4514 { return this->offset_; }
4515
4516 // Whether this is a segment created to hold large data sections.
4517 bool
4518 is_large_data_segment() const
4519 { return this->is_large_data_segment_; }
4520
4521 // Record that this is a segment created to hold large data
4522 // sections.
4523 void
4524 set_is_large_data_segment()
4525 { this->is_large_data_segment_ = true; }
4526
4527 bool
4528 is_unique_segment() const
4529 { return this->is_unique_segment_; }
4530
4531 // Mark segment as unique, happens when linker plugins request that
4532 // certain input sections be mapped to unique segments.
4533 void
4534 set_is_unique_segment()
4535 { this->is_unique_segment_ = true; }
4536
4537 // Return the maximum alignment of the Output_data.
4538 uint64_t
4539 maximum_alignment();
4540
4541 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4542 // the segment flags to use.
4543 void
4544 add_output_section_to_load(Layout* layout, Output_section* os,
4545 elfcpp::Elf_Word seg_flags);
4546
4547 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4548 // is the segment flags to use.
4549 void
4550 add_output_section_to_nonload(Output_section* os,
4551 elfcpp::Elf_Word seg_flags);
4552
4553 // Remove an Output_section from this segment. It is an error if it
4554 // is not present.
4555 void
4556 remove_output_section(Output_section* os);
4557
4558 // Add an Output_data (which need not be an Output_section) to the
4559 // start of this segment.
4560 void
4561 add_initial_output_data(Output_data*);
4562
4563 // Return true if this segment has any sections which hold actual
4564 // data, rather than being a BSS section.
4565 bool
4566 has_any_data_sections() const;
4567
4568 // Whether this segment has a dynamic relocs.
4569 bool
4570 has_dynamic_reloc() const;
4571
4572 // Return the first section.
4573 Output_section*
4574 first_section() const;
4575
4576 // Return the address of the first section.
4577 uint64_t
4578 first_section_load_address() const
4579 {
4580 const Output_section* os = this->first_section();
4581 return os->has_load_address() ? os->load_address() : os->address();
4582 }
4583
4584 // Return whether the addresses have been set already.
4585 bool
4586 are_addresses_set() const
4587 { return this->are_addresses_set_; }
4588
4589 // Set the addresses.
4590 void
4591 set_addresses(uint64_t vaddr, uint64_t paddr)
4592 {
4593 this->vaddr_ = vaddr;
4594 this->paddr_ = paddr;
4595 this->are_addresses_set_ = true;
4596 }
4597
4598 // Update the flags for the flags of an output section added to this
4599 // segment.
4600 void
4601 update_flags_for_output_section(elfcpp::Elf_Xword flags)
4602 {
4603 // The ELF ABI specifies that a PT_TLS segment should always have
4604 // PF_R as the flags.
4605 if (this->type() != elfcpp::PT_TLS)
4606 this->flags_ |= flags;
4607 }
4608
4609 // Set the segment flags. This is only used if we have a PHDRS
4610 // clause which explicitly specifies the flags.
4611 void
4612 set_flags(elfcpp::Elf_Word flags)
4613 { this->flags_ = flags; }
4614
4615 // Set the address of the segment to ADDR and the offset to *POFF
4616 // and set the addresses and offsets of all contained output
4617 // sections accordingly. Set the section indexes of all contained
4618 // output sections starting with *PSHNDX. If RESET is true, first
4619 // reset the addresses of the contained sections. Return the
4620 // address of the immediately following segment. Update *POFF and
4621 // *PSHNDX. This should only be called for a PT_LOAD segment.
4622 uint64_t
4623 set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
4624 unsigned int* increase_relro, bool* has_relro,
4625 off_t* poff, unsigned int* pshndx);
4626
4627 // Set the minimum alignment of this segment. This may be adjusted
4628 // upward based on the section alignments.
4629 void
4630 set_minimum_p_align(uint64_t align)
4631 {
4632 if (align > this->min_p_align_)
4633 this->min_p_align_ = align;
4634 }
4635
4636 // Set the offset of this segment based on the section. This should
4637 // only be called for a non-PT_LOAD segment.
4638 void
4639 set_offset(unsigned int increase);
4640
4641 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4642 void
4643 set_tls_offsets();
4644
4645 // Return the number of output sections.
4646 unsigned int
4647 output_section_count() const;
4648
4649 // Return the section attached to the list segment with the lowest
4650 // load address. This is used when handling a PHDRS clause in a
4651 // linker script.
4652 Output_section*
4653 section_with_lowest_load_address() const;
4654
4655 // Write the segment header into *OPHDR.
4656 template<int size, bool big_endian>
4657 void
4658 write_header(elfcpp::Phdr_write<size, big_endian>*);
4659
4660 // Write the section headers of associated sections into V.
4661 template<int size, bool big_endian>
4662 unsigned char*
4663 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4664 unsigned int* pshndx) const;
4665
4666 // Print the output sections in the map file.
4667 void
4668 print_sections_to_mapfile(Mapfile*) const;
4669
4670 private:
4671 typedef std::vector<Output_data*> Output_data_list;
4672
4673 // Find the maximum alignment in an Output_data_list.
4674 static uint64_t
4675 maximum_alignment_list(const Output_data_list*);
4676
4677 // Return whether the first data section is a relro section.
4678 bool
4679 is_first_section_relro() const;
4680
4681 // Set the section addresses in an Output_data_list.
4682 uint64_t
4683 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4684 uint64_t addr, off_t* poff, unsigned int* pshndx,
4685 bool* in_tls);
4686
4687 // Return the number of Output_sections in an Output_data_list.
4688 unsigned int
4689 output_section_count_list(const Output_data_list*) const;
4690
4691 // Return whether an Output_data_list has a dynamic reloc.
4692 bool
4693 has_dynamic_reloc_list(const Output_data_list*) const;
4694
4695 // Find the section with the lowest load address in an
4696 // Output_data_list.
4697 void
4698 lowest_load_address_in_list(const Output_data_list* pdl,
4699 Output_section** found,
4700 uint64_t* found_lma) const;
4701
4702 // Find the first and last entries by address.
4703 void
4704 find_first_and_last_list(const Output_data_list* pdl,
4705 const Output_data** pfirst,
4706 const Output_data** plast) const;
4707
4708 // Write the section headers in the list into V.
4709 template<int size, bool big_endian>
4710 unsigned char*
4711 write_section_headers_list(const Layout*, const Stringpool*,
4712 const Output_data_list*, unsigned char* v,
4713 unsigned int* pshdx) const;
4714
4715 // Print a section list to the mapfile.
4716 void
4717 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4718
4719 // NOTE: We want to use the copy constructor. Currently, shallow copy
4720 // works for us so we do not need to write our own copy constructor.
4721
4722 // The list of output data attached to this segment.
4723 Output_data_list output_lists_[ORDER_MAX];
4724 // The segment virtual address.
4725 uint64_t vaddr_;
4726 // The segment physical address.
4727 uint64_t paddr_;
4728 // The size of the segment in memory.
4729 uint64_t memsz_;
4730 // The maximum section alignment. The is_max_align_known_ field
4731 // indicates whether this has been finalized.
4732 uint64_t max_align_;
4733 // The required minimum value for the p_align field. This is used
4734 // for PT_LOAD segments. Note that this does not mean that
4735 // addresses should be aligned to this value; it means the p_paddr
4736 // and p_vaddr fields must be congruent modulo this value. For
4737 // non-PT_LOAD segments, the dynamic linker works more efficiently
4738 // if the p_align field has the more conventional value, although it
4739 // can align as needed.
4740 uint64_t min_p_align_;
4741 // The offset of the segment data within the file.
4742 off_t offset_;
4743 // The size of the segment data in the file.
4744 off_t filesz_;
4745 // The segment type;
4746 elfcpp::Elf_Word type_;
4747 // The segment flags.
4748 elfcpp::Elf_Word flags_;
4749 // Whether we have finalized max_align_.
4750 bool is_max_align_known_ : 1;
4751 // Whether vaddr and paddr were set by a linker script.
4752 bool are_addresses_set_ : 1;
4753 // Whether this segment holds large data sections.
4754 bool is_large_data_segment_ : 1;
4755 // Whether this was marked as a unique segment via a linker plugin.
4756 bool is_unique_segment_ : 1;
4757 };
4758
4759 // This class represents the output file.
4760
4761 class Output_file
4762 {
4763 public:
4764 Output_file(const char* name);
4765
4766 // Indicate that this is a temporary file which should not be
4767 // output.
4768 void
4769 set_is_temporary()
4770 { this->is_temporary_ = true; }
4771
4772 // Try to open an existing file. Returns false if the file doesn't
4773 // exist, has a size of 0 or can't be mmaped. This method is
4774 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4775 // that file as the base for incremental linking.
4776 bool
4777 open_base_file(const char* base_name, bool writable);
4778
4779 // Open the output file. FILE_SIZE is the final size of the file.
4780 // If the file already exists, it is deleted/truncated. This method
4781 // is thread-unsafe.
4782 void
4783 open(off_t file_size);
4784
4785 // Resize the output file. This method is thread-unsafe.
4786 void
4787 resize(off_t file_size);
4788
4789 // Close the output file (flushing all buffered data) and make sure
4790 // there are no errors. This method is thread-unsafe.
4791 void
4792 close();
4793
4794 // Return the size of this file.
4795 off_t
4796 filesize()
4797 { return this->file_size_; }
4798
4799 // Return the name of this file.
4800 const char*
4801 filename()
4802 { return this->name_; }
4803
4804 // We currently always use mmap which makes the view handling quite
4805 // simple. In the future we may support other approaches.
4806
4807 // Write data to the output file.
4808 void
4809 write(off_t offset, const void* data, size_t len)
4810 { memcpy(this->base_ + offset, data, len); }
4811
4812 // Get a buffer to use to write to the file, given the offset into
4813 // the file and the size.
4814 unsigned char*
4815 get_output_view(off_t start, size_t size)
4816 {
4817 gold_assert(start >= 0
4818 && start + static_cast<off_t>(size) <= this->file_size_);
4819 return this->base_ + start;
4820 }
4821
4822 // VIEW must have been returned by get_output_view. Write the
4823 // buffer to the file, passing in the offset and the size.
4824 void
4825 write_output_view(off_t, size_t, unsigned char*)
4826 { }
4827
4828 // Get a read/write buffer. This is used when we want to write part
4829 // of the file, read it in, and write it again.
4830 unsigned char*
4831 get_input_output_view(off_t start, size_t size)
4832 { return this->get_output_view(start, size); }
4833
4834 // Write a read/write buffer back to the file.
4835 void
4836 write_input_output_view(off_t, size_t, unsigned char*)
4837 { }
4838
4839 // Get a read buffer. This is used when we just want to read part
4840 // of the file back it in.
4841 const unsigned char*
4842 get_input_view(off_t start, size_t size)
4843 { return this->get_output_view(start, size); }
4844
4845 // Release a read bfufer.
4846 void
4847 free_input_view(off_t, size_t, const unsigned char*)
4848 { }
4849
4850 private:
4851 // Map the file into memory or, if that fails, allocate anonymous
4852 // memory.
4853 void
4854 map();
4855
4856 // Allocate anonymous memory for the file.
4857 bool
4858 map_anonymous();
4859
4860 // Map the file into memory.
4861 bool
4862 map_no_anonymous(bool);
4863
4864 // Unmap the file from memory (and flush to disk buffers).
4865 void
4866 unmap();
4867
4868 // File name.
4869 const char* name_;
4870 // File descriptor.
4871 int o_;
4872 // File size.
4873 off_t file_size_;
4874 // Base of file mapped into memory.
4875 unsigned char* base_;
4876 // True iff base_ points to a memory buffer rather than an output file.
4877 bool map_is_anonymous_;
4878 // True if base_ was allocated using new rather than mmap.
4879 bool map_is_allocated_;
4880 // True if this is a temporary file which should not be output.
4881 bool is_temporary_;
4882 };
4883
4884 } // End namespace gold.
4885
4886 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.129213 seconds and 5 git commands to generate.