]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/output.h
* output.h (Output_data_got::add_constant): Tidy.
[thirdparty/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 { return this->add_got_entry(Got_entry(constant)); }
2384
2385 // Add a pair of constants to the GOT. This returns the offset of
2386 // the new entry from the start of the GOT.
2387 unsigned int
2388 add_constant_pair(Valtype c1, Valtype c2)
2389 { return this->add_got_entry_pair(Got_entry(c1), Got_entry(c2)); }
2390
2391 // Replace GOT entry I with a new constant.
2392 void
2393 replace_constant(unsigned int i, Valtype constant)
2394 {
2395 this->replace_got_entry(i, Got_entry(constant));
2396 }
2397
2398 // Reserve a slot in the GOT for a local symbol.
2399 void
2400 reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2401 unsigned int got_type);
2402
2403 // Reserve a slot in the GOT for a global symbol.
2404 void
2405 reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2406
2407 protected:
2408 // Write out the GOT table.
2409 void
2410 do_write(Output_file*);
2411
2412 // Write to a map file.
2413 void
2414 do_print_to_mapfile(Mapfile* mapfile) const
2415 { mapfile->print_output_data(this, _("** GOT")); }
2416
2417 // Reserve the slot at index I in the GOT.
2418 virtual void
2419 do_reserve_slot(unsigned int i)
2420 { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2421
2422 // Return the number of words in the GOT.
2423 unsigned int
2424 num_entries () const
2425 { return this->entries_.size(); }
2426
2427 // Return the offset into the GOT of GOT entry I.
2428 unsigned int
2429 got_offset(unsigned int i) const
2430 { return i * (got_size / 8); }
2431
2432 private:
2433 // This POD class holds a single GOT entry.
2434 class Got_entry
2435 {
2436 public:
2437 // Create a zero entry.
2438 Got_entry()
2439 : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false)
2440 { this->u_.constant = 0; }
2441
2442 // Create a global symbol entry.
2443 Got_entry(Symbol* gsym, bool use_plt_or_tls_offset)
2444 : local_sym_index_(GSYM_CODE),
2445 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2446 { this->u_.gsym = gsym; }
2447
2448 // Create a local symbol entry.
2449 Got_entry(Relobj* object, unsigned int local_sym_index,
2450 bool use_plt_or_tls_offset)
2451 : local_sym_index_(local_sym_index),
2452 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2453 {
2454 gold_assert(local_sym_index != GSYM_CODE
2455 && local_sym_index != CONSTANT_CODE
2456 && local_sym_index != RESERVED_CODE
2457 && local_sym_index == this->local_sym_index_);
2458 this->u_.object = object;
2459 }
2460
2461 // Create a constant entry. The constant is a host value--it will
2462 // be swapped, if necessary, when it is written out.
2463 explicit Got_entry(Valtype constant)
2464 : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
2465 { this->u_.constant = constant; }
2466
2467 // Write the GOT entry to an output view.
2468 void
2469 write(unsigned int got_indx, unsigned char* pov) const;
2470
2471 private:
2472 enum
2473 {
2474 GSYM_CODE = 0x7fffffff,
2475 CONSTANT_CODE = 0x7ffffffe,
2476 RESERVED_CODE = 0x7ffffffd
2477 };
2478
2479 union
2480 {
2481 // For a local symbol, the object.
2482 Relobj* object;
2483 // For a global symbol, the symbol.
2484 Symbol* gsym;
2485 // For a constant, the constant.
2486 Valtype constant;
2487 } u_;
2488 // For a local symbol, the local symbol index. This is GSYM_CODE
2489 // for a global symbol, or CONSTANT_CODE for a constant.
2490 unsigned int local_sym_index_ : 31;
2491 // Whether to use the PLT offset of the symbol if it has one.
2492 // For TLS symbols, whether to offset the symbol value.
2493 bool use_plt_or_tls_offset_ : 1;
2494 };
2495
2496 typedef std::vector<Got_entry> Got_entries;
2497
2498 // Create a new GOT entry and return its offset.
2499 unsigned int
2500 add_got_entry(Got_entry got_entry);
2501
2502 // Create a pair of new GOT entries and return the offset of the first.
2503 unsigned int
2504 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2505
2506 // Replace GOT entry I with a new value.
2507 void
2508 replace_got_entry(unsigned int i, Got_entry got_entry);
2509
2510 // Return the offset into the GOT of the last entry added.
2511 unsigned int
2512 last_got_offset() const
2513 { return this->got_offset(this->num_entries() - 1); }
2514
2515 // Set the size of the section.
2516 void
2517 set_got_size()
2518 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2519
2520 // The list of GOT entries.
2521 Got_entries entries_;
2522
2523 // List of available regions within the section, for incremental
2524 // update links.
2525 Free_list free_list_;
2526 };
2527
2528 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2529 // section.
2530
2531 class Output_data_dynamic : public Output_section_data
2532 {
2533 public:
2534 Output_data_dynamic(Stringpool* pool)
2535 : Output_section_data(Output_data::default_alignment()),
2536 entries_(), pool_(pool)
2537 { }
2538
2539 // Add a new dynamic entry with a fixed numeric value.
2540 void
2541 add_constant(elfcpp::DT tag, unsigned int val)
2542 { this->add_entry(Dynamic_entry(tag, val)); }
2543
2544 // Add a new dynamic entry with the address of output data.
2545 void
2546 add_section_address(elfcpp::DT tag, const Output_data* od)
2547 { this->add_entry(Dynamic_entry(tag, od, false)); }
2548
2549 // Add a new dynamic entry with the address of output data
2550 // plus a constant offset.
2551 void
2552 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2553 unsigned int offset)
2554 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2555
2556 // Add a new dynamic entry with the size of output data.
2557 void
2558 add_section_size(elfcpp::DT tag, const Output_data* od)
2559 { this->add_entry(Dynamic_entry(tag, od, true)); }
2560
2561 // Add a new dynamic entry with the total size of two output datas.
2562 void
2563 add_section_size(elfcpp::DT tag, const Output_data* od,
2564 const Output_data* od2)
2565 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2566
2567 // Add a new dynamic entry with the address of a symbol.
2568 void
2569 add_symbol(elfcpp::DT tag, const Symbol* sym)
2570 { this->add_entry(Dynamic_entry(tag, sym)); }
2571
2572 // Add a new dynamic entry with a string.
2573 void
2574 add_string(elfcpp::DT tag, const char* str)
2575 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2576
2577 void
2578 add_string(elfcpp::DT tag, const std::string& str)
2579 { this->add_string(tag, str.c_str()); }
2580
2581 protected:
2582 // Adjust the output section to set the entry size.
2583 void
2584 do_adjust_output_section(Output_section*);
2585
2586 // Set the final data size.
2587 void
2588 set_final_data_size();
2589
2590 // Write out the dynamic entries.
2591 void
2592 do_write(Output_file*);
2593
2594 // Write to a map file.
2595 void
2596 do_print_to_mapfile(Mapfile* mapfile) const
2597 { mapfile->print_output_data(this, _("** dynamic")); }
2598
2599 private:
2600 // This POD class holds a single dynamic entry.
2601 class Dynamic_entry
2602 {
2603 public:
2604 // Create an entry with a fixed numeric value.
2605 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2606 : tag_(tag), offset_(DYNAMIC_NUMBER)
2607 { this->u_.val = val; }
2608
2609 // Create an entry with the size or address of a section.
2610 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2611 : tag_(tag),
2612 offset_(section_size
2613 ? DYNAMIC_SECTION_SIZE
2614 : DYNAMIC_SECTION_ADDRESS)
2615 {
2616 this->u_.od = od;
2617 this->od2 = NULL;
2618 }
2619
2620 // Create an entry with the size of two sections.
2621 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2622 : tag_(tag),
2623 offset_(DYNAMIC_SECTION_SIZE)
2624 {
2625 this->u_.od = od;
2626 this->od2 = od2;
2627 }
2628
2629 // Create an entry with the address of a section plus a constant offset.
2630 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2631 : tag_(tag),
2632 offset_(offset)
2633 { this->u_.od = od; }
2634
2635 // Create an entry with the address of a symbol.
2636 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2637 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2638 { this->u_.sym = sym; }
2639
2640 // Create an entry with a string.
2641 Dynamic_entry(elfcpp::DT tag, const char* str)
2642 : tag_(tag), offset_(DYNAMIC_STRING)
2643 { this->u_.str = str; }
2644
2645 // Return the tag of this entry.
2646 elfcpp::DT
2647 tag() const
2648 { return this->tag_; }
2649
2650 // Write the dynamic entry to an output view.
2651 template<int size, bool big_endian>
2652 void
2653 write(unsigned char* pov, const Stringpool*) const;
2654
2655 private:
2656 // Classification is encoded in the OFFSET field.
2657 enum Classification
2658 {
2659 // Section address.
2660 DYNAMIC_SECTION_ADDRESS = 0,
2661 // Number.
2662 DYNAMIC_NUMBER = -1U,
2663 // Section size.
2664 DYNAMIC_SECTION_SIZE = -2U,
2665 // Symbol adress.
2666 DYNAMIC_SYMBOL = -3U,
2667 // String.
2668 DYNAMIC_STRING = -4U
2669 // Any other value indicates a section address plus OFFSET.
2670 };
2671
2672 union
2673 {
2674 // For DYNAMIC_NUMBER.
2675 unsigned int val;
2676 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2677 const Output_data* od;
2678 // For DYNAMIC_SYMBOL.
2679 const Symbol* sym;
2680 // For DYNAMIC_STRING.
2681 const char* str;
2682 } u_;
2683 // For DYNAMIC_SYMBOL with two sections.
2684 const Output_data* od2;
2685 // The dynamic tag.
2686 elfcpp::DT tag_;
2687 // The type of entry (Classification) or offset within a section.
2688 unsigned int offset_;
2689 };
2690
2691 // Add an entry to the list.
2692 void
2693 add_entry(const Dynamic_entry& entry)
2694 { this->entries_.push_back(entry); }
2695
2696 // Sized version of write function.
2697 template<int size, bool big_endian>
2698 void
2699 sized_write(Output_file* of);
2700
2701 // The type of the list of entries.
2702 typedef std::vector<Dynamic_entry> Dynamic_entries;
2703
2704 // The entries.
2705 Dynamic_entries entries_;
2706 // The pool used for strings.
2707 Stringpool* pool_;
2708 };
2709
2710 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2711 // which may be required if the object file has more than
2712 // SHN_LORESERVE sections.
2713
2714 class Output_symtab_xindex : public Output_section_data
2715 {
2716 public:
2717 Output_symtab_xindex(size_t symcount)
2718 : Output_section_data(symcount * 4, 4, true),
2719 entries_()
2720 { }
2721
2722 // Add an entry: symbol number SYMNDX has section SHNDX.
2723 void
2724 add(unsigned int symndx, unsigned int shndx)
2725 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2726
2727 protected:
2728 void
2729 do_write(Output_file*);
2730
2731 // Write to a map file.
2732 void
2733 do_print_to_mapfile(Mapfile* mapfile) const
2734 { mapfile->print_output_data(this, _("** symtab xindex")); }
2735
2736 private:
2737 template<bool big_endian>
2738 void
2739 endian_do_write(unsigned char*);
2740
2741 // It is likely that most symbols will not require entries. Rather
2742 // than keep a vector for all symbols, we keep pairs of symbol index
2743 // and section index.
2744 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2745
2746 // The entries we need.
2747 Xindex_entries entries_;
2748 };
2749
2750 // A relaxed input section.
2751 class Output_relaxed_input_section : public Output_section_data_build
2752 {
2753 public:
2754 // We would like to call relobj->section_addralign(shndx) to get the
2755 // alignment but we do not want the constructor to fail. So callers
2756 // are repsonsible for ensuring that.
2757 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2758 uint64_t addralign)
2759 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2760 { }
2761
2762 // Return the Relobj of this relaxed input section.
2763 Relobj*
2764 relobj() const
2765 { return this->relobj_; }
2766
2767 // Return the section index of this relaxed input section.
2768 unsigned int
2769 shndx() const
2770 { return this->shndx_; }
2771
2772 protected:
2773 void
2774 set_relobj(Relobj* relobj)
2775 { this->relobj_ = relobj; }
2776
2777 void
2778 set_shndx(unsigned int shndx)
2779 { this->shndx_ = shndx; }
2780
2781 private:
2782 Relobj* relobj_;
2783 unsigned int shndx_;
2784 };
2785
2786 // This class describes properties of merge data sections. It is used
2787 // as a key type for maps.
2788 class Merge_section_properties
2789 {
2790 public:
2791 Merge_section_properties(bool is_string, uint64_t entsize,
2792 uint64_t addralign)
2793 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2794 { }
2795
2796 // Whether this equals to another Merge_section_properties MSP.
2797 bool
2798 eq(const Merge_section_properties& msp) const
2799 {
2800 return ((this->is_string_ == msp.is_string_)
2801 && (this->entsize_ == msp.entsize_)
2802 && (this->addralign_ == msp.addralign_));
2803 }
2804
2805 // Compute a hash value for this using 64-bit FNV-1a hash.
2806 size_t
2807 hash_value() const
2808 {
2809 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2810 uint64_t prime = 1099511628211ULL;
2811 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2812 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2813 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2814 return h;
2815 }
2816
2817 // Functors for associative containers.
2818 struct equal_to
2819 {
2820 bool
2821 operator()(const Merge_section_properties& msp1,
2822 const Merge_section_properties& msp2) const
2823 { return msp1.eq(msp2); }
2824 };
2825
2826 struct hash
2827 {
2828 size_t
2829 operator()(const Merge_section_properties& msp) const
2830 { return msp.hash_value(); }
2831 };
2832
2833 private:
2834 // Whether this merge data section is for strings.
2835 bool is_string_;
2836 // Entsize of this merge data section.
2837 uint64_t entsize_;
2838 // Address alignment.
2839 uint64_t addralign_;
2840 };
2841
2842 // This class is used to speed up look up of special input sections in an
2843 // Output_section.
2844
2845 class Output_section_lookup_maps
2846 {
2847 public:
2848 Output_section_lookup_maps()
2849 : is_valid_(true), merge_sections_by_properties_(),
2850 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2851 { }
2852
2853 // Whether the maps are valid.
2854 bool
2855 is_valid() const
2856 { return this->is_valid_; }
2857
2858 // Invalidate the maps.
2859 void
2860 invalidate()
2861 { this->is_valid_ = false; }
2862
2863 // Clear the maps.
2864 void
2865 clear()
2866 {
2867 this->merge_sections_by_properties_.clear();
2868 this->merge_sections_by_id_.clear();
2869 this->relaxed_input_sections_by_id_.clear();
2870 // A cleared map is valid.
2871 this->is_valid_ = true;
2872 }
2873
2874 // Find a merge section by merge section properties. Return NULL if none
2875 // is found.
2876 Output_merge_base*
2877 find_merge_section(const Merge_section_properties& msp) const
2878 {
2879 gold_assert(this->is_valid_);
2880 Merge_sections_by_properties::const_iterator p =
2881 this->merge_sections_by_properties_.find(msp);
2882 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2883 }
2884
2885 // Find a merge section by section ID of a merge input section. Return NULL
2886 // if none is found.
2887 Output_merge_base*
2888 find_merge_section(const Object* object, unsigned int shndx) const
2889 {
2890 gold_assert(this->is_valid_);
2891 Merge_sections_by_id::const_iterator p =
2892 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2893 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2894 }
2895
2896 // Add a merge section pointed by POMB with properties MSP.
2897 void
2898 add_merge_section(const Merge_section_properties& msp,
2899 Output_merge_base* pomb)
2900 {
2901 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2902 std::pair<Merge_sections_by_properties::iterator, bool> result =
2903 this->merge_sections_by_properties_.insert(value);
2904 gold_assert(result.second);
2905 }
2906
2907 // Add a mapping from a merged input section in OBJECT with index SHNDX
2908 // to a merge output section pointed by POMB.
2909 void
2910 add_merge_input_section(const Object* object, unsigned int shndx,
2911 Output_merge_base* pomb)
2912 {
2913 Const_section_id csid(object, shndx);
2914 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2915 std::pair<Merge_sections_by_id::iterator, bool> result =
2916 this->merge_sections_by_id_.insert(value);
2917 gold_assert(result.second);
2918 }
2919
2920 // Find a relaxed input section of OBJECT with index SHNDX.
2921 Output_relaxed_input_section*
2922 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2923 {
2924 gold_assert(this->is_valid_);
2925 Relaxed_input_sections_by_id::const_iterator p =
2926 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2927 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2928 }
2929
2930 // Add a relaxed input section pointed by POMB and whose original input
2931 // section is in OBJECT with index SHNDX.
2932 void
2933 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2934 Output_relaxed_input_section* poris)
2935 {
2936 Const_section_id csid(relobj, shndx);
2937 std::pair<Const_section_id, Output_relaxed_input_section*>
2938 value(csid, poris);
2939 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2940 this->relaxed_input_sections_by_id_.insert(value);
2941 gold_assert(result.second);
2942 }
2943
2944 private:
2945 typedef Unordered_map<Const_section_id, Output_merge_base*,
2946 Const_section_id_hash>
2947 Merge_sections_by_id;
2948
2949 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2950 Merge_section_properties::hash,
2951 Merge_section_properties::equal_to>
2952 Merge_sections_by_properties;
2953
2954 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2955 Const_section_id_hash>
2956 Relaxed_input_sections_by_id;
2957
2958 // Whether this is valid
2959 bool is_valid_;
2960 // Merge sections by merge section properties.
2961 Merge_sections_by_properties merge_sections_by_properties_;
2962 // Merge sections by section IDs.
2963 Merge_sections_by_id merge_sections_by_id_;
2964 // Relaxed sections by section IDs.
2965 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2966 };
2967
2968 // This abstract base class defines the interface for the
2969 // types of methods used to fill free space left in an output
2970 // section during an incremental link. These methods are used
2971 // to insert dummy compilation units into debug info so that
2972 // debug info consumers can scan the debug info serially.
2973
2974 class Output_fill
2975 {
2976 public:
2977 Output_fill()
2978 : is_big_endian_(parameters->target().is_big_endian())
2979 { }
2980
2981 virtual
2982 ~Output_fill()
2983 { }
2984
2985 // Return the smallest size chunk of free space that can be
2986 // filled with a dummy compilation unit.
2987 size_t
2988 minimum_hole_size() const
2989 { return this->do_minimum_hole_size(); }
2990
2991 // Write a fill pattern of length LEN at offset OFF in the file.
2992 void
2993 write(Output_file* of, off_t off, size_t len) const
2994 { this->do_write(of, off, len); }
2995
2996 protected:
2997 virtual size_t
2998 do_minimum_hole_size() const = 0;
2999
3000 virtual void
3001 do_write(Output_file* of, off_t off, size_t len) const = 0;
3002
3003 bool
3004 is_big_endian() const
3005 { return this->is_big_endian_; }
3006
3007 private:
3008 bool is_big_endian_;
3009 };
3010
3011 // Fill method that introduces a dummy compilation unit in
3012 // a .debug_info or .debug_types section.
3013
3014 class Output_fill_debug_info : public Output_fill
3015 {
3016 public:
3017 Output_fill_debug_info(bool is_debug_types)
3018 : is_debug_types_(is_debug_types)
3019 { }
3020
3021 protected:
3022 virtual size_t
3023 do_minimum_hole_size() const;
3024
3025 virtual void
3026 do_write(Output_file* of, off_t off, size_t len) const;
3027
3028 private:
3029 // Version of the header.
3030 static const int version = 4;
3031 // True if this is a .debug_types section.
3032 bool is_debug_types_;
3033 };
3034
3035 // Fill method that introduces a dummy compilation unit in
3036 // a .debug_line section.
3037
3038 class Output_fill_debug_line : public Output_fill
3039 {
3040 public:
3041 Output_fill_debug_line()
3042 { }
3043
3044 protected:
3045 virtual size_t
3046 do_minimum_hole_size() const;
3047
3048 virtual void
3049 do_write(Output_file* of, off_t off, size_t len) const;
3050
3051 private:
3052 // Version of the header. We write a DWARF-3 header because it's smaller
3053 // and many tools have not yet been updated to understand the DWARF-4 header.
3054 static const int version = 3;
3055 // Length of the portion of the header that follows the header_length
3056 // field. This includes the following fields:
3057 // minimum_instruction_length, default_is_stmt, line_base, line_range,
3058 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3059 // The standard_opcode_lengths array is 12 bytes long, and the
3060 // include_directories and filenames fields each contain only a single
3061 // null byte.
3062 static const size_t header_length = 19;
3063 };
3064
3065 // An output section. We don't expect to have too many output
3066 // sections, so we don't bother to do a template on the size.
3067
3068 class Output_section : public Output_data
3069 {
3070 public:
3071 // Create an output section, giving the name, type, and flags.
3072 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
3073 virtual ~Output_section();
3074
3075 // Add a new input section SHNDX, named NAME, with header SHDR, from
3076 // object OBJECT. RELOC_SHNDX is the index of a relocation section
3077 // which applies to this section, or 0 if none, or -1 if more than
3078 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3079 // in a linker script; in that case we need to keep track of input
3080 // sections associated with an output section. Return the offset
3081 // within the output section.
3082 template<int size, bool big_endian>
3083 off_t
3084 add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
3085 unsigned int shndx, const char* name,
3086 const elfcpp::Shdr<size, big_endian>& shdr,
3087 unsigned int reloc_shndx, bool have_sections_script);
3088
3089 // Add generated data POSD to this output section.
3090 void
3091 add_output_section_data(Output_section_data* posd);
3092
3093 // Add a relaxed input section PORIS called NAME to this output section
3094 // with LAYOUT.
3095 void
3096 add_relaxed_input_section(Layout* layout,
3097 Output_relaxed_input_section* poris,
3098 const std::string& name);
3099
3100 // Return the section name.
3101 const char*
3102 name() const
3103 { return this->name_; }
3104
3105 // Return the section type.
3106 elfcpp::Elf_Word
3107 type() const
3108 { return this->type_; }
3109
3110 // Return the section flags.
3111 elfcpp::Elf_Xword
3112 flags() const
3113 { return this->flags_; }
3114
3115 typedef std::map<Section_id, unsigned int> Section_layout_order;
3116
3117 void
3118 update_section_layout(const Section_layout_order* order_map);
3119
3120 // Update the output section flags based on input section flags.
3121 void
3122 update_flags_for_input_section(elfcpp::Elf_Xword flags);
3123
3124 // Return the entsize field.
3125 uint64_t
3126 entsize() const
3127 { return this->entsize_; }
3128
3129 // Set the entsize field.
3130 void
3131 set_entsize(uint64_t v);
3132
3133 // Set the load address.
3134 void
3135 set_load_address(uint64_t load_address)
3136 {
3137 this->load_address_ = load_address;
3138 this->has_load_address_ = true;
3139 }
3140
3141 // Set the link field to the output section index of a section.
3142 void
3143 set_link_section(const Output_data* od)
3144 {
3145 gold_assert(this->link_ == 0
3146 && !this->should_link_to_symtab_
3147 && !this->should_link_to_dynsym_);
3148 this->link_section_ = od;
3149 }
3150
3151 // Set the link field to a constant.
3152 void
3153 set_link(unsigned int v)
3154 {
3155 gold_assert(this->link_section_ == NULL
3156 && !this->should_link_to_symtab_
3157 && !this->should_link_to_dynsym_);
3158 this->link_ = v;
3159 }
3160
3161 // Record that this section should link to the normal symbol table.
3162 void
3163 set_should_link_to_symtab()
3164 {
3165 gold_assert(this->link_section_ == NULL
3166 && this->link_ == 0
3167 && !this->should_link_to_dynsym_);
3168 this->should_link_to_symtab_ = true;
3169 }
3170
3171 // Record that this section should link to the dynamic symbol table.
3172 void
3173 set_should_link_to_dynsym()
3174 {
3175 gold_assert(this->link_section_ == NULL
3176 && this->link_ == 0
3177 && !this->should_link_to_symtab_);
3178 this->should_link_to_dynsym_ = true;
3179 }
3180
3181 // Return the info field.
3182 unsigned int
3183 info() const
3184 {
3185 gold_assert(this->info_section_ == NULL
3186 && this->info_symndx_ == NULL);
3187 return this->info_;
3188 }
3189
3190 // Set the info field to the output section index of a section.
3191 void
3192 set_info_section(const Output_section* os)
3193 {
3194 gold_assert((this->info_section_ == NULL
3195 || (this->info_section_ == os
3196 && this->info_uses_section_index_))
3197 && this->info_symndx_ == NULL
3198 && this->info_ == 0);
3199 this->info_section_ = os;
3200 this->info_uses_section_index_= true;
3201 }
3202
3203 // Set the info field to the symbol table index of a symbol.
3204 void
3205 set_info_symndx(const Symbol* sym)
3206 {
3207 gold_assert(this->info_section_ == NULL
3208 && (this->info_symndx_ == NULL
3209 || this->info_symndx_ == sym)
3210 && this->info_ == 0);
3211 this->info_symndx_ = sym;
3212 }
3213
3214 // Set the info field to the symbol table index of a section symbol.
3215 void
3216 set_info_section_symndx(const Output_section* os)
3217 {
3218 gold_assert((this->info_section_ == NULL
3219 || (this->info_section_ == os
3220 && !this->info_uses_section_index_))
3221 && this->info_symndx_ == NULL
3222 && this->info_ == 0);
3223 this->info_section_ = os;
3224 this->info_uses_section_index_ = false;
3225 }
3226
3227 // Set the info field to a constant.
3228 void
3229 set_info(unsigned int v)
3230 {
3231 gold_assert(this->info_section_ == NULL
3232 && this->info_symndx_ == NULL
3233 && (this->info_ == 0
3234 || this->info_ == v));
3235 this->info_ = v;
3236 }
3237
3238 // Set the addralign field.
3239 void
3240 set_addralign(uint64_t v)
3241 { this->addralign_ = v; }
3242
3243 void
3244 checkpoint_set_addralign(uint64_t val)
3245 {
3246 if (this->checkpoint_ != NULL)
3247 this->checkpoint_->set_addralign(val);
3248 }
3249
3250 // Whether the output section index has been set.
3251 bool
3252 has_out_shndx() const
3253 { return this->out_shndx_ != -1U; }
3254
3255 // Indicate that we need a symtab index.
3256 void
3257 set_needs_symtab_index()
3258 { this->needs_symtab_index_ = true; }
3259
3260 // Return whether we need a symtab index.
3261 bool
3262 needs_symtab_index() const
3263 { return this->needs_symtab_index_; }
3264
3265 // Get the symtab index.
3266 unsigned int
3267 symtab_index() const
3268 {
3269 gold_assert(this->symtab_index_ != 0);
3270 return this->symtab_index_;
3271 }
3272
3273 // Set the symtab index.
3274 void
3275 set_symtab_index(unsigned int index)
3276 {
3277 gold_assert(index != 0);
3278 this->symtab_index_ = index;
3279 }
3280
3281 // Indicate that we need a dynsym index.
3282 void
3283 set_needs_dynsym_index()
3284 { this->needs_dynsym_index_ = true; }
3285
3286 // Return whether we need a dynsym index.
3287 bool
3288 needs_dynsym_index() const
3289 { return this->needs_dynsym_index_; }
3290
3291 // Get the dynsym index.
3292 unsigned int
3293 dynsym_index() const
3294 {
3295 gold_assert(this->dynsym_index_ != 0);
3296 return this->dynsym_index_;
3297 }
3298
3299 // Set the dynsym index.
3300 void
3301 set_dynsym_index(unsigned int index)
3302 {
3303 gold_assert(index != 0);
3304 this->dynsym_index_ = index;
3305 }
3306
3307 // Sort the attached input sections.
3308 void
3309 sort_attached_input_sections();
3310
3311 // Return whether the input sections sections attachd to this output
3312 // section may require sorting. This is used to handle constructor
3313 // priorities compatibly with GNU ld.
3314 bool
3315 may_sort_attached_input_sections() const
3316 { return this->may_sort_attached_input_sections_; }
3317
3318 // Record that the input sections attached to this output section
3319 // may require sorting.
3320 void
3321 set_may_sort_attached_input_sections()
3322 { this->may_sort_attached_input_sections_ = true; }
3323
3324 // Returns true if input sections must be sorted according to the
3325 // order in which their name appear in the --section-ordering-file.
3326 bool
3327 input_section_order_specified()
3328 { return this->input_section_order_specified_; }
3329
3330 // Record that input sections must be sorted as some of their names
3331 // match the patterns specified through --section-ordering-file.
3332 void
3333 set_input_section_order_specified()
3334 { this->input_section_order_specified_ = true; }
3335
3336 // Return whether the input sections attached to this output section
3337 // require sorting. This is used to handle constructor priorities
3338 // compatibly with GNU ld.
3339 bool
3340 must_sort_attached_input_sections() const
3341 { return this->must_sort_attached_input_sections_; }
3342
3343 // Record that the input sections attached to this output section
3344 // require sorting.
3345 void
3346 set_must_sort_attached_input_sections()
3347 { this->must_sort_attached_input_sections_ = true; }
3348
3349 // Get the order in which this section appears in the PT_LOAD output
3350 // segment.
3351 Output_section_order
3352 order() const
3353 { return this->order_; }
3354
3355 // Set the order for this section.
3356 void
3357 set_order(Output_section_order order)
3358 { this->order_ = order; }
3359
3360 // Return whether this section holds relro data--data which has
3361 // dynamic relocations but which may be marked read-only after the
3362 // dynamic relocations have been completed.
3363 bool
3364 is_relro() const
3365 { return this->is_relro_; }
3366
3367 // Record that this section holds relro data.
3368 void
3369 set_is_relro()
3370 { this->is_relro_ = true; }
3371
3372 // Record that this section does not hold relro data.
3373 void
3374 clear_is_relro()
3375 { this->is_relro_ = false; }
3376
3377 // True if this is a small section: a section which holds small
3378 // variables.
3379 bool
3380 is_small_section() const
3381 { return this->is_small_section_; }
3382
3383 // Record that this is a small section.
3384 void
3385 set_is_small_section()
3386 { this->is_small_section_ = true; }
3387
3388 // True if this is a large section: a section which holds large
3389 // variables.
3390 bool
3391 is_large_section() const
3392 { return this->is_large_section_; }
3393
3394 // Record that this is a large section.
3395 void
3396 set_is_large_section()
3397 { this->is_large_section_ = true; }
3398
3399 // True if this is a large data (not BSS) section.
3400 bool
3401 is_large_data_section()
3402 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3403
3404 // Return whether this section should be written after all the input
3405 // sections are complete.
3406 bool
3407 after_input_sections() const
3408 { return this->after_input_sections_; }
3409
3410 // Record that this section should be written after all the input
3411 // sections are complete.
3412 void
3413 set_after_input_sections()
3414 { this->after_input_sections_ = true; }
3415
3416 // Return whether this section requires postprocessing after all
3417 // relocations have been applied.
3418 bool
3419 requires_postprocessing() const
3420 { return this->requires_postprocessing_; }
3421
3422 bool
3423 is_unique_segment() const
3424 { return this->is_unique_segment_; }
3425
3426 void
3427 set_is_unique_segment()
3428 { this->is_unique_segment_ = true; }
3429
3430 uint64_t extra_segment_flags() const
3431 { return this->extra_segment_flags_; }
3432
3433 void
3434 set_extra_segment_flags(uint64_t flags)
3435 { this->extra_segment_flags_ = flags; }
3436
3437 uint64_t segment_alignment() const
3438 { return this->segment_alignment_; }
3439
3440 void
3441 set_segment_alignment(uint64_t align)
3442 { this->segment_alignment_ = align; }
3443
3444 // If a section requires postprocessing, return the buffer to use.
3445 unsigned char*
3446 postprocessing_buffer() const
3447 {
3448 gold_assert(this->postprocessing_buffer_ != NULL);
3449 return this->postprocessing_buffer_;
3450 }
3451
3452 // If a section requires postprocessing, create the buffer to use.
3453 void
3454 create_postprocessing_buffer();
3455
3456 // If a section requires postprocessing, this is the size of the
3457 // buffer to which relocations should be applied.
3458 off_t
3459 postprocessing_buffer_size() const
3460 { return this->current_data_size_for_child(); }
3461
3462 // Modify the section name. This is only permitted for an
3463 // unallocated section, and only before the size has been finalized.
3464 // Otherwise the name will not get into Layout::namepool_.
3465 void
3466 set_name(const char* newname)
3467 {
3468 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3469 gold_assert(!this->is_data_size_valid());
3470 this->name_ = newname;
3471 }
3472
3473 // Return whether the offset OFFSET in the input section SHNDX in
3474 // object OBJECT is being included in the link.
3475 bool
3476 is_input_address_mapped(const Relobj* object, unsigned int shndx,
3477 off_t offset) const;
3478
3479 // Return the offset within the output section of OFFSET relative to
3480 // the start of input section SHNDX in object OBJECT.
3481 section_offset_type
3482 output_offset(const Relobj* object, unsigned int shndx,
3483 section_offset_type offset) const;
3484
3485 // Return the output virtual address of OFFSET relative to the start
3486 // of input section SHNDX in object OBJECT.
3487 uint64_t
3488 output_address(const Relobj* object, unsigned int shndx,
3489 off_t offset) const;
3490
3491 // Look for the merged section for input section SHNDX in object
3492 // OBJECT. If found, return true, and set *ADDR to the address of
3493 // the start of the merged section. This is not necessary the
3494 // output offset corresponding to input offset 0 in the section,
3495 // since the section may be mapped arbitrarily.
3496 bool
3497 find_starting_output_address(const Relobj* object, unsigned int shndx,
3498 uint64_t* addr) const;
3499
3500 // Record that this output section was found in the SECTIONS clause
3501 // of a linker script.
3502 void
3503 set_found_in_sections_clause()
3504 { this->found_in_sections_clause_ = true; }
3505
3506 // Return whether this output section was found in the SECTIONS
3507 // clause of a linker script.
3508 bool
3509 found_in_sections_clause() const
3510 { return this->found_in_sections_clause_; }
3511
3512 // Write the section header into *OPHDR.
3513 template<int size, bool big_endian>
3514 void
3515 write_header(const Layout*, const Stringpool*,
3516 elfcpp::Shdr_write<size, big_endian>*) const;
3517
3518 // The next few calls are for linker script support.
3519
3520 // In some cases we need to keep a list of the input sections
3521 // associated with this output section. We only need the list if we
3522 // might have to change the offsets of the input section within the
3523 // output section after we add the input section. The ordinary
3524 // input sections will be written out when we process the object
3525 // file, and as such we don't need to track them here. We do need
3526 // to track Output_section_data objects here. We store instances of
3527 // this structure in a std::vector, so it must be a POD. There can
3528 // be many instances of this structure, so we use a union to save
3529 // some space.
3530 class Input_section
3531 {
3532 public:
3533 Input_section()
3534 : shndx_(0), p2align_(0)
3535 {
3536 this->u1_.data_size = 0;
3537 this->u2_.object = NULL;
3538 }
3539
3540 // For an ordinary input section.
3541 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3542 uint64_t addralign)
3543 : shndx_(shndx),
3544 p2align_(ffsll(static_cast<long long>(addralign))),
3545 section_order_index_(0)
3546 {
3547 gold_assert(shndx != OUTPUT_SECTION_CODE
3548 && shndx != MERGE_DATA_SECTION_CODE
3549 && shndx != MERGE_STRING_SECTION_CODE
3550 && shndx != RELAXED_INPUT_SECTION_CODE);
3551 this->u1_.data_size = data_size;
3552 this->u2_.object = object;
3553 }
3554
3555 // For a non-merge output section.
3556 Input_section(Output_section_data* posd)
3557 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3558 section_order_index_(0)
3559 {
3560 this->u1_.data_size = 0;
3561 this->u2_.posd = posd;
3562 }
3563
3564 // For a merge section.
3565 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3566 : shndx_(is_string
3567 ? MERGE_STRING_SECTION_CODE
3568 : MERGE_DATA_SECTION_CODE),
3569 p2align_(0),
3570 section_order_index_(0)
3571 {
3572 this->u1_.entsize = entsize;
3573 this->u2_.posd = posd;
3574 }
3575
3576 // For a relaxed input section.
3577 Input_section(Output_relaxed_input_section* psection)
3578 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3579 section_order_index_(0)
3580 {
3581 this->u1_.data_size = 0;
3582 this->u2_.poris = psection;
3583 }
3584
3585 unsigned int
3586 section_order_index() const
3587 {
3588 return this->section_order_index_;
3589 }
3590
3591 void
3592 set_section_order_index(unsigned int number)
3593 {
3594 this->section_order_index_ = number;
3595 }
3596
3597 // The required alignment.
3598 uint64_t
3599 addralign() const
3600 {
3601 if (this->p2align_ != 0)
3602 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3603 else if (!this->is_input_section())
3604 return this->u2_.posd->addralign();
3605 else
3606 return 0;
3607 }
3608
3609 // Set the required alignment, which must be either 0 or a power of 2.
3610 // For input sections that are sub-classes of Output_section_data, a
3611 // alignment of zero means asking the underlying object for alignment.
3612 void
3613 set_addralign(uint64_t addralign)
3614 {
3615 if (addralign == 0)
3616 this->p2align_ = 0;
3617 else
3618 {
3619 gold_assert((addralign & (addralign - 1)) == 0);
3620 this->p2align_ = ffsll(static_cast<long long>(addralign));
3621 }
3622 }
3623
3624 // Return the current required size, without finalization.
3625 off_t
3626 current_data_size() const;
3627
3628 // Return the required size.
3629 off_t
3630 data_size() const;
3631
3632 // Whether this is an input section.
3633 bool
3634 is_input_section() const
3635 {
3636 return (this->shndx_ != OUTPUT_SECTION_CODE
3637 && this->shndx_ != MERGE_DATA_SECTION_CODE
3638 && this->shndx_ != MERGE_STRING_SECTION_CODE
3639 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3640 }
3641
3642 // Return whether this is a merge section which matches the
3643 // parameters.
3644 bool
3645 is_merge_section(bool is_string, uint64_t entsize,
3646 uint64_t addralign) const
3647 {
3648 return (this->shndx_ == (is_string
3649 ? MERGE_STRING_SECTION_CODE
3650 : MERGE_DATA_SECTION_CODE)
3651 && this->u1_.entsize == entsize
3652 && this->addralign() == addralign);
3653 }
3654
3655 // Return whether this is a merge section for some input section.
3656 bool
3657 is_merge_section() const
3658 {
3659 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3660 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3661 }
3662
3663 // Return whether this is a relaxed input section.
3664 bool
3665 is_relaxed_input_section() const
3666 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3667
3668 // Return whether this is a generic Output_section_data.
3669 bool
3670 is_output_section_data() const
3671 {
3672 return this->shndx_ == OUTPUT_SECTION_CODE;
3673 }
3674
3675 // Return the object for an input section.
3676 Relobj*
3677 relobj() const;
3678
3679 // Return the input section index for an input section.
3680 unsigned int
3681 shndx() const;
3682
3683 // For non-input-sections, return the associated Output_section_data
3684 // object.
3685 Output_section_data*
3686 output_section_data() const
3687 {
3688 gold_assert(!this->is_input_section());
3689 return this->u2_.posd;
3690 }
3691
3692 // For a merge section, return the Output_merge_base pointer.
3693 Output_merge_base*
3694 output_merge_base() const
3695 {
3696 gold_assert(this->is_merge_section());
3697 return this->u2_.pomb;
3698 }
3699
3700 // Return the Output_relaxed_input_section object.
3701 Output_relaxed_input_section*
3702 relaxed_input_section() const
3703 {
3704 gold_assert(this->is_relaxed_input_section());
3705 return this->u2_.poris;
3706 }
3707
3708 // Set the output section.
3709 void
3710 set_output_section(Output_section* os)
3711 {
3712 gold_assert(!this->is_input_section());
3713 Output_section_data* posd =
3714 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3715 posd->set_output_section(os);
3716 }
3717
3718 // Set the address and file offset. This is called during
3719 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3720 // the enclosing section.
3721 void
3722 set_address_and_file_offset(uint64_t address, off_t file_offset,
3723 off_t section_file_offset);
3724
3725 // Reset the address and file offset.
3726 void
3727 reset_address_and_file_offset();
3728
3729 // Finalize the data size.
3730 void
3731 finalize_data_size();
3732
3733 // Add an input section, for SHF_MERGE sections.
3734 bool
3735 add_input_section(Relobj* object, unsigned int shndx)
3736 {
3737 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3738 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3739 return this->u2_.posd->add_input_section(object, shndx);
3740 }
3741
3742 // Given an input OBJECT, an input section index SHNDX within that
3743 // object, and an OFFSET relative to the start of that input
3744 // section, return whether or not the output offset is known. If
3745 // this function returns true, it sets *POUTPUT to the offset in
3746 // the output section, relative to the start of the input section
3747 // in the output section. *POUTPUT may be different from OFFSET
3748 // for a merged section.
3749 bool
3750 output_offset(const Relobj* object, unsigned int shndx,
3751 section_offset_type offset,
3752 section_offset_type* poutput) const;
3753
3754 // Return whether this is the merge section for the input section
3755 // SHNDX in OBJECT.
3756 bool
3757 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3758
3759 // Write out the data. This does nothing for an input section.
3760 void
3761 write(Output_file*);
3762
3763 // Write the data to a buffer. This does nothing for an input
3764 // section.
3765 void
3766 write_to_buffer(unsigned char*);
3767
3768 // Print to a map file.
3769 void
3770 print_to_mapfile(Mapfile*) const;
3771
3772 // Print statistics about merge sections to stderr.
3773 void
3774 print_merge_stats(const char* section_name)
3775 {
3776 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3777 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3778 this->u2_.posd->print_merge_stats(section_name);
3779 }
3780
3781 private:
3782 // Code values which appear in shndx_. If the value is not one of
3783 // these codes, it is the input section index in the object file.
3784 enum
3785 {
3786 // An Output_section_data.
3787 OUTPUT_SECTION_CODE = -1U,
3788 // An Output_section_data for an SHF_MERGE section with
3789 // SHF_STRINGS not set.
3790 MERGE_DATA_SECTION_CODE = -2U,
3791 // An Output_section_data for an SHF_MERGE section with
3792 // SHF_STRINGS set.
3793 MERGE_STRING_SECTION_CODE = -3U,
3794 // An Output_section_data for a relaxed input section.
3795 RELAXED_INPUT_SECTION_CODE = -4U
3796 };
3797
3798 // For an ordinary input section, this is the section index in the
3799 // input file. For an Output_section_data, this is
3800 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3801 // MERGE_STRING_SECTION_CODE.
3802 unsigned int shndx_;
3803 // The required alignment, stored as a power of 2.
3804 unsigned int p2align_;
3805 union
3806 {
3807 // For an ordinary input section, the section size.
3808 off_t data_size;
3809 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3810 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3811 // entity size.
3812 uint64_t entsize;
3813 } u1_;
3814 union
3815 {
3816 // For an ordinary input section, the object which holds the
3817 // input section.
3818 Relobj* object;
3819 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3820 // MERGE_STRING_SECTION_CODE, the data.
3821 Output_section_data* posd;
3822 Output_merge_base* pomb;
3823 // For RELAXED_INPUT_SECTION_CODE, the data.
3824 Output_relaxed_input_section* poris;
3825 } u2_;
3826 // The line number of the pattern it matches in the --section-ordering-file
3827 // file. It is 0 if does not match any pattern.
3828 unsigned int section_order_index_;
3829 };
3830
3831 // Store the list of input sections for this Output_section into the
3832 // list passed in. This removes the input sections, leaving only
3833 // any Output_section_data elements. This returns the size of those
3834 // Output_section_data elements. ADDRESS is the address of this
3835 // output section. FILL is the fill value to use, in case there are
3836 // any spaces between the remaining Output_section_data elements.
3837 uint64_t
3838 get_input_sections(uint64_t address, const std::string& fill,
3839 std::list<Input_section>*);
3840
3841 // Add a script input section. A script input section can either be
3842 // a plain input section or a sub-class of Output_section_data.
3843 void
3844 add_script_input_section(const Input_section& input_section);
3845
3846 // Set the current size of the output section.
3847 void
3848 set_current_data_size(off_t size)
3849 { this->set_current_data_size_for_child(size); }
3850
3851 // End of linker script support.
3852
3853 // Save states before doing section layout.
3854 // This is used for relaxation.
3855 void
3856 save_states();
3857
3858 // Restore states prior to section layout.
3859 void
3860 restore_states();
3861
3862 // Discard states.
3863 void
3864 discard_states();
3865
3866 // Convert existing input sections to relaxed input sections.
3867 void
3868 convert_input_sections_to_relaxed_sections(
3869 const std::vector<Output_relaxed_input_section*>& sections);
3870
3871 // Find a relaxed input section to an input section in OBJECT
3872 // with index SHNDX. Return NULL if none is found.
3873 const Output_relaxed_input_section*
3874 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3875
3876 // Whether section offsets need adjustment due to relaxation.
3877 bool
3878 section_offsets_need_adjustment() const
3879 { return this->section_offsets_need_adjustment_; }
3880
3881 // Set section_offsets_need_adjustment to be true.
3882 void
3883 set_section_offsets_need_adjustment()
3884 { this->section_offsets_need_adjustment_ = true; }
3885
3886 // Set section_offsets_need_adjustment to be false.
3887 void
3888 clear_section_offsets_need_adjustment()
3889 { this->section_offsets_need_adjustment_ = false; }
3890
3891 // Adjust section offsets of input sections in this. This is
3892 // requires if relaxation caused some input sections to change sizes.
3893 void
3894 adjust_section_offsets();
3895
3896 // Whether this is a NOLOAD section.
3897 bool
3898 is_noload() const
3899 { return this->is_noload_; }
3900
3901 // Set NOLOAD flag.
3902 void
3903 set_is_noload()
3904 { this->is_noload_ = true; }
3905
3906 // Print merge statistics to stderr.
3907 void
3908 print_merge_stats();
3909
3910 // Set a fixed layout for the section. Used for incremental update links.
3911 void
3912 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3913 uint64_t sh_addralign);
3914
3915 // Return TRUE if the section has a fixed layout.
3916 bool
3917 has_fixed_layout() const
3918 { return this->has_fixed_layout_; }
3919
3920 // Set flag to allow patch space for this section. Used for full
3921 // incremental links.
3922 void
3923 set_is_patch_space_allowed()
3924 { this->is_patch_space_allowed_ = true; }
3925
3926 // Set a fill method to use for free space left in the output section
3927 // during incremental links.
3928 void
3929 set_free_space_fill(Output_fill* free_space_fill)
3930 {
3931 this->free_space_fill_ = free_space_fill;
3932 this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3933 }
3934
3935 // Reserve space within the fixed layout for the section. Used for
3936 // incremental update links.
3937 void
3938 reserve(uint64_t sh_offset, uint64_t sh_size);
3939
3940 // Allocate space from the free list for the section. Used for
3941 // incremental update links.
3942 off_t
3943 allocate(off_t len, uint64_t addralign);
3944
3945 typedef std::vector<Input_section> Input_section_list;
3946
3947 // Allow access to the input sections.
3948 const Input_section_list&
3949 input_sections() const
3950 { return this->input_sections_; }
3951
3952 Input_section_list&
3953 input_sections()
3954 { return this->input_sections_; }
3955
3956 protected:
3957 // Return the output section--i.e., the object itself.
3958 Output_section*
3959 do_output_section()
3960 { return this; }
3961
3962 const Output_section*
3963 do_output_section() const
3964 { return this; }
3965
3966 // Return the section index in the output file.
3967 unsigned int
3968 do_out_shndx() const
3969 {
3970 gold_assert(this->out_shndx_ != -1U);
3971 return this->out_shndx_;
3972 }
3973
3974 // Set the output section index.
3975 void
3976 do_set_out_shndx(unsigned int shndx)
3977 {
3978 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3979 this->out_shndx_ = shndx;
3980 }
3981
3982 // Update the data size of the Output_section. For a typical
3983 // Output_section, there is nothing to do, but if there are any
3984 // Output_section_data objects we need to do a trial layout
3985 // here.
3986 virtual void
3987 update_data_size();
3988
3989 // Set the final data size of the Output_section. For a typical
3990 // Output_section, there is nothing to do, but if there are any
3991 // Output_section_data objects we need to set their final addresses
3992 // here.
3993 virtual void
3994 set_final_data_size();
3995
3996 // Reset the address and file offset.
3997 void
3998 do_reset_address_and_file_offset();
3999
4000 // Return true if address and file offset already have reset values. In
4001 // other words, calling reset_address_and_file_offset will not change them.
4002 bool
4003 do_address_and_file_offset_have_reset_values() const;
4004
4005 // Write the data to the file. For a typical Output_section, this
4006 // does nothing: the data is written out by calling Object::Relocate
4007 // on each input object. But if there are any Output_section_data
4008 // objects we do need to write them out here.
4009 virtual void
4010 do_write(Output_file*);
4011
4012 // Return the address alignment--function required by parent class.
4013 uint64_t
4014 do_addralign() const
4015 { return this->addralign_; }
4016
4017 // Return whether there is a load address.
4018 bool
4019 do_has_load_address() const
4020 { return this->has_load_address_; }
4021
4022 // Return the load address.
4023 uint64_t
4024 do_load_address() const
4025 {
4026 gold_assert(this->has_load_address_);
4027 return this->load_address_;
4028 }
4029
4030 // Return whether this is an Output_section.
4031 bool
4032 do_is_section() const
4033 { return true; }
4034
4035 // Return whether this is a section of the specified type.
4036 bool
4037 do_is_section_type(elfcpp::Elf_Word type) const
4038 { return this->type_ == type; }
4039
4040 // Return whether the specified section flag is set.
4041 bool
4042 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
4043 { return (this->flags_ & flag) != 0; }
4044
4045 // Set the TLS offset. Called only for SHT_TLS sections.
4046 void
4047 do_set_tls_offset(uint64_t tls_base);
4048
4049 // Return the TLS offset, relative to the base of the TLS segment.
4050 // Valid only for SHT_TLS sections.
4051 uint64_t
4052 do_tls_offset() const
4053 { return this->tls_offset_; }
4054
4055 // This may be implemented by a child class.
4056 virtual void
4057 do_finalize_name(Layout*)
4058 { }
4059
4060 // Print to the map file.
4061 virtual void
4062 do_print_to_mapfile(Mapfile*) const;
4063
4064 // Record that this section requires postprocessing after all
4065 // relocations have been applied. This is called by a child class.
4066 void
4067 set_requires_postprocessing()
4068 {
4069 this->requires_postprocessing_ = true;
4070 this->after_input_sections_ = true;
4071 }
4072
4073 // Write all the data of an Output_section into the postprocessing
4074 // buffer.
4075 void
4076 write_to_postprocessing_buffer();
4077
4078 // Whether this always keeps an input section list
4079 bool
4080 always_keeps_input_sections() const
4081 { return this->always_keeps_input_sections_; }
4082
4083 // Always keep an input section list.
4084 void
4085 set_always_keeps_input_sections()
4086 {
4087 gold_assert(this->current_data_size_for_child() == 0);
4088 this->always_keeps_input_sections_ = true;
4089 }
4090
4091 private:
4092 // We only save enough information to undo the effects of section layout.
4093 class Checkpoint_output_section
4094 {
4095 public:
4096 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
4097 const Input_section_list& input_sections,
4098 off_t first_input_offset,
4099 bool attached_input_sections_are_sorted)
4100 : addralign_(addralign), flags_(flags),
4101 input_sections_(input_sections),
4102 input_sections_size_(input_sections_.size()),
4103 input_sections_copy_(), first_input_offset_(first_input_offset),
4104 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
4105 { }
4106
4107 virtual
4108 ~Checkpoint_output_section()
4109 { }
4110
4111 // Return the address alignment.
4112 uint64_t
4113 addralign() const
4114 { return this->addralign_; }
4115
4116 void
4117 set_addralign(uint64_t val)
4118 { this->addralign_ = val; }
4119
4120 // Return the section flags.
4121 elfcpp::Elf_Xword
4122 flags() const
4123 { return this->flags_; }
4124
4125 // Return a reference to the input section list copy.
4126 Input_section_list*
4127 input_sections()
4128 { return &this->input_sections_copy_; }
4129
4130 // Return the size of input_sections at the time when checkpoint is
4131 // taken.
4132 size_t
4133 input_sections_size() const
4134 { return this->input_sections_size_; }
4135
4136 // Whether input sections are copied.
4137 bool
4138 input_sections_saved() const
4139 { return this->input_sections_copy_.size() == this->input_sections_size_; }
4140
4141 off_t
4142 first_input_offset() const
4143 { return this->first_input_offset_; }
4144
4145 bool
4146 attached_input_sections_are_sorted() const
4147 { return this->attached_input_sections_are_sorted_; }
4148
4149 // Save input sections.
4150 void
4151 save_input_sections()
4152 {
4153 this->input_sections_copy_.reserve(this->input_sections_size_);
4154 this->input_sections_copy_.clear();
4155 Input_section_list::const_iterator p = this->input_sections_.begin();
4156 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
4157 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
4158 this->input_sections_copy_.push_back(*p);
4159 }
4160
4161 private:
4162 // The section alignment.
4163 uint64_t addralign_;
4164 // The section flags.
4165 elfcpp::Elf_Xword flags_;
4166 // Reference to the input sections to be checkpointed.
4167 const Input_section_list& input_sections_;
4168 // Size of the checkpointed portion of input_sections_;
4169 size_t input_sections_size_;
4170 // Copy of input sections.
4171 Input_section_list input_sections_copy_;
4172 // The offset of the first entry in input_sections_.
4173 off_t first_input_offset_;
4174 // True if the input sections attached to this output section have
4175 // already been sorted.
4176 bool attached_input_sections_are_sorted_;
4177 };
4178
4179 // This class is used to sort the input sections.
4180 class Input_section_sort_entry;
4181
4182 // This is the sort comparison function for ctors and dtors.
4183 struct Input_section_sort_compare
4184 {
4185 bool
4186 operator()(const Input_section_sort_entry&,
4187 const Input_section_sort_entry&) const;
4188 };
4189
4190 // This is the sort comparison function for .init_array and .fini_array.
4191 struct Input_section_sort_init_fini_compare
4192 {
4193 bool
4194 operator()(const Input_section_sort_entry&,
4195 const Input_section_sort_entry&) const;
4196 };
4197
4198 // This is the sort comparison function when a section order is specified
4199 // from an input file.
4200 struct Input_section_sort_section_order_index_compare
4201 {
4202 bool
4203 operator()(const Input_section_sort_entry&,
4204 const Input_section_sort_entry&) const;
4205 };
4206
4207 // This is the sort comparison function for .text to sort sections with
4208 // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4209 struct Input_section_sort_section_prefix_special_ordering_compare
4210 {
4211 bool
4212 operator()(const Input_section_sort_entry&,
4213 const Input_section_sort_entry&) const;
4214 };
4215
4216 // This is the sort comparison function for sorting sections by name.
4217 struct Input_section_sort_section_name_compare
4218 {
4219 bool
4220 operator()(const Input_section_sort_entry&,
4221 const Input_section_sort_entry&) const;
4222 };
4223
4224 // Fill data. This is used to fill in data between input sections.
4225 // It is also used for data statements (BYTE, WORD, etc.) in linker
4226 // scripts. When we have to keep track of the input sections, we
4227 // can use an Output_data_const, but we don't want to have to keep
4228 // track of input sections just to implement fills.
4229 class Fill
4230 {
4231 public:
4232 Fill(off_t section_offset, off_t length)
4233 : section_offset_(section_offset),
4234 length_(convert_to_section_size_type(length))
4235 { }
4236
4237 // Return section offset.
4238 off_t
4239 section_offset() const
4240 { return this->section_offset_; }
4241
4242 // Return fill length.
4243 section_size_type
4244 length() const
4245 { return this->length_; }
4246
4247 private:
4248 // The offset within the output section.
4249 off_t section_offset_;
4250 // The length of the space to fill.
4251 section_size_type length_;
4252 };
4253
4254 typedef std::vector<Fill> Fill_list;
4255
4256 // Map used during relaxation of existing sections. This map
4257 // a section id an input section list index. We assume that
4258 // Input_section_list is a vector.
4259 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4260
4261 // Add a new output section by Input_section.
4262 void
4263 add_output_section_data(Input_section*);
4264
4265 // Add an SHF_MERGE input section. Returns true if the section was
4266 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4267 // stores information about the merged input sections.
4268 bool
4269 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4270 uint64_t entsize, uint64_t addralign,
4271 bool keeps_input_sections);
4272
4273 // Add an output SHF_MERGE section POSD to this output section.
4274 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4275 // ENTSIZE is the entity size. This returns the entry added to
4276 // input_sections_.
4277 void
4278 add_output_merge_section(Output_section_data* posd, bool is_string,
4279 uint64_t entsize);
4280
4281 // Find the merge section into which an input section with index SHNDX in
4282 // OBJECT has been added. Return NULL if none found.
4283 Output_section_data*
4284 find_merge_section(const Relobj* object, unsigned int shndx) const;
4285
4286 // Build a relaxation map.
4287 void
4288 build_relaxation_map(
4289 const Input_section_list& input_sections,
4290 size_t limit,
4291 Relaxation_map* map) const;
4292
4293 // Convert input sections in an input section list into relaxed sections.
4294 void
4295 convert_input_sections_in_list_to_relaxed_sections(
4296 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4297 const Relaxation_map& map,
4298 Input_section_list* input_sections);
4299
4300 // Build the lookup maps for merge and relaxed input sections.
4301 void
4302 build_lookup_maps() const;
4303
4304 // Most of these fields are only valid after layout.
4305
4306 // The name of the section. This will point into a Stringpool.
4307 const char* name_;
4308 // The section address is in the parent class.
4309 // The section alignment.
4310 uint64_t addralign_;
4311 // The section entry size.
4312 uint64_t entsize_;
4313 // The load address. This is only used when using a linker script
4314 // with a SECTIONS clause. The has_load_address_ field indicates
4315 // whether this field is valid.
4316 uint64_t load_address_;
4317 // The file offset is in the parent class.
4318 // Set the section link field to the index of this section.
4319 const Output_data* link_section_;
4320 // If link_section_ is NULL, this is the link field.
4321 unsigned int link_;
4322 // Set the section info field to the index of this section.
4323 const Output_section* info_section_;
4324 // If info_section_ is NULL, set the info field to the symbol table
4325 // index of this symbol.
4326 const Symbol* info_symndx_;
4327 // If info_section_ and info_symndx_ are NULL, this is the section
4328 // info field.
4329 unsigned int info_;
4330 // The section type.
4331 const elfcpp::Elf_Word type_;
4332 // The section flags.
4333 elfcpp::Elf_Xword flags_;
4334 // The order of this section in the output segment.
4335 Output_section_order order_;
4336 // The section index.
4337 unsigned int out_shndx_;
4338 // If there is a STT_SECTION for this output section in the normal
4339 // symbol table, this is the symbol index. This starts out as zero.
4340 // It is initialized in Layout::finalize() to be the index, or -1U
4341 // if there isn't one.
4342 unsigned int symtab_index_;
4343 // If there is a STT_SECTION for this output section in the dynamic
4344 // symbol table, this is the symbol index. This starts out as zero.
4345 // It is initialized in Layout::finalize() to be the index, or -1U
4346 // if there isn't one.
4347 unsigned int dynsym_index_;
4348 // The input sections. This will be empty in cases where we don't
4349 // need to keep track of them.
4350 Input_section_list input_sections_;
4351 // The offset of the first entry in input_sections_.
4352 off_t first_input_offset_;
4353 // The fill data. This is separate from input_sections_ because we
4354 // often will need fill sections without needing to keep track of
4355 // input sections.
4356 Fill_list fills_;
4357 // If the section requires postprocessing, this buffer holds the
4358 // section contents during relocation.
4359 unsigned char* postprocessing_buffer_;
4360 // Whether this output section needs a STT_SECTION symbol in the
4361 // normal symbol table. This will be true if there is a relocation
4362 // which needs it.
4363 bool needs_symtab_index_ : 1;
4364 // Whether this output section needs a STT_SECTION symbol in the
4365 // dynamic symbol table. This will be true if there is a dynamic
4366 // relocation which needs it.
4367 bool needs_dynsym_index_ : 1;
4368 // Whether the link field of this output section should point to the
4369 // normal symbol table.
4370 bool should_link_to_symtab_ : 1;
4371 // Whether the link field of this output section should point to the
4372 // dynamic symbol table.
4373 bool should_link_to_dynsym_ : 1;
4374 // Whether this section should be written after all the input
4375 // sections are complete.
4376 bool after_input_sections_ : 1;
4377 // Whether this section requires post processing after all
4378 // relocations have been applied.
4379 bool requires_postprocessing_ : 1;
4380 // Whether an input section was mapped to this output section
4381 // because of a SECTIONS clause in a linker script.
4382 bool found_in_sections_clause_ : 1;
4383 // Whether this section has an explicitly specified load address.
4384 bool has_load_address_ : 1;
4385 // True if the info_section_ field means the section index of the
4386 // section, false if it means the symbol index of the corresponding
4387 // section symbol.
4388 bool info_uses_section_index_ : 1;
4389 // True if input sections attached to this output section have to be
4390 // sorted according to a specified order.
4391 bool input_section_order_specified_ : 1;
4392 // True if the input sections attached to this output section may
4393 // need sorting.
4394 bool may_sort_attached_input_sections_ : 1;
4395 // True if the input sections attached to this output section must
4396 // be sorted.
4397 bool must_sort_attached_input_sections_ : 1;
4398 // True if the input sections attached to this output section have
4399 // already been sorted.
4400 bool attached_input_sections_are_sorted_ : 1;
4401 // True if this section holds relro data.
4402 bool is_relro_ : 1;
4403 // True if this is a small section.
4404 bool is_small_section_ : 1;
4405 // True if this is a large section.
4406 bool is_large_section_ : 1;
4407 // Whether code-fills are generated at write.
4408 bool generate_code_fills_at_write_ : 1;
4409 // Whether the entry size field should be zero.
4410 bool is_entsize_zero_ : 1;
4411 // Whether section offsets need adjustment due to relaxation.
4412 bool section_offsets_need_adjustment_ : 1;
4413 // Whether this is a NOLOAD section.
4414 bool is_noload_ : 1;
4415 // Whether this always keeps input section.
4416 bool always_keeps_input_sections_ : 1;
4417 // Whether this section has a fixed layout, for incremental update links.
4418 bool has_fixed_layout_ : 1;
4419 // True if we can add patch space to this section.
4420 bool is_patch_space_allowed_ : 1;
4421 // True if this output section goes into a unique segment.
4422 bool is_unique_segment_ : 1;
4423 // For SHT_TLS sections, the offset of this section relative to the base
4424 // of the TLS segment.
4425 uint64_t tls_offset_;
4426 // Additional segment flags, specified via linker plugin, when mapping some
4427 // input sections to unique segments.
4428 uint64_t extra_segment_flags_;
4429 // Segment alignment specified via linker plugin, when mapping some
4430 // input sections to unique segments.
4431 uint64_t segment_alignment_;
4432 // Saved checkpoint.
4433 Checkpoint_output_section* checkpoint_;
4434 // Fast lookup maps for merged and relaxed input sections.
4435 Output_section_lookup_maps* lookup_maps_;
4436 // List of available regions within the section, for incremental
4437 // update links.
4438 Free_list free_list_;
4439 // Method for filling chunks of free space.
4440 Output_fill* free_space_fill_;
4441 // Amount added as patch space for incremental linking.
4442 off_t patch_space_;
4443 };
4444
4445 // An output segment. PT_LOAD segments are built from collections of
4446 // output sections. Other segments typically point within PT_LOAD
4447 // segments, and are built directly as needed.
4448 //
4449 // NOTE: We want to use the copy constructor for this class. During
4450 // relaxation, we may try built the segments multiple times. We do
4451 // that by copying the original segment list before lay-out, doing
4452 // a trial lay-out and roll-back to the saved copied if we need to
4453 // to the lay-out again.
4454
4455 class Output_segment
4456 {
4457 public:
4458 // Create an output segment, specifying the type and flags.
4459 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4460
4461 // Return the virtual address.
4462 uint64_t
4463 vaddr() const
4464 { return this->vaddr_; }
4465
4466 // Return the physical address.
4467 uint64_t
4468 paddr() const
4469 { return this->paddr_; }
4470
4471 // Return the segment type.
4472 elfcpp::Elf_Word
4473 type() const
4474 { return this->type_; }
4475
4476 // Return the segment flags.
4477 elfcpp::Elf_Word
4478 flags() const
4479 { return this->flags_; }
4480
4481 // Return the memory size.
4482 uint64_t
4483 memsz() const
4484 { return this->memsz_; }
4485
4486 // Return the file size.
4487 off_t
4488 filesz() const
4489 { return this->filesz_; }
4490
4491 // Return the file offset.
4492 off_t
4493 offset() const
4494 { return this->offset_; }
4495
4496 // Whether this is a segment created to hold large data sections.
4497 bool
4498 is_large_data_segment() const
4499 { return this->is_large_data_segment_; }
4500
4501 // Record that this is a segment created to hold large data
4502 // sections.
4503 void
4504 set_is_large_data_segment()
4505 { this->is_large_data_segment_ = true; }
4506
4507 bool
4508 is_unique_segment() const
4509 { return this->is_unique_segment_; }
4510
4511 // Mark segment as unique, happens when linker plugins request that
4512 // certain input sections be mapped to unique segments.
4513 void
4514 set_is_unique_segment()
4515 { this->is_unique_segment_ = true; }
4516
4517 // Return the maximum alignment of the Output_data.
4518 uint64_t
4519 maximum_alignment();
4520
4521 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4522 // the segment flags to use.
4523 void
4524 add_output_section_to_load(Layout* layout, Output_section* os,
4525 elfcpp::Elf_Word seg_flags);
4526
4527 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4528 // is the segment flags to use.
4529 void
4530 add_output_section_to_nonload(Output_section* os,
4531 elfcpp::Elf_Word seg_flags);
4532
4533 // Remove an Output_section from this segment. It is an error if it
4534 // is not present.
4535 void
4536 remove_output_section(Output_section* os);
4537
4538 // Add an Output_data (which need not be an Output_section) to the
4539 // start of this segment.
4540 void
4541 add_initial_output_data(Output_data*);
4542
4543 // Return true if this segment has any sections which hold actual
4544 // data, rather than being a BSS section.
4545 bool
4546 has_any_data_sections() const;
4547
4548 // Whether this segment has a dynamic relocs.
4549 bool
4550 has_dynamic_reloc() const;
4551
4552 // Return the first section.
4553 Output_section*
4554 first_section() const;
4555
4556 // Return the address of the first section.
4557 uint64_t
4558 first_section_load_address() const
4559 {
4560 const Output_section* os = this->first_section();
4561 return os->has_load_address() ? os->load_address() : os->address();
4562 }
4563
4564 // Return whether the addresses have been set already.
4565 bool
4566 are_addresses_set() const
4567 { return this->are_addresses_set_; }
4568
4569 // Set the addresses.
4570 void
4571 set_addresses(uint64_t vaddr, uint64_t paddr)
4572 {
4573 this->vaddr_ = vaddr;
4574 this->paddr_ = paddr;
4575 this->are_addresses_set_ = true;
4576 }
4577
4578 // Update the flags for the flags of an output section added to this
4579 // segment.
4580 void
4581 update_flags_for_output_section(elfcpp::Elf_Xword flags)
4582 {
4583 // The ELF ABI specifies that a PT_TLS segment should always have
4584 // PF_R as the flags.
4585 if (this->type() != elfcpp::PT_TLS)
4586 this->flags_ |= flags;
4587 }
4588
4589 // Set the segment flags. This is only used if we have a PHDRS
4590 // clause which explicitly specifies the flags.
4591 void
4592 set_flags(elfcpp::Elf_Word flags)
4593 { this->flags_ = flags; }
4594
4595 // Set the address of the segment to ADDR and the offset to *POFF
4596 // and set the addresses and offsets of all contained output
4597 // sections accordingly. Set the section indexes of all contained
4598 // output sections starting with *PSHNDX. If RESET is true, first
4599 // reset the addresses of the contained sections. Return the
4600 // address of the immediately following segment. Update *POFF and
4601 // *PSHNDX. This should only be called for a PT_LOAD segment.
4602 uint64_t
4603 set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
4604 unsigned int* increase_relro, bool* has_relro,
4605 off_t* poff, unsigned int* pshndx);
4606
4607 // Set the minimum alignment of this segment. This may be adjusted
4608 // upward based on the section alignments.
4609 void
4610 set_minimum_p_align(uint64_t align)
4611 {
4612 if (align > this->min_p_align_)
4613 this->min_p_align_ = align;
4614 }
4615
4616 // Set the offset of this segment based on the section. This should
4617 // only be called for a non-PT_LOAD segment.
4618 void
4619 set_offset(unsigned int increase);
4620
4621 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4622 void
4623 set_tls_offsets();
4624
4625 // Return the number of output sections.
4626 unsigned int
4627 output_section_count() const;
4628
4629 // Return the section attached to the list segment with the lowest
4630 // load address. This is used when handling a PHDRS clause in a
4631 // linker script.
4632 Output_section*
4633 section_with_lowest_load_address() const;
4634
4635 // Write the segment header into *OPHDR.
4636 template<int size, bool big_endian>
4637 void
4638 write_header(elfcpp::Phdr_write<size, big_endian>*);
4639
4640 // Write the section headers of associated sections into V.
4641 template<int size, bool big_endian>
4642 unsigned char*
4643 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4644 unsigned int* pshndx) const;
4645
4646 // Print the output sections in the map file.
4647 void
4648 print_sections_to_mapfile(Mapfile*) const;
4649
4650 private:
4651 typedef std::vector<Output_data*> Output_data_list;
4652
4653 // Find the maximum alignment in an Output_data_list.
4654 static uint64_t
4655 maximum_alignment_list(const Output_data_list*);
4656
4657 // Return whether the first data section is a relro section.
4658 bool
4659 is_first_section_relro() const;
4660
4661 // Set the section addresses in an Output_data_list.
4662 uint64_t
4663 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4664 uint64_t addr, off_t* poff, unsigned int* pshndx,
4665 bool* in_tls);
4666
4667 // Return the number of Output_sections in an Output_data_list.
4668 unsigned int
4669 output_section_count_list(const Output_data_list*) const;
4670
4671 // Return whether an Output_data_list has a dynamic reloc.
4672 bool
4673 has_dynamic_reloc_list(const Output_data_list*) const;
4674
4675 // Find the section with the lowest load address in an
4676 // Output_data_list.
4677 void
4678 lowest_load_address_in_list(const Output_data_list* pdl,
4679 Output_section** found,
4680 uint64_t* found_lma) const;
4681
4682 // Find the first and last entries by address.
4683 void
4684 find_first_and_last_list(const Output_data_list* pdl,
4685 const Output_data** pfirst,
4686 const Output_data** plast) const;
4687
4688 // Write the section headers in the list into V.
4689 template<int size, bool big_endian>
4690 unsigned char*
4691 write_section_headers_list(const Layout*, const Stringpool*,
4692 const Output_data_list*, unsigned char* v,
4693 unsigned int* pshdx) const;
4694
4695 // Print a section list to the mapfile.
4696 void
4697 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4698
4699 // NOTE: We want to use the copy constructor. Currently, shallow copy
4700 // works for us so we do not need to write our own copy constructor.
4701
4702 // The list of output data attached to this segment.
4703 Output_data_list output_lists_[ORDER_MAX];
4704 // The segment virtual address.
4705 uint64_t vaddr_;
4706 // The segment physical address.
4707 uint64_t paddr_;
4708 // The size of the segment in memory.
4709 uint64_t memsz_;
4710 // The maximum section alignment. The is_max_align_known_ field
4711 // indicates whether this has been finalized.
4712 uint64_t max_align_;
4713 // The required minimum value for the p_align field. This is used
4714 // for PT_LOAD segments. Note that this does not mean that
4715 // addresses should be aligned to this value; it means the p_paddr
4716 // and p_vaddr fields must be congruent modulo this value. For
4717 // non-PT_LOAD segments, the dynamic linker works more efficiently
4718 // if the p_align field has the more conventional value, although it
4719 // can align as needed.
4720 uint64_t min_p_align_;
4721 // The offset of the segment data within the file.
4722 off_t offset_;
4723 // The size of the segment data in the file.
4724 off_t filesz_;
4725 // The segment type;
4726 elfcpp::Elf_Word type_;
4727 // The segment flags.
4728 elfcpp::Elf_Word flags_;
4729 // Whether we have finalized max_align_.
4730 bool is_max_align_known_ : 1;
4731 // Whether vaddr and paddr were set by a linker script.
4732 bool are_addresses_set_ : 1;
4733 // Whether this segment holds large data sections.
4734 bool is_large_data_segment_ : 1;
4735 // Whether this was marked as a unique segment via a linker plugin.
4736 bool is_unique_segment_ : 1;
4737 };
4738
4739 // This class represents the output file.
4740
4741 class Output_file
4742 {
4743 public:
4744 Output_file(const char* name);
4745
4746 // Indicate that this is a temporary file which should not be
4747 // output.
4748 void
4749 set_is_temporary()
4750 { this->is_temporary_ = true; }
4751
4752 // Try to open an existing file. Returns false if the file doesn't
4753 // exist, has a size of 0 or can't be mmaped. This method is
4754 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4755 // that file as the base for incremental linking.
4756 bool
4757 open_base_file(const char* base_name, bool writable);
4758
4759 // Open the output file. FILE_SIZE is the final size of the file.
4760 // If the file already exists, it is deleted/truncated. This method
4761 // is thread-unsafe.
4762 void
4763 open(off_t file_size);
4764
4765 // Resize the output file. This method is thread-unsafe.
4766 void
4767 resize(off_t file_size);
4768
4769 // Close the output file (flushing all buffered data) and make sure
4770 // there are no errors. This method is thread-unsafe.
4771 void
4772 close();
4773
4774 // Return the size of this file.
4775 off_t
4776 filesize()
4777 { return this->file_size_; }
4778
4779 // Return the name of this file.
4780 const char*
4781 filename()
4782 { return this->name_; }
4783
4784 // We currently always use mmap which makes the view handling quite
4785 // simple. In the future we may support other approaches.
4786
4787 // Write data to the output file.
4788 void
4789 write(off_t offset, const void* data, size_t len)
4790 { memcpy(this->base_ + offset, data, len); }
4791
4792 // Get a buffer to use to write to the file, given the offset into
4793 // the file and the size.
4794 unsigned char*
4795 get_output_view(off_t start, size_t size)
4796 {
4797 gold_assert(start >= 0
4798 && start + static_cast<off_t>(size) <= this->file_size_);
4799 return this->base_ + start;
4800 }
4801
4802 // VIEW must have been returned by get_output_view. Write the
4803 // buffer to the file, passing in the offset and the size.
4804 void
4805 write_output_view(off_t, size_t, unsigned char*)
4806 { }
4807
4808 // Get a read/write buffer. This is used when we want to write part
4809 // of the file, read it in, and write it again.
4810 unsigned char*
4811 get_input_output_view(off_t start, size_t size)
4812 { return this->get_output_view(start, size); }
4813
4814 // Write a read/write buffer back to the file.
4815 void
4816 write_input_output_view(off_t, size_t, unsigned char*)
4817 { }
4818
4819 // Get a read buffer. This is used when we just want to read part
4820 // of the file back it in.
4821 const unsigned char*
4822 get_input_view(off_t start, size_t size)
4823 { return this->get_output_view(start, size); }
4824
4825 // Release a read bfufer.
4826 void
4827 free_input_view(off_t, size_t, const unsigned char*)
4828 { }
4829
4830 private:
4831 // Map the file into memory or, if that fails, allocate anonymous
4832 // memory.
4833 void
4834 map();
4835
4836 // Allocate anonymous memory for the file.
4837 bool
4838 map_anonymous();
4839
4840 // Map the file into memory.
4841 bool
4842 map_no_anonymous(bool);
4843
4844 // Unmap the file from memory (and flush to disk buffers).
4845 void
4846 unmap();
4847
4848 // File name.
4849 const char* name_;
4850 // File descriptor.
4851 int o_;
4852 // File size.
4853 off_t file_size_;
4854 // Base of file mapped into memory.
4855 unsigned char* base_;
4856 // True iff base_ points to a memory buffer rather than an output file.
4857 bool map_is_anonymous_;
4858 // True if base_ was allocated using new rather than mmap.
4859 bool map_is_allocated_;
4860 // True if this is a temporary file which should not be output.
4861 bool is_temporary_;
4862 };
4863
4864 } // End namespace gold.
4865
4866 #endif // !defined(GOLD_OUTPUT_H)