1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
32 #include "reloc-types.h"
37 class General_options
;
41 class Output_merge_base
;
43 class Relocatable_relocs
;
45 template<int size
, bool big_endian
>
47 template<int size
, bool big_endian
>
50 // An abtract class for data which has to go into the output file.
55 explicit Output_data()
56 : address_(0), data_size_(0), offset_(-1),
57 is_address_valid_(false), is_data_size_valid_(false),
58 is_offset_valid_(false), is_data_size_fixed_(false),
59 has_dynamic_reloc_(false)
65 // Return the address. For allocated sections, this is only valid
66 // after Layout::finalize is finished.
70 gold_assert(this->is_address_valid_
);
71 return this->address_
;
74 // Return the size of the data. For allocated sections, this must
75 // be valid after Layout::finalize calls set_address, but need not
76 // be valid before then.
80 gold_assert(this->is_data_size_valid_
);
81 return this->data_size_
;
84 // Get the current data size.
86 current_data_size() const
87 { return this->current_data_size_for_child(); }
89 // Return true if data size is fixed.
91 is_data_size_fixed() const
92 { return this->is_data_size_fixed_
; }
94 // Return the file offset. This is only valid after
95 // Layout::finalize is finished. For some non-allocated sections,
96 // it may not be valid until near the end of the link.
100 gold_assert(this->is_offset_valid_
);
101 return this->offset_
;
104 // Reset the address and file offset. This essentially disables the
105 // sanity testing about duplicate and unknown settings.
107 reset_address_and_file_offset()
109 this->is_address_valid_
= false;
110 this->is_offset_valid_
= false;
111 if (!this->is_data_size_fixed_
)
112 this->is_data_size_valid_
= false;
113 this->do_reset_address_and_file_offset();
116 // Return true if address and file offset already have reset values. In
117 // other words, calling reset_address_and_file_offset will not change them.
119 address_and_file_offset_have_reset_values() const
120 { return this->do_address_and_file_offset_have_reset_values(); }
122 // Return the required alignment.
125 { return this->do_addralign(); }
127 // Return whether this has a load address.
129 has_load_address() const
130 { return this->do_has_load_address(); }
132 // Return the load address.
135 { return this->do_load_address(); }
137 // Return whether this is an Output_section.
140 { return this->do_is_section(); }
142 // Return whether this is an Output_section of the specified type.
144 is_section_type(elfcpp::Elf_Word stt
) const
145 { return this->do_is_section_type(stt
); }
147 // Return whether this is an Output_section with the specified flag
150 is_section_flag_set(elfcpp::Elf_Xword shf
) const
151 { return this->do_is_section_flag_set(shf
); }
153 // Return the output section that this goes in, if there is one.
156 { return this->do_output_section(); }
158 const Output_section
*
159 output_section() const
160 { return this->do_output_section(); }
162 // Return the output section index, if there is an output section.
165 { return this->do_out_shndx(); }
167 // Set the output section index, if this is an output section.
169 set_out_shndx(unsigned int shndx
)
170 { this->do_set_out_shndx(shndx
); }
172 // Set the address and file offset of this data, and finalize the
173 // size of the data. This is called during Layout::finalize for
174 // allocated sections.
176 set_address_and_file_offset(uint64_t addr
, off_t off
)
178 this->set_address(addr
);
179 this->set_file_offset(off
);
180 this->finalize_data_size();
185 set_address(uint64_t addr
)
187 gold_assert(!this->is_address_valid_
);
188 this->address_
= addr
;
189 this->is_address_valid_
= true;
192 // Set the file offset.
194 set_file_offset(off_t off
)
196 gold_assert(!this->is_offset_valid_
);
198 this->is_offset_valid_
= true;
201 // Update the data size without finalizing it.
203 pre_finalize_data_size()
205 if (!this->is_data_size_valid_
)
207 // Tell the child class to update the data size.
208 this->update_data_size();
212 // Finalize the data size.
216 if (!this->is_data_size_valid_
)
218 // Tell the child class to set the data size.
219 this->set_final_data_size();
220 gold_assert(this->is_data_size_valid_
);
224 // Set the TLS offset. Called only for SHT_TLS sections.
226 set_tls_offset(uint64_t tls_base
)
227 { this->do_set_tls_offset(tls_base
); }
229 // Return the TLS offset, relative to the base of the TLS segment.
230 // Valid only for SHT_TLS sections.
233 { return this->do_tls_offset(); }
235 // Write the data to the output file. This is called after
236 // Layout::finalize is complete.
238 write(Output_file
* file
)
239 { this->do_write(file
); }
241 // This is called by Layout::finalize to note that the sizes of
242 // allocated sections must now be fixed.
245 { Output_data::allocated_sizes_are_fixed
= true; }
247 // Used to check that layout has been done.
250 { return Output_data::allocated_sizes_are_fixed
; }
252 // Note that a dynamic reloc has been applied to this data.
255 { this->has_dynamic_reloc_
= true; }
257 // Return whether a dynamic reloc has been applied.
259 has_dynamic_reloc() const
260 { return this->has_dynamic_reloc_
; }
262 // Whether the address is valid.
264 is_address_valid() const
265 { return this->is_address_valid_
; }
267 // Whether the file offset is valid.
269 is_offset_valid() const
270 { return this->is_offset_valid_
; }
272 // Whether the data size is valid.
274 is_data_size_valid() const
275 { return this->is_data_size_valid_
; }
277 // Print information to the map file.
279 print_to_mapfile(Mapfile
* mapfile
) const
280 { return this->do_print_to_mapfile(mapfile
); }
283 // Functions that child classes may or in some cases must implement.
285 // Write the data to the output file.
287 do_write(Output_file
*) = 0;
289 // Return the required alignment.
291 do_addralign() const = 0;
293 // Return whether this has a load address.
295 do_has_load_address() const
298 // Return the load address.
300 do_load_address() const
301 { gold_unreachable(); }
303 // Return whether this is an Output_section.
305 do_is_section() const
308 // Return whether this is an Output_section of the specified type.
309 // This only needs to be implement by Output_section.
311 do_is_section_type(elfcpp::Elf_Word
) const
314 // Return whether this is an Output_section with the specific flag
315 // set. This only needs to be implemented by Output_section.
317 do_is_section_flag_set(elfcpp::Elf_Xword
) const
320 // Return the output section, if there is one.
321 virtual Output_section
*
325 virtual const Output_section
*
326 do_output_section() const
329 // Return the output section index, if there is an output section.
332 { gold_unreachable(); }
334 // Set the output section index, if this is an output section.
336 do_set_out_shndx(unsigned int)
337 { gold_unreachable(); }
339 // This is a hook for derived classes to set the preliminary data size.
340 // This is called by pre_finalize_data_size, normally called during
341 // Layout::finalize, before the section address is set, and is used
342 // during an incremental update, when we need to know the size of a
343 // section before allocating space in the output file. For classes
344 // where the current data size is up to date, this default version of
345 // the method can be inherited.
350 // This is a hook for derived classes to set the data size. This is
351 // called by finalize_data_size, normally called during
352 // Layout::finalize, when the section address is set.
354 set_final_data_size()
355 { gold_unreachable(); }
357 // A hook for resetting the address and file offset.
359 do_reset_address_and_file_offset()
362 // Return true if address and file offset already have reset values. In
363 // other words, calling reset_address_and_file_offset will not change them.
364 // A child class overriding do_reset_address_and_file_offset may need to
365 // also override this.
367 do_address_and_file_offset_have_reset_values() const
368 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
370 // Set the TLS offset. Called only for SHT_TLS sections.
372 do_set_tls_offset(uint64_t)
373 { gold_unreachable(); }
375 // Return the TLS offset, relative to the base of the TLS segment.
376 // Valid only for SHT_TLS sections.
378 do_tls_offset() const
379 { gold_unreachable(); }
381 // Print to the map file. This only needs to be implemented by
382 // classes which may appear in a PT_LOAD segment.
384 do_print_to_mapfile(Mapfile
*) const
385 { gold_unreachable(); }
387 // Functions that child classes may call.
389 // Reset the address. The Output_section class needs this when an
390 // SHF_ALLOC input section is added to an output section which was
391 // formerly not SHF_ALLOC.
393 mark_address_invalid()
394 { this->is_address_valid_
= false; }
396 // Set the size of the data.
398 set_data_size(off_t data_size
)
400 gold_assert(!this->is_data_size_valid_
401 && !this->is_data_size_fixed_
);
402 this->data_size_
= data_size
;
403 this->is_data_size_valid_
= true;
406 // Fix the data size. Once it is fixed, it cannot be changed
407 // and the data size remains always valid.
411 gold_assert(this->is_data_size_valid_
);
412 this->is_data_size_fixed_
= true;
415 // Get the current data size--this is for the convenience of
416 // sections which build up their size over time.
418 current_data_size_for_child() const
419 { return this->data_size_
; }
421 // Set the current data size--this is for the convenience of
422 // sections which build up their size over time.
424 set_current_data_size_for_child(off_t data_size
)
426 gold_assert(!this->is_data_size_valid_
);
427 this->data_size_
= data_size
;
430 // Return default alignment for the target size.
434 // Return default alignment for a specified size--32 or 64.
436 default_alignment_for_size(int size
);
439 Output_data(const Output_data
&);
440 Output_data
& operator=(const Output_data
&);
442 // This is used for verification, to make sure that we don't try to
443 // change any sizes of allocated sections after we set the section
445 static bool allocated_sizes_are_fixed
;
447 // Memory address in output file.
449 // Size of data in output file.
451 // File offset of contents in output file.
453 // Whether address_ is valid.
454 bool is_address_valid_
: 1;
455 // Whether data_size_ is valid.
456 bool is_data_size_valid_
: 1;
457 // Whether offset_ is valid.
458 bool is_offset_valid_
: 1;
459 // Whether data size is fixed.
460 bool is_data_size_fixed_
: 1;
461 // Whether any dynamic relocs have been applied to this section.
462 bool has_dynamic_reloc_
: 1;
465 // Output the section headers.
467 class Output_section_headers
: public Output_data
470 Output_section_headers(const Layout
*,
471 const Layout::Segment_list
*,
472 const Layout::Section_list
*,
473 const Layout::Section_list
*,
475 const Output_section
*);
478 // Write the data to the file.
480 do_write(Output_file
*);
482 // Return the required alignment.
485 { return Output_data::default_alignment(); }
487 // Write to a map file.
489 do_print_to_mapfile(Mapfile
* mapfile
) const
490 { mapfile
->print_output_data(this, _("** section headers")); }
492 // Update the data size.
495 { this->set_data_size(this->do_size()); }
497 // Set final data size.
499 set_final_data_size()
500 { this->set_data_size(this->do_size()); }
503 // Write the data to the file with the right size and endianness.
504 template<int size
, bool big_endian
>
506 do_sized_write(Output_file
*);
508 // Compute data size.
512 const Layout
* layout_
;
513 const Layout::Segment_list
* segment_list_
;
514 const Layout::Section_list
* section_list_
;
515 const Layout::Section_list
* unattached_section_list_
;
516 const Stringpool
* secnamepool_
;
517 const Output_section
* shstrtab_section_
;
520 // Output the segment headers.
522 class Output_segment_headers
: public Output_data
525 Output_segment_headers(const Layout::Segment_list
& segment_list
);
528 // Write the data to the file.
530 do_write(Output_file
*);
532 // Return the required alignment.
535 { return Output_data::default_alignment(); }
537 // Write to a map file.
539 do_print_to_mapfile(Mapfile
* mapfile
) const
540 { mapfile
->print_output_data(this, _("** segment headers")); }
542 // Set final data size.
544 set_final_data_size()
545 { this->set_data_size(this->do_size()); }
548 // Write the data to the file with the right size and endianness.
549 template<int size
, bool big_endian
>
551 do_sized_write(Output_file
*);
553 // Compute the current size.
557 const Layout::Segment_list
& segment_list_
;
560 // Output the ELF file header.
562 class Output_file_header
: public Output_data
565 Output_file_header(const Target
*,
567 const Output_segment_headers
*,
570 // Add information about the section headers. We lay out the ELF
571 // file header before we create the section headers.
572 void set_section_info(const Output_section_headers
*,
573 const Output_section
* shstrtab
);
576 // Write the data to the file.
578 do_write(Output_file
*);
580 // Return the required alignment.
583 { return Output_data::default_alignment(); }
585 // Write to a map file.
587 do_print_to_mapfile(Mapfile
* mapfile
) const
588 { mapfile
->print_output_data(this, _("** file header")); }
590 // Set final data size.
592 set_final_data_size(void)
593 { this->set_data_size(this->do_size()); }
596 // Write the data to the file with the right size and endianness.
597 template<int size
, bool big_endian
>
599 do_sized_write(Output_file
*);
601 // Return the value to use for the entry address.
603 typename
elfcpp::Elf_types
<size
>::Elf_Addr
606 // Compute the current data size.
610 const Target
* target_
;
611 const Symbol_table
* symtab_
;
612 const Output_segment_headers
* segment_header_
;
613 const Output_section_headers
* section_header_
;
614 const Output_section
* shstrtab_
;
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.
623 class Output_section_data
: public Output_data
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
)
630 this->set_data_size(data_size
);
631 if (is_data_size_fixed
)
632 this->fix_data_size();
635 Output_section_data(uint64_t addralign
)
636 : Output_data(), output_section_(NULL
), addralign_(addralign
)
639 // Return the output section.
642 { return this->output_section_
; }
644 const Output_section
*
645 output_section() const
646 { return this->output_section_
; }
648 // Record the output section.
650 set_output_section(Output_section
* os
);
652 // Add an input section, for SHF_MERGE sections. This returns true
653 // if the section was handled.
655 add_input_section(Relobj
* object
, unsigned int shndx
)
656 { return this->do_add_input_section(object
, shndx
); }
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.
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
); }
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.
674 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
675 { return this->do_is_merge_section_for(object
, shndx
); }
677 // Write the contents to a buffer. This is used for sections which
678 // require postprocessing, such as compression.
680 write_to_buffer(unsigned char* buffer
)
681 { this->do_write_to_buffer(buffer
); }
683 // Print merge stats to stderr. This should only be called for
684 // SHF_MERGE sections.
686 print_merge_stats(const char* section_name
)
687 { this->do_print_merge_stats(section_name
); }
690 // The child class must implement do_write.
692 // The child class may implement specific adjustments to the output
695 do_adjust_output_section(Output_section
*)
698 // May be implemented by child class. Return true if the section
701 do_add_input_section(Relobj
*, unsigned int)
702 { gold_unreachable(); }
704 // The child class may implement output_offset.
706 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
707 section_offset_type
*) const
710 // The child class may implement is_merge_section_for.
712 do_is_merge_section_for(const Relobj
*, unsigned int) const
715 // The child class may implement write_to_buffer. Most child
716 // classes can not appear in a compressed section, and they do not
719 do_write_to_buffer(unsigned char*)
720 { gold_unreachable(); }
722 // Print merge statistics.
724 do_print_merge_stats(const char*)
725 { gold_unreachable(); }
727 // Return the required alignment.
730 { return this->addralign_
; }
732 // Return the output section.
735 { return this->output_section_
; }
737 const Output_section
*
738 do_output_section() const
739 { return this->output_section_
; }
741 // Return the section index of the output section.
743 do_out_shndx() const;
745 // Set the alignment.
747 set_addralign(uint64_t addralign
);
750 // The output section for this section.
751 Output_section
* output_section_
;
752 // The required alignment.
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
760 class Output_section_data_build
: public Output_section_data
763 Output_section_data_build(uint64_t addralign
)
764 : Output_section_data(addralign
)
767 // Set the current data size.
769 set_current_data_size(off_t data_size
)
770 { this->set_current_data_size_for_child(data_size
); }
773 // Set the final data size.
775 set_final_data_size()
776 { this->set_data_size(this->current_data_size_for_child()); }
779 // A simple case of Output_data in which we have constant data to
782 class Output_data_const
: public Output_section_data
785 Output_data_const(const std::string
& data
, uint64_t addralign
)
786 : Output_section_data(data
.size(), addralign
, true), data_(data
)
789 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
790 : Output_section_data(len
, addralign
, true), data_(p
, len
)
793 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
794 : Output_section_data(len
, addralign
, true),
795 data_(reinterpret_cast<const char*>(p
), len
)
799 // Write the data to the output file.
801 do_write(Output_file
*);
803 // Write the data to a buffer.
805 do_write_to_buffer(unsigned char* buffer
)
806 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
808 // Write to a map file.
810 do_print_to_mapfile(Mapfile
* mapfile
) const
811 { mapfile
->print_output_data(this, _("** fill")); }
817 // Another version of Output_data with constant data, in which the
818 // buffer is allocated by the caller.
820 class Output_data_const_buffer
: public Output_section_data
823 Output_data_const_buffer(const unsigned char* p
, off_t len
,
824 uint64_t addralign
, const char* map_name
)
825 : Output_section_data(len
, addralign
, true),
826 p_(p
), map_name_(map_name
)
830 // Write the data the output file.
832 do_write(Output_file
*);
834 // Write the data to a buffer.
836 do_write_to_buffer(unsigned char* buffer
)
837 { memcpy(buffer
, this->p_
, this->data_size()); }
839 // Write to a map file.
841 do_print_to_mapfile(Mapfile
* mapfile
) const
842 { mapfile
->print_output_data(this, _(this->map_name_
)); }
845 // The data to output.
846 const unsigned char* p_
;
847 // Name to use in a map file. Maps are a rarely used feature, but
848 // the space usage is minor as aren't very many of these objects.
849 const char* map_name_
;
852 // A place holder for a fixed amount of data written out via some
855 class Output_data_fixed_space
: public Output_section_data
858 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
859 const char* map_name
)
860 : Output_section_data(data_size
, addralign
, true),
865 // Write out the data--the actual data must be written out
868 do_write(Output_file
*)
871 // Write to a map file.
873 do_print_to_mapfile(Mapfile
* mapfile
) const
874 { mapfile
->print_output_data(this, _(this->map_name_
)); }
877 // Name to use in a map file. Maps are a rarely used feature, but
878 // the space usage is minor as aren't very many of these objects.
879 const char* map_name_
;
882 // A place holder for variable sized data written out via some other
885 class Output_data_space
: public Output_section_data_build
888 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
889 : Output_section_data_build(addralign
),
893 // Set the alignment.
895 set_space_alignment(uint64_t align
)
896 { this->set_addralign(align
); }
899 // Write out the data--the actual data must be written out
902 do_write(Output_file
*)
905 // Write to a map file.
907 do_print_to_mapfile(Mapfile
* mapfile
) const
908 { mapfile
->print_output_data(this, _(this->map_name_
)); }
911 // Name to use in a map file. Maps are a rarely used feature, but
912 // the space usage is minor as aren't very many of these objects.
913 const char* map_name_
;
916 // Fill fixed space with zeroes. This is just like
917 // Output_data_fixed_space, except that the map name is known.
919 class Output_data_zero_fill
: public Output_section_data
922 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
923 : Output_section_data(data_size
, addralign
, true)
927 // There is no data to write out.
929 do_write(Output_file
*)
932 // Write to a map file.
934 do_print_to_mapfile(Mapfile
* mapfile
) const
935 { mapfile
->print_output_data(this, "** zero fill"); }
938 // A string table which goes into an output section.
940 class Output_data_strtab
: public Output_section_data
943 Output_data_strtab(Stringpool
* strtab
)
944 : Output_section_data(1), strtab_(strtab
)
948 // This is called to update the section size prior to assigning
949 // the address and file offset.
952 { this->set_final_data_size(); }
954 // This is called to set the address and file offset. Here we make
955 // sure that the Stringpool is finalized.
957 set_final_data_size();
959 // Write out the data.
961 do_write(Output_file
*);
963 // Write the data to a buffer.
965 do_write_to_buffer(unsigned char* buffer
)
966 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
968 // Write to a map file.
970 do_print_to_mapfile(Mapfile
* mapfile
) const
971 { mapfile
->print_output_data(this, _("** string table")); }
977 // This POD class is used to represent a single reloc in the output
978 // file. This could be a private class within Output_data_reloc, but
979 // the templatization is complex enough that I broke it out into a
980 // separate class. The class is templatized on either elfcpp::SHT_REL
981 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
982 // relocation or an ordinary relocation.
984 // A relocation can be against a global symbol, a local symbol, a
985 // local section symbol, an output section, or the undefined symbol at
986 // index 0. We represent the latter by using a NULL global symbol.
988 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
991 template<bool dynamic
, int size
, bool big_endian
>
992 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
995 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
996 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
998 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1000 // An uninitialized entry. We need this because we want to put
1001 // instances of this class into an STL container.
1003 : local_sym_index_(INVALID_CODE
)
1006 // We have a bunch of different constructors. They come in pairs
1007 // depending on how the address of the relocation is specified. It
1008 // can either be an offset in an Output_data or an offset in an
1011 // A reloc against a global symbol.
1013 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1014 Address address
, bool is_relative
, bool is_symbolless
);
1016 Output_reloc(Symbol
* gsym
, unsigned int type
,
1017 Sized_relobj
<size
, big_endian
>* relobj
,
1018 unsigned int shndx
, Address address
, bool is_relative
,
1019 bool is_symbolless
);
1021 // A reloc against a local symbol or local section symbol.
1023 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1024 unsigned int local_sym_index
, unsigned int type
,
1025 Output_data
* od
, Address address
, bool is_relative
,
1026 bool is_symbolless
, bool is_section_symbol
);
1028 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1029 unsigned int local_sym_index
, unsigned int type
,
1030 unsigned int shndx
, Address address
, bool is_relative
,
1031 bool is_symbolless
, bool is_section_symbol
);
1033 // A reloc against the STT_SECTION symbol of an output section.
1035 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1038 Output_reloc(Output_section
* os
, unsigned int type
,
1039 Sized_relobj
<size
, big_endian
>* relobj
,
1040 unsigned int shndx
, Address address
);
1042 // An absolute relocation with no symbol.
1044 Output_reloc(unsigned int type
, Output_data
* od
, Address address
);
1046 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1047 unsigned int shndx
, Address address
);
1049 // A target specific relocation. The target will be called to get
1050 // the symbol index, passing ARG. The type and offset will be set
1051 // as for other relocation types.
1053 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1056 Output_reloc(unsigned int type
, void* arg
,
1057 Sized_relobj
<size
, big_endian
>* relobj
,
1058 unsigned int shndx
, Address address
);
1060 // Return the reloc type.
1063 { return this->type_
; }
1065 // Return whether this is a RELATIVE relocation.
1068 { return this->is_relative_
; }
1070 // Return whether this is a relocation which should not use
1071 // a symbol, but which obtains its addend from a symbol.
1073 is_symbolless() const
1074 { return this->is_symbolless_
; }
1076 // Return whether this is against a local section symbol.
1078 is_local_section_symbol() const
1080 return (this->local_sym_index_
!= GSYM_CODE
1081 && this->local_sym_index_
!= SECTION_CODE
1082 && this->local_sym_index_
!= INVALID_CODE
1083 && this->local_sym_index_
!= TARGET_CODE
1084 && this->is_section_symbol_
);
1087 // Return whether this is a target specific relocation.
1089 is_target_specific() const
1090 { return this->local_sym_index_
== TARGET_CODE
; }
1092 // Return the argument to pass to the target for a target specific
1097 gold_assert(this->local_sym_index_
== TARGET_CODE
);
1098 return this->u1_
.arg
;
1101 // For a local section symbol, return the offset of the input
1102 // section within the output section. ADDEND is the addend being
1103 // applied to the input section.
1105 local_section_offset(Addend addend
) const;
1107 // Get the value of the symbol referred to by a Rel relocation when
1108 // we are adding the given ADDEND.
1110 symbol_value(Addend addend
) const;
1112 // Write the reloc entry to an output view.
1114 write(unsigned char* pov
) const;
1116 // Write the offset and info fields to Write_rel.
1117 template<typename Write_rel
>
1118 void write_rel(Write_rel
*) const;
1120 // This is used when sorting dynamic relocs. Return -1 to sort this
1121 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1123 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1126 // Return whether this reloc should be sorted before the argument
1127 // when sorting dynamic relocs.
1129 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1131 { return this->compare(r2
) < 0; }
1134 // Record that we need a dynamic symbol index.
1136 set_needs_dynsym_index();
1138 // Return the symbol index.
1140 get_symbol_index() const;
1142 // Return the output address.
1144 get_address() const;
1146 // Codes for local_sym_index_.
1155 // Invalid uninitialized entry.
1161 // For a local symbol or local section symbol
1162 // (this->local_sym_index_ >= 0), the object. We will never
1163 // generate a relocation against a local symbol in a dynamic
1164 // object; that doesn't make sense. And our callers will always
1165 // be templatized, so we use Sized_relobj here.
1166 Sized_relobj
<size
, big_endian
>* relobj
;
1167 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1168 // symbol. If this is NULL, it indicates a relocation against the
1169 // undefined 0 symbol.
1171 // For a relocation against an output section
1172 // (this->local_sym_index_ == SECTION_CODE), the output section.
1174 // For a target specific relocation, an argument to pass to the
1180 // If this->shndx_ is not INVALID CODE, the object which holds the
1181 // input section being used to specify the reloc address.
1182 Sized_relobj
<size
, big_endian
>* relobj
;
1183 // If this->shndx_ is INVALID_CODE, the output data being used to
1184 // specify the reloc address. This may be NULL if the reloc
1185 // address is absolute.
1188 // The address offset within the input section or the Output_data.
1190 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1191 // relocation against an output section, or TARGET_CODE for a target
1192 // specific relocation, or INVALID_CODE for an uninitialized value.
1193 // Otherwise, for a local symbol (this->is_section_symbol_ is
1194 // false), the local symbol index. For a local section symbol
1195 // (this->is_section_symbol_ is true), the section index in the
1197 unsigned int local_sym_index_
;
1198 // The reloc type--a processor specific code.
1199 unsigned int type_
: 29;
1200 // True if the relocation is a RELATIVE relocation.
1201 bool is_relative_
: 1;
1202 // True if the relocation is one which should not use
1203 // a symbol, but which obtains its addend from a symbol.
1204 bool is_symbolless_
: 1;
1205 // True if the relocation is against a section symbol.
1206 bool is_section_symbol_
: 1;
1207 // If the reloc address is an input section in an object, the
1208 // section index. This is INVALID_CODE if the reloc address is
1209 // specified in some other way.
1210 unsigned int shndx_
;
1213 // The SHT_RELA version of Output_reloc<>. This is just derived from
1214 // the SHT_REL version of Output_reloc, but it adds an addend.
1216 template<bool dynamic
, int size
, bool big_endian
>
1217 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1220 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1221 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1223 // An uninitialized entry.
1228 // A reloc against a global symbol.
1230 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1231 Address address
, Addend addend
, bool is_relative
,
1233 : rel_(gsym
, type
, od
, address
, is_relative
, is_symbolless
),
1237 Output_reloc(Symbol
* gsym
, unsigned int type
,
1238 Sized_relobj
<size
, big_endian
>* relobj
,
1239 unsigned int shndx
, Address address
, Addend addend
,
1240 bool is_relative
, bool is_symbolless
)
1241 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
,
1242 is_symbolless
), addend_(addend
)
1245 // A reloc against a local symbol.
1247 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1248 unsigned int local_sym_index
, unsigned int type
,
1249 Output_data
* od
, Address address
,
1250 Addend addend
, bool is_relative
,
1251 bool is_symbolless
, bool is_section_symbol
)
1252 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1253 is_symbolless
, is_section_symbol
),
1257 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1258 unsigned int local_sym_index
, unsigned int type
,
1259 unsigned int shndx
, Address address
,
1260 Addend addend
, bool is_relative
,
1261 bool is_symbolless
, bool is_section_symbol
)
1262 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1263 is_symbolless
, is_section_symbol
),
1267 // A reloc against the STT_SECTION symbol of an output section.
1269 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1270 Address address
, Addend addend
)
1271 : rel_(os
, type
, od
, address
), addend_(addend
)
1274 Output_reloc(Output_section
* os
, unsigned int type
,
1275 Sized_relobj
<size
, big_endian
>* relobj
,
1276 unsigned int shndx
, Address address
, Addend addend
)
1277 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1280 // An absolute relocation with no symbol.
1282 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1284 : rel_(type
, od
, address
), addend_(addend
)
1287 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1288 unsigned int shndx
, Address address
, Addend addend
)
1289 : rel_(type
, relobj
, shndx
, address
), addend_(addend
)
1292 // A target specific relocation. The target will be called to get
1293 // the symbol index and the addend, passing ARG. The type and
1294 // offset will be set as for other relocation types.
1296 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1297 Address address
, Addend addend
)
1298 : rel_(type
, arg
, od
, address
), addend_(addend
)
1301 Output_reloc(unsigned int type
, void* arg
,
1302 Sized_relobj
<size
, big_endian
>* relobj
,
1303 unsigned int shndx
, Address address
, Addend addend
)
1304 : rel_(type
, arg
, relobj
, shndx
, address
), addend_(addend
)
1307 // Return whether this is a RELATIVE relocation.
1310 { return this->rel_
.is_relative(); }
1312 // Return whether this is a relocation which should not use
1313 // a symbol, but which obtains its addend from a symbol.
1315 is_symbolless() const
1316 { return this->rel_
.is_symbolless(); }
1318 // Write the reloc entry to an output view.
1320 write(unsigned char* pov
) const;
1322 // Return whether this reloc should be sorted before the argument
1323 // when sorting dynamic relocs.
1325 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1328 int i
= this->rel_
.compare(r2
.rel_
);
1334 return this->addend_
< r2
.addend_
;
1339 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1344 // Output_data_reloc_generic is a non-template base class for
1345 // Output_data_reloc_base. This gives the generic code a way to hold
1346 // a pointer to a reloc section.
1348 class Output_data_reloc_generic
: public Output_section_data_build
1351 Output_data_reloc_generic(int size
, bool sort_relocs
)
1352 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1353 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1356 // Return the number of relative relocs in this section.
1358 relative_reloc_count() const
1359 { return this->relative_reloc_count_
; }
1361 // Whether we should sort the relocs.
1364 { return this->sort_relocs_
; }
1367 // Note that we've added another relative reloc.
1369 bump_relative_reloc_count()
1370 { ++this->relative_reloc_count_
; }
1373 // The number of relative relocs added to this section. This is to
1374 // support DT_RELCOUNT.
1375 size_t relative_reloc_count_
;
1376 // Whether to sort the relocations when writing them out, to make
1377 // the dynamic linker more efficient.
1381 // Output_data_reloc is used to manage a section containing relocs.
1382 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1383 // indicates whether this is a dynamic relocation or a normal
1384 // relocation. Output_data_reloc_base is a base class.
1385 // Output_data_reloc is the real class, which we specialize based on
1388 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1389 class Output_data_reloc_base
: public Output_data_reloc_generic
1392 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1393 typedef typename
Output_reloc_type::Address Address
;
1394 static const int reloc_size
=
1395 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1397 // Construct the section.
1398 Output_data_reloc_base(bool sort_relocs
)
1399 : Output_data_reloc_generic(size
, sort_relocs
)
1403 // Write out the data.
1405 do_write(Output_file
*);
1407 // Set the entry size and the link.
1409 do_adjust_output_section(Output_section
* os
);
1411 // Write to a map file.
1413 do_print_to_mapfile(Mapfile
* mapfile
) const
1415 mapfile
->print_output_data(this,
1417 ? _("** dynamic relocs")
1421 // Add a relocation entry.
1423 add(Output_data
* od
, const Output_reloc_type
& reloc
)
1425 this->relocs_
.push_back(reloc
);
1426 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1427 od
->add_dynamic_reloc();
1428 if (reloc
.is_relative())
1429 this->bump_relative_reloc_count();
1433 typedef std::vector
<Output_reloc_type
> Relocs
;
1435 // The class used to sort the relocations.
1436 struct Sort_relocs_comparison
1439 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1440 { return r1
.sort_before(r2
); }
1443 // The relocations in this section.
1447 // The class which callers actually create.
1449 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1450 class Output_data_reloc
;
1452 // The SHT_REL version of Output_data_reloc.
1454 template<bool dynamic
, int size
, bool big_endian
>
1455 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1456 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1459 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1463 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1464 typedef typename
Output_reloc_type::Address Address
;
1466 Output_data_reloc(bool sr
)
1467 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1470 // Add a reloc against a global symbol.
1473 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1474 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, false)); }
1477 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1478 Sized_relobj
<size
, big_endian
>* relobj
,
1479 unsigned int shndx
, Address address
)
1480 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1483 // These are to simplify the Copy_relocs class.
1486 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1489 gold_assert(addend
== 0);
1490 this->add_global(gsym
, type
, od
, address
);
1494 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1495 Sized_relobj
<size
, big_endian
>* relobj
,
1496 unsigned int shndx
, Address address
, Address addend
)
1498 gold_assert(addend
== 0);
1499 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1502 // Add a RELATIVE reloc against a global symbol. The final relocation
1503 // will not reference the symbol.
1506 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1508 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true, true)); }
1511 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1512 Sized_relobj
<size
, big_endian
>* relobj
,
1513 unsigned int shndx
, Address address
)
1515 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1519 // Add a global relocation which does not use a symbol for the relocation,
1520 // but which gets its addend from a symbol.
1523 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1524 Output_data
* od
, Address address
)
1525 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, true)); }
1528 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1530 Sized_relobj
<size
, big_endian
>* relobj
,
1531 unsigned int shndx
, Address address
)
1533 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1537 // Add a reloc against a local symbol.
1540 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1541 unsigned int local_sym_index
, unsigned int type
,
1542 Output_data
* od
, Address address
)
1544 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1545 address
, false, false, false));
1549 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1550 unsigned int local_sym_index
, unsigned int type
,
1551 Output_data
* od
, unsigned int shndx
, Address address
)
1553 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1554 address
, false, false, false));
1557 // Add a RELATIVE reloc against a local symbol.
1560 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1561 unsigned int local_sym_index
, unsigned int type
,
1562 Output_data
* od
, Address address
)
1564 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1565 address
, true, true, false));
1569 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1570 unsigned int local_sym_index
, unsigned int type
,
1571 Output_data
* od
, unsigned int shndx
, Address address
)
1573 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1574 address
, true, true, false));
1577 // Add a local relocation which does not use a symbol for the relocation,
1578 // but which gets its addend from a symbol.
1581 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1582 unsigned int local_sym_index
, unsigned int type
,
1583 Output_data
* od
, Address address
)
1585 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1586 address
, false, true, false));
1590 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1591 unsigned int local_sym_index
, unsigned int type
,
1592 Output_data
* od
, unsigned int shndx
,
1595 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1596 address
, false, true, false));
1599 // Add a reloc against a local section symbol. This will be
1600 // converted into a reloc against the STT_SECTION symbol of the
1604 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1605 unsigned int input_shndx
, unsigned int type
,
1606 Output_data
* od
, Address address
)
1608 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1609 address
, false, false, true));
1613 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1614 unsigned int input_shndx
, unsigned int type
,
1615 Output_data
* od
, unsigned int shndx
, Address address
)
1617 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1618 address
, false, false, true));
1621 // A reloc against the STT_SECTION symbol of an output section.
1622 // OS is the Output_section that the relocation refers to; OD is
1623 // the Output_data object being relocated.
1626 add_output_section(Output_section
* os
, unsigned int type
,
1627 Output_data
* od
, Address address
)
1628 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1631 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1632 Sized_relobj
<size
, big_endian
>* relobj
,
1633 unsigned int shndx
, Address address
)
1634 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1636 // Add an absolute relocation.
1639 add_absolute(unsigned int type
, Output_data
* od
, Address address
)
1640 { this->add(od
, Output_reloc_type(type
, od
, address
)); }
1643 add_absolute(unsigned int type
, Output_data
* od
,
1644 Sized_relobj
<size
, big_endian
>* relobj
,
1645 unsigned int shndx
, Address address
)
1646 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
)); }
1648 // Add a target specific relocation. A target which calls this must
1649 // define the reloc_symbol_index and reloc_addend virtual functions.
1652 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1654 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
)); }
1657 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1658 Sized_relobj
<size
, big_endian
>* relobj
,
1659 unsigned int shndx
, Address address
)
1660 { this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
)); }
1663 // The SHT_RELA version of Output_data_reloc.
1665 template<bool dynamic
, int size
, bool big_endian
>
1666 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1667 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1670 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1674 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1675 typedef typename
Output_reloc_type::Address Address
;
1676 typedef typename
Output_reloc_type::Addend Addend
;
1678 Output_data_reloc(bool sr
)
1679 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1682 // Add a reloc against a global symbol.
1685 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1686 Address address
, Addend addend
)
1687 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1691 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1692 Sized_relobj
<size
, big_endian
>* relobj
,
1693 unsigned int shndx
, Address address
,
1695 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1696 addend
, false, false)); }
1698 // Add a RELATIVE reloc against a global symbol. The final output
1699 // relocation will not reference the symbol, but we must keep the symbol
1700 // information long enough to set the addend of the relocation correctly
1701 // when it is written.
1704 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1705 Address address
, Addend addend
)
1706 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true,
1710 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1711 Sized_relobj
<size
, big_endian
>* relobj
,
1712 unsigned int shndx
, Address address
, Addend addend
)
1713 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1714 addend
, true, true)); }
1716 // Add a global relocation which does not use a symbol for the relocation,
1717 // but which gets its addend from a symbol.
1720 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1721 Address address
, Addend addend
)
1722 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1726 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1728 Sized_relobj
<size
, big_endian
>* relobj
,
1729 unsigned int shndx
, Address address
, Addend addend
)
1730 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1731 addend
, false, true)); }
1733 // Add a reloc against a local symbol.
1736 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1737 unsigned int local_sym_index
, unsigned int type
,
1738 Output_data
* od
, Address address
, Addend addend
)
1740 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1741 addend
, false, false, false));
1745 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1746 unsigned int local_sym_index
, unsigned int type
,
1747 Output_data
* od
, unsigned int shndx
, Address address
,
1750 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1751 address
, addend
, false, false, false));
1754 // Add a RELATIVE reloc against a local symbol.
1757 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1758 unsigned int local_sym_index
, unsigned int type
,
1759 Output_data
* od
, Address address
, Addend addend
)
1761 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1762 addend
, true, true, false));
1766 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1767 unsigned int local_sym_index
, unsigned int type
,
1768 Output_data
* od
, unsigned int shndx
, Address address
,
1771 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1772 address
, addend
, true, true, false));
1775 // Add a local relocation which does not use a symbol for the relocation,
1776 // but which gets it's addend from a symbol.
1779 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1780 unsigned int local_sym_index
, unsigned int type
,
1781 Output_data
* od
, Address address
, Addend addend
)
1783 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1784 addend
, false, true, false));
1788 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1789 unsigned int local_sym_index
, unsigned int type
,
1790 Output_data
* od
, unsigned int shndx
,
1791 Address address
, Addend addend
)
1793 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1794 address
, addend
, false, true, false));
1797 // Add a reloc against a local section symbol. This will be
1798 // converted into a reloc against the STT_SECTION symbol of the
1802 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1803 unsigned int input_shndx
, unsigned int type
,
1804 Output_data
* od
, Address address
, Addend addend
)
1806 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1807 addend
, false, false, true));
1811 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1812 unsigned int input_shndx
, unsigned int type
,
1813 Output_data
* od
, unsigned int shndx
, Address address
,
1816 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1817 address
, addend
, false, false, true));
1820 // A reloc against the STT_SECTION symbol of an output section.
1823 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1824 Address address
, Addend addend
)
1825 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1828 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1829 Sized_relobj
<size
, big_endian
>* relobj
,
1830 unsigned int shndx
, Address address
, Addend addend
)
1831 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1834 // Add an absolute relocation.
1837 add_absolute(unsigned int type
, Output_data
* od
, Address address
,
1839 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
)); }
1842 add_absolute(unsigned int type
, Output_data
* od
,
1843 Sized_relobj
<size
, big_endian
>* relobj
,
1844 unsigned int shndx
, Address address
, Addend addend
)
1845 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
)); }
1847 // Add a target specific relocation. A target which calls this must
1848 // define the reloc_symbol_index and reloc_addend virtual functions.
1851 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1852 Address address
, Addend addend
)
1853 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
, addend
)); }
1856 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1857 Sized_relobj
<size
, big_endian
>* relobj
,
1858 unsigned int shndx
, Address address
, Addend addend
)
1860 this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
,
1865 // Output_relocatable_relocs represents a relocation section in a
1866 // relocatable link. The actual data is written out in the target
1867 // hook relocate_for_relocatable. This just saves space for it.
1869 template<int sh_type
, int size
, bool big_endian
>
1870 class Output_relocatable_relocs
: public Output_section_data
1873 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1874 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1879 set_final_data_size();
1881 // Write out the data. There is nothing to do here.
1883 do_write(Output_file
*)
1886 // Write to a map file.
1888 do_print_to_mapfile(Mapfile
* mapfile
) const
1889 { mapfile
->print_output_data(this, _("** relocs")); }
1892 // The relocs associated with this input section.
1893 Relocatable_relocs
* rr_
;
1896 // Handle a GROUP section.
1898 template<int size
, bool big_endian
>
1899 class Output_data_group
: public Output_section_data
1902 // The constructor clears *INPUT_SHNDXES.
1903 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1904 section_size_type entry_count
,
1905 elfcpp::Elf_Word flags
,
1906 std::vector
<unsigned int>* input_shndxes
);
1909 do_write(Output_file
*);
1911 // Write to a map file.
1913 do_print_to_mapfile(Mapfile
* mapfile
) const
1914 { mapfile
->print_output_data(this, _("** group")); }
1916 // Set final data size.
1918 set_final_data_size()
1919 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1922 // The input object.
1923 Sized_relobj
<size
, big_endian
>* relobj_
;
1924 // The group flag word.
1925 elfcpp::Elf_Word flags_
;
1926 // The section indexes of the input sections in this group.
1927 std::vector
<unsigned int> input_shndxes_
;
1930 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1931 // for one symbol--either a global symbol or a local symbol in an
1932 // object. The target specific code adds entries to the GOT as
1935 template<int size
, bool big_endian
>
1936 class Output_data_got
: public Output_section_data_build
1939 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1940 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1941 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1944 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1948 // Add an entry for a global symbol to the GOT. Return true if this
1949 // is a new GOT entry, false if the symbol was already in the GOT.
1951 add_global(Symbol
* gsym
, unsigned int got_type
);
1953 // Like add_global, but use the PLT offset of the global symbol if
1956 add_global_plt(Symbol
* gsym
, unsigned int got_type
);
1958 // Add an entry for a global symbol to the GOT, and add a dynamic
1959 // relocation of type R_TYPE for the GOT entry.
1961 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1962 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1965 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1966 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1968 // Add a pair of entries for a global symbol to the GOT, and add
1969 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1971 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1972 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1973 unsigned int r_type_2
);
1976 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1977 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1978 unsigned int r_type_2
);
1980 // Add an entry for a local symbol to the GOT. This returns true if
1981 // this is a new GOT entry, false if the symbol already has a GOT
1984 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1985 unsigned int got_type
);
1987 // Like add_local, but use the PLT offset of the local symbol if it
1990 add_local_plt(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1991 unsigned int got_type
);
1993 // Add an entry for a local symbol to the GOT, and add a dynamic
1994 // relocation of type R_TYPE for the GOT entry.
1996 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1997 unsigned int sym_index
, unsigned int got_type
,
1998 Rel_dyn
* rel_dyn
, unsigned int r_type
);
2001 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
2002 unsigned int sym_index
, unsigned int got_type
,
2003 Rela_dyn
* rela_dyn
, unsigned int r_type
);
2005 // Add a pair of entries for a local symbol to the GOT, and add
2006 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2008 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
2009 unsigned int sym_index
, unsigned int shndx
,
2010 unsigned int got_type
, Rel_dyn
* rel_dyn
,
2011 unsigned int r_type_1
, unsigned int r_type_2
);
2014 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
2015 unsigned int sym_index
, unsigned int shndx
,
2016 unsigned int got_type
, Rela_dyn
* rela_dyn
,
2017 unsigned int r_type_1
, unsigned int r_type_2
);
2019 // Add a constant to the GOT. This returns the offset of the new
2020 // entry from the start of the GOT.
2022 add_constant(Valtype constant
)
2024 this->entries_
.push_back(Got_entry(constant
));
2025 this->set_got_size();
2026 return this->last_got_offset();
2030 // Write out the GOT table.
2032 do_write(Output_file
*);
2034 // Write to a map file.
2036 do_print_to_mapfile(Mapfile
* mapfile
) const
2037 { mapfile
->print_output_data(this, _("** GOT")); }
2040 // This POD class holds a single GOT entry.
2044 // Create a zero entry.
2046 : local_sym_index_(CONSTANT_CODE
), use_plt_offset_(false)
2047 { this->u_
.constant
= 0; }
2049 // Create a global symbol entry.
2050 Got_entry(Symbol
* gsym
, bool use_plt_offset
)
2051 : local_sym_index_(GSYM_CODE
), use_plt_offset_(use_plt_offset
)
2052 { this->u_
.gsym
= gsym
; }
2054 // Create a local symbol entry.
2055 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
2056 unsigned int local_sym_index
, bool use_plt_offset
)
2057 : local_sym_index_(local_sym_index
), use_plt_offset_(use_plt_offset
)
2059 gold_assert(local_sym_index
!= GSYM_CODE
2060 && local_sym_index
!= CONSTANT_CODE
2061 && local_sym_index
== this->local_sym_index_
);
2062 this->u_
.object
= object
;
2065 // Create a constant entry. The constant is a host value--it will
2066 // be swapped, if necessary, when it is written out.
2067 explicit Got_entry(Valtype constant
)
2068 : local_sym_index_(CONSTANT_CODE
), use_plt_offset_(false)
2069 { this->u_
.constant
= constant
; }
2071 // Write the GOT entry to an output view.
2073 write(unsigned char* pov
) const;
2078 GSYM_CODE
= 0x7fffffff,
2079 CONSTANT_CODE
= 0x7ffffffe
2084 // For a local symbol, the object.
2085 Sized_relobj
<size
, big_endian
>* object
;
2086 // For a global symbol, the symbol.
2088 // For a constant, the constant.
2091 // For a local symbol, the local symbol index. This is GSYM_CODE
2092 // for a global symbol, or CONSTANT_CODE for a constant.
2093 unsigned int local_sym_index_
: 31;
2094 // Whether to use the PLT offset of the symbol if it has one.
2095 bool use_plt_offset_
: 1;
2098 typedef std::vector
<Got_entry
> Got_entries
;
2100 // Return the offset into the GOT of GOT entry I.
2102 got_offset(unsigned int i
) const
2103 { return i
* (size
/ 8); }
2105 // Return the offset into the GOT of the last entry added.
2107 last_got_offset() const
2108 { return this->got_offset(this->entries_
.size() - 1); }
2110 // Set the size of the section.
2113 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
2115 // The list of GOT entries.
2116 Got_entries entries_
;
2119 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2122 class Output_data_dynamic
: public Output_section_data
2125 Output_data_dynamic(Stringpool
* pool
)
2126 : Output_section_data(Output_data::default_alignment()),
2127 entries_(), pool_(pool
)
2130 // Add a new dynamic entry with a fixed numeric value.
2132 add_constant(elfcpp::DT tag
, unsigned int val
)
2133 { this->add_entry(Dynamic_entry(tag
, val
)); }
2135 // Add a new dynamic entry with the address of output data.
2137 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
2138 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
2140 // Add a new dynamic entry with the address of output data
2141 // plus a constant offset.
2143 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
2144 unsigned int offset
)
2145 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
2147 // Add a new dynamic entry with the size of output data.
2149 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
2150 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
2152 // Add a new dynamic entry with the total size of two output datas.
2154 add_section_size(elfcpp::DT tag
, const Output_data
* od
,
2155 const Output_data
* od2
)
2156 { this->add_entry(Dynamic_entry(tag
, od
, od2
)); }
2158 // Add a new dynamic entry with the address of a symbol.
2160 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
2161 { this->add_entry(Dynamic_entry(tag
, sym
)); }
2163 // Add a new dynamic entry with a string.
2165 add_string(elfcpp::DT tag
, const char* str
)
2166 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
2169 add_string(elfcpp::DT tag
, const std::string
& str
)
2170 { this->add_string(tag
, str
.c_str()); }
2173 // Adjust the output section to set the entry size.
2175 do_adjust_output_section(Output_section
*);
2177 // Set the final data size.
2179 set_final_data_size();
2181 // Write out the dynamic entries.
2183 do_write(Output_file
*);
2185 // Write to a map file.
2187 do_print_to_mapfile(Mapfile
* mapfile
) const
2188 { mapfile
->print_output_data(this, _("** dynamic")); }
2191 // This POD class holds a single dynamic entry.
2195 // Create an entry with a fixed numeric value.
2196 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
2197 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
2198 { this->u_
.val
= val
; }
2200 // Create an entry with the size or address of a section.
2201 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
2203 offset_(section_size
2204 ? DYNAMIC_SECTION_SIZE
2205 : DYNAMIC_SECTION_ADDRESS
)
2211 // Create an entry with the size of two sections.
2212 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, const Output_data
* od2
)
2214 offset_(DYNAMIC_SECTION_SIZE
)
2220 // Create an entry with the address of a section plus a constant offset.
2221 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
2224 { this->u_
.od
= od
; }
2226 // Create an entry with the address of a symbol.
2227 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
2228 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
2229 { this->u_
.sym
= sym
; }
2231 // Create an entry with a string.
2232 Dynamic_entry(elfcpp::DT tag
, const char* str
)
2233 : tag_(tag
), offset_(DYNAMIC_STRING
)
2234 { this->u_
.str
= str
; }
2236 // Return the tag of this entry.
2239 { return this->tag_
; }
2241 // Write the dynamic entry to an output view.
2242 template<int size
, bool big_endian
>
2244 write(unsigned char* pov
, const Stringpool
*) const;
2247 // Classification is encoded in the OFFSET field.
2251 DYNAMIC_SECTION_ADDRESS
= 0,
2253 DYNAMIC_NUMBER
= -1U,
2255 DYNAMIC_SECTION_SIZE
= -2U,
2257 DYNAMIC_SYMBOL
= -3U,
2259 DYNAMIC_STRING
= -4U
2260 // Any other value indicates a section address plus OFFSET.
2265 // For DYNAMIC_NUMBER.
2267 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2268 const Output_data
* od
;
2269 // For DYNAMIC_SYMBOL.
2271 // For DYNAMIC_STRING.
2274 // For DYNAMIC_SYMBOL with two sections.
2275 const Output_data
* od2
;
2278 // The type of entry (Classification) or offset within a section.
2279 unsigned int offset_
;
2282 // Add an entry to the list.
2284 add_entry(const Dynamic_entry
& entry
)
2285 { this->entries_
.push_back(entry
); }
2287 // Sized version of write function.
2288 template<int size
, bool big_endian
>
2290 sized_write(Output_file
* of
);
2292 // The type of the list of entries.
2293 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2296 Dynamic_entries entries_
;
2297 // The pool used for strings.
2301 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2302 // which may be required if the object file has more than
2303 // SHN_LORESERVE sections.
2305 class Output_symtab_xindex
: public Output_section_data
2308 Output_symtab_xindex(size_t symcount
)
2309 : Output_section_data(symcount
* 4, 4, true),
2313 // Add an entry: symbol number SYMNDX has section SHNDX.
2315 add(unsigned int symndx
, unsigned int shndx
)
2316 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2320 do_write(Output_file
*);
2322 // Write to a map file.
2324 do_print_to_mapfile(Mapfile
* mapfile
) const
2325 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2328 template<bool big_endian
>
2330 endian_do_write(unsigned char*);
2332 // It is likely that most symbols will not require entries. Rather
2333 // than keep a vector for all symbols, we keep pairs of symbol index
2334 // and section index.
2335 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2337 // The entries we need.
2338 Xindex_entries entries_
;
2341 // A relaxed input section.
2342 class Output_relaxed_input_section
: public Output_section_data_build
2345 // We would like to call relobj->section_addralign(shndx) to get the
2346 // alignment but we do not want the constructor to fail. So callers
2347 // are repsonsible for ensuring that.
2348 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2350 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2353 // Return the Relobj of this relaxed input section.
2356 { return this->relobj_
; }
2358 // Return the section index of this relaxed input section.
2361 { return this->shndx_
; }
2365 unsigned int shndx_
;
2368 // This class describes properties of merge data sections. It is used
2369 // as a key type for maps.
2370 class Merge_section_properties
2373 Merge_section_properties(bool is_string
, uint64_t entsize
,
2375 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
2378 // Whether this equals to another Merge_section_properties MSP.
2380 eq(const Merge_section_properties
& msp
) const
2382 return ((this->is_string_
== msp
.is_string_
)
2383 && (this->entsize_
== msp
.entsize_
)
2384 && (this->addralign_
== msp
.addralign_
));
2387 // Compute a hash value for this using 64-bit FNV-1a hash.
2391 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
2392 uint64_t prime
= 1099511628211ULL;
2393 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
2394 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
2395 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
2399 // Functors for associative containers.
2403 operator()(const Merge_section_properties
& msp1
,
2404 const Merge_section_properties
& msp2
) const
2405 { return msp1
.eq(msp2
); }
2411 operator()(const Merge_section_properties
& msp
) const
2412 { return msp
.hash_value(); }
2416 // Whether this merge data section is for strings.
2418 // Entsize of this merge data section.
2420 // Address alignment.
2421 uint64_t addralign_
;
2424 // This class is used to speed up look up of special input sections in an
2427 class Output_section_lookup_maps
2430 Output_section_lookup_maps()
2431 : is_valid_(true), merge_sections_by_properties_(),
2432 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2435 // Whether the maps are valid.
2438 { return this->is_valid_
; }
2440 // Invalidate the maps.
2443 { this->is_valid_
= false; }
2449 this->merge_sections_by_properties_
.clear();
2450 this->merge_sections_by_id_
.clear();
2451 this->relaxed_input_sections_by_id_
.clear();
2452 // A cleared map is valid.
2453 this->is_valid_
= true;
2456 // Find a merge section by merge section properties. Return NULL if none
2459 find_merge_section(const Merge_section_properties
& msp
) const
2461 gold_assert(this->is_valid_
);
2462 Merge_sections_by_properties::const_iterator p
=
2463 this->merge_sections_by_properties_
.find(msp
);
2464 return p
!= this->merge_sections_by_properties_
.end() ? p
->second
: NULL
;
2467 // Find a merge section by section ID of a merge input section. Return NULL
2468 // if none is found.
2470 find_merge_section(const Object
* object
, unsigned int shndx
) const
2472 gold_assert(this->is_valid_
);
2473 Merge_sections_by_id::const_iterator p
=
2474 this->merge_sections_by_id_
.find(Const_section_id(object
, shndx
));
2475 return p
!= this->merge_sections_by_id_
.end() ? p
->second
: NULL
;
2478 // Add a merge section pointed by POMB with properties MSP.
2480 add_merge_section(const Merge_section_properties
& msp
,
2481 Output_merge_base
* pomb
)
2483 std::pair
<Merge_section_properties
, Output_merge_base
*> value(msp
, pomb
);
2484 std::pair
<Merge_sections_by_properties::iterator
, bool> result
=
2485 this->merge_sections_by_properties_
.insert(value
);
2486 gold_assert(result
.second
);
2489 // Add a mapping from a merged input section in OBJECT with index SHNDX
2490 // to a merge output section pointed by POMB.
2492 add_merge_input_section(const Object
* object
, unsigned int shndx
,
2493 Output_merge_base
* pomb
)
2495 Const_section_id
csid(object
, shndx
);
2496 std::pair
<Const_section_id
, Output_merge_base
*> value(csid
, pomb
);
2497 std::pair
<Merge_sections_by_id::iterator
, bool> result
=
2498 this->merge_sections_by_id_
.insert(value
);
2499 gold_assert(result
.second
);
2502 // Find a relaxed input section of OBJECT with index SHNDX.
2503 Output_relaxed_input_section
*
2504 find_relaxed_input_section(const Object
* object
, unsigned int shndx
) const
2506 gold_assert(this->is_valid_
);
2507 Relaxed_input_sections_by_id::const_iterator p
=
2508 this->relaxed_input_sections_by_id_
.find(Const_section_id(object
, shndx
));
2509 return p
!= this->relaxed_input_sections_by_id_
.end() ? p
->second
: NULL
;
2512 // Add a relaxed input section pointed by POMB and whose original input
2513 // section is in OBJECT with index SHNDX.
2515 add_relaxed_input_section(const Relobj
* relobj
, unsigned int shndx
,
2516 Output_relaxed_input_section
* poris
)
2518 Const_section_id
csid(relobj
, shndx
);
2519 std::pair
<Const_section_id
, Output_relaxed_input_section
*>
2521 std::pair
<Relaxed_input_sections_by_id::iterator
, bool> result
=
2522 this->relaxed_input_sections_by_id_
.insert(value
);
2523 gold_assert(result
.second
);
2527 typedef Unordered_map
<Const_section_id
, Output_merge_base
*,
2528 Const_section_id_hash
>
2529 Merge_sections_by_id
;
2531 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
2532 Merge_section_properties::hash
,
2533 Merge_section_properties::equal_to
>
2534 Merge_sections_by_properties
;
2536 typedef Unordered_map
<Const_section_id
, Output_relaxed_input_section
*,
2537 Const_section_id_hash
>
2538 Relaxed_input_sections_by_id
;
2540 // Whether this is valid
2542 // Merge sections by merge section properties.
2543 Merge_sections_by_properties merge_sections_by_properties_
;
2544 // Merge sections by section IDs.
2545 Merge_sections_by_id merge_sections_by_id_
;
2546 // Relaxed sections by section IDs.
2547 Relaxed_input_sections_by_id relaxed_input_sections_by_id_
;
2550 // An output section. We don't expect to have too many output
2551 // sections, so we don't bother to do a template on the size.
2553 class Output_section
: public Output_data
2556 // Create an output section, giving the name, type, and flags.
2557 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2558 virtual ~Output_section();
2560 // Add a new input section SHNDX, named NAME, with header SHDR, from
2561 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2562 // which applies to this section, or 0 if none, or -1 if more than
2563 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2564 // in a linker script; in that case we need to keep track of input
2565 // sections associated with an output section. Return the offset
2566 // within the output section.
2567 template<int size
, bool big_endian
>
2569 add_input_section(Layout
* layout
, Sized_relobj
<size
, big_endian
>* object
,
2570 unsigned int shndx
, const char* name
,
2571 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2572 unsigned int reloc_shndx
, bool have_sections_script
);
2574 // Add generated data POSD to this output section.
2576 add_output_section_data(Output_section_data
* posd
);
2578 // Add a relaxed input section PORIS called NAME to this output section
2581 add_relaxed_input_section(Layout
* layout
,
2582 Output_relaxed_input_section
* poris
,
2583 const std::string
& name
);
2585 // Return the section name.
2588 { return this->name_
; }
2590 // Return the section type.
2593 { return this->type_
; }
2595 // Return the section flags.
2598 { return this->flags_
; }
2600 // Update the output section flags based on input section flags.
2602 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2604 // Return the entsize field.
2607 { return this->entsize_
; }
2609 // Set the entsize field.
2611 set_entsize(uint64_t v
);
2613 // Set the load address.
2615 set_load_address(uint64_t load_address
)
2617 this->load_address_
= load_address
;
2618 this->has_load_address_
= true;
2621 // Set the link field to the output section index of a section.
2623 set_link_section(const Output_data
* od
)
2625 gold_assert(this->link_
== 0
2626 && !this->should_link_to_symtab_
2627 && !this->should_link_to_dynsym_
);
2628 this->link_section_
= od
;
2631 // Set the link field to a constant.
2633 set_link(unsigned int v
)
2635 gold_assert(this->link_section_
== NULL
2636 && !this->should_link_to_symtab_
2637 && !this->should_link_to_dynsym_
);
2641 // Record that this section should link to the normal symbol table.
2643 set_should_link_to_symtab()
2645 gold_assert(this->link_section_
== NULL
2647 && !this->should_link_to_dynsym_
);
2648 this->should_link_to_symtab_
= true;
2651 // Record that this section should link to the dynamic symbol table.
2653 set_should_link_to_dynsym()
2655 gold_assert(this->link_section_
== NULL
2657 && !this->should_link_to_symtab_
);
2658 this->should_link_to_dynsym_
= true;
2661 // Return the info field.
2665 gold_assert(this->info_section_
== NULL
2666 && this->info_symndx_
== NULL
);
2670 // Set the info field to the output section index of a section.
2672 set_info_section(const Output_section
* os
)
2674 gold_assert((this->info_section_
== NULL
2675 || (this->info_section_
== os
2676 && this->info_uses_section_index_
))
2677 && this->info_symndx_
== NULL
2678 && this->info_
== 0);
2679 this->info_section_
= os
;
2680 this->info_uses_section_index_
= true;
2683 // Set the info field to the symbol table index of a symbol.
2685 set_info_symndx(const Symbol
* sym
)
2687 gold_assert(this->info_section_
== NULL
2688 && (this->info_symndx_
== NULL
2689 || this->info_symndx_
== sym
)
2690 && this->info_
== 0);
2691 this->info_symndx_
= sym
;
2694 // Set the info field to the symbol table index of a section symbol.
2696 set_info_section_symndx(const Output_section
* os
)
2698 gold_assert((this->info_section_
== NULL
2699 || (this->info_section_
== os
2700 && !this->info_uses_section_index_
))
2701 && this->info_symndx_
== NULL
2702 && this->info_
== 0);
2703 this->info_section_
= os
;
2704 this->info_uses_section_index_
= false;
2707 // Set the info field to a constant.
2709 set_info(unsigned int v
)
2711 gold_assert(this->info_section_
== NULL
2712 && this->info_symndx_
== NULL
2713 && (this->info_
== 0
2714 || this->info_
== v
));
2718 // Set the addralign field.
2720 set_addralign(uint64_t v
)
2721 { this->addralign_
= v
; }
2723 // Whether the output section index has been set.
2725 has_out_shndx() const
2726 { return this->out_shndx_
!= -1U; }
2728 // Indicate that we need a symtab index.
2730 set_needs_symtab_index()
2731 { this->needs_symtab_index_
= true; }
2733 // Return whether we need a symtab index.
2735 needs_symtab_index() const
2736 { return this->needs_symtab_index_
; }
2738 // Get the symtab index.
2740 symtab_index() const
2742 gold_assert(this->symtab_index_
!= 0);
2743 return this->symtab_index_
;
2746 // Set the symtab index.
2748 set_symtab_index(unsigned int index
)
2750 gold_assert(index
!= 0);
2751 this->symtab_index_
= index
;
2754 // Indicate that we need a dynsym index.
2756 set_needs_dynsym_index()
2757 { this->needs_dynsym_index_
= true; }
2759 // Return whether we need a dynsym index.
2761 needs_dynsym_index() const
2762 { return this->needs_dynsym_index_
; }
2764 // Get the dynsym index.
2766 dynsym_index() const
2768 gold_assert(this->dynsym_index_
!= 0);
2769 return this->dynsym_index_
;
2772 // Set the dynsym index.
2774 set_dynsym_index(unsigned int index
)
2776 gold_assert(index
!= 0);
2777 this->dynsym_index_
= index
;
2780 // Return whether the input sections sections attachd to this output
2781 // section may require sorting. This is used to handle constructor
2782 // priorities compatibly with GNU ld.
2784 may_sort_attached_input_sections() const
2785 { return this->may_sort_attached_input_sections_
; }
2787 // Record that the input sections attached to this output section
2788 // may require sorting.
2790 set_may_sort_attached_input_sections()
2791 { this->may_sort_attached_input_sections_
= true; }
2793 // Returns true if input sections must be sorted according to the
2794 // order in which their name appear in the --section-ordering-file.
2796 input_section_order_specified()
2797 { return this->input_section_order_specified_
; }
2799 // Record that input sections must be sorted as some of their names
2800 // match the patterns specified through --section-ordering-file.
2802 set_input_section_order_specified()
2803 { this->input_section_order_specified_
= true; }
2805 // Return whether the input sections attached to this output section
2806 // require sorting. This is used to handle constructor priorities
2807 // compatibly with GNU ld.
2809 must_sort_attached_input_sections() const
2810 { return this->must_sort_attached_input_sections_
; }
2812 // Record that the input sections attached to this output section
2815 set_must_sort_attached_input_sections()
2816 { this->must_sort_attached_input_sections_
= true; }
2818 // Get the order in which this section appears in the PT_LOAD output
2820 Output_section_order
2822 { return this->order_
; }
2824 // Set the order for this section.
2826 set_order(Output_section_order order
)
2827 { this->order_
= order
; }
2829 // Return whether this section holds relro data--data which has
2830 // dynamic relocations but which may be marked read-only after the
2831 // dynamic relocations have been completed.
2834 { return this->is_relro_
; }
2836 // Record that this section holds relro data.
2839 { this->is_relro_
= true; }
2841 // Record that this section does not hold relro data.
2844 { this->is_relro_
= false; }
2846 // True if this is a small section: a section which holds small
2849 is_small_section() const
2850 { return this->is_small_section_
; }
2852 // Record that this is a small section.
2854 set_is_small_section()
2855 { this->is_small_section_
= true; }
2857 // True if this is a large section: a section which holds large
2860 is_large_section() const
2861 { return this->is_large_section_
; }
2863 // Record that this is a large section.
2865 set_is_large_section()
2866 { this->is_large_section_
= true; }
2868 // True if this is a large data (not BSS) section.
2870 is_large_data_section()
2871 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2873 // Return whether this section should be written after all the input
2874 // sections are complete.
2876 after_input_sections() const
2877 { return this->after_input_sections_
; }
2879 // Record that this section should be written after all the input
2880 // sections are complete.
2882 set_after_input_sections()
2883 { this->after_input_sections_
= true; }
2885 // Return whether this section requires postprocessing after all
2886 // relocations have been applied.
2888 requires_postprocessing() const
2889 { return this->requires_postprocessing_
; }
2891 // If a section requires postprocessing, return the buffer to use.
2893 postprocessing_buffer() const
2895 gold_assert(this->postprocessing_buffer_
!= NULL
);
2896 return this->postprocessing_buffer_
;
2899 // If a section requires postprocessing, create the buffer to use.
2901 create_postprocessing_buffer();
2903 // If a section requires postprocessing, this is the size of the
2904 // buffer to which relocations should be applied.
2906 postprocessing_buffer_size() const
2907 { return this->current_data_size_for_child(); }
2909 // Modify the section name. This is only permitted for an
2910 // unallocated section, and only before the size has been finalized.
2911 // Otherwise the name will not get into Layout::namepool_.
2913 set_name(const char* newname
)
2915 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2916 gold_assert(!this->is_data_size_valid());
2917 this->name_
= newname
;
2920 // Return whether the offset OFFSET in the input section SHNDX in
2921 // object OBJECT is being included in the link.
2923 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2924 off_t offset
) const;
2926 // Return the offset within the output section of OFFSET relative to
2927 // the start of input section SHNDX in object OBJECT.
2929 output_offset(const Relobj
* object
, unsigned int shndx
,
2930 section_offset_type offset
) const;
2932 // Return the output virtual address of OFFSET relative to the start
2933 // of input section SHNDX in object OBJECT.
2935 output_address(const Relobj
* object
, unsigned int shndx
,
2936 off_t offset
) const;
2938 // Look for the merged section for input section SHNDX in object
2939 // OBJECT. If found, return true, and set *ADDR to the address of
2940 // the start of the merged section. This is not necessary the
2941 // output offset corresponding to input offset 0 in the section,
2942 // since the section may be mapped arbitrarily.
2944 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2945 uint64_t* addr
) const;
2947 // Record that this output section was found in the SECTIONS clause
2948 // of a linker script.
2950 set_found_in_sections_clause()
2951 { this->found_in_sections_clause_
= true; }
2953 // Return whether this output section was found in the SECTIONS
2954 // clause of a linker script.
2956 found_in_sections_clause() const
2957 { return this->found_in_sections_clause_
; }
2959 // Write the section header into *OPHDR.
2960 template<int size
, bool big_endian
>
2962 write_header(const Layout
*, const Stringpool
*,
2963 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2965 // The next few calls are for linker script support.
2967 // In some cases we need to keep a list of the input sections
2968 // associated with this output section. We only need the list if we
2969 // might have to change the offsets of the input section within the
2970 // output section after we add the input section. The ordinary
2971 // input sections will be written out when we process the object
2972 // file, and as such we don't need to track them here. We do need
2973 // to track Output_section_data objects here. We store instances of
2974 // this structure in a std::vector, so it must be a POD. There can
2975 // be many instances of this structure, so we use a union to save
2981 : shndx_(0), p2align_(0)
2983 this->u1_
.data_size
= 0;
2984 this->u2_
.object
= NULL
;
2987 // For an ordinary input section.
2988 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2991 p2align_(ffsll(static_cast<long long>(addralign
))),
2992 section_order_index_(0)
2994 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2995 && shndx
!= MERGE_DATA_SECTION_CODE
2996 && shndx
!= MERGE_STRING_SECTION_CODE
2997 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
2998 this->u1_
.data_size
= data_size
;
2999 this->u2_
.object
= object
;
3002 // For a non-merge output section.
3003 Input_section(Output_section_data
* posd
)
3004 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0),
3005 section_order_index_(0)
3007 this->u1_
.data_size
= 0;
3008 this->u2_
.posd
= posd
;
3011 // For a merge section.
3012 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
3014 ? MERGE_STRING_SECTION_CODE
3015 : MERGE_DATA_SECTION_CODE
),
3017 section_order_index_(0)
3019 this->u1_
.entsize
= entsize
;
3020 this->u2_
.posd
= posd
;
3023 // For a relaxed input section.
3024 Input_section(Output_relaxed_input_section
* psection
)
3025 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0),
3026 section_order_index_(0)
3028 this->u1_
.data_size
= 0;
3029 this->u2_
.poris
= psection
;
3033 section_order_index() const
3035 return this->section_order_index_
;
3039 set_section_order_index(unsigned int number
)
3041 this->section_order_index_
= number
;
3044 // The required alignment.
3048 if (this->p2align_
!= 0)
3049 return static_cast<uint64_t>(1) << (this->p2align_
- 1);
3050 else if (!this->is_input_section())
3051 return this->u2_
.posd
->addralign();
3056 // Set the required alignment, which must be either 0 or a power of 2.
3057 // For input sections that are sub-classes of Output_section_data, a
3058 // alignment of zero means asking the underlying object for alignment.
3060 set_addralign(uint64_t addralign
)
3066 gold_assert((addralign
& (addralign
- 1)) == 0);
3067 this->p2align_
= ffsll(static_cast<long long>(addralign
));
3071 // Return the current required size, without finalization.
3073 current_data_size() const;
3075 // Return the required size.
3079 // Whether this is an input section.
3081 is_input_section() const
3083 return (this->shndx_
!= OUTPUT_SECTION_CODE
3084 && this->shndx_
!= MERGE_DATA_SECTION_CODE
3085 && this->shndx_
!= MERGE_STRING_SECTION_CODE
3086 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
3089 // Return whether this is a merge section which matches the
3092 is_merge_section(bool is_string
, uint64_t entsize
,
3093 uint64_t addralign
) const
3095 return (this->shndx_
== (is_string
3096 ? MERGE_STRING_SECTION_CODE
3097 : MERGE_DATA_SECTION_CODE
)
3098 && this->u1_
.entsize
== entsize
3099 && this->addralign() == addralign
);
3102 // Return whether this is a merge section for some input section.
3104 is_merge_section() const
3106 return (this->shndx_
== MERGE_DATA_SECTION_CODE
3107 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3110 // Return whether this is a relaxed input section.
3112 is_relaxed_input_section() const
3113 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
3115 // Return whether this is a generic Output_section_data.
3117 is_output_section_data() const
3119 return this->shndx_
== OUTPUT_SECTION_CODE
;
3122 // Return the object for an input section.
3126 // Return the input section index for an input section.
3130 // For non-input-sections, return the associated Output_section_data
3132 Output_section_data
*
3133 output_section_data() const
3135 gold_assert(!this->is_input_section());
3136 return this->u2_
.posd
;
3139 // For a merge section, return the Output_merge_base pointer.
3141 output_merge_base() const
3143 gold_assert(this->is_merge_section());
3144 return this->u2_
.pomb
;
3147 // Return the Output_relaxed_input_section object.
3148 Output_relaxed_input_section
*
3149 relaxed_input_section() const
3151 gold_assert(this->is_relaxed_input_section());
3152 return this->u2_
.poris
;
3155 // Set the output section.
3157 set_output_section(Output_section
* os
)
3159 gold_assert(!this->is_input_section());
3160 Output_section_data
* posd
=
3161 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
3162 posd
->set_output_section(os
);
3165 // Set the address and file offset. This is called during
3166 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3167 // the enclosing section.
3169 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
3170 off_t section_file_offset
);
3172 // Reset the address and file offset.
3174 reset_address_and_file_offset();
3176 // Finalize the data size.
3178 finalize_data_size();
3180 // Add an input section, for SHF_MERGE sections.
3182 add_input_section(Relobj
* object
, unsigned int shndx
)
3184 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
3185 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3186 return this->u2_
.posd
->add_input_section(object
, shndx
);
3189 // Given an input OBJECT, an input section index SHNDX within that
3190 // object, and an OFFSET relative to the start of that input
3191 // section, return whether or not the output offset is known. If
3192 // this function returns true, it sets *POUTPUT to the offset in
3193 // the output section, relative to the start of the input section
3194 // in the output section. *POUTPUT may be different from OFFSET
3195 // for a merged section.
3197 output_offset(const Relobj
* object
, unsigned int shndx
,
3198 section_offset_type offset
,
3199 section_offset_type
* poutput
) const;
3201 // Return whether this is the merge section for the input section
3204 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3206 // Write out the data. This does nothing for an input section.
3208 write(Output_file
*);
3210 // Write the data to a buffer. This does nothing for an input
3213 write_to_buffer(unsigned char*);
3215 // Print to a map file.
3217 print_to_mapfile(Mapfile
*) const;
3219 // Print statistics about merge sections to stderr.
3221 print_merge_stats(const char* section_name
)
3223 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3224 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3225 this->u2_
.posd
->print_merge_stats(section_name
);
3229 // Code values which appear in shndx_. If the value is not one of
3230 // these codes, it is the input section index in the object file.
3233 // An Output_section_data.
3234 OUTPUT_SECTION_CODE
= -1U,
3235 // An Output_section_data for an SHF_MERGE section with
3236 // SHF_STRINGS not set.
3237 MERGE_DATA_SECTION_CODE
= -2U,
3238 // An Output_section_data for an SHF_MERGE section with
3240 MERGE_STRING_SECTION_CODE
= -3U,
3241 // An Output_section_data for a relaxed input section.
3242 RELAXED_INPUT_SECTION_CODE
= -4U
3245 // For an ordinary input section, this is the section index in the
3246 // input file. For an Output_section_data, this is
3247 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3248 // MERGE_STRING_SECTION_CODE.
3249 unsigned int shndx_
;
3250 // The required alignment, stored as a power of 2.
3251 unsigned int p2align_
;
3254 // For an ordinary input section, the section size.
3256 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3257 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3263 // For an ordinary input section, the object which holds the
3266 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3267 // MERGE_STRING_SECTION_CODE, the data.
3268 Output_section_data
* posd
;
3269 Output_merge_base
* pomb
;
3270 // For RELAXED_INPUT_SECTION_CODE, the data.
3271 Output_relaxed_input_section
* poris
;
3273 // The line number of the pattern it matches in the --section-ordering-file
3274 // file. It is 0 if does not match any pattern.
3275 unsigned int section_order_index_
;
3278 // Store the list of input sections for this Output_section into the
3279 // list passed in. This removes the input sections, leaving only
3280 // any Output_section_data elements. This returns the size of those
3281 // Output_section_data elements. ADDRESS is the address of this
3282 // output section. FILL is the fill value to use, in case there are
3283 // any spaces between the remaining Output_section_data elements.
3285 get_input_sections(uint64_t address
, const std::string
& fill
,
3286 std::list
<Input_section
>*);
3288 // Add a script input section. A script input section can either be
3289 // a plain input section or a sub-class of Output_section_data.
3291 add_script_input_section(const Input_section
& input_section
);
3293 // Set the current size of the output section.
3295 set_current_data_size(off_t size
)
3296 { this->set_current_data_size_for_child(size
); }
3298 // End of linker script support.
3300 // Save states before doing section layout.
3301 // This is used for relaxation.
3305 // Restore states prior to section layout.
3313 // Convert existing input sections to relaxed input sections.
3315 convert_input_sections_to_relaxed_sections(
3316 const std::vector
<Output_relaxed_input_section
*>& sections
);
3318 // Find a relaxed input section to an input section in OBJECT
3319 // with index SHNDX. Return NULL if none is found.
3320 const Output_relaxed_input_section
*
3321 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
3323 // Whether section offsets need adjustment due to relaxation.
3325 section_offsets_need_adjustment() const
3326 { return this->section_offsets_need_adjustment_
; }
3328 // Set section_offsets_need_adjustment to be true.
3330 set_section_offsets_need_adjustment()
3331 { this->section_offsets_need_adjustment_
= true; }
3333 // Adjust section offsets of input sections in this. This is
3334 // requires if relaxation caused some input sections to change sizes.
3336 adjust_section_offsets();
3338 // Whether this is a NOLOAD section.
3341 { return this->is_noload_
; }
3346 { this->is_noload_
= true; }
3348 // Print merge statistics to stderr.
3350 print_merge_stats();
3352 // Set a fixed layout for the section. Used for incremental update links.
3354 set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
, off_t sh_size
,
3355 uint64_t sh_addralign
);
3357 // Return TRUE if the section has a fixed layout.
3359 has_fixed_layout() const
3360 { return this->has_fixed_layout_
; }
3362 // Reserve space within the fixed layout for the section. Used for
3363 // incremental update links.
3365 reserve(uint64_t sh_offset
, uint64_t sh_size
);
3368 // Return the output section--i.e., the object itself.
3373 const Output_section
*
3374 do_output_section() const
3377 // Return the section index in the output file.
3379 do_out_shndx() const
3381 gold_assert(this->out_shndx_
!= -1U);
3382 return this->out_shndx_
;
3385 // Set the output section index.
3387 do_set_out_shndx(unsigned int shndx
)
3389 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
3390 this->out_shndx_
= shndx
;
3393 // Update the data size of the Output_section. For a typical
3394 // Output_section, there is nothing to do, but if there are any
3395 // Output_section_data objects we need to do a trial layout
3400 // Set the final data size of the Output_section. For a typical
3401 // Output_section, there is nothing to do, but if there are any
3402 // Output_section_data objects we need to set their final addresses
3405 set_final_data_size();
3407 // Reset the address and file offset.
3409 do_reset_address_and_file_offset();
3411 // Return true if address and file offset already have reset values. In
3412 // other words, calling reset_address_and_file_offset will not change them.
3414 do_address_and_file_offset_have_reset_values() const;
3416 // Write the data to the file. For a typical Output_section, this
3417 // does nothing: the data is written out by calling Object::Relocate
3418 // on each input object. But if there are any Output_section_data
3419 // objects we do need to write them out here.
3421 do_write(Output_file
*);
3423 // Return the address alignment--function required by parent class.
3425 do_addralign() const
3426 { return this->addralign_
; }
3428 // Return whether there is a load address.
3430 do_has_load_address() const
3431 { return this->has_load_address_
; }
3433 // Return the load address.
3435 do_load_address() const
3437 gold_assert(this->has_load_address_
);
3438 return this->load_address_
;
3441 // Return whether this is an Output_section.
3443 do_is_section() const
3446 // Return whether this is a section of the specified type.
3448 do_is_section_type(elfcpp::Elf_Word type
) const
3449 { return this->type_
== type
; }
3451 // Return whether the specified section flag is set.
3453 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
3454 { return (this->flags_
& flag
) != 0; }
3456 // Set the TLS offset. Called only for SHT_TLS sections.
3458 do_set_tls_offset(uint64_t tls_base
);
3460 // Return the TLS offset, relative to the base of the TLS segment.
3461 // Valid only for SHT_TLS sections.
3463 do_tls_offset() const
3464 { return this->tls_offset_
; }
3466 // This may be implemented by a child class.
3468 do_finalize_name(Layout
*)
3471 // Print to the map file.
3473 do_print_to_mapfile(Mapfile
*) const;
3475 // Record that this section requires postprocessing after all
3476 // relocations have been applied. This is called by a child class.
3478 set_requires_postprocessing()
3480 this->requires_postprocessing_
= true;
3481 this->after_input_sections_
= true;
3484 // Write all the data of an Output_section into the postprocessing
3487 write_to_postprocessing_buffer();
3489 typedef std::vector
<Input_section
> Input_section_list
;
3491 // Allow a child class to access the input sections.
3492 const Input_section_list
&
3493 input_sections() const
3494 { return this->input_sections_
; }
3496 // Whether this always keeps an input section list
3498 always_keeps_input_sections() const
3499 { return this->always_keeps_input_sections_
; }
3501 // Always keep an input section list.
3503 set_always_keeps_input_sections()
3505 gold_assert(this->current_data_size_for_child() == 0);
3506 this->always_keeps_input_sections_
= true;
3510 // We only save enough information to undo the effects of section layout.
3511 class Checkpoint_output_section
3514 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3515 const Input_section_list
& input_sections
,
3516 off_t first_input_offset
,
3517 bool attached_input_sections_are_sorted
)
3518 : addralign_(addralign
), flags_(flags
),
3519 input_sections_(input_sections
),
3520 input_sections_size_(input_sections_
.size()),
3521 input_sections_copy_(), first_input_offset_(first_input_offset
),
3522 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3526 ~Checkpoint_output_section()
3529 // Return the address alignment.
3532 { return this->addralign_
; }
3534 // Return the section flags.
3537 { return this->flags_
; }
3539 // Return a reference to the input section list copy.
3542 { return &this->input_sections_copy_
; }
3544 // Return the size of input_sections at the time when checkpoint is
3547 input_sections_size() const
3548 { return this->input_sections_size_
; }
3550 // Whether input sections are copied.
3552 input_sections_saved() const
3553 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3556 first_input_offset() const
3557 { return this->first_input_offset_
; }
3560 attached_input_sections_are_sorted() const
3561 { return this->attached_input_sections_are_sorted_
; }
3563 // Save input sections.
3565 save_input_sections()
3567 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3568 this->input_sections_copy_
.clear();
3569 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3570 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3571 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3572 this->input_sections_copy_
.push_back(*p
);
3576 // The section alignment.
3577 uint64_t addralign_
;
3578 // The section flags.
3579 elfcpp::Elf_Xword flags_
;
3580 // Reference to the input sections to be checkpointed.
3581 const Input_section_list
& input_sections_
;
3582 // Size of the checkpointed portion of input_sections_;
3583 size_t input_sections_size_
;
3584 // Copy of input sections.
3585 Input_section_list input_sections_copy_
;
3586 // The offset of the first entry in input_sections_.
3587 off_t first_input_offset_
;
3588 // True if the input sections attached to this output section have
3589 // already been sorted.
3590 bool attached_input_sections_are_sorted_
;
3593 // This class is used to sort the input sections.
3594 class Input_section_sort_entry
;
3596 // This is the sort comparison function for ctors and dtors.
3597 struct Input_section_sort_compare
3600 operator()(const Input_section_sort_entry
&,
3601 const Input_section_sort_entry
&) const;
3604 // This is the sort comparison function for .init_array and .fini_array.
3605 struct Input_section_sort_init_fini_compare
3608 operator()(const Input_section_sort_entry
&,
3609 const Input_section_sort_entry
&) const;
3612 // This is the sort comparison function when a section order is specified
3613 // from an input file.
3614 struct Input_section_sort_section_order_index_compare
3617 operator()(const Input_section_sort_entry
&,
3618 const Input_section_sort_entry
&) const;
3621 // Fill data. This is used to fill in data between input sections.
3622 // It is also used for data statements (BYTE, WORD, etc.) in linker
3623 // scripts. When we have to keep track of the input sections, we
3624 // can use an Output_data_const, but we don't want to have to keep
3625 // track of input sections just to implement fills.
3629 Fill(off_t section_offset
, off_t length
)
3630 : section_offset_(section_offset
),
3631 length_(convert_to_section_size_type(length
))
3634 // Return section offset.
3636 section_offset() const
3637 { return this->section_offset_
; }
3639 // Return fill length.
3642 { return this->length_
; }
3645 // The offset within the output section.
3646 off_t section_offset_
;
3647 // The length of the space to fill.
3648 section_size_type length_
;
3651 typedef std::vector
<Fill
> Fill_list
;
3653 // Map used during relaxation of existing sections. This map
3654 // a section id an input section list index. We assume that
3655 // Input_section_list is a vector.
3656 typedef Unordered_map
<Section_id
, size_t, Section_id_hash
> Relaxation_map
;
3658 // Add a new output section by Input_section.
3660 add_output_section_data(Input_section
*);
3662 // Add an SHF_MERGE input section. Returns true if the section was
3663 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3664 // stores information about the merged input sections.
3666 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3667 uint64_t entsize
, uint64_t addralign
,
3668 bool keeps_input_sections
);
3670 // Add an output SHF_MERGE section POSD to this output section.
3671 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3672 // ENTSIZE is the entity size. This returns the entry added to
3675 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3678 // Sort the attached input sections.
3680 sort_attached_input_sections();
3682 // Find the merge section into which an input section with index SHNDX in
3683 // OBJECT has been added. Return NULL if none found.
3684 Output_section_data
*
3685 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3687 // Build a relaxation map.
3689 build_relaxation_map(
3690 const Input_section_list
& input_sections
,
3692 Relaxation_map
* map
) const;
3694 // Convert input sections in an input section list into relaxed sections.
3696 convert_input_sections_in_list_to_relaxed_sections(
3697 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3698 const Relaxation_map
& map
,
3699 Input_section_list
* input_sections
);
3701 // Build the lookup maps for merge and relaxed input sections.
3703 build_lookup_maps() const;
3705 // Most of these fields are only valid after layout.
3707 // The name of the section. This will point into a Stringpool.
3709 // The section address is in the parent class.
3710 // The section alignment.
3711 uint64_t addralign_
;
3712 // The section entry size.
3714 // The load address. This is only used when using a linker script
3715 // with a SECTIONS clause. The has_load_address_ field indicates
3716 // whether this field is valid.
3717 uint64_t load_address_
;
3718 // The file offset is in the parent class.
3719 // Set the section link field to the index of this section.
3720 const Output_data
* link_section_
;
3721 // If link_section_ is NULL, this is the link field.
3723 // Set the section info field to the index of this section.
3724 const Output_section
* info_section_
;
3725 // If info_section_ is NULL, set the info field to the symbol table
3726 // index of this symbol.
3727 const Symbol
* info_symndx_
;
3728 // If info_section_ and info_symndx_ are NULL, this is the section
3731 // The section type.
3732 const elfcpp::Elf_Word type_
;
3733 // The section flags.
3734 elfcpp::Elf_Xword flags_
;
3735 // The order of this section in the output segment.
3736 Output_section_order order_
;
3737 // The section index.
3738 unsigned int out_shndx_
;
3739 // If there is a STT_SECTION for this output section in the normal
3740 // symbol table, this is the symbol index. This starts out as zero.
3741 // It is initialized in Layout::finalize() to be the index, or -1U
3742 // if there isn't one.
3743 unsigned int symtab_index_
;
3744 // If there is a STT_SECTION for this output section in the dynamic
3745 // symbol table, this is the symbol index. This starts out as zero.
3746 // It is initialized in Layout::finalize() to be the index, or -1U
3747 // if there isn't one.
3748 unsigned int dynsym_index_
;
3749 // The input sections. This will be empty in cases where we don't
3750 // need to keep track of them.
3751 Input_section_list input_sections_
;
3752 // The offset of the first entry in input_sections_.
3753 off_t first_input_offset_
;
3754 // The fill data. This is separate from input_sections_ because we
3755 // often will need fill sections without needing to keep track of
3758 // If the section requires postprocessing, this buffer holds the
3759 // section contents during relocation.
3760 unsigned char* postprocessing_buffer_
;
3761 // Whether this output section needs a STT_SECTION symbol in the
3762 // normal symbol table. This will be true if there is a relocation
3764 bool needs_symtab_index_
: 1;
3765 // Whether this output section needs a STT_SECTION symbol in the
3766 // dynamic symbol table. This will be true if there is a dynamic
3767 // relocation which needs it.
3768 bool needs_dynsym_index_
: 1;
3769 // Whether the link field of this output section should point to the
3770 // normal symbol table.
3771 bool should_link_to_symtab_
: 1;
3772 // Whether the link field of this output section should point to the
3773 // dynamic symbol table.
3774 bool should_link_to_dynsym_
: 1;
3775 // Whether this section should be written after all the input
3776 // sections are complete.
3777 bool after_input_sections_
: 1;
3778 // Whether this section requires post processing after all
3779 // relocations have been applied.
3780 bool requires_postprocessing_
: 1;
3781 // Whether an input section was mapped to this output section
3782 // because of a SECTIONS clause in a linker script.
3783 bool found_in_sections_clause_
: 1;
3784 // Whether this section has an explicitly specified load address.
3785 bool has_load_address_
: 1;
3786 // True if the info_section_ field means the section index of the
3787 // section, false if it means the symbol index of the corresponding
3789 bool info_uses_section_index_
: 1;
3790 // True if input sections attached to this output section have to be
3791 // sorted according to a specified order.
3792 bool input_section_order_specified_
: 1;
3793 // True if the input sections attached to this output section may
3795 bool may_sort_attached_input_sections_
: 1;
3796 // True if the input sections attached to this output section must
3798 bool must_sort_attached_input_sections_
: 1;
3799 // True if the input sections attached to this output section have
3800 // already been sorted.
3801 bool attached_input_sections_are_sorted_
: 1;
3802 // True if this section holds relro data.
3804 // True if this is a small section.
3805 bool is_small_section_
: 1;
3806 // True if this is a large section.
3807 bool is_large_section_
: 1;
3808 // Whether code-fills are generated at write.
3809 bool generate_code_fills_at_write_
: 1;
3810 // Whether the entry size field should be zero.
3811 bool is_entsize_zero_
: 1;
3812 // Whether section offsets need adjustment due to relaxation.
3813 bool section_offsets_need_adjustment_
: 1;
3814 // Whether this is a NOLOAD section.
3815 bool is_noload_
: 1;
3816 // Whether this always keeps input section.
3817 bool always_keeps_input_sections_
: 1;
3818 // Whether this section has a fixed layout, for incremental update links.
3819 bool has_fixed_layout_
: 1;
3820 // For SHT_TLS sections, the offset of this section relative to the base
3821 // of the TLS segment.
3822 uint64_t tls_offset_
;
3823 // Saved checkpoint.
3824 Checkpoint_output_section
* checkpoint_
;
3825 // Fast lookup maps for merged and relaxed input sections.
3826 Output_section_lookup_maps
* lookup_maps_
;
3827 // List of available regions within the section, for incremental
3829 Free_list free_list_
;
3832 // An output segment. PT_LOAD segments are built from collections of
3833 // output sections. Other segments typically point within PT_LOAD
3834 // segments, and are built directly as needed.
3836 // NOTE: We want to use the copy constructor for this class. During
3837 // relaxation, we may try built the segments multiple times. We do
3838 // that by copying the original segment list before lay-out, doing
3839 // a trial lay-out and roll-back to the saved copied if we need to
3840 // to the lay-out again.
3842 class Output_segment
3845 // Create an output segment, specifying the type and flags.
3846 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3848 // Return the virtual address.
3851 { return this->vaddr_
; }
3853 // Return the physical address.
3856 { return this->paddr_
; }
3858 // Return the segment type.
3861 { return this->type_
; }
3863 // Return the segment flags.
3866 { return this->flags_
; }
3868 // Return the memory size.
3871 { return this->memsz_
; }
3873 // Return the file size.
3876 { return this->filesz_
; }
3878 // Return the file offset.
3881 { return this->offset_
; }
3883 // Whether this is a segment created to hold large data sections.
3885 is_large_data_segment() const
3886 { return this->is_large_data_segment_
; }
3888 // Record that this is a segment created to hold large data
3891 set_is_large_data_segment()
3892 { this->is_large_data_segment_
= true; }
3894 // Return the maximum alignment of the Output_data.
3896 maximum_alignment();
3898 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
3899 // the segment flags to use.
3901 add_output_section_to_load(Layout
* layout
, Output_section
* os
,
3902 elfcpp::Elf_Word seg_flags
);
3904 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
3905 // is the segment flags to use.
3907 add_output_section_to_nonload(Output_section
* os
,
3908 elfcpp::Elf_Word seg_flags
);
3910 // Remove an Output_section from this segment. It is an error if it
3913 remove_output_section(Output_section
* os
);
3915 // Add an Output_data (which need not be an Output_section) to the
3916 // start of this segment.
3918 add_initial_output_data(Output_data
*);
3920 // Return true if this segment has any sections which hold actual
3921 // data, rather than being a BSS section.
3923 has_any_data_sections() const;
3925 // Whether this segment has a dynamic relocs.
3927 has_dynamic_reloc() const;
3929 // Return the address of the first section.
3931 first_section_load_address() const;
3933 // Return whether the addresses have been set already.
3935 are_addresses_set() const
3936 { return this->are_addresses_set_
; }
3938 // Set the addresses.
3940 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3942 this->vaddr_
= vaddr
;
3943 this->paddr_
= paddr
;
3944 this->are_addresses_set_
= true;
3947 // Update the flags for the flags of an output section added to this
3950 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
3952 // The ELF ABI specifies that a PT_TLS segment should always have
3953 // PF_R as the flags.
3954 if (this->type() != elfcpp::PT_TLS
)
3955 this->flags_
|= flags
;
3958 // Set the segment flags. This is only used if we have a PHDRS
3959 // clause which explicitly specifies the flags.
3961 set_flags(elfcpp::Elf_Word flags
)
3962 { this->flags_
= flags
; }
3964 // Set the address of the segment to ADDR and the offset to *POFF
3965 // and set the addresses and offsets of all contained output
3966 // sections accordingly. Set the section indexes of all contained
3967 // output sections starting with *PSHNDX. If RESET is true, first
3968 // reset the addresses of the contained sections. Return the
3969 // address of the immediately following segment. Update *POFF and
3970 // *PSHNDX. This should only be called for a PT_LOAD segment.
3972 set_section_addresses(Layout
*, bool reset
, uint64_t addr
,
3973 unsigned int* increase_relro
, bool* has_relro
,
3974 off_t
* poff
, unsigned int* pshndx
);
3976 // Set the minimum alignment of this segment. This may be adjusted
3977 // upward based on the section alignments.
3979 set_minimum_p_align(uint64_t align
)
3981 if (align
> this->min_p_align_
)
3982 this->min_p_align_
= align
;
3985 // Set the offset of this segment based on the section. This should
3986 // only be called for a non-PT_LOAD segment.
3988 set_offset(unsigned int increase
);
3990 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3994 // Return the number of output sections.
3996 output_section_count() const;
3998 // Return the section attached to the list segment with the lowest
3999 // load address. This is used when handling a PHDRS clause in a
4002 section_with_lowest_load_address() const;
4004 // Write the segment header into *OPHDR.
4005 template<int size
, bool big_endian
>
4007 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
4009 // Write the section headers of associated sections into V.
4010 template<int size
, bool big_endian
>
4012 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
4013 unsigned int* pshndx
) const;
4015 // Print the output sections in the map file.
4017 print_sections_to_mapfile(Mapfile
*) const;
4020 typedef std::vector
<Output_data
*> Output_data_list
;
4022 // Find the maximum alignment in an Output_data_list.
4024 maximum_alignment_list(const Output_data_list
*);
4026 // Return whether the first data section is a relro section.
4028 is_first_section_relro() const;
4030 // Set the section addresses in an Output_data_list.
4032 set_section_list_addresses(Layout
*, bool reset
, Output_data_list
*,
4033 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
4036 // Return the number of Output_sections in an Output_data_list.
4038 output_section_count_list(const Output_data_list
*) const;
4040 // Return whether an Output_data_list has a dynamic reloc.
4042 has_dynamic_reloc_list(const Output_data_list
*) const;
4044 // Find the section with the lowest load address in an
4045 // Output_data_list.
4047 lowest_load_address_in_list(const Output_data_list
* pdl
,
4048 Output_section
** found
,
4049 uint64_t* found_lma
) const;
4051 // Find the first and last entries by address.
4053 find_first_and_last_list(const Output_data_list
* pdl
,
4054 const Output_data
** pfirst
,
4055 const Output_data
** plast
) const;
4057 // Write the section headers in the list into V.
4058 template<int size
, bool big_endian
>
4060 write_section_headers_list(const Layout
*, const Stringpool
*,
4061 const Output_data_list
*, unsigned char* v
,
4062 unsigned int* pshdx
) const;
4064 // Print a section list to the mapfile.
4066 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
4068 // NOTE: We want to use the copy constructor. Currently, shallow copy
4069 // works for us so we do not need to write our own copy constructor.
4071 // The list of output data attached to this segment.
4072 Output_data_list output_lists_
[ORDER_MAX
];
4073 // The segment virtual address.
4075 // The segment physical address.
4077 // The size of the segment in memory.
4079 // The maximum section alignment. The is_max_align_known_ field
4080 // indicates whether this has been finalized.
4081 uint64_t max_align_
;
4082 // The required minimum value for the p_align field. This is used
4083 // for PT_LOAD segments. Note that this does not mean that
4084 // addresses should be aligned to this value; it means the p_paddr
4085 // and p_vaddr fields must be congruent modulo this value. For
4086 // non-PT_LOAD segments, the dynamic linker works more efficiently
4087 // if the p_align field has the more conventional value, although it
4088 // can align as needed.
4089 uint64_t min_p_align_
;
4090 // The offset of the segment data within the file.
4092 // The size of the segment data in the file.
4094 // The segment type;
4095 elfcpp::Elf_Word type_
;
4096 // The segment flags.
4097 elfcpp::Elf_Word flags_
;
4098 // Whether we have finalized max_align_.
4099 bool is_max_align_known_
: 1;
4100 // Whether vaddr and paddr were set by a linker script.
4101 bool are_addresses_set_
: 1;
4102 // Whether this segment holds large data sections.
4103 bool is_large_data_segment_
: 1;
4106 // This class represents the output file.
4111 Output_file(const char* name
);
4113 // Indicate that this is a temporary file which should not be
4117 { this->is_temporary_
= true; }
4119 // Try to open an existing file. Returns false if the file doesn't
4120 // exist, has a size of 0 or can't be mmaped. This method is
4123 open_for_modification();
4125 // Open the output file. FILE_SIZE is the final size of the file.
4126 // If the file already exists, it is deleted/truncated. This method
4127 // is thread-unsafe.
4129 open(off_t file_size
);
4131 // Resize the output file. This method is thread-unsafe.
4133 resize(off_t file_size
);
4135 // Close the output file (flushing all buffered data) and make sure
4136 // there are no errors. This method is thread-unsafe.
4140 // Return the size of this file.
4143 { return this->file_size_
; }
4145 // Return the name of this file.
4148 { return this->name_
; }
4150 // We currently always use mmap which makes the view handling quite
4151 // simple. In the future we may support other approaches.
4153 // Write data to the output file.
4155 write(off_t offset
, const void* data
, size_t len
)
4156 { memcpy(this->base_
+ offset
, data
, len
); }
4158 // Get a buffer to use to write to the file, given the offset into
4159 // the file and the size.
4161 get_output_view(off_t start
, size_t size
)
4163 gold_assert(start
>= 0
4164 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
4165 return this->base_
+ start
;
4168 // VIEW must have been returned by get_output_view. Write the
4169 // buffer to the file, passing in the offset and the size.
4171 write_output_view(off_t
, size_t, unsigned char*)
4174 // Get a read/write buffer. This is used when we want to write part
4175 // of the file, read it in, and write it again.
4177 get_input_output_view(off_t start
, size_t size
)
4178 { return this->get_output_view(start
, size
); }
4180 // Write a read/write buffer back to the file.
4182 write_input_output_view(off_t
, size_t, unsigned char*)
4185 // Get a read buffer. This is used when we just want to read part
4186 // of the file back it in.
4187 const unsigned char*
4188 get_input_view(off_t start
, size_t size
)
4189 { return this->get_output_view(start
, size
); }
4191 // Release a read bfufer.
4193 free_input_view(off_t
, size_t, const unsigned char*)
4197 // Map the file into memory or, if that fails, allocate anonymous
4202 // Allocate anonymous memory for the file.
4206 // Map the file into memory.
4210 // Unmap the file from memory (and flush to disk buffers).
4220 // Base of file mapped into memory.
4221 unsigned char* base_
;
4222 // True iff base_ points to a memory buffer rather than an output file.
4223 bool map_is_anonymous_
;
4224 // True if base_ was allocated using new rather than mmap.
4225 bool map_is_allocated_
;
4226 // True if this is a temporary file which should not be output.
4230 } // End namespace gold.
4232 #endif // !defined(GOLD_OUTPUT_H)