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 // An abtract class for data which has to go into the output file.
55 explicit Output_data()
56 : address_(0), data_size_(0), offset_(-1),
57 is_address_valid_(false), is_data_size_valid_(false),
58 is_offset_valid_(false), is_data_size_fixed_(false),
59 dynamic_reloc_count_(0)
65 // Return the address. For allocated sections, this is only valid
66 // after Layout::finalize is finished.
70 gold_assert(this->is_address_valid_
);
71 return this->address_
;
74 // Return the size of the data. For allocated sections, this must
75 // be valid after Layout::finalize calls set_address, but need not
76 // be valid before then.
80 gold_assert(this->is_data_size_valid_
);
81 return this->data_size_
;
84 // Return true if data size is fixed.
86 is_data_size_fixed() const
87 { return this->is_data_size_fixed_
; }
89 // Return the file offset. This is only valid after
90 // Layout::finalize is finished. For some non-allocated sections,
91 // it may not be valid until near the end of the link.
95 gold_assert(this->is_offset_valid_
);
99 // Reset the address and file offset. This essentially disables the
100 // sanity testing about duplicate and unknown settings.
102 reset_address_and_file_offset()
104 this->is_address_valid_
= false;
105 this->is_offset_valid_
= false;
106 if (!this->is_data_size_fixed_
)
107 this->is_data_size_valid_
= false;
108 this->do_reset_address_and_file_offset();
111 // Return true if address and file offset already have reset values. In
112 // other words, calling reset_address_and_file_offset will not change them.
114 address_and_file_offset_have_reset_values() const
115 { return this->do_address_and_file_offset_have_reset_values(); }
117 // Return the required alignment.
120 { return this->do_addralign(); }
122 // Return whether this has a load address.
124 has_load_address() const
125 { return this->do_has_load_address(); }
127 // Return the load address.
130 { return this->do_load_address(); }
132 // Return whether this is an Output_section.
135 { return this->do_is_section(); }
137 // Return whether this is an Output_section of the specified type.
139 is_section_type(elfcpp::Elf_Word stt
) const
140 { return this->do_is_section_type(stt
); }
142 // Return whether this is an Output_section with the specified flag
145 is_section_flag_set(elfcpp::Elf_Xword shf
) const
146 { return this->do_is_section_flag_set(shf
); }
148 // Return the output section that this goes in, if there is one.
151 { return this->do_output_section(); }
153 const Output_section
*
154 output_section() const
155 { return this->do_output_section(); }
157 // Return the output section index, if there is an output section.
160 { return this->do_out_shndx(); }
162 // Set the output section index, if this is an output section.
164 set_out_shndx(unsigned int shndx
)
165 { this->do_set_out_shndx(shndx
); }
167 // Set the address and file offset of this data, and finalize the
168 // size of the data. This is called during Layout::finalize for
169 // allocated sections.
171 set_address_and_file_offset(uint64_t addr
, off_t off
)
173 this->set_address(addr
);
174 this->set_file_offset(off
);
175 this->finalize_data_size();
180 set_address(uint64_t addr
)
182 gold_assert(!this->is_address_valid_
);
183 this->address_
= addr
;
184 this->is_address_valid_
= true;
187 // Set the file offset.
189 set_file_offset(off_t off
)
191 gold_assert(!this->is_offset_valid_
);
193 this->is_offset_valid_
= true;
196 // Finalize the data size.
200 if (!this->is_data_size_valid_
)
202 // Tell the child class to set the data size.
203 this->set_final_data_size();
204 gold_assert(this->is_data_size_valid_
);
208 // Set the TLS offset. Called only for SHT_TLS sections.
210 set_tls_offset(uint64_t tls_base
)
211 { this->do_set_tls_offset(tls_base
); }
213 // Return the TLS offset, relative to the base of the TLS segment.
214 // Valid only for SHT_TLS sections.
217 { return this->do_tls_offset(); }
219 // Write the data to the output file. This is called after
220 // Layout::finalize is complete.
222 write(Output_file
* file
)
223 { this->do_write(file
); }
225 // This is called by Layout::finalize to note that the sizes of
226 // allocated sections must now be fixed.
229 { Output_data::allocated_sizes_are_fixed
= true; }
231 // Used to check that layout has been done.
234 { return Output_data::allocated_sizes_are_fixed
; }
236 // Count the number of dynamic relocations applied to this section.
239 { ++this->dynamic_reloc_count_
; }
241 // Return the number of dynamic relocations applied to this section.
243 dynamic_reloc_count() const
244 { return this->dynamic_reloc_count_
; }
246 // Whether the address is valid.
248 is_address_valid() const
249 { return this->is_address_valid_
; }
251 // Whether the file offset is valid.
253 is_offset_valid() const
254 { return this->is_offset_valid_
; }
256 // Whether the data size is valid.
258 is_data_size_valid() const
259 { return this->is_data_size_valid_
; }
261 // Print information to the map file.
263 print_to_mapfile(Mapfile
* mapfile
) const
264 { return this->do_print_to_mapfile(mapfile
); }
267 // Functions that child classes may or in some cases must implement.
269 // Write the data to the output file.
271 do_write(Output_file
*) = 0;
273 // Return the required alignment.
275 do_addralign() const = 0;
277 // Return whether this has a load address.
279 do_has_load_address() const
282 // Return the load address.
284 do_load_address() const
285 { gold_unreachable(); }
287 // Return whether this is an Output_section.
289 do_is_section() const
292 // Return whether this is an Output_section of the specified type.
293 // This only needs to be implement by Output_section.
295 do_is_section_type(elfcpp::Elf_Word
) const
298 // Return whether this is an Output_section with the specific flag
299 // set. This only needs to be implemented by Output_section.
301 do_is_section_flag_set(elfcpp::Elf_Xword
) const
304 // Return the output section, if there is one.
305 virtual Output_section
*
309 virtual const Output_section
*
310 do_output_section() const
313 // Return the output section index, if there is an output section.
316 { gold_unreachable(); }
318 // Set the output section index, if this is an output section.
320 do_set_out_shndx(unsigned int)
321 { gold_unreachable(); }
323 // This is a hook for derived classes to set the data size. This is
324 // called by finalize_data_size, normally called during
325 // Layout::finalize, when the section address is set.
327 set_final_data_size()
328 { gold_unreachable(); }
330 // A hook for resetting the address and file offset.
332 do_reset_address_and_file_offset()
335 // Return true if address and file offset already have reset values. In
336 // other words, calling reset_address_and_file_offset will not change them.
337 // A child class overriding do_reset_address_and_file_offset may need to
338 // also override this.
340 do_address_and_file_offset_have_reset_values() const
341 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
343 // Set the TLS offset. Called only for SHT_TLS sections.
345 do_set_tls_offset(uint64_t)
346 { gold_unreachable(); }
348 // Return the TLS offset, relative to the base of the TLS segment.
349 // Valid only for SHT_TLS sections.
351 do_tls_offset() const
352 { gold_unreachable(); }
354 // Print to the map file. This only needs to be implemented by
355 // classes which may appear in a PT_LOAD segment.
357 do_print_to_mapfile(Mapfile
*) const
358 { gold_unreachable(); }
360 // Functions that child classes may call.
362 // Reset the address. The Output_section class needs this when an
363 // SHF_ALLOC input section is added to an output section which was
364 // formerly not SHF_ALLOC.
366 mark_address_invalid()
367 { this->is_address_valid_
= false; }
369 // Set the size of the data.
371 set_data_size(off_t data_size
)
373 gold_assert(!this->is_data_size_valid_
374 && !this->is_data_size_fixed_
);
375 this->data_size_
= data_size
;
376 this->is_data_size_valid_
= true;
379 // Fix the data size. Once it is fixed, it cannot be changed
380 // and the data size remains always valid.
384 gold_assert(this->is_data_size_valid_
);
385 this->is_data_size_fixed_
= true;
388 // Get the current data size--this is for the convenience of
389 // sections which build up their size over time.
391 current_data_size_for_child() const
392 { return this->data_size_
; }
394 // Set the current data size--this is for the convenience of
395 // sections which build up their size over time.
397 set_current_data_size_for_child(off_t data_size
)
399 gold_assert(!this->is_data_size_valid_
);
400 this->data_size_
= data_size
;
403 // Return default alignment for the target size.
407 // Return default alignment for a specified size--32 or 64.
409 default_alignment_for_size(int size
);
412 Output_data(const Output_data
&);
413 Output_data
& operator=(const Output_data
&);
415 // This is used for verification, to make sure that we don't try to
416 // change any sizes of allocated sections after we set the section
418 static bool allocated_sizes_are_fixed
;
420 // Memory address in output file.
422 // Size of data in output file.
424 // File offset of contents in output file.
426 // Whether address_ is valid.
427 bool is_address_valid_
;
428 // Whether data_size_ is valid.
429 bool is_data_size_valid_
;
430 // Whether offset_ is valid.
431 bool is_offset_valid_
;
432 // Whether data size is fixed.
433 bool is_data_size_fixed_
;
434 // Count of dynamic relocations applied to this section.
435 unsigned int dynamic_reloc_count_
;
438 // Output the section headers.
440 class Output_section_headers
: public Output_data
443 Output_section_headers(const Layout
*,
444 const Layout::Segment_list
*,
445 const Layout::Section_list
*,
446 const Layout::Section_list
*,
448 const Output_section
*);
451 // Write the data to the file.
453 do_write(Output_file
*);
455 // Return the required alignment.
458 { return Output_data::default_alignment(); }
460 // Write to a map file.
462 do_print_to_mapfile(Mapfile
* mapfile
) const
463 { mapfile
->print_output_data(this, _("** section headers")); }
465 // Set final data size.
467 set_final_data_size()
468 { this->set_data_size(this->do_size()); }
471 // Write the data to the file with the right size and endianness.
472 template<int size
, bool big_endian
>
474 do_sized_write(Output_file
*);
476 // Compute data size.
480 const Layout
* layout_
;
481 const Layout::Segment_list
* segment_list_
;
482 const Layout::Section_list
* section_list_
;
483 const Layout::Section_list
* unattached_section_list_
;
484 const Stringpool
* secnamepool_
;
485 const Output_section
* shstrtab_section_
;
488 // Output the segment headers.
490 class Output_segment_headers
: public Output_data
493 Output_segment_headers(const Layout::Segment_list
& segment_list
);
496 // Write the data to the file.
498 do_write(Output_file
*);
500 // Return the required alignment.
503 { return Output_data::default_alignment(); }
505 // Write to a map file.
507 do_print_to_mapfile(Mapfile
* mapfile
) const
508 { mapfile
->print_output_data(this, _("** segment headers")); }
510 // Set final data size.
512 set_final_data_size()
513 { this->set_data_size(this->do_size()); }
516 // Write the data to the file with the right size and endianness.
517 template<int size
, bool big_endian
>
519 do_sized_write(Output_file
*);
521 // Compute the current size.
525 const Layout::Segment_list
& segment_list_
;
528 // Output the ELF file header.
530 class Output_file_header
: public Output_data
533 Output_file_header(const Target
*,
535 const Output_segment_headers
*,
538 // Add information about the section headers. We lay out the ELF
539 // file header before we create the section headers.
540 void set_section_info(const Output_section_headers
*,
541 const Output_section
* shstrtab
);
544 // Write the data to the file.
546 do_write(Output_file
*);
548 // Return the required alignment.
551 { return Output_data::default_alignment(); }
553 // Write to a map file.
555 do_print_to_mapfile(Mapfile
* mapfile
) const
556 { mapfile
->print_output_data(this, _("** file header")); }
558 // Set final data size.
560 set_final_data_size(void)
561 { this->set_data_size(this->do_size()); }
564 // Write the data to the file with the right size and endianness.
565 template<int size
, bool big_endian
>
567 do_sized_write(Output_file
*);
569 // Return the value to use for the entry address.
571 typename
elfcpp::Elf_types
<size
>::Elf_Addr
574 // Compute the current data size.
578 const Target
* target_
;
579 const Symbol_table
* symtab_
;
580 const Output_segment_headers
* segment_header_
;
581 const Output_section_headers
* section_header_
;
582 const Output_section
* shstrtab_
;
586 // Output sections are mainly comprised of input sections. However,
587 // there are cases where we have data to write out which is not in an
588 // input section. Output_section_data is used in such cases. This is
589 // an abstract base class.
591 class Output_section_data
: public Output_data
594 Output_section_data(off_t data_size
, uint64_t addralign
,
595 bool is_data_size_fixed
)
596 : Output_data(), output_section_(NULL
), addralign_(addralign
)
598 this->set_data_size(data_size
);
599 if (is_data_size_fixed
)
600 this->fix_data_size();
603 Output_section_data(uint64_t addralign
)
604 : Output_data(), output_section_(NULL
), addralign_(addralign
)
607 // Return the output section.
608 const Output_section
*
609 output_section() const
610 { return this->output_section_
; }
612 // Record the output section.
614 set_output_section(Output_section
* os
);
616 // Add an input section, for SHF_MERGE sections. This returns true
617 // if the section was handled.
619 add_input_section(Relobj
* object
, unsigned int shndx
)
620 { return this->do_add_input_section(object
, shndx
); }
622 // Given an input OBJECT, an input section index SHNDX within that
623 // object, and an OFFSET relative to the start of that input
624 // section, return whether or not the corresponding offset within
625 // the output section is known. If this function returns true, it
626 // sets *POUTPUT to the output offset. The value -1 indicates that
627 // this input offset is being discarded.
629 output_offset(const Relobj
* object
, unsigned int shndx
,
630 section_offset_type offset
,
631 section_offset_type
*poutput
) const
632 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
634 // Return whether this is the merge section for the input section
635 // SHNDX in OBJECT. This should return true when output_offset
636 // would return true for some values of OFFSET.
638 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
639 { return this->do_is_merge_section_for(object
, shndx
); }
641 // Write the contents to a buffer. This is used for sections which
642 // require postprocessing, such as compression.
644 write_to_buffer(unsigned char* buffer
)
645 { this->do_write_to_buffer(buffer
); }
647 // Print merge stats to stderr. This should only be called for
648 // SHF_MERGE sections.
650 print_merge_stats(const char* section_name
)
651 { this->do_print_merge_stats(section_name
); }
654 // The child class must implement do_write.
656 // The child class may implement specific adjustments to the output
659 do_adjust_output_section(Output_section
*)
662 // May be implemented by child class. Return true if the section
665 do_add_input_section(Relobj
*, unsigned int)
666 { gold_unreachable(); }
668 // The child class may implement output_offset.
670 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
671 section_offset_type
*) const
674 // The child class may implement is_merge_section_for.
676 do_is_merge_section_for(const Relobj
*, unsigned int) const
679 // The child class may implement write_to_buffer. Most child
680 // classes can not appear in a compressed section, and they do not
683 do_write_to_buffer(unsigned char*)
684 { gold_unreachable(); }
686 // Print merge statistics.
688 do_print_merge_stats(const char*)
689 { gold_unreachable(); }
691 // Return the required alignment.
694 { return this->addralign_
; }
696 // Return the output section.
699 { return this->output_section_
; }
701 const Output_section
*
702 do_output_section() const
703 { return this->output_section_
; }
705 // Return the section index of the output section.
707 do_out_shndx() const;
709 // Set the alignment.
711 set_addralign(uint64_t addralign
);
714 // The output section for this section.
715 Output_section
* output_section_
;
716 // The required alignment.
720 // Some Output_section_data classes build up their data step by step,
721 // rather than all at once. This class provides an interface for
724 class Output_section_data_build
: public Output_section_data
727 Output_section_data_build(uint64_t addralign
)
728 : Output_section_data(addralign
)
731 // Get the current data size.
733 current_data_size() const
734 { return this->current_data_size_for_child(); }
736 // Set the current data size.
738 set_current_data_size(off_t data_size
)
739 { this->set_current_data_size_for_child(data_size
); }
742 // Set the final data size.
744 set_final_data_size()
745 { this->set_data_size(this->current_data_size_for_child()); }
748 // A simple case of Output_data in which we have constant data to
751 class Output_data_const
: public Output_section_data
754 Output_data_const(const std::string
& data
, uint64_t addralign
)
755 : Output_section_data(data
.size(), addralign
, true), data_(data
)
758 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
759 : Output_section_data(len
, addralign
, true), data_(p
, len
)
762 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
763 : Output_section_data(len
, addralign
, true),
764 data_(reinterpret_cast<const char*>(p
), len
)
768 // Write the data to the output file.
770 do_write(Output_file
*);
772 // Write the data to a buffer.
774 do_write_to_buffer(unsigned char* buffer
)
775 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
777 // Write to a map file.
779 do_print_to_mapfile(Mapfile
* mapfile
) const
780 { mapfile
->print_output_data(this, _("** fill")); }
786 // Another version of Output_data with constant data, in which the
787 // buffer is allocated by the caller.
789 class Output_data_const_buffer
: public Output_section_data
792 Output_data_const_buffer(const unsigned char* p
, off_t len
,
793 uint64_t addralign
, const char* map_name
)
794 : Output_section_data(len
, addralign
, true),
795 p_(p
), map_name_(map_name
)
799 // Write the data the output file.
801 do_write(Output_file
*);
803 // Write the data to a buffer.
805 do_write_to_buffer(unsigned char* buffer
)
806 { memcpy(buffer
, this->p_
, this->data_size()); }
808 // Write to a map file.
810 do_print_to_mapfile(Mapfile
* mapfile
) const
811 { mapfile
->print_output_data(this, _(this->map_name_
)); }
814 // The data to output.
815 const unsigned char* p_
;
816 // Name to use in a map file. Maps are a rarely used feature, but
817 // the space usage is minor as aren't very many of these objects.
818 const char* map_name_
;
821 // A place holder for a fixed amount of data written out via some
824 class Output_data_fixed_space
: public Output_section_data
827 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
828 const char* map_name
)
829 : Output_section_data(data_size
, addralign
, true),
834 // Write out the data--the actual data must be written out
837 do_write(Output_file
*)
840 // Write to a map file.
842 do_print_to_mapfile(Mapfile
* mapfile
) const
843 { mapfile
->print_output_data(this, _(this->map_name_
)); }
846 // Name to use in a map file. Maps are a rarely used feature, but
847 // the space usage is minor as aren't very many of these objects.
848 const char* map_name_
;
851 // A place holder for variable sized data written out via some other
854 class Output_data_space
: public Output_section_data_build
857 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
858 : Output_section_data_build(addralign
),
862 // Set the alignment.
864 set_space_alignment(uint64_t align
)
865 { this->set_addralign(align
); }
868 // Write out the data--the actual data must be written out
871 do_write(Output_file
*)
874 // Write to a map file.
876 do_print_to_mapfile(Mapfile
* mapfile
) const
877 { mapfile
->print_output_data(this, _(this->map_name_
)); }
880 // Name to use in a map file. Maps are a rarely used feature, but
881 // the space usage is minor as aren't very many of these objects.
882 const char* map_name_
;
885 // Fill fixed space with zeroes. This is just like
886 // Output_data_fixed_space, except that the map name is known.
888 class Output_data_zero_fill
: public Output_section_data
891 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
892 : Output_section_data(data_size
, addralign
, true)
896 // There is no data to write out.
898 do_write(Output_file
*)
901 // Write to a map file.
903 do_print_to_mapfile(Mapfile
* mapfile
) const
904 { mapfile
->print_output_data(this, "** zero fill"); }
907 // A string table which goes into an output section.
909 class Output_data_strtab
: public Output_section_data
912 Output_data_strtab(Stringpool
* strtab
)
913 : Output_section_data(1), strtab_(strtab
)
917 // This is called to set the address and file offset. Here we make
918 // sure that the Stringpool is finalized.
920 set_final_data_size();
922 // Write out the data.
924 do_write(Output_file
*);
926 // Write the data to a buffer.
928 do_write_to_buffer(unsigned char* buffer
)
929 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
931 // Write to a map file.
933 do_print_to_mapfile(Mapfile
* mapfile
) const
934 { mapfile
->print_output_data(this, _("** string table")); }
940 // This POD class is used to represent a single reloc in the output
941 // file. This could be a private class within Output_data_reloc, but
942 // the templatization is complex enough that I broke it out into a
943 // separate class. The class is templatized on either elfcpp::SHT_REL
944 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
945 // relocation or an ordinary relocation.
947 // A relocation can be against a global symbol, a local symbol, a
948 // local section symbol, an output section, or the undefined symbol at
949 // index 0. We represent the latter by using a NULL global symbol.
951 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
954 template<bool dynamic
, int size
, bool big_endian
>
955 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
958 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
959 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
961 static const Address invalid_address
= static_cast<Address
>(0) - 1;
963 // An uninitialized entry. We need this because we want to put
964 // instances of this class into an STL container.
966 : local_sym_index_(INVALID_CODE
)
969 // We have a bunch of different constructors. They come in pairs
970 // depending on how the address of the relocation is specified. It
971 // can either be an offset in an Output_data or an offset in an
974 // A reloc against a global symbol.
976 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
977 Address address
, bool is_relative
, bool is_symbolless
);
979 Output_reloc(Symbol
* gsym
, unsigned int type
,
980 Sized_relobj
<size
, big_endian
>* relobj
,
981 unsigned int shndx
, Address address
, bool is_relative
,
984 // A reloc against a local symbol or local section symbol.
986 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
987 unsigned int local_sym_index
, unsigned int type
,
988 Output_data
* od
, Address address
, bool is_relative
,
989 bool is_symbolless
, bool is_section_symbol
);
991 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
992 unsigned int local_sym_index
, unsigned int type
,
993 unsigned int shndx
, Address address
, bool is_relative
,
994 bool is_symbolless
, bool is_section_symbol
);
996 // A reloc against the STT_SECTION symbol of an output section.
998 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1001 Output_reloc(Output_section
* os
, unsigned int type
,
1002 Sized_relobj
<size
, big_endian
>* relobj
,
1003 unsigned int shndx
, Address address
);
1005 // An absolute relocation with no symbol.
1007 Output_reloc(unsigned int type
, Output_data
* od
, Address address
);
1009 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1010 unsigned int shndx
, Address address
);
1012 // A target specific relocation. The target will be called to get
1013 // the symbol index, passing ARG. The type and offset will be set
1014 // as for other relocation types.
1016 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1019 Output_reloc(unsigned int type
, void* arg
,
1020 Sized_relobj
<size
, big_endian
>* relobj
,
1021 unsigned int shndx
, Address address
);
1023 // Return the reloc type.
1026 { return this->type_
; }
1028 // Return whether this is a RELATIVE relocation.
1031 { return this->is_relative_
; }
1033 // Return whether this is a relocation which should not use
1034 // a symbol, but which obtains its addend from a symbol.
1036 is_symbolless() const
1037 { return this->is_symbolless_
; }
1039 // Return whether this is against a local section symbol.
1041 is_local_section_symbol() const
1043 return (this->local_sym_index_
!= GSYM_CODE
1044 && this->local_sym_index_
!= SECTION_CODE
1045 && this->local_sym_index_
!= INVALID_CODE
1046 && this->local_sym_index_
!= TARGET_CODE
1047 && this->is_section_symbol_
);
1050 // Return whether this is a target specific relocation.
1052 is_target_specific() const
1053 { return this->local_sym_index_
== TARGET_CODE
; }
1055 // Return the argument to pass to the target for a target specific
1060 gold_assert(this->local_sym_index_
== TARGET_CODE
);
1061 return this->u1_
.arg
;
1064 // For a local section symbol, return the offset of the input
1065 // section within the output section. ADDEND is the addend being
1066 // applied to the input section.
1068 local_section_offset(Addend addend
) const;
1070 // Get the value of the symbol referred to by a Rel relocation when
1071 // we are adding the given ADDEND.
1073 symbol_value(Addend addend
) const;
1075 // Write the reloc entry to an output view.
1077 write(unsigned char* pov
) const;
1079 // Write the offset and info fields to Write_rel.
1080 template<typename Write_rel
>
1081 void write_rel(Write_rel
*) const;
1083 // This is used when sorting dynamic relocs. Return -1 to sort this
1084 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1086 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1089 // Return whether this reloc should be sorted before the argument
1090 // when sorting dynamic relocs.
1092 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1094 { return this->compare(r2
) < 0; }
1097 // Record that we need a dynamic symbol index.
1099 set_needs_dynsym_index();
1101 // Return the symbol index.
1103 get_symbol_index() const;
1105 // Return the output address.
1107 get_address() const;
1109 // Codes for local_sym_index_.
1118 // Invalid uninitialized entry.
1124 // For a local symbol or local section symbol
1125 // (this->local_sym_index_ >= 0), the object. We will never
1126 // generate a relocation against a local symbol in a dynamic
1127 // object; that doesn't make sense. And our callers will always
1128 // be templatized, so we use Sized_relobj here.
1129 Sized_relobj
<size
, big_endian
>* relobj
;
1130 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1131 // symbol. If this is NULL, it indicates a relocation against the
1132 // undefined 0 symbol.
1134 // For a relocation against an output section
1135 // (this->local_sym_index_ == SECTION_CODE), the output section.
1137 // For a target specific relocation, an argument to pass to the
1143 // If this->shndx_ is not INVALID CODE, the object which holds the
1144 // input section being used to specify the reloc address.
1145 Sized_relobj
<size
, big_endian
>* relobj
;
1146 // If this->shndx_ is INVALID_CODE, the output data being used to
1147 // specify the reloc address. This may be NULL if the reloc
1148 // address is absolute.
1151 // The address offset within the input section or the Output_data.
1153 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1154 // relocation against an output section, or TARGET_CODE for a target
1155 // specific relocation, or INVALID_CODE for an uninitialized value.
1156 // Otherwise, for a local symbol (this->is_section_symbol_ is
1157 // false), the local symbol index. For a local section symbol
1158 // (this->is_section_symbol_ is true), the section index in the
1160 unsigned int local_sym_index_
;
1161 // The reloc type--a processor specific code.
1162 unsigned int type_
: 29;
1163 // True if the relocation is a RELATIVE relocation.
1164 bool is_relative_
: 1;
1165 // True if the relocation is one which should not use
1166 // a symbol, but which obtains its addend from a symbol.
1167 bool is_symbolless_
: 1;
1168 // True if the relocation is against a section symbol.
1169 bool is_section_symbol_
: 1;
1170 // If the reloc address is an input section in an object, the
1171 // section index. This is INVALID_CODE if the reloc address is
1172 // specified in some other way.
1173 unsigned int shndx_
;
1176 // The SHT_RELA version of Output_reloc<>. This is just derived from
1177 // the SHT_REL version of Output_reloc, but it adds an addend.
1179 template<bool dynamic
, int size
, bool big_endian
>
1180 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1183 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1184 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1186 // An uninitialized entry.
1191 // A reloc against a global symbol.
1193 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1194 Address address
, Addend addend
, bool is_relative
,
1196 : rel_(gsym
, type
, od
, address
, is_relative
, is_symbolless
),
1200 Output_reloc(Symbol
* gsym
, unsigned int type
,
1201 Sized_relobj
<size
, big_endian
>* relobj
,
1202 unsigned int shndx
, Address address
, Addend addend
,
1203 bool is_relative
, bool is_symbolless
)
1204 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
,
1205 is_symbolless
), addend_(addend
)
1208 // A reloc against a local symbol.
1210 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1211 unsigned int local_sym_index
, unsigned int type
,
1212 Output_data
* od
, Address address
,
1213 Addend addend
, bool is_relative
,
1214 bool is_symbolless
, bool is_section_symbol
)
1215 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1216 is_symbolless
, is_section_symbol
),
1220 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1221 unsigned int local_sym_index
, unsigned int type
,
1222 unsigned int shndx
, Address address
,
1223 Addend addend
, bool is_relative
,
1224 bool is_symbolless
, bool is_section_symbol
)
1225 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1226 is_symbolless
, is_section_symbol
),
1230 // A reloc against the STT_SECTION symbol of an output section.
1232 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1233 Address address
, Addend addend
)
1234 : rel_(os
, type
, od
, address
), addend_(addend
)
1237 Output_reloc(Output_section
* os
, unsigned int type
,
1238 Sized_relobj
<size
, big_endian
>* relobj
,
1239 unsigned int shndx
, Address address
, Addend addend
)
1240 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1243 // An absolute relocation with no symbol.
1245 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1247 : rel_(type
, od
, address
), addend_(addend
)
1250 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1251 unsigned int shndx
, Address address
, Addend addend
)
1252 : rel_(type
, relobj
, shndx
, address
), addend_(addend
)
1255 // A target specific relocation. The target will be called to get
1256 // the symbol index and the addend, passing ARG. The type and
1257 // offset will be set as for other relocation types.
1259 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1260 Address address
, Addend addend
)
1261 : rel_(type
, arg
, od
, address
), addend_(addend
)
1264 Output_reloc(unsigned int type
, void* arg
,
1265 Sized_relobj
<size
, big_endian
>* relobj
,
1266 unsigned int shndx
, Address address
, Addend addend
)
1267 : rel_(type
, arg
, relobj
, shndx
, address
), addend_(addend
)
1270 // Return whether this is a RELATIVE relocation.
1273 { return this->rel_
.is_relative(); }
1275 // Return whether this is a relocation which should not use
1276 // a symbol, but which obtains its addend from a symbol.
1278 is_symbolless() const
1279 { return this->rel_
.is_symbolless(); }
1281 // Write the reloc entry to an output view.
1283 write(unsigned char* pov
) const;
1285 // Return whether this reloc should be sorted before the argument
1286 // when sorting dynamic relocs.
1288 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1291 int i
= this->rel_
.compare(r2
.rel_
);
1297 return this->addend_
< r2
.addend_
;
1302 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1307 // Output_data_reloc_generic is a non-template base class for
1308 // Output_data_reloc_base. This gives the generic code a way to hold
1309 // a pointer to a reloc section.
1311 class Output_data_reloc_generic
: public Output_section_data_build
1314 Output_data_reloc_generic(int size
, bool sort_relocs
)
1315 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1316 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1319 // Return the number of relative relocs in this section.
1321 relative_reloc_count() const
1322 { return this->relative_reloc_count_
; }
1324 // Whether we should sort the relocs.
1327 { return this->sort_relocs_
; }
1330 // Note that we've added another relative reloc.
1332 bump_relative_reloc_count()
1333 { ++this->relative_reloc_count_
; }
1336 // The number of relative relocs added to this section. This is to
1337 // support DT_RELCOUNT.
1338 size_t relative_reloc_count_
;
1339 // Whether to sort the relocations when writing them out, to make
1340 // the dynamic linker more efficient.
1344 // Output_data_reloc is used to manage a section containing relocs.
1345 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1346 // indicates whether this is a dynamic relocation or a normal
1347 // relocation. Output_data_reloc_base is a base class.
1348 // Output_data_reloc is the real class, which we specialize based on
1351 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1352 class Output_data_reloc_base
: public Output_data_reloc_generic
1355 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1356 typedef typename
Output_reloc_type::Address Address
;
1357 static const int reloc_size
=
1358 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1360 // Construct the section.
1361 Output_data_reloc_base(bool sort_relocs
)
1362 : Output_data_reloc_generic(size
, sort_relocs
)
1366 // Write out the data.
1368 do_write(Output_file
*);
1370 // Set the entry size and the link.
1372 do_adjust_output_section(Output_section
*os
);
1374 // Write to a map file.
1376 do_print_to_mapfile(Mapfile
* mapfile
) const
1378 mapfile
->print_output_data(this,
1380 ? _("** dynamic relocs")
1384 // Add a relocation entry.
1386 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1388 this->relocs_
.push_back(reloc
);
1389 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1390 od
->add_dynamic_reloc();
1391 if (reloc
.is_relative())
1392 this->bump_relative_reloc_count();
1396 typedef std::vector
<Output_reloc_type
> Relocs
;
1398 // The class used to sort the relocations.
1399 struct Sort_relocs_comparison
1402 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1403 { return r1
.sort_before(r2
); }
1406 // The relocations in this section.
1410 // The class which callers actually create.
1412 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1413 class Output_data_reloc
;
1415 // The SHT_REL version of Output_data_reloc.
1417 template<bool dynamic
, int size
, bool big_endian
>
1418 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1419 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1422 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1426 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1427 typedef typename
Output_reloc_type::Address Address
;
1429 Output_data_reloc(bool sr
)
1430 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1433 // Add a reloc against a global symbol.
1436 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1437 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, false)); }
1440 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1441 Sized_relobj
<size
, big_endian
>* relobj
,
1442 unsigned int shndx
, Address address
)
1443 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1446 // These are to simplify the Copy_relocs class.
1449 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1452 gold_assert(addend
== 0);
1453 this->add_global(gsym
, type
, od
, address
);
1457 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1458 Sized_relobj
<size
, big_endian
>* relobj
,
1459 unsigned int shndx
, Address address
, Address addend
)
1461 gold_assert(addend
== 0);
1462 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1465 // Add a RELATIVE reloc against a global symbol. The final relocation
1466 // will not reference the symbol.
1469 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1471 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true, true)); }
1474 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1475 Sized_relobj
<size
, big_endian
>* relobj
,
1476 unsigned int shndx
, Address address
)
1478 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1482 // Add a global relocation which does not use a symbol for the relocation,
1483 // but which gets its addend from a symbol.
1486 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1487 Output_data
* od
, Address address
)
1488 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, true)); }
1491 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1493 Sized_relobj
<size
, big_endian
>* relobj
,
1494 unsigned int shndx
, Address address
)
1496 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1500 // Add a reloc against a local symbol.
1503 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1504 unsigned int local_sym_index
, unsigned int type
,
1505 Output_data
* od
, Address address
)
1507 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1508 address
, false, false, false));
1512 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1513 unsigned int local_sym_index
, unsigned int type
,
1514 Output_data
* od
, unsigned int shndx
, Address address
)
1516 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1517 address
, false, false, false));
1520 // Add a RELATIVE reloc against a local symbol.
1523 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1524 unsigned int local_sym_index
, unsigned int type
,
1525 Output_data
* od
, Address address
)
1527 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1528 address
, true, true, false));
1532 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1533 unsigned int local_sym_index
, unsigned int type
,
1534 Output_data
* od
, unsigned int shndx
, Address address
)
1536 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1537 address
, true, true, false));
1540 // Add a local relocation which does not use a symbol for the relocation,
1541 // but which gets its addend from a symbol.
1544 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1545 unsigned int local_sym_index
, unsigned int type
,
1546 Output_data
* od
, Address address
)
1548 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1549 address
, false, true, false));
1553 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1554 unsigned int local_sym_index
, unsigned int type
,
1555 Output_data
* od
, unsigned int shndx
,
1558 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1559 address
, false, true, false));
1562 // Add a reloc against a local section symbol. This will be
1563 // converted into a reloc against the STT_SECTION symbol of the
1567 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1568 unsigned int input_shndx
, unsigned int type
,
1569 Output_data
* od
, Address address
)
1571 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1572 address
, false, false, true));
1576 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1577 unsigned int input_shndx
, unsigned int type
,
1578 Output_data
* od
, unsigned int shndx
, Address address
)
1580 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1581 address
, false, false, true));
1584 // A reloc against the STT_SECTION symbol of an output section.
1585 // OS is the Output_section that the relocation refers to; OD is
1586 // the Output_data object being relocated.
1589 add_output_section(Output_section
* os
, unsigned int type
,
1590 Output_data
* od
, Address address
)
1591 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1594 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1595 Sized_relobj
<size
, big_endian
>* relobj
,
1596 unsigned int shndx
, Address address
)
1597 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1599 // Add an absolute relocation.
1602 add_absolute(unsigned int type
, Output_data
* od
, Address address
)
1603 { this->add(od
, Output_reloc_type(type
, od
, address
)); }
1606 add_absolute(unsigned int type
, Output_data
* od
,
1607 Sized_relobj
<size
, big_endian
>* relobj
,
1608 unsigned int shndx
, Address address
)
1609 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
)); }
1611 // Add a target specific relocation. A target which calls this must
1612 // define the reloc_symbol_index and reloc_addend virtual functions.
1615 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1617 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
)); }
1620 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1621 Sized_relobj
<size
, big_endian
>* relobj
,
1622 unsigned int shndx
, Address address
)
1623 { this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
)); }
1626 // The SHT_RELA version of Output_data_reloc.
1628 template<bool dynamic
, int size
, bool big_endian
>
1629 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1630 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1633 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1637 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1638 typedef typename
Output_reloc_type::Address Address
;
1639 typedef typename
Output_reloc_type::Addend Addend
;
1641 Output_data_reloc(bool sr
)
1642 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1645 // Add a reloc against a global symbol.
1648 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1649 Address address
, Addend addend
)
1650 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1654 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1655 Sized_relobj
<size
, big_endian
>* relobj
,
1656 unsigned int shndx
, Address address
,
1658 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1659 addend
, false, false)); }
1661 // Add a RELATIVE reloc against a global symbol. The final output
1662 // relocation will not reference the symbol, but we must keep the symbol
1663 // information long enough to set the addend of the relocation correctly
1664 // when it is written.
1667 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1668 Address address
, Addend addend
)
1669 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true,
1673 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1674 Sized_relobj
<size
, big_endian
>* relobj
,
1675 unsigned int shndx
, Address address
, Addend addend
)
1676 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1677 addend
, true, true)); }
1679 // Add a global relocation which does not use a symbol for the relocation,
1680 // but which gets its addend from a symbol.
1683 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1684 Address address
, Addend addend
)
1685 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1689 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1691 Sized_relobj
<size
, big_endian
>* relobj
,
1692 unsigned int shndx
, Address address
, Addend addend
)
1693 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1694 addend
, false, true)); }
1696 // Add a reloc against a local symbol.
1699 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1700 unsigned int local_sym_index
, unsigned int type
,
1701 Output_data
* od
, Address address
, Addend addend
)
1703 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1704 addend
, false, false, false));
1708 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1709 unsigned int local_sym_index
, unsigned int type
,
1710 Output_data
* od
, unsigned int shndx
, Address address
,
1713 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1714 address
, addend
, false, false, false));
1717 // Add a RELATIVE reloc against a local symbol.
1720 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1721 unsigned int local_sym_index
, unsigned int type
,
1722 Output_data
* od
, Address address
, Addend addend
)
1724 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1725 addend
, true, true, false));
1729 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1730 unsigned int local_sym_index
, unsigned int type
,
1731 Output_data
* od
, unsigned int shndx
, Address address
,
1734 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1735 address
, addend
, true, true, false));
1738 // Add a local relocation which does not use a symbol for the relocation,
1739 // but which gets it's addend from a symbol.
1742 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1743 unsigned int local_sym_index
, unsigned int type
,
1744 Output_data
* od
, Address address
, Addend addend
)
1746 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1747 addend
, false, true, false));
1751 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1752 unsigned int local_sym_index
, unsigned int type
,
1753 Output_data
* od
, unsigned int shndx
,
1754 Address address
, Addend addend
)
1756 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1757 address
, addend
, false, true, false));
1760 // Add a reloc against a local section symbol. This will be
1761 // converted into a reloc against the STT_SECTION symbol of the
1765 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1766 unsigned int input_shndx
, unsigned int type
,
1767 Output_data
* od
, Address address
, Addend addend
)
1769 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1770 addend
, false, false, true));
1774 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1775 unsigned int input_shndx
, unsigned int type
,
1776 Output_data
* od
, unsigned int shndx
, Address address
,
1779 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1780 address
, addend
, false, false, true));
1783 // A reloc against the STT_SECTION symbol of an output section.
1786 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1787 Address address
, Addend addend
)
1788 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1791 add_output_section(Output_section
* os
, unsigned int type
,
1792 Sized_relobj
<size
, big_endian
>* relobj
,
1793 unsigned int shndx
, Address address
, Addend addend
)
1794 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1797 // Add an absolute relocation.
1800 add_absolute(unsigned int type
, Output_data
* od
, Address address
,
1802 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
)); }
1805 add_absolute(unsigned int type
, Output_data
* od
,
1806 Sized_relobj
<size
, big_endian
>* relobj
,
1807 unsigned int shndx
, Address address
, Addend addend
)
1808 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
)); }
1810 // Add a target specific relocation. A target which calls this must
1811 // define the reloc_symbol_index and reloc_addend virtual functions.
1814 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1815 Address address
, Addend addend
)
1816 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
, addend
)); }
1819 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1820 Sized_relobj
<size
, big_endian
>* relobj
,
1821 unsigned int shndx
, Address address
, Addend addend
)
1823 this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
,
1828 // Output_relocatable_relocs represents a relocation section in a
1829 // relocatable link. The actual data is written out in the target
1830 // hook relocate_for_relocatable. This just saves space for it.
1832 template<int sh_type
, int size
, bool big_endian
>
1833 class Output_relocatable_relocs
: public Output_section_data
1836 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1837 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1842 set_final_data_size();
1844 // Write out the data. There is nothing to do here.
1846 do_write(Output_file
*)
1849 // Write to a map file.
1851 do_print_to_mapfile(Mapfile
* mapfile
) const
1852 { mapfile
->print_output_data(this, _("** relocs")); }
1855 // The relocs associated with this input section.
1856 Relocatable_relocs
* rr_
;
1859 // Handle a GROUP section.
1861 template<int size
, bool big_endian
>
1862 class Output_data_group
: public Output_section_data
1865 // The constructor clears *INPUT_SHNDXES.
1866 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1867 section_size_type entry_count
,
1868 elfcpp::Elf_Word flags
,
1869 std::vector
<unsigned int>* input_shndxes
);
1872 do_write(Output_file
*);
1874 // Write to a map file.
1876 do_print_to_mapfile(Mapfile
* mapfile
) const
1877 { mapfile
->print_output_data(this, _("** group")); }
1879 // Set final data size.
1881 set_final_data_size()
1882 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1885 // The input object.
1886 Sized_relobj
<size
, big_endian
>* relobj_
;
1887 // The group flag word.
1888 elfcpp::Elf_Word flags_
;
1889 // The section indexes of the input sections in this group.
1890 std::vector
<unsigned int> input_shndxes_
;
1893 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1894 // for one symbol--either a global symbol or a local symbol in an
1895 // object. The target specific code adds entries to the GOT as
1898 template<int size
, bool big_endian
>
1899 class Output_data_got
: public Output_section_data_build
1902 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1903 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1904 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1907 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1911 // Add an entry for a global symbol to the GOT. Return true if this
1912 // is a new GOT entry, false if the symbol was already in the GOT.
1914 add_global(Symbol
* gsym
, unsigned int got_type
);
1916 // Add an entry for a global symbol to the GOT, and add a dynamic
1917 // relocation of type R_TYPE for the GOT entry.
1919 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1920 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1923 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1924 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1926 // Add a pair of entries for a global symbol to the GOT, and add
1927 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1929 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1930 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1931 unsigned int r_type_2
);
1934 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1935 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1936 unsigned int r_type_2
);
1938 // Add an entry for a local symbol to the GOT. This returns true if
1939 // this is a new GOT entry, false if the symbol already has a GOT
1942 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1943 unsigned int got_type
);
1945 // Add an entry for a local symbol to the GOT, and add a dynamic
1946 // relocation of type R_TYPE for the GOT entry.
1948 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1949 unsigned int sym_index
, unsigned int got_type
,
1950 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1953 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1954 unsigned int sym_index
, unsigned int got_type
,
1955 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1957 // Add a pair of entries for a local symbol to the GOT, and add
1958 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1960 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1961 unsigned int sym_index
, unsigned int shndx
,
1962 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1963 unsigned int r_type_1
, unsigned int r_type_2
);
1966 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1967 unsigned int sym_index
, unsigned int shndx
,
1968 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1969 unsigned int r_type_1
, unsigned int r_type_2
);
1971 // Add a constant to the GOT. This returns the offset of the new
1972 // entry from the start of the GOT.
1974 add_constant(Valtype constant
)
1976 this->entries_
.push_back(Got_entry(constant
));
1977 this->set_got_size();
1978 return this->last_got_offset();
1982 // Write out the GOT table.
1984 do_write(Output_file
*);
1986 // Write to a map file.
1988 do_print_to_mapfile(Mapfile
* mapfile
) const
1989 { mapfile
->print_output_data(this, _("** GOT")); }
1992 // This POD class holds a single GOT entry.
1996 // Create a zero entry.
1998 : local_sym_index_(CONSTANT_CODE
)
1999 { this->u_
.constant
= 0; }
2001 // Create a global symbol entry.
2002 explicit Got_entry(Symbol
* gsym
)
2003 : local_sym_index_(GSYM_CODE
)
2004 { this->u_
.gsym
= gsym
; }
2006 // Create a local symbol entry.
2007 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
2008 unsigned int local_sym_index
)
2009 : local_sym_index_(local_sym_index
)
2011 gold_assert(local_sym_index
!= GSYM_CODE
2012 && local_sym_index
!= CONSTANT_CODE
);
2013 this->u_
.object
= object
;
2016 // Create a constant entry. The constant is a host value--it will
2017 // be swapped, if necessary, when it is written out.
2018 explicit Got_entry(Valtype constant
)
2019 : local_sym_index_(CONSTANT_CODE
)
2020 { this->u_
.constant
= constant
; }
2022 // Write the GOT entry to an output view.
2024 write(unsigned char* pov
) const;
2035 // For a local symbol, the object.
2036 Sized_relobj
<size
, big_endian
>* object
;
2037 // For a global symbol, the symbol.
2039 // For a constant, the constant.
2042 // For a local symbol, the local symbol index. This is GSYM_CODE
2043 // for a global symbol, or CONSTANT_CODE for a constant.
2044 unsigned int local_sym_index_
;
2047 typedef std::vector
<Got_entry
> Got_entries
;
2049 // Return the offset into the GOT of GOT entry I.
2051 got_offset(unsigned int i
) const
2052 { return i
* (size
/ 8); }
2054 // Return the offset into the GOT of the last entry added.
2056 last_got_offset() const
2057 { return this->got_offset(this->entries_
.size() - 1); }
2059 // Set the size of the section.
2062 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
2064 // The list of GOT entries.
2065 Got_entries entries_
;
2068 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2071 class Output_data_dynamic
: public Output_section_data
2074 Output_data_dynamic(Stringpool
* pool
)
2075 : Output_section_data(Output_data::default_alignment()),
2076 entries_(), pool_(pool
)
2079 // Add a new dynamic entry with a fixed numeric value.
2081 add_constant(elfcpp::DT tag
, unsigned int val
)
2082 { this->add_entry(Dynamic_entry(tag
, val
)); }
2084 // Add a new dynamic entry with the address of output data.
2086 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
2087 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
2089 // Add a new dynamic entry with the address of output data
2090 // plus a constant offset.
2092 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
2093 unsigned int offset
)
2094 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
2096 // Add a new dynamic entry with the size of output data.
2098 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
2099 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
2101 // Add a new dynamic entry with the total size of two output datas.
2103 add_section_size(elfcpp::DT tag
, const Output_data
* od
,
2104 const Output_data
* od2
)
2105 { this->add_entry(Dynamic_entry(tag
, od
, od2
)); }
2107 // Add a new dynamic entry with the address of a symbol.
2109 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
2110 { this->add_entry(Dynamic_entry(tag
, sym
)); }
2112 // Add a new dynamic entry with a string.
2114 add_string(elfcpp::DT tag
, const char* str
)
2115 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
2118 add_string(elfcpp::DT tag
, const std::string
& str
)
2119 { this->add_string(tag
, str
.c_str()); }
2122 // Adjust the output section to set the entry size.
2124 do_adjust_output_section(Output_section
*);
2126 // Set the final data size.
2128 set_final_data_size();
2130 // Write out the dynamic entries.
2132 do_write(Output_file
*);
2134 // Write to a map file.
2136 do_print_to_mapfile(Mapfile
* mapfile
) const
2137 { mapfile
->print_output_data(this, _("** dynamic")); }
2140 // This POD class holds a single dynamic entry.
2144 // Create an entry with a fixed numeric value.
2145 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
2146 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
2147 { this->u_
.val
= val
; }
2149 // Create an entry with the size or address of a section.
2150 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
2152 offset_(section_size
2153 ? DYNAMIC_SECTION_SIZE
2154 : DYNAMIC_SECTION_ADDRESS
)
2160 // Create an entry with the size of two sections.
2161 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, const Output_data
* od2
)
2163 offset_(DYNAMIC_SECTION_SIZE
)
2169 // Create an entry with the address of a section plus a constant offset.
2170 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
2173 { this->u_
.od
= od
; }
2175 // Create an entry with the address of a symbol.
2176 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
2177 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
2178 { this->u_
.sym
= sym
; }
2180 // Create an entry with a string.
2181 Dynamic_entry(elfcpp::DT tag
, const char* str
)
2182 : tag_(tag
), offset_(DYNAMIC_STRING
)
2183 { this->u_
.str
= str
; }
2185 // Return the tag of this entry.
2188 { return this->tag_
; }
2190 // Write the dynamic entry to an output view.
2191 template<int size
, bool big_endian
>
2193 write(unsigned char* pov
, const Stringpool
*) const;
2196 // Classification is encoded in the OFFSET field.
2200 DYNAMIC_SECTION_ADDRESS
= 0,
2202 DYNAMIC_NUMBER
= -1U,
2204 DYNAMIC_SECTION_SIZE
= -2U,
2206 DYNAMIC_SYMBOL
= -3U,
2208 DYNAMIC_STRING
= -4U
2209 // Any other value indicates a section address plus OFFSET.
2214 // For DYNAMIC_NUMBER.
2216 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2217 const Output_data
* od
;
2218 // For DYNAMIC_SYMBOL.
2220 // For DYNAMIC_STRING.
2223 // For DYNAMIC_SYMBOL with two sections.
2224 const Output_data
* od2
;
2227 // The type of entry (Classification) or offset within a section.
2228 unsigned int offset_
;
2231 // Add an entry to the list.
2233 add_entry(const Dynamic_entry
& entry
)
2234 { this->entries_
.push_back(entry
); }
2236 // Sized version of write function.
2237 template<int size
, bool big_endian
>
2239 sized_write(Output_file
* of
);
2241 // The type of the list of entries.
2242 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2245 Dynamic_entries entries_
;
2246 // The pool used for strings.
2250 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2251 // which may be required if the object file has more than
2252 // SHN_LORESERVE sections.
2254 class Output_symtab_xindex
: public Output_section_data
2257 Output_symtab_xindex(size_t symcount
)
2258 : Output_section_data(symcount
* 4, 4, true),
2262 // Add an entry: symbol number SYMNDX has section SHNDX.
2264 add(unsigned int symndx
, unsigned int shndx
)
2265 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2269 do_write(Output_file
*);
2271 // Write to a map file.
2273 do_print_to_mapfile(Mapfile
* mapfile
) const
2274 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2277 template<bool big_endian
>
2279 endian_do_write(unsigned char*);
2281 // It is likely that most symbols will not require entries. Rather
2282 // than keep a vector for all symbols, we keep pairs of symbol index
2283 // and section index.
2284 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2286 // The entries we need.
2287 Xindex_entries entries_
;
2290 // A relaxed input section.
2291 class Output_relaxed_input_section
: public Output_section_data_build
2294 // We would like to call relobj->section_addralign(shndx) to get the
2295 // alignment but we do not want the constructor to fail. So callers
2296 // are repsonsible for ensuring that.
2297 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2299 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2302 // Return the Relobj of this relaxed input section.
2305 { return this->relobj_
; }
2307 // Return the section index of this relaxed input section.
2310 { return this->shndx_
; }
2314 unsigned int shndx_
;
2317 // An output section. We don't expect to have too many output
2318 // sections, so we don't bother to do a template on the size.
2320 class Output_section
: public Output_data
2323 // Create an output section, giving the name, type, and flags.
2324 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2325 virtual ~Output_section();
2327 // Add a new input section SHNDX, named NAME, with header SHDR, from
2328 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2329 // which applies to this section, or 0 if none, or -1 if more than
2330 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2331 // in a linker script; in that case we need to keep track of input
2332 // sections associated with an output section. Return the offset
2333 // within the output section.
2334 template<int size
, bool big_endian
>
2336 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
2338 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2339 unsigned int reloc_shndx
, bool have_sections_script
);
2341 // Add generated data POSD to this output section.
2343 add_output_section_data(Output_section_data
* posd
);
2345 // Add a relaxed input section PORIS to this output section.
2347 add_relaxed_input_section(Output_relaxed_input_section
* poris
);
2349 // Return the section name.
2352 { return this->name_
; }
2354 // Return the section type.
2357 { return this->type_
; }
2359 // Return the section flags.
2362 { return this->flags_
; }
2364 // Update the output section flags based on input section flags.
2366 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2368 // Return the entsize field.
2371 { return this->entsize_
; }
2373 // Set the entsize field.
2375 set_entsize(uint64_t v
);
2377 // Set the load address.
2379 set_load_address(uint64_t load_address
)
2381 this->load_address_
= load_address
;
2382 this->has_load_address_
= true;
2385 // Set the link field to the output section index of a section.
2387 set_link_section(const Output_data
* od
)
2389 gold_assert(this->link_
== 0
2390 && !this->should_link_to_symtab_
2391 && !this->should_link_to_dynsym_
);
2392 this->link_section_
= od
;
2395 // Set the link field to a constant.
2397 set_link(unsigned int v
)
2399 gold_assert(this->link_section_
== NULL
2400 && !this->should_link_to_symtab_
2401 && !this->should_link_to_dynsym_
);
2405 // Record that this section should link to the normal symbol table.
2407 set_should_link_to_symtab()
2409 gold_assert(this->link_section_
== NULL
2411 && !this->should_link_to_dynsym_
);
2412 this->should_link_to_symtab_
= true;
2415 // Record that this section should link to the dynamic symbol table.
2417 set_should_link_to_dynsym()
2419 gold_assert(this->link_section_
== NULL
2421 && !this->should_link_to_symtab_
);
2422 this->should_link_to_dynsym_
= true;
2425 // Return the info field.
2429 gold_assert(this->info_section_
== NULL
2430 && this->info_symndx_
== NULL
);
2434 // Set the info field to the output section index of a section.
2436 set_info_section(const Output_section
* os
)
2438 gold_assert((this->info_section_
== NULL
2439 || (this->info_section_
== os
2440 && this->info_uses_section_index_
))
2441 && this->info_symndx_
== NULL
2442 && this->info_
== 0);
2443 this->info_section_
= os
;
2444 this->info_uses_section_index_
= true;
2447 // Set the info field to the symbol table index of a symbol.
2449 set_info_symndx(const Symbol
* sym
)
2451 gold_assert(this->info_section_
== NULL
2452 && (this->info_symndx_
== NULL
2453 || this->info_symndx_
== sym
)
2454 && this->info_
== 0);
2455 this->info_symndx_
= sym
;
2458 // Set the info field to the symbol table index of a section symbol.
2460 set_info_section_symndx(const Output_section
* os
)
2462 gold_assert((this->info_section_
== NULL
2463 || (this->info_section_
== os
2464 && !this->info_uses_section_index_
))
2465 && this->info_symndx_
== NULL
2466 && this->info_
== 0);
2467 this->info_section_
= os
;
2468 this->info_uses_section_index_
= false;
2471 // Set the info field to a constant.
2473 set_info(unsigned int v
)
2475 gold_assert(this->info_section_
== NULL
2476 && this->info_symndx_
== NULL
2477 && (this->info_
== 0
2478 || this->info_
== v
));
2482 // Set the addralign field.
2484 set_addralign(uint64_t v
)
2485 { this->addralign_
= v
; }
2487 // Whether the output section index has been set.
2489 has_out_shndx() const
2490 { return this->out_shndx_
!= -1U; }
2492 // Indicate that we need a symtab index.
2494 set_needs_symtab_index()
2495 { this->needs_symtab_index_
= true; }
2497 // Return whether we need a symtab index.
2499 needs_symtab_index() const
2500 { return this->needs_symtab_index_
; }
2502 // Get the symtab index.
2504 symtab_index() const
2506 gold_assert(this->symtab_index_
!= 0);
2507 return this->symtab_index_
;
2510 // Set the symtab index.
2512 set_symtab_index(unsigned int index
)
2514 gold_assert(index
!= 0);
2515 this->symtab_index_
= index
;
2518 // Indicate that we need a dynsym index.
2520 set_needs_dynsym_index()
2521 { this->needs_dynsym_index_
= true; }
2523 // Return whether we need a dynsym index.
2525 needs_dynsym_index() const
2526 { return this->needs_dynsym_index_
; }
2528 // Get the dynsym index.
2530 dynsym_index() const
2532 gold_assert(this->dynsym_index_
!= 0);
2533 return this->dynsym_index_
;
2536 // Set the dynsym index.
2538 set_dynsym_index(unsigned int index
)
2540 gold_assert(index
!= 0);
2541 this->dynsym_index_
= index
;
2544 // Return whether the input sections sections attachd to this output
2545 // section may require sorting. This is used to handle constructor
2546 // priorities compatibly with GNU ld.
2548 may_sort_attached_input_sections() const
2549 { return this->may_sort_attached_input_sections_
; }
2551 // Record that the input sections attached to this output section
2552 // may require sorting.
2554 set_may_sort_attached_input_sections()
2555 { this->may_sort_attached_input_sections_
= true; }
2557 // Return whether the input sections attached to this output section
2558 // require sorting. This is used to handle constructor priorities
2559 // compatibly with GNU ld.
2561 must_sort_attached_input_sections() const
2562 { return this->must_sort_attached_input_sections_
; }
2564 // Record that the input sections attached to this output section
2567 set_must_sort_attached_input_sections()
2568 { this->must_sort_attached_input_sections_
= true; }
2570 // Return whether this section holds relro data--data which has
2571 // dynamic relocations but which may be marked read-only after the
2572 // dynamic relocations have been completed.
2575 { return this->is_relro_
; }
2577 // Record that this section holds relro data.
2580 { this->is_relro_
= true; }
2582 // Record that this section does not hold relro data.
2585 { this->is_relro_
= false; }
2587 // True if this section holds relro local data--relro data for which
2588 // the dynamic relocations are all RELATIVE relocations.
2590 is_relro_local() const
2591 { return this->is_relro_local_
; }
2593 // Record that this section holds relro local data.
2595 set_is_relro_local()
2596 { this->is_relro_local_
= true; }
2598 // True if this must be the last relro section.
2600 is_last_relro() const
2601 { return this->is_last_relro_
; }
2603 // Record that this must be the last relro section.
2607 gold_assert(this->is_relro_
);
2608 this->is_last_relro_
= true;
2611 // True if this must be the first section following the relro sections.
2613 is_first_non_relro() const
2615 gold_assert(!this->is_relro_
);
2616 return this->is_first_non_relro_
;
2619 // Record that this must be the first non-relro section.
2621 set_is_first_non_relro()
2623 gold_assert(!this->is_relro_
);
2624 this->is_first_non_relro_
= true;
2627 // True if this is a small section: a section which holds small
2630 is_small_section() const
2631 { return this->is_small_section_
; }
2633 // Record that this is a small section.
2635 set_is_small_section()
2636 { this->is_small_section_
= true; }
2638 // True if this is a large section: a section which holds large
2641 is_large_section() const
2642 { return this->is_large_section_
; }
2644 // Record that this is a large section.
2646 set_is_large_section()
2647 { this->is_large_section_
= true; }
2649 // True if this is a large data (not BSS) section.
2651 is_large_data_section()
2652 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2654 // True if this is the .interp section which goes into the PT_INTERP
2658 { return this->is_interp_
; }
2660 // Record that this is the interp section.
2663 { this->is_interp_
= true; }
2665 // True if this is a section used by the dynamic linker.
2667 is_dynamic_linker_section() const
2668 { return this->is_dynamic_linker_section_
; }
2670 // Record that this is a section used by the dynamic linker.
2672 set_is_dynamic_linker_section()
2673 { this->is_dynamic_linker_section_
= true; }
2675 // Return whether this section should be written after all the input
2676 // sections are complete.
2678 after_input_sections() const
2679 { return this->after_input_sections_
; }
2681 // Record that this section should be written after all the input
2682 // sections are complete.
2684 set_after_input_sections()
2685 { this->after_input_sections_
= true; }
2687 // Return whether this section requires postprocessing after all
2688 // relocations have been applied.
2690 requires_postprocessing() const
2691 { return this->requires_postprocessing_
; }
2693 // If a section requires postprocessing, return the buffer to use.
2695 postprocessing_buffer() const
2697 gold_assert(this->postprocessing_buffer_
!= NULL
);
2698 return this->postprocessing_buffer_
;
2701 // If a section requires postprocessing, create the buffer to use.
2703 create_postprocessing_buffer();
2705 // If a section requires postprocessing, this is the size of the
2706 // buffer to which relocations should be applied.
2708 postprocessing_buffer_size() const
2709 { return this->current_data_size_for_child(); }
2711 // Modify the section name. This is only permitted for an
2712 // unallocated section, and only before the size has been finalized.
2713 // Otherwise the name will not get into Layout::namepool_.
2715 set_name(const char* newname
)
2717 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2718 gold_assert(!this->is_data_size_valid());
2719 this->name_
= newname
;
2722 // Return whether the offset OFFSET in the input section SHNDX in
2723 // object OBJECT is being included in the link.
2725 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2726 off_t offset
) const;
2728 // Return the offset within the output section of OFFSET relative to
2729 // the start of input section SHNDX in object OBJECT.
2731 output_offset(const Relobj
* object
, unsigned int shndx
,
2732 section_offset_type offset
) const;
2734 // Return the output virtual address of OFFSET relative to the start
2735 // of input section SHNDX in object OBJECT.
2737 output_address(const Relobj
* object
, unsigned int shndx
,
2738 off_t offset
) const;
2740 // Look for the merged section for input section SHNDX in object
2741 // OBJECT. If found, return true, and set *ADDR to the address of
2742 // the start of the merged section. This is not necessary the
2743 // output offset corresponding to input offset 0 in the section,
2744 // since the section may be mapped arbitrarily.
2746 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2747 uint64_t* addr
) const;
2749 // Record that this output section was found in the SECTIONS clause
2750 // of a linker script.
2752 set_found_in_sections_clause()
2753 { this->found_in_sections_clause_
= true; }
2755 // Return whether this output section was found in the SECTIONS
2756 // clause of a linker script.
2758 found_in_sections_clause() const
2759 { return this->found_in_sections_clause_
; }
2761 // Write the section header into *OPHDR.
2762 template<int size
, bool big_endian
>
2764 write_header(const Layout
*, const Stringpool
*,
2765 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2767 // The next few calls are for linker script support.
2769 // We need to export the input sections to linker scripts. Previously
2770 // we export a pair of Relobj pointer and section index. We now need to
2771 // handle relaxed input sections as well. So we use this class.
2772 class Simple_input_section
2775 static const unsigned int invalid_shndx
= static_cast<unsigned int>(-1);
2778 Simple_input_section(Relobj
*relobj
, unsigned int shndx
)
2781 gold_assert(shndx
!= invalid_shndx
);
2782 this->u_
.relobj
= relobj
;
2785 Simple_input_section(Output_relaxed_input_section
* section
)
2786 : shndx_(invalid_shndx
)
2787 { this->u_
.relaxed_input_section
= section
; }
2789 // Whether this is a relaxed section.
2791 is_relaxed_input_section() const
2792 { return this->shndx_
== invalid_shndx
; }
2794 // Return object of an input section.
2798 return ((this->shndx_
!= invalid_shndx
)
2800 : this->u_
.relaxed_input_section
->relobj());
2803 // Return index of an input section.
2807 return ((this->shndx_
!= invalid_shndx
)
2809 : this->u_
.relaxed_input_section
->shndx());
2812 // Return the Output_relaxed_input_section object of a relaxed section.
2813 Output_relaxed_input_section
*
2814 relaxed_input_section() const
2816 gold_assert(this->shndx_
== invalid_shndx
);
2817 return this->u_
.relaxed_input_section
;
2821 // Pointer to either an Relobj or an Output_relaxed_input_section.
2825 Output_relaxed_input_section
* relaxed_input_section
;
2827 // Section index for an non-relaxed section or invalid_shndx for
2828 // a relaxed section.
2829 unsigned int shndx_
;
2832 // Store the list of input sections for this Output_section into the
2833 // list passed in. This removes the input sections, leaving only
2834 // any Output_section_data elements. This returns the size of those
2835 // Output_section_data elements. ADDRESS is the address of this
2836 // output section. FILL is the fill value to use, in case there are
2837 // any spaces between the remaining Output_section_data elements.
2839 get_input_sections(uint64_t address
, const std::string
& fill
,
2840 std::list
<Simple_input_section
>*);
2842 // Add a simple input section.
2844 add_simple_input_section(const Simple_input_section
& input_section
,
2845 off_t data_size
, uint64_t addralign
);
2847 // Set the current size of the output section.
2849 set_current_data_size(off_t size
)
2850 { this->set_current_data_size_for_child(size
); }
2852 // Get the current size of the output section.
2854 current_data_size() const
2855 { return this->current_data_size_for_child(); }
2857 // End of linker script support.
2859 // Save states before doing section layout.
2860 // This is used for relaxation.
2864 // Restore states prior to section layout.
2872 // Convert existing input sections to relaxed input sections.
2874 convert_input_sections_to_relaxed_sections(
2875 const std::vector
<Output_relaxed_input_section
*>& sections
);
2877 // Find a relaxed input section to an input section in OBJECT
2878 // with index SHNDX. Return NULL if none is found.
2879 const Output_relaxed_input_section
*
2880 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
2882 // Whether section offsets need adjustment due to relaxation.
2884 section_offsets_need_adjustment() const
2885 { return this->section_offsets_need_adjustment_
; }
2887 // Set section_offsets_need_adjustment to be true.
2889 set_section_offsets_need_adjustment()
2890 { this->section_offsets_need_adjustment_
= true; }
2892 // Adjust section offsets of input sections in this. This is
2893 // requires if relaxation caused some input sections to change sizes.
2895 adjust_section_offsets();
2897 // Print merge statistics to stderr.
2899 print_merge_stats();
2902 // Return the output section--i.e., the object itself.
2907 const Output_section
*
2908 do_output_section() const
2911 // Return the section index in the output file.
2913 do_out_shndx() const
2915 gold_assert(this->out_shndx_
!= -1U);
2916 return this->out_shndx_
;
2919 // Set the output section index.
2921 do_set_out_shndx(unsigned int shndx
)
2923 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2924 this->out_shndx_
= shndx
;
2927 // Set the final data size of the Output_section. For a typical
2928 // Output_section, there is nothing to do, but if there are any
2929 // Output_section_data objects we need to set their final addresses
2932 set_final_data_size();
2934 // Reset the address and file offset.
2936 do_reset_address_and_file_offset();
2938 // Return true if address and file offset already have reset values. In
2939 // other words, calling reset_address_and_file_offset will not change them.
2941 do_address_and_file_offset_have_reset_values() const;
2943 // Write the data to the file. For a typical Output_section, this
2944 // does nothing: the data is written out by calling Object::Relocate
2945 // on each input object. But if there are any Output_section_data
2946 // objects we do need to write them out here.
2948 do_write(Output_file
*);
2950 // Return the address alignment--function required by parent class.
2952 do_addralign() const
2953 { return this->addralign_
; }
2955 // Return whether there is a load address.
2957 do_has_load_address() const
2958 { return this->has_load_address_
; }
2960 // Return the load address.
2962 do_load_address() const
2964 gold_assert(this->has_load_address_
);
2965 return this->load_address_
;
2968 // Return whether this is an Output_section.
2970 do_is_section() const
2973 // Return whether this is a section of the specified type.
2975 do_is_section_type(elfcpp::Elf_Word type
) const
2976 { return this->type_
== type
; }
2978 // Return whether the specified section flag is set.
2980 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2981 { return (this->flags_
& flag
) != 0; }
2983 // Set the TLS offset. Called only for SHT_TLS sections.
2985 do_set_tls_offset(uint64_t tls_base
);
2987 // Return the TLS offset, relative to the base of the TLS segment.
2988 // Valid only for SHT_TLS sections.
2990 do_tls_offset() const
2991 { return this->tls_offset_
; }
2993 // This may be implemented by a child class.
2995 do_finalize_name(Layout
*)
2998 // Print to the map file.
3000 do_print_to_mapfile(Mapfile
*) const;
3002 // Record that this section requires postprocessing after all
3003 // relocations have been applied. This is called by a child class.
3005 set_requires_postprocessing()
3007 this->requires_postprocessing_
= true;
3008 this->after_input_sections_
= true;
3011 // Write all the data of an Output_section into the postprocessing
3014 write_to_postprocessing_buffer();
3016 // In some cases we need to keep a list of the input sections
3017 // associated with this output section. We only need the list if we
3018 // might have to change the offsets of the input section within the
3019 // output section after we add the input section. The ordinary
3020 // input sections will be written out when we process the object
3021 // file, and as such we don't need to track them here. We do need
3022 // to track Output_section_data objects here. We store instances of
3023 // this structure in a std::vector, so it must be a POD. There can
3024 // be many instances of this structure, so we use a union to save
3030 : shndx_(0), p2align_(0)
3032 this->u1_
.data_size
= 0;
3033 this->u2_
.object
= NULL
;
3036 // For an ordinary input section.
3037 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
3040 p2align_(ffsll(static_cast<long long>(addralign
)))
3042 gold_assert(shndx
!= OUTPUT_SECTION_CODE
3043 && shndx
!= MERGE_DATA_SECTION_CODE
3044 && shndx
!= MERGE_STRING_SECTION_CODE
3045 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
3046 this->u1_
.data_size
= data_size
;
3047 this->u2_
.object
= object
;
3050 // For a non-merge output section.
3051 Input_section(Output_section_data
* posd
)
3052 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0)
3054 this->u1_
.data_size
= 0;
3055 this->u2_
.posd
= posd
;
3058 // For a merge section.
3059 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
3061 ? MERGE_STRING_SECTION_CODE
3062 : MERGE_DATA_SECTION_CODE
),
3065 this->u1_
.entsize
= entsize
;
3066 this->u2_
.posd
= posd
;
3069 // For a relaxed input section.
3070 Input_section(Output_relaxed_input_section
*psection
)
3071 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0)
3073 this->u1_
.data_size
= 0;
3074 this->u2_
.poris
= psection
;
3077 // The required alignment.
3081 if (!this->is_input_section())
3082 return this->u2_
.posd
->addralign();
3083 return (this->p2align_
== 0
3085 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
3088 // Return the required size.
3092 // Whether this is an input section.
3094 is_input_section() const
3096 return (this->shndx_
!= OUTPUT_SECTION_CODE
3097 && this->shndx_
!= MERGE_DATA_SECTION_CODE
3098 && this->shndx_
!= MERGE_STRING_SECTION_CODE
3099 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
3102 // Return whether this is a merge section which matches the
3105 is_merge_section(bool is_string
, uint64_t entsize
,
3106 uint64_t addralign
) const
3108 return (this->shndx_
== (is_string
3109 ? MERGE_STRING_SECTION_CODE
3110 : MERGE_DATA_SECTION_CODE
)
3111 && this->u1_
.entsize
== entsize
3112 && this->addralign() == addralign
);
3115 // Return whether this is a relaxed input section.
3117 is_relaxed_input_section() const
3118 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
3120 // Return whether this is a generic Output_section_data.
3122 is_output_section_data() const
3124 return this->shndx_
== OUTPUT_SECTION_CODE
;
3127 // Return the object for an input section.
3131 if (this->is_input_section())
3132 return this->u2_
.object
;
3133 else if (this->is_relaxed_input_section())
3134 return this->u2_
.poris
->relobj();
3139 // Return the input section index for an input section.
3143 if (this->is_input_section())
3144 return this->shndx_
;
3145 else if (this->is_relaxed_input_section())
3146 return this->u2_
.poris
->shndx();
3151 // For non-input-sections, return the associated Output_section_data
3153 Output_section_data
*
3154 output_section_data() const
3156 gold_assert(!this->is_input_section());
3157 return this->u2_
.posd
;
3160 // Return the Output_relaxed_input_section object.
3161 Output_relaxed_input_section
*
3162 relaxed_input_section() const
3164 gold_assert(this->is_relaxed_input_section());
3165 return this->u2_
.poris
;
3168 // Set the output section.
3170 set_output_section(Output_section
* os
)
3172 gold_assert(!this->is_input_section());
3173 Output_section_data
*posd
=
3174 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
3175 posd
->set_output_section(os
);
3178 // Set the address and file offset. This is called during
3179 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3180 // the enclosing section.
3182 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
3183 off_t section_file_offset
);
3185 // Reset the address and file offset.
3187 reset_address_and_file_offset();
3189 // Finalize the data size.
3191 finalize_data_size();
3193 // Add an input section, for SHF_MERGE sections.
3195 add_input_section(Relobj
* object
, unsigned int shndx
)
3197 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
3198 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3199 return this->u2_
.posd
->add_input_section(object
, shndx
);
3202 // Given an input OBJECT, an input section index SHNDX within that
3203 // object, and an OFFSET relative to the start of that input
3204 // section, return whether or not the output offset is known. If
3205 // this function returns true, it sets *POUTPUT to the offset in
3206 // the output section, relative to the start of the input section
3207 // in the output section. *POUTPUT may be different from OFFSET
3208 // for a merged section.
3210 output_offset(const Relobj
* object
, unsigned int shndx
,
3211 section_offset_type offset
,
3212 section_offset_type
*poutput
) const;
3214 // Return whether this is the merge section for the input section
3217 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3219 // Write out the data. This does nothing for an input section.
3221 write(Output_file
*);
3223 // Write the data to a buffer. This does nothing for an input
3226 write_to_buffer(unsigned char*);
3228 // Print to a map file.
3230 print_to_mapfile(Mapfile
*) const;
3232 // Print statistics about merge sections to stderr.
3234 print_merge_stats(const char* section_name
)
3236 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3237 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3238 this->u2_
.posd
->print_merge_stats(section_name
);
3242 // Code values which appear in shndx_. If the value is not one of
3243 // these codes, it is the input section index in the object file.
3246 // An Output_section_data.
3247 OUTPUT_SECTION_CODE
= -1U,
3248 // An Output_section_data for an SHF_MERGE section with
3249 // SHF_STRINGS not set.
3250 MERGE_DATA_SECTION_CODE
= -2U,
3251 // An Output_section_data for an SHF_MERGE section with
3253 MERGE_STRING_SECTION_CODE
= -3U,
3254 // An Output_section_data for a relaxed input section.
3255 RELAXED_INPUT_SECTION_CODE
= -4U
3258 // For an ordinary input section, this is the section index in the
3259 // input file. For an Output_section_data, this is
3260 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3261 // MERGE_STRING_SECTION_CODE.
3262 unsigned int shndx_
;
3263 // The required alignment, stored as a power of 2.
3264 unsigned int p2align_
;
3267 // For an ordinary input section, the section size.
3269 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3270 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3276 // For an ordinary input section, the object which holds the
3279 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3280 // MERGE_STRING_SECTION_CODE, the data.
3281 Output_section_data
* posd
;
3282 // For RELAXED_INPUT_SECTION_CODE, the data.
3283 Output_relaxed_input_section
* poris
;
3287 typedef std::vector
<Input_section
> Input_section_list
;
3289 // Allow a child class to access the input sections.
3290 const Input_section_list
&
3291 input_sections() const
3292 { return this->input_sections_
; }
3295 // We only save enough information to undo the effects of section layout.
3296 class Checkpoint_output_section
3299 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3300 const Input_section_list
& input_sections
,
3301 off_t first_input_offset
,
3302 bool attached_input_sections_are_sorted
)
3303 : addralign_(addralign
), flags_(flags
),
3304 input_sections_(input_sections
),
3305 input_sections_size_(input_sections_
.size()),
3306 input_sections_copy_(), first_input_offset_(first_input_offset
),
3307 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3311 ~Checkpoint_output_section()
3314 // Return the address alignment.
3317 { return this->addralign_
; }
3319 // Return the section flags.
3322 { return this->flags_
; }
3324 // Return a reference to the input section list copy.
3327 { return &this->input_sections_copy_
; }
3329 // Return the size of input_sections at the time when checkpoint is
3332 input_sections_size() const
3333 { return this->input_sections_size_
; }
3335 // Whether input sections are copied.
3337 input_sections_saved() const
3338 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3341 first_input_offset() const
3342 { return this->first_input_offset_
; }
3345 attached_input_sections_are_sorted() const
3346 { return this->attached_input_sections_are_sorted_
; }
3348 // Save input sections.
3350 save_input_sections()
3352 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3353 this->input_sections_copy_
.clear();
3354 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3355 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3356 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3357 this->input_sections_copy_
.push_back(*p
);
3361 // The section alignment.
3362 uint64_t addralign_
;
3363 // The section flags.
3364 elfcpp::Elf_Xword flags_
;
3365 // Reference to the input sections to be checkpointed.
3366 const Input_section_list
& input_sections_
;
3367 // Size of the checkpointed portion of input_sections_;
3368 size_t input_sections_size_
;
3369 // Copy of input sections.
3370 Input_section_list input_sections_copy_
;
3371 // The offset of the first entry in input_sections_.
3372 off_t first_input_offset_
;
3373 // True if the input sections attached to this output section have
3374 // already been sorted.
3375 bool attached_input_sections_are_sorted_
;
3378 // This class is used to sort the input sections.
3379 class Input_section_sort_entry
;
3381 // This is the sort comparison function for ctors and dtors.
3382 struct Input_section_sort_compare
3385 operator()(const Input_section_sort_entry
&,
3386 const Input_section_sort_entry
&) const;
3389 // This is the sort comparison function for .init_array and .fini_array.
3390 struct Input_section_sort_init_fini_compare
3393 operator()(const Input_section_sort_entry
&,
3394 const Input_section_sort_entry
&) const;
3397 // Fill data. This is used to fill in data between input sections.
3398 // It is also used for data statements (BYTE, WORD, etc.) in linker
3399 // scripts. When we have to keep track of the input sections, we
3400 // can use an Output_data_const, but we don't want to have to keep
3401 // track of input sections just to implement fills.
3405 Fill(off_t section_offset
, off_t length
)
3406 : section_offset_(section_offset
),
3407 length_(convert_to_section_size_type(length
))
3410 // Return section offset.
3412 section_offset() const
3413 { return this->section_offset_
; }
3415 // Return fill length.
3418 { return this->length_
; }
3421 // The offset within the output section.
3422 off_t section_offset_
;
3423 // The length of the space to fill.
3424 section_size_type length_
;
3427 typedef std::vector
<Fill
> Fill_list
;
3429 // This class describes properties of merge data sections. It is used
3430 // as a key type for maps.
3431 class Merge_section_properties
3434 Merge_section_properties(bool is_string
, uint64_t entsize
,
3436 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
3439 // Whether this equals to another Merge_section_properties MSP.
3441 eq(const Merge_section_properties
& msp
) const
3443 return ((this->is_string_
== msp
.is_string_
)
3444 && (this->entsize_
== msp
.entsize_
)
3445 && (this->addralign_
== msp
.addralign_
));
3448 // Compute a hash value for this using 64-bit FNV-1a hash.
3452 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
3453 uint64_t prime
= 1099511628211ULL;
3454 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
3455 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
3456 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
3460 // Functors for associative containers.
3464 operator()(const Merge_section_properties
& msp1
,
3465 const Merge_section_properties
& msp2
) const
3466 { return msp1
.eq(msp2
); }
3472 operator()(const Merge_section_properties
& msp
) const
3473 { return msp
.hash_value(); }
3477 // Whether this merge data section is for strings.
3479 // Entsize of this merge data section.
3481 // Address alignment.
3482 uint64_t addralign_
;
3485 // Map that link Merge_section_properties to Output_merge_base.
3486 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
3487 Merge_section_properties::hash
,
3488 Merge_section_properties::equal_to
>
3489 Merge_section_by_properties_map
;
3491 // Map that link Const_section_id to Output_section_data.
3492 typedef Unordered_map
<Const_section_id
, Output_section_data
*,
3493 Const_section_id_hash
>
3494 Output_section_data_by_input_section_map
;
3496 // Map that link Const_section_id to Output_relaxed_input_section.
3497 typedef Unordered_map
<Const_section_id
, Output_relaxed_input_section
*,
3498 Const_section_id_hash
>
3499 Output_relaxed_input_section_by_input_section_map
;
3501 // Map used during relaxation of existing sections. This map
3502 // a section id an input section list index. We assume that
3503 // Input_section_list is a vector.
3504 typedef Unordered_map
<Section_id
, size_t, Section_id_hash
> Relaxation_map
;
3506 // Add a new output section by Input_section.
3508 add_output_section_data(Input_section
*);
3510 // Add an SHF_MERGE input section. Returns true if the section was
3513 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3514 uint64_t entsize
, uint64_t addralign
);
3516 // Add an output SHF_MERGE section POSD to this output section.
3517 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3518 // ENTSIZE is the entity size. This returns the entry added to
3521 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3524 // Sort the attached input sections.
3526 sort_attached_input_sections();
3528 // Find the merge section into which an input section with index SHNDX in
3529 // OBJECT has been added. Return NULL if none found.
3530 Output_section_data
*
3531 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3533 // Build a relaxation map.
3535 build_relaxation_map(
3536 const Input_section_list
& input_sections
,
3538 Relaxation_map
* map
) const;
3540 // Convert input sections in an input section list into relaxed sections.
3542 convert_input_sections_in_list_to_relaxed_sections(
3543 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3544 const Relaxation_map
& map
,
3545 Input_section_list
* input_sections
);
3547 // Most of these fields are only valid after layout.
3549 // The name of the section. This will point into a Stringpool.
3551 // The section address is in the parent class.
3552 // The section alignment.
3553 uint64_t addralign_
;
3554 // The section entry size.
3556 // The load address. This is only used when using a linker script
3557 // with a SECTIONS clause. The has_load_address_ field indicates
3558 // whether this field is valid.
3559 uint64_t load_address_
;
3560 // The file offset is in the parent class.
3561 // Set the section link field to the index of this section.
3562 const Output_data
* link_section_
;
3563 // If link_section_ is NULL, this is the link field.
3565 // Set the section info field to the index of this section.
3566 const Output_section
* info_section_
;
3567 // If info_section_ is NULL, set the info field to the symbol table
3568 // index of this symbol.
3569 const Symbol
* info_symndx_
;
3570 // If info_section_ and info_symndx_ are NULL, this is the section
3573 // The section type.
3574 const elfcpp::Elf_Word type_
;
3575 // The section flags.
3576 elfcpp::Elf_Xword flags_
;
3577 // The section index.
3578 unsigned int out_shndx_
;
3579 // If there is a STT_SECTION for this output section in the normal
3580 // symbol table, this is the symbol index. This starts out as zero.
3581 // It is initialized in Layout::finalize() to be the index, or -1U
3582 // if there isn't one.
3583 unsigned int symtab_index_
;
3584 // If there is a STT_SECTION for this output section in the dynamic
3585 // symbol table, this is the symbol index. This starts out as zero.
3586 // It is initialized in Layout::finalize() to be the index, or -1U
3587 // if there isn't one.
3588 unsigned int dynsym_index_
;
3589 // The input sections. This will be empty in cases where we don't
3590 // need to keep track of them.
3591 Input_section_list input_sections_
;
3592 // The offset of the first entry in input_sections_.
3593 off_t first_input_offset_
;
3594 // The fill data. This is separate from input_sections_ because we
3595 // often will need fill sections without needing to keep track of
3598 // If the section requires postprocessing, this buffer holds the
3599 // section contents during relocation.
3600 unsigned char* postprocessing_buffer_
;
3601 // Whether this output section needs a STT_SECTION symbol in the
3602 // normal symbol table. This will be true if there is a relocation
3604 bool needs_symtab_index_
: 1;
3605 // Whether this output section needs a STT_SECTION symbol in the
3606 // dynamic symbol table. This will be true if there is a dynamic
3607 // relocation which needs it.
3608 bool needs_dynsym_index_
: 1;
3609 // Whether the link field of this output section should point to the
3610 // normal symbol table.
3611 bool should_link_to_symtab_
: 1;
3612 // Whether the link field of this output section should point to the
3613 // dynamic symbol table.
3614 bool should_link_to_dynsym_
: 1;
3615 // Whether this section should be written after all the input
3616 // sections are complete.
3617 bool after_input_sections_
: 1;
3618 // Whether this section requires post processing after all
3619 // relocations have been applied.
3620 bool requires_postprocessing_
: 1;
3621 // Whether an input section was mapped to this output section
3622 // because of a SECTIONS clause in a linker script.
3623 bool found_in_sections_clause_
: 1;
3624 // Whether this section has an explicitly specified load address.
3625 bool has_load_address_
: 1;
3626 // True if the info_section_ field means the section index of the
3627 // section, false if it means the symbol index of the corresponding
3629 bool info_uses_section_index_
: 1;
3630 // True if the input sections attached to this output section may
3632 bool may_sort_attached_input_sections_
: 1;
3633 // True if the input sections attached to this output section must
3635 bool must_sort_attached_input_sections_
: 1;
3636 // True if the input sections attached to this output section have
3637 // already been sorted.
3638 bool attached_input_sections_are_sorted_
: 1;
3639 // True if this section holds relro data.
3641 // True if this section holds relro local data.
3642 bool is_relro_local_
: 1;
3643 // True if this must be the last relro section.
3644 bool is_last_relro_
: 1;
3645 // True if this must be the first section after the relro sections.
3646 bool is_first_non_relro_
: 1;
3647 // True if this is a small section.
3648 bool is_small_section_
: 1;
3649 // True if this is a large section.
3650 bool is_large_section_
: 1;
3651 // True if this is the .interp section going into the PT_INTERP
3653 bool is_interp_
: 1;
3654 // True if this is section is read by the dynamic linker.
3655 bool is_dynamic_linker_section_
: 1;
3656 // Whether code-fills are generated at write.
3657 bool generate_code_fills_at_write_
: 1;
3658 // Whether the entry size field should be zero.
3659 bool is_entsize_zero_
: 1;
3660 // Whether section offsets need adjustment due to relaxation.
3661 bool section_offsets_need_adjustment_
: 1;
3662 // For SHT_TLS sections, the offset of this section relative to the base
3663 // of the TLS segment.
3664 uint64_t tls_offset_
;
3665 // Saved checkpoint.
3666 Checkpoint_output_section
* checkpoint_
;
3667 // Map from input sections to merge sections.
3668 Output_section_data_by_input_section_map merge_section_map_
;
3669 // Map from merge section properties to merge_sections;
3670 Merge_section_by_properties_map merge_section_by_properties_map_
;
3671 // Map from input sections to relaxed input sections. This is mutable
3672 // because it is updated lazily. We may need to update it in a
3673 // const qualified method.
3674 mutable Output_relaxed_input_section_by_input_section_map
3675 relaxed_input_section_map_
;
3676 // Whether relaxed_input_section_map_ is valid.
3677 mutable bool is_relaxed_input_section_map_valid_
;
3680 // An output segment. PT_LOAD segments are built from collections of
3681 // output sections. Other segments typically point within PT_LOAD
3682 // segments, and are built directly as needed.
3684 // NOTE: We want to use the copy constructor for this class. During
3685 // relaxation, we may try built the segments multiple times. We do
3686 // that by copying the original segment list before lay-out, doing
3687 // a trial lay-out and roll-back to the saved copied if we need to
3688 // to the lay-out again.
3690 class Output_segment
3693 // Create an output segment, specifying the type and flags.
3694 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3696 // Return the virtual address.
3699 { return this->vaddr_
; }
3701 // Return the physical address.
3704 { return this->paddr_
; }
3706 // Return the segment type.
3709 { return this->type_
; }
3711 // Return the segment flags.
3714 { return this->flags_
; }
3716 // Return the memory size.
3719 { return this->memsz_
; }
3721 // Return the file size.
3724 { return this->filesz_
; }
3726 // Return the file offset.
3729 { return this->offset_
; }
3731 // Whether this is a segment created to hold large data sections.
3733 is_large_data_segment() const
3734 { return this->is_large_data_segment_
; }
3736 // Record that this is a segment created to hold large data
3739 set_is_large_data_segment()
3740 { this->is_large_data_segment_
= true; }
3742 // Return the maximum alignment of the Output_data.
3744 maximum_alignment();
3746 // Add the Output_section OS to this segment. SEG_FLAGS is the
3747 // segment flags to use. DO_SORT is true if we should sort the
3748 // placement of the input section for more efficient generated code.
3750 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
,
3753 // Remove an Output_section from this segment. It is an error if it
3756 remove_output_section(Output_section
* os
);
3758 // Add an Output_data (which need not be an Output_section) to the
3759 // start of this segment.
3761 add_initial_output_data(Output_data
*);
3763 // Return true if this segment has any sections which hold actual
3764 // data, rather than being a BSS section.
3766 has_any_data_sections() const
3767 { return !this->output_data_
.empty(); }
3769 // Return the number of dynamic relocations applied to this segment.
3771 dynamic_reloc_count() const;
3773 // Return the address of the first section.
3775 first_section_load_address() const;
3777 // Return whether the addresses have been set already.
3779 are_addresses_set() const
3780 { return this->are_addresses_set_
; }
3782 // Set the addresses.
3784 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3786 this->vaddr_
= vaddr
;
3787 this->paddr_
= paddr
;
3788 this->are_addresses_set_
= true;
3791 // Update the flags for the flags of an output section added to this
3794 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
3796 // The ELF ABI specifies that a PT_TLS segment should always have
3797 // PF_R as the flags.
3798 if (this->type() != elfcpp::PT_TLS
)
3799 this->flags_
|= flags
;
3802 // Set the segment flags. This is only used if we have a PHDRS
3803 // clause which explicitly specifies the flags.
3805 set_flags(elfcpp::Elf_Word flags
)
3806 { this->flags_
= flags
; }
3808 // Set the address of the segment to ADDR and the offset to *POFF
3809 // and set the addresses and offsets of all contained output
3810 // sections accordingly. Set the section indexes of all contained
3811 // output sections starting with *PSHNDX. If RESET is true, first
3812 // reset the addresses of the contained sections. Return the
3813 // address of the immediately following segment. Update *POFF and
3814 // *PSHNDX. This should only be called for a PT_LOAD segment.
3816 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
,
3817 unsigned int increase_relro
, off_t
* poff
,
3818 unsigned int* pshndx
);
3820 // Set the minimum alignment of this segment. This may be adjusted
3821 // upward based on the section alignments.
3823 set_minimum_p_align(uint64_t align
)
3824 { this->min_p_align_
= align
; }
3826 // Set the offset of this segment based on the section. This should
3827 // only be called for a non-PT_LOAD segment.
3829 set_offset(unsigned int increase
);
3831 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3835 // Return the number of output sections.
3837 output_section_count() const;
3839 // Return the section attached to the list segment with the lowest
3840 // load address. This is used when handling a PHDRS clause in a
3843 section_with_lowest_load_address() const;
3845 // Write the segment header into *OPHDR.
3846 template<int size
, bool big_endian
>
3848 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
3850 // Write the section headers of associated sections into V.
3851 template<int size
, bool big_endian
>
3853 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
3854 unsigned int* pshndx
) const;
3856 // Print the output sections in the map file.
3858 print_sections_to_mapfile(Mapfile
*) const;
3861 typedef std::list
<Output_data
*> Output_data_list
;
3863 // Find the maximum alignment in an Output_data_list.
3865 maximum_alignment_list(const Output_data_list
*);
3867 // Return whether the first data section is a relro section.
3869 is_first_section_relro() const;
3871 // Set the section addresses in an Output_data_list.
3873 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
3874 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
3877 // Return the number of Output_sections in an Output_data_list.
3879 output_section_count_list(const Output_data_list
*) const;
3881 // Return the number of dynamic relocs in an Output_data_list.
3883 dynamic_reloc_count_list(const Output_data_list
*) const;
3885 // Find the section with the lowest load address in an
3886 // Output_data_list.
3888 lowest_load_address_in_list(const Output_data_list
* pdl
,
3889 Output_section
** found
,
3890 uint64_t* found_lma
) const;
3892 // Write the section headers in the list into V.
3893 template<int size
, bool big_endian
>
3895 write_section_headers_list(const Layout
*, const Stringpool
*,
3896 const Output_data_list
*, unsigned char* v
,
3897 unsigned int* pshdx
) const;
3899 // Print a section list to the mapfile.
3901 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
3903 // NOTE: We want to use the copy constructor. Currently, shallow copy
3904 // works for us so we do not need to write our own copy constructor.
3906 // The list of output data with contents attached to this segment.
3907 Output_data_list output_data_
;
3908 // The list of output data without contents attached to this segment.
3909 Output_data_list output_bss_
;
3910 // The segment virtual address.
3912 // The segment physical address.
3914 // The size of the segment in memory.
3916 // The maximum section alignment. The is_max_align_known_ field
3917 // indicates whether this has been finalized.
3918 uint64_t max_align_
;
3919 // The required minimum value for the p_align field. This is used
3920 // for PT_LOAD segments. Note that this does not mean that
3921 // addresses should be aligned to this value; it means the p_paddr
3922 // and p_vaddr fields must be congruent modulo this value. For
3923 // non-PT_LOAD segments, the dynamic linker works more efficiently
3924 // if the p_align field has the more conventional value, although it
3925 // can align as needed.
3926 uint64_t min_p_align_
;
3927 // The offset of the segment data within the file.
3929 // The size of the segment data in the file.
3931 // The segment type;
3932 elfcpp::Elf_Word type_
;
3933 // The segment flags.
3934 elfcpp::Elf_Word flags_
;
3935 // Whether we have finalized max_align_.
3936 bool is_max_align_known_
: 1;
3937 // Whether vaddr and paddr were set by a linker script.
3938 bool are_addresses_set_
: 1;
3939 // Whether this segment holds large data sections.
3940 bool is_large_data_segment_
: 1;
3943 // This class represents the output file.
3948 Output_file(const char* name
);
3950 // Indicate that this is a temporary file which should not be
3954 { this->is_temporary_
= true; }
3956 // Try to open an existing file. Returns false if the file doesn't
3957 // exist, has a size of 0 or can't be mmaped. This method is
3960 open_for_modification();
3962 // Open the output file. FILE_SIZE is the final size of the file.
3963 // If the file already exists, it is deleted/truncated. This method
3964 // is thread-unsafe.
3966 open(off_t file_size
);
3968 // Resize the output file. This method is thread-unsafe.
3970 resize(off_t file_size
);
3972 // Close the output file (flushing all buffered data) and make sure
3973 // there are no errors. This method is thread-unsafe.
3977 // Return the size of this file.
3980 { return this->file_size_
; }
3982 // Return the name of this file.
3985 { return this->name_
; }
3987 // We currently always use mmap which makes the view handling quite
3988 // simple. In the future we may support other approaches.
3990 // Write data to the output file.
3992 write(off_t offset
, const void* data
, size_t len
)
3993 { memcpy(this->base_
+ offset
, data
, len
); }
3995 // Get a buffer to use to write to the file, given the offset into
3996 // the file and the size.
3998 get_output_view(off_t start
, size_t size
)
4000 gold_assert(start
>= 0
4001 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
4002 return this->base_
+ start
;
4005 // VIEW must have been returned by get_output_view. Write the
4006 // buffer to the file, passing in the offset and the size.
4008 write_output_view(off_t
, size_t, unsigned char*)
4011 // Get a read/write buffer. This is used when we want to write part
4012 // of the file, read it in, and write it again.
4014 get_input_output_view(off_t start
, size_t size
)
4015 { return this->get_output_view(start
, size
); }
4017 // Write a read/write buffer back to the file.
4019 write_input_output_view(off_t
, size_t, unsigned char*)
4022 // Get a read buffer. This is used when we just want to read part
4023 // of the file back it in.
4024 const unsigned char*
4025 get_input_view(off_t start
, size_t size
)
4026 { return this->get_output_view(start
, size
); }
4028 // Release a read bfufer.
4030 free_input_view(off_t
, size_t, const unsigned char*)
4034 // Map the file into memory or, if that fails, allocate anonymous
4039 // Allocate anonymous memory for the file.
4043 // Map the file into memory.
4047 // Unmap the file from memory (and flush to disk buffers).
4057 // Base of file mapped into memory.
4058 unsigned char* base_
;
4059 // True iff base_ points to a memory buffer rather than an output file.
4060 bool map_is_anonymous_
;
4061 // True if this is a temporary file which should not be output.
4065 } // End namespace gold.
4067 #endif // !defined(GOLD_OUTPUT_H)