1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 // Return true if data size is fixed.
86 is_data_size_fixed() const
87 { return this->is_data_size_fixed_
; }
89 // Return the file offset. This is only valid after
90 // Layout::finalize is finished. For some non-allocated sections,
91 // it may not be valid until near the end of the link.
95 gold_assert(this->is_offset_valid_
);
99 // Reset the address and file offset. This essentially disables the
100 // sanity testing about duplicate and unknown settings.
102 reset_address_and_file_offset()
104 this->is_address_valid_
= false;
105 this->is_offset_valid_
= false;
106 if (!this->is_data_size_fixed_
)
107 this->is_data_size_valid_
= false;
108 this->do_reset_address_and_file_offset();
111 // Return true if address and file offset already have reset values. In
112 // other words, calling reset_address_and_file_offset will not change them.
114 address_and_file_offset_have_reset_values() const
115 { return this->do_address_and_file_offset_have_reset_values(); }
117 // Return the required alignment.
120 { return this->do_addralign(); }
122 // Return whether this has a load address.
124 has_load_address() const
125 { return this->do_has_load_address(); }
127 // Return the load address.
130 { return this->do_load_address(); }
132 // Return whether this is an Output_section.
135 { return this->do_is_section(); }
137 // Return whether this is an Output_section of the specified type.
139 is_section_type(elfcpp::Elf_Word stt
) const
140 { return this->do_is_section_type(stt
); }
142 // Return whether this is an Output_section with the specified flag
145 is_section_flag_set(elfcpp::Elf_Xword shf
) const
146 { return this->do_is_section_flag_set(shf
); }
148 // Return the output section that this goes in, if there is one.
151 { return this->do_output_section(); }
153 const Output_section
*
154 output_section() const
155 { return this->do_output_section(); }
157 // Return the output section index, if there is an output section.
160 { return this->do_out_shndx(); }
162 // Set the output section index, if this is an output section.
164 set_out_shndx(unsigned int shndx
)
165 { this->do_set_out_shndx(shndx
); }
167 // Set the address and file offset of this data, and finalize the
168 // size of the data. This is called during Layout::finalize for
169 // allocated sections.
171 set_address_and_file_offset(uint64_t addr
, off_t off
)
173 this->set_address(addr
);
174 this->set_file_offset(off
);
175 this->finalize_data_size();
180 set_address(uint64_t addr
)
182 gold_assert(!this->is_address_valid_
);
183 this->address_
= addr
;
184 this->is_address_valid_
= true;
187 // Set the file offset.
189 set_file_offset(off_t off
)
191 gold_assert(!this->is_offset_valid_
);
193 this->is_offset_valid_
= true;
196 // Finalize the data size.
200 if (!this->is_data_size_valid_
)
202 // Tell the child class to set the data size.
203 this->set_final_data_size();
204 gold_assert(this->is_data_size_valid_
);
208 // Set the TLS offset. Called only for SHT_TLS sections.
210 set_tls_offset(uint64_t tls_base
)
211 { this->do_set_tls_offset(tls_base
); }
213 // Return the TLS offset, relative to the base of the TLS segment.
214 // Valid only for SHT_TLS sections.
217 { return this->do_tls_offset(); }
219 // Write the data to the output file. This is called after
220 // Layout::finalize is complete.
222 write(Output_file
* file
)
223 { this->do_write(file
); }
225 // This is called by Layout::finalize to note that the sizes of
226 // allocated sections must now be fixed.
229 { Output_data::allocated_sizes_are_fixed
= true; }
231 // Used to check that layout has been done.
234 { return Output_data::allocated_sizes_are_fixed
; }
236 // Note that a dynamic reloc has been applied to this data.
239 { this->has_dynamic_reloc_
= true; }
241 // Return whether a dynamic reloc has been applied.
243 has_dynamic_reloc() const
244 { return this->has_dynamic_reloc_
; }
246 // Whether the address is valid.
248 is_address_valid() const
249 { return this->is_address_valid_
; }
251 // Whether the file offset is valid.
253 is_offset_valid() const
254 { return this->is_offset_valid_
; }
256 // Whether the data size is valid.
258 is_data_size_valid() const
259 { return this->is_data_size_valid_
; }
261 // Print information to the map file.
263 print_to_mapfile(Mapfile
* mapfile
) const
264 { return this->do_print_to_mapfile(mapfile
); }
267 // Functions that child classes may or in some cases must implement.
269 // Write the data to the output file.
271 do_write(Output_file
*) = 0;
273 // Return the required alignment.
275 do_addralign() const = 0;
277 // Return whether this has a load address.
279 do_has_load_address() const
282 // Return the load address.
284 do_load_address() const
285 { gold_unreachable(); }
287 // Return whether this is an Output_section.
289 do_is_section() const
292 // Return whether this is an Output_section of the specified type.
293 // This only needs to be implement by Output_section.
295 do_is_section_type(elfcpp::Elf_Word
) const
298 // Return whether this is an Output_section with the specific flag
299 // set. This only needs to be implemented by Output_section.
301 do_is_section_flag_set(elfcpp::Elf_Xword
) const
304 // Return the output section, if there is one.
305 virtual Output_section
*
309 virtual const Output_section
*
310 do_output_section() const
313 // Return the output section index, if there is an output section.
316 { gold_unreachable(); }
318 // Set the output section index, if this is an output section.
320 do_set_out_shndx(unsigned int)
321 { gold_unreachable(); }
323 // This is a hook for derived classes to set the data size. This is
324 // called by finalize_data_size, normally called during
325 // Layout::finalize, when the section address is set.
327 set_final_data_size()
328 { gold_unreachable(); }
330 // A hook for resetting the address and file offset.
332 do_reset_address_and_file_offset()
335 // Return true if address and file offset already have reset values. In
336 // other words, calling reset_address_and_file_offset will not change them.
337 // A child class overriding do_reset_address_and_file_offset may need to
338 // also override this.
340 do_address_and_file_offset_have_reset_values() const
341 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
343 // Set the TLS offset. Called only for SHT_TLS sections.
345 do_set_tls_offset(uint64_t)
346 { gold_unreachable(); }
348 // Return the TLS offset, relative to the base of the TLS segment.
349 // Valid only for SHT_TLS sections.
351 do_tls_offset() const
352 { gold_unreachable(); }
354 // Print to the map file. This only needs to be implemented by
355 // classes which may appear in a PT_LOAD segment.
357 do_print_to_mapfile(Mapfile
*) const
358 { gold_unreachable(); }
360 // Functions that child classes may call.
362 // Reset the address. The Output_section class needs this when an
363 // SHF_ALLOC input section is added to an output section which was
364 // formerly not SHF_ALLOC.
366 mark_address_invalid()
367 { this->is_address_valid_
= false; }
369 // Set the size of the data.
371 set_data_size(off_t data_size
)
373 gold_assert(!this->is_data_size_valid_
374 && !this->is_data_size_fixed_
);
375 this->data_size_
= data_size
;
376 this->is_data_size_valid_
= true;
379 // Fix the data size. Once it is fixed, it cannot be changed
380 // and the data size remains always valid.
384 gold_assert(this->is_data_size_valid_
);
385 this->is_data_size_fixed_
= true;
388 // Get the current data size--this is for the convenience of
389 // sections which build up their size over time.
391 current_data_size_for_child() const
392 { return this->data_size_
; }
394 // Set the current data size--this is for the convenience of
395 // sections which build up their size over time.
397 set_current_data_size_for_child(off_t data_size
)
399 gold_assert(!this->is_data_size_valid_
);
400 this->data_size_
= data_size
;
403 // Return default alignment for the target size.
407 // Return default alignment for a specified size--32 or 64.
409 default_alignment_for_size(int size
);
412 Output_data(const Output_data
&);
413 Output_data
& operator=(const Output_data
&);
415 // This is used for verification, to make sure that we don't try to
416 // change any sizes of allocated sections after we set the section
418 static bool allocated_sizes_are_fixed
;
420 // Memory address in output file.
422 // Size of data in output file.
424 // File offset of contents in output file.
426 // Whether address_ is valid.
427 bool is_address_valid_
: 1;
428 // Whether data_size_ is valid.
429 bool is_data_size_valid_
: 1;
430 // Whether offset_ is valid.
431 bool is_offset_valid_
: 1;
432 // Whether data size is fixed.
433 bool is_data_size_fixed_
: 1;
434 // Whether any dynamic relocs have been applied to this section.
435 bool has_dynamic_reloc_
: 1;
438 // Output the section headers.
440 class Output_section_headers
: public Output_data
443 Output_section_headers(const Layout
*,
444 const Layout::Segment_list
*,
445 const Layout::Section_list
*,
446 const Layout::Section_list
*,
448 const Output_section
*);
451 // Write the data to the file.
453 do_write(Output_file
*);
455 // Return the required alignment.
458 { return Output_data::default_alignment(); }
460 // Write to a map file.
462 do_print_to_mapfile(Mapfile
* mapfile
) const
463 { mapfile
->print_output_data(this, _("** section headers")); }
465 // Set final data size.
467 set_final_data_size()
468 { this->set_data_size(this->do_size()); }
471 // Write the data to the file with the right size and endianness.
472 template<int size
, bool big_endian
>
474 do_sized_write(Output_file
*);
476 // Compute data size.
480 const Layout
* layout_
;
481 const Layout::Segment_list
* segment_list_
;
482 const Layout::Section_list
* section_list_
;
483 const Layout::Section_list
* unattached_section_list_
;
484 const Stringpool
* secnamepool_
;
485 const Output_section
* shstrtab_section_
;
488 // Output the segment headers.
490 class Output_segment_headers
: public Output_data
493 Output_segment_headers(const Layout::Segment_list
& segment_list
);
496 // Write the data to the file.
498 do_write(Output_file
*);
500 // Return the required alignment.
503 { return Output_data::default_alignment(); }
505 // Write to a map file.
507 do_print_to_mapfile(Mapfile
* mapfile
) const
508 { mapfile
->print_output_data(this, _("** segment headers")); }
510 // Set final data size.
512 set_final_data_size()
513 { this->set_data_size(this->do_size()); }
516 // Write the data to the file with the right size and endianness.
517 template<int size
, bool big_endian
>
519 do_sized_write(Output_file
*);
521 // Compute the current size.
525 const Layout::Segment_list
& segment_list_
;
528 // Output the ELF file header.
530 class Output_file_header
: public Output_data
533 Output_file_header(const Target
*,
535 const Output_segment_headers
*,
538 // Add information about the section headers. We lay out the ELF
539 // file header before we create the section headers.
540 void set_section_info(const Output_section_headers
*,
541 const Output_section
* shstrtab
);
544 // Write the data to the file.
546 do_write(Output_file
*);
548 // Return the required alignment.
551 { return Output_data::default_alignment(); }
553 // Write to a map file.
555 do_print_to_mapfile(Mapfile
* mapfile
) const
556 { mapfile
->print_output_data(this, _("** file header")); }
558 // Set final data size.
560 set_final_data_size(void)
561 { this->set_data_size(this->do_size()); }
564 // Write the data to the file with the right size and endianness.
565 template<int size
, bool big_endian
>
567 do_sized_write(Output_file
*);
569 // Return the value to use for the entry address.
571 typename
elfcpp::Elf_types
<size
>::Elf_Addr
574 // Compute the current data size.
578 const Target
* target_
;
579 const Symbol_table
* symtab_
;
580 const Output_segment_headers
* segment_header_
;
581 const Output_section_headers
* section_header_
;
582 const Output_section
* shstrtab_
;
586 // Output sections are mainly comprised of input sections. However,
587 // there are cases where we have data to write out which is not in an
588 // input section. Output_section_data is used in such cases. This is
589 // an abstract base class.
591 class Output_section_data
: public Output_data
594 Output_section_data(off_t data_size
, uint64_t addralign
,
595 bool is_data_size_fixed
)
596 : Output_data(), output_section_(NULL
), addralign_(addralign
)
598 this->set_data_size(data_size
);
599 if (is_data_size_fixed
)
600 this->fix_data_size();
603 Output_section_data(uint64_t addralign
)
604 : Output_data(), output_section_(NULL
), addralign_(addralign
)
607 // Return the output section.
610 { return this->output_section_
; }
612 const Output_section
*
613 output_section() const
614 { return this->output_section_
; }
616 // Record the output section.
618 set_output_section(Output_section
* os
);
620 // Add an input section, for SHF_MERGE sections. This returns true
621 // if the section was handled.
623 add_input_section(Relobj
* object
, unsigned int shndx
)
624 { return this->do_add_input_section(object
, shndx
); }
626 // Given an input OBJECT, an input section index SHNDX within that
627 // object, and an OFFSET relative to the start of that input
628 // section, return whether or not the corresponding offset within
629 // the output section is known. If this function returns true, it
630 // sets *POUTPUT to the output offset. The value -1 indicates that
631 // this input offset is being discarded.
633 output_offset(const Relobj
* object
, unsigned int shndx
,
634 section_offset_type offset
,
635 section_offset_type
*poutput
) const
636 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
638 // Return whether this is the merge section for the input section
639 // SHNDX in OBJECT. This should return true when output_offset
640 // would return true for some values of OFFSET.
642 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
643 { return this->do_is_merge_section_for(object
, shndx
); }
645 // Write the contents to a buffer. This is used for sections which
646 // require postprocessing, such as compression.
648 write_to_buffer(unsigned char* buffer
)
649 { this->do_write_to_buffer(buffer
); }
651 // Print merge stats to stderr. This should only be called for
652 // SHF_MERGE sections.
654 print_merge_stats(const char* section_name
)
655 { this->do_print_merge_stats(section_name
); }
658 // The child class must implement do_write.
660 // The child class may implement specific adjustments to the output
663 do_adjust_output_section(Output_section
*)
666 // May be implemented by child class. Return true if the section
669 do_add_input_section(Relobj
*, unsigned int)
670 { gold_unreachable(); }
672 // The child class may implement output_offset.
674 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
675 section_offset_type
*) const
678 // The child class may implement is_merge_section_for.
680 do_is_merge_section_for(const Relobj
*, unsigned int) const
683 // The child class may implement write_to_buffer. Most child
684 // classes can not appear in a compressed section, and they do not
687 do_write_to_buffer(unsigned char*)
688 { gold_unreachable(); }
690 // Print merge statistics.
692 do_print_merge_stats(const char*)
693 { gold_unreachable(); }
695 // Return the required alignment.
698 { return this->addralign_
; }
700 // Return the output section.
703 { return this->output_section_
; }
705 const Output_section
*
706 do_output_section() const
707 { return this->output_section_
; }
709 // Return the section index of the output section.
711 do_out_shndx() const;
713 // Set the alignment.
715 set_addralign(uint64_t addralign
);
718 // The output section for this section.
719 Output_section
* output_section_
;
720 // The required alignment.
724 // Some Output_section_data classes build up their data step by step,
725 // rather than all at once. This class provides an interface for
728 class Output_section_data_build
: public Output_section_data
731 Output_section_data_build(uint64_t addralign
)
732 : Output_section_data(addralign
)
735 // Get the current data size.
737 current_data_size() const
738 { return this->current_data_size_for_child(); }
740 // Set the current data size.
742 set_current_data_size(off_t data_size
)
743 { this->set_current_data_size_for_child(data_size
); }
746 // Set the final data size.
748 set_final_data_size()
749 { this->set_data_size(this->current_data_size_for_child()); }
752 // A simple case of Output_data in which we have constant data to
755 class Output_data_const
: public Output_section_data
758 Output_data_const(const std::string
& data
, uint64_t addralign
)
759 : Output_section_data(data
.size(), addralign
, true), data_(data
)
762 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
763 : Output_section_data(len
, addralign
, true), data_(p
, len
)
766 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
767 : Output_section_data(len
, addralign
, true),
768 data_(reinterpret_cast<const char*>(p
), len
)
772 // Write the data to the output file.
774 do_write(Output_file
*);
776 // Write the data to a buffer.
778 do_write_to_buffer(unsigned char* buffer
)
779 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
781 // Write to a map file.
783 do_print_to_mapfile(Mapfile
* mapfile
) const
784 { mapfile
->print_output_data(this, _("** fill")); }
790 // Another version of Output_data with constant data, in which the
791 // buffer is allocated by the caller.
793 class Output_data_const_buffer
: public Output_section_data
796 Output_data_const_buffer(const unsigned char* p
, off_t len
,
797 uint64_t addralign
, const char* map_name
)
798 : Output_section_data(len
, addralign
, true),
799 p_(p
), map_name_(map_name
)
803 // Write the data the output file.
805 do_write(Output_file
*);
807 // Write the data to a buffer.
809 do_write_to_buffer(unsigned char* buffer
)
810 { memcpy(buffer
, this->p_
, this->data_size()); }
812 // Write to a map file.
814 do_print_to_mapfile(Mapfile
* mapfile
) const
815 { mapfile
->print_output_data(this, _(this->map_name_
)); }
818 // The data to output.
819 const unsigned char* p_
;
820 // Name to use in a map file. Maps are a rarely used feature, but
821 // the space usage is minor as aren't very many of these objects.
822 const char* map_name_
;
825 // A place holder for a fixed amount of data written out via some
828 class Output_data_fixed_space
: public Output_section_data
831 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
832 const char* map_name
)
833 : Output_section_data(data_size
, addralign
, true),
838 // Write out the data--the actual data must be written out
841 do_write(Output_file
*)
844 // Write to a map file.
846 do_print_to_mapfile(Mapfile
* mapfile
) const
847 { mapfile
->print_output_data(this, _(this->map_name_
)); }
850 // Name to use in a map file. Maps are a rarely used feature, but
851 // the space usage is minor as aren't very many of these objects.
852 const char* map_name_
;
855 // A place holder for variable sized data written out via some other
858 class Output_data_space
: public Output_section_data_build
861 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
862 : Output_section_data_build(addralign
),
866 // Set the alignment.
868 set_space_alignment(uint64_t align
)
869 { this->set_addralign(align
); }
872 // Write out the data--the actual data must be written out
875 do_write(Output_file
*)
878 // Write to a map file.
880 do_print_to_mapfile(Mapfile
* mapfile
) const
881 { mapfile
->print_output_data(this, _(this->map_name_
)); }
884 // Name to use in a map file. Maps are a rarely used feature, but
885 // the space usage is minor as aren't very many of these objects.
886 const char* map_name_
;
889 // Fill fixed space with zeroes. This is just like
890 // Output_data_fixed_space, except that the map name is known.
892 class Output_data_zero_fill
: public Output_section_data
895 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
896 : Output_section_data(data_size
, addralign
, true)
900 // There is no data to write 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, "** zero fill"); }
911 // A string table which goes into an output section.
913 class Output_data_strtab
: public Output_section_data
916 Output_data_strtab(Stringpool
* strtab
)
917 : Output_section_data(1), strtab_(strtab
)
921 // This is called to set the address and file offset. Here we make
922 // sure that the Stringpool is finalized.
924 set_final_data_size();
926 // Write out the data.
928 do_write(Output_file
*);
930 // Write the data to a buffer.
932 do_write_to_buffer(unsigned char* buffer
)
933 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
935 // Write to a map file.
937 do_print_to_mapfile(Mapfile
* mapfile
) const
938 { mapfile
->print_output_data(this, _("** string table")); }
944 // This POD class is used to represent a single reloc in the output
945 // file. This could be a private class within Output_data_reloc, but
946 // the templatization is complex enough that I broke it out into a
947 // separate class. The class is templatized on either elfcpp::SHT_REL
948 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
949 // relocation or an ordinary relocation.
951 // A relocation can be against a global symbol, a local symbol, a
952 // local section symbol, an output section, or the undefined symbol at
953 // index 0. We represent the latter by using a NULL global symbol.
955 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
958 template<bool dynamic
, int size
, bool big_endian
>
959 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
962 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
963 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
965 static const Address invalid_address
= static_cast<Address
>(0) - 1;
967 // An uninitialized entry. We need this because we want to put
968 // instances of this class into an STL container.
970 : local_sym_index_(INVALID_CODE
)
973 // We have a bunch of different constructors. They come in pairs
974 // depending on how the address of the relocation is specified. It
975 // can either be an offset in an Output_data or an offset in an
978 // A reloc against a global symbol.
980 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
981 Address address
, bool is_relative
, bool is_symbolless
);
983 Output_reloc(Symbol
* gsym
, unsigned int type
,
984 Sized_relobj
<size
, big_endian
>* relobj
,
985 unsigned int shndx
, Address address
, bool is_relative
,
988 // A reloc against a local symbol or local section symbol.
990 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
991 unsigned int local_sym_index
, unsigned int type
,
992 Output_data
* od
, Address address
, bool is_relative
,
993 bool is_symbolless
, bool is_section_symbol
);
995 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
996 unsigned int local_sym_index
, unsigned int type
,
997 unsigned int shndx
, Address address
, bool is_relative
,
998 bool is_symbolless
, bool is_section_symbol
);
1000 // A reloc against the STT_SECTION symbol of an output section.
1002 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1005 Output_reloc(Output_section
* os
, unsigned int type
,
1006 Sized_relobj
<size
, big_endian
>* relobj
,
1007 unsigned int shndx
, Address address
);
1009 // An absolute relocation with no symbol.
1011 Output_reloc(unsigned int type
, Output_data
* od
, Address address
);
1013 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1014 unsigned int shndx
, Address address
);
1016 // A target specific relocation. The target will be called to get
1017 // the symbol index, passing ARG. The type and offset will be set
1018 // as for other relocation types.
1020 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1023 Output_reloc(unsigned int type
, void* arg
,
1024 Sized_relobj
<size
, big_endian
>* relobj
,
1025 unsigned int shndx
, Address address
);
1027 // Return the reloc type.
1030 { return this->type_
; }
1032 // Return whether this is a RELATIVE relocation.
1035 { return this->is_relative_
; }
1037 // Return whether this is a relocation which should not use
1038 // a symbol, but which obtains its addend from a symbol.
1040 is_symbolless() const
1041 { return this->is_symbolless_
; }
1043 // Return whether this is against a local section symbol.
1045 is_local_section_symbol() const
1047 return (this->local_sym_index_
!= GSYM_CODE
1048 && this->local_sym_index_
!= SECTION_CODE
1049 && this->local_sym_index_
!= INVALID_CODE
1050 && this->local_sym_index_
!= TARGET_CODE
1051 && this->is_section_symbol_
);
1054 // Return whether this is a target specific relocation.
1056 is_target_specific() const
1057 { return this->local_sym_index_
== TARGET_CODE
; }
1059 // Return the argument to pass to the target for a target specific
1064 gold_assert(this->local_sym_index_
== TARGET_CODE
);
1065 return this->u1_
.arg
;
1068 // For a local section symbol, return the offset of the input
1069 // section within the output section. ADDEND is the addend being
1070 // applied to the input section.
1072 local_section_offset(Addend addend
) const;
1074 // Get the value of the symbol referred to by a Rel relocation when
1075 // we are adding the given ADDEND.
1077 symbol_value(Addend addend
) const;
1079 // Write the reloc entry to an output view.
1081 write(unsigned char* pov
) const;
1083 // Write the offset and info fields to Write_rel.
1084 template<typename Write_rel
>
1085 void write_rel(Write_rel
*) const;
1087 // This is used when sorting dynamic relocs. Return -1 to sort this
1088 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1090 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1093 // Return whether this reloc should be sorted before the argument
1094 // when sorting dynamic relocs.
1096 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1098 { return this->compare(r2
) < 0; }
1101 // Record that we need a dynamic symbol index.
1103 set_needs_dynsym_index();
1105 // Return the symbol index.
1107 get_symbol_index() const;
1109 // Return the output address.
1111 get_address() const;
1113 // Codes for local_sym_index_.
1122 // Invalid uninitialized entry.
1128 // For a local symbol or local section symbol
1129 // (this->local_sym_index_ >= 0), the object. We will never
1130 // generate a relocation against a local symbol in a dynamic
1131 // object; that doesn't make sense. And our callers will always
1132 // be templatized, so we use Sized_relobj here.
1133 Sized_relobj
<size
, big_endian
>* relobj
;
1134 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1135 // symbol. If this is NULL, it indicates a relocation against the
1136 // undefined 0 symbol.
1138 // For a relocation against an output section
1139 // (this->local_sym_index_ == SECTION_CODE), the output section.
1141 // For a target specific relocation, an argument to pass to the
1147 // If this->shndx_ is not INVALID CODE, the object which holds the
1148 // input section being used to specify the reloc address.
1149 Sized_relobj
<size
, big_endian
>* relobj
;
1150 // If this->shndx_ is INVALID_CODE, the output data being used to
1151 // specify the reloc address. This may be NULL if the reloc
1152 // address is absolute.
1155 // The address offset within the input section or the Output_data.
1157 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1158 // relocation against an output section, or TARGET_CODE for a target
1159 // specific relocation, or INVALID_CODE for an uninitialized value.
1160 // Otherwise, for a local symbol (this->is_section_symbol_ is
1161 // false), the local symbol index. For a local section symbol
1162 // (this->is_section_symbol_ is true), the section index in the
1164 unsigned int local_sym_index_
;
1165 // The reloc type--a processor specific code.
1166 unsigned int type_
: 29;
1167 // True if the relocation is a RELATIVE relocation.
1168 bool is_relative_
: 1;
1169 // True if the relocation is one which should not use
1170 // a symbol, but which obtains its addend from a symbol.
1171 bool is_symbolless_
: 1;
1172 // True if the relocation is against a section symbol.
1173 bool is_section_symbol_
: 1;
1174 // If the reloc address is an input section in an object, the
1175 // section index. This is INVALID_CODE if the reloc address is
1176 // specified in some other way.
1177 unsigned int shndx_
;
1180 // The SHT_RELA version of Output_reloc<>. This is just derived from
1181 // the SHT_REL version of Output_reloc, but it adds an addend.
1183 template<bool dynamic
, int size
, bool big_endian
>
1184 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1187 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1188 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1190 // An uninitialized entry.
1195 // A reloc against a global symbol.
1197 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1198 Address address
, Addend addend
, bool is_relative
,
1200 : rel_(gsym
, type
, od
, address
, is_relative
, is_symbolless
),
1204 Output_reloc(Symbol
* gsym
, unsigned int type
,
1205 Sized_relobj
<size
, big_endian
>* relobj
,
1206 unsigned int shndx
, Address address
, Addend addend
,
1207 bool is_relative
, bool is_symbolless
)
1208 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
,
1209 is_symbolless
), addend_(addend
)
1212 // A reloc against a local symbol.
1214 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1215 unsigned int local_sym_index
, unsigned int type
,
1216 Output_data
* od
, Address address
,
1217 Addend addend
, bool is_relative
,
1218 bool is_symbolless
, bool is_section_symbol
)
1219 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1220 is_symbolless
, is_section_symbol
),
1224 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1225 unsigned int local_sym_index
, unsigned int type
,
1226 unsigned int shndx
, Address address
,
1227 Addend addend
, bool is_relative
,
1228 bool is_symbolless
, bool is_section_symbol
)
1229 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1230 is_symbolless
, is_section_symbol
),
1234 // A reloc against the STT_SECTION symbol of an output section.
1236 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1237 Address address
, Addend addend
)
1238 : rel_(os
, type
, od
, address
), addend_(addend
)
1241 Output_reloc(Output_section
* os
, unsigned int type
,
1242 Sized_relobj
<size
, big_endian
>* relobj
,
1243 unsigned int shndx
, Address address
, Addend addend
)
1244 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1247 // An absolute relocation with no symbol.
1249 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1251 : rel_(type
, od
, address
), addend_(addend
)
1254 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1255 unsigned int shndx
, Address address
, Addend addend
)
1256 : rel_(type
, relobj
, shndx
, address
), addend_(addend
)
1259 // A target specific relocation. The target will be called to get
1260 // the symbol index and the addend, passing ARG. The type and
1261 // offset will be set as for other relocation types.
1263 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1264 Address address
, Addend addend
)
1265 : rel_(type
, arg
, od
, address
), addend_(addend
)
1268 Output_reloc(unsigned int type
, void* arg
,
1269 Sized_relobj
<size
, big_endian
>* relobj
,
1270 unsigned int shndx
, Address address
, Addend addend
)
1271 : rel_(type
, arg
, relobj
, shndx
, address
), addend_(addend
)
1274 // Return whether this is a RELATIVE relocation.
1277 { return this->rel_
.is_relative(); }
1279 // Return whether this is a relocation which should not use
1280 // a symbol, but which obtains its addend from a symbol.
1282 is_symbolless() const
1283 { return this->rel_
.is_symbolless(); }
1285 // Write the reloc entry to an output view.
1287 write(unsigned char* pov
) const;
1289 // Return whether this reloc should be sorted before the argument
1290 // when sorting dynamic relocs.
1292 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1295 int i
= this->rel_
.compare(r2
.rel_
);
1301 return this->addend_
< r2
.addend_
;
1306 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1311 // Output_data_reloc_generic is a non-template base class for
1312 // Output_data_reloc_base. This gives the generic code a way to hold
1313 // a pointer to a reloc section.
1315 class Output_data_reloc_generic
: public Output_section_data_build
1318 Output_data_reloc_generic(int size
, bool sort_relocs
)
1319 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1320 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1323 // Return the number of relative relocs in this section.
1325 relative_reloc_count() const
1326 { return this->relative_reloc_count_
; }
1328 // Whether we should sort the relocs.
1331 { return this->sort_relocs_
; }
1334 // Note that we've added another relative reloc.
1336 bump_relative_reloc_count()
1337 { ++this->relative_reloc_count_
; }
1340 // The number of relative relocs added to this section. This is to
1341 // support DT_RELCOUNT.
1342 size_t relative_reloc_count_
;
1343 // Whether to sort the relocations when writing them out, to make
1344 // the dynamic linker more efficient.
1348 // Output_data_reloc is used to manage a section containing relocs.
1349 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1350 // indicates whether this is a dynamic relocation or a normal
1351 // relocation. Output_data_reloc_base is a base class.
1352 // Output_data_reloc is the real class, which we specialize based on
1355 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1356 class Output_data_reloc_base
: public Output_data_reloc_generic
1359 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1360 typedef typename
Output_reloc_type::Address Address
;
1361 static const int reloc_size
=
1362 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1364 // Construct the section.
1365 Output_data_reloc_base(bool sort_relocs
)
1366 : Output_data_reloc_generic(size
, sort_relocs
)
1370 // Write out the data.
1372 do_write(Output_file
*);
1374 // Set the entry size and the link.
1376 do_adjust_output_section(Output_section
*os
);
1378 // Write to a map file.
1380 do_print_to_mapfile(Mapfile
* mapfile
) const
1382 mapfile
->print_output_data(this,
1384 ? _("** dynamic relocs")
1388 // Add a relocation entry.
1390 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1392 this->relocs_
.push_back(reloc
);
1393 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1394 od
->add_dynamic_reloc();
1395 if (reloc
.is_relative())
1396 this->bump_relative_reloc_count();
1400 typedef std::vector
<Output_reloc_type
> Relocs
;
1402 // The class used to sort the relocations.
1403 struct Sort_relocs_comparison
1406 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1407 { return r1
.sort_before(r2
); }
1410 // The relocations in this section.
1414 // The class which callers actually create.
1416 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1417 class Output_data_reloc
;
1419 // The SHT_REL version of Output_data_reloc.
1421 template<bool dynamic
, int size
, bool big_endian
>
1422 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1423 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1426 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1430 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1431 typedef typename
Output_reloc_type::Address Address
;
1433 Output_data_reloc(bool sr
)
1434 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1437 // Add a reloc against a global symbol.
1440 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1441 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, false)); }
1444 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1445 Sized_relobj
<size
, big_endian
>* relobj
,
1446 unsigned int shndx
, Address address
)
1447 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1450 // These are to simplify the Copy_relocs class.
1453 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1456 gold_assert(addend
== 0);
1457 this->add_global(gsym
, type
, od
, address
);
1461 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1462 Sized_relobj
<size
, big_endian
>* relobj
,
1463 unsigned int shndx
, Address address
, Address addend
)
1465 gold_assert(addend
== 0);
1466 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1469 // Add a RELATIVE reloc against a global symbol. The final relocation
1470 // will not reference the symbol.
1473 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1475 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true, true)); }
1478 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1479 Sized_relobj
<size
, big_endian
>* relobj
,
1480 unsigned int shndx
, Address address
)
1482 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1486 // Add a global relocation which does not use a symbol for the relocation,
1487 // but which gets its addend from a symbol.
1490 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1491 Output_data
* od
, Address address
)
1492 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, true)); }
1495 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1497 Sized_relobj
<size
, big_endian
>* relobj
,
1498 unsigned int shndx
, Address address
)
1500 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1504 // Add a reloc against a local symbol.
1507 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1508 unsigned int local_sym_index
, unsigned int type
,
1509 Output_data
* od
, Address address
)
1511 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1512 address
, false, false, false));
1516 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1517 unsigned int local_sym_index
, unsigned int type
,
1518 Output_data
* od
, unsigned int shndx
, Address address
)
1520 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1521 address
, false, false, false));
1524 // Add a RELATIVE reloc against a local symbol.
1527 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1528 unsigned int local_sym_index
, unsigned int type
,
1529 Output_data
* od
, Address address
)
1531 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1532 address
, true, true, false));
1536 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1537 unsigned int local_sym_index
, unsigned int type
,
1538 Output_data
* od
, unsigned int shndx
, Address address
)
1540 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1541 address
, true, true, false));
1544 // Add a local relocation which does not use a symbol for the relocation,
1545 // but which gets its addend from a symbol.
1548 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1549 unsigned int local_sym_index
, unsigned int type
,
1550 Output_data
* od
, Address address
)
1552 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1553 address
, false, true, false));
1557 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1558 unsigned int local_sym_index
, unsigned int type
,
1559 Output_data
* od
, unsigned int shndx
,
1562 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1563 address
, false, true, false));
1566 // Add a reloc against a local section symbol. This will be
1567 // converted into a reloc against the STT_SECTION symbol of the
1571 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1572 unsigned int input_shndx
, unsigned int type
,
1573 Output_data
* od
, Address address
)
1575 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1576 address
, false, false, true));
1580 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1581 unsigned int input_shndx
, unsigned int type
,
1582 Output_data
* od
, unsigned int shndx
, Address address
)
1584 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1585 address
, false, false, true));
1588 // A reloc against the STT_SECTION symbol of an output section.
1589 // OS is the Output_section that the relocation refers to; OD is
1590 // the Output_data object being relocated.
1593 add_output_section(Output_section
* os
, unsigned int type
,
1594 Output_data
* od
, Address address
)
1595 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1598 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1599 Sized_relobj
<size
, big_endian
>* relobj
,
1600 unsigned int shndx
, Address address
)
1601 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1603 // Add an absolute relocation.
1606 add_absolute(unsigned int type
, Output_data
* od
, Address address
)
1607 { this->add(od
, Output_reloc_type(type
, od
, address
)); }
1610 add_absolute(unsigned int type
, Output_data
* od
,
1611 Sized_relobj
<size
, big_endian
>* relobj
,
1612 unsigned int shndx
, Address address
)
1613 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
)); }
1615 // Add a target specific relocation. A target which calls this must
1616 // define the reloc_symbol_index and reloc_addend virtual functions.
1619 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1621 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
)); }
1624 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1625 Sized_relobj
<size
, big_endian
>* relobj
,
1626 unsigned int shndx
, Address address
)
1627 { this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
)); }
1630 // The SHT_RELA version of Output_data_reloc.
1632 template<bool dynamic
, int size
, bool big_endian
>
1633 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1634 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1637 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1641 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1642 typedef typename
Output_reloc_type::Address Address
;
1643 typedef typename
Output_reloc_type::Addend Addend
;
1645 Output_data_reloc(bool sr
)
1646 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1649 // Add a reloc against a global symbol.
1652 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1653 Address address
, Addend addend
)
1654 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1658 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1659 Sized_relobj
<size
, big_endian
>* relobj
,
1660 unsigned int shndx
, Address address
,
1662 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1663 addend
, false, false)); }
1665 // Add a RELATIVE reloc against a global symbol. The final output
1666 // relocation will not reference the symbol, but we must keep the symbol
1667 // information long enough to set the addend of the relocation correctly
1668 // when it is written.
1671 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1672 Address address
, Addend addend
)
1673 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true,
1677 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1678 Sized_relobj
<size
, big_endian
>* relobj
,
1679 unsigned int shndx
, Address address
, Addend addend
)
1680 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1681 addend
, true, true)); }
1683 // Add a global relocation which does not use a symbol for the relocation,
1684 // but which gets its addend from a symbol.
1687 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1688 Address address
, Addend addend
)
1689 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1693 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1695 Sized_relobj
<size
, big_endian
>* relobj
,
1696 unsigned int shndx
, Address address
, Addend addend
)
1697 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1698 addend
, false, true)); }
1700 // Add a reloc against a local symbol.
1703 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1704 unsigned int local_sym_index
, unsigned int type
,
1705 Output_data
* od
, Address address
, Addend addend
)
1707 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1708 addend
, false, false, false));
1712 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1713 unsigned int local_sym_index
, unsigned int type
,
1714 Output_data
* od
, unsigned int shndx
, Address address
,
1717 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1718 address
, addend
, false, false, false));
1721 // Add a RELATIVE reloc against a local symbol.
1724 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1725 unsigned int local_sym_index
, unsigned int type
,
1726 Output_data
* od
, Address address
, Addend addend
)
1728 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1729 addend
, true, true, false));
1733 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1734 unsigned int local_sym_index
, unsigned int type
,
1735 Output_data
* od
, unsigned int shndx
, Address address
,
1738 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1739 address
, addend
, true, true, false));
1742 // Add a local relocation which does not use a symbol for the relocation,
1743 // but which gets it's addend from a symbol.
1746 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1747 unsigned int local_sym_index
, unsigned int type
,
1748 Output_data
* od
, Address address
, Addend addend
)
1750 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1751 addend
, false, true, false));
1755 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1756 unsigned int local_sym_index
, unsigned int type
,
1757 Output_data
* od
, unsigned int shndx
,
1758 Address address
, Addend addend
)
1760 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1761 address
, addend
, false, true, false));
1764 // Add a reloc against a local section symbol. This will be
1765 // converted into a reloc against the STT_SECTION symbol of the
1769 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1770 unsigned int input_shndx
, unsigned int type
,
1771 Output_data
* od
, Address address
, Addend addend
)
1773 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1774 addend
, false, false, true));
1778 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1779 unsigned int input_shndx
, unsigned int type
,
1780 Output_data
* od
, unsigned int shndx
, Address address
,
1783 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1784 address
, addend
, false, false, true));
1787 // A reloc against the STT_SECTION symbol of an output section.
1790 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1791 Address address
, Addend addend
)
1792 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1795 add_output_section(Output_section
* os
, unsigned int type
,
1796 Sized_relobj
<size
, big_endian
>* relobj
,
1797 unsigned int shndx
, Address address
, Addend addend
)
1798 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1801 // Add an absolute relocation.
1804 add_absolute(unsigned int type
, Output_data
* od
, Address address
,
1806 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
)); }
1809 add_absolute(unsigned int type
, Output_data
* od
,
1810 Sized_relobj
<size
, big_endian
>* relobj
,
1811 unsigned int shndx
, Address address
, Addend addend
)
1812 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
)); }
1814 // Add a target specific relocation. A target which calls this must
1815 // define the reloc_symbol_index and reloc_addend virtual functions.
1818 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1819 Address address
, Addend addend
)
1820 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
, addend
)); }
1823 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1824 Sized_relobj
<size
, big_endian
>* relobj
,
1825 unsigned int shndx
, Address address
, Addend addend
)
1827 this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
,
1832 // Output_relocatable_relocs represents a relocation section in a
1833 // relocatable link. The actual data is written out in the target
1834 // hook relocate_for_relocatable. This just saves space for it.
1836 template<int sh_type
, int size
, bool big_endian
>
1837 class Output_relocatable_relocs
: public Output_section_data
1840 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1841 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1846 set_final_data_size();
1848 // Write out the data. There is nothing to do here.
1850 do_write(Output_file
*)
1853 // Write to a map file.
1855 do_print_to_mapfile(Mapfile
* mapfile
) const
1856 { mapfile
->print_output_data(this, _("** relocs")); }
1859 // The relocs associated with this input section.
1860 Relocatable_relocs
* rr_
;
1863 // Handle a GROUP section.
1865 template<int size
, bool big_endian
>
1866 class Output_data_group
: public Output_section_data
1869 // The constructor clears *INPUT_SHNDXES.
1870 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1871 section_size_type entry_count
,
1872 elfcpp::Elf_Word flags
,
1873 std::vector
<unsigned int>* input_shndxes
);
1876 do_write(Output_file
*);
1878 // Write to a map file.
1880 do_print_to_mapfile(Mapfile
* mapfile
) const
1881 { mapfile
->print_output_data(this, _("** group")); }
1883 // Set final data size.
1885 set_final_data_size()
1886 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1889 // The input object.
1890 Sized_relobj
<size
, big_endian
>* relobj_
;
1891 // The group flag word.
1892 elfcpp::Elf_Word flags_
;
1893 // The section indexes of the input sections in this group.
1894 std::vector
<unsigned int> input_shndxes_
;
1897 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1898 // for one symbol--either a global symbol or a local symbol in an
1899 // object. The target specific code adds entries to the GOT as
1902 template<int size
, bool big_endian
>
1903 class Output_data_got
: public Output_section_data_build
1906 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1907 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1908 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1911 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1915 // Add an entry for a global symbol to the GOT. Return true if this
1916 // is a new GOT entry, false if the symbol was already in the GOT.
1918 add_global(Symbol
* gsym
, unsigned int got_type
);
1920 // Like add_global, but use the PLT offset of the global symbol if
1923 add_global_plt(Symbol
* gsym
, unsigned int got_type
);
1925 // Add an entry for a global symbol to the GOT, and add a dynamic
1926 // relocation of type R_TYPE for the GOT entry.
1928 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1929 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1932 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1933 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1935 // Add a pair of entries for a global symbol to the GOT, and add
1936 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1938 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1939 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1940 unsigned int r_type_2
);
1943 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1944 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1945 unsigned int r_type_2
);
1947 // Add an entry for a local symbol to the GOT. This returns true if
1948 // this is a new GOT entry, false if the symbol already has a GOT
1951 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1952 unsigned int got_type
);
1954 // Like add_local, but use the PLT offset of the local symbol if it
1957 add_local_plt(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1958 unsigned int got_type
);
1960 // Add an entry for a local symbol to the GOT, and add a dynamic
1961 // relocation of type R_TYPE for the GOT entry.
1963 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1964 unsigned int sym_index
, unsigned int got_type
,
1965 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1968 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1969 unsigned int sym_index
, unsigned int got_type
,
1970 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1972 // Add a pair of entries for a local symbol to the GOT, and add
1973 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1975 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1976 unsigned int sym_index
, unsigned int shndx
,
1977 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1978 unsigned int r_type_1
, unsigned int r_type_2
);
1981 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1982 unsigned int sym_index
, unsigned int shndx
,
1983 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1984 unsigned int r_type_1
, unsigned int r_type_2
);
1986 // Add a constant to the GOT. This returns the offset of the new
1987 // entry from the start of the GOT.
1989 add_constant(Valtype constant
)
1991 this->entries_
.push_back(Got_entry(constant
));
1992 this->set_got_size();
1993 return this->last_got_offset();
1997 // Write out the GOT table.
1999 do_write(Output_file
*);
2001 // Write to a map file.
2003 do_print_to_mapfile(Mapfile
* mapfile
) const
2004 { mapfile
->print_output_data(this, _("** GOT")); }
2007 // This POD class holds a single GOT entry.
2011 // Create a zero entry.
2013 : local_sym_index_(CONSTANT_CODE
), use_plt_offset_(false)
2014 { this->u_
.constant
= 0; }
2016 // Create a global symbol entry.
2017 Got_entry(Symbol
* gsym
, bool use_plt_offset
)
2018 : local_sym_index_(GSYM_CODE
), use_plt_offset_(use_plt_offset
)
2019 { this->u_
.gsym
= gsym
; }
2021 // Create a local symbol entry.
2022 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
2023 unsigned int local_sym_index
, bool use_plt_offset
)
2024 : local_sym_index_(local_sym_index
), use_plt_offset_(use_plt_offset
)
2026 gold_assert(local_sym_index
!= GSYM_CODE
2027 && local_sym_index
!= CONSTANT_CODE
2028 && local_sym_index
== this->local_sym_index_
);
2029 this->u_
.object
= object
;
2032 // Create a constant entry. The constant is a host value--it will
2033 // be swapped, if necessary, when it is written out.
2034 explicit Got_entry(Valtype constant
)
2035 : local_sym_index_(CONSTANT_CODE
), use_plt_offset_(false)
2036 { this->u_
.constant
= constant
; }
2038 // Write the GOT entry to an output view.
2040 write(unsigned char* pov
) const;
2045 GSYM_CODE
= 0x7fffffff,
2046 CONSTANT_CODE
= 0x7ffffffe
2051 // For a local symbol, the object.
2052 Sized_relobj
<size
, big_endian
>* object
;
2053 // For a global symbol, the symbol.
2055 // For a constant, the constant.
2058 // For a local symbol, the local symbol index. This is GSYM_CODE
2059 // for a global symbol, or CONSTANT_CODE for a constant.
2060 unsigned int local_sym_index_
: 31;
2061 // Whether to use the PLT offset of the symbol if it has one.
2062 bool use_plt_offset_
: 1;
2065 typedef std::vector
<Got_entry
> Got_entries
;
2067 // Return the offset into the GOT of GOT entry I.
2069 got_offset(unsigned int i
) const
2070 { return i
* (size
/ 8); }
2072 // Return the offset into the GOT of the last entry added.
2074 last_got_offset() const
2075 { return this->got_offset(this->entries_
.size() - 1); }
2077 // Set the size of the section.
2080 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
2082 // The list of GOT entries.
2083 Got_entries entries_
;
2086 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2089 class Output_data_dynamic
: public Output_section_data
2092 Output_data_dynamic(Stringpool
* pool
)
2093 : Output_section_data(Output_data::default_alignment()),
2094 entries_(), pool_(pool
)
2097 // Add a new dynamic entry with a fixed numeric value.
2099 add_constant(elfcpp::DT tag
, unsigned int val
)
2100 { this->add_entry(Dynamic_entry(tag
, val
)); }
2102 // Add a new dynamic entry with the address of output data.
2104 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
2105 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
2107 // Add a new dynamic entry with the address of output data
2108 // plus a constant offset.
2110 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
2111 unsigned int offset
)
2112 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
2114 // Add a new dynamic entry with the size of output data.
2116 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
2117 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
2119 // Add a new dynamic entry with the total size of two output datas.
2121 add_section_size(elfcpp::DT tag
, const Output_data
* od
,
2122 const Output_data
* od2
)
2123 { this->add_entry(Dynamic_entry(tag
, od
, od2
)); }
2125 // Add a new dynamic entry with the address of a symbol.
2127 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
2128 { this->add_entry(Dynamic_entry(tag
, sym
)); }
2130 // Add a new dynamic entry with a string.
2132 add_string(elfcpp::DT tag
, const char* str
)
2133 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
2136 add_string(elfcpp::DT tag
, const std::string
& str
)
2137 { this->add_string(tag
, str
.c_str()); }
2140 // Adjust the output section to set the entry size.
2142 do_adjust_output_section(Output_section
*);
2144 // Set the final data size.
2146 set_final_data_size();
2148 // Write out the dynamic entries.
2150 do_write(Output_file
*);
2152 // Write to a map file.
2154 do_print_to_mapfile(Mapfile
* mapfile
) const
2155 { mapfile
->print_output_data(this, _("** dynamic")); }
2158 // This POD class holds a single dynamic entry.
2162 // Create an entry with a fixed numeric value.
2163 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
2164 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
2165 { this->u_
.val
= val
; }
2167 // Create an entry with the size or address of a section.
2168 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
2170 offset_(section_size
2171 ? DYNAMIC_SECTION_SIZE
2172 : DYNAMIC_SECTION_ADDRESS
)
2178 // Create an entry with the size of two sections.
2179 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, const Output_data
* od2
)
2181 offset_(DYNAMIC_SECTION_SIZE
)
2187 // Create an entry with the address of a section plus a constant offset.
2188 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
2191 { this->u_
.od
= od
; }
2193 // Create an entry with the address of a symbol.
2194 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
2195 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
2196 { this->u_
.sym
= sym
; }
2198 // Create an entry with a string.
2199 Dynamic_entry(elfcpp::DT tag
, const char* str
)
2200 : tag_(tag
), offset_(DYNAMIC_STRING
)
2201 { this->u_
.str
= str
; }
2203 // Return the tag of this entry.
2206 { return this->tag_
; }
2208 // Write the dynamic entry to an output view.
2209 template<int size
, bool big_endian
>
2211 write(unsigned char* pov
, const Stringpool
*) const;
2214 // Classification is encoded in the OFFSET field.
2218 DYNAMIC_SECTION_ADDRESS
= 0,
2220 DYNAMIC_NUMBER
= -1U,
2222 DYNAMIC_SECTION_SIZE
= -2U,
2224 DYNAMIC_SYMBOL
= -3U,
2226 DYNAMIC_STRING
= -4U
2227 // Any other value indicates a section address plus OFFSET.
2232 // For DYNAMIC_NUMBER.
2234 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2235 const Output_data
* od
;
2236 // For DYNAMIC_SYMBOL.
2238 // For DYNAMIC_STRING.
2241 // For DYNAMIC_SYMBOL with two sections.
2242 const Output_data
* od2
;
2245 // The type of entry (Classification) or offset within a section.
2246 unsigned int offset_
;
2249 // Add an entry to the list.
2251 add_entry(const Dynamic_entry
& entry
)
2252 { this->entries_
.push_back(entry
); }
2254 // Sized version of write function.
2255 template<int size
, bool big_endian
>
2257 sized_write(Output_file
* of
);
2259 // The type of the list of entries.
2260 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2263 Dynamic_entries entries_
;
2264 // The pool used for strings.
2268 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2269 // which may be required if the object file has more than
2270 // SHN_LORESERVE sections.
2272 class Output_symtab_xindex
: public Output_section_data
2275 Output_symtab_xindex(size_t symcount
)
2276 : Output_section_data(symcount
* 4, 4, true),
2280 // Add an entry: symbol number SYMNDX has section SHNDX.
2282 add(unsigned int symndx
, unsigned int shndx
)
2283 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2287 do_write(Output_file
*);
2289 // Write to a map file.
2291 do_print_to_mapfile(Mapfile
* mapfile
) const
2292 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2295 template<bool big_endian
>
2297 endian_do_write(unsigned char*);
2299 // It is likely that most symbols will not require entries. Rather
2300 // than keep a vector for all symbols, we keep pairs of symbol index
2301 // and section index.
2302 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2304 // The entries we need.
2305 Xindex_entries entries_
;
2308 // A relaxed input section.
2309 class Output_relaxed_input_section
: public Output_section_data_build
2312 // We would like to call relobj->section_addralign(shndx) to get the
2313 // alignment but we do not want the constructor to fail. So callers
2314 // are repsonsible for ensuring that.
2315 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2317 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2320 // Return the Relobj of this relaxed input section.
2323 { return this->relobj_
; }
2325 // Return the section index of this relaxed input section.
2328 { return this->shndx_
; }
2332 unsigned int shndx_
;
2335 // This class describes properties of merge data sections. It is used
2336 // as a key type for maps.
2337 class Merge_section_properties
2340 Merge_section_properties(bool is_string
, uint64_t entsize
,
2342 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
2345 // Whether this equals to another Merge_section_properties MSP.
2347 eq(const Merge_section_properties
& msp
) const
2349 return ((this->is_string_
== msp
.is_string_
)
2350 && (this->entsize_
== msp
.entsize_
)
2351 && (this->addralign_
== msp
.addralign_
));
2354 // Compute a hash value for this using 64-bit FNV-1a hash.
2358 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
2359 uint64_t prime
= 1099511628211ULL;
2360 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
2361 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
2362 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
2366 // Functors for associative containers.
2370 operator()(const Merge_section_properties
& msp1
,
2371 const Merge_section_properties
& msp2
) const
2372 { return msp1
.eq(msp2
); }
2378 operator()(const Merge_section_properties
& msp
) const
2379 { return msp
.hash_value(); }
2383 // Whether this merge data section is for strings.
2385 // Entsize of this merge data section.
2387 // Address alignment.
2388 uint64_t addralign_
;
2391 // This class is used to speed up look up of special input sections in an
2394 class Output_section_lookup_maps
2397 Output_section_lookup_maps()
2398 : is_valid_(true), merge_sections_by_properties_(),
2399 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2402 // Whether the maps are valid.
2405 { return this->is_valid_
; }
2407 // Invalidate the maps.
2410 { this->is_valid_
= false; }
2416 this->merge_sections_by_properties_
.clear();
2417 this->merge_sections_by_id_
.clear();
2418 this->relaxed_input_sections_by_id_
.clear();
2419 // A cleared map is valid.
2420 this->is_valid_
= true;
2423 // Find a merge section by merge section properties. Return NULL if none
2426 find_merge_section(const Merge_section_properties
& msp
) const
2428 gold_assert(this->is_valid_
);
2429 Merge_sections_by_properties::const_iterator p
=
2430 this->merge_sections_by_properties_
.find(msp
);
2431 return p
!= this->merge_sections_by_properties_
.end() ? p
->second
: NULL
;
2434 // Find a merge section by section ID of a merge input section. Return NULL
2435 // if none is found.
2437 find_merge_section(const Object
* object
, unsigned int shndx
) const
2439 gold_assert(this->is_valid_
);
2440 Merge_sections_by_id::const_iterator p
=
2441 this->merge_sections_by_id_
.find(Const_section_id(object
, shndx
));
2442 return p
!= this->merge_sections_by_id_
.end() ? p
->second
: NULL
;
2445 // Add a merge section pointed by POMB with properties MSP.
2447 add_merge_section(const Merge_section_properties
& msp
,
2448 Output_merge_base
* pomb
)
2450 std::pair
<Merge_section_properties
, Output_merge_base
*> value(msp
, pomb
);
2451 std::pair
<Merge_sections_by_properties::iterator
, bool> result
=
2452 this->merge_sections_by_properties_
.insert(value
);
2453 gold_assert(result
.second
);
2456 // Add a mapping from a merged input section in OBJECT with index SHNDX
2457 // to a merge output section pointed by POMB.
2459 add_merge_input_section(const Object
* object
, unsigned int shndx
,
2460 Output_merge_base
* pomb
)
2462 Const_section_id
csid(object
, shndx
);
2463 std::pair
<Const_section_id
, Output_merge_base
*> value(csid
, pomb
);
2464 std::pair
<Merge_sections_by_id::iterator
, bool> result
=
2465 this->merge_sections_by_id_
.insert(value
);
2466 gold_assert(result
.second
);
2469 // Find a relaxed input section of OBJECT with index SHNDX.
2470 Output_relaxed_input_section
*
2471 find_relaxed_input_section(const Object
* object
, unsigned int shndx
) const
2473 gold_assert(this->is_valid_
);
2474 Relaxed_input_sections_by_id::const_iterator p
=
2475 this->relaxed_input_sections_by_id_
.find(Const_section_id(object
, shndx
));
2476 return p
!= this->relaxed_input_sections_by_id_
.end() ? p
->second
: NULL
;
2479 // Add a relaxed input section pointed by POMB and whose original input
2480 // section is in OBJECT with index SHNDX.
2482 add_relaxed_input_section(const Relobj
* relobj
, unsigned int shndx
,
2483 Output_relaxed_input_section
* poris
)
2485 Const_section_id
csid(relobj
, shndx
);
2486 std::pair
<Const_section_id
, Output_relaxed_input_section
*>
2488 std::pair
<Relaxed_input_sections_by_id::iterator
, bool> result
=
2489 this->relaxed_input_sections_by_id_
.insert(value
);
2490 gold_assert(result
.second
);
2494 typedef Unordered_map
<Const_section_id
, Output_merge_base
*,
2495 Const_section_id_hash
>
2496 Merge_sections_by_id
;
2498 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
2499 Merge_section_properties::hash
,
2500 Merge_section_properties::equal_to
>
2501 Merge_sections_by_properties
;
2503 typedef Unordered_map
<Const_section_id
, Output_relaxed_input_section
*,
2504 Const_section_id_hash
>
2505 Relaxed_input_sections_by_id
;
2507 // Whether this is valid
2509 // Merge sections by merge section properties.
2510 Merge_sections_by_properties merge_sections_by_properties_
;
2511 // Merge sections by section IDs.
2512 Merge_sections_by_id merge_sections_by_id_
;
2513 // Relaxed sections by section IDs.
2514 Relaxed_input_sections_by_id relaxed_input_sections_by_id_
;
2517 // An output section. We don't expect to have too many output
2518 // sections, so we don't bother to do a template on the size.
2520 class Output_section
: public Output_data
2523 // Create an output section, giving the name, type, and flags.
2524 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2525 virtual ~Output_section();
2527 // Add a new input section SHNDX, named NAME, with header SHDR, from
2528 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2529 // which applies to this section, or 0 if none, or -1 if more than
2530 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2531 // in a linker script; in that case we need to keep track of input
2532 // sections associated with an output section. Return the offset
2533 // within the output section.
2534 template<int size
, bool big_endian
>
2536 add_input_section(Layout
* layout
, Sized_relobj
<size
, big_endian
>* object
,
2537 unsigned int shndx
, const char *name
,
2538 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2539 unsigned int reloc_shndx
, bool have_sections_script
);
2541 // Add generated data POSD to this output section.
2543 add_output_section_data(Output_section_data
* posd
);
2545 // Add a relaxed input section PORIS to this output section.
2547 add_relaxed_input_section(Output_relaxed_input_section
* poris
);
2549 // Return the section name.
2552 { return this->name_
; }
2554 // Return the section type.
2557 { return this->type_
; }
2559 // Return the section flags.
2562 { return this->flags_
; }
2564 // Update the output section flags based on input section flags.
2566 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2568 // Return the entsize field.
2571 { return this->entsize_
; }
2573 // Set the entsize field.
2575 set_entsize(uint64_t v
);
2577 // Set the load address.
2579 set_load_address(uint64_t load_address
)
2581 this->load_address_
= load_address
;
2582 this->has_load_address_
= true;
2585 // Set the link field to the output section index of a section.
2587 set_link_section(const Output_data
* od
)
2589 gold_assert(this->link_
== 0
2590 && !this->should_link_to_symtab_
2591 && !this->should_link_to_dynsym_
);
2592 this->link_section_
= od
;
2595 // Set the link field to a constant.
2597 set_link(unsigned int v
)
2599 gold_assert(this->link_section_
== NULL
2600 && !this->should_link_to_symtab_
2601 && !this->should_link_to_dynsym_
);
2605 // Record that this section should link to the normal symbol table.
2607 set_should_link_to_symtab()
2609 gold_assert(this->link_section_
== NULL
2611 && !this->should_link_to_dynsym_
);
2612 this->should_link_to_symtab_
= true;
2615 // Record that this section should link to the dynamic symbol table.
2617 set_should_link_to_dynsym()
2619 gold_assert(this->link_section_
== NULL
2621 && !this->should_link_to_symtab_
);
2622 this->should_link_to_dynsym_
= true;
2625 // Return the info field.
2629 gold_assert(this->info_section_
== NULL
2630 && this->info_symndx_
== NULL
);
2634 // Set the info field to the output section index of a section.
2636 set_info_section(const Output_section
* os
)
2638 gold_assert((this->info_section_
== NULL
2639 || (this->info_section_
== os
2640 && this->info_uses_section_index_
))
2641 && this->info_symndx_
== NULL
2642 && this->info_
== 0);
2643 this->info_section_
= os
;
2644 this->info_uses_section_index_
= true;
2647 // Set the info field to the symbol table index of a symbol.
2649 set_info_symndx(const Symbol
* sym
)
2651 gold_assert(this->info_section_
== NULL
2652 && (this->info_symndx_
== NULL
2653 || this->info_symndx_
== sym
)
2654 && this->info_
== 0);
2655 this->info_symndx_
= sym
;
2658 // Set the info field to the symbol table index of a section symbol.
2660 set_info_section_symndx(const Output_section
* os
)
2662 gold_assert((this->info_section_
== NULL
2663 || (this->info_section_
== os
2664 && !this->info_uses_section_index_
))
2665 && this->info_symndx_
== NULL
2666 && this->info_
== 0);
2667 this->info_section_
= os
;
2668 this->info_uses_section_index_
= false;
2671 // Set the info field to a constant.
2673 set_info(unsigned int v
)
2675 gold_assert(this->info_section_
== NULL
2676 && this->info_symndx_
== NULL
2677 && (this->info_
== 0
2678 || this->info_
== v
));
2682 // Set the addralign field.
2684 set_addralign(uint64_t v
)
2685 { this->addralign_
= v
; }
2687 // Whether the output section index has been set.
2689 has_out_shndx() const
2690 { return this->out_shndx_
!= -1U; }
2692 // Indicate that we need a symtab index.
2694 set_needs_symtab_index()
2695 { this->needs_symtab_index_
= true; }
2697 // Return whether we need a symtab index.
2699 needs_symtab_index() const
2700 { return this->needs_symtab_index_
; }
2702 // Get the symtab index.
2704 symtab_index() const
2706 gold_assert(this->symtab_index_
!= 0);
2707 return this->symtab_index_
;
2710 // Set the symtab index.
2712 set_symtab_index(unsigned int index
)
2714 gold_assert(index
!= 0);
2715 this->symtab_index_
= index
;
2718 // Indicate that we need a dynsym index.
2720 set_needs_dynsym_index()
2721 { this->needs_dynsym_index_
= true; }
2723 // Return whether we need a dynsym index.
2725 needs_dynsym_index() const
2726 { return this->needs_dynsym_index_
; }
2728 // Get the dynsym index.
2730 dynsym_index() const
2732 gold_assert(this->dynsym_index_
!= 0);
2733 return this->dynsym_index_
;
2736 // Set the dynsym index.
2738 set_dynsym_index(unsigned int index
)
2740 gold_assert(index
!= 0);
2741 this->dynsym_index_
= index
;
2744 // Return whether the input sections sections attachd to this output
2745 // section may require sorting. This is used to handle constructor
2746 // priorities compatibly with GNU ld.
2748 may_sort_attached_input_sections() const
2749 { return this->may_sort_attached_input_sections_
; }
2751 // Record that the input sections attached to this output section
2752 // may require sorting.
2754 set_may_sort_attached_input_sections()
2755 { this->may_sort_attached_input_sections_
= true; }
2757 // Returns true if input sections must be sorted according to the
2758 // order in which their name appear in the --section-ordering-file.
2760 input_section_order_specified()
2761 { return this->input_section_order_specified_
; }
2763 // Record that input sections must be sorted as some of their names
2764 // match the patterns specified through --section-ordering-file.
2766 set_input_section_order_specified()
2767 { this->input_section_order_specified_
= true; }
2769 // Return whether the input sections attached to this output section
2770 // require sorting. This is used to handle constructor priorities
2771 // compatibly with GNU ld.
2773 must_sort_attached_input_sections() const
2774 { return this->must_sort_attached_input_sections_
; }
2776 // Record that the input sections attached to this output section
2779 set_must_sort_attached_input_sections()
2780 { this->must_sort_attached_input_sections_
= true; }
2782 // Get the order in which this section appears in the PT_LOAD output
2784 Output_section_order
2786 { return this->order_
; }
2788 // Set the order for this section.
2790 set_order(Output_section_order order
)
2791 { this->order_
= order
; }
2793 // Return whether this section holds relro data--data which has
2794 // dynamic relocations but which may be marked read-only after the
2795 // dynamic relocations have been completed.
2798 { return this->is_relro_
; }
2800 // Record that this section holds relro data.
2803 { this->is_relro_
= true; }
2805 // Record that this section does not hold relro data.
2808 { this->is_relro_
= false; }
2810 // True if this is a small section: a section which holds small
2813 is_small_section() const
2814 { return this->is_small_section_
; }
2816 // Record that this is a small section.
2818 set_is_small_section()
2819 { this->is_small_section_
= true; }
2821 // True if this is a large section: a section which holds large
2824 is_large_section() const
2825 { return this->is_large_section_
; }
2827 // Record that this is a large section.
2829 set_is_large_section()
2830 { this->is_large_section_
= true; }
2832 // True if this is a large data (not BSS) section.
2834 is_large_data_section()
2835 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2837 // Return whether this section should be written after all the input
2838 // sections are complete.
2840 after_input_sections() const
2841 { return this->after_input_sections_
; }
2843 // Record that this section should be written after all the input
2844 // sections are complete.
2846 set_after_input_sections()
2847 { this->after_input_sections_
= true; }
2849 // Return whether this section requires postprocessing after all
2850 // relocations have been applied.
2852 requires_postprocessing() const
2853 { return this->requires_postprocessing_
; }
2855 // If a section requires postprocessing, return the buffer to use.
2857 postprocessing_buffer() const
2859 gold_assert(this->postprocessing_buffer_
!= NULL
);
2860 return this->postprocessing_buffer_
;
2863 // If a section requires postprocessing, create the buffer to use.
2865 create_postprocessing_buffer();
2867 // If a section requires postprocessing, this is the size of the
2868 // buffer to which relocations should be applied.
2870 postprocessing_buffer_size() const
2871 { return this->current_data_size_for_child(); }
2873 // Modify the section name. This is only permitted for an
2874 // unallocated section, and only before the size has been finalized.
2875 // Otherwise the name will not get into Layout::namepool_.
2877 set_name(const char* newname
)
2879 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2880 gold_assert(!this->is_data_size_valid());
2881 this->name_
= newname
;
2884 // Return whether the offset OFFSET in the input section SHNDX in
2885 // object OBJECT is being included in the link.
2887 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2888 off_t offset
) const;
2890 // Return the offset within the output section of OFFSET relative to
2891 // the start of input section SHNDX in object OBJECT.
2893 output_offset(const Relobj
* object
, unsigned int shndx
,
2894 section_offset_type offset
) const;
2896 // Return the output virtual address of OFFSET relative to the start
2897 // of input section SHNDX in object OBJECT.
2899 output_address(const Relobj
* object
, unsigned int shndx
,
2900 off_t offset
) const;
2902 // Look for the merged section for input section SHNDX in object
2903 // OBJECT. If found, return true, and set *ADDR to the address of
2904 // the start of the merged section. This is not necessary the
2905 // output offset corresponding to input offset 0 in the section,
2906 // since the section may be mapped arbitrarily.
2908 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2909 uint64_t* addr
) const;
2911 // Record that this output section was found in the SECTIONS clause
2912 // of a linker script.
2914 set_found_in_sections_clause()
2915 { this->found_in_sections_clause_
= true; }
2917 // Return whether this output section was found in the SECTIONS
2918 // clause of a linker script.
2920 found_in_sections_clause() const
2921 { return this->found_in_sections_clause_
; }
2923 // Write the section header into *OPHDR.
2924 template<int size
, bool big_endian
>
2926 write_header(const Layout
*, const Stringpool
*,
2927 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2929 // The next few calls are for linker script support.
2931 // In some cases we need to keep a list of the input sections
2932 // associated with this output section. We only need the list if we
2933 // might have to change the offsets of the input section within the
2934 // output section after we add the input section. The ordinary
2935 // input sections will be written out when we process the object
2936 // file, and as such we don't need to track them here. We do need
2937 // to track Output_section_data objects here. We store instances of
2938 // this structure in a std::vector, so it must be a POD. There can
2939 // be many instances of this structure, so we use a union to save
2945 : shndx_(0), p2align_(0)
2947 this->u1_
.data_size
= 0;
2948 this->u2_
.object
= NULL
;
2951 // For an ordinary input section.
2952 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2955 p2align_(ffsll(static_cast<long long>(addralign
))),
2956 section_order_index_(0)
2958 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2959 && shndx
!= MERGE_DATA_SECTION_CODE
2960 && shndx
!= MERGE_STRING_SECTION_CODE
2961 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
2962 this->u1_
.data_size
= data_size
;
2963 this->u2_
.object
= object
;
2966 // For a non-merge output section.
2967 Input_section(Output_section_data
* posd
)
2968 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0),
2969 section_order_index_(0)
2971 this->u1_
.data_size
= 0;
2972 this->u2_
.posd
= posd
;
2975 // For a merge section.
2976 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2978 ? MERGE_STRING_SECTION_CODE
2979 : MERGE_DATA_SECTION_CODE
),
2981 section_order_index_(0)
2983 this->u1_
.entsize
= entsize
;
2984 this->u2_
.posd
= posd
;
2987 // For a relaxed input section.
2988 Input_section(Output_relaxed_input_section
*psection
)
2989 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0),
2990 section_order_index_(0)
2992 this->u1_
.data_size
= 0;
2993 this->u2_
.poris
= psection
;
2997 section_order_index() const
2999 return this->section_order_index_
;
3003 set_section_order_index(unsigned int number
)
3005 this->section_order_index_
= number
;
3008 // The required alignment.
3012 if (this->p2align_
!= 0)
3013 return static_cast<uint64_t>(1) << (this->p2align_
- 1);
3014 else if (!this->is_input_section())
3015 return this->u2_
.posd
->addralign();
3020 // Set the required alignment, which must be either 0 or a power of 2.
3021 // For input sections that are sub-classes of Output_section_data, a
3022 // alignment of zero means asking the underlying object for alignment.
3024 set_addralign(uint64_t addralign
)
3030 gold_assert((addralign
& (addralign
- 1)) == 0);
3031 this->p2align_
= ffsll(static_cast<long long>(addralign
));
3035 // Return the required size.
3039 // Whether this is an input section.
3041 is_input_section() const
3043 return (this->shndx_
!= OUTPUT_SECTION_CODE
3044 && this->shndx_
!= MERGE_DATA_SECTION_CODE
3045 && this->shndx_
!= MERGE_STRING_SECTION_CODE
3046 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
3049 // Return whether this is a merge section which matches the
3052 is_merge_section(bool is_string
, uint64_t entsize
,
3053 uint64_t addralign
) const
3055 return (this->shndx_
== (is_string
3056 ? MERGE_STRING_SECTION_CODE
3057 : MERGE_DATA_SECTION_CODE
)
3058 && this->u1_
.entsize
== entsize
3059 && this->addralign() == addralign
);
3062 // Return whether this is a merge section for some input section.
3064 is_merge_section() const
3066 return (this->shndx_
== MERGE_DATA_SECTION_CODE
3067 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3070 // Return whether this is a relaxed input section.
3072 is_relaxed_input_section() const
3073 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
3075 // Return whether this is a generic Output_section_data.
3077 is_output_section_data() const
3079 return this->shndx_
== OUTPUT_SECTION_CODE
;
3082 // Return the object for an input section.
3086 // Return the input section index for an input section.
3090 // For non-input-sections, return the associated Output_section_data
3092 Output_section_data
*
3093 output_section_data() const
3095 gold_assert(!this->is_input_section());
3096 return this->u2_
.posd
;
3099 // For a merge section, return the Output_merge_base pointer.
3101 output_merge_base() const
3103 gold_assert(this->is_merge_section());
3104 return this->u2_
.pomb
;
3107 // Return the Output_relaxed_input_section object.
3108 Output_relaxed_input_section
*
3109 relaxed_input_section() const
3111 gold_assert(this->is_relaxed_input_section());
3112 return this->u2_
.poris
;
3115 // Set the output section.
3117 set_output_section(Output_section
* os
)
3119 gold_assert(!this->is_input_section());
3120 Output_section_data
*posd
=
3121 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
3122 posd
->set_output_section(os
);
3125 // Set the address and file offset. This is called during
3126 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3127 // the enclosing section.
3129 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
3130 off_t section_file_offset
);
3132 // Reset the address and file offset.
3134 reset_address_and_file_offset();
3136 // Finalize the data size.
3138 finalize_data_size();
3140 // Add an input section, for SHF_MERGE sections.
3142 add_input_section(Relobj
* object
, unsigned int shndx
)
3144 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
3145 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3146 return this->u2_
.posd
->add_input_section(object
, shndx
);
3149 // Given an input OBJECT, an input section index SHNDX within that
3150 // object, and an OFFSET relative to the start of that input
3151 // section, return whether or not the output offset is known. If
3152 // this function returns true, it sets *POUTPUT to the offset in
3153 // the output section, relative to the start of the input section
3154 // in the output section. *POUTPUT may be different from OFFSET
3155 // for a merged section.
3157 output_offset(const Relobj
* object
, unsigned int shndx
,
3158 section_offset_type offset
,
3159 section_offset_type
*poutput
) const;
3161 // Return whether this is the merge section for the input section
3164 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3166 // Write out the data. This does nothing for an input section.
3168 write(Output_file
*);
3170 // Write the data to a buffer. This does nothing for an input
3173 write_to_buffer(unsigned char*);
3175 // Print to a map file.
3177 print_to_mapfile(Mapfile
*) const;
3179 // Print statistics about merge sections to stderr.
3181 print_merge_stats(const char* section_name
)
3183 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3184 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3185 this->u2_
.posd
->print_merge_stats(section_name
);
3189 // Code values which appear in shndx_. If the value is not one of
3190 // these codes, it is the input section index in the object file.
3193 // An Output_section_data.
3194 OUTPUT_SECTION_CODE
= -1U,
3195 // An Output_section_data for an SHF_MERGE section with
3196 // SHF_STRINGS not set.
3197 MERGE_DATA_SECTION_CODE
= -2U,
3198 // An Output_section_data for an SHF_MERGE section with
3200 MERGE_STRING_SECTION_CODE
= -3U,
3201 // An Output_section_data for a relaxed input section.
3202 RELAXED_INPUT_SECTION_CODE
= -4U
3205 // For an ordinary input section, this is the section index in the
3206 // input file. For an Output_section_data, this is
3207 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3208 // MERGE_STRING_SECTION_CODE.
3209 unsigned int shndx_
;
3210 // The required alignment, stored as a power of 2.
3211 unsigned int p2align_
;
3214 // For an ordinary input section, the section size.
3216 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3217 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3223 // For an ordinary input section, the object which holds the
3226 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3227 // MERGE_STRING_SECTION_CODE, the data.
3228 Output_section_data
* posd
;
3229 Output_merge_base
* pomb
;
3230 // For RELAXED_INPUT_SECTION_CODE, the data.
3231 Output_relaxed_input_section
* poris
;
3233 // The line number of the pattern it matches in the --section-ordering-file
3234 // file. It is 0 if does not match any pattern.
3235 unsigned int section_order_index_
;
3238 // Store the list of input sections for this Output_section into the
3239 // list passed in. This removes the input sections, leaving only
3240 // any Output_section_data elements. This returns the size of those
3241 // Output_section_data elements. ADDRESS is the address of this
3242 // output section. FILL is the fill value to use, in case there are
3243 // any spaces between the remaining Output_section_data elements.
3245 get_input_sections(uint64_t address
, const std::string
& fill
,
3246 std::list
<Input_section
>*);
3248 // Add a script input section. A script input section can either be
3249 // a plain input section or a sub-class of Output_section_data.
3251 add_script_input_section(const Input_section
& input_section
);
3253 // Set the current size of the output section.
3255 set_current_data_size(off_t size
)
3256 { this->set_current_data_size_for_child(size
); }
3258 // Get the current size of the output section.
3260 current_data_size() const
3261 { return this->current_data_size_for_child(); }
3263 // End of linker script support.
3265 // Save states before doing section layout.
3266 // This is used for relaxation.
3270 // Restore states prior to section layout.
3278 // Convert existing input sections to relaxed input sections.
3280 convert_input_sections_to_relaxed_sections(
3281 const std::vector
<Output_relaxed_input_section
*>& sections
);
3283 // Find a relaxed input section to an input section in OBJECT
3284 // with index SHNDX. Return NULL if none is found.
3285 const Output_relaxed_input_section
*
3286 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
3288 // Whether section offsets need adjustment due to relaxation.
3290 section_offsets_need_adjustment() const
3291 { return this->section_offsets_need_adjustment_
; }
3293 // Set section_offsets_need_adjustment to be true.
3295 set_section_offsets_need_adjustment()
3296 { this->section_offsets_need_adjustment_
= true; }
3298 // Adjust section offsets of input sections in this. This is
3299 // requires if relaxation caused some input sections to change sizes.
3301 adjust_section_offsets();
3303 // Whether this is a NOLOAD section.
3306 { return this->is_noload_
; }
3311 { this->is_noload_
= true; }
3313 // Print merge statistics to stderr.
3315 print_merge_stats();
3318 // Return the output section--i.e., the object itself.
3323 const Output_section
*
3324 do_output_section() const
3327 // Return the section index in the output file.
3329 do_out_shndx() const
3331 gold_assert(this->out_shndx_
!= -1U);
3332 return this->out_shndx_
;
3335 // Set the output section index.
3337 do_set_out_shndx(unsigned int shndx
)
3339 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
3340 this->out_shndx_
= shndx
;
3343 // Set the final data size of the Output_section. For a typical
3344 // Output_section, there is nothing to do, but if there are any
3345 // Output_section_data objects we need to set their final addresses
3348 set_final_data_size();
3350 // Reset the address and file offset.
3352 do_reset_address_and_file_offset();
3354 // Return true if address and file offset already have reset values. In
3355 // other words, calling reset_address_and_file_offset will not change them.
3357 do_address_and_file_offset_have_reset_values() const;
3359 // Write the data to the file. For a typical Output_section, this
3360 // does nothing: the data is written out by calling Object::Relocate
3361 // on each input object. But if there are any Output_section_data
3362 // objects we do need to write them out here.
3364 do_write(Output_file
*);
3366 // Return the address alignment--function required by parent class.
3368 do_addralign() const
3369 { return this->addralign_
; }
3371 // Return whether there is a load address.
3373 do_has_load_address() const
3374 { return this->has_load_address_
; }
3376 // Return the load address.
3378 do_load_address() const
3380 gold_assert(this->has_load_address_
);
3381 return this->load_address_
;
3384 // Return whether this is an Output_section.
3386 do_is_section() const
3389 // Return whether this is a section of the specified type.
3391 do_is_section_type(elfcpp::Elf_Word type
) const
3392 { return this->type_
== type
; }
3394 // Return whether the specified section flag is set.
3396 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
3397 { return (this->flags_
& flag
) != 0; }
3399 // Set the TLS offset. Called only for SHT_TLS sections.
3401 do_set_tls_offset(uint64_t tls_base
);
3403 // Return the TLS offset, relative to the base of the TLS segment.
3404 // Valid only for SHT_TLS sections.
3406 do_tls_offset() const
3407 { return this->tls_offset_
; }
3409 // This may be implemented by a child class.
3411 do_finalize_name(Layout
*)
3414 // Print to the map file.
3416 do_print_to_mapfile(Mapfile
*) const;
3418 // Record that this section requires postprocessing after all
3419 // relocations have been applied. This is called by a child class.
3421 set_requires_postprocessing()
3423 this->requires_postprocessing_
= true;
3424 this->after_input_sections_
= true;
3427 // Write all the data of an Output_section into the postprocessing
3430 write_to_postprocessing_buffer();
3432 typedef std::vector
<Input_section
> Input_section_list
;
3434 // Allow a child class to access the input sections.
3435 const Input_section_list
&
3436 input_sections() const
3437 { return this->input_sections_
; }
3439 // Whether this always keeps an input section list
3441 always_keeps_input_sections() const
3442 { return this->always_keeps_input_sections_
; }
3444 // Always keep an input section list.
3446 set_always_keeps_input_sections()
3448 gold_assert(this->current_data_size_for_child() == 0);
3449 this->always_keeps_input_sections_
= true;
3453 // We only save enough information to undo the effects of section layout.
3454 class Checkpoint_output_section
3457 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3458 const Input_section_list
& input_sections
,
3459 off_t first_input_offset
,
3460 bool attached_input_sections_are_sorted
)
3461 : addralign_(addralign
), flags_(flags
),
3462 input_sections_(input_sections
),
3463 input_sections_size_(input_sections_
.size()),
3464 input_sections_copy_(), first_input_offset_(first_input_offset
),
3465 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3469 ~Checkpoint_output_section()
3472 // Return the address alignment.
3475 { return this->addralign_
; }
3477 // Return the section flags.
3480 { return this->flags_
; }
3482 // Return a reference to the input section list copy.
3485 { return &this->input_sections_copy_
; }
3487 // Return the size of input_sections at the time when checkpoint is
3490 input_sections_size() const
3491 { return this->input_sections_size_
; }
3493 // Whether input sections are copied.
3495 input_sections_saved() const
3496 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3499 first_input_offset() const
3500 { return this->first_input_offset_
; }
3503 attached_input_sections_are_sorted() const
3504 { return this->attached_input_sections_are_sorted_
; }
3506 // Save input sections.
3508 save_input_sections()
3510 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3511 this->input_sections_copy_
.clear();
3512 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3513 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3514 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3515 this->input_sections_copy_
.push_back(*p
);
3519 // The section alignment.
3520 uint64_t addralign_
;
3521 // The section flags.
3522 elfcpp::Elf_Xword flags_
;
3523 // Reference to the input sections to be checkpointed.
3524 const Input_section_list
& input_sections_
;
3525 // Size of the checkpointed portion of input_sections_;
3526 size_t input_sections_size_
;
3527 // Copy of input sections.
3528 Input_section_list input_sections_copy_
;
3529 // The offset of the first entry in input_sections_.
3530 off_t first_input_offset_
;
3531 // True if the input sections attached to this output section have
3532 // already been sorted.
3533 bool attached_input_sections_are_sorted_
;
3536 // This class is used to sort the input sections.
3537 class Input_section_sort_entry
;
3539 // This is the sort comparison function for ctors and dtors.
3540 struct Input_section_sort_compare
3543 operator()(const Input_section_sort_entry
&,
3544 const Input_section_sort_entry
&) const;
3547 // This is the sort comparison function for .init_array and .fini_array.
3548 struct Input_section_sort_init_fini_compare
3551 operator()(const Input_section_sort_entry
&,
3552 const Input_section_sort_entry
&) const;
3555 // This is the sort comparison function when a section order is specified
3556 // from an input file.
3557 struct Input_section_sort_section_order_index_compare
3560 operator()(const Input_section_sort_entry
&,
3561 const Input_section_sort_entry
&) const;
3564 // Fill data. This is used to fill in data between input sections.
3565 // It is also used for data statements (BYTE, WORD, etc.) in linker
3566 // scripts. When we have to keep track of the input sections, we
3567 // can use an Output_data_const, but we don't want to have to keep
3568 // track of input sections just to implement fills.
3572 Fill(off_t section_offset
, off_t length
)
3573 : section_offset_(section_offset
),
3574 length_(convert_to_section_size_type(length
))
3577 // Return section offset.
3579 section_offset() const
3580 { return this->section_offset_
; }
3582 // Return fill length.
3585 { return this->length_
; }
3588 // The offset within the output section.
3589 off_t section_offset_
;
3590 // The length of the space to fill.
3591 section_size_type length_
;
3594 typedef std::vector
<Fill
> Fill_list
;
3596 // Map used during relaxation of existing sections. This map
3597 // a section id an input section list index. We assume that
3598 // Input_section_list is a vector.
3599 typedef Unordered_map
<Section_id
, size_t, Section_id_hash
> Relaxation_map
;
3601 // Add a new output section by Input_section.
3603 add_output_section_data(Input_section
*);
3605 // Add an SHF_MERGE input section. Returns true if the section was
3606 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3607 // stores information about the merged input sections.
3609 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3610 uint64_t entsize
, uint64_t addralign
,
3611 bool keeps_input_sections
);
3613 // Add an output SHF_MERGE section POSD to this output section.
3614 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3615 // ENTSIZE is the entity size. This returns the entry added to
3618 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3621 // Sort the attached input sections.
3623 sort_attached_input_sections();
3625 // Find the merge section into which an input section with index SHNDX in
3626 // OBJECT has been added. Return NULL if none found.
3627 Output_section_data
*
3628 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3630 // Build a relaxation map.
3632 build_relaxation_map(
3633 const Input_section_list
& input_sections
,
3635 Relaxation_map
* map
) const;
3637 // Convert input sections in an input section list into relaxed sections.
3639 convert_input_sections_in_list_to_relaxed_sections(
3640 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3641 const Relaxation_map
& map
,
3642 Input_section_list
* input_sections
);
3644 // Build the lookup maps for merge and relaxed input sections.
3646 build_lookup_maps() const;
3648 // Most of these fields are only valid after layout.
3650 // The name of the section. This will point into a Stringpool.
3652 // The section address is in the parent class.
3653 // The section alignment.
3654 uint64_t addralign_
;
3655 // The section entry size.
3657 // The load address. This is only used when using a linker script
3658 // with a SECTIONS clause. The has_load_address_ field indicates
3659 // whether this field is valid.
3660 uint64_t load_address_
;
3661 // The file offset is in the parent class.
3662 // Set the section link field to the index of this section.
3663 const Output_data
* link_section_
;
3664 // If link_section_ is NULL, this is the link field.
3666 // Set the section info field to the index of this section.
3667 const Output_section
* info_section_
;
3668 // If info_section_ is NULL, set the info field to the symbol table
3669 // index of this symbol.
3670 const Symbol
* info_symndx_
;
3671 // If info_section_ and info_symndx_ are NULL, this is the section
3674 // The section type.
3675 const elfcpp::Elf_Word type_
;
3676 // The section flags.
3677 elfcpp::Elf_Xword flags_
;
3678 // The order of this section in the output segment.
3679 Output_section_order order_
;
3680 // The section index.
3681 unsigned int out_shndx_
;
3682 // If there is a STT_SECTION for this output section in the normal
3683 // symbol table, this is the symbol index. This starts out as zero.
3684 // It is initialized in Layout::finalize() to be the index, or -1U
3685 // if there isn't one.
3686 unsigned int symtab_index_
;
3687 // If there is a STT_SECTION for this output section in the dynamic
3688 // symbol table, this is the symbol index. This starts out as zero.
3689 // It is initialized in Layout::finalize() to be the index, or -1U
3690 // if there isn't one.
3691 unsigned int dynsym_index_
;
3692 // The input sections. This will be empty in cases where we don't
3693 // need to keep track of them.
3694 Input_section_list input_sections_
;
3695 // The offset of the first entry in input_sections_.
3696 off_t first_input_offset_
;
3697 // The fill data. This is separate from input_sections_ because we
3698 // often will need fill sections without needing to keep track of
3701 // If the section requires postprocessing, this buffer holds the
3702 // section contents during relocation.
3703 unsigned char* postprocessing_buffer_
;
3704 // Whether this output section needs a STT_SECTION symbol in the
3705 // normal symbol table. This will be true if there is a relocation
3707 bool needs_symtab_index_
: 1;
3708 // Whether this output section needs a STT_SECTION symbol in the
3709 // dynamic symbol table. This will be true if there is a dynamic
3710 // relocation which needs it.
3711 bool needs_dynsym_index_
: 1;
3712 // Whether the link field of this output section should point to the
3713 // normal symbol table.
3714 bool should_link_to_symtab_
: 1;
3715 // Whether the link field of this output section should point to the
3716 // dynamic symbol table.
3717 bool should_link_to_dynsym_
: 1;
3718 // Whether this section should be written after all the input
3719 // sections are complete.
3720 bool after_input_sections_
: 1;
3721 // Whether this section requires post processing after all
3722 // relocations have been applied.
3723 bool requires_postprocessing_
: 1;
3724 // Whether an input section was mapped to this output section
3725 // because of a SECTIONS clause in a linker script.
3726 bool found_in_sections_clause_
: 1;
3727 // Whether this section has an explicitly specified load address.
3728 bool has_load_address_
: 1;
3729 // True if the info_section_ field means the section index of the
3730 // section, false if it means the symbol index of the corresponding
3732 bool info_uses_section_index_
: 1;
3733 // True if input sections attached to this output section have to be
3734 // sorted according to a specified order.
3735 bool input_section_order_specified_
: 1;
3736 // True if the input sections attached to this output section may
3738 bool may_sort_attached_input_sections_
: 1;
3739 // True if the input sections attached to this output section must
3741 bool must_sort_attached_input_sections_
: 1;
3742 // True if the input sections attached to this output section have
3743 // already been sorted.
3744 bool attached_input_sections_are_sorted_
: 1;
3745 // True if this section holds relro data.
3747 // True if this is a small section.
3748 bool is_small_section_
: 1;
3749 // True if this is a large section.
3750 bool is_large_section_
: 1;
3751 // Whether code-fills are generated at write.
3752 bool generate_code_fills_at_write_
: 1;
3753 // Whether the entry size field should be zero.
3754 bool is_entsize_zero_
: 1;
3755 // Whether section offsets need adjustment due to relaxation.
3756 bool section_offsets_need_adjustment_
: 1;
3757 // Whether this is a NOLOAD section.
3758 bool is_noload_
: 1;
3759 // Whether this always keeps input section.
3760 bool always_keeps_input_sections_
: 1;
3761 // For SHT_TLS sections, the offset of this section relative to the base
3762 // of the TLS segment.
3763 uint64_t tls_offset_
;
3764 // Saved checkpoint.
3765 Checkpoint_output_section
* checkpoint_
;
3766 // Fast lookup maps for merged and relaxed input sections.
3767 Output_section_lookup_maps
* lookup_maps_
;
3770 // An output segment. PT_LOAD segments are built from collections of
3771 // output sections. Other segments typically point within PT_LOAD
3772 // segments, and are built directly as needed.
3774 // NOTE: We want to use the copy constructor for this class. During
3775 // relaxation, we may try built the segments multiple times. We do
3776 // that by copying the original segment list before lay-out, doing
3777 // a trial lay-out and roll-back to the saved copied if we need to
3778 // to the lay-out again.
3780 class Output_segment
3783 // Create an output segment, specifying the type and flags.
3784 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3786 // Return the virtual address.
3789 { return this->vaddr_
; }
3791 // Return the physical address.
3794 { return this->paddr_
; }
3796 // Return the segment type.
3799 { return this->type_
; }
3801 // Return the segment flags.
3804 { return this->flags_
; }
3806 // Return the memory size.
3809 { return this->memsz_
; }
3811 // Return the file size.
3814 { return this->filesz_
; }
3816 // Return the file offset.
3819 { return this->offset_
; }
3821 // Whether this is a segment created to hold large data sections.
3823 is_large_data_segment() const
3824 { return this->is_large_data_segment_
; }
3826 // Record that this is a segment created to hold large data
3829 set_is_large_data_segment()
3830 { this->is_large_data_segment_
= true; }
3832 // Return the maximum alignment of the Output_data.
3834 maximum_alignment();
3836 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
3837 // the segment flags to use.
3839 add_output_section_to_load(Layout
* layout
, Output_section
* os
,
3840 elfcpp::Elf_Word seg_flags
);
3842 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
3843 // is the segment flags to use.
3845 add_output_section_to_nonload(Output_section
* os
,
3846 elfcpp::Elf_Word seg_flags
);
3848 // Remove an Output_section from this segment. It is an error if it
3851 remove_output_section(Output_section
* os
);
3853 // Add an Output_data (which need not be an Output_section) to the
3854 // start of this segment.
3856 add_initial_output_data(Output_data
*);
3858 // Return true if this segment has any sections which hold actual
3859 // data, rather than being a BSS section.
3861 has_any_data_sections() const;
3863 // Whether this segment has a dynamic relocs.
3865 has_dynamic_reloc() const;
3867 // Return the address of the first section.
3869 first_section_load_address() const;
3871 // Return whether the addresses have been set already.
3873 are_addresses_set() const
3874 { return this->are_addresses_set_
; }
3876 // Set the addresses.
3878 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3880 this->vaddr_
= vaddr
;
3881 this->paddr_
= paddr
;
3882 this->are_addresses_set_
= true;
3885 // Update the flags for the flags of an output section added to this
3888 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
3890 // The ELF ABI specifies that a PT_TLS segment should always have
3891 // PF_R as the flags.
3892 if (this->type() != elfcpp::PT_TLS
)
3893 this->flags_
|= flags
;
3896 // Set the segment flags. This is only used if we have a PHDRS
3897 // clause which explicitly specifies the flags.
3899 set_flags(elfcpp::Elf_Word flags
)
3900 { this->flags_
= flags
; }
3902 // Set the address of the segment to ADDR and the offset to *POFF
3903 // and set the addresses and offsets of all contained output
3904 // sections accordingly. Set the section indexes of all contained
3905 // output sections starting with *PSHNDX. If RESET is true, first
3906 // reset the addresses of the contained sections. Return the
3907 // address of the immediately following segment. Update *POFF and
3908 // *PSHNDX. This should only be called for a PT_LOAD segment.
3910 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
,
3911 unsigned int increase_relro
, off_t
* poff
,
3912 unsigned int* pshndx
);
3914 // Set the minimum alignment of this segment. This may be adjusted
3915 // upward based on the section alignments.
3917 set_minimum_p_align(uint64_t align
)
3919 if (align
> this->min_p_align_
)
3920 this->min_p_align_
= align
;
3923 // Set the offset of this segment based on the section. This should
3924 // only be called for a non-PT_LOAD segment.
3926 set_offset(unsigned int increase
);
3928 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3932 // Return the number of output sections.
3934 output_section_count() const;
3936 // Return the section attached to the list segment with the lowest
3937 // load address. This is used when handling a PHDRS clause in a
3940 section_with_lowest_load_address() const;
3942 // Write the segment header into *OPHDR.
3943 template<int size
, bool big_endian
>
3945 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
3947 // Write the section headers of associated sections into V.
3948 template<int size
, bool big_endian
>
3950 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
3951 unsigned int* pshndx
) const;
3953 // Print the output sections in the map file.
3955 print_sections_to_mapfile(Mapfile
*) const;
3958 typedef std::vector
<Output_data
*> Output_data_list
;
3960 // Find the maximum alignment in an Output_data_list.
3962 maximum_alignment_list(const Output_data_list
*);
3964 // Return whether the first data section is a relro section.
3966 is_first_section_relro() const;
3968 // Set the section addresses in an Output_data_list.
3970 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
3971 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
3974 // Return the number of Output_sections in an Output_data_list.
3976 output_section_count_list(const Output_data_list
*) const;
3978 // Return whether an Output_data_list has a dynamic reloc.
3980 has_dynamic_reloc_list(const Output_data_list
*) const;
3982 // Find the section with the lowest load address in an
3983 // Output_data_list.
3985 lowest_load_address_in_list(const Output_data_list
* pdl
,
3986 Output_section
** found
,
3987 uint64_t* found_lma
) const;
3989 // Find the first and last entries by address.
3991 find_first_and_last_list(const Output_data_list
* pdl
,
3992 const Output_data
** pfirst
,
3993 const Output_data
** plast
) const;
3995 // Write the section headers in the list into V.
3996 template<int size
, bool big_endian
>
3998 write_section_headers_list(const Layout
*, const Stringpool
*,
3999 const Output_data_list
*, unsigned char* v
,
4000 unsigned int* pshdx
) const;
4002 // Print a section list to the mapfile.
4004 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
4006 // NOTE: We want to use the copy constructor. Currently, shallow copy
4007 // works for us so we do not need to write our own copy constructor.
4009 // The list of output data attached to this segment.
4010 Output_data_list output_lists_
[ORDER_MAX
];
4011 // The segment virtual address.
4013 // The segment physical address.
4015 // The size of the segment in memory.
4017 // The maximum section alignment. The is_max_align_known_ field
4018 // indicates whether this has been finalized.
4019 uint64_t max_align_
;
4020 // The required minimum value for the p_align field. This is used
4021 // for PT_LOAD segments. Note that this does not mean that
4022 // addresses should be aligned to this value; it means the p_paddr
4023 // and p_vaddr fields must be congruent modulo this value. For
4024 // non-PT_LOAD segments, the dynamic linker works more efficiently
4025 // if the p_align field has the more conventional value, although it
4026 // can align as needed.
4027 uint64_t min_p_align_
;
4028 // The offset of the segment data within the file.
4030 // The size of the segment data in the file.
4032 // The segment type;
4033 elfcpp::Elf_Word type_
;
4034 // The segment flags.
4035 elfcpp::Elf_Word flags_
;
4036 // Whether we have finalized max_align_.
4037 bool is_max_align_known_
: 1;
4038 // Whether vaddr and paddr were set by a linker script.
4039 bool are_addresses_set_
: 1;
4040 // Whether this segment holds large data sections.
4041 bool is_large_data_segment_
: 1;
4044 // This class represents the output file.
4049 Output_file(const char* name
);
4051 // Indicate that this is a temporary file which should not be
4055 { this->is_temporary_
= true; }
4057 // Try to open an existing file. Returns false if the file doesn't
4058 // exist, has a size of 0 or can't be mmaped. This method is
4061 open_for_modification();
4063 // Open the output file. FILE_SIZE is the final size of the file.
4064 // If the file already exists, it is deleted/truncated. This method
4065 // is thread-unsafe.
4067 open(off_t file_size
);
4069 // Resize the output file. This method is thread-unsafe.
4071 resize(off_t file_size
);
4073 // Close the output file (flushing all buffered data) and make sure
4074 // there are no errors. This method is thread-unsafe.
4078 // Return the size of this file.
4081 { return this->file_size_
; }
4083 // Return the name of this file.
4086 { return this->name_
; }
4088 // We currently always use mmap which makes the view handling quite
4089 // simple. In the future we may support other approaches.
4091 // Write data to the output file.
4093 write(off_t offset
, const void* data
, size_t len
)
4094 { memcpy(this->base_
+ offset
, data
, len
); }
4096 // Get a buffer to use to write to the file, given the offset into
4097 // the file and the size.
4099 get_output_view(off_t start
, size_t size
)
4101 gold_assert(start
>= 0
4102 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
4103 return this->base_
+ start
;
4106 // VIEW must have been returned by get_output_view. Write the
4107 // buffer to the file, passing in the offset and the size.
4109 write_output_view(off_t
, size_t, unsigned char*)
4112 // Get a read/write buffer. This is used when we want to write part
4113 // of the file, read it in, and write it again.
4115 get_input_output_view(off_t start
, size_t size
)
4116 { return this->get_output_view(start
, size
); }
4118 // Write a read/write buffer back to the file.
4120 write_input_output_view(off_t
, size_t, unsigned char*)
4123 // Get a read buffer. This is used when we just want to read part
4124 // of the file back it in.
4125 const unsigned char*
4126 get_input_view(off_t start
, size_t size
)
4127 { return this->get_output_view(start
, size
); }
4129 // Release a read bfufer.
4131 free_input_view(off_t
, size_t, const unsigned char*)
4135 // Map the file into memory or, if that fails, allocate anonymous
4140 // Allocate anonymous memory for the file.
4144 // Map the file into memory.
4148 // Unmap the file from memory (and flush to disk buffers).
4158 // Base of file mapped into memory.
4159 unsigned char* base_
;
4160 // True iff base_ points to a memory buffer rather than an output file.
4161 bool map_is_anonymous_
;
4162 // True if this is a temporary file which should not be output.
4166 } // End namespace gold.
4168 #endif // !defined(GOLD_OUTPUT_H)