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