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