1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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 // This class specifies an input section. It is used as a key type
53 class Input_section_specifier
56 Input_section_specifier(const Relobj
* relobj
, unsigned int shndx
)
57 : relobj_(relobj
), shndx_(shndx
)
60 // Return Relobj of this.
63 { return this->relobj_
; }
65 // Return section index of this.
68 { return this->shndx_
; }
70 // Whether this equals to another specifier ISS.
72 eq(const Input_section_specifier
& iss
) const
73 { return this->relobj_
== iss
.relobj_
&& this->shndx_
== iss
.shndx_
; }
75 // Compute a hash value of this.
79 return (gold::string_hash
<char>(this->relobj_
->name().c_str())
83 // Functors for containers.
87 operator()(const Input_section_specifier
& iss1
,
88 const Input_section_specifier
& iss2
) const
89 { return iss1
.eq(iss2
); }
95 operator()(const Input_section_specifier
& iss
) const
96 { return iss
.hash_value(); }
101 const Relobj
* relobj_
;
106 // An abtract class for data which has to go into the output file.
111 explicit Output_data()
112 : address_(0), data_size_(0), offset_(-1),
113 is_address_valid_(false), is_data_size_valid_(false),
114 is_offset_valid_(false), is_data_size_fixed_(false),
115 dynamic_reloc_count_(0)
121 // Return the address. For allocated sections, this is only valid
122 // after Layout::finalize is finished.
126 gold_assert(this->is_address_valid_
);
127 return this->address_
;
130 // Return the size of the data. For allocated sections, this must
131 // be valid after Layout::finalize calls set_address, but need not
132 // be valid before then.
136 gold_assert(this->is_data_size_valid_
);
137 return this->data_size_
;
140 // Return true if data size is fixed.
142 is_data_size_fixed() const
143 { return this->is_data_size_fixed_
; }
145 // Return the file offset. This is only valid after
146 // Layout::finalize is finished. For some non-allocated sections,
147 // it may not be valid until near the end of the link.
151 gold_assert(this->is_offset_valid_
);
152 return this->offset_
;
155 // Reset the address and file offset. This essentially disables the
156 // sanity testing about duplicate and unknown settings.
158 reset_address_and_file_offset()
160 this->is_address_valid_
= false;
161 this->is_offset_valid_
= false;
162 if (!this->is_data_size_fixed_
)
163 this->is_data_size_valid_
= false;
164 this->do_reset_address_and_file_offset();
167 // Return true if address and file offset already have reset values. In
168 // other words, calling reset_address_and_file_offset will not change them.
170 address_and_file_offset_have_reset_values() const
171 { return this->do_address_and_file_offset_have_reset_values(); }
173 // Return the required alignment.
176 { return this->do_addralign(); }
178 // Return whether this has a load address.
180 has_load_address() const
181 { return this->do_has_load_address(); }
183 // Return the load address.
186 { return this->do_load_address(); }
188 // Return whether this is an Output_section.
191 { return this->do_is_section(); }
193 // Return whether this is an Output_section of the specified type.
195 is_section_type(elfcpp::Elf_Word stt
) const
196 { return this->do_is_section_type(stt
); }
198 // Return whether this is an Output_section with the specified flag
201 is_section_flag_set(elfcpp::Elf_Xword shf
) const
202 { return this->do_is_section_flag_set(shf
); }
204 // Return the output section that this goes in, if there is one.
207 { return this->do_output_section(); }
209 // Return the output section index, if there is an output section.
212 { return this->do_out_shndx(); }
214 // Set the output section index, if this is an output section.
216 set_out_shndx(unsigned int shndx
)
217 { this->do_set_out_shndx(shndx
); }
219 // Set the address and file offset of this data, and finalize the
220 // size of the data. This is called during Layout::finalize for
221 // allocated sections.
223 set_address_and_file_offset(uint64_t addr
, off_t off
)
225 this->set_address(addr
);
226 this->set_file_offset(off
);
227 this->finalize_data_size();
232 set_address(uint64_t addr
)
234 gold_assert(!this->is_address_valid_
);
235 this->address_
= addr
;
236 this->is_address_valid_
= true;
239 // Set the file offset.
241 set_file_offset(off_t off
)
243 gold_assert(!this->is_offset_valid_
);
245 this->is_offset_valid_
= true;
248 // Finalize the data size.
252 if (!this->is_data_size_valid_
)
254 // Tell the child class to set the data size.
255 this->set_final_data_size();
256 gold_assert(this->is_data_size_valid_
);
260 // Set the TLS offset. Called only for SHT_TLS sections.
262 set_tls_offset(uint64_t tls_base
)
263 { this->do_set_tls_offset(tls_base
); }
265 // Return the TLS offset, relative to the base of the TLS segment.
266 // Valid only for SHT_TLS sections.
269 { return this->do_tls_offset(); }
271 // Write the data to the output file. This is called after
272 // Layout::finalize is complete.
274 write(Output_file
* file
)
275 { this->do_write(file
); }
277 // This is called by Layout::finalize to note that the sizes of
278 // allocated sections must now be fixed.
281 { Output_data::allocated_sizes_are_fixed
= true; }
283 // Used to check that layout has been done.
286 { return Output_data::allocated_sizes_are_fixed
; }
288 // Count the number of dynamic relocations applied to this section.
291 { ++this->dynamic_reloc_count_
; }
293 // Return the number of dynamic relocations applied to this section.
295 dynamic_reloc_count() const
296 { return this->dynamic_reloc_count_
; }
298 // Whether the address is valid.
300 is_address_valid() const
301 { return this->is_address_valid_
; }
303 // Whether the file offset is valid.
305 is_offset_valid() const
306 { return this->is_offset_valid_
; }
308 // Whether the data size is valid.
310 is_data_size_valid() const
311 { return this->is_data_size_valid_
; }
313 // Print information to the map file.
315 print_to_mapfile(Mapfile
* mapfile
) const
316 { return this->do_print_to_mapfile(mapfile
); }
319 // Functions that child classes may or in some cases must implement.
321 // Write the data to the output file.
323 do_write(Output_file
*) = 0;
325 // Return the required alignment.
327 do_addralign() const = 0;
329 // Return whether this has a load address.
331 do_has_load_address() const
334 // Return the load address.
336 do_load_address() const
337 { gold_unreachable(); }
339 // Return whether this is an Output_section.
341 do_is_section() const
344 // Return whether this is an Output_section of the specified type.
345 // This only needs to be implement by Output_section.
347 do_is_section_type(elfcpp::Elf_Word
) const
350 // Return whether this is an Output_section with the specific flag
351 // set. This only needs to be implemented by Output_section.
353 do_is_section_flag_set(elfcpp::Elf_Xword
) const
356 // Return the output section, if there is one.
357 virtual Output_section
*
361 // Return the output section index, if there is an output section.
364 { gold_unreachable(); }
366 // Set the output section index, if this is an output section.
368 do_set_out_shndx(unsigned int)
369 { gold_unreachable(); }
371 // This is a hook for derived classes to set the data size. This is
372 // called by finalize_data_size, normally called during
373 // Layout::finalize, when the section address is set.
375 set_final_data_size()
376 { gold_unreachable(); }
378 // A hook for resetting the address and file offset.
380 do_reset_address_and_file_offset()
383 // Return true if address and file offset already have reset values. In
384 // other words, calling reset_address_and_file_offset will not change them.
385 // A child class overriding do_reset_address_and_file_offset may need to
386 // also override this.
388 do_address_and_file_offset_have_reset_values() const
389 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
391 // Set the TLS offset. Called only for SHT_TLS sections.
393 do_set_tls_offset(uint64_t)
394 { gold_unreachable(); }
396 // Return the TLS offset, relative to the base of the TLS segment.
397 // Valid only for SHT_TLS sections.
399 do_tls_offset() const
400 { gold_unreachable(); }
402 // Print to the map file. This only needs to be implemented by
403 // classes which may appear in a PT_LOAD segment.
405 do_print_to_mapfile(Mapfile
*) const
406 { gold_unreachable(); }
408 // Functions that child classes may call.
410 // Reset the address. The Output_section class needs this when an
411 // SHF_ALLOC input section is added to an output section which was
412 // formerly not SHF_ALLOC.
414 mark_address_invalid()
415 { this->is_address_valid_
= false; }
417 // Set the size of the data.
419 set_data_size(off_t data_size
)
421 gold_assert(!this->is_data_size_valid_
422 && !this->is_data_size_fixed_
);
423 this->data_size_
= data_size
;
424 this->is_data_size_valid_
= true;
427 // Fix the data size. Once it is fixed, it cannot be changed
428 // and the data size remains always valid.
432 gold_assert(this->is_data_size_valid_
);
433 this->is_data_size_fixed_
= true;
436 // Get the current data size--this is for the convenience of
437 // sections which build up their size over time.
439 current_data_size_for_child() const
440 { return this->data_size_
; }
442 // Set the current data size--this is for the convenience of
443 // sections which build up their size over time.
445 set_current_data_size_for_child(off_t data_size
)
447 gold_assert(!this->is_data_size_valid_
);
448 this->data_size_
= data_size
;
451 // Return default alignment for the target size.
455 // Return default alignment for a specified size--32 or 64.
457 default_alignment_for_size(int size
);
460 Output_data(const Output_data
&);
461 Output_data
& operator=(const Output_data
&);
463 // This is used for verification, to make sure that we don't try to
464 // change any sizes of allocated sections after we set the section
466 static bool allocated_sizes_are_fixed
;
468 // Memory address in output file.
470 // Size of data in output file.
472 // File offset of contents in output file.
474 // Whether address_ is valid.
475 bool is_address_valid_
;
476 // Whether data_size_ is valid.
477 bool is_data_size_valid_
;
478 // Whether offset_ is valid.
479 bool is_offset_valid_
;
480 // Whether data size is fixed.
481 bool is_data_size_fixed_
;
482 // Count of dynamic relocations applied to this section.
483 unsigned int dynamic_reloc_count_
;
486 // Output the section headers.
488 class Output_section_headers
: public Output_data
491 Output_section_headers(const Layout
*,
492 const Layout::Segment_list
*,
493 const Layout::Section_list
*,
494 const Layout::Section_list
*,
496 const Output_section
*);
499 // Write the data to the file.
501 do_write(Output_file
*);
503 // Return the required alignment.
506 { return Output_data::default_alignment(); }
508 // Write to a map file.
510 do_print_to_mapfile(Mapfile
* mapfile
) const
511 { mapfile
->print_output_data(this, _("** section headers")); }
513 // Set final data size.
515 set_final_data_size()
516 { this->set_data_size(this->do_size()); }
519 // Write the data to the file with the right size and endianness.
520 template<int size
, bool big_endian
>
522 do_sized_write(Output_file
*);
524 // Compute data size.
528 const Layout
* layout_
;
529 const Layout::Segment_list
* segment_list_
;
530 const Layout::Section_list
* section_list_
;
531 const Layout::Section_list
* unattached_section_list_
;
532 const Stringpool
* secnamepool_
;
533 const Output_section
* shstrtab_section_
;
536 // Output the segment headers.
538 class Output_segment_headers
: public Output_data
541 Output_segment_headers(const Layout::Segment_list
& segment_list
);
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, _("** segment headers")); }
558 // Set final data size.
560 set_final_data_size()
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 // Compute the current size.
573 const Layout::Segment_list
& segment_list_
;
576 // Output the ELF file header.
578 class Output_file_header
: public Output_data
581 Output_file_header(const Target
*,
583 const Output_segment_headers
*,
586 // Add information about the section headers. We lay out the ELF
587 // file header before we create the section headers.
588 void set_section_info(const Output_section_headers
*,
589 const Output_section
* shstrtab
);
592 // Write the data to the file.
594 do_write(Output_file
*);
596 // Return the required alignment.
599 { return Output_data::default_alignment(); }
601 // Write to a map file.
603 do_print_to_mapfile(Mapfile
* mapfile
) const
604 { mapfile
->print_output_data(this, _("** file header")); }
606 // Set final data size.
608 set_final_data_size(void)
609 { this->set_data_size(this->do_size()); }
612 // Write the data to the file with the right size and endianness.
613 template<int size
, bool big_endian
>
615 do_sized_write(Output_file
*);
617 // Return the value to use for the entry address.
619 typename
elfcpp::Elf_types
<size
>::Elf_Addr
622 // Compute the current data size.
626 const Target
* target_
;
627 const Symbol_table
* symtab_
;
628 const Output_segment_headers
* segment_header_
;
629 const Output_section_headers
* section_header_
;
630 const Output_section
* shstrtab_
;
634 // Output sections are mainly comprised of input sections. However,
635 // there are cases where we have data to write out which is not in an
636 // input section. Output_section_data is used in such cases. This is
637 // an abstract base class.
639 class Output_section_data
: public Output_data
642 Output_section_data(off_t data_size
, uint64_t addralign
,
643 bool is_data_size_fixed
)
644 : Output_data(), output_section_(NULL
), addralign_(addralign
)
646 this->set_data_size(data_size
);
647 if (is_data_size_fixed
)
648 this->fix_data_size();
651 Output_section_data(uint64_t addralign
)
652 : Output_data(), output_section_(NULL
), addralign_(addralign
)
655 // Return the output section.
656 const Output_section
*
657 output_section() const
658 { return this->output_section_
; }
660 // Record the output section.
662 set_output_section(Output_section
* os
);
664 // Add an input section, for SHF_MERGE sections. This returns true
665 // if the section was handled.
667 add_input_section(Relobj
* object
, unsigned int shndx
)
668 { return this->do_add_input_section(object
, shndx
); }
670 // Given an input OBJECT, an input section index SHNDX within that
671 // object, and an OFFSET relative to the start of that input
672 // section, return whether or not the corresponding offset within
673 // the output section is known. If this function returns true, it
674 // sets *POUTPUT to the output offset. The value -1 indicates that
675 // this input offset is being discarded.
677 output_offset(const Relobj
* object
, unsigned int shndx
,
678 section_offset_type offset
,
679 section_offset_type
*poutput
) const
680 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
682 // Return whether this is the merge section for the input section
683 // SHNDX in OBJECT. This should return true when output_offset
684 // would return true for some values of OFFSET.
686 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
687 { return this->do_is_merge_section_for(object
, shndx
); }
689 // Write the contents to a buffer. This is used for sections which
690 // require postprocessing, such as compression.
692 write_to_buffer(unsigned char* buffer
)
693 { this->do_write_to_buffer(buffer
); }
695 // Print merge stats to stderr. This should only be called for
696 // SHF_MERGE sections.
698 print_merge_stats(const char* section_name
)
699 { this->do_print_merge_stats(section_name
); }
702 // The child class must implement do_write.
704 // The child class may implement specific adjustments to the output
707 do_adjust_output_section(Output_section
*)
710 // May be implemented by child class. Return true if the section
713 do_add_input_section(Relobj
*, unsigned int)
714 { gold_unreachable(); }
716 // The child class may implement output_offset.
718 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
719 section_offset_type
*) const
722 // The child class may implement is_merge_section_for.
724 do_is_merge_section_for(const Relobj
*, unsigned int) const
727 // The child class may implement write_to_buffer. Most child
728 // classes can not appear in a compressed section, and they do not
731 do_write_to_buffer(unsigned char*)
732 { gold_unreachable(); }
734 // Print merge statistics.
736 do_print_merge_stats(const char*)
737 { gold_unreachable(); }
739 // Return the required alignment.
742 { return this->addralign_
; }
744 // Return the output section.
747 { return this->output_section_
; }
749 // Return the section index of the output section.
751 do_out_shndx() const;
753 // Set the alignment.
755 set_addralign(uint64_t addralign
);
758 // The output section for this section.
759 Output_section
* output_section_
;
760 // The required alignment.
764 // Some Output_section_data classes build up their data step by step,
765 // rather than all at once. This class provides an interface for
768 class Output_section_data_build
: public Output_section_data
771 Output_section_data_build(uint64_t addralign
)
772 : Output_section_data(addralign
)
775 // Get the current data size.
777 current_data_size() const
778 { return this->current_data_size_for_child(); }
780 // Set the current data size.
782 set_current_data_size(off_t data_size
)
783 { this->set_current_data_size_for_child(data_size
); }
786 // Set the final data size.
788 set_final_data_size()
789 { this->set_data_size(this->current_data_size_for_child()); }
792 // A simple case of Output_data in which we have constant data to
795 class Output_data_const
: public Output_section_data
798 Output_data_const(const std::string
& data
, uint64_t addralign
)
799 : Output_section_data(data
.size(), addralign
, true), data_(data
)
802 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
803 : Output_section_data(len
, addralign
, true), data_(p
, len
)
806 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
807 : Output_section_data(len
, addralign
, true),
808 data_(reinterpret_cast<const char*>(p
), len
)
812 // Write the data to the output file.
814 do_write(Output_file
*);
816 // Write the data to a buffer.
818 do_write_to_buffer(unsigned char* buffer
)
819 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
821 // Write to a map file.
823 do_print_to_mapfile(Mapfile
* mapfile
) const
824 { mapfile
->print_output_data(this, _("** fill")); }
830 // Another version of Output_data with constant data, in which the
831 // buffer is allocated by the caller.
833 class Output_data_const_buffer
: public Output_section_data
836 Output_data_const_buffer(const unsigned char* p
, off_t len
,
837 uint64_t addralign
, const char* map_name
)
838 : Output_section_data(len
, addralign
, true),
839 p_(p
), map_name_(map_name
)
843 // Write the data the output file.
845 do_write(Output_file
*);
847 // Write the data to a buffer.
849 do_write_to_buffer(unsigned char* buffer
)
850 { memcpy(buffer
, this->p_
, this->data_size()); }
852 // Write to a map file.
854 do_print_to_mapfile(Mapfile
* mapfile
) const
855 { mapfile
->print_output_data(this, _(this->map_name_
)); }
858 // The data to output.
859 const unsigned char* p_
;
860 // Name to use in a map file. Maps are a rarely used feature, but
861 // the space usage is minor as aren't very many of these objects.
862 const char* map_name_
;
865 // A place holder for a fixed amount of data written out via some
868 class Output_data_fixed_space
: public Output_section_data
871 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
872 const char* map_name
)
873 : Output_section_data(data_size
, addralign
, true),
878 // Write out the data--the actual data must be written out
881 do_write(Output_file
*)
884 // Write to a map file.
886 do_print_to_mapfile(Mapfile
* mapfile
) const
887 { mapfile
->print_output_data(this, _(this->map_name_
)); }
890 // Name to use in a map file. Maps are a rarely used feature, but
891 // the space usage is minor as aren't very many of these objects.
892 const char* map_name_
;
895 // A place holder for variable sized data written out via some other
898 class Output_data_space
: public Output_section_data_build
901 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
902 : Output_section_data_build(addralign
),
906 // Set the alignment.
908 set_space_alignment(uint64_t align
)
909 { this->set_addralign(align
); }
912 // Write out the data--the actual data must be written out
915 do_write(Output_file
*)
918 // Write to a map file.
920 do_print_to_mapfile(Mapfile
* mapfile
) const
921 { mapfile
->print_output_data(this, _(this->map_name_
)); }
924 // Name to use in a map file. Maps are a rarely used feature, but
925 // the space usage is minor as aren't very many of these objects.
926 const char* map_name_
;
929 // Fill fixed space with zeroes. This is just like
930 // Output_data_fixed_space, except that the map name is known.
932 class Output_data_zero_fill
: public Output_section_data
935 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
936 : Output_section_data(data_size
, addralign
, true)
940 // There is no data to write out.
942 do_write(Output_file
*)
945 // Write to a map file.
947 do_print_to_mapfile(Mapfile
* mapfile
) const
948 { mapfile
->print_output_data(this, "** zero fill"); }
951 // A string table which goes into an output section.
953 class Output_data_strtab
: public Output_section_data
956 Output_data_strtab(Stringpool
* strtab
)
957 : Output_section_data(1), strtab_(strtab
)
961 // This is called to set the address and file offset. Here we make
962 // sure that the Stringpool is finalized.
964 set_final_data_size();
966 // Write out the data.
968 do_write(Output_file
*);
970 // Write the data to a buffer.
972 do_write_to_buffer(unsigned char* buffer
)
973 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
975 // Write to a map file.
977 do_print_to_mapfile(Mapfile
* mapfile
) const
978 { mapfile
->print_output_data(this, _("** string table")); }
984 // This POD class is used to represent a single reloc in the output
985 // file. This could be a private class within Output_data_reloc, but
986 // the templatization is complex enough that I broke it out into a
987 // separate class. The class is templatized on either elfcpp::SHT_REL
988 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
989 // relocation or an ordinary relocation.
991 // A relocation can be against a global symbol, a local symbol, a
992 // local section symbol, an output section, or the undefined symbol at
993 // index 0. We represent the latter by using a NULL global symbol.
995 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
998 template<bool dynamic
, int size
, bool big_endian
>
999 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1002 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1003 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1005 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1007 // An uninitialized entry. We need this because we want to put
1008 // instances of this class into an STL container.
1010 : local_sym_index_(INVALID_CODE
)
1013 // We have a bunch of different constructors. They come in pairs
1014 // depending on how the address of the relocation is specified. It
1015 // can either be an offset in an Output_data or an offset in an
1018 // A reloc against a global symbol.
1020 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1021 Address address
, bool is_relative
);
1023 Output_reloc(Symbol
* gsym
, unsigned int type
,
1024 Sized_relobj
<size
, big_endian
>* relobj
,
1025 unsigned int shndx
, Address address
, bool is_relative
);
1027 // A reloc against a local symbol or local section symbol.
1029 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1030 unsigned int local_sym_index
, unsigned int type
,
1031 Output_data
* od
, Address address
, bool is_relative
,
1032 bool is_section_symbol
);
1034 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1035 unsigned int local_sym_index
, unsigned int type
,
1036 unsigned int shndx
, Address address
, bool is_relative
,
1037 bool is_section_symbol
);
1039 // A reloc against the STT_SECTION symbol of an output section.
1041 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1044 Output_reloc(Output_section
* os
, unsigned int type
,
1045 Sized_relobj
<size
, big_endian
>* relobj
,
1046 unsigned int shndx
, Address address
);
1048 // Return TRUE if this is a RELATIVE relocation.
1051 { return this->is_relative_
; }
1053 // Return whether this is against a local section symbol.
1055 is_local_section_symbol() const
1057 return (this->local_sym_index_
!= GSYM_CODE
1058 && this->local_sym_index_
!= SECTION_CODE
1059 && this->local_sym_index_
!= INVALID_CODE
1060 && this->is_section_symbol_
);
1063 // For a local section symbol, return the offset of the input
1064 // section within the output section. ADDEND is the addend being
1065 // applied to the input section.
1067 local_section_offset(Addend addend
) const;
1069 // Get the value of the symbol referred to by a Rel relocation when
1070 // we are adding the given ADDEND.
1072 symbol_value(Addend addend
) const;
1074 // Write the reloc entry to an output view.
1076 write(unsigned char* pov
) const;
1078 // Write the offset and info fields to Write_rel.
1079 template<typename Write_rel
>
1080 void write_rel(Write_rel
*) const;
1082 // This is used when sorting dynamic relocs. Return -1 to sort this
1083 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1085 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1088 // Return whether this reloc should be sorted before the argument
1089 // when sorting dynamic relocs.
1091 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1093 { return this->compare(r2
) < 0; }
1096 // Record that we need a dynamic symbol index.
1098 set_needs_dynsym_index();
1100 // Return the symbol index.
1102 get_symbol_index() const;
1104 // Return the output address.
1106 get_address() const;
1108 // Codes for local_sym_index_.
1115 // Invalid uninitialized entry.
1121 // For a local symbol or local section symbol
1122 // (this->local_sym_index_ >= 0), the object. We will never
1123 // generate a relocation against a local symbol in a dynamic
1124 // object; that doesn't make sense. And our callers will always
1125 // be templatized, so we use Sized_relobj here.
1126 Sized_relobj
<size
, big_endian
>* relobj
;
1127 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1128 // symbol. If this is NULL, it indicates a relocation against the
1129 // undefined 0 symbol.
1131 // For a relocation against an output section
1132 // (this->local_sym_index_ == SECTION_CODE), the output section.
1137 // If this->shndx_ is not INVALID CODE, the object which holds the
1138 // input section being used to specify the reloc address.
1139 Sized_relobj
<size
, big_endian
>* relobj
;
1140 // If this->shndx_ is INVALID_CODE, the output data being used to
1141 // specify the reloc address. This may be NULL if the reloc
1142 // address is absolute.
1145 // The address offset within the input section or the Output_data.
1147 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1148 // relocation against an output section, or INVALID_CODE for an
1149 // uninitialized value. Otherwise, for a local symbol
1150 // (this->is_section_symbol_ is false), the local symbol index. For
1151 // a local section symbol (this->is_section_symbol_ is true), the
1152 // section index in the input file.
1153 unsigned int local_sym_index_
;
1154 // The reloc type--a processor specific code.
1155 unsigned int type_
: 30;
1156 // True if the relocation is a RELATIVE relocation.
1157 bool is_relative_
: 1;
1158 // True if the relocation is against a section symbol.
1159 bool is_section_symbol_
: 1;
1160 // If the reloc address is an input section in an object, the
1161 // section index. This is INVALID_CODE if the reloc address is
1162 // specified in some other way.
1163 unsigned int shndx_
;
1166 // The SHT_RELA version of Output_reloc<>. This is just derived from
1167 // the SHT_REL version of Output_reloc, but it adds an addend.
1169 template<bool dynamic
, int size
, bool big_endian
>
1170 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1173 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1174 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1176 // An uninitialized entry.
1181 // A reloc against a global symbol.
1183 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1184 Address address
, Addend addend
, bool is_relative
)
1185 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
1188 Output_reloc(Symbol
* gsym
, unsigned int type
,
1189 Sized_relobj
<size
, big_endian
>* relobj
,
1190 unsigned int shndx
, Address address
, Addend addend
,
1192 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1195 // A reloc against a local symbol.
1197 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1198 unsigned int local_sym_index
, unsigned int type
,
1199 Output_data
* od
, Address address
,
1200 Addend addend
, bool is_relative
, bool is_section_symbol
)
1201 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1206 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1207 unsigned int local_sym_index
, unsigned int type
,
1208 unsigned int shndx
, Address address
,
1209 Addend addend
, bool is_relative
, bool is_section_symbol
)
1210 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1215 // A reloc against the STT_SECTION symbol of an output section.
1217 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1218 Address address
, Addend addend
)
1219 : rel_(os
, type
, od
, address
), addend_(addend
)
1222 Output_reloc(Output_section
* os
, unsigned int type
,
1223 Sized_relobj
<size
, big_endian
>* relobj
,
1224 unsigned int shndx
, Address address
, Addend addend
)
1225 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1228 // Write the reloc entry to an output view.
1230 write(unsigned char* pov
) const;
1232 // Return whether this reloc should be sorted before the argument
1233 // when sorting dynamic relocs.
1235 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1238 int i
= this->rel_
.compare(r2
.rel_
);
1244 return this->addend_
< r2
.addend_
;
1249 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1254 // Output_data_reloc is used to manage a section containing relocs.
1255 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1256 // indicates whether this is a dynamic relocation or a normal
1257 // relocation. Output_data_reloc_base is a base class.
1258 // Output_data_reloc is the real class, which we specialize based on
1261 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1262 class Output_data_reloc_base
: public Output_section_data_build
1265 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1266 typedef typename
Output_reloc_type::Address Address
;
1267 static const int reloc_size
=
1268 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1270 // Construct the section.
1271 Output_data_reloc_base(bool sort_relocs
)
1272 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1273 sort_relocs_(sort_relocs
)
1277 // Write out the data.
1279 do_write(Output_file
*);
1281 // Set the entry size and the link.
1283 do_adjust_output_section(Output_section
*os
);
1285 // Write to a map file.
1287 do_print_to_mapfile(Mapfile
* mapfile
) const
1289 mapfile
->print_output_data(this,
1291 ? _("** dynamic relocs")
1295 // Add a relocation entry.
1297 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1299 this->relocs_
.push_back(reloc
);
1300 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1301 od
->add_dynamic_reloc();
1305 typedef std::vector
<Output_reloc_type
> Relocs
;
1307 // The class used to sort the relocations.
1308 struct Sort_relocs_comparison
1311 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1312 { return r1
.sort_before(r2
); }
1315 // The relocations in this section.
1317 // Whether to sort the relocations when writing them out, to make
1318 // the dynamic linker more efficient.
1322 // The class which callers actually create.
1324 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1325 class Output_data_reloc
;
1327 // The SHT_REL version of Output_data_reloc.
1329 template<bool dynamic
, int size
, bool big_endian
>
1330 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1331 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1334 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1338 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1339 typedef typename
Output_reloc_type::Address Address
;
1341 Output_data_reloc(bool sr
)
1342 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1345 // Add a reloc against a global symbol.
1348 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1349 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1352 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1353 Sized_relobj
<size
, big_endian
>* relobj
,
1354 unsigned int shndx
, Address address
)
1355 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1358 // These are to simplify the Copy_relocs class.
1361 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1364 gold_assert(addend
== 0);
1365 this->add_global(gsym
, type
, od
, address
);
1369 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1370 Sized_relobj
<size
, big_endian
>* relobj
,
1371 unsigned int shndx
, Address address
, Address addend
)
1373 gold_assert(addend
== 0);
1374 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1377 // Add a RELATIVE reloc against a global symbol. The final relocation
1378 // will not reference the symbol.
1381 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1383 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1386 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1387 Sized_relobj
<size
, big_endian
>* relobj
,
1388 unsigned int shndx
, Address address
)
1390 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1394 // Add a reloc against a local symbol.
1397 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1398 unsigned int local_sym_index
, unsigned int type
,
1399 Output_data
* od
, Address address
)
1401 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1402 address
, false, false));
1406 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1407 unsigned int local_sym_index
, unsigned int type
,
1408 Output_data
* od
, unsigned int shndx
, Address address
)
1410 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1411 address
, false, false));
1414 // Add a RELATIVE reloc against a local symbol.
1417 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1418 unsigned int local_sym_index
, unsigned int type
,
1419 Output_data
* od
, Address address
)
1421 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1422 address
, true, false));
1426 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1427 unsigned int local_sym_index
, unsigned int type
,
1428 Output_data
* od
, unsigned int shndx
, Address address
)
1430 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1431 address
, true, false));
1434 // Add a reloc against a local section symbol. This will be
1435 // converted into a reloc against the STT_SECTION symbol of the
1439 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1440 unsigned int input_shndx
, unsigned int type
,
1441 Output_data
* od
, Address address
)
1443 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1444 address
, false, true));
1448 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1449 unsigned int input_shndx
, unsigned int type
,
1450 Output_data
* od
, unsigned int shndx
, Address address
)
1452 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1453 address
, false, true));
1456 // A reloc against the STT_SECTION symbol of an output section.
1457 // OS is the Output_section that the relocation refers to; OD is
1458 // the Output_data object being relocated.
1461 add_output_section(Output_section
* os
, unsigned int type
,
1462 Output_data
* od
, Address address
)
1463 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1466 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1467 Sized_relobj
<size
, big_endian
>* relobj
,
1468 unsigned int shndx
, Address address
)
1469 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1472 // The SHT_RELA version of Output_data_reloc.
1474 template<bool dynamic
, int size
, bool big_endian
>
1475 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1476 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1479 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1483 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1484 typedef typename
Output_reloc_type::Address Address
;
1485 typedef typename
Output_reloc_type::Addend Addend
;
1487 Output_data_reloc(bool sr
)
1488 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1491 // Add a reloc against a global symbol.
1494 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1495 Address address
, Addend addend
)
1496 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1500 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1501 Sized_relobj
<size
, big_endian
>* relobj
,
1502 unsigned int shndx
, Address address
,
1504 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1507 // Add a RELATIVE reloc against a global symbol. The final output
1508 // relocation will not reference the symbol, but we must keep the symbol
1509 // information long enough to set the addend of the relocation correctly
1510 // when it is written.
1513 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1514 Address address
, Addend addend
)
1515 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1518 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1519 Sized_relobj
<size
, big_endian
>* relobj
,
1520 unsigned int shndx
, Address address
, Addend addend
)
1521 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1524 // Add a reloc against a local symbol.
1527 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1528 unsigned int local_sym_index
, unsigned int type
,
1529 Output_data
* od
, Address address
, Addend addend
)
1531 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1532 addend
, false, false));
1536 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1537 unsigned int local_sym_index
, unsigned int type
,
1538 Output_data
* od
, unsigned int shndx
, Address address
,
1541 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1542 address
, addend
, false, false));
1545 // Add a RELATIVE reloc against a local symbol.
1548 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1549 unsigned int local_sym_index
, unsigned int type
,
1550 Output_data
* od
, Address address
, Addend addend
)
1552 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1553 addend
, true, false));
1557 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1558 unsigned int local_sym_index
, unsigned int type
,
1559 Output_data
* od
, unsigned int shndx
, Address address
,
1562 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1563 address
, addend
, 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
, Addend addend
)
1575 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1576 addend
, 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
,
1585 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1586 address
, addend
, false, true));
1589 // A reloc against the STT_SECTION symbol of an output section.
1592 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1593 Address address
, Addend addend
)
1594 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1597 add_output_section(Output_section
* os
, unsigned int type
,
1598 Sized_relobj
<size
, big_endian
>* relobj
,
1599 unsigned int shndx
, Address address
, Addend addend
)
1600 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1604 // Output_relocatable_relocs represents a relocation section in a
1605 // relocatable link. The actual data is written out in the target
1606 // hook relocate_for_relocatable. This just saves space for it.
1608 template<int sh_type
, int size
, bool big_endian
>
1609 class Output_relocatable_relocs
: public Output_section_data
1612 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1613 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1618 set_final_data_size();
1620 // Write out the data. There is nothing to do here.
1622 do_write(Output_file
*)
1625 // Write to a map file.
1627 do_print_to_mapfile(Mapfile
* mapfile
) const
1628 { mapfile
->print_output_data(this, _("** relocs")); }
1631 // The relocs associated with this input section.
1632 Relocatable_relocs
* rr_
;
1635 // Handle a GROUP section.
1637 template<int size
, bool big_endian
>
1638 class Output_data_group
: public Output_section_data
1641 // The constructor clears *INPUT_SHNDXES.
1642 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1643 section_size_type entry_count
,
1644 elfcpp::Elf_Word flags
,
1645 std::vector
<unsigned int>* input_shndxes
);
1648 do_write(Output_file
*);
1650 // Write to a map file.
1652 do_print_to_mapfile(Mapfile
* mapfile
) const
1653 { mapfile
->print_output_data(this, _("** group")); }
1655 // Set final data size.
1657 set_final_data_size()
1658 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1661 // The input object.
1662 Sized_relobj
<size
, big_endian
>* relobj_
;
1663 // The group flag word.
1664 elfcpp::Elf_Word flags_
;
1665 // The section indexes of the input sections in this group.
1666 std::vector
<unsigned int> input_shndxes_
;
1669 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1670 // for one symbol--either a global symbol or a local symbol in an
1671 // object. The target specific code adds entries to the GOT as
1674 template<int size
, bool big_endian
>
1675 class Output_data_got
: public Output_section_data_build
1678 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1679 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1680 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1683 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1687 // Add an entry for a global symbol to the GOT. Return true if this
1688 // is a new GOT entry, false if the symbol was already in the GOT.
1690 add_global(Symbol
* gsym
, unsigned int got_type
);
1692 // Add an entry for a global symbol to the GOT, and add a dynamic
1693 // relocation of type R_TYPE for the GOT entry.
1695 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1696 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1699 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1700 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1702 // Add a pair of entries for a global symbol to the GOT, and add
1703 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1705 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1706 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1707 unsigned int r_type_2
);
1710 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1711 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1712 unsigned int r_type_2
);
1714 // Add an entry for a local symbol to the GOT. This returns true if
1715 // this is a new GOT entry, false if the symbol already has a GOT
1718 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1719 unsigned int got_type
);
1721 // Add an entry for a local symbol to the GOT, and add a dynamic
1722 // relocation of type R_TYPE for the GOT entry.
1724 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1725 unsigned int sym_index
, unsigned int got_type
,
1726 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1729 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1730 unsigned int sym_index
, unsigned int got_type
,
1731 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1733 // Add a pair of entries for a local symbol to the GOT, and add
1734 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1736 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1737 unsigned int sym_index
, unsigned int shndx
,
1738 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1739 unsigned int r_type_1
, unsigned int r_type_2
);
1742 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1743 unsigned int sym_index
, unsigned int shndx
,
1744 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1745 unsigned int r_type_1
, unsigned int r_type_2
);
1747 // Add a constant to the GOT. This returns the offset of the new
1748 // entry from the start of the GOT.
1750 add_constant(Valtype constant
)
1752 this->entries_
.push_back(Got_entry(constant
));
1753 this->set_got_size();
1754 return this->last_got_offset();
1758 // Write out the GOT table.
1760 do_write(Output_file
*);
1762 // Write to a map file.
1764 do_print_to_mapfile(Mapfile
* mapfile
) const
1765 { mapfile
->print_output_data(this, _("** GOT")); }
1768 // This POD class holds a single GOT entry.
1772 // Create a zero entry.
1774 : local_sym_index_(CONSTANT_CODE
)
1775 { this->u_
.constant
= 0; }
1777 // Create a global symbol entry.
1778 explicit Got_entry(Symbol
* gsym
)
1779 : local_sym_index_(GSYM_CODE
)
1780 { this->u_
.gsym
= gsym
; }
1782 // Create a local symbol entry.
1783 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1784 unsigned int local_sym_index
)
1785 : local_sym_index_(local_sym_index
)
1787 gold_assert(local_sym_index
!= GSYM_CODE
1788 && local_sym_index
!= CONSTANT_CODE
);
1789 this->u_
.object
= object
;
1792 // Create a constant entry. The constant is a host value--it will
1793 // be swapped, if necessary, when it is written out.
1794 explicit Got_entry(Valtype constant
)
1795 : local_sym_index_(CONSTANT_CODE
)
1796 { this->u_
.constant
= constant
; }
1798 // Write the GOT entry to an output view.
1800 write(unsigned char* pov
) const;
1811 // For a local symbol, the object.
1812 Sized_relobj
<size
, big_endian
>* object
;
1813 // For a global symbol, the symbol.
1815 // For a constant, the constant.
1818 // For a local symbol, the local symbol index. This is GSYM_CODE
1819 // for a global symbol, or CONSTANT_CODE for a constant.
1820 unsigned int local_sym_index_
;
1823 typedef std::vector
<Got_entry
> Got_entries
;
1825 // Return the offset into the GOT of GOT entry I.
1827 got_offset(unsigned int i
) const
1828 { return i
* (size
/ 8); }
1830 // Return the offset into the GOT of the last entry added.
1832 last_got_offset() const
1833 { return this->got_offset(this->entries_
.size() - 1); }
1835 // Set the size of the section.
1838 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1840 // The list of GOT entries.
1841 Got_entries entries_
;
1844 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1847 class Output_data_dynamic
: public Output_section_data
1850 Output_data_dynamic(Stringpool
* pool
)
1851 : Output_section_data(Output_data::default_alignment()),
1852 entries_(), pool_(pool
)
1855 // Add a new dynamic entry with a fixed numeric value.
1857 add_constant(elfcpp::DT tag
, unsigned int val
)
1858 { this->add_entry(Dynamic_entry(tag
, val
)); }
1860 // Add a new dynamic entry with the address of output data.
1862 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1863 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1865 // Add a new dynamic entry with the address of output data
1866 // plus a constant offset.
1868 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
1869 unsigned int offset
)
1870 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
1872 // Add a new dynamic entry with the size of output data.
1874 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1875 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1877 // Add a new dynamic entry with the address of a symbol.
1879 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1880 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1882 // Add a new dynamic entry with a string.
1884 add_string(elfcpp::DT tag
, const char* str
)
1885 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1888 add_string(elfcpp::DT tag
, const std::string
& str
)
1889 { this->add_string(tag
, str
.c_str()); }
1892 // Adjust the output section to set the entry size.
1894 do_adjust_output_section(Output_section
*);
1896 // Set the final data size.
1898 set_final_data_size();
1900 // Write out the dynamic entries.
1902 do_write(Output_file
*);
1904 // Write to a map file.
1906 do_print_to_mapfile(Mapfile
* mapfile
) const
1907 { mapfile
->print_output_data(this, _("** dynamic")); }
1910 // This POD class holds a single dynamic entry.
1914 // Create an entry with a fixed numeric value.
1915 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1916 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
1917 { this->u_
.val
= val
; }
1919 // Create an entry with the size or address of a section.
1920 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1922 offset_(section_size
1923 ? DYNAMIC_SECTION_SIZE
1924 : DYNAMIC_SECTION_ADDRESS
)
1925 { this->u_
.od
= od
; }
1927 // Create an entry with the address of a section plus a constant offset.
1928 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
1931 { this->u_
.od
= od
; }
1933 // Create an entry with the address of a symbol.
1934 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1935 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
1936 { this->u_
.sym
= sym
; }
1938 // Create an entry with a string.
1939 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1940 : tag_(tag
), offset_(DYNAMIC_STRING
)
1941 { this->u_
.str
= str
; }
1943 // Return the tag of this entry.
1946 { return this->tag_
; }
1948 // Write the dynamic entry to an output view.
1949 template<int size
, bool big_endian
>
1951 write(unsigned char* pov
, const Stringpool
*) const;
1954 // Classification is encoded in the OFFSET field.
1958 DYNAMIC_SECTION_ADDRESS
= 0,
1960 DYNAMIC_NUMBER
= -1U,
1962 DYNAMIC_SECTION_SIZE
= -2U,
1964 DYNAMIC_SYMBOL
= -3U,
1966 DYNAMIC_STRING
= -4U
1967 // Any other value indicates a section address plus OFFSET.
1972 // For DYNAMIC_NUMBER.
1974 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1975 const Output_data
* od
;
1976 // For DYNAMIC_SYMBOL.
1978 // For DYNAMIC_STRING.
1983 // The type of entry (Classification) or offset within a section.
1984 unsigned int offset_
;
1987 // Add an entry to the list.
1989 add_entry(const Dynamic_entry
& entry
)
1990 { this->entries_
.push_back(entry
); }
1992 // Sized version of write function.
1993 template<int size
, bool big_endian
>
1995 sized_write(Output_file
* of
);
1997 // The type of the list of entries.
1998 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2001 Dynamic_entries entries_
;
2002 // The pool used for strings.
2006 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2007 // which may be required if the object file has more than
2008 // SHN_LORESERVE sections.
2010 class Output_symtab_xindex
: public Output_section_data
2013 Output_symtab_xindex(size_t symcount
)
2014 : Output_section_data(symcount
* 4, 4, true),
2018 // Add an entry: symbol number SYMNDX has section SHNDX.
2020 add(unsigned int symndx
, unsigned int shndx
)
2021 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2025 do_write(Output_file
*);
2027 // Write to a map file.
2029 do_print_to_mapfile(Mapfile
* mapfile
) const
2030 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2033 template<bool big_endian
>
2035 endian_do_write(unsigned char*);
2037 // It is likely that most symbols will not require entries. Rather
2038 // than keep a vector for all symbols, we keep pairs of symbol index
2039 // and section index.
2040 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2042 // The entries we need.
2043 Xindex_entries entries_
;
2046 // A relaxed input section.
2047 class Output_relaxed_input_section
: public Output_section_data_build
2050 // We would like to call relobj->section_addralign(shndx) to get the
2051 // alignment but we do not want the constructor to fail. So callers
2052 // are repsonsible for ensuring that.
2053 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2055 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2058 // Return the Relobj of this relaxed input section.
2061 { return this->relobj_
; }
2063 // Return the section index of this relaxed input section.
2066 { return this->shndx_
; }
2070 unsigned int shndx_
;
2073 // An output section. We don't expect to have too many output
2074 // sections, so we don't bother to do a template on the size.
2076 class Output_section
: public Output_data
2079 // Create an output section, giving the name, type, and flags.
2080 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2081 virtual ~Output_section();
2083 // Add a new input section SHNDX, named NAME, with header SHDR, from
2084 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2085 // which applies to this section, or 0 if none, or -1 if more than
2086 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2087 // in a linker script; in that case we need to keep track of input
2088 // sections associated with an output section. Return the offset
2089 // within the output section.
2090 template<int size
, bool big_endian
>
2092 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
2094 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2095 unsigned int reloc_shndx
, bool have_sections_script
);
2097 // Add generated data POSD to this output section.
2099 add_output_section_data(Output_section_data
* posd
);
2101 // Add a relaxed input section PORIS to this output section.
2103 add_relaxed_input_section(Output_relaxed_input_section
* poris
);
2105 // Return the section name.
2108 { return this->name_
; }
2110 // Return the section type.
2113 { return this->type_
; }
2115 // Return the section flags.
2118 { return this->flags_
; }
2120 // Update the output section flags based on input section flags.
2122 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2124 // Return the entsize field.
2127 { return this->entsize_
; }
2129 // Set the entsize field.
2131 set_entsize(uint64_t v
);
2133 // Set the load address.
2135 set_load_address(uint64_t load_address
)
2137 this->load_address_
= load_address
;
2138 this->has_load_address_
= true;
2141 // Set the link field to the output section index of a section.
2143 set_link_section(const Output_data
* od
)
2145 gold_assert(this->link_
== 0
2146 && !this->should_link_to_symtab_
2147 && !this->should_link_to_dynsym_
);
2148 this->link_section_
= od
;
2151 // Set the link field to a constant.
2153 set_link(unsigned int v
)
2155 gold_assert(this->link_section_
== NULL
2156 && !this->should_link_to_symtab_
2157 && !this->should_link_to_dynsym_
);
2161 // Record that this section should link to the normal symbol table.
2163 set_should_link_to_symtab()
2165 gold_assert(this->link_section_
== NULL
2167 && !this->should_link_to_dynsym_
);
2168 this->should_link_to_symtab_
= true;
2171 // Record that this section should link to the dynamic symbol table.
2173 set_should_link_to_dynsym()
2175 gold_assert(this->link_section_
== NULL
2177 && !this->should_link_to_symtab_
);
2178 this->should_link_to_dynsym_
= true;
2181 // Return the info field.
2185 gold_assert(this->info_section_
== NULL
2186 && this->info_symndx_
== NULL
);
2190 // Set the info field to the output section index of a section.
2192 set_info_section(const Output_section
* os
)
2194 gold_assert((this->info_section_
== NULL
2195 || (this->info_section_
== os
2196 && this->info_uses_section_index_
))
2197 && this->info_symndx_
== NULL
2198 && this->info_
== 0);
2199 this->info_section_
= os
;
2200 this->info_uses_section_index_
= true;
2203 // Set the info field to the symbol table index of a symbol.
2205 set_info_symndx(const Symbol
* sym
)
2207 gold_assert(this->info_section_
== NULL
2208 && (this->info_symndx_
== NULL
2209 || this->info_symndx_
== sym
)
2210 && this->info_
== 0);
2211 this->info_symndx_
= sym
;
2214 // Set the info field to the symbol table index of a section symbol.
2216 set_info_section_symndx(const Output_section
* os
)
2218 gold_assert((this->info_section_
== NULL
2219 || (this->info_section_
== os
2220 && !this->info_uses_section_index_
))
2221 && this->info_symndx_
== NULL
2222 && this->info_
== 0);
2223 this->info_section_
= os
;
2224 this->info_uses_section_index_
= false;
2227 // Set the info field to a constant.
2229 set_info(unsigned int v
)
2231 gold_assert(this->info_section_
== NULL
2232 && this->info_symndx_
== NULL
2233 && (this->info_
== 0
2234 || this->info_
== v
));
2238 // Set the addralign field.
2240 set_addralign(uint64_t v
)
2241 { this->addralign_
= v
; }
2243 // Whether the output section index has been set.
2245 has_out_shndx() const
2246 { return this->out_shndx_
!= -1U; }
2248 // Indicate that we need a symtab index.
2250 set_needs_symtab_index()
2251 { this->needs_symtab_index_
= true; }
2253 // Return whether we need a symtab index.
2255 needs_symtab_index() const
2256 { return this->needs_symtab_index_
; }
2258 // Get the symtab index.
2260 symtab_index() const
2262 gold_assert(this->symtab_index_
!= 0);
2263 return this->symtab_index_
;
2266 // Set the symtab index.
2268 set_symtab_index(unsigned int index
)
2270 gold_assert(index
!= 0);
2271 this->symtab_index_
= index
;
2274 // Indicate that we need a dynsym index.
2276 set_needs_dynsym_index()
2277 { this->needs_dynsym_index_
= true; }
2279 // Return whether we need a dynsym index.
2281 needs_dynsym_index() const
2282 { return this->needs_dynsym_index_
; }
2284 // Get the dynsym index.
2286 dynsym_index() const
2288 gold_assert(this->dynsym_index_
!= 0);
2289 return this->dynsym_index_
;
2292 // Set the dynsym index.
2294 set_dynsym_index(unsigned int index
)
2296 gold_assert(index
!= 0);
2297 this->dynsym_index_
= index
;
2300 // Return whether the input sections sections attachd to this output
2301 // section may require sorting. This is used to handle constructor
2302 // priorities compatibly with GNU ld.
2304 may_sort_attached_input_sections() const
2305 { return this->may_sort_attached_input_sections_
; }
2307 // Record that the input sections attached to this output section
2308 // may require sorting.
2310 set_may_sort_attached_input_sections()
2311 { this->may_sort_attached_input_sections_
= true; }
2313 // Return whether the input sections attached to this output section
2314 // require sorting. This is used to handle constructor priorities
2315 // compatibly with GNU ld.
2317 must_sort_attached_input_sections() const
2318 { return this->must_sort_attached_input_sections_
; }
2320 // Record that the input sections attached to this output section
2323 set_must_sort_attached_input_sections()
2324 { this->must_sort_attached_input_sections_
= true; }
2326 // Return whether this section holds relro data--data which has
2327 // dynamic relocations but which may be marked read-only after the
2328 // dynamic relocations have been completed.
2331 { return this->is_relro_
; }
2333 // Record that this section holds relro data.
2336 { this->is_relro_
= true; }
2338 // Record that this section does not hold relro data.
2341 { this->is_relro_
= false; }
2343 // True if this section holds relro local data--relro data for which
2344 // the dynamic relocations are all RELATIVE relocations.
2346 is_relro_local() const
2347 { return this->is_relro_local_
; }
2349 // Record that this section holds relro local data.
2351 set_is_relro_local()
2352 { this->is_relro_local_
= true; }
2354 // True if this must be the last relro section.
2356 is_last_relro() const
2357 { return this->is_last_relro_
; }
2359 // Record that this must be the last relro section.
2363 gold_assert(this->is_relro_
);
2364 this->is_last_relro_
= true;
2367 // True if this must be the first section following the relro sections.
2369 is_first_non_relro() const
2371 gold_assert(!this->is_relro_
);
2372 return this->is_first_non_relro_
;
2375 // Record that this must be the first non-relro section.
2377 set_is_first_non_relro()
2379 gold_assert(!this->is_relro_
);
2380 this->is_first_non_relro_
= true;
2383 // True if this is a small section: a section which holds small
2386 is_small_section() const
2387 { return this->is_small_section_
; }
2389 // Record that this is a small section.
2391 set_is_small_section()
2392 { this->is_small_section_
= true; }
2394 // True if this is a large section: a section which holds large
2397 is_large_section() const
2398 { return this->is_large_section_
; }
2400 // Record that this is a large section.
2402 set_is_large_section()
2403 { this->is_large_section_
= true; }
2405 // True if this is a large data (not BSS) section.
2407 is_large_data_section()
2408 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2410 // True if this is the .interp section which goes into the PT_INTERP
2414 { return this->is_interp_
; }
2416 // Record that this is the interp section.
2419 { this->is_interp_
= true; }
2421 // True if this is a section used by the dynamic linker.
2423 is_dynamic_linker_section() const
2424 { return this->is_dynamic_linker_section_
; }
2426 // Record that this is a section used by the dynamic linker.
2428 set_is_dynamic_linker_section()
2429 { this->is_dynamic_linker_section_
= true; }
2431 // Return whether this section should be written after all the input
2432 // sections are complete.
2434 after_input_sections() const
2435 { return this->after_input_sections_
; }
2437 // Record that this section should be written after all the input
2438 // sections are complete.
2440 set_after_input_sections()
2441 { this->after_input_sections_
= true; }
2443 // Return whether this section requires postprocessing after all
2444 // relocations have been applied.
2446 requires_postprocessing() const
2447 { return this->requires_postprocessing_
; }
2449 // If a section requires postprocessing, return the buffer to use.
2451 postprocessing_buffer() const
2453 gold_assert(this->postprocessing_buffer_
!= NULL
);
2454 return this->postprocessing_buffer_
;
2457 // If a section requires postprocessing, create the buffer to use.
2459 create_postprocessing_buffer();
2461 // If a section requires postprocessing, this is the size of the
2462 // buffer to which relocations should be applied.
2464 postprocessing_buffer_size() const
2465 { return this->current_data_size_for_child(); }
2467 // Modify the section name. This is only permitted for an
2468 // unallocated section, and only before the size has been finalized.
2469 // Otherwise the name will not get into Layout::namepool_.
2471 set_name(const char* newname
)
2473 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2474 gold_assert(!this->is_data_size_valid());
2475 this->name_
= newname
;
2478 // Return whether the offset OFFSET in the input section SHNDX in
2479 // object OBJECT is being included in the link.
2481 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2482 off_t offset
) const;
2484 // Return the offset within the output section of OFFSET relative to
2485 // the start of input section SHNDX in object OBJECT.
2487 output_offset(const Relobj
* object
, unsigned int shndx
,
2488 section_offset_type offset
) const;
2490 // Return the output virtual address of OFFSET relative to the start
2491 // of input section SHNDX in object OBJECT.
2493 output_address(const Relobj
* object
, unsigned int shndx
,
2494 off_t offset
) const;
2496 // Look for the merged section for input section SHNDX in object
2497 // OBJECT. If found, return true, and set *ADDR to the address of
2498 // the start of the merged section. This is not necessary the
2499 // output offset corresponding to input offset 0 in the section,
2500 // since the section may be mapped arbitrarily.
2502 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2503 uint64_t* addr
) const;
2505 // Record that this output section was found in the SECTIONS clause
2506 // of a linker script.
2508 set_found_in_sections_clause()
2509 { this->found_in_sections_clause_
= true; }
2511 // Return whether this output section was found in the SECTIONS
2512 // clause of a linker script.
2514 found_in_sections_clause() const
2515 { return this->found_in_sections_clause_
; }
2517 // Write the section header into *OPHDR.
2518 template<int size
, bool big_endian
>
2520 write_header(const Layout
*, const Stringpool
*,
2521 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2523 // The next few calls are for linker script support.
2525 // We need to export the input sections to linker scripts. Previously
2526 // we export a pair of Relobj pointer and section index. We now need to
2527 // handle relaxed input sections as well. So we use this class.
2528 class Simple_input_section
2531 static const unsigned int invalid_shndx
= static_cast<unsigned int>(-1);
2534 Simple_input_section(Relobj
*relobj
, unsigned int shndx
)
2537 gold_assert(shndx
!= invalid_shndx
);
2538 this->u_
.relobj
= relobj
;
2541 Simple_input_section(Output_relaxed_input_section
* section
)
2542 : shndx_(invalid_shndx
)
2543 { this->u_
.relaxed_input_section
= section
; }
2545 // Whether this is a relaxed section.
2547 is_relaxed_input_section() const
2548 { return this->shndx_
== invalid_shndx
; }
2550 // Return object of an input section.
2554 return ((this->shndx_
!= invalid_shndx
)
2556 : this->u_
.relaxed_input_section
->relobj());
2559 // Return index of an input section.
2563 return ((this->shndx_
!= invalid_shndx
)
2565 : this->u_
.relaxed_input_section
->shndx());
2568 // Return the Output_relaxed_input_section object of a relaxed section.
2569 Output_relaxed_input_section
*
2570 relaxed_input_section() const
2572 gold_assert(this->shndx_
== invalid_shndx
);
2573 return this->u_
.relaxed_input_section
;
2577 // Pointer to either an Relobj or an Output_relaxed_input_section.
2581 Output_relaxed_input_section
* relaxed_input_section
;
2583 // Section index for an non-relaxed section or invalid_shndx for
2584 // a relaxed section.
2585 unsigned int shndx_
;
2588 // Store the list of input sections for this Output_section into the
2589 // list passed in. This removes the input sections, leaving only
2590 // any Output_section_data elements. This returns the size of those
2591 // Output_section_data elements. ADDRESS is the address of this
2592 // output section. FILL is the fill value to use, in case there are
2593 // any spaces between the remaining Output_section_data elements.
2595 get_input_sections(uint64_t address
, const std::string
& fill
,
2596 std::list
<Simple_input_section
>*);
2598 // Add an input section from a script.
2600 add_input_section_for_script(const Simple_input_section
& input_section
,
2601 off_t data_size
, uint64_t addralign
);
2603 // Set the current size of the output section.
2605 set_current_data_size(off_t size
)
2606 { this->set_current_data_size_for_child(size
); }
2608 // Get the current size of the output section.
2610 current_data_size() const
2611 { return this->current_data_size_for_child(); }
2613 // End of linker script support.
2615 // Save states before doing section layout.
2616 // This is used for relaxation.
2620 // Restore states prior to section layout.
2624 // Convert existing input sections to relaxed input sections.
2626 convert_input_sections_to_relaxed_sections(
2627 const std::vector
<Output_relaxed_input_section
*>& sections
);
2629 // Find a relaxed input section to an input section in OBJECT
2630 // with index SHNDX. Return NULL if none is found.
2631 const Output_relaxed_input_section
*
2632 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
2634 // Print merge statistics to stderr.
2636 print_merge_stats();
2639 // Return the output section--i.e., the object itself.
2644 // Return the section index in the output file.
2646 do_out_shndx() const
2648 gold_assert(this->out_shndx_
!= -1U);
2649 return this->out_shndx_
;
2652 // Set the output section index.
2654 do_set_out_shndx(unsigned int shndx
)
2656 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2657 this->out_shndx_
= shndx
;
2660 // Set the final data size of the Output_section. For a typical
2661 // Output_section, there is nothing to do, but if there are any
2662 // Output_section_data objects we need to set their final addresses
2665 set_final_data_size();
2667 // Reset the address and file offset.
2669 do_reset_address_and_file_offset();
2671 // Return true if address and file offset already have reset values. In
2672 // other words, calling reset_address_and_file_offset will not change them.
2674 do_address_and_file_offset_have_reset_values() const;
2676 // Write the data to the file. For a typical Output_section, this
2677 // does nothing: the data is written out by calling Object::Relocate
2678 // on each input object. But if there are any Output_section_data
2679 // objects we do need to write them out here.
2681 do_write(Output_file
*);
2683 // Return the address alignment--function required by parent class.
2685 do_addralign() const
2686 { return this->addralign_
; }
2688 // Return whether there is a load address.
2690 do_has_load_address() const
2691 { return this->has_load_address_
; }
2693 // Return the load address.
2695 do_load_address() const
2697 gold_assert(this->has_load_address_
);
2698 return this->load_address_
;
2701 // Return whether this is an Output_section.
2703 do_is_section() const
2706 // Return whether this is a section of the specified type.
2708 do_is_section_type(elfcpp::Elf_Word type
) const
2709 { return this->type_
== type
; }
2711 // Return whether the specified section flag is set.
2713 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2714 { return (this->flags_
& flag
) != 0; }
2716 // Set the TLS offset. Called only for SHT_TLS sections.
2718 do_set_tls_offset(uint64_t tls_base
);
2720 // Return the TLS offset, relative to the base of the TLS segment.
2721 // Valid only for SHT_TLS sections.
2723 do_tls_offset() const
2724 { return this->tls_offset_
; }
2726 // This may be implemented by a child class.
2728 do_finalize_name(Layout
*)
2731 // Print to the map file.
2733 do_print_to_mapfile(Mapfile
*) const;
2735 // Record that this section requires postprocessing after all
2736 // relocations have been applied. This is called by a child class.
2738 set_requires_postprocessing()
2740 this->requires_postprocessing_
= true;
2741 this->after_input_sections_
= true;
2744 // Write all the data of an Output_section into the postprocessing
2747 write_to_postprocessing_buffer();
2749 // In some cases we need to keep a list of the input sections
2750 // associated with this output section. We only need the list if we
2751 // might have to change the offsets of the input section within the
2752 // output section after we add the input section. The ordinary
2753 // input sections will be written out when we process the object
2754 // file, and as such we don't need to track them here. We do need
2755 // to track Output_section_data objects here. We store instances of
2756 // this structure in a std::vector, so it must be a POD. There can
2757 // be many instances of this structure, so we use a union to save
2763 : shndx_(0), p2align_(0)
2765 this->u1_
.data_size
= 0;
2766 this->u2_
.object
= NULL
;
2769 // For an ordinary input section.
2770 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2773 p2align_(ffsll(static_cast<long long>(addralign
)))
2775 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2776 && shndx
!= MERGE_DATA_SECTION_CODE
2777 && shndx
!= MERGE_STRING_SECTION_CODE
2778 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
2779 this->u1_
.data_size
= data_size
;
2780 this->u2_
.object
= object
;
2783 // For a non-merge output section.
2784 Input_section(Output_section_data
* posd
)
2785 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0)
2787 this->u1_
.data_size
= 0;
2788 this->u2_
.posd
= posd
;
2791 // For a merge section.
2792 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2794 ? MERGE_STRING_SECTION_CODE
2795 : MERGE_DATA_SECTION_CODE
),
2798 this->u1_
.entsize
= entsize
;
2799 this->u2_
.posd
= posd
;
2802 // For a relaxed input section.
2803 Input_section(Output_relaxed_input_section
*psection
)
2804 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0)
2806 this->u1_
.data_size
= 0;
2807 this->u2_
.poris
= psection
;
2810 // The required alignment.
2814 if (!this->is_input_section())
2815 return this->u2_
.posd
->addralign();
2816 return (this->p2align_
== 0
2818 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2821 // Return the required size.
2825 // Whether this is an input section.
2827 is_input_section() const
2829 return (this->shndx_
!= OUTPUT_SECTION_CODE
2830 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2831 && this->shndx_
!= MERGE_STRING_SECTION_CODE
2832 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
2835 // Return whether this is a merge section which matches the
2838 is_merge_section(bool is_string
, uint64_t entsize
,
2839 uint64_t addralign
) const
2841 return (this->shndx_
== (is_string
2842 ? MERGE_STRING_SECTION_CODE
2843 : MERGE_DATA_SECTION_CODE
)
2844 && this->u1_
.entsize
== entsize
2845 && this->addralign() == addralign
);
2848 // Return whether this is a relaxed input section.
2850 is_relaxed_input_section() const
2851 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
2853 // Return whether this is a generic Output_section_data.
2855 is_output_section_data() const
2857 return this->shndx_
== OUTPUT_SECTION_CODE
;
2860 // Return the object for an input section.
2864 if (this->is_input_section())
2865 return this->u2_
.object
;
2866 else if (this->is_relaxed_input_section())
2867 return this->u2_
.poris
->relobj();
2872 // Return the input section index for an input section.
2876 if (this->is_input_section())
2877 return this->shndx_
;
2878 else if (this->is_relaxed_input_section())
2879 return this->u2_
.poris
->shndx();
2884 // For non-input-sections, return the associated Output_section_data
2886 Output_section_data
*
2887 output_section_data() const
2889 gold_assert(!this->is_input_section());
2890 return this->u2_
.posd
;
2893 // Return the Output_relaxed_input_section object.
2894 Output_relaxed_input_section
*
2895 relaxed_input_section() const
2897 gold_assert(this->is_relaxed_input_section());
2898 return this->u2_
.poris
;
2901 // Set the output section.
2903 set_output_section(Output_section
* os
)
2905 gold_assert(!this->is_input_section());
2906 Output_section_data
*posd
=
2907 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
2908 posd
->set_output_section(os
);
2911 // Set the address and file offset. This is called during
2912 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2913 // the enclosing section.
2915 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2916 off_t section_file_offset
);
2918 // Reset the address and file offset.
2920 reset_address_and_file_offset();
2922 // Finalize the data size.
2924 finalize_data_size();
2926 // Add an input section, for SHF_MERGE sections.
2928 add_input_section(Relobj
* object
, unsigned int shndx
)
2930 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2931 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2932 return this->u2_
.posd
->add_input_section(object
, shndx
);
2935 // Given an input OBJECT, an input section index SHNDX within that
2936 // object, and an OFFSET relative to the start of that input
2937 // section, return whether or not the output offset is known. If
2938 // this function returns true, it sets *POUTPUT to the offset in
2939 // the output section, relative to the start of the input section
2940 // in the output section. *POUTPUT may be different from OFFSET
2941 // for a merged section.
2943 output_offset(const Relobj
* object
, unsigned int shndx
,
2944 section_offset_type offset
,
2945 section_offset_type
*poutput
) const;
2947 // Return whether this is the merge section for the input section
2950 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2952 // Write out the data. This does nothing for an input section.
2954 write(Output_file
*);
2956 // Write the data to a buffer. This does nothing for an input
2959 write_to_buffer(unsigned char*);
2961 // Print to a map file.
2963 print_to_mapfile(Mapfile
*) const;
2965 // Print statistics about merge sections to stderr.
2967 print_merge_stats(const char* section_name
)
2969 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2970 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2971 this->u2_
.posd
->print_merge_stats(section_name
);
2975 // Code values which appear in shndx_. If the value is not one of
2976 // these codes, it is the input section index in the object file.
2979 // An Output_section_data.
2980 OUTPUT_SECTION_CODE
= -1U,
2981 // An Output_section_data for an SHF_MERGE section with
2982 // SHF_STRINGS not set.
2983 MERGE_DATA_SECTION_CODE
= -2U,
2984 // An Output_section_data for an SHF_MERGE section with
2986 MERGE_STRING_SECTION_CODE
= -3U,
2987 // An Output_section_data for a relaxed input section.
2988 RELAXED_INPUT_SECTION_CODE
= -4U
2991 // For an ordinary input section, this is the section index in the
2992 // input file. For an Output_section_data, this is
2993 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2994 // MERGE_STRING_SECTION_CODE.
2995 unsigned int shndx_
;
2996 // The required alignment, stored as a power of 2.
2997 unsigned int p2align_
;
3000 // For an ordinary input section, the section size.
3002 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3003 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3009 // For an ordinary input section, the object which holds the
3012 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3013 // MERGE_STRING_SECTION_CODE, the data.
3014 Output_section_data
* posd
;
3015 // For RELAXED_INPUT_SECTION_CODE, the data.
3016 Output_relaxed_input_section
* poris
;
3020 typedef std::vector
<Input_section
> Input_section_list
;
3022 // Allow a child class to access the input sections.
3023 const Input_section_list
&
3024 input_sections() const
3025 { return this->input_sections_
; }
3028 // We only save enough information to undo the effects of section layout.
3029 class Checkpoint_output_section
3032 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3033 const Input_section_list
& input_sections
,
3034 off_t first_input_offset
,
3035 bool attached_input_sections_are_sorted
)
3036 : addralign_(addralign
), flags_(flags
),
3037 input_sections_(input_sections
),
3038 input_sections_size_(input_sections_
.size()),
3039 input_sections_copy_(), first_input_offset_(first_input_offset
),
3040 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3044 ~Checkpoint_output_section()
3047 // Return the address alignment.
3050 { return this->addralign_
; }
3052 // Return the section flags.
3055 { return this->flags_
; }
3057 // Return a reference to the input section list copy.
3060 { return &this->input_sections_copy_
; }
3062 // Return the size of input_sections at the time when checkpoint is
3065 input_sections_size() const
3066 { return this->input_sections_size_
; }
3068 // Whether input sections are copied.
3070 input_sections_saved() const
3071 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3074 first_input_offset() const
3075 { return this->first_input_offset_
; }
3078 attached_input_sections_are_sorted() const
3079 { return this->attached_input_sections_are_sorted_
; }
3081 // Save input sections.
3083 save_input_sections()
3085 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3086 this->input_sections_copy_
.clear();
3087 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3088 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3089 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3090 this->input_sections_copy_
.push_back(*p
);
3094 // The section alignment.
3095 uint64_t addralign_
;
3096 // The section flags.
3097 elfcpp::Elf_Xword flags_
;
3098 // Reference to the input sections to be checkpointed.
3099 const Input_section_list
& input_sections_
;
3100 // Size of the checkpointed portion of input_sections_;
3101 size_t input_sections_size_
;
3102 // Copy of input sections.
3103 Input_section_list input_sections_copy_
;
3104 // The offset of the first entry in input_sections_.
3105 off_t first_input_offset_
;
3106 // True if the input sections attached to this output section have
3107 // already been sorted.
3108 bool attached_input_sections_are_sorted_
;
3111 // This class is used to sort the input sections.
3112 class Input_section_sort_entry
;
3114 // This is the sort comparison function.
3115 struct Input_section_sort_compare
3118 operator()(const Input_section_sort_entry
&,
3119 const Input_section_sort_entry
&) const;
3122 // Fill data. This is used to fill in data between input sections.
3123 // It is also used for data statements (BYTE, WORD, etc.) in linker
3124 // scripts. When we have to keep track of the input sections, we
3125 // can use an Output_data_const, but we don't want to have to keep
3126 // track of input sections just to implement fills.
3130 Fill(off_t section_offset
, off_t length
)
3131 : section_offset_(section_offset
),
3132 length_(convert_to_section_size_type(length
))
3135 // Return section offset.
3137 section_offset() const
3138 { return this->section_offset_
; }
3140 // Return fill length.
3143 { return this->length_
; }
3146 // The offset within the output section.
3147 off_t section_offset_
;
3148 // The length of the space to fill.
3149 section_size_type length_
;
3152 typedef std::vector
<Fill
> Fill_list
;
3154 // This class describes properties of merge data sections. It is used
3155 // as a key type for maps.
3156 class Merge_section_properties
3159 Merge_section_properties(bool is_string
, uint64_t entsize
,
3161 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
3164 // Whether this equals to another Merge_section_properties MSP.
3166 eq(const Merge_section_properties
& msp
) const
3168 return ((this->is_string_
== msp
.is_string_
)
3169 && (this->entsize_
== msp
.entsize_
)
3170 && (this->addralign_
== msp
.addralign_
));
3173 // Compute a hash value for this using 64-bit FNV-1a hash.
3177 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
3178 uint64_t prime
= 1099511628211ULL;
3179 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
3180 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
3181 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
3185 // Functors for associative containers.
3189 operator()(const Merge_section_properties
& msp1
,
3190 const Merge_section_properties
& msp2
) const
3191 { return msp1
.eq(msp2
); }
3197 operator()(const Merge_section_properties
& msp
) const
3198 { return msp
.hash_value(); }
3202 // Whether this merge data section is for strings.
3204 // Entsize of this merge data section.
3206 // Address alignment.
3207 uint64_t addralign_
;
3210 // Map that link Merge_section_properties to Output_merge_base.
3211 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
3212 Merge_section_properties::hash
,
3213 Merge_section_properties::equal_to
>
3214 Merge_section_by_properties_map
;
3216 // Map that link Input_section_specifier to Output_section_data.
3217 typedef Unordered_map
<Input_section_specifier
, Output_section_data
*,
3218 Input_section_specifier::hash
,
3219 Input_section_specifier::equal_to
>
3220 Output_section_data_by_input_section_map
;
3222 // Map that link Input_section_specifier to Output_relaxed_input_section.
3223 typedef Unordered_map
<Input_section_specifier
, Output_relaxed_input_section
*,
3224 Input_section_specifier::hash
,
3225 Input_section_specifier::equal_to
>
3226 Output_relaxed_input_section_by_input_section_map
;
3228 // Map used during relaxation of existing sections. This map
3229 // an input section specifier to an input section list index.
3230 // We assume that Input_section_list is a vector.
3231 typedef Unordered_map
<Input_section_specifier
, size_t,
3232 Input_section_specifier::hash
,
3233 Input_section_specifier::equal_to
>
3236 // Add a new output section by Input_section.
3238 add_output_section_data(Input_section
*);
3240 // Add an SHF_MERGE input section. Returns true if the section was
3243 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3244 uint64_t entsize
, uint64_t addralign
);
3246 // Add an output SHF_MERGE section POSD to this output section.
3247 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3248 // ENTSIZE is the entity size. This returns the entry added to
3251 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3254 // Sort the attached input sections.
3256 sort_attached_input_sections();
3258 // Find the merge section into which an input section with index SHNDX in
3259 // OBJECT has been added. Return NULL if none found.
3260 Output_section_data
*
3261 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3263 // Build a relaxation map.
3265 build_relaxation_map(
3266 const Input_section_list
& input_sections
,
3268 Relaxation_map
* map
) const;
3270 // Convert input sections in an input section list into relaxed sections.
3272 convert_input_sections_in_list_to_relaxed_sections(
3273 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3274 const Relaxation_map
& map
,
3275 Input_section_list
* input_sections
);
3277 // Most of these fields are only valid after layout.
3279 // The name of the section. This will point into a Stringpool.
3281 // The section address is in the parent class.
3282 // The section alignment.
3283 uint64_t addralign_
;
3284 // The section entry size.
3286 // The load address. This is only used when using a linker script
3287 // with a SECTIONS clause. The has_load_address_ field indicates
3288 // whether this field is valid.
3289 uint64_t load_address_
;
3290 // The file offset is in the parent class.
3291 // Set the section link field to the index of this section.
3292 const Output_data
* link_section_
;
3293 // If link_section_ is NULL, this is the link field.
3295 // Set the section info field to the index of this section.
3296 const Output_section
* info_section_
;
3297 // If info_section_ is NULL, set the info field to the symbol table
3298 // index of this symbol.
3299 const Symbol
* info_symndx_
;
3300 // If info_section_ and info_symndx_ are NULL, this is the section
3303 // The section type.
3304 const elfcpp::Elf_Word type_
;
3305 // The section flags.
3306 elfcpp::Elf_Xword flags_
;
3307 // The section index.
3308 unsigned int out_shndx_
;
3309 // If there is a STT_SECTION for this output section in the normal
3310 // symbol table, this is the symbol index. This starts out as zero.
3311 // It is initialized in Layout::finalize() to be the index, or -1U
3312 // if there isn't one.
3313 unsigned int symtab_index_
;
3314 // If there is a STT_SECTION for this output section in the dynamic
3315 // symbol table, this is the symbol index. This starts out as zero.
3316 // It is initialized in Layout::finalize() to be the index, or -1U
3317 // if there isn't one.
3318 unsigned int dynsym_index_
;
3319 // The input sections. This will be empty in cases where we don't
3320 // need to keep track of them.
3321 Input_section_list input_sections_
;
3322 // The offset of the first entry in input_sections_.
3323 off_t first_input_offset_
;
3324 // The fill data. This is separate from input_sections_ because we
3325 // often will need fill sections without needing to keep track of
3328 // If the section requires postprocessing, this buffer holds the
3329 // section contents during relocation.
3330 unsigned char* postprocessing_buffer_
;
3331 // Whether this output section needs a STT_SECTION symbol in the
3332 // normal symbol table. This will be true if there is a relocation
3334 bool needs_symtab_index_
: 1;
3335 // Whether this output section needs a STT_SECTION symbol in the
3336 // dynamic symbol table. This will be true if there is a dynamic
3337 // relocation which needs it.
3338 bool needs_dynsym_index_
: 1;
3339 // Whether the link field of this output section should point to the
3340 // normal symbol table.
3341 bool should_link_to_symtab_
: 1;
3342 // Whether the link field of this output section should point to the
3343 // dynamic symbol table.
3344 bool should_link_to_dynsym_
: 1;
3345 // Whether this section should be written after all the input
3346 // sections are complete.
3347 bool after_input_sections_
: 1;
3348 // Whether this section requires post processing after all
3349 // relocations have been applied.
3350 bool requires_postprocessing_
: 1;
3351 // Whether an input section was mapped to this output section
3352 // because of a SECTIONS clause in a linker script.
3353 bool found_in_sections_clause_
: 1;
3354 // Whether this section has an explicitly specified load address.
3355 bool has_load_address_
: 1;
3356 // True if the info_section_ field means the section index of the
3357 // section, false if it means the symbol index of the corresponding
3359 bool info_uses_section_index_
: 1;
3360 // True if the input sections attached to this output section may
3362 bool may_sort_attached_input_sections_
: 1;
3363 // True if the input sections attached to this output section must
3365 bool must_sort_attached_input_sections_
: 1;
3366 // True if the input sections attached to this output section have
3367 // already been sorted.
3368 bool attached_input_sections_are_sorted_
: 1;
3369 // True if this section holds relro data.
3371 // True if this section holds relro local data.
3372 bool is_relro_local_
: 1;
3373 // True if this must be the last relro section.
3374 bool is_last_relro_
: 1;
3375 // True if this must be the first section after the relro sections.
3376 bool is_first_non_relro_
: 1;
3377 // True if this is a small section.
3378 bool is_small_section_
: 1;
3379 // True if this is a large section.
3380 bool is_large_section_
: 1;
3381 // True if this is the .interp section going into the PT_INTERP
3383 bool is_interp_
: 1;
3384 // True if this is section is read by the dynamic linker.
3385 bool is_dynamic_linker_section_
: 1;
3386 // Whether code-fills are generated at write.
3387 bool generate_code_fills_at_write_
: 1;
3388 // Whether the entry size field should be zero.
3389 bool is_entsize_zero_
: 1;
3390 // For SHT_TLS sections, the offset of this section relative to the base
3391 // of the TLS segment.
3392 uint64_t tls_offset_
;
3393 // Saved checkpoint.
3394 Checkpoint_output_section
* checkpoint_
;
3395 // Map from input sections to merge sections.
3396 Output_section_data_by_input_section_map merge_section_map_
;
3397 // Map from merge section properties to merge_sections;
3398 Merge_section_by_properties_map merge_section_by_properties_map_
;
3399 // Map from input sections to relaxed input sections. This is mutable
3400 // because it is updated lazily. We may need to update it in a
3401 // const qualified method.
3402 mutable Output_relaxed_input_section_by_input_section_map
3403 relaxed_input_section_map_
;
3404 // Whether relaxed_input_section_map_ is valid.
3405 mutable bool is_relaxed_input_section_map_valid_
;
3408 // An output segment. PT_LOAD segments are built from collections of
3409 // output sections. Other segments typically point within PT_LOAD
3410 // segments, and are built directly as needed.
3412 // NOTE: We want to use the copy constructor for this class. During
3413 // relaxation, we may try built the segments multiple times. We do
3414 // that by copying the original segment list before lay-out, doing
3415 // a trial lay-out and roll-back to the saved copied if we need to
3416 // to the lay-out again.
3418 class Output_segment
3421 // Create an output segment, specifying the type and flags.
3422 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3424 // Return the virtual address.
3427 { return this->vaddr_
; }
3429 // Return the physical address.
3432 { return this->paddr_
; }
3434 // Return the segment type.
3437 { return this->type_
; }
3439 // Return the segment flags.
3442 { return this->flags_
; }
3444 // Return the memory size.
3447 { return this->memsz_
; }
3449 // Return the file size.
3452 { return this->filesz_
; }
3454 // Return the file offset.
3457 { return this->offset_
; }
3459 // Whether this is a segment created to hold large data sections.
3461 is_large_data_segment() const
3462 { return this->is_large_data_segment_
; }
3464 // Record that this is a segment created to hold large data
3467 set_is_large_data_segment()
3468 { this->is_large_data_segment_
= true; }
3470 // Return the maximum alignment of the Output_data.
3472 maximum_alignment();
3474 // Add the Output_section OS to this segment. SEG_FLAGS is the
3475 // segment flags to use. DO_SORT is true if we should sort the
3476 // placement of the input section for more efficient generated code.
3478 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
,
3481 // Remove an Output_section from this segment. It is an error if it
3484 remove_output_section(Output_section
* os
);
3486 // Add an Output_data (which is not an Output_section) to the start
3489 add_initial_output_data(Output_data
*);
3491 // Return true if this segment has any sections which hold actual
3492 // data, rather than being a BSS section.
3494 has_any_data_sections() const
3495 { return !this->output_data_
.empty(); }
3497 // Return the number of dynamic relocations applied to this segment.
3499 dynamic_reloc_count() const;
3501 // Return the address of the first section.
3503 first_section_load_address() const;
3505 // Return whether the addresses have been set already.
3507 are_addresses_set() const
3508 { return this->are_addresses_set_
; }
3510 // Set the addresses.
3512 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3514 this->vaddr_
= vaddr
;
3515 this->paddr_
= paddr
;
3516 this->are_addresses_set_
= true;
3519 // Set the segment flags. This is only used if we have a PHDRS
3520 // clause which explicitly specifies the flags.
3522 set_flags(elfcpp::Elf_Word flags
)
3523 { this->flags_
= flags
; }
3525 // Set the address of the segment to ADDR and the offset to *POFF
3526 // and set the addresses and offsets of all contained output
3527 // sections accordingly. Set the section indexes of all contained
3528 // output sections starting with *PSHNDX. If RESET is true, first
3529 // reset the addresses of the contained sections. Return the
3530 // address of the immediately following segment. Update *POFF and
3531 // *PSHNDX. This should only be called for a PT_LOAD segment.
3533 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
,
3534 unsigned int increase_relro
, off_t
* poff
,
3535 unsigned int* pshndx
);
3537 // Set the minimum alignment of this segment. This may be adjusted
3538 // upward based on the section alignments.
3540 set_minimum_p_align(uint64_t align
)
3541 { this->min_p_align_
= align
; }
3543 // Set the offset of this segment based on the section. This should
3544 // only be called for a non-PT_LOAD segment.
3546 set_offset(unsigned int increase
);
3548 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3552 // Return the number of output sections.
3554 output_section_count() const;
3556 // Return the section attached to the list segment with the lowest
3557 // load address. This is used when handling a PHDRS clause in a
3560 section_with_lowest_load_address() const;
3562 // Write the segment header into *OPHDR.
3563 template<int size
, bool big_endian
>
3565 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
3567 // Write the section headers of associated sections into V.
3568 template<int size
, bool big_endian
>
3570 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
3571 unsigned int* pshndx
) const;
3573 // Print the output sections in the map file.
3575 print_sections_to_mapfile(Mapfile
*) const;
3578 typedef std::list
<Output_data
*> Output_data_list
;
3580 // Find the maximum alignment in an Output_data_list.
3582 maximum_alignment_list(const Output_data_list
*);
3584 // Return whether the first data section is a relro section.
3586 is_first_section_relro() const;
3588 // Set the section addresses in an Output_data_list.
3590 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
3591 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
3594 // Return the number of Output_sections in an Output_data_list.
3596 output_section_count_list(const Output_data_list
*) const;
3598 // Return the number of dynamic relocs in an Output_data_list.
3600 dynamic_reloc_count_list(const Output_data_list
*) const;
3602 // Find the section with the lowest load address in an
3603 // Output_data_list.
3605 lowest_load_address_in_list(const Output_data_list
* pdl
,
3606 Output_section
** found
,
3607 uint64_t* found_lma
) const;
3609 // Write the section headers in the list into V.
3610 template<int size
, bool big_endian
>
3612 write_section_headers_list(const Layout
*, const Stringpool
*,
3613 const Output_data_list
*, unsigned char* v
,
3614 unsigned int* pshdx
) const;
3616 // Print a section list to the mapfile.
3618 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
3620 // NOTE: We want to use the copy constructor. Currently, shallow copy
3621 // works for us so we do not need to write our own copy constructor.
3623 // The list of output data with contents attached to this segment.
3624 Output_data_list output_data_
;
3625 // The list of output data without contents attached to this segment.
3626 Output_data_list output_bss_
;
3627 // The segment virtual address.
3629 // The segment physical address.
3631 // The size of the segment in memory.
3633 // The maximum section alignment. The is_max_align_known_ field
3634 // indicates whether this has been finalized.
3635 uint64_t max_align_
;
3636 // The required minimum value for the p_align field. This is used
3637 // for PT_LOAD segments. Note that this does not mean that
3638 // addresses should be aligned to this value; it means the p_paddr
3639 // and p_vaddr fields must be congruent modulo this value. For
3640 // non-PT_LOAD segments, the dynamic linker works more efficiently
3641 // if the p_align field has the more conventional value, although it
3642 // can align as needed.
3643 uint64_t min_p_align_
;
3644 // The offset of the segment data within the file.
3646 // The size of the segment data in the file.
3648 // The segment type;
3649 elfcpp::Elf_Word type_
;
3650 // The segment flags.
3651 elfcpp::Elf_Word flags_
;
3652 // Whether we have finalized max_align_.
3653 bool is_max_align_known_
: 1;
3654 // Whether vaddr and paddr were set by a linker script.
3655 bool are_addresses_set_
: 1;
3656 // Whether this segment holds large data sections.
3657 bool is_large_data_segment_
: 1;
3660 // This class represents the output file.
3665 Output_file(const char* name
);
3667 // Indicate that this is a temporary file which should not be
3671 { this->is_temporary_
= true; }
3673 // Try to open an existing file. Returns false if the file doesn't
3674 // exist, has a size of 0 or can't be mmaped. This method is
3677 open_for_modification();
3679 // Open the output file. FILE_SIZE is the final size of the file.
3680 // If the file already exists, it is deleted/truncated. This method
3681 // is thread-unsafe.
3683 open(off_t file_size
);
3685 // Resize the output file. This method is thread-unsafe.
3687 resize(off_t file_size
);
3689 // Close the output file (flushing all buffered data) and make sure
3690 // there are no errors. This method is thread-unsafe.
3694 // Return the size of this file.
3697 { return this->file_size_
; }
3699 // Return the name of this file.
3702 { return this->name_
; }
3704 // We currently always use mmap which makes the view handling quite
3705 // simple. In the future we may support other approaches.
3707 // Write data to the output file.
3709 write(off_t offset
, const void* data
, size_t len
)
3710 { memcpy(this->base_
+ offset
, data
, len
); }
3712 // Get a buffer to use to write to the file, given the offset into
3713 // the file and the size.
3715 get_output_view(off_t start
, size_t size
)
3717 gold_assert(start
>= 0
3718 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
3719 return this->base_
+ start
;
3722 // VIEW must have been returned by get_output_view. Write the
3723 // buffer to the file, passing in the offset and the size.
3725 write_output_view(off_t
, size_t, unsigned char*)
3728 // Get a read/write buffer. This is used when we want to write part
3729 // of the file, read it in, and write it again.
3731 get_input_output_view(off_t start
, size_t size
)
3732 { return this->get_output_view(start
, size
); }
3734 // Write a read/write buffer back to the file.
3736 write_input_output_view(off_t
, size_t, unsigned char*)
3739 // Get a read buffer. This is used when we just want to read part
3740 // of the file back it in.
3741 const unsigned char*
3742 get_input_view(off_t start
, size_t size
)
3743 { return this->get_output_view(start
, size
); }
3745 // Release a read bfufer.
3747 free_input_view(off_t
, size_t, const unsigned char*)
3751 // Map the file into memory or, if that fails, allocate anonymous
3756 // Allocate anonymous memory for the file.
3760 // Map the file into memory.
3764 // Unmap the file from memory (and flush to disk buffers).
3774 // Base of file mapped into memory.
3775 unsigned char* base_
;
3776 // True iff base_ points to a memory buffer rather than an output file.
3777 bool map_is_anonymous_
;
3778 // True if this is a temporary file which should not be output.
3782 } // End namespace gold.
3784 #endif // !defined(GOLD_OUTPUT_H)