1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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
;
42 class Relocatable_relocs
;
44 template<int size
, bool big_endian
>
46 template<int size
, bool big_endian
>
49 // An abtract class for data which has to go into the output file.
54 explicit Output_data()
55 : address_(0), data_size_(0), offset_(-1),
56 is_address_valid_(false), is_data_size_valid_(false),
57 is_offset_valid_(false),
58 dynamic_reloc_count_(0)
64 // Return the address. For allocated sections, this is only valid
65 // after Layout::finalize is finished.
69 gold_assert(this->is_address_valid_
);
70 return this->address_
;
73 // Return the size of the data. For allocated sections, this must
74 // be valid after Layout::finalize calls set_address, but need not
75 // be valid before then.
79 gold_assert(this->is_data_size_valid_
);
80 return this->data_size_
;
83 // Return the file offset. This is only valid after
84 // Layout::finalize is finished. For some non-allocated sections,
85 // it may not be valid until near the end of the link.
89 gold_assert(this->is_offset_valid_
);
93 // Reset the address and file offset. This essentially disables the
94 // sanity testing about duplicate and unknown settings.
96 reset_address_and_file_offset()
98 this->is_address_valid_
= false;
99 this->is_offset_valid_
= false;
100 this->is_data_size_valid_
= false;
101 this->do_reset_address_and_file_offset();
104 // Return the required alignment.
107 { return this->do_addralign(); }
109 // Return whether this has a load address.
111 has_load_address() const
112 { return this->do_has_load_address(); }
114 // Return the load address.
117 { return this->do_load_address(); }
119 // Return whether this is an Output_section.
122 { return this->do_is_section(); }
124 // Return whether this is an Output_section of the specified type.
126 is_section_type(elfcpp::Elf_Word stt
) const
127 { return this->do_is_section_type(stt
); }
129 // Return whether this is an Output_section with the specified flag
132 is_section_flag_set(elfcpp::Elf_Xword shf
) const
133 { return this->do_is_section_flag_set(shf
); }
135 // Return the output section that this goes in, if there is one.
138 { return this->do_output_section(); }
140 // Return the output section index, if there is an output section.
143 { return this->do_out_shndx(); }
145 // Set the output section index, if this is an output section.
147 set_out_shndx(unsigned int shndx
)
148 { this->do_set_out_shndx(shndx
); }
150 // Set the address and file offset of this data, and finalize the
151 // size of the data. This is called during Layout::finalize for
152 // allocated sections.
154 set_address_and_file_offset(uint64_t addr
, off_t off
)
156 this->set_address(addr
);
157 this->set_file_offset(off
);
158 this->finalize_data_size();
163 set_address(uint64_t addr
)
165 gold_assert(!this->is_address_valid_
);
166 this->address_
= addr
;
167 this->is_address_valid_
= true;
170 // Set the file offset.
172 set_file_offset(off_t off
)
174 gold_assert(!this->is_offset_valid_
);
176 this->is_offset_valid_
= true;
179 // Finalize the data size.
183 if (!this->is_data_size_valid_
)
185 // Tell the child class to set the data size.
186 this->set_final_data_size();
187 gold_assert(this->is_data_size_valid_
);
191 // Set the TLS offset. Called only for SHT_TLS sections.
193 set_tls_offset(uint64_t tls_base
)
194 { this->do_set_tls_offset(tls_base
); }
196 // Return the TLS offset, relative to the base of the TLS segment.
197 // Valid only for SHT_TLS sections.
200 { return this->do_tls_offset(); }
202 // Write the data to the output file. This is called after
203 // Layout::finalize is complete.
205 write(Output_file
* file
)
206 { this->do_write(file
); }
208 // This is called by Layout::finalize to note that the sizes of
209 // allocated sections must now be fixed.
212 { Output_data::allocated_sizes_are_fixed
= true; }
214 // Used to check that layout has been done.
217 { return Output_data::allocated_sizes_are_fixed
; }
219 // Count the number of dynamic relocations applied to this section.
222 { ++this->dynamic_reloc_count_
; }
224 // Return the number of dynamic relocations applied to this section.
226 dynamic_reloc_count() const
227 { return this->dynamic_reloc_count_
; }
229 // Whether the address is valid.
231 is_address_valid() const
232 { return this->is_address_valid_
; }
234 // Whether the file offset is valid.
236 is_offset_valid() const
237 { return this->is_offset_valid_
; }
239 // Whether the data size is valid.
241 is_data_size_valid() const
242 { return this->is_data_size_valid_
; }
244 // Print information to the map file.
246 print_to_mapfile(Mapfile
* mapfile
) const
247 { return this->do_print_to_mapfile(mapfile
); }
250 // Functions that child classes may or in some cases must implement.
252 // Write the data to the output file.
254 do_write(Output_file
*) = 0;
256 // Return the required alignment.
258 do_addralign() const = 0;
260 // Return whether this has a load address.
262 do_has_load_address() const
265 // Return the load address.
267 do_load_address() const
268 { gold_unreachable(); }
270 // Return whether this is an Output_section.
272 do_is_section() const
275 // Return whether this is an Output_section of the specified type.
276 // This only needs to be implement by Output_section.
278 do_is_section_type(elfcpp::Elf_Word
) const
281 // Return whether this is an Output_section with the specific flag
282 // set. This only needs to be implemented by Output_section.
284 do_is_section_flag_set(elfcpp::Elf_Xword
) const
287 // Return the output section, if there is one.
288 virtual Output_section
*
292 // Return the output section index, if there is an output section.
295 { gold_unreachable(); }
297 // Set the output section index, if this is an output section.
299 do_set_out_shndx(unsigned int)
300 { gold_unreachable(); }
302 // This is a hook for derived classes to set the data size. This is
303 // called by finalize_data_size, normally called during
304 // Layout::finalize, when the section address is set.
306 set_final_data_size()
307 { gold_unreachable(); }
309 // A hook for resetting the address and file offset.
311 do_reset_address_and_file_offset()
314 // Set the TLS offset. Called only for SHT_TLS sections.
316 do_set_tls_offset(uint64_t)
317 { gold_unreachable(); }
319 // Return the TLS offset, relative to the base of the TLS segment.
320 // Valid only for SHT_TLS sections.
322 do_tls_offset() const
323 { gold_unreachable(); }
325 // Print to the map file. This only needs to be implemented by
326 // classes which may appear in a PT_LOAD segment.
328 do_print_to_mapfile(Mapfile
*) const
329 { gold_unreachable(); }
331 // Functions that child classes may call.
333 // Set the size of the data.
335 set_data_size(off_t data_size
)
337 gold_assert(!this->is_data_size_valid_
);
338 this->data_size_
= data_size
;
339 this->is_data_size_valid_
= true;
342 // Get the current data size--this is for the convenience of
343 // sections which build up their size over time.
345 current_data_size_for_child() const
346 { return this->data_size_
; }
348 // Set the current data size--this is for the convenience of
349 // sections which build up their size over time.
351 set_current_data_size_for_child(off_t data_size
)
353 gold_assert(!this->is_data_size_valid_
);
354 this->data_size_
= data_size
;
357 // Return default alignment for the target size.
361 // Return default alignment for a specified size--32 or 64.
363 default_alignment_for_size(int size
);
366 Output_data(const Output_data
&);
367 Output_data
& operator=(const Output_data
&);
369 // This is used for verification, to make sure that we don't try to
370 // change any sizes of allocated sections after we set the section
372 static bool allocated_sizes_are_fixed
;
374 // Memory address in output file.
376 // Size of data in output file.
378 // File offset of contents in output file.
380 // Whether address_ is valid.
381 bool is_address_valid_
;
382 // Whether data_size_ is valid.
383 bool is_data_size_valid_
;
384 // Whether offset_ is valid.
385 bool is_offset_valid_
;
386 // Count of dynamic relocations applied to this section.
387 unsigned int dynamic_reloc_count_
;
390 // Output the section headers.
392 class Output_section_headers
: public Output_data
395 Output_section_headers(const Layout
*,
396 const Layout::Segment_list
*,
397 const Layout::Section_list
*,
398 const Layout::Section_list
*,
400 const Output_section
*);
403 // Write the data to the file.
405 do_write(Output_file
*);
407 // Return the required alignment.
410 { return Output_data::default_alignment(); }
412 // Write to a map file.
414 do_print_to_mapfile(Mapfile
* mapfile
) const
415 { mapfile
->print_output_data(this, _("** section headers")); }
418 // Write the data to the file with the right size and endianness.
419 template<int size
, bool big_endian
>
421 do_sized_write(Output_file
*);
423 const Layout
* layout_
;
424 const Layout::Segment_list
* segment_list_
;
425 const Layout::Section_list
* section_list_
;
426 const Layout::Section_list
* unattached_section_list_
;
427 const Stringpool
* secnamepool_
;
428 const Output_section
* shstrtab_section_
;
431 // Output the segment headers.
433 class Output_segment_headers
: public Output_data
436 Output_segment_headers(const Layout::Segment_list
& segment_list
);
439 // Write the data to the file.
441 do_write(Output_file
*);
443 // Return the required alignment.
446 { return Output_data::default_alignment(); }
448 // Write to a map file.
450 do_print_to_mapfile(Mapfile
* mapfile
) const
451 { mapfile
->print_output_data(this, _("** segment headers")); }
454 // Write the data to the file with the right size and endianness.
455 template<int size
, bool big_endian
>
457 do_sized_write(Output_file
*);
459 const Layout::Segment_list
& segment_list_
;
462 // Output the ELF file header.
464 class Output_file_header
: public Output_data
467 Output_file_header(const Target
*,
469 const Output_segment_headers
*,
472 // Add information about the section headers. We lay out the ELF
473 // file header before we create the section headers.
474 void set_section_info(const Output_section_headers
*,
475 const Output_section
* shstrtab
);
478 // Write the data to the file.
480 do_write(Output_file
*);
482 // Return the required alignment.
485 { return Output_data::default_alignment(); }
487 // Write to a map file.
489 do_print_to_mapfile(Mapfile
* mapfile
) const
490 { mapfile
->print_output_data(this, _("** file header")); }
493 // Write the data to the file with the right size and endianness.
494 template<int size
, bool big_endian
>
496 do_sized_write(Output_file
*);
498 // Return the value to use for the entry address.
500 typename
elfcpp::Elf_types
<size
>::Elf_Addr
503 const Target
* target_
;
504 const Symbol_table
* symtab_
;
505 const Output_segment_headers
* segment_header_
;
506 const Output_section_headers
* section_header_
;
507 const Output_section
* shstrtab_
;
511 // Output sections are mainly comprised of input sections. However,
512 // there are cases where we have data to write out which is not in an
513 // input section. Output_section_data is used in such cases. This is
514 // an abstract base class.
516 class Output_section_data
: public Output_data
519 Output_section_data(off_t data_size
, uint64_t addralign
)
520 : Output_data(), output_section_(NULL
), addralign_(addralign
)
521 { this->set_data_size(data_size
); }
523 Output_section_data(uint64_t addralign
)
524 : Output_data(), output_section_(NULL
), addralign_(addralign
)
527 // Return the output section.
528 const Output_section
*
529 output_section() const
530 { return this->output_section_
; }
532 // Record the output section.
534 set_output_section(Output_section
* os
);
536 // Add an input section, for SHF_MERGE sections. This returns true
537 // if the section was handled.
539 add_input_section(Relobj
* object
, unsigned int shndx
)
540 { return this->do_add_input_section(object
, shndx
); }
542 // Given an input OBJECT, an input section index SHNDX within that
543 // object, and an OFFSET relative to the start of that input
544 // section, return whether or not the corresponding offset within
545 // the output section is known. If this function returns true, it
546 // sets *POUTPUT to the output offset. The value -1 indicates that
547 // this input offset is being discarded.
549 output_offset(const Relobj
* object
, unsigned int shndx
,
550 section_offset_type offset
,
551 section_offset_type
*poutput
) const
552 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
554 // Return whether this is the merge section for the input section
555 // SHNDX in OBJECT. This should return true when output_offset
556 // would return true for some values of OFFSET.
558 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
559 { return this->do_is_merge_section_for(object
, shndx
); }
561 // Write the contents to a buffer. This is used for sections which
562 // require postprocessing, such as compression.
564 write_to_buffer(unsigned char* buffer
)
565 { this->do_write_to_buffer(buffer
); }
567 // Print merge stats to stderr. This should only be called for
568 // SHF_MERGE sections.
570 print_merge_stats(const char* section_name
)
571 { this->do_print_merge_stats(section_name
); }
574 // The child class must implement do_write.
576 // The child class may implement specific adjustments to the output
579 do_adjust_output_section(Output_section
*)
582 // May be implemented by child class. Return true if the section
585 do_add_input_section(Relobj
*, unsigned int)
586 { gold_unreachable(); }
588 // The child class may implement output_offset.
590 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
591 section_offset_type
*) const
594 // The child class may implement is_merge_section_for.
596 do_is_merge_section_for(const Relobj
*, unsigned int) const
599 // The child class may implement write_to_buffer. Most child
600 // classes can not appear in a compressed section, and they do not
603 do_write_to_buffer(unsigned char*)
604 { gold_unreachable(); }
606 // Print merge statistics.
608 do_print_merge_stats(const char*)
609 { gold_unreachable(); }
611 // Return the required alignment.
614 { return this->addralign_
; }
616 // Return the output section.
619 { return this->output_section_
; }
621 // Return the section index of the output section.
623 do_out_shndx() const;
625 // Set the alignment.
627 set_addralign(uint64_t addralign
);
630 // The output section for this section.
631 Output_section
* output_section_
;
632 // The required alignment.
636 // Some Output_section_data classes build up their data step by step,
637 // rather than all at once. This class provides an interface for
640 class Output_section_data_build
: public Output_section_data
643 Output_section_data_build(uint64_t addralign
)
644 : Output_section_data(addralign
)
647 // Get the current data size.
649 current_data_size() const
650 { return this->current_data_size_for_child(); }
652 // Set the current data size.
654 set_current_data_size(off_t data_size
)
655 { this->set_current_data_size_for_child(data_size
); }
658 // Set the final data size.
660 set_final_data_size()
661 { this->set_data_size(this->current_data_size_for_child()); }
664 // A simple case of Output_data in which we have constant data to
667 class Output_data_const
: public Output_section_data
670 Output_data_const(const std::string
& data
, uint64_t addralign
)
671 : Output_section_data(data
.size(), addralign
), data_(data
)
674 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
675 : Output_section_data(len
, addralign
), data_(p
, len
)
678 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
679 : Output_section_data(len
, addralign
),
680 data_(reinterpret_cast<const char*>(p
), len
)
684 // Write the data to the output file.
686 do_write(Output_file
*);
688 // Write the data to a buffer.
690 do_write_to_buffer(unsigned char* buffer
)
691 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
693 // Write to a map file.
695 do_print_to_mapfile(Mapfile
* mapfile
) const
696 { mapfile
->print_output_data(this, _("** fill")); }
702 // Another version of Output_data with constant data, in which the
703 // buffer is allocated by the caller.
705 class Output_data_const_buffer
: public Output_section_data
708 Output_data_const_buffer(const unsigned char* p
, off_t len
,
709 uint64_t addralign
, const char* map_name
)
710 : Output_section_data(len
, addralign
),
711 p_(p
), map_name_(map_name
)
715 // Write the data the output file.
717 do_write(Output_file
*);
719 // Write the data to a buffer.
721 do_write_to_buffer(unsigned char* buffer
)
722 { memcpy(buffer
, this->p_
, this->data_size()); }
724 // Write to a map file.
726 do_print_to_mapfile(Mapfile
* mapfile
) const
727 { mapfile
->print_output_data(this, _(this->map_name_
)); }
730 // The data to output.
731 const unsigned char* p_
;
732 // Name to use in a map file. Maps are a rarely used feature, but
733 // the space usage is minor as aren't very many of these objects.
734 const char* map_name_
;
737 // A place holder for a fixed amount of data written out via some
740 class Output_data_fixed_space
: public Output_section_data
743 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
744 const char* map_name
)
745 : Output_section_data(data_size
, addralign
),
750 // Write out the data--the actual data must be written out
753 do_write(Output_file
*)
756 // Write to a map file.
758 do_print_to_mapfile(Mapfile
* mapfile
) const
759 { mapfile
->print_output_data(this, _(this->map_name_
)); }
762 // Name to use in a map file. Maps are a rarely used feature, but
763 // the space usage is minor as aren't very many of these objects.
764 const char* map_name_
;
767 // A place holder for variable sized data written out via some other
770 class Output_data_space
: public Output_section_data_build
773 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
774 : Output_section_data_build(addralign
),
778 // Set the alignment.
780 set_space_alignment(uint64_t align
)
781 { this->set_addralign(align
); }
784 // Write out the data--the actual data must be written out
787 do_write(Output_file
*)
790 // Write to a map file.
792 do_print_to_mapfile(Mapfile
* mapfile
) const
793 { mapfile
->print_output_data(this, _(this->map_name_
)); }
796 // Name to use in a map file. Maps are a rarely used feature, but
797 // the space usage is minor as aren't very many of these objects.
798 const char* map_name_
;
801 // Fill fixed space with zeroes. This is just like
802 // Output_data_fixed_space, except that the map name is known.
804 class Output_data_zero_fill
: public Output_section_data
807 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
808 : Output_section_data(data_size
, addralign
)
812 // There is no data to write out.
814 do_write(Output_file
*)
817 // Write to a map file.
819 do_print_to_mapfile(Mapfile
* mapfile
) const
820 { mapfile
->print_output_data(this, "** zero fill"); }
823 // A string table which goes into an output section.
825 class Output_data_strtab
: public Output_section_data
828 Output_data_strtab(Stringpool
* strtab
)
829 : Output_section_data(1), strtab_(strtab
)
833 // This is called to set the address and file offset. Here we make
834 // sure that the Stringpool is finalized.
836 set_final_data_size();
838 // Write out the data.
840 do_write(Output_file
*);
842 // Write the data to a buffer.
844 do_write_to_buffer(unsigned char* buffer
)
845 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
847 // Write to a map file.
849 do_print_to_mapfile(Mapfile
* mapfile
) const
850 { mapfile
->print_output_data(this, _("** string table")); }
856 // This POD class is used to represent a single reloc in the output
857 // file. This could be a private class within Output_data_reloc, but
858 // the templatization is complex enough that I broke it out into a
859 // separate class. The class is templatized on either elfcpp::SHT_REL
860 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
861 // relocation or an ordinary relocation.
863 // A relocation can be against a global symbol, a local symbol, a
864 // local section symbol, an output section, or the undefined symbol at
865 // index 0. We represent the latter by using a NULL global symbol.
867 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
870 template<bool dynamic
, int size
, bool big_endian
>
871 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
874 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
875 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
877 static const Address invalid_address
= static_cast<Address
>(0) - 1;
879 // An uninitialized entry. We need this because we want to put
880 // instances of this class into an STL container.
882 : local_sym_index_(INVALID_CODE
)
885 // We have a bunch of different constructors. They come in pairs
886 // depending on how the address of the relocation is specified. It
887 // can either be an offset in an Output_data or an offset in an
890 // A reloc against a global symbol.
892 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
893 Address address
, bool is_relative
);
895 Output_reloc(Symbol
* gsym
, unsigned int type
,
896 Sized_relobj
<size
, big_endian
>* relobj
,
897 unsigned int shndx
, Address address
, bool is_relative
);
899 // A reloc against a local symbol or local section symbol.
901 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
902 unsigned int local_sym_index
, unsigned int type
,
903 Output_data
* od
, Address address
, bool is_relative
,
904 bool is_section_symbol
);
906 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
907 unsigned int local_sym_index
, unsigned int type
,
908 unsigned int shndx
, Address address
, bool is_relative
,
909 bool is_section_symbol
);
911 // A reloc against the STT_SECTION symbol of an output section.
913 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
916 Output_reloc(Output_section
* os
, unsigned int type
,
917 Sized_relobj
<size
, big_endian
>* relobj
,
918 unsigned int shndx
, Address address
);
920 // Return TRUE if this is a RELATIVE relocation.
923 { return this->is_relative_
; }
925 // Return whether this is against a local section symbol.
927 is_local_section_symbol() const
929 return (this->local_sym_index_
!= GSYM_CODE
930 && this->local_sym_index_
!= SECTION_CODE
931 && this->local_sym_index_
!= INVALID_CODE
932 && this->is_section_symbol_
);
935 // For a local section symbol, return the offset of the input
936 // section within the output section. ADDEND is the addend being
937 // applied to the input section.
939 local_section_offset(Addend addend
) const;
941 // Get the value of the symbol referred to by a Rel relocation when
942 // we are adding the given ADDEND.
944 symbol_value(Addend addend
) const;
946 // Write the reloc entry to an output view.
948 write(unsigned char* pov
) const;
950 // Write the offset and info fields to Write_rel.
951 template<typename Write_rel
>
952 void write_rel(Write_rel
*) const;
954 // This is used when sorting dynamic relocs. Return -1 to sort this
955 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
957 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
960 // Return whether this reloc should be sorted before the argument
961 // when sorting dynamic relocs.
963 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
965 { return this->compare(r2
) < 0; }
968 // Record that we need a dynamic symbol index.
970 set_needs_dynsym_index();
972 // Return the symbol index.
974 get_symbol_index() const;
976 // Return the output address.
980 // Codes for local_sym_index_.
987 // Invalid uninitialized entry.
993 // For a local symbol or local section symbol
994 // (this->local_sym_index_ >= 0), the object. We will never
995 // generate a relocation against a local symbol in a dynamic
996 // object; that doesn't make sense. And our callers will always
997 // be templatized, so we use Sized_relobj here.
998 Sized_relobj
<size
, big_endian
>* relobj
;
999 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1000 // symbol. If this is NULL, it indicates a relocation against the
1001 // undefined 0 symbol.
1003 // For a relocation against an output section
1004 // (this->local_sym_index_ == SECTION_CODE), the output section.
1009 // If this->shndx_ is not INVALID CODE, the object which holds the
1010 // input section being used to specify the reloc address.
1011 Sized_relobj
<size
, big_endian
>* relobj
;
1012 // If this->shndx_ is INVALID_CODE, the output data being used to
1013 // specify the reloc address. This may be NULL if the reloc
1014 // address is absolute.
1017 // The address offset within the input section or the Output_data.
1019 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1020 // relocation against an output section, or INVALID_CODE for an
1021 // uninitialized value. Otherwise, for a local symbol
1022 // (this->is_section_symbol_ is false), the local symbol index. For
1023 // a local section symbol (this->is_section_symbol_ is true), the
1024 // section index in the input file.
1025 unsigned int local_sym_index_
;
1026 // The reloc type--a processor specific code.
1027 unsigned int type_
: 30;
1028 // True if the relocation is a RELATIVE relocation.
1029 bool is_relative_
: 1;
1030 // True if the relocation is against a section symbol.
1031 bool is_section_symbol_
: 1;
1032 // If the reloc address is an input section in an object, the
1033 // section index. This is INVALID_CODE if the reloc address is
1034 // specified in some other way.
1035 unsigned int shndx_
;
1038 // The SHT_RELA version of Output_reloc<>. This is just derived from
1039 // the SHT_REL version of Output_reloc, but it adds an addend.
1041 template<bool dynamic
, int size
, bool big_endian
>
1042 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1045 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1046 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1048 // An uninitialized entry.
1053 // A reloc against a global symbol.
1055 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1056 Address address
, Addend addend
, bool is_relative
)
1057 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
1060 Output_reloc(Symbol
* gsym
, unsigned int type
,
1061 Sized_relobj
<size
, big_endian
>* relobj
,
1062 unsigned int shndx
, Address address
, Addend addend
,
1064 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1067 // A reloc against a local symbol.
1069 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1070 unsigned int local_sym_index
, unsigned int type
,
1071 Output_data
* od
, Address address
,
1072 Addend addend
, bool is_relative
, bool is_section_symbol
)
1073 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1078 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1079 unsigned int local_sym_index
, unsigned int type
,
1080 unsigned int shndx
, Address address
,
1081 Addend addend
, bool is_relative
, bool is_section_symbol
)
1082 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1087 // A reloc against the STT_SECTION symbol of an output section.
1089 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1090 Address address
, Addend addend
)
1091 : rel_(os
, type
, od
, address
), addend_(addend
)
1094 Output_reloc(Output_section
* os
, unsigned int type
,
1095 Sized_relobj
<size
, big_endian
>* relobj
,
1096 unsigned int shndx
, Address address
, Addend addend
)
1097 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1100 // Write the reloc entry to an output view.
1102 write(unsigned char* pov
) const;
1104 // Return whether this reloc should be sorted before the argument
1105 // when sorting dynamic relocs.
1107 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1110 int i
= this->rel_
.compare(r2
.rel_
);
1116 return this->addend_
< r2
.addend_
;
1121 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1126 // Output_data_reloc is used to manage a section containing relocs.
1127 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1128 // indicates whether this is a dynamic relocation or a normal
1129 // relocation. Output_data_reloc_base is a base class.
1130 // Output_data_reloc is the real class, which we specialize based on
1133 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1134 class Output_data_reloc_base
: public Output_section_data_build
1137 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1138 typedef typename
Output_reloc_type::Address Address
;
1139 static const int reloc_size
=
1140 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1142 // Construct the section.
1143 Output_data_reloc_base(bool sort_relocs
)
1144 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1145 sort_relocs_(sort_relocs
)
1149 // Write out the data.
1151 do_write(Output_file
*);
1153 // Set the entry size and the link.
1155 do_adjust_output_section(Output_section
*os
);
1157 // Write to a map file.
1159 do_print_to_mapfile(Mapfile
* mapfile
) const
1161 mapfile
->print_output_data(this,
1163 ? _("** dynamic relocs")
1167 // Add a relocation entry.
1169 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1171 this->relocs_
.push_back(reloc
);
1172 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1173 od
->add_dynamic_reloc();
1177 typedef std::vector
<Output_reloc_type
> Relocs
;
1179 // The class used to sort the relocations.
1180 struct Sort_relocs_comparison
1183 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1184 { return r1
.sort_before(r2
); }
1187 // The relocations in this section.
1189 // Whether to sort the relocations when writing them out, to make
1190 // the dynamic linker more efficient.
1194 // The class which callers actually create.
1196 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1197 class Output_data_reloc
;
1199 // The SHT_REL version of Output_data_reloc.
1201 template<bool dynamic
, int size
, bool big_endian
>
1202 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1203 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1206 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1210 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1211 typedef typename
Output_reloc_type::Address Address
;
1213 Output_data_reloc(bool sr
)
1214 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1217 // Add a reloc against a global symbol.
1220 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1221 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1224 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1225 Sized_relobj
<size
, big_endian
>* relobj
,
1226 unsigned int shndx
, Address address
)
1227 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1230 // These are to simplify the Copy_relocs class.
1233 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1236 gold_assert(addend
== 0);
1237 this->add_global(gsym
, type
, od
, address
);
1241 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1242 Sized_relobj
<size
, big_endian
>* relobj
,
1243 unsigned int shndx
, Address address
, Address addend
)
1245 gold_assert(addend
== 0);
1246 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1249 // Add a RELATIVE reloc against a global symbol. The final relocation
1250 // will not reference the symbol.
1253 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1255 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1258 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1259 Sized_relobj
<size
, big_endian
>* relobj
,
1260 unsigned int shndx
, Address address
)
1262 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1266 // Add a reloc against a local symbol.
1269 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1270 unsigned int local_sym_index
, unsigned int type
,
1271 Output_data
* od
, Address address
)
1273 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1274 address
, false, false));
1278 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1279 unsigned int local_sym_index
, unsigned int type
,
1280 Output_data
* od
, unsigned int shndx
, Address address
)
1282 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1283 address
, false, false));
1286 // Add a RELATIVE reloc against a local symbol.
1289 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1290 unsigned int local_sym_index
, unsigned int type
,
1291 Output_data
* od
, Address address
)
1293 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1294 address
, true, false));
1298 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1299 unsigned int local_sym_index
, unsigned int type
,
1300 Output_data
* od
, unsigned int shndx
, Address address
)
1302 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1303 address
, true, false));
1306 // Add a reloc against a local section symbol. This will be
1307 // converted into a reloc against the STT_SECTION symbol of the
1311 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1312 unsigned int input_shndx
, unsigned int type
,
1313 Output_data
* od
, Address address
)
1315 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1316 address
, false, true));
1320 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1321 unsigned int input_shndx
, unsigned int type
,
1322 Output_data
* od
, unsigned int shndx
, Address address
)
1324 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1325 address
, false, true));
1328 // A reloc against the STT_SECTION symbol of an output section.
1329 // OS is the Output_section that the relocation refers to; OD is
1330 // the Output_data object being relocated.
1333 add_output_section(Output_section
* os
, unsigned int type
,
1334 Output_data
* od
, Address address
)
1335 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1338 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1339 Sized_relobj
<size
, big_endian
>* relobj
,
1340 unsigned int shndx
, Address address
)
1341 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1344 // The SHT_RELA version of Output_data_reloc.
1346 template<bool dynamic
, int size
, bool big_endian
>
1347 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1348 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1351 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1355 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1356 typedef typename
Output_reloc_type::Address Address
;
1357 typedef typename
Output_reloc_type::Addend Addend
;
1359 Output_data_reloc(bool sr
)
1360 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1363 // Add a reloc against a global symbol.
1366 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1367 Address address
, Addend addend
)
1368 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1372 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1373 Sized_relobj
<size
, big_endian
>* relobj
,
1374 unsigned int shndx
, Address address
,
1376 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1379 // Add a RELATIVE reloc against a global symbol. The final output
1380 // relocation will not reference the symbol, but we must keep the symbol
1381 // information long enough to set the addend of the relocation correctly
1382 // when it is written.
1385 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1386 Address address
, Addend addend
)
1387 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1390 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1391 Sized_relobj
<size
, big_endian
>* relobj
,
1392 unsigned int shndx
, Address address
, Addend addend
)
1393 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1396 // Add a reloc against a local symbol.
1399 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1400 unsigned int local_sym_index
, unsigned int type
,
1401 Output_data
* od
, Address address
, Addend addend
)
1403 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1404 addend
, false, false));
1408 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1409 unsigned int local_sym_index
, unsigned int type
,
1410 Output_data
* od
, unsigned int shndx
, Address address
,
1413 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1414 address
, addend
, false, false));
1417 // Add a RELATIVE reloc against a local symbol.
1420 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1421 unsigned int local_sym_index
, unsigned int type
,
1422 Output_data
* od
, Address address
, Addend addend
)
1424 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1425 addend
, true, false));
1429 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1430 unsigned int local_sym_index
, unsigned int type
,
1431 Output_data
* od
, unsigned int shndx
, Address address
,
1434 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1435 address
, addend
, true, false));
1438 // Add a reloc against a local section symbol. This will be
1439 // converted into a reloc against the STT_SECTION symbol of the
1443 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1444 unsigned int input_shndx
, unsigned int type
,
1445 Output_data
* od
, Address address
, Addend addend
)
1447 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1448 addend
, false, true));
1452 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1453 unsigned int input_shndx
, unsigned int type
,
1454 Output_data
* od
, unsigned int shndx
, Address address
,
1457 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1458 address
, addend
, false, true));
1461 // A reloc against the STT_SECTION symbol of an output section.
1464 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1465 Address address
, Addend addend
)
1466 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1469 add_output_section(Output_section
* os
, unsigned int type
,
1470 Sized_relobj
<size
, big_endian
>* relobj
,
1471 unsigned int shndx
, Address address
, Addend addend
)
1472 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1476 // Output_relocatable_relocs represents a relocation section in a
1477 // relocatable link. The actual data is written out in the target
1478 // hook relocate_for_relocatable. This just saves space for it.
1480 template<int sh_type
, int size
, bool big_endian
>
1481 class Output_relocatable_relocs
: public Output_section_data
1484 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1485 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1490 set_final_data_size();
1492 // Write out the data. There is nothing to do here.
1494 do_write(Output_file
*)
1497 // Write to a map file.
1499 do_print_to_mapfile(Mapfile
* mapfile
) const
1500 { mapfile
->print_output_data(this, _("** relocs")); }
1503 // The relocs associated with this input section.
1504 Relocatable_relocs
* rr_
;
1507 // Handle a GROUP section.
1509 template<int size
, bool big_endian
>
1510 class Output_data_group
: public Output_section_data
1513 // The constructor clears *INPUT_SHNDXES.
1514 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1515 section_size_type entry_count
,
1516 elfcpp::Elf_Word flags
,
1517 std::vector
<unsigned int>* input_shndxes
);
1520 do_write(Output_file
*);
1522 // Write to a map file.
1524 do_print_to_mapfile(Mapfile
* mapfile
) const
1525 { mapfile
->print_output_data(this, _("** group")); }
1528 // The input object.
1529 Sized_relobj
<size
, big_endian
>* relobj_
;
1530 // The group flag word.
1531 elfcpp::Elf_Word flags_
;
1532 // The section indexes of the input sections in this group.
1533 std::vector
<unsigned int> input_shndxes_
;
1536 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1537 // for one symbol--either a global symbol or a local symbol in an
1538 // object. The target specific code adds entries to the GOT as
1541 template<int size
, bool big_endian
>
1542 class Output_data_got
: public Output_section_data_build
1545 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1546 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1547 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1550 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1554 // Add an entry for a global symbol to the GOT. Return true if this
1555 // is a new GOT entry, false if the symbol was already in the GOT.
1557 add_global(Symbol
* gsym
, unsigned int got_type
);
1559 // Add an entry for a global symbol to the GOT, and add a dynamic
1560 // relocation of type R_TYPE for the GOT entry.
1562 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1563 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1566 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1567 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1569 // Add a pair of entries for a global symbol to the GOT, and add
1570 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1572 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1573 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1574 unsigned int r_type_2
);
1577 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1578 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1579 unsigned int r_type_2
);
1581 // Add an entry for a local symbol to the GOT. This returns true if
1582 // this is a new GOT entry, false if the symbol already has a GOT
1585 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1586 unsigned int got_type
);
1588 // Add an entry for a local symbol to the GOT, and add a dynamic
1589 // relocation of type R_TYPE for the GOT entry.
1591 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1592 unsigned int sym_index
, unsigned int got_type
,
1593 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1596 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1597 unsigned int sym_index
, unsigned int got_type
,
1598 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1600 // Add a pair of entries for a local symbol to the GOT, and add
1601 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1603 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1604 unsigned int sym_index
, unsigned int shndx
,
1605 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1606 unsigned int r_type_1
, unsigned int r_type_2
);
1609 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1610 unsigned int sym_index
, unsigned int shndx
,
1611 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1612 unsigned int r_type_1
, unsigned int r_type_2
);
1614 // Add a constant to the GOT. This returns the offset of the new
1615 // entry from the start of the GOT.
1617 add_constant(Valtype constant
)
1619 this->entries_
.push_back(Got_entry(constant
));
1620 this->set_got_size();
1621 return this->last_got_offset();
1625 // Write out the GOT table.
1627 do_write(Output_file
*);
1629 // Write to a map file.
1631 do_print_to_mapfile(Mapfile
* mapfile
) const
1632 { mapfile
->print_output_data(this, _("** GOT")); }
1635 // This POD class holds a single GOT entry.
1639 // Create a zero entry.
1641 : local_sym_index_(CONSTANT_CODE
)
1642 { this->u_
.constant
= 0; }
1644 // Create a global symbol entry.
1645 explicit Got_entry(Symbol
* gsym
)
1646 : local_sym_index_(GSYM_CODE
)
1647 { this->u_
.gsym
= gsym
; }
1649 // Create a local symbol entry.
1650 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1651 unsigned int local_sym_index
)
1652 : local_sym_index_(local_sym_index
)
1654 gold_assert(local_sym_index
!= GSYM_CODE
1655 && local_sym_index
!= CONSTANT_CODE
);
1656 this->u_
.object
= object
;
1659 // Create a constant entry. The constant is a host value--it will
1660 // be swapped, if necessary, when it is written out.
1661 explicit Got_entry(Valtype constant
)
1662 : local_sym_index_(CONSTANT_CODE
)
1663 { this->u_
.constant
= constant
; }
1665 // Write the GOT entry to an output view.
1667 write(unsigned char* pov
) const;
1678 // For a local symbol, the object.
1679 Sized_relobj
<size
, big_endian
>* object
;
1680 // For a global symbol, the symbol.
1682 // For a constant, the constant.
1685 // For a local symbol, the local symbol index. This is GSYM_CODE
1686 // for a global symbol, or CONSTANT_CODE for a constant.
1687 unsigned int local_sym_index_
;
1690 typedef std::vector
<Got_entry
> Got_entries
;
1692 // Return the offset into the GOT of GOT entry I.
1694 got_offset(unsigned int i
) const
1695 { return i
* (size
/ 8); }
1697 // Return the offset into the GOT of the last entry added.
1699 last_got_offset() const
1700 { return this->got_offset(this->entries_
.size() - 1); }
1702 // Set the size of the section.
1705 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1707 // The list of GOT entries.
1708 Got_entries entries_
;
1711 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1714 class Output_data_dynamic
: public Output_section_data
1717 Output_data_dynamic(Stringpool
* pool
)
1718 : Output_section_data(Output_data::default_alignment()),
1719 entries_(), pool_(pool
)
1722 // Add a new dynamic entry with a fixed numeric value.
1724 add_constant(elfcpp::DT tag
, unsigned int val
)
1725 { this->add_entry(Dynamic_entry(tag
, val
)); }
1727 // Add a new dynamic entry with the address of output data.
1729 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1730 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1732 // Add a new dynamic entry with the address of output data
1733 // plus a constant offset.
1735 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
1736 unsigned int offset
)
1737 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
1739 // Add a new dynamic entry with the size of output data.
1741 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1742 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1744 // Add a new dynamic entry with the address of a symbol.
1746 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1747 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1749 // Add a new dynamic entry with a string.
1751 add_string(elfcpp::DT tag
, const char* str
)
1752 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1755 add_string(elfcpp::DT tag
, const std::string
& str
)
1756 { this->add_string(tag
, str
.c_str()); }
1759 // Adjust the output section to set the entry size.
1761 do_adjust_output_section(Output_section
*);
1763 // Set the final data size.
1765 set_final_data_size();
1767 // Write out the dynamic entries.
1769 do_write(Output_file
*);
1771 // Write to a map file.
1773 do_print_to_mapfile(Mapfile
* mapfile
) const
1774 { mapfile
->print_output_data(this, _("** dynamic")); }
1777 // This POD class holds a single dynamic entry.
1781 // Create an entry with a fixed numeric value.
1782 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1783 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
1784 { this->u_
.val
= val
; }
1786 // Create an entry with the size or address of a section.
1787 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1789 offset_(section_size
1790 ? DYNAMIC_SECTION_SIZE
1791 : DYNAMIC_SECTION_ADDRESS
)
1792 { this->u_
.od
= od
; }
1794 // Create an entry with the address of a section plus a constant offset.
1795 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
1798 { this->u_
.od
= od
; }
1800 // Create an entry with the address of a symbol.
1801 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1802 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
1803 { this->u_
.sym
= sym
; }
1805 // Create an entry with a string.
1806 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1807 : tag_(tag
), offset_(DYNAMIC_STRING
)
1808 { this->u_
.str
= str
; }
1810 // Write the dynamic entry to an output view.
1811 template<int size
, bool big_endian
>
1813 write(unsigned char* pov
, const Stringpool
*) const;
1816 // Classification is encoded in the OFFSET field.
1820 DYNAMIC_SECTION_ADDRESS
= 0,
1822 DYNAMIC_NUMBER
= -1U,
1824 DYNAMIC_SECTION_SIZE
= -2U,
1826 DYNAMIC_SYMBOL
= -3U,
1828 DYNAMIC_STRING
= -4U
1829 // Any other value indicates a section address plus OFFSET.
1834 // For DYNAMIC_NUMBER.
1836 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1837 const Output_data
* od
;
1838 // For DYNAMIC_SYMBOL.
1840 // For DYNAMIC_STRING.
1845 // The type of entry (Classification) or offset within a section.
1846 unsigned int offset_
;
1849 // Add an entry to the list.
1851 add_entry(const Dynamic_entry
& entry
)
1852 { this->entries_
.push_back(entry
); }
1854 // Sized version of write function.
1855 template<int size
, bool big_endian
>
1857 sized_write(Output_file
* of
);
1859 // The type of the list of entries.
1860 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1863 Dynamic_entries entries_
;
1864 // The pool used for strings.
1868 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1869 // which may be required if the object file has more than
1870 // SHN_LORESERVE sections.
1872 class Output_symtab_xindex
: public Output_section_data
1875 Output_symtab_xindex(size_t symcount
)
1876 : Output_section_data(symcount
* 4, 4),
1880 // Add an entry: symbol number SYMNDX has section SHNDX.
1882 add(unsigned int symndx
, unsigned int shndx
)
1883 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
1887 do_write(Output_file
*);
1889 // Write to a map file.
1891 do_print_to_mapfile(Mapfile
* mapfile
) const
1892 { mapfile
->print_output_data(this, _("** symtab xindex")); }
1895 template<bool big_endian
>
1897 endian_do_write(unsigned char*);
1899 // It is likely that most symbols will not require entries. Rather
1900 // than keep a vector for all symbols, we keep pairs of symbol index
1901 // and section index.
1902 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
1904 // The entries we need.
1905 Xindex_entries entries_
;
1908 // An output section. We don't expect to have too many output
1909 // sections, so we don't bother to do a template on the size.
1911 class Output_section
: public Output_data
1914 // Create an output section, giving the name, type, and flags.
1915 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1916 virtual ~Output_section();
1918 // Add a new input section SHNDX, named NAME, with header SHDR, from
1919 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1920 // which applies to this section, or 0 if none, or -1 if more than
1921 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1922 // in a linker script; in that case we need to keep track of input
1923 // sections associated with an output section. Return the offset
1924 // within the output section.
1925 template<int size
, bool big_endian
>
1927 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1929 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1930 unsigned int reloc_shndx
, bool have_sections_script
);
1932 // Add generated data POSD to this output section.
1934 add_output_section_data(Output_section_data
* posd
);
1936 // Return the section name.
1939 { return this->name_
; }
1941 // Return the section type.
1944 { return this->type_
; }
1946 // Return the section flags.
1949 { return this->flags_
; }
1951 // Set the section flags. This may only be used with the Layout
1952 // code when it is prepared to move the section to a different
1955 set_flags(elfcpp::Elf_Xword flags
)
1956 { this->flags_
= flags
; }
1958 // Update the output section flags based on input section flags.
1960 update_flags_for_input_section(elfcpp::Elf_Xword flags
)
1962 this->flags_
|= (flags
1963 & (elfcpp::SHF_WRITE
1965 | elfcpp::SHF_EXECINSTR
));
1968 // Return the entsize field.
1971 { return this->entsize_
; }
1973 // Set the entsize field.
1975 set_entsize(uint64_t v
);
1977 // Set the load address.
1979 set_load_address(uint64_t load_address
)
1981 this->load_address_
= load_address
;
1982 this->has_load_address_
= true;
1985 // Set the link field to the output section index of a section.
1987 set_link_section(const Output_data
* od
)
1989 gold_assert(this->link_
== 0
1990 && !this->should_link_to_symtab_
1991 && !this->should_link_to_dynsym_
);
1992 this->link_section_
= od
;
1995 // Set the link field to a constant.
1997 set_link(unsigned int v
)
1999 gold_assert(this->link_section_
== NULL
2000 && !this->should_link_to_symtab_
2001 && !this->should_link_to_dynsym_
);
2005 // Record that this section should link to the normal symbol table.
2007 set_should_link_to_symtab()
2009 gold_assert(this->link_section_
== NULL
2011 && !this->should_link_to_dynsym_
);
2012 this->should_link_to_symtab_
= true;
2015 // Record that this section should link to the dynamic symbol table.
2017 set_should_link_to_dynsym()
2019 gold_assert(this->link_section_
== NULL
2021 && !this->should_link_to_symtab_
);
2022 this->should_link_to_dynsym_
= true;
2025 // Return the info field.
2029 gold_assert(this->info_section_
== NULL
2030 && this->info_symndx_
== NULL
);
2034 // Set the info field to the output section index of a section.
2036 set_info_section(const Output_section
* os
)
2038 gold_assert((this->info_section_
== NULL
2039 || (this->info_section_
== os
2040 && this->info_uses_section_index_
))
2041 && this->info_symndx_
== NULL
2042 && this->info_
== 0);
2043 this->info_section_
= os
;
2044 this->info_uses_section_index_
= true;
2047 // Set the info field to the symbol table index of a symbol.
2049 set_info_symndx(const Symbol
* sym
)
2051 gold_assert(this->info_section_
== NULL
2052 && (this->info_symndx_
== NULL
2053 || this->info_symndx_
== sym
)
2054 && this->info_
== 0);
2055 this->info_symndx_
= sym
;
2058 // Set the info field to the symbol table index of a section symbol.
2060 set_info_section_symndx(const Output_section
* os
)
2062 gold_assert((this->info_section_
== NULL
2063 || (this->info_section_
== os
2064 && !this->info_uses_section_index_
))
2065 && this->info_symndx_
== NULL
2066 && this->info_
== 0);
2067 this->info_section_
= os
;
2068 this->info_uses_section_index_
= false;
2071 // Set the info field to a constant.
2073 set_info(unsigned int v
)
2075 gold_assert(this->info_section_
== NULL
2076 && this->info_symndx_
== NULL
2077 && (this->info_
== 0
2078 || this->info_
== v
));
2082 // Set the addralign field.
2084 set_addralign(uint64_t v
)
2085 { this->addralign_
= v
; }
2087 // Whether the output section index has been set.
2089 has_out_shndx() const
2090 { return this->out_shndx_
!= -1U; }
2092 // Indicate that we need a symtab index.
2094 set_needs_symtab_index()
2095 { this->needs_symtab_index_
= true; }
2097 // Return whether we need a symtab index.
2099 needs_symtab_index() const
2100 { return this->needs_symtab_index_
; }
2102 // Get the symtab index.
2104 symtab_index() const
2106 gold_assert(this->symtab_index_
!= 0);
2107 return this->symtab_index_
;
2110 // Set the symtab index.
2112 set_symtab_index(unsigned int index
)
2114 gold_assert(index
!= 0);
2115 this->symtab_index_
= index
;
2118 // Indicate that we need a dynsym index.
2120 set_needs_dynsym_index()
2121 { this->needs_dynsym_index_
= true; }
2123 // Return whether we need a dynsym index.
2125 needs_dynsym_index() const
2126 { return this->needs_dynsym_index_
; }
2128 // Get the dynsym index.
2130 dynsym_index() const
2132 gold_assert(this->dynsym_index_
!= 0);
2133 return this->dynsym_index_
;
2136 // Set the dynsym index.
2138 set_dynsym_index(unsigned int index
)
2140 gold_assert(index
!= 0);
2141 this->dynsym_index_
= index
;
2144 // Return whether the input sections sections attachd to this output
2145 // section may require sorting. This is used to handle constructor
2146 // priorities compatibly with GNU ld.
2148 may_sort_attached_input_sections() const
2149 { return this->may_sort_attached_input_sections_
; }
2151 // Record that the input sections attached to this output section
2152 // may require sorting.
2154 set_may_sort_attached_input_sections()
2155 { this->may_sort_attached_input_sections_
= true; }
2157 // Return whether the input sections attached to this output section
2158 // require sorting. This is used to handle constructor priorities
2159 // compatibly with GNU ld.
2161 must_sort_attached_input_sections() const
2162 { return this->must_sort_attached_input_sections_
; }
2164 // Record that the input sections attached to this output section
2167 set_must_sort_attached_input_sections()
2168 { this->must_sort_attached_input_sections_
= true; }
2170 // Return whether this section holds relro data--data which has
2171 // dynamic relocations but which may be marked read-only after the
2172 // dynamic relocations have been completed.
2175 { return this->is_relro_
; }
2177 // Record that this section holds relro data.
2180 { this->is_relro_
= true; }
2182 // Record that this section does not hold relro data.
2185 { this->is_relro_
= false; }
2187 // True if this section holds relro local data--relro data for which
2188 // the dynamic relocations are all RELATIVE relocations.
2190 is_relro_local() const
2191 { return this->is_relro_local_
; }
2193 // Record that this section holds relro local data.
2195 set_is_relro_local()
2196 { this->is_relro_local_
= true; }
2198 // Return whether this section should be written after all the input
2199 // sections are complete.
2201 after_input_sections() const
2202 { return this->after_input_sections_
; }
2204 // Record that this section should be written after all the input
2205 // sections are complete.
2207 set_after_input_sections()
2208 { this->after_input_sections_
= true; }
2210 // Return whether this section requires postprocessing after all
2211 // relocations have been applied.
2213 requires_postprocessing() const
2214 { return this->requires_postprocessing_
; }
2216 // If a section requires postprocessing, return the buffer to use.
2218 postprocessing_buffer() const
2220 gold_assert(this->postprocessing_buffer_
!= NULL
);
2221 return this->postprocessing_buffer_
;
2224 // If a section requires postprocessing, create the buffer to use.
2226 create_postprocessing_buffer();
2228 // If a section requires postprocessing, this is the size of the
2229 // buffer to which relocations should be applied.
2231 postprocessing_buffer_size() const
2232 { return this->current_data_size_for_child(); }
2234 // Modify the section name. This is only permitted for an
2235 // unallocated section, and only before the size has been finalized.
2236 // Otherwise the name will not get into Layout::namepool_.
2238 set_name(const char* newname
)
2240 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2241 gold_assert(!this->is_data_size_valid());
2242 this->name_
= newname
;
2245 // Return whether the offset OFFSET in the input section SHNDX in
2246 // object OBJECT is being included in the link.
2248 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2249 off_t offset
) const;
2251 // Return the offset within the output section of OFFSET relative to
2252 // the start of input section SHNDX in object OBJECT.
2254 output_offset(const Relobj
* object
, unsigned int shndx
,
2255 section_offset_type offset
) const;
2257 // Return the output virtual address of OFFSET relative to the start
2258 // of input section SHNDX in object OBJECT.
2260 output_address(const Relobj
* object
, unsigned int shndx
,
2261 off_t offset
) const;
2263 // Return the output address of the start of the merged section for
2264 // input section SHNDX in object OBJECT. This is not necessarily
2265 // the offset corresponding to input offset 0 in the section, since
2266 // the section may be mapped arbitrarily.
2268 starting_output_address(const Relobj
* object
, unsigned int shndx
) const;
2270 // Record that this output section was found in the SECTIONS clause
2271 // of a linker script.
2273 set_found_in_sections_clause()
2274 { this->found_in_sections_clause_
= true; }
2276 // Return whether this output section was found in the SECTIONS
2277 // clause of a linker script.
2279 found_in_sections_clause() const
2280 { return this->found_in_sections_clause_
; }
2282 // Write the section header into *OPHDR.
2283 template<int size
, bool big_endian
>
2285 write_header(const Layout
*, const Stringpool
*,
2286 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2288 // The next few calls are for linker script support.
2290 // Store the list of input sections for this Output_section into the
2291 // list passed in. This removes the input sections, leaving only
2292 // any Output_section_data elements. This returns the size of those
2293 // Output_section_data elements. ADDRESS is the address of this
2294 // output section. FILL is the fill value to use, in case there are
2295 // any spaces between the remaining Output_section_data elements.
2297 get_input_sections(uint64_t address
, const std::string
& fill
,
2298 std::list
<std::pair
<Relobj
*, unsigned int > >*);
2300 // Add an input section from a script.
2302 add_input_section_for_script(Relobj
* object
, unsigned int shndx
,
2303 off_t data_size
, uint64_t addralign
);
2305 // Set the current size of the output section.
2307 set_current_data_size(off_t size
)
2308 { this->set_current_data_size_for_child(size
); }
2310 // Get the current size of the output section.
2312 current_data_size() const
2313 { return this->current_data_size_for_child(); }
2315 // End of linker script support.
2317 // Print merge statistics to stderr.
2319 print_merge_stats();
2322 // Return the output section--i.e., the object itself.
2327 // Return the section index in the output file.
2329 do_out_shndx() const
2331 gold_assert(this->out_shndx_
!= -1U);
2332 return this->out_shndx_
;
2335 // Set the output section index.
2337 do_set_out_shndx(unsigned int shndx
)
2339 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2340 this->out_shndx_
= shndx
;
2343 // Set the final data size of the Output_section. For a typical
2344 // Output_section, there is nothing to do, but if there are any
2345 // Output_section_data objects we need to set their final addresses
2348 set_final_data_size();
2350 // Reset the address and file offset.
2352 do_reset_address_and_file_offset();
2354 // Write the data to the file. For a typical Output_section, this
2355 // does nothing: the data is written out by calling Object::Relocate
2356 // on each input object. But if there are any Output_section_data
2357 // objects we do need to write them out here.
2359 do_write(Output_file
*);
2361 // Return the address alignment--function required by parent class.
2363 do_addralign() const
2364 { return this->addralign_
; }
2366 // Return whether there is a load address.
2368 do_has_load_address() const
2369 { return this->has_load_address_
; }
2371 // Return the load address.
2373 do_load_address() const
2375 gold_assert(this->has_load_address_
);
2376 return this->load_address_
;
2379 // Return whether this is an Output_section.
2381 do_is_section() const
2384 // Return whether this is a section of the specified type.
2386 do_is_section_type(elfcpp::Elf_Word type
) const
2387 { return this->type_
== type
; }
2389 // Return whether the specified section flag is set.
2391 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2392 { return (this->flags_
& flag
) != 0; }
2394 // Set the TLS offset. Called only for SHT_TLS sections.
2396 do_set_tls_offset(uint64_t tls_base
);
2398 // Return the TLS offset, relative to the base of the TLS segment.
2399 // Valid only for SHT_TLS sections.
2401 do_tls_offset() const
2402 { return this->tls_offset_
; }
2404 // This may be implemented by a child class.
2406 do_finalize_name(Layout
*)
2409 // Print to the map file.
2411 do_print_to_mapfile(Mapfile
*) const;
2413 // Record that this section requires postprocessing after all
2414 // relocations have been applied. This is called by a child class.
2416 set_requires_postprocessing()
2418 this->requires_postprocessing_
= true;
2419 this->after_input_sections_
= true;
2422 // Write all the data of an Output_section into the postprocessing
2425 write_to_postprocessing_buffer();
2428 // In some cases we need to keep a list of the input sections
2429 // associated with this output section. We only need the list if we
2430 // might have to change the offsets of the input section within the
2431 // output section after we add the input section. The ordinary
2432 // input sections will be written out when we process the object
2433 // file, and as such we don't need to track them here. We do need
2434 // to track Output_section_data objects here. We store instances of
2435 // this structure in a std::vector, so it must be a POD. There can
2436 // be many instances of this structure, so we use a union to save
2442 : shndx_(0), p2align_(0)
2444 this->u1_
.data_size
= 0;
2445 this->u2_
.object
= NULL
;
2448 // For an ordinary input section.
2449 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2452 p2align_(ffsll(static_cast<long long>(addralign
)))
2454 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2455 && shndx
!= MERGE_DATA_SECTION_CODE
2456 && shndx
!= MERGE_STRING_SECTION_CODE
);
2457 this->u1_
.data_size
= data_size
;
2458 this->u2_
.object
= object
;
2461 // For a non-merge output section.
2462 Input_section(Output_section_data
* posd
)
2463 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0)
2465 this->u1_
.data_size
= 0;
2466 this->u2_
.posd
= posd
;
2469 // For a merge section.
2470 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2472 ? MERGE_STRING_SECTION_CODE
2473 : MERGE_DATA_SECTION_CODE
),
2476 this->u1_
.entsize
= entsize
;
2477 this->u2_
.posd
= posd
;
2480 // The required alignment.
2484 if (!this->is_input_section())
2485 return this->u2_
.posd
->addralign();
2486 return (this->p2align_
== 0
2488 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2491 // Return the required size.
2495 // Whether this is an input section.
2497 is_input_section() const
2499 return (this->shndx_
!= OUTPUT_SECTION_CODE
2500 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2501 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
2504 // Return whether this is a merge section which matches the
2507 is_merge_section(bool is_string
, uint64_t entsize
,
2508 uint64_t addralign
) const
2510 return (this->shndx_
== (is_string
2511 ? MERGE_STRING_SECTION_CODE
2512 : MERGE_DATA_SECTION_CODE
)
2513 && this->u1_
.entsize
== entsize
2514 && this->addralign() == addralign
);
2517 // Return the object for an input section.
2521 gold_assert(this->is_input_section());
2522 return this->u2_
.object
;
2525 // Return the input section index for an input section.
2529 gold_assert(this->is_input_section());
2530 return this->shndx_
;
2533 // Set the output section.
2535 set_output_section(Output_section
* os
)
2537 gold_assert(!this->is_input_section());
2538 this->u2_
.posd
->set_output_section(os
);
2541 // Set the address and file offset. This is called during
2542 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2543 // the enclosing section.
2545 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2546 off_t section_file_offset
);
2548 // Reset the address and file offset.
2550 reset_address_and_file_offset();
2552 // Finalize the data size.
2554 finalize_data_size();
2556 // Add an input section, for SHF_MERGE sections.
2558 add_input_section(Relobj
* object
, unsigned int shndx
)
2560 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2561 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2562 return this->u2_
.posd
->add_input_section(object
, shndx
);
2565 // Given an input OBJECT, an input section index SHNDX within that
2566 // object, and an OFFSET relative to the start of that input
2567 // section, return whether or not the output offset is known. If
2568 // this function returns true, it sets *POUTPUT to the offset in
2569 // the output section, relative to the start of the input section
2570 // in the output section. *POUTPUT may be different from OFFSET
2571 // for a merged section.
2573 output_offset(const Relobj
* object
, unsigned int shndx
,
2574 section_offset_type offset
,
2575 section_offset_type
*poutput
) const;
2577 // Return whether this is the merge section for the input section
2580 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2582 // Write out the data. This does nothing for an input section.
2584 write(Output_file
*);
2586 // Write the data to a buffer. This does nothing for an input
2589 write_to_buffer(unsigned char*);
2591 // Print to a map file.
2593 print_to_mapfile(Mapfile
*) const;
2595 // Print statistics about merge sections to stderr.
2597 print_merge_stats(const char* section_name
)
2599 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2600 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2601 this->u2_
.posd
->print_merge_stats(section_name
);
2605 // Code values which appear in shndx_. If the value is not one of
2606 // these codes, it is the input section index in the object file.
2609 // An Output_section_data.
2610 OUTPUT_SECTION_CODE
= -1U,
2611 // An Output_section_data for an SHF_MERGE section with
2612 // SHF_STRINGS not set.
2613 MERGE_DATA_SECTION_CODE
= -2U,
2614 // An Output_section_data for an SHF_MERGE section with
2616 MERGE_STRING_SECTION_CODE
= -3U
2619 // For an ordinary input section, this is the section index in the
2620 // input file. For an Output_section_data, this is
2621 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2622 // MERGE_STRING_SECTION_CODE.
2623 unsigned int shndx_
;
2624 // The required alignment, stored as a power of 2.
2625 unsigned int p2align_
;
2628 // For an ordinary input section, the section size.
2630 // For OUTPUT_SECTION_CODE, this is not used. For
2631 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2637 // For an ordinary input section, the object which holds the
2640 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2641 // MERGE_STRING_SECTION_CODE, the data.
2642 Output_section_data
* posd
;
2646 typedef std::vector
<Input_section
> Input_section_list
;
2648 // This class is used to sort the input sections.
2649 class Input_section_sort_entry
;
2651 // This is the sort comparison function.
2652 struct Input_section_sort_compare
2655 operator()(const Input_section_sort_entry
&,
2656 const Input_section_sort_entry
&) const;
2659 // Fill data. This is used to fill in data between input sections.
2660 // It is also used for data statements (BYTE, WORD, etc.) in linker
2661 // scripts. When we have to keep track of the input sections, we
2662 // can use an Output_data_const, but we don't want to have to keep
2663 // track of input sections just to implement fills.
2667 Fill(off_t section_offset
, off_t length
)
2668 : section_offset_(section_offset
),
2669 length_(convert_to_section_size_type(length
))
2672 // Return section offset.
2674 section_offset() const
2675 { return this->section_offset_
; }
2677 // Return fill length.
2680 { return this->length_
; }
2683 // The offset within the output section.
2684 off_t section_offset_
;
2685 // The length of the space to fill.
2686 section_size_type length_
;
2689 typedef std::vector
<Fill
> Fill_list
;
2691 // Add a new output section by Input_section.
2693 add_output_section_data(Input_section
*);
2695 // Add an SHF_MERGE input section. Returns true if the section was
2698 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
2699 uint64_t entsize
, uint64_t addralign
);
2701 // Add an output SHF_MERGE section POSD to this output section.
2702 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2703 // ENTSIZE is the entity size. This returns the entry added to
2706 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
2709 // Sort the attached input sections.
2711 sort_attached_input_sections();
2713 // Most of these fields are only valid after layout.
2715 // The name of the section. This will point into a Stringpool.
2717 // The section address is in the parent class.
2718 // The section alignment.
2719 uint64_t addralign_
;
2720 // The section entry size.
2722 // The load address. This is only used when using a linker script
2723 // with a SECTIONS clause. The has_load_address_ field indicates
2724 // whether this field is valid.
2725 uint64_t load_address_
;
2726 // The file offset is in the parent class.
2727 // Set the section link field to the index of this section.
2728 const Output_data
* link_section_
;
2729 // If link_section_ is NULL, this is the link field.
2731 // Set the section info field to the index of this section.
2732 const Output_section
* info_section_
;
2733 // If info_section_ is NULL, set the info field to the symbol table
2734 // index of this symbol.
2735 const Symbol
* info_symndx_
;
2736 // If info_section_ and info_symndx_ are NULL, this is the section
2739 // The section type.
2740 const elfcpp::Elf_Word type_
;
2741 // The section flags.
2742 elfcpp::Elf_Xword flags_
;
2743 // The section index.
2744 unsigned int out_shndx_
;
2745 // If there is a STT_SECTION for this output section in the normal
2746 // symbol table, this is the symbol index. This starts out as zero.
2747 // It is initialized in Layout::finalize() to be the index, or -1U
2748 // if there isn't one.
2749 unsigned int symtab_index_
;
2750 // If there is a STT_SECTION for this output section in the dynamic
2751 // symbol table, this is the symbol index. This starts out as zero.
2752 // It is initialized in Layout::finalize() to be the index, or -1U
2753 // if there isn't one.
2754 unsigned int dynsym_index_
;
2755 // The input sections. This will be empty in cases where we don't
2756 // need to keep track of them.
2757 Input_section_list input_sections_
;
2758 // The offset of the first entry in input_sections_.
2759 off_t first_input_offset_
;
2760 // The fill data. This is separate from input_sections_ because we
2761 // often will need fill sections without needing to keep track of
2764 // If the section requires postprocessing, this buffer holds the
2765 // section contents during relocation.
2766 unsigned char* postprocessing_buffer_
;
2767 // Whether this output section needs a STT_SECTION symbol in the
2768 // normal symbol table. This will be true if there is a relocation
2770 bool needs_symtab_index_
: 1;
2771 // Whether this output section needs a STT_SECTION symbol in the
2772 // dynamic symbol table. This will be true if there is a dynamic
2773 // relocation which needs it.
2774 bool needs_dynsym_index_
: 1;
2775 // Whether the link field of this output section should point to the
2776 // normal symbol table.
2777 bool should_link_to_symtab_
: 1;
2778 // Whether the link field of this output section should point to the
2779 // dynamic symbol table.
2780 bool should_link_to_dynsym_
: 1;
2781 // Whether this section should be written after all the input
2782 // sections are complete.
2783 bool after_input_sections_
: 1;
2784 // Whether this section requires post processing after all
2785 // relocations have been applied.
2786 bool requires_postprocessing_
: 1;
2787 // Whether an input section was mapped to this output section
2788 // because of a SECTIONS clause in a linker script.
2789 bool found_in_sections_clause_
: 1;
2790 // Whether this section has an explicitly specified load address.
2791 bool has_load_address_
: 1;
2792 // True if the info_section_ field means the section index of the
2793 // section, false if it means the symbol index of the corresponding
2795 bool info_uses_section_index_
: 1;
2796 // True if the input sections attached to this output section may
2798 bool may_sort_attached_input_sections_
: 1;
2799 // True if the input sections attached to this output section must
2801 bool must_sort_attached_input_sections_
: 1;
2802 // True if the input sections attached to this output section have
2803 // already been sorted.
2804 bool attached_input_sections_are_sorted_
: 1;
2805 // True if this section holds relro data.
2807 // True if this section holds relro local data.
2808 bool is_relro_local_
: 1;
2809 // For SHT_TLS sections, the offset of this section relative to the base
2810 // of the TLS segment.
2811 uint64_t tls_offset_
;
2814 // An output segment. PT_LOAD segments are built from collections of
2815 // output sections. Other segments typically point within PT_LOAD
2816 // segments, and are built directly as needed.
2818 class Output_segment
2821 // Create an output segment, specifying the type and flags.
2822 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2824 // Return the virtual address.
2827 { return this->vaddr_
; }
2829 // Return the physical address.
2832 { return this->paddr_
; }
2834 // Return the segment type.
2837 { return this->type_
; }
2839 // Return the segment flags.
2842 { return this->flags_
; }
2844 // Return the memory size.
2847 { return this->memsz_
; }
2849 // Return the file size.
2852 { return this->filesz_
; }
2854 // Return the file offset.
2857 { return this->offset_
; }
2859 // Return the maximum alignment of the Output_data.
2861 maximum_alignment();
2863 // Add an Output_section to this segment.
2865 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
);
2867 // Remove an Output_section from this segment. It is an error if it
2870 remove_output_section(Output_section
* os
);
2872 // Add an Output_data (which is not an Output_section) to the start
2875 add_initial_output_data(Output_data
*);
2877 // Return true if this segment has any sections which hold actual
2878 // data, rather than being a BSS section.
2880 has_any_data_sections() const
2881 { return !this->output_data_
.empty(); }
2883 // Return the number of dynamic relocations applied to this segment.
2885 dynamic_reloc_count() const;
2887 // Return the address of the first section.
2889 first_section_load_address() const;
2891 // Return whether the addresses have been set already.
2893 are_addresses_set() const
2894 { return this->are_addresses_set_
; }
2896 // Set the addresses.
2898 set_addresses(uint64_t vaddr
, uint64_t paddr
)
2900 this->vaddr_
= vaddr
;
2901 this->paddr_
= paddr
;
2902 this->are_addresses_set_
= true;
2905 // Set the segment flags. This is only used if we have a PHDRS
2906 // clause which explicitly specifies the flags.
2908 set_flags(elfcpp::Elf_Word flags
)
2909 { this->flags_
= flags
; }
2911 // Set the address of the segment to ADDR and the offset to *POFF
2912 // and set the addresses and offsets of all contained output
2913 // sections accordingly. Set the section indexes of all contained
2914 // output sections starting with *PSHNDX. If RESET is true, first
2915 // reset the addresses of the contained sections. Return the
2916 // address of the immediately following segment. Update *POFF and
2917 // *PSHNDX. This should only be called for a PT_LOAD segment.
2919 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
, off_t
* poff
,
2920 unsigned int* pshndx
);
2922 // Set the minimum alignment of this segment. This may be adjusted
2923 // upward based on the section alignments.
2925 set_minimum_p_align(uint64_t align
)
2926 { this->min_p_align_
= align
; }
2928 // Set the offset of this segment based on the section. This should
2929 // only be called for a non-PT_LOAD segment.
2933 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2937 // Return the number of output sections.
2939 output_section_count() const;
2941 // Return the section attached to the list segment with the lowest
2942 // load address. This is used when handling a PHDRS clause in a
2945 section_with_lowest_load_address() const;
2947 // Write the segment header into *OPHDR.
2948 template<int size
, bool big_endian
>
2950 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2952 // Write the section headers of associated sections into V.
2953 template<int size
, bool big_endian
>
2955 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2956 unsigned int* pshndx
) const;
2958 // Print the output sections in the map file.
2960 print_sections_to_mapfile(Mapfile
*) const;
2963 Output_segment(const Output_segment
&);
2964 Output_segment
& operator=(const Output_segment
&);
2966 typedef std::list
<Output_data
*> Output_data_list
;
2968 // Find the maximum alignment in an Output_data_list.
2970 maximum_alignment_list(const Output_data_list
*);
2972 // Return whether the first data section is a relro section.
2974 is_first_section_relro() const;
2976 // Set the section addresses in an Output_data_list.
2978 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
2979 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
2980 bool* in_tls
, bool* in_relro
);
2982 // Return the number of Output_sections in an Output_data_list.
2984 output_section_count_list(const Output_data_list
*) const;
2986 // Return the number of dynamic relocs in an Output_data_list.
2988 dynamic_reloc_count_list(const Output_data_list
*) const;
2990 // Find the section with the lowest load address in an
2991 // Output_data_list.
2993 lowest_load_address_in_list(const Output_data_list
* pdl
,
2994 Output_section
** found
,
2995 uint64_t* found_lma
) const;
2997 // Write the section headers in the list into V.
2998 template<int size
, bool big_endian
>
3000 write_section_headers_list(const Layout
*, const Stringpool
*,
3001 const Output_data_list
*, unsigned char* v
,
3002 unsigned int* pshdx
) const;
3004 // Print a section list to the mapfile.
3006 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
3008 // The list of output data with contents attached to this segment.
3009 Output_data_list output_data_
;
3010 // The list of output data without contents attached to this segment.
3011 Output_data_list output_bss_
;
3012 // The segment virtual address.
3014 // The segment physical address.
3016 // The size of the segment in memory.
3018 // The maximum section alignment. The is_max_align_known_ field
3019 // indicates whether this has been finalized.
3020 uint64_t max_align_
;
3021 // The required minimum value for the p_align field. This is used
3022 // for PT_LOAD segments. Note that this does not mean that
3023 // addresses should be aligned to this value; it means the p_paddr
3024 // and p_vaddr fields must be congruent modulo this value. For
3025 // non-PT_LOAD segments, the dynamic linker works more efficiently
3026 // if the p_align field has the more conventional value, although it
3027 // can align as needed.
3028 uint64_t min_p_align_
;
3029 // The offset of the segment data within the file.
3031 // The size of the segment data in the file.
3033 // The segment type;
3034 elfcpp::Elf_Word type_
;
3035 // The segment flags.
3036 elfcpp::Elf_Word flags_
;
3037 // Whether we have finalized max_align_.
3038 bool is_max_align_known_
: 1;
3039 // Whether vaddr and paddr were set by a linker script.
3040 bool are_addresses_set_
: 1;
3043 // This class represents the output file.
3048 Output_file(const char* name
);
3050 // Indicate that this is a temporary file which should not be
3054 { this->is_temporary_
= true; }
3056 // Open the output file. FILE_SIZE is the final size of the file.
3058 open(off_t file_size
);
3060 // Resize the output file.
3062 resize(off_t file_size
);
3064 // Close the output file (flushing all buffered data) and make sure
3065 // there are no errors.
3069 // We currently always use mmap which makes the view handling quite
3070 // simple. In the future we may support other approaches.
3072 // Write data to the output file.
3074 write(off_t offset
, const void* data
, size_t len
)
3075 { memcpy(this->base_
+ offset
, data
, len
); }
3077 // Get a buffer to use to write to the file, given the offset into
3078 // the file and the size.
3080 get_output_view(off_t start
, size_t size
)
3082 gold_assert(start
>= 0
3083 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
3084 return this->base_
+ start
;
3087 // VIEW must have been returned by get_output_view. Write the
3088 // buffer to the file, passing in the offset and the size.
3090 write_output_view(off_t
, size_t, unsigned char*)
3093 // Get a read/write buffer. This is used when we want to write part
3094 // of the file, read it in, and write it again.
3096 get_input_output_view(off_t start
, size_t size
)
3097 { return this->get_output_view(start
, size
); }
3099 // Write a read/write buffer back to the file.
3101 write_input_output_view(off_t
, size_t, unsigned char*)
3104 // Get a read buffer. This is used when we just want to read part
3105 // of the file back it in.
3106 const unsigned char*
3107 get_input_view(off_t start
, size_t size
)
3108 { return this->get_output_view(start
, size
); }
3110 // Release a read bfufer.
3112 free_input_view(off_t
, size_t, const unsigned char*)
3116 // Map the file into memory and return a pointer to the map.
3120 // Unmap the file from memory (and flush to disk buffers).
3130 // Base of file mapped into memory.
3131 unsigned char* base_
;
3132 // True iff base_ points to a memory buffer rather than an output file.
3133 bool map_is_anonymous_
;
3134 // True if this is a temporary file which should not be output.
3138 } // End namespace gold.
3140 #endif // !defined(GOLD_OUTPUT_H)