1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright (C) 2006-2014 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
>
49 template<int size
, bool big_endian
>
50 class Sized_relobj_file
;
52 // An abtract class for data which has to go into the output file.
57 explicit Output_data()
58 : address_(0), data_size_(0), offset_(-1),
59 is_address_valid_(false), is_data_size_valid_(false),
60 is_offset_valid_(false), is_data_size_fixed_(false),
61 has_dynamic_reloc_(false)
67 // Return the address. For allocated sections, this is only valid
68 // after Layout::finalize is finished.
72 gold_assert(this->is_address_valid_
);
73 return this->address_
;
76 // Return the size of the data. For allocated sections, this must
77 // be valid after Layout::finalize calls set_address, but need not
78 // be valid before then.
82 gold_assert(this->is_data_size_valid_
);
83 return this->data_size_
;
86 // Get the current data size.
88 current_data_size() const
89 { return this->current_data_size_for_child(); }
91 // Return true if data size is fixed.
93 is_data_size_fixed() const
94 { return this->is_data_size_fixed_
; }
96 // Return the file offset. This is only valid after
97 // Layout::finalize is finished. For some non-allocated sections,
98 // it may not be valid until near the end of the link.
102 gold_assert(this->is_offset_valid_
);
103 return this->offset_
;
106 // Reset the address, file offset and data size. This essentially
107 // disables the sanity testing about duplicate and unknown settings.
109 reset_address_and_file_offset()
111 this->is_address_valid_
= false;
112 this->is_offset_valid_
= false;
113 if (!this->is_data_size_fixed_
)
114 this->is_data_size_valid_
= false;
115 this->do_reset_address_and_file_offset();
118 // As above, but just for data size.
122 if (!this->is_data_size_fixed_
)
123 this->is_data_size_valid_
= false;
126 // Return true if address and file offset already have reset values. In
127 // other words, calling reset_address_and_file_offset will not change them.
129 address_and_file_offset_have_reset_values() const
130 { return this->do_address_and_file_offset_have_reset_values(); }
132 // Return the required alignment.
135 { return this->do_addralign(); }
137 // Return whether this has a load address.
139 has_load_address() const
140 { return this->do_has_load_address(); }
142 // Return the load address.
145 { return this->do_load_address(); }
147 // Return whether this is an Output_section.
150 { return this->do_is_section(); }
152 // Return whether this is an Output_section of the specified type.
154 is_section_type(elfcpp::Elf_Word stt
) const
155 { return this->do_is_section_type(stt
); }
157 // Return whether this is an Output_section with the specified flag
160 is_section_flag_set(elfcpp::Elf_Xword shf
) const
161 { return this->do_is_section_flag_set(shf
); }
163 // Return the output section that this goes in, if there is one.
166 { return this->do_output_section(); }
168 const Output_section
*
169 output_section() const
170 { return this->do_output_section(); }
172 // Return the output section index, if there is an output section.
175 { return this->do_out_shndx(); }
177 // Set the output section index, if this is an output section.
179 set_out_shndx(unsigned int shndx
)
180 { this->do_set_out_shndx(shndx
); }
182 // Set the address and file offset of this data, and finalize the
183 // size of the data. This is called during Layout::finalize for
184 // allocated sections.
186 set_address_and_file_offset(uint64_t addr
, off_t off
)
188 this->set_address(addr
);
189 this->set_file_offset(off
);
190 this->finalize_data_size();
195 set_address(uint64_t addr
)
197 gold_assert(!this->is_address_valid_
);
198 this->address_
= addr
;
199 this->is_address_valid_
= true;
202 // Set the file offset.
204 set_file_offset(off_t off
)
206 gold_assert(!this->is_offset_valid_
);
208 this->is_offset_valid_
= true;
211 // Update the data size without finalizing it.
213 pre_finalize_data_size()
215 if (!this->is_data_size_valid_
)
217 // Tell the child class to update the data size.
218 this->update_data_size();
222 // Finalize the data size.
226 if (!this->is_data_size_valid_
)
228 // Tell the child class to set the data size.
229 this->set_final_data_size();
230 gold_assert(this->is_data_size_valid_
);
234 // Set the TLS offset. Called only for SHT_TLS sections.
236 set_tls_offset(uint64_t tls_base
)
237 { this->do_set_tls_offset(tls_base
); }
239 // Return the TLS offset, relative to the base of the TLS segment.
240 // Valid only for SHT_TLS sections.
243 { return this->do_tls_offset(); }
245 // Write the data to the output file. This is called after
246 // Layout::finalize is complete.
248 write(Output_file
* file
)
249 { this->do_write(file
); }
251 // This is called by Layout::finalize to note that the sizes of
252 // allocated sections must now be fixed.
255 { Output_data::allocated_sizes_are_fixed
= true; }
257 // Used to check that layout has been done.
260 { return Output_data::allocated_sizes_are_fixed
; }
262 // Note that a dynamic reloc has been applied to this data.
265 { this->has_dynamic_reloc_
= true; }
267 // Return whether a dynamic reloc has been applied.
269 has_dynamic_reloc() const
270 { return this->has_dynamic_reloc_
; }
272 // Whether the address is valid.
274 is_address_valid() const
275 { return this->is_address_valid_
; }
277 // Whether the file offset is valid.
279 is_offset_valid() const
280 { return this->is_offset_valid_
; }
282 // Whether the data size is valid.
284 is_data_size_valid() const
285 { return this->is_data_size_valid_
; }
287 // Print information to the map file.
289 print_to_mapfile(Mapfile
* mapfile
) const
290 { return this->do_print_to_mapfile(mapfile
); }
293 // Functions that child classes may or in some cases must implement.
295 // Write the data to the output file.
297 do_write(Output_file
*) = 0;
299 // Return the required alignment.
301 do_addralign() const = 0;
303 // Return whether this has a load address.
305 do_has_load_address() const
308 // Return the load address.
310 do_load_address() const
311 { gold_unreachable(); }
313 // Return whether this is an Output_section.
315 do_is_section() const
318 // Return whether this is an Output_section of the specified type.
319 // This only needs to be implement by Output_section.
321 do_is_section_type(elfcpp::Elf_Word
) const
324 // Return whether this is an Output_section with the specific flag
325 // set. This only needs to be implemented by Output_section.
327 do_is_section_flag_set(elfcpp::Elf_Xword
) const
330 // Return the output section, if there is one.
331 virtual Output_section
*
335 virtual const Output_section
*
336 do_output_section() const
339 // Return the output section index, if there is an output section.
342 { gold_unreachable(); }
344 // Set the output section index, if this is an output section.
346 do_set_out_shndx(unsigned int)
347 { gold_unreachable(); }
349 // This is a hook for derived classes to set the preliminary data size.
350 // This is called by pre_finalize_data_size, normally called during
351 // Layout::finalize, before the section address is set, and is used
352 // during an incremental update, when we need to know the size of a
353 // section before allocating space in the output file. For classes
354 // where the current data size is up to date, this default version of
355 // the method can be inherited.
360 // This is a hook for derived classes to set the data size. This is
361 // called by finalize_data_size, normally called during
362 // Layout::finalize, when the section address is set.
364 set_final_data_size()
365 { gold_unreachable(); }
367 // A hook for resetting the address and file offset.
369 do_reset_address_and_file_offset()
372 // Return true if address and file offset already have reset values. In
373 // other words, calling reset_address_and_file_offset will not change them.
374 // A child class overriding do_reset_address_and_file_offset may need to
375 // also override this.
377 do_address_and_file_offset_have_reset_values() const
378 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
380 // Set the TLS offset. Called only for SHT_TLS sections.
382 do_set_tls_offset(uint64_t)
383 { gold_unreachable(); }
385 // Return the TLS offset, relative to the base of the TLS segment.
386 // Valid only for SHT_TLS sections.
388 do_tls_offset() const
389 { gold_unreachable(); }
391 // Print to the map file. This only needs to be implemented by
392 // classes which may appear in a PT_LOAD segment.
394 do_print_to_mapfile(Mapfile
*) const
395 { gold_unreachable(); }
397 // Functions that child classes may call.
399 // Reset the address. The Output_section class needs this when an
400 // SHF_ALLOC input section is added to an output section which was
401 // formerly not SHF_ALLOC.
403 mark_address_invalid()
404 { this->is_address_valid_
= false; }
406 // Set the size of the data.
408 set_data_size(off_t data_size
)
410 gold_assert(!this->is_data_size_valid_
411 && !this->is_data_size_fixed_
);
412 this->data_size_
= data_size
;
413 this->is_data_size_valid_
= true;
416 // Fix the data size. Once it is fixed, it cannot be changed
417 // and the data size remains always valid.
421 gold_assert(this->is_data_size_valid_
);
422 this->is_data_size_fixed_
= true;
425 // Get the current data size--this is for the convenience of
426 // sections which build up their size over time.
428 current_data_size_for_child() const
429 { return this->data_size_
; }
431 // Set the current data size--this is for the convenience of
432 // sections which build up their size over time.
434 set_current_data_size_for_child(off_t data_size
)
436 gold_assert(!this->is_data_size_valid_
);
437 this->data_size_
= data_size
;
440 // Return default alignment for the target size.
444 // Return default alignment for a specified size--32 or 64.
446 default_alignment_for_size(int size
);
449 Output_data(const Output_data
&);
450 Output_data
& operator=(const Output_data
&);
452 // This is used for verification, to make sure that we don't try to
453 // change any sizes of allocated sections after we set the section
455 static bool allocated_sizes_are_fixed
;
457 // Memory address in output file.
459 // Size of data in output file.
461 // File offset of contents in output file.
463 // Whether address_ is valid.
464 bool is_address_valid_
: 1;
465 // Whether data_size_ is valid.
466 bool is_data_size_valid_
: 1;
467 // Whether offset_ is valid.
468 bool is_offset_valid_
: 1;
469 // Whether data size is fixed.
470 bool is_data_size_fixed_
: 1;
471 // Whether any dynamic relocs have been applied to this section.
472 bool has_dynamic_reloc_
: 1;
475 // Output the section headers.
477 class Output_section_headers
: public Output_data
480 Output_section_headers(const Layout
*,
481 const Layout::Segment_list
*,
482 const Layout::Section_list
*,
483 const Layout::Section_list
*,
485 const Output_section
*);
488 // Write the data to the file.
490 do_write(Output_file
*);
492 // Return the required alignment.
495 { return Output_data::default_alignment(); }
497 // Write to a map file.
499 do_print_to_mapfile(Mapfile
* mapfile
) const
500 { mapfile
->print_output_data(this, _("** section headers")); }
502 // Update the data size.
505 { this->set_data_size(this->do_size()); }
507 // Set final data size.
509 set_final_data_size()
510 { this->set_data_size(this->do_size()); }
513 // Write the data to the file with the right size and endianness.
514 template<int size
, bool big_endian
>
516 do_sized_write(Output_file
*);
518 // Compute data size.
522 const Layout
* layout_
;
523 const Layout::Segment_list
* segment_list_
;
524 const Layout::Section_list
* section_list_
;
525 const Layout::Section_list
* unattached_section_list_
;
526 const Stringpool
* secnamepool_
;
527 const Output_section
* shstrtab_section_
;
530 // Output the segment headers.
532 class Output_segment_headers
: public Output_data
535 Output_segment_headers(const Layout::Segment_list
& segment_list
);
538 // Write the data to the file.
540 do_write(Output_file
*);
542 // Return the required alignment.
545 { return Output_data::default_alignment(); }
547 // Write to a map file.
549 do_print_to_mapfile(Mapfile
* mapfile
) const
550 { mapfile
->print_output_data(this, _("** segment headers")); }
552 // Set final data size.
554 set_final_data_size()
555 { this->set_data_size(this->do_size()); }
558 // Write the data to the file with the right size and endianness.
559 template<int size
, bool big_endian
>
561 do_sized_write(Output_file
*);
563 // Compute the current size.
567 const Layout::Segment_list
& segment_list_
;
570 // Output the ELF file header.
572 class Output_file_header
: public Output_data
575 Output_file_header(Target
*,
577 const Output_segment_headers
*);
579 // Add information about the section headers. We lay out the ELF
580 // file header before we create the section headers.
581 void set_section_info(const Output_section_headers
*,
582 const Output_section
* shstrtab
);
585 // Write the data to the file.
587 do_write(Output_file
*);
589 // Return the required alignment.
592 { return Output_data::default_alignment(); }
594 // Write to a map file.
596 do_print_to_mapfile(Mapfile
* mapfile
) const
597 { mapfile
->print_output_data(this, _("** file header")); }
599 // Set final data size.
601 set_final_data_size(void)
602 { this->set_data_size(this->do_size()); }
605 // Write the data to the file with the right size and endianness.
606 template<int size
, bool big_endian
>
608 do_sized_write(Output_file
*);
610 // Return the value to use for the entry address.
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr
615 // Compute the current data size.
620 const Symbol_table
* symtab_
;
621 const Output_segment_headers
* segment_header_
;
622 const Output_section_headers
* section_header_
;
623 const Output_section
* shstrtab_
;
626 // Output sections are mainly comprised of input sections. However,
627 // there are cases where we have data to write out which is not in an
628 // input section. Output_section_data is used in such cases. This is
629 // an abstract base class.
631 class Output_section_data
: public Output_data
634 Output_section_data(off_t data_size
, uint64_t addralign
,
635 bool is_data_size_fixed
)
636 : Output_data(), output_section_(NULL
), addralign_(addralign
)
638 this->set_data_size(data_size
);
639 if (is_data_size_fixed
)
640 this->fix_data_size();
643 Output_section_data(uint64_t addralign
)
644 : Output_data(), output_section_(NULL
), addralign_(addralign
)
647 // Return the output section.
650 { return this->output_section_
; }
652 const Output_section
*
653 output_section() const
654 { return this->output_section_
; }
656 // Record the output section.
658 set_output_section(Output_section
* os
);
660 // Add an input section, for SHF_MERGE sections. This returns true
661 // if the section was handled.
663 add_input_section(Relobj
* object
, unsigned int shndx
)
664 { return this->do_add_input_section(object
, shndx
); }
666 // Given an input OBJECT, an input section index SHNDX within that
667 // object, and an OFFSET relative to the start of that input
668 // section, return whether or not the corresponding offset within
669 // the output section is known. If this function returns true, it
670 // sets *POUTPUT to the output offset. The value -1 indicates that
671 // this input offset is being discarded.
673 output_offset(const Relobj
* object
, unsigned int shndx
,
674 section_offset_type offset
,
675 section_offset_type
* poutput
) const
676 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
678 // Return whether this is the merge section for the input section
679 // SHNDX in OBJECT. This should return true when output_offset
680 // would return true for some values of OFFSET.
682 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
683 { return this->do_is_merge_section_for(object
, shndx
); }
685 // Write the contents to a buffer. This is used for sections which
686 // require postprocessing, such as compression.
688 write_to_buffer(unsigned char* buffer
)
689 { this->do_write_to_buffer(buffer
); }
691 // Print merge stats to stderr. This should only be called for
692 // SHF_MERGE sections.
694 print_merge_stats(const char* section_name
)
695 { this->do_print_merge_stats(section_name
); }
698 // The child class must implement do_write.
700 // The child class may implement specific adjustments to the output
703 do_adjust_output_section(Output_section
*)
706 // May be implemented by child class. Return true if the section
709 do_add_input_section(Relobj
*, unsigned int)
710 { gold_unreachable(); }
712 // The child class may implement output_offset.
714 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
715 section_offset_type
*) const
718 // The child class may implement is_merge_section_for.
720 do_is_merge_section_for(const Relobj
*, unsigned int) const
723 // The child class may implement write_to_buffer. Most child
724 // classes can not appear in a compressed section, and they do not
727 do_write_to_buffer(unsigned char*)
728 { gold_unreachable(); }
730 // Print merge statistics.
732 do_print_merge_stats(const char*)
733 { gold_unreachable(); }
735 // Return the required alignment.
738 { return this->addralign_
; }
740 // Return the output section.
743 { return this->output_section_
; }
745 const Output_section
*
746 do_output_section() const
747 { return this->output_section_
; }
749 // Return the section index of the output section.
751 do_out_shndx() const;
753 // Set the alignment.
755 set_addralign(uint64_t addralign
);
758 // The output section for this section.
759 Output_section
* output_section_
;
760 // The required alignment.
764 // Some Output_section_data classes build up their data step by step,
765 // rather than all at once. This class provides an interface for
768 class Output_section_data_build
: public Output_section_data
771 Output_section_data_build(uint64_t addralign
)
772 : Output_section_data(addralign
)
775 Output_section_data_build(off_t data_size
, uint64_t addralign
)
776 : Output_section_data(data_size
, addralign
, false)
779 // Set the current data size.
781 set_current_data_size(off_t data_size
)
782 { this->set_current_data_size_for_child(data_size
); }
785 // Set the final data size.
787 set_final_data_size()
788 { this->set_data_size(this->current_data_size_for_child()); }
791 // A simple case of Output_data in which we have constant data to
794 class Output_data_const
: public Output_section_data
797 Output_data_const(const std::string
& data
, uint64_t addralign
)
798 : Output_section_data(data
.size(), addralign
, true), data_(data
)
801 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
802 : Output_section_data(len
, addralign
, true), data_(p
, len
)
805 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
806 : Output_section_data(len
, addralign
, true),
807 data_(reinterpret_cast<const char*>(p
), len
)
811 // Write the data to the output file.
813 do_write(Output_file
*);
815 // Write the data to a buffer.
817 do_write_to_buffer(unsigned char* buffer
)
818 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
820 // Write to a map file.
822 do_print_to_mapfile(Mapfile
* mapfile
) const
823 { mapfile
->print_output_data(this, _("** fill")); }
829 // Another version of Output_data with constant data, in which the
830 // buffer is allocated by the caller.
832 class Output_data_const_buffer
: public Output_section_data
835 Output_data_const_buffer(const unsigned char* p
, off_t len
,
836 uint64_t addralign
, const char* map_name
)
837 : Output_section_data(len
, addralign
, true),
838 p_(p
), map_name_(map_name
)
842 // Write the data the output file.
844 do_write(Output_file
*);
846 // Write the data to a buffer.
848 do_write_to_buffer(unsigned char* buffer
)
849 { memcpy(buffer
, this->p_
, this->data_size()); }
851 // Write to a map file.
853 do_print_to_mapfile(Mapfile
* mapfile
) const
854 { mapfile
->print_output_data(this, _(this->map_name_
)); }
857 // The data to output.
858 const unsigned char* p_
;
859 // Name to use in a map file. Maps are a rarely used feature, but
860 // the space usage is minor as aren't very many of these objects.
861 const char* map_name_
;
864 // A place holder for a fixed amount of data written out via some
867 class Output_data_fixed_space
: public Output_section_data
870 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
871 const char* map_name
)
872 : Output_section_data(data_size
, addralign
, true),
877 // Write out the data--the actual data must be written out
880 do_write(Output_file
*)
883 // Write to a map file.
885 do_print_to_mapfile(Mapfile
* mapfile
) const
886 { mapfile
->print_output_data(this, _(this->map_name_
)); }
889 // Name to use in a map file. Maps are a rarely used feature, but
890 // the space usage is minor as aren't very many of these objects.
891 const char* map_name_
;
894 // A place holder for variable sized data written out via some other
897 class Output_data_space
: public Output_section_data_build
900 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
901 : Output_section_data_build(addralign
),
905 explicit Output_data_space(off_t data_size
, uint64_t addralign
,
906 const char* map_name
)
907 : Output_section_data_build(data_size
, addralign
),
911 // Set the alignment.
913 set_space_alignment(uint64_t align
)
914 { this->set_addralign(align
); }
917 // Write out the data--the actual data must be written out
920 do_write(Output_file
*)
923 // Write to a map file.
925 do_print_to_mapfile(Mapfile
* mapfile
) const
926 { mapfile
->print_output_data(this, _(this->map_name_
)); }
929 // Name to use in a map file. Maps are a rarely used feature, but
930 // the space usage is minor as aren't very many of these objects.
931 const char* map_name_
;
934 // Fill fixed space with zeroes. This is just like
935 // Output_data_fixed_space, except that the map name is known.
937 class Output_data_zero_fill
: public Output_section_data
940 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
941 : Output_section_data(data_size
, addralign
, true)
945 // There is no data to write out.
947 do_write(Output_file
*)
950 // Write to a map file.
952 do_print_to_mapfile(Mapfile
* mapfile
) const
953 { mapfile
->print_output_data(this, "** zero fill"); }
956 // A string table which goes into an output section.
958 class Output_data_strtab
: public Output_section_data
961 Output_data_strtab(Stringpool
* strtab
)
962 : Output_section_data(1), strtab_(strtab
)
966 // This is called to update the section size prior to assigning
967 // the address and file offset.
970 { this->set_final_data_size(); }
972 // This is called to set the address and file offset. Here we make
973 // sure that the Stringpool is finalized.
975 set_final_data_size();
977 // Write out the data.
979 do_write(Output_file
*);
981 // Write the data to a buffer.
983 do_write_to_buffer(unsigned char* buffer
)
984 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
986 // Write to a map file.
988 do_print_to_mapfile(Mapfile
* mapfile
) const
989 { mapfile
->print_output_data(this, _("** string table")); }
995 // This POD class is used to represent a single reloc in the output
996 // file. This could be a private class within Output_data_reloc, but
997 // the templatization is complex enough that I broke it out into a
998 // separate class. The class is templatized on either elfcpp::SHT_REL
999 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1000 // relocation or an ordinary relocation.
1002 // A relocation can be against a global symbol, a local symbol, a
1003 // local section symbol, an output section, or the undefined symbol at
1004 // index 0. We represent the latter by using a NULL global symbol.
1006 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1009 template<bool dynamic
, int size
, bool big_endian
>
1010 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1013 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1014 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1016 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1018 // An uninitialized entry. We need this because we want to put
1019 // instances of this class into an STL container.
1021 : local_sym_index_(INVALID_CODE
)
1024 // We have a bunch of different constructors. They come in pairs
1025 // depending on how the address of the relocation is specified. It
1026 // can either be an offset in an Output_data or an offset in an
1029 // A reloc against a global symbol.
1031 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1032 Address address
, bool is_relative
, bool is_symbolless
,
1033 bool use_plt_offset
);
1035 Output_reloc(Symbol
* gsym
, unsigned int type
,
1036 Sized_relobj
<size
, big_endian
>* relobj
,
1037 unsigned int shndx
, Address address
, bool is_relative
,
1038 bool is_symbolless
, bool use_plt_offset
);
1040 // A reloc against a local symbol or local section symbol.
1042 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1043 unsigned int local_sym_index
, unsigned int type
,
1044 Output_data
* od
, Address address
, bool is_relative
,
1045 bool is_symbolless
, bool is_section_symbol
,
1046 bool use_plt_offset
);
1048 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1049 unsigned int local_sym_index
, unsigned int type
,
1050 unsigned int shndx
, Address address
, bool is_relative
,
1051 bool is_symbolless
, bool is_section_symbol
,
1052 bool use_plt_offset
);
1054 // A reloc against the STT_SECTION symbol of an output section.
1056 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1057 Address address
, bool is_relative
);
1059 Output_reloc(Output_section
* os
, unsigned int type
,
1060 Sized_relobj
<size
, big_endian
>* relobj
, unsigned int shndx
,
1061 Address address
, bool is_relative
);
1063 // An absolute or relative relocation with no symbol.
1065 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1068 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1069 unsigned int shndx
, Address address
, bool is_relative
);
1071 // A target specific relocation. The target will be called to get
1072 // the symbol index, passing ARG. The type and offset will be set
1073 // as for other relocation types.
1075 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1078 Output_reloc(unsigned int type
, void* arg
,
1079 Sized_relobj
<size
, big_endian
>* relobj
,
1080 unsigned int shndx
, Address address
);
1082 // Return the reloc type.
1085 { return this->type_
; }
1087 // Return whether this is a RELATIVE relocation.
1090 { return this->is_relative_
; }
1092 // Return whether this is a relocation which should not use
1093 // a symbol, but which obtains its addend from a symbol.
1095 is_symbolless() const
1096 { return this->is_symbolless_
; }
1098 // Return whether this is against a local section symbol.
1100 is_local_section_symbol() const
1102 return (this->local_sym_index_
!= GSYM_CODE
1103 && this->local_sym_index_
!= SECTION_CODE
1104 && this->local_sym_index_
!= INVALID_CODE
1105 && this->local_sym_index_
!= TARGET_CODE
1106 && this->is_section_symbol_
);
1109 // Return whether this is a target specific relocation.
1111 is_target_specific() const
1112 { return this->local_sym_index_
== TARGET_CODE
; }
1114 // Return the argument to pass to the target for a target specific
1119 gold_assert(this->local_sym_index_
== TARGET_CODE
);
1120 return this->u1_
.arg
;
1123 // For a local section symbol, return the offset of the input
1124 // section within the output section. ADDEND is the addend being
1125 // applied to the input section.
1127 local_section_offset(Addend addend
) const;
1129 // Get the value of the symbol referred to by a Rel relocation when
1130 // we are adding the given ADDEND.
1132 symbol_value(Addend addend
) const;
1134 // If this relocation is against an input section, return the
1135 // relocatable object containing the input section.
1136 Sized_relobj
<size
, big_endian
>*
1139 if (this->shndx_
== INVALID_CODE
)
1141 return this->u2_
.relobj
;
1144 // Write the reloc entry to an output view.
1146 write(unsigned char* pov
) const;
1148 // Write the offset and info fields to Write_rel.
1149 template<typename Write_rel
>
1150 void write_rel(Write_rel
*) const;
1152 // This is used when sorting dynamic relocs. Return -1 to sort this
1153 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1155 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1158 // Return whether this reloc should be sorted before the argument
1159 // when sorting dynamic relocs.
1161 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1163 { return this->compare(r2
) < 0; }
1166 // Record that we need a dynamic symbol index.
1168 set_needs_dynsym_index();
1170 // Return the symbol index.
1172 get_symbol_index() const;
1174 // Return the output address.
1176 get_address() const;
1178 // Codes for local_sym_index_.
1187 // Invalid uninitialized entry.
1193 // For a local symbol or local section symbol
1194 // (this->local_sym_index_ >= 0), the object. We will never
1195 // generate a relocation against a local symbol in a dynamic
1196 // object; that doesn't make sense. And our callers will always
1197 // be templatized, so we use Sized_relobj here.
1198 Sized_relobj
<size
, big_endian
>* relobj
;
1199 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1200 // symbol. If this is NULL, it indicates a relocation against the
1201 // undefined 0 symbol.
1203 // For a relocation against an output section
1204 // (this->local_sym_index_ == SECTION_CODE), the output section.
1206 // For a target specific relocation, an argument to pass to the
1212 // If this->shndx_ is not INVALID CODE, the object which holds the
1213 // input section being used to specify the reloc address.
1214 Sized_relobj
<size
, big_endian
>* relobj
;
1215 // If this->shndx_ is INVALID_CODE, the output data being used to
1216 // specify the reloc address. This may be NULL if the reloc
1217 // address is absolute.
1220 // The address offset within the input section or the Output_data.
1222 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1223 // relocation against an output section, or TARGET_CODE for a target
1224 // specific relocation, or INVALID_CODE for an uninitialized value.
1225 // Otherwise, for a local symbol (this->is_section_symbol_ is
1226 // false), the local symbol index. For a local section symbol
1227 // (this->is_section_symbol_ is true), the section index in the
1229 unsigned int local_sym_index_
;
1230 // The reloc type--a processor specific code.
1231 unsigned int type_
: 28;
1232 // True if the relocation is a RELATIVE relocation.
1233 bool is_relative_
: 1;
1234 // True if the relocation is one which should not use
1235 // a symbol, but which obtains its addend from a symbol.
1236 bool is_symbolless_
: 1;
1237 // True if the relocation is against a section symbol.
1238 bool is_section_symbol_
: 1;
1239 // True if the addend should be the PLT offset.
1240 // (Used only for RELA, but stored here for space.)
1241 bool use_plt_offset_
: 1;
1242 // If the reloc address is an input section in an object, the
1243 // section index. This is INVALID_CODE if the reloc address is
1244 // specified in some other way.
1245 unsigned int shndx_
;
1248 // The SHT_RELA version of Output_reloc<>. This is just derived from
1249 // the SHT_REL version of Output_reloc, but it adds an addend.
1251 template<bool dynamic
, int size
, bool big_endian
>
1252 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1255 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1256 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1258 // An uninitialized entry.
1263 // A reloc against a global symbol.
1265 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1266 Address address
, Addend addend
, bool is_relative
,
1267 bool is_symbolless
, bool use_plt_offset
)
1268 : rel_(gsym
, type
, od
, address
, is_relative
, is_symbolless
,
1273 Output_reloc(Symbol
* gsym
, unsigned int type
,
1274 Sized_relobj
<size
, big_endian
>* relobj
,
1275 unsigned int shndx
, Address address
, Addend addend
,
1276 bool is_relative
, bool is_symbolless
, bool use_plt_offset
)
1277 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
,
1278 is_symbolless
, use_plt_offset
), addend_(addend
)
1281 // A reloc against a local symbol.
1283 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1284 unsigned int local_sym_index
, unsigned int type
,
1285 Output_data
* od
, Address address
,
1286 Addend addend
, bool is_relative
,
1287 bool is_symbolless
, bool is_section_symbol
,
1288 bool use_plt_offset
)
1289 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1290 is_symbolless
, is_section_symbol
, use_plt_offset
),
1294 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1295 unsigned int local_sym_index
, unsigned int type
,
1296 unsigned int shndx
, Address address
,
1297 Addend addend
, bool is_relative
,
1298 bool is_symbolless
, bool is_section_symbol
,
1299 bool use_plt_offset
)
1300 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1301 is_symbolless
, is_section_symbol
, use_plt_offset
),
1305 // A reloc against the STT_SECTION symbol of an output section.
1307 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1308 Address address
, Addend addend
, bool is_relative
)
1309 : rel_(os
, type
, od
, address
, is_relative
), addend_(addend
)
1312 Output_reloc(Output_section
* os
, unsigned int type
,
1313 Sized_relobj
<size
, big_endian
>* relobj
,
1314 unsigned int shndx
, Address address
, Addend addend
,
1316 : rel_(os
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1319 // An absolute or relative relocation with no symbol.
1321 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1322 Addend addend
, bool is_relative
)
1323 : rel_(type
, od
, address
, is_relative
), addend_(addend
)
1326 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1327 unsigned int shndx
, Address address
, Addend addend
,
1329 : rel_(type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1332 // A target specific relocation. The target will be called to get
1333 // the symbol index and the addend, passing ARG. The type and
1334 // offset will be set as for other relocation types.
1336 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1337 Address address
, Addend addend
)
1338 : rel_(type
, arg
, od
, address
), addend_(addend
)
1341 Output_reloc(unsigned int type
, void* arg
,
1342 Sized_relobj
<size
, big_endian
>* relobj
,
1343 unsigned int shndx
, Address address
, Addend addend
)
1344 : rel_(type
, arg
, relobj
, shndx
, address
), addend_(addend
)
1347 // Return whether this is a RELATIVE relocation.
1350 { return this->rel_
.is_relative(); }
1352 // Return whether this is a relocation which should not use
1353 // a symbol, but which obtains its addend from a symbol.
1355 is_symbolless() const
1356 { return this->rel_
.is_symbolless(); }
1358 // If this relocation is against an input section, return the
1359 // relocatable object containing the input section.
1360 Sized_relobj
<size
, big_endian
>*
1362 { return this->rel_
.get_relobj(); }
1364 // Write the reloc entry to an output view.
1366 write(unsigned char* pov
) const;
1368 // Return whether this reloc should be sorted before the argument
1369 // when sorting dynamic relocs.
1371 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1374 int i
= this->rel_
.compare(r2
.rel_
);
1380 return this->addend_
< r2
.addend_
;
1385 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1390 // Output_data_reloc_generic is a non-template base class for
1391 // Output_data_reloc_base. This gives the generic code a way to hold
1392 // a pointer to a reloc section.
1394 class Output_data_reloc_generic
: public Output_section_data_build
1397 Output_data_reloc_generic(int size
, bool sort_relocs
)
1398 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1399 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1402 // Return the number of relative relocs in this section.
1404 relative_reloc_count() const
1405 { return this->relative_reloc_count_
; }
1407 // Whether we should sort the relocs.
1410 { return this->sort_relocs_
; }
1412 // Add a reloc of type TYPE against the global symbol GSYM. The
1413 // relocation applies to the data at offset ADDRESS within OD.
1415 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1416 uint64_t address
, uint64_t addend
) = 0;
1418 // Add a reloc of type TYPE against the global symbol GSYM. The
1419 // relocation applies to data at offset ADDRESS within section SHNDX
1420 // of object file RELOBJ. OD is the associated output section.
1422 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1423 Relobj
* relobj
, unsigned int shndx
, uint64_t address
,
1424 uint64_t addend
) = 0;
1426 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1427 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1430 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1431 unsigned int type
, Output_data
* od
, uint64_t address
,
1432 uint64_t addend
) = 0;
1434 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1435 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1436 // within section SHNDX of RELOBJ. OD is the associated output
1439 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1440 unsigned int type
, Output_data
* od
, unsigned int shndx
,
1441 uint64_t address
, uint64_t addend
) = 0;
1443 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1444 // output section OS. The relocation applies to the data at offset
1445 // ADDRESS within OD.
1447 add_output_section_generic(Output_section
*os
, unsigned int type
,
1448 Output_data
* od
, uint64_t address
,
1449 uint64_t addend
) = 0;
1451 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1452 // output section OS. The relocation applies to the data at offset
1453 // ADDRESS within section SHNDX of RELOBJ. OD is the associated
1456 add_output_section_generic(Output_section
* os
, unsigned int type
,
1457 Output_data
* od
, Relobj
* relobj
,
1458 unsigned int shndx
, uint64_t address
,
1459 uint64_t addend
) = 0;
1462 // Note that we've added another relative reloc.
1464 bump_relative_reloc_count()
1465 { ++this->relative_reloc_count_
; }
1468 // The number of relative relocs added to this section. This is to
1469 // support DT_RELCOUNT.
1470 size_t relative_reloc_count_
;
1471 // Whether to sort the relocations when writing them out, to make
1472 // the dynamic linker more efficient.
1476 // Output_data_reloc is used to manage a section containing relocs.
1477 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1478 // indicates whether this is a dynamic relocation or a normal
1479 // relocation. Output_data_reloc_base is a base class.
1480 // Output_data_reloc is the real class, which we specialize based on
1483 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1484 class Output_data_reloc_base
: public Output_data_reloc_generic
1487 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1488 typedef typename
Output_reloc_type::Address Address
;
1489 static const int reloc_size
=
1490 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1492 // Construct the section.
1493 Output_data_reloc_base(bool sort_relocs
)
1494 : Output_data_reloc_generic(size
, sort_relocs
)
1498 // Write out the data.
1500 do_write(Output_file
*);
1502 // Set the entry size and the link.
1504 do_adjust_output_section(Output_section
* os
);
1506 // Write to a map file.
1508 do_print_to_mapfile(Mapfile
* mapfile
) const
1510 mapfile
->print_output_data(this,
1512 ? _("** dynamic relocs")
1516 // Add a relocation entry.
1518 add(Output_data
* od
, const Output_reloc_type
& reloc
)
1520 this->relocs_
.push_back(reloc
);
1521 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1523 od
->add_dynamic_reloc();
1524 if (reloc
.is_relative())
1525 this->bump_relative_reloc_count();
1526 Sized_relobj
<size
, big_endian
>* relobj
= reloc
.get_relobj();
1528 relobj
->add_dyn_reloc(this->relocs_
.size() - 1);
1532 typedef std::vector
<Output_reloc_type
> Relocs
;
1534 // The class used to sort the relocations.
1535 struct Sort_relocs_comparison
1538 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1539 { return r1
.sort_before(r2
); }
1542 // The relocations in this section.
1546 // The class which callers actually create.
1548 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1549 class Output_data_reloc
;
1551 // The SHT_REL version of Output_data_reloc.
1553 template<bool dynamic
, int size
, bool big_endian
>
1554 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1555 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1558 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1562 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1563 typedef typename
Output_reloc_type::Address Address
;
1565 Output_data_reloc(bool sr
)
1566 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1569 // Add a reloc against a global symbol.
1572 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1574 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
,
1575 false, false, false));
1579 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1580 Sized_relobj
<size
, big_endian
>* relobj
,
1581 unsigned int shndx
, Address address
)
1583 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1584 false, false, false));
1588 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1589 uint64_t address
, uint64_t addend
)
1591 gold_assert(addend
== 0);
1592 this->add(od
, Output_reloc_type(gsym
, type
, od
,
1593 convert_types
<Address
, uint64_t>(address
),
1594 false, false, false));
1598 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1599 Relobj
* relobj
, unsigned int shndx
, uint64_t address
,
1602 gold_assert(addend
== 0);
1603 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1604 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
1605 this->add(od
, Output_reloc_type(gsym
, type
, sized_relobj
, shndx
,
1606 convert_types
<Address
, uint64_t>(address
),
1607 false, false, false));
1610 // Add a RELATIVE reloc against a global symbol. The final relocation
1611 // will not reference the symbol.
1614 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1617 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true, true,
1622 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1623 Sized_relobj
<size
, big_endian
>* relobj
,
1624 unsigned int shndx
, Address address
)
1626 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1627 true, true, false));
1630 // Add a global relocation which does not use a symbol for the relocation,
1631 // but which gets its addend from a symbol.
1634 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1635 Output_data
* od
, Address address
)
1637 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, true,
1642 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1644 Sized_relobj
<size
, big_endian
>* relobj
,
1645 unsigned int shndx
, Address address
)
1647 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1648 false, true, false));
1651 // Add a reloc against a local symbol.
1654 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1655 unsigned int local_sym_index
, unsigned int type
,
1656 Output_data
* od
, Address address
)
1658 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1659 address
, false, false, false, false));
1663 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1664 unsigned int local_sym_index
, unsigned int type
,
1665 Output_data
* od
, unsigned int shndx
, Address address
)
1667 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1668 address
, false, false, false, false));
1672 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1673 unsigned int type
, Output_data
* od
, uint64_t address
,
1676 gold_assert(addend
== 0);
1677 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1678 static_cast<Sized_relobj
<size
, big_endian
> *>(relobj
);
1679 this->add(od
, Output_reloc_type(sized_relobj
, local_sym_index
, type
, od
,
1680 convert_types
<Address
, uint64_t>(address
),
1681 false, false, false, false));
1685 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1686 unsigned int type
, Output_data
* od
, unsigned int shndx
,
1687 uint64_t address
, uint64_t addend
)
1689 gold_assert(addend
== 0);
1690 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1691 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
1692 this->add(od
, Output_reloc_type(sized_relobj
, local_sym_index
, type
, shndx
,
1693 convert_types
<Address
, uint64_t>(address
),
1694 false, false, false, false));
1697 // Add a RELATIVE reloc against a local symbol.
1700 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1701 unsigned int local_sym_index
, unsigned int type
,
1702 Output_data
* od
, Address address
)
1704 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1705 address
, true, true, false, false));
1709 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1710 unsigned int local_sym_index
, unsigned int type
,
1711 Output_data
* od
, unsigned int shndx
, Address address
)
1713 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1714 address
, true, true, false, false));
1717 // Add a local relocation which does not use a symbol for the relocation,
1718 // but which gets its addend from a symbol.
1721 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1722 unsigned int local_sym_index
, unsigned int type
,
1723 Output_data
* od
, Address address
)
1725 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1726 address
, false, true, false, false));
1730 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1731 unsigned int local_sym_index
, unsigned int type
,
1732 Output_data
* od
, unsigned int shndx
,
1735 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1736 address
, false, true, false, false));
1739 // Add a reloc against a local section symbol. This will be
1740 // converted into a reloc against the STT_SECTION symbol of the
1744 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1745 unsigned int input_shndx
, unsigned int type
,
1746 Output_data
* od
, Address address
)
1748 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1749 address
, false, false, true, false));
1753 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1754 unsigned int input_shndx
, unsigned int type
,
1755 Output_data
* od
, unsigned int shndx
, Address address
)
1757 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1758 address
, false, false, true, false));
1761 // A reloc against the STT_SECTION symbol of an output section.
1762 // OS is the Output_section that the relocation refers to; OD is
1763 // the Output_data object being relocated.
1766 add_output_section(Output_section
* os
, unsigned int type
,
1767 Output_data
* od
, Address address
)
1768 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, false)); }
1771 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1772 Sized_relobj
<size
, big_endian
>* relobj
,
1773 unsigned int shndx
, Address address
)
1774 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
, false)); }
1777 add_output_section_generic(Output_section
* os
, unsigned int type
,
1778 Output_data
* od
, uint64_t address
,
1781 gold_assert(addend
== 0);
1782 this->add(od
, Output_reloc_type(os
, type
, od
,
1783 convert_types
<Address
, uint64_t>(address
),
1788 add_output_section_generic(Output_section
* os
, unsigned int type
,
1789 Output_data
* od
, Relobj
* relobj
,
1790 unsigned int shndx
, uint64_t address
,
1793 gold_assert(addend
== 0);
1794 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1795 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
1796 this->add(od
, Output_reloc_type(os
, type
, sized_relobj
, shndx
,
1797 convert_types
<Address
, uint64_t>(address
),
1801 // As above, but the reloc TYPE is relative
1804 add_output_section_relative(Output_section
* os
, unsigned int type
,
1805 Output_data
* od
, Address address
)
1806 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, true)); }
1809 add_output_section_relative(Output_section
* os
, unsigned int type
,
1811 Sized_relobj
<size
, big_endian
>* relobj
,
1812 unsigned int shndx
, Address address
)
1813 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
, true)); }
1815 // Add an absolute relocation.
1818 add_absolute(unsigned int type
, Output_data
* od
, Address address
)
1819 { this->add(od
, Output_reloc_type(type
, od
, address
, false)); }
1822 add_absolute(unsigned int type
, Output_data
* od
,
1823 Sized_relobj
<size
, big_endian
>* relobj
,
1824 unsigned int shndx
, Address address
)
1825 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, false)); }
1827 // Add a relative relocation
1830 add_relative(unsigned int type
, Output_data
* od
, Address address
)
1831 { this->add(od
, Output_reloc_type(type
, od
, address
, true)); }
1834 add_relative(unsigned int type
, Output_data
* od
,
1835 Sized_relobj
<size
, big_endian
>* relobj
,
1836 unsigned int shndx
, Address address
)
1837 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, true)); }
1839 // Add a target specific relocation. A target which calls this must
1840 // define the reloc_symbol_index and reloc_addend virtual functions.
1843 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1845 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
)); }
1848 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1849 Sized_relobj
<size
, big_endian
>* relobj
,
1850 unsigned int shndx
, Address address
)
1851 { this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
)); }
1854 // The SHT_RELA version of Output_data_reloc.
1856 template<bool dynamic
, int size
, bool big_endian
>
1857 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1858 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1861 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1865 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1866 typedef typename
Output_reloc_type::Address Address
;
1867 typedef typename
Output_reloc_type::Addend Addend
;
1869 Output_data_reloc(bool sr
)
1870 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1873 // Add a reloc against a global symbol.
1876 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1877 Address address
, Addend addend
)
1879 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1880 false, false, false));
1884 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1885 Sized_relobj
<size
, big_endian
>* relobj
,
1886 unsigned int shndx
, Address address
,
1889 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1890 addend
, false, false, false));
1894 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1895 uint64_t address
, uint64_t addend
)
1897 this->add(od
, Output_reloc_type(gsym
, type
, od
,
1898 convert_types
<Address
, uint64_t>(address
),
1899 convert_types
<Addend
, uint64_t>(addend
),
1900 false, false, false));
1904 add_global_generic(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1905 Relobj
* relobj
, unsigned int shndx
, uint64_t address
,
1908 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1909 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
1910 this->add(od
, Output_reloc_type(gsym
, type
, sized_relobj
, shndx
,
1911 convert_types
<Address
, uint64_t>(address
),
1912 convert_types
<Addend
, uint64_t>(addend
),
1913 false, false, false));
1916 // Add a RELATIVE reloc against a global symbol. The final output
1917 // relocation will not reference the symbol, but we must keep the symbol
1918 // information long enough to set the addend of the relocation correctly
1919 // when it is written.
1922 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1923 Address address
, Addend addend
, bool use_plt_offset
)
1925 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true,
1926 true, use_plt_offset
));
1930 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1931 Sized_relobj
<size
, big_endian
>* relobj
,
1932 unsigned int shndx
, Address address
, Addend addend
,
1933 bool use_plt_offset
)
1935 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1936 addend
, true, true, use_plt_offset
));
1939 // Add a global relocation which does not use a symbol for the relocation,
1940 // but which gets its addend from a symbol.
1943 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1944 Address address
, Addend addend
)
1946 this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1947 false, true, false));
1951 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1953 Sized_relobj
<size
, big_endian
>* relobj
,
1954 unsigned int shndx
, Address address
,
1957 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1958 addend
, false, true, false));
1961 // Add a reloc against a local symbol.
1964 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1965 unsigned int local_sym_index
, unsigned int type
,
1966 Output_data
* od
, Address address
, Addend addend
)
1968 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1969 addend
, false, false, false, false));
1973 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1974 unsigned int local_sym_index
, unsigned int type
,
1975 Output_data
* od
, unsigned int shndx
, Address address
,
1978 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1979 address
, addend
, false, false, false,
1984 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1985 unsigned int type
, Output_data
* od
, uint64_t address
,
1988 Sized_relobj
<size
, big_endian
>* sized_relobj
=
1989 static_cast<Sized_relobj
<size
, big_endian
> *>(relobj
);
1990 this->add(od
, Output_reloc_type(sized_relobj
, local_sym_index
, type
, od
,
1991 convert_types
<Address
, uint64_t>(address
),
1992 convert_types
<Addend
, uint64_t>(addend
),
1993 false, false, false, false));
1997 add_local_generic(Relobj
* relobj
, unsigned int local_sym_index
,
1998 unsigned int type
, Output_data
* od
, unsigned int shndx
,
1999 uint64_t address
, uint64_t addend
)
2001 Sized_relobj
<size
, big_endian
>* sized_relobj
=
2002 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
2003 this->add(od
, Output_reloc_type(sized_relobj
, local_sym_index
, type
, shndx
,
2004 convert_types
<Address
, uint64_t>(address
),
2005 convert_types
<Addend
, uint64_t>(addend
),
2006 false, false, false, false));
2009 // Add a RELATIVE reloc against a local symbol.
2012 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
2013 unsigned int local_sym_index
, unsigned int type
,
2014 Output_data
* od
, Address address
, Addend addend
,
2015 bool use_plt_offset
)
2017 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
2018 addend
, true, true, false,
2023 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
2024 unsigned int local_sym_index
, unsigned int type
,
2025 Output_data
* od
, unsigned int shndx
, Address address
,
2026 Addend addend
, bool use_plt_offset
)
2028 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
2029 address
, addend
, true, true, false,
2033 // Add a local relocation which does not use a symbol for the relocation,
2034 // but which gets it's addend from a symbol.
2037 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
2038 unsigned int local_sym_index
, unsigned int type
,
2039 Output_data
* od
, Address address
, Addend addend
)
2041 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
2042 addend
, false, true, false, false));
2046 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
2047 unsigned int local_sym_index
, unsigned int type
,
2048 Output_data
* od
, unsigned int shndx
,
2049 Address address
, Addend addend
)
2051 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
2052 address
, addend
, false, true, false,
2056 // Add a reloc against a local section symbol. This will be
2057 // converted into a reloc against the STT_SECTION symbol of the
2061 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
2062 unsigned int input_shndx
, unsigned int type
,
2063 Output_data
* od
, Address address
, Addend addend
)
2065 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
2066 addend
, false, false, true, false));
2070 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
2071 unsigned int input_shndx
, unsigned int type
,
2072 Output_data
* od
, unsigned int shndx
, Address address
,
2075 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
2076 address
, addend
, false, false, true,
2080 // A reloc against the STT_SECTION symbol of an output section.
2083 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
2084 Address address
, Addend addend
)
2085 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, addend
, false)); }
2088 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
2089 Sized_relobj
<size
, big_endian
>* relobj
,
2090 unsigned int shndx
, Address address
, Addend addend
)
2092 this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
2097 add_output_section_generic(Output_section
* os
, unsigned int type
,
2098 Output_data
* od
, uint64_t address
,
2101 this->add(od
, Output_reloc_type(os
, type
, od
,
2102 convert_types
<Address
, uint64_t>(address
),
2103 convert_types
<Addend
, uint64_t>(addend
),
2108 add_output_section_generic(Output_section
* os
, unsigned int type
,
2109 Output_data
* od
, Relobj
* relobj
,
2110 unsigned int shndx
, uint64_t address
,
2113 Sized_relobj
<size
, big_endian
>* sized_relobj
=
2114 static_cast<Sized_relobj
<size
, big_endian
>*>(relobj
);
2115 this->add(od
, Output_reloc_type(os
, type
, sized_relobj
, shndx
,
2116 convert_types
<Address
, uint64_t>(address
),
2117 convert_types
<Addend
, uint64_t>(addend
),
2121 // As above, but the reloc TYPE is relative
2124 add_output_section_relative(Output_section
* os
, unsigned int type
,
2125 Output_data
* od
, Address address
, Addend addend
)
2126 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, addend
, true)); }
2129 add_output_section_relative(Output_section
* os
, unsigned int type
,
2131 Sized_relobj
<size
, big_endian
>* relobj
,
2132 unsigned int shndx
, Address address
,
2135 this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
,
2136 address
, addend
, true));
2139 // Add an absolute relocation.
2142 add_absolute(unsigned int type
, Output_data
* od
, Address address
,
2144 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
, false)); }
2147 add_absolute(unsigned int type
, Output_data
* od
,
2148 Sized_relobj
<size
, big_endian
>* relobj
,
2149 unsigned int shndx
, Address address
, Addend addend
)
2151 this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
,
2155 // Add a relative relocation
2158 add_relative(unsigned int type
, Output_data
* od
, Address address
,
2160 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
, true)); }
2163 add_relative(unsigned int type
, Output_data
* od
,
2164 Sized_relobj
<size
, big_endian
>* relobj
,
2165 unsigned int shndx
, Address address
, Addend addend
)
2167 this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
,
2171 // Add a target specific relocation. A target which calls this must
2172 // define the reloc_symbol_index and reloc_addend virtual functions.
2175 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
2176 Address address
, Addend addend
)
2177 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
, addend
)); }
2180 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
2181 Sized_relobj
<size
, big_endian
>* relobj
,
2182 unsigned int shndx
, Address address
, Addend addend
)
2184 this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
,
2189 // Output_relocatable_relocs represents a relocation section in a
2190 // relocatable link. The actual data is written out in the target
2191 // hook relocate_relocs. This just saves space for it.
2193 template<int sh_type
, int size
, bool big_endian
>
2194 class Output_relocatable_relocs
: public Output_section_data
2197 Output_relocatable_relocs(Relocatable_relocs
* rr
)
2198 : Output_section_data(Output_data::default_alignment_for_size(size
)),
2203 set_final_data_size();
2205 // Write out the data. There is nothing to do here.
2207 do_write(Output_file
*)
2210 // Write to a map file.
2212 do_print_to_mapfile(Mapfile
* mapfile
) const
2213 { mapfile
->print_output_data(this, _("** relocs")); }
2216 // The relocs associated with this input section.
2217 Relocatable_relocs
* rr_
;
2220 // Handle a GROUP section.
2222 template<int size
, bool big_endian
>
2223 class Output_data_group
: public Output_section_data
2226 // The constructor clears *INPUT_SHNDXES.
2227 Output_data_group(Sized_relobj_file
<size
, big_endian
>* relobj
,
2228 section_size_type entry_count
,
2229 elfcpp::Elf_Word flags
,
2230 std::vector
<unsigned int>* input_shndxes
);
2233 do_write(Output_file
*);
2235 // Write to a map file.
2237 do_print_to_mapfile(Mapfile
* mapfile
) const
2238 { mapfile
->print_output_data(this, _("** group")); }
2240 // Set final data size.
2242 set_final_data_size()
2243 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
2246 // The input object.
2247 Sized_relobj_file
<size
, big_endian
>* relobj_
;
2248 // The group flag word.
2249 elfcpp::Elf_Word flags_
;
2250 // The section indexes of the input sections in this group.
2251 std::vector
<unsigned int> input_shndxes_
;
2254 // Output_data_got is used to manage a GOT. Each entry in the GOT is
2255 // for one symbol--either a global symbol or a local symbol in an
2256 // object. The target specific code adds entries to the GOT as
2257 // needed. The GOT_SIZE template parameter is the size in bits of a
2258 // GOT entry, typically 32 or 64.
2260 class Output_data_got_base
: public Output_section_data_build
2263 Output_data_got_base(uint64_t align
)
2264 : Output_section_data_build(align
)
2267 Output_data_got_base(off_t data_size
, uint64_t align
)
2268 : Output_section_data_build(data_size
, align
)
2271 // Reserve the slot at index I in the GOT.
2273 reserve_slot(unsigned int i
)
2274 { this->do_reserve_slot(i
); }
2277 // Reserve the slot at index I in the GOT.
2279 do_reserve_slot(unsigned int i
) = 0;
2282 template<int got_size
, bool big_endian
>
2283 class Output_data_got
: public Output_data_got_base
2286 typedef typename
elfcpp::Elf_types
<got_size
>::Elf_Addr Valtype
;
2289 : Output_data_got_base(Output_data::default_alignment_for_size(got_size
)),
2290 entries_(), free_list_()
2293 Output_data_got(off_t data_size
)
2294 : Output_data_got_base(data_size
,
2295 Output_data::default_alignment_for_size(got_size
)),
2296 entries_(), free_list_()
2298 // For an incremental update, we have an existing GOT section.
2299 // Initialize the list of entries and the free list.
2300 this->entries_
.resize(data_size
/ (got_size
/ 8));
2301 this->free_list_
.init(data_size
, false);
2304 // Add an entry for a global symbol to the GOT. Return true if this
2305 // is a new GOT entry, false if the symbol was already in the GOT.
2307 add_global(Symbol
* gsym
, unsigned int got_type
);
2309 // Like add_global, but use the PLT offset of the global symbol if
2312 add_global_plt(Symbol
* gsym
, unsigned int got_type
);
2314 // Like add_global, but for a TLS symbol where the value will be
2315 // offset using Target::tls_offset_for_global.
2317 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2318 { return add_global_plt(gsym
, got_type
); }
2320 // Add an entry for a global symbol to the GOT, and add a dynamic
2321 // relocation of type R_TYPE for the GOT entry.
2323 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2324 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
);
2326 // Add a pair of entries for a global symbol to the GOT, and add
2327 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2329 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2330 Output_data_reloc_generic
* rel_dyn
,
2331 unsigned int r_type_1
, unsigned int r_type_2
);
2333 // Add an entry for a local symbol to the GOT. This returns true if
2334 // this is a new GOT entry, false if the symbol already has a GOT
2337 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
);
2339 // Like add_local, but use the PLT offset of the local symbol if it
2342 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
);
2344 // Like add_local, but for a TLS symbol where the value will be
2345 // offset using Target::tls_offset_for_local.
2347 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2348 { return add_local_plt(object
, sym_index
, got_type
); }
2350 // Add an entry for a local symbol to the GOT, and add a dynamic
2351 // relocation of type R_TYPE for the GOT entry.
2353 add_local_with_rel(Relobj
* object
, unsigned int sym_index
,
2354 unsigned int got_type
, Output_data_reloc_generic
* rel_dyn
,
2355 unsigned int r_type
);
2357 // Add a pair of entries for a local symbol to the GOT, and add
2358 // a dynamic relocation of type R_TYPE using the section symbol of
2359 // the output section to which input section SHNDX maps, on the first.
2360 // The first got entry will have a value of zero, the second the
2361 // value of the local symbol.
2363 add_local_pair_with_rel(Relobj
* object
, unsigned int sym_index
,
2364 unsigned int shndx
, unsigned int got_type
,
2365 Output_data_reloc_generic
* rel_dyn
,
2366 unsigned int r_type
);
2368 // Add a pair of entries for a local symbol to the GOT, and add
2369 // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
2370 // The first got entry will have a value of zero, the second the
2371 // value of the local symbol offset by Target::tls_offset_for_local.
2373 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2374 unsigned int got_type
,
2375 Output_data_reloc_generic
* rel_dyn
,
2376 unsigned int r_type
);
2378 // Add a constant to the GOT. This returns the offset of the new
2379 // entry from the start of the GOT.
2381 add_constant(Valtype constant
)
2382 { return this->add_got_entry(Got_entry(constant
)); }
2384 // Add a pair of constants to the GOT. This returns the offset of
2385 // the new entry from the start of the GOT.
2387 add_constant_pair(Valtype c1
, Valtype c2
)
2388 { return this->add_got_entry_pair(Got_entry(c1
), Got_entry(c2
)); }
2390 // Replace GOT entry I with a new constant.
2392 replace_constant(unsigned int i
, Valtype constant
)
2394 this->replace_got_entry(i
, Got_entry(constant
));
2397 // Reserve a slot in the GOT for a local symbol.
2399 reserve_local(unsigned int i
, Relobj
* object
, unsigned int sym_index
,
2400 unsigned int got_type
);
2402 // Reserve a slot in the GOT for a global symbol.
2404 reserve_global(unsigned int i
, Symbol
* gsym
, unsigned int got_type
);
2407 // Write out the GOT table.
2409 do_write(Output_file
*);
2411 // Write to a map file.
2413 do_print_to_mapfile(Mapfile
* mapfile
) const
2414 { mapfile
->print_output_data(this, _("** GOT")); }
2416 // Reserve the slot at index I in the GOT.
2418 do_reserve_slot(unsigned int i
)
2419 { this->free_list_
.remove(i
* got_size
/ 8, (i
+ 1) * got_size
/ 8); }
2421 // Return the number of words in the GOT.
2423 num_entries () const
2424 { return this->entries_
.size(); }
2426 // Return the offset into the GOT of GOT entry I.
2428 got_offset(unsigned int i
) const
2429 { return i
* (got_size
/ 8); }
2432 // This POD class holds a single GOT entry.
2436 // Create a zero entry.
2438 : local_sym_index_(RESERVED_CODE
), use_plt_or_tls_offset_(false)
2439 { this->u_
.constant
= 0; }
2441 // Create a global symbol entry.
2442 Got_entry(Symbol
* gsym
, bool use_plt_or_tls_offset
)
2443 : local_sym_index_(GSYM_CODE
),
2444 use_plt_or_tls_offset_(use_plt_or_tls_offset
)
2445 { this->u_
.gsym
= gsym
; }
2447 // Create a local symbol entry.
2448 Got_entry(Relobj
* object
, unsigned int local_sym_index
,
2449 bool use_plt_or_tls_offset
)
2450 : local_sym_index_(local_sym_index
),
2451 use_plt_or_tls_offset_(use_plt_or_tls_offset
)
2453 gold_assert(local_sym_index
!= GSYM_CODE
2454 && local_sym_index
!= CONSTANT_CODE
2455 && local_sym_index
!= RESERVED_CODE
2456 && local_sym_index
== this->local_sym_index_
);
2457 this->u_
.object
= object
;
2460 // Create a constant entry. The constant is a host value--it will
2461 // be swapped, if necessary, when it is written out.
2462 explicit Got_entry(Valtype constant
)
2463 : local_sym_index_(CONSTANT_CODE
), use_plt_or_tls_offset_(false)
2464 { this->u_
.constant
= constant
; }
2466 // Write the GOT entry to an output view.
2468 write(unsigned int got_indx
, unsigned char* pov
) const;
2473 GSYM_CODE
= 0x7fffffff,
2474 CONSTANT_CODE
= 0x7ffffffe,
2475 RESERVED_CODE
= 0x7ffffffd
2480 // For a local symbol, the object.
2482 // For a global symbol, the symbol.
2484 // For a constant, the constant.
2487 // For a local symbol, the local symbol index. This is GSYM_CODE
2488 // for a global symbol, or CONSTANT_CODE for a constant.
2489 unsigned int local_sym_index_
: 31;
2490 // Whether to use the PLT offset of the symbol if it has one.
2491 // For TLS symbols, whether to offset the symbol value.
2492 bool use_plt_or_tls_offset_
: 1;
2495 typedef std::vector
<Got_entry
> Got_entries
;
2497 // Create a new GOT entry and return its offset.
2499 add_got_entry(Got_entry got_entry
);
2501 // Create a pair of new GOT entries and return the offset of the first.
2503 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
);
2505 // Replace GOT entry I with a new value.
2507 replace_got_entry(unsigned int i
, Got_entry got_entry
);
2509 // Return the offset into the GOT of the last entry added.
2511 last_got_offset() const
2512 { return this->got_offset(this->num_entries() - 1); }
2514 // Set the size of the section.
2517 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2519 // The list of GOT entries.
2520 Got_entries entries_
;
2522 // List of available regions within the section, for incremental
2524 Free_list free_list_
;
2527 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2530 class Output_data_dynamic
: public Output_section_data
2533 Output_data_dynamic(Stringpool
* pool
)
2534 : Output_section_data(Output_data::default_alignment()),
2535 entries_(), pool_(pool
)
2538 // Add a new dynamic entry with a fixed numeric value.
2540 add_constant(elfcpp::DT tag
, unsigned int val
)
2541 { this->add_entry(Dynamic_entry(tag
, val
)); }
2543 // Add a new dynamic entry with the address of output data.
2545 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
2546 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
2548 // Add a new dynamic entry with the address of output data
2549 // plus a constant offset.
2551 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
2552 unsigned int offset
)
2553 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
2555 // Add a new dynamic entry with the size of output data.
2557 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
2558 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
2560 // Add a new dynamic entry with the total size of two output datas.
2562 add_section_size(elfcpp::DT tag
, const Output_data
* od
,
2563 const Output_data
* od2
)
2564 { this->add_entry(Dynamic_entry(tag
, od
, od2
)); }
2566 // Add a new dynamic entry with the address of a symbol.
2568 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
2569 { this->add_entry(Dynamic_entry(tag
, sym
)); }
2571 // Add a new dynamic entry with a string.
2573 add_string(elfcpp::DT tag
, const char* str
)
2574 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
2577 add_string(elfcpp::DT tag
, const std::string
& str
)
2578 { this->add_string(tag
, str
.c_str()); }
2581 // Adjust the output section to set the entry size.
2583 do_adjust_output_section(Output_section
*);
2585 // Set the final data size.
2587 set_final_data_size();
2589 // Write out the dynamic entries.
2591 do_write(Output_file
*);
2593 // Write to a map file.
2595 do_print_to_mapfile(Mapfile
* mapfile
) const
2596 { mapfile
->print_output_data(this, _("** dynamic")); }
2599 // This POD class holds a single dynamic entry.
2603 // Create an entry with a fixed numeric value.
2604 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
2605 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
2606 { this->u_
.val
= val
; }
2608 // Create an entry with the size or address of a section.
2609 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
2611 offset_(section_size
2612 ? DYNAMIC_SECTION_SIZE
2613 : DYNAMIC_SECTION_ADDRESS
)
2619 // Create an entry with the size of two sections.
2620 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, const Output_data
* od2
)
2622 offset_(DYNAMIC_SECTION_SIZE
)
2628 // Create an entry with the address of a section plus a constant offset.
2629 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
2632 { this->u_
.od
= od
; }
2634 // Create an entry with the address of a symbol.
2635 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
2636 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
2637 { this->u_
.sym
= sym
; }
2639 // Create an entry with a string.
2640 Dynamic_entry(elfcpp::DT tag
, const char* str
)
2641 : tag_(tag
), offset_(DYNAMIC_STRING
)
2642 { this->u_
.str
= str
; }
2644 // Return the tag of this entry.
2647 { return this->tag_
; }
2649 // Write the dynamic entry to an output view.
2650 template<int size
, bool big_endian
>
2652 write(unsigned char* pov
, const Stringpool
*) const;
2655 // Classification is encoded in the OFFSET field.
2659 DYNAMIC_SECTION_ADDRESS
= 0,
2661 DYNAMIC_NUMBER
= -1U,
2663 DYNAMIC_SECTION_SIZE
= -2U,
2665 DYNAMIC_SYMBOL
= -3U,
2667 DYNAMIC_STRING
= -4U
2668 // Any other value indicates a section address plus OFFSET.
2673 // For DYNAMIC_NUMBER.
2675 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2676 const Output_data
* od
;
2677 // For DYNAMIC_SYMBOL.
2679 // For DYNAMIC_STRING.
2682 // For DYNAMIC_SYMBOL with two sections.
2683 const Output_data
* od2
;
2686 // The type of entry (Classification) or offset within a section.
2687 unsigned int offset_
;
2690 // Add an entry to the list.
2692 add_entry(const Dynamic_entry
& entry
)
2693 { this->entries_
.push_back(entry
); }
2695 // Sized version of write function.
2696 template<int size
, bool big_endian
>
2698 sized_write(Output_file
* of
);
2700 // The type of the list of entries.
2701 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2704 Dynamic_entries entries_
;
2705 // The pool used for strings.
2709 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2710 // which may be required if the object file has more than
2711 // SHN_LORESERVE sections.
2713 class Output_symtab_xindex
: public Output_section_data
2716 Output_symtab_xindex(size_t symcount
)
2717 : Output_section_data(symcount
* 4, 4, true),
2721 // Add an entry: symbol number SYMNDX has section SHNDX.
2723 add(unsigned int symndx
, unsigned int shndx
)
2724 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2728 do_write(Output_file
*);
2730 // Write to a map file.
2732 do_print_to_mapfile(Mapfile
* mapfile
) const
2733 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2736 template<bool big_endian
>
2738 endian_do_write(unsigned char*);
2740 // It is likely that most symbols will not require entries. Rather
2741 // than keep a vector for all symbols, we keep pairs of symbol index
2742 // and section index.
2743 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2745 // The entries we need.
2746 Xindex_entries entries_
;
2749 // A relaxed input section.
2750 class Output_relaxed_input_section
: public Output_section_data_build
2753 // We would like to call relobj->section_addralign(shndx) to get the
2754 // alignment but we do not want the constructor to fail. So callers
2755 // are repsonsible for ensuring that.
2756 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2758 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2761 // Return the Relobj of this relaxed input section.
2764 { return this->relobj_
; }
2766 // Return the section index of this relaxed input section.
2769 { return this->shndx_
; }
2773 set_relobj(Relobj
* relobj
)
2774 { this->relobj_
= relobj
; }
2777 set_shndx(unsigned int shndx
)
2778 { this->shndx_
= shndx
; }
2782 unsigned int shndx_
;
2785 // This class describes properties of merge data sections. It is used
2786 // as a key type for maps.
2787 class Merge_section_properties
2790 Merge_section_properties(bool is_string
, uint64_t entsize
,
2792 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
2795 // Whether this equals to another Merge_section_properties MSP.
2797 eq(const Merge_section_properties
& msp
) const
2799 return ((this->is_string_
== msp
.is_string_
)
2800 && (this->entsize_
== msp
.entsize_
)
2801 && (this->addralign_
== msp
.addralign_
));
2804 // Compute a hash value for this using 64-bit FNV-1a hash.
2808 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
2809 uint64_t prime
= 1099511628211ULL;
2810 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
2811 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
2812 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
2816 // Functors for associative containers.
2820 operator()(const Merge_section_properties
& msp1
,
2821 const Merge_section_properties
& msp2
) const
2822 { return msp1
.eq(msp2
); }
2828 operator()(const Merge_section_properties
& msp
) const
2829 { return msp
.hash_value(); }
2833 // Whether this merge data section is for strings.
2835 // Entsize of this merge data section.
2837 // Address alignment.
2838 uint64_t addralign_
;
2841 // This class is used to speed up look up of special input sections in an
2844 class Output_section_lookup_maps
2847 Output_section_lookup_maps()
2848 : is_valid_(true), merge_sections_by_properties_(),
2849 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2852 // Whether the maps are valid.
2855 { return this->is_valid_
; }
2857 // Invalidate the maps.
2860 { this->is_valid_
= false; }
2866 this->merge_sections_by_properties_
.clear();
2867 this->merge_sections_by_id_
.clear();
2868 this->relaxed_input_sections_by_id_
.clear();
2869 // A cleared map is valid.
2870 this->is_valid_
= true;
2873 // Find a merge section by merge section properties. Return NULL if none
2876 find_merge_section(const Merge_section_properties
& msp
) const
2878 gold_assert(this->is_valid_
);
2879 Merge_sections_by_properties::const_iterator p
=
2880 this->merge_sections_by_properties_
.find(msp
);
2881 return p
!= this->merge_sections_by_properties_
.end() ? p
->second
: NULL
;
2884 // Find a merge section by section ID of a merge input section. Return NULL
2885 // if none is found.
2887 find_merge_section(const Object
* object
, unsigned int shndx
) const
2889 gold_assert(this->is_valid_
);
2890 Merge_sections_by_id::const_iterator p
=
2891 this->merge_sections_by_id_
.find(Const_section_id(object
, shndx
));
2892 return p
!= this->merge_sections_by_id_
.end() ? p
->second
: NULL
;
2895 // Add a merge section pointed by POMB with properties MSP.
2897 add_merge_section(const Merge_section_properties
& msp
,
2898 Output_merge_base
* pomb
)
2900 std::pair
<Merge_section_properties
, Output_merge_base
*> value(msp
, pomb
);
2901 std::pair
<Merge_sections_by_properties::iterator
, bool> result
=
2902 this->merge_sections_by_properties_
.insert(value
);
2903 gold_assert(result
.second
);
2906 // Add a mapping from a merged input section in OBJECT with index SHNDX
2907 // to a merge output section pointed by POMB.
2909 add_merge_input_section(const Object
* object
, unsigned int shndx
,
2910 Output_merge_base
* pomb
)
2912 Const_section_id
csid(object
, shndx
);
2913 std::pair
<Const_section_id
, Output_merge_base
*> value(csid
, pomb
);
2914 std::pair
<Merge_sections_by_id::iterator
, bool> result
=
2915 this->merge_sections_by_id_
.insert(value
);
2916 gold_assert(result
.second
);
2919 // Find a relaxed input section of OBJECT with index SHNDX.
2920 Output_relaxed_input_section
*
2921 find_relaxed_input_section(const Object
* object
, unsigned int shndx
) const
2923 gold_assert(this->is_valid_
);
2924 Relaxed_input_sections_by_id::const_iterator p
=
2925 this->relaxed_input_sections_by_id_
.find(Const_section_id(object
, shndx
));
2926 return p
!= this->relaxed_input_sections_by_id_
.end() ? p
->second
: NULL
;
2929 // Add a relaxed input section pointed by POMB and whose original input
2930 // section is in OBJECT with index SHNDX.
2932 add_relaxed_input_section(const Relobj
* relobj
, unsigned int shndx
,
2933 Output_relaxed_input_section
* poris
)
2935 Const_section_id
csid(relobj
, shndx
);
2936 std::pair
<Const_section_id
, Output_relaxed_input_section
*>
2938 std::pair
<Relaxed_input_sections_by_id::iterator
, bool> result
=
2939 this->relaxed_input_sections_by_id_
.insert(value
);
2940 gold_assert(result
.second
);
2944 typedef Unordered_map
<Const_section_id
, Output_merge_base
*,
2945 Const_section_id_hash
>
2946 Merge_sections_by_id
;
2948 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
2949 Merge_section_properties::hash
,
2950 Merge_section_properties::equal_to
>
2951 Merge_sections_by_properties
;
2953 typedef Unordered_map
<Const_section_id
, Output_relaxed_input_section
*,
2954 Const_section_id_hash
>
2955 Relaxed_input_sections_by_id
;
2957 // Whether this is valid
2959 // Merge sections by merge section properties.
2960 Merge_sections_by_properties merge_sections_by_properties_
;
2961 // Merge sections by section IDs.
2962 Merge_sections_by_id merge_sections_by_id_
;
2963 // Relaxed sections by section IDs.
2964 Relaxed_input_sections_by_id relaxed_input_sections_by_id_
;
2967 // This abstract base class defines the interface for the
2968 // types of methods used to fill free space left in an output
2969 // section during an incremental link. These methods are used
2970 // to insert dummy compilation units into debug info so that
2971 // debug info consumers can scan the debug info serially.
2977 : is_big_endian_(parameters
->target().is_big_endian())
2984 // Return the smallest size chunk of free space that can be
2985 // filled with a dummy compilation unit.
2987 minimum_hole_size() const
2988 { return this->do_minimum_hole_size(); }
2990 // Write a fill pattern of length LEN at offset OFF in the file.
2992 write(Output_file
* of
, off_t off
, size_t len
) const
2993 { this->do_write(of
, off
, len
); }
2997 do_minimum_hole_size() const = 0;
3000 do_write(Output_file
* of
, off_t off
, size_t len
) const = 0;
3003 is_big_endian() const
3004 { return this->is_big_endian_
; }
3007 bool is_big_endian_
;
3010 // Fill method that introduces a dummy compilation unit in
3011 // a .debug_info or .debug_types section.
3013 class Output_fill_debug_info
: public Output_fill
3016 Output_fill_debug_info(bool is_debug_types
)
3017 : is_debug_types_(is_debug_types
)
3022 do_minimum_hole_size() const;
3025 do_write(Output_file
* of
, off_t off
, size_t len
) const;
3028 // Version of the header.
3029 static const int version
= 4;
3030 // True if this is a .debug_types section.
3031 bool is_debug_types_
;
3034 // Fill method that introduces a dummy compilation unit in
3035 // a .debug_line section.
3037 class Output_fill_debug_line
: public Output_fill
3040 Output_fill_debug_line()
3045 do_minimum_hole_size() const;
3048 do_write(Output_file
* of
, off_t off
, size_t len
) const;
3051 // Version of the header. We write a DWARF-3 header because it's smaller
3052 // and many tools have not yet been updated to understand the DWARF-4 header.
3053 static const int version
= 3;
3054 // Length of the portion of the header that follows the header_length
3055 // field. This includes the following fields:
3056 // minimum_instruction_length, default_is_stmt, line_base, line_range,
3057 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3058 // The standard_opcode_lengths array is 12 bytes long, and the
3059 // include_directories and filenames fields each contain only a single
3061 static const size_t header_length
= 19;
3064 // An output section. We don't expect to have too many output
3065 // sections, so we don't bother to do a template on the size.
3067 class Output_section
: public Output_data
3070 // Create an output section, giving the name, type, and flags.
3071 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
3072 virtual ~Output_section();
3074 // Add a new input section SHNDX, named NAME, with header SHDR, from
3075 // object OBJECT. RELOC_SHNDX is the index of a relocation section
3076 // which applies to this section, or 0 if none, or -1 if more than
3077 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3078 // in a linker script; in that case we need to keep track of input
3079 // sections associated with an output section. Return the offset
3080 // within the output section.
3081 template<int size
, bool big_endian
>
3083 add_input_section(Layout
* layout
, Sized_relobj_file
<size
, big_endian
>* object
,
3084 unsigned int shndx
, const char* name
,
3085 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
3086 unsigned int reloc_shndx
, bool have_sections_script
);
3088 // Add generated data POSD to this output section.
3090 add_output_section_data(Output_section_data
* posd
);
3092 // Add a relaxed input section PORIS called NAME to this output section
3095 add_relaxed_input_section(Layout
* layout
,
3096 Output_relaxed_input_section
* poris
,
3097 const std::string
& name
);
3099 // Return the section name.
3102 { return this->name_
; }
3104 // Return the section type.
3107 { return this->type_
; }
3109 // Return the section flags.
3112 { return this->flags_
; }
3114 typedef std::map
<Section_id
, unsigned int> Section_layout_order
;
3117 update_section_layout(const Section_layout_order
* order_map
);
3119 // Update the output section flags based on input section flags.
3121 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
3123 // Return the entsize field.
3126 { return this->entsize_
; }
3128 // Set the entsize field.
3130 set_entsize(uint64_t v
);
3132 // Set the load address.
3134 set_load_address(uint64_t load_address
)
3136 this->load_address_
= load_address
;
3137 this->has_load_address_
= true;
3140 // Set the link field to the output section index of a section.
3142 set_link_section(const Output_data
* od
)
3144 gold_assert(this->link_
== 0
3145 && !this->should_link_to_symtab_
3146 && !this->should_link_to_dynsym_
);
3147 this->link_section_
= od
;
3150 // Set the link field to a constant.
3152 set_link(unsigned int v
)
3154 gold_assert(this->link_section_
== NULL
3155 && !this->should_link_to_symtab_
3156 && !this->should_link_to_dynsym_
);
3160 // Record that this section should link to the normal symbol table.
3162 set_should_link_to_symtab()
3164 gold_assert(this->link_section_
== NULL
3166 && !this->should_link_to_dynsym_
);
3167 this->should_link_to_symtab_
= true;
3170 // Record that this section should link to the dynamic symbol table.
3172 set_should_link_to_dynsym()
3174 gold_assert(this->link_section_
== NULL
3176 && !this->should_link_to_symtab_
);
3177 this->should_link_to_dynsym_
= true;
3180 // Return the info field.
3184 gold_assert(this->info_section_
== NULL
3185 && this->info_symndx_
== NULL
);
3189 // Set the info field to the output section index of a section.
3191 set_info_section(const Output_section
* os
)
3193 gold_assert((this->info_section_
== NULL
3194 || (this->info_section_
== os
3195 && this->info_uses_section_index_
))
3196 && this->info_symndx_
== NULL
3197 && this->info_
== 0);
3198 this->info_section_
= os
;
3199 this->info_uses_section_index_
= true;
3202 // Set the info field to the symbol table index of a symbol.
3204 set_info_symndx(const Symbol
* sym
)
3206 gold_assert(this->info_section_
== NULL
3207 && (this->info_symndx_
== NULL
3208 || this->info_symndx_
== sym
)
3209 && this->info_
== 0);
3210 this->info_symndx_
= sym
;
3213 // Set the info field to the symbol table index of a section symbol.
3215 set_info_section_symndx(const Output_section
* os
)
3217 gold_assert((this->info_section_
== NULL
3218 || (this->info_section_
== os
3219 && !this->info_uses_section_index_
))
3220 && this->info_symndx_
== NULL
3221 && this->info_
== 0);
3222 this->info_section_
= os
;
3223 this->info_uses_section_index_
= false;
3226 // Set the info field to a constant.
3228 set_info(unsigned int v
)
3230 gold_assert(this->info_section_
== NULL
3231 && this->info_symndx_
== NULL
3232 && (this->info_
== 0
3233 || this->info_
== v
));
3237 // Set the addralign field.
3239 set_addralign(uint64_t v
)
3240 { this->addralign_
= v
; }
3243 checkpoint_set_addralign(uint64_t val
)
3245 if (this->checkpoint_
!= NULL
)
3246 this->checkpoint_
->set_addralign(val
);
3249 // Whether the output section index has been set.
3251 has_out_shndx() const
3252 { return this->out_shndx_
!= -1U; }
3254 // Indicate that we need a symtab index.
3256 set_needs_symtab_index()
3257 { this->needs_symtab_index_
= true; }
3259 // Return whether we need a symtab index.
3261 needs_symtab_index() const
3262 { return this->needs_symtab_index_
; }
3264 // Get the symtab index.
3266 symtab_index() const
3268 gold_assert(this->symtab_index_
!= 0);
3269 return this->symtab_index_
;
3272 // Set the symtab index.
3274 set_symtab_index(unsigned int index
)
3276 gold_assert(index
!= 0);
3277 this->symtab_index_
= index
;
3280 // Indicate that we need a dynsym index.
3282 set_needs_dynsym_index()
3283 { this->needs_dynsym_index_
= true; }
3285 // Return whether we need a dynsym index.
3287 needs_dynsym_index() const
3288 { return this->needs_dynsym_index_
; }
3290 // Get the dynsym index.
3292 dynsym_index() const
3294 gold_assert(this->dynsym_index_
!= 0);
3295 return this->dynsym_index_
;
3298 // Set the dynsym index.
3300 set_dynsym_index(unsigned int index
)
3302 gold_assert(index
!= 0);
3303 this->dynsym_index_
= index
;
3306 // Sort the attached input sections.
3308 sort_attached_input_sections();
3310 // Return whether the input sections sections attachd to this output
3311 // section may require sorting. This is used to handle constructor
3312 // priorities compatibly with GNU ld.
3314 may_sort_attached_input_sections() const
3315 { return this->may_sort_attached_input_sections_
; }
3317 // Record that the input sections attached to this output section
3318 // may require sorting.
3320 set_may_sort_attached_input_sections()
3321 { this->may_sort_attached_input_sections_
= true; }
3323 // Returns true if input sections must be sorted according to the
3324 // order in which their name appear in the --section-ordering-file.
3326 input_section_order_specified()
3327 { return this->input_section_order_specified_
; }
3329 // Record that input sections must be sorted as some of their names
3330 // match the patterns specified through --section-ordering-file.
3332 set_input_section_order_specified()
3333 { this->input_section_order_specified_
= true; }
3335 // Return whether the input sections attached to this output section
3336 // require sorting. This is used to handle constructor priorities
3337 // compatibly with GNU ld.
3339 must_sort_attached_input_sections() const
3340 { return this->must_sort_attached_input_sections_
; }
3342 // Record that the input sections attached to this output section
3345 set_must_sort_attached_input_sections()
3346 { this->must_sort_attached_input_sections_
= true; }
3348 // Get the order in which this section appears in the PT_LOAD output
3350 Output_section_order
3352 { return this->order_
; }
3354 // Set the order for this section.
3356 set_order(Output_section_order order
)
3357 { this->order_
= order
; }
3359 // Return whether this section holds relro data--data which has
3360 // dynamic relocations but which may be marked read-only after the
3361 // dynamic relocations have been completed.
3364 { return this->is_relro_
; }
3366 // Record that this section holds relro data.
3369 { this->is_relro_
= true; }
3371 // Record that this section does not hold relro data.
3374 { this->is_relro_
= false; }
3376 // True if this is a small section: a section which holds small
3379 is_small_section() const
3380 { return this->is_small_section_
; }
3382 // Record that this is a small section.
3384 set_is_small_section()
3385 { this->is_small_section_
= true; }
3387 // True if this is a large section: a section which holds large
3390 is_large_section() const
3391 { return this->is_large_section_
; }
3393 // Record that this is a large section.
3395 set_is_large_section()
3396 { this->is_large_section_
= true; }
3398 // True if this is a large data (not BSS) section.
3400 is_large_data_section()
3401 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
3403 // Return whether this section should be written after all the input
3404 // sections are complete.
3406 after_input_sections() const
3407 { return this->after_input_sections_
; }
3409 // Record that this section should be written after all the input
3410 // sections are complete.
3412 set_after_input_sections()
3413 { this->after_input_sections_
= true; }
3415 // Return whether this section requires postprocessing after all
3416 // relocations have been applied.
3418 requires_postprocessing() const
3419 { return this->requires_postprocessing_
; }
3422 is_unique_segment() const
3423 { return this->is_unique_segment_
; }
3426 set_is_unique_segment()
3427 { this->is_unique_segment_
= true; }
3429 uint64_t extra_segment_flags() const
3430 { return this->extra_segment_flags_
; }
3433 set_extra_segment_flags(uint64_t flags
)
3434 { this->extra_segment_flags_
= flags
; }
3436 uint64_t segment_alignment() const
3437 { return this->segment_alignment_
; }
3440 set_segment_alignment(uint64_t align
)
3441 { this->segment_alignment_
= align
; }
3443 // If a section requires postprocessing, return the buffer to use.
3445 postprocessing_buffer() const
3447 gold_assert(this->postprocessing_buffer_
!= NULL
);
3448 return this->postprocessing_buffer_
;
3451 // If a section requires postprocessing, create the buffer to use.
3453 create_postprocessing_buffer();
3455 // If a section requires postprocessing, this is the size of the
3456 // buffer to which relocations should be applied.
3458 postprocessing_buffer_size() const
3459 { return this->current_data_size_for_child(); }
3461 // Modify the section name. This is only permitted for an
3462 // unallocated section, and only before the size has been finalized.
3463 // Otherwise the name will not get into Layout::namepool_.
3465 set_name(const char* newname
)
3467 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
3468 gold_assert(!this->is_data_size_valid());
3469 this->name_
= newname
;
3472 // Return whether the offset OFFSET in the input section SHNDX in
3473 // object OBJECT is being included in the link.
3475 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
3476 off_t offset
) const;
3478 // Return the offset within the output section of OFFSET relative to
3479 // the start of input section SHNDX in object OBJECT.
3481 output_offset(const Relobj
* object
, unsigned int shndx
,
3482 section_offset_type offset
) const;
3484 // Return the output virtual address of OFFSET relative to the start
3485 // of input section SHNDX in object OBJECT.
3487 output_address(const Relobj
* object
, unsigned int shndx
,
3488 off_t offset
) const;
3490 // Look for the merged section for input section SHNDX in object
3491 // OBJECT. If found, return true, and set *ADDR to the address of
3492 // the start of the merged section. This is not necessary the
3493 // output offset corresponding to input offset 0 in the section,
3494 // since the section may be mapped arbitrarily.
3496 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
3497 uint64_t* addr
) const;
3499 // Record that this output section was found in the SECTIONS clause
3500 // of a linker script.
3502 set_found_in_sections_clause()
3503 { this->found_in_sections_clause_
= true; }
3505 // Return whether this output section was found in the SECTIONS
3506 // clause of a linker script.
3508 found_in_sections_clause() const
3509 { return this->found_in_sections_clause_
; }
3511 // Write the section header into *OPHDR.
3512 template<int size
, bool big_endian
>
3514 write_header(const Layout
*, const Stringpool
*,
3515 elfcpp::Shdr_write
<size
, big_endian
>*) const;
3517 // The next few calls are for linker script support.
3519 // In some cases we need to keep a list of the input sections
3520 // associated with this output section. We only need the list if we
3521 // might have to change the offsets of the input section within the
3522 // output section after we add the input section. The ordinary
3523 // input sections will be written out when we process the object
3524 // file, and as such we don't need to track them here. We do need
3525 // to track Output_section_data objects here. We store instances of
3526 // this structure in a std::vector, so it must be a POD. There can
3527 // be many instances of this structure, so we use a union to save
3533 : shndx_(0), p2align_(0)
3535 this->u1_
.data_size
= 0;
3536 this->u2_
.object
= NULL
;
3539 // For an ordinary input section.
3540 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
3543 p2align_(ffsll(static_cast<long long>(addralign
))),
3544 section_order_index_(0)
3546 gold_assert(shndx
!= OUTPUT_SECTION_CODE
3547 && shndx
!= MERGE_DATA_SECTION_CODE
3548 && shndx
!= MERGE_STRING_SECTION_CODE
3549 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
3550 this->u1_
.data_size
= data_size
;
3551 this->u2_
.object
= object
;
3554 // For a non-merge output section.
3555 Input_section(Output_section_data
* posd
)
3556 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0),
3557 section_order_index_(0)
3559 this->u1_
.data_size
= 0;
3560 this->u2_
.posd
= posd
;
3563 // For a merge section.
3564 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
3566 ? MERGE_STRING_SECTION_CODE
3567 : MERGE_DATA_SECTION_CODE
),
3569 section_order_index_(0)
3571 this->u1_
.entsize
= entsize
;
3572 this->u2_
.posd
= posd
;
3575 // For a relaxed input section.
3576 Input_section(Output_relaxed_input_section
* psection
)
3577 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0),
3578 section_order_index_(0)
3580 this->u1_
.data_size
= 0;
3581 this->u2_
.poris
= psection
;
3585 section_order_index() const
3587 return this->section_order_index_
;
3591 set_section_order_index(unsigned int number
)
3593 this->section_order_index_
= number
;
3596 // The required alignment.
3600 if (this->p2align_
!= 0)
3601 return static_cast<uint64_t>(1) << (this->p2align_
- 1);
3602 else if (!this->is_input_section())
3603 return this->u2_
.posd
->addralign();
3608 // Set the required alignment, which must be either 0 or a power of 2.
3609 // For input sections that are sub-classes of Output_section_data, a
3610 // alignment of zero means asking the underlying object for alignment.
3612 set_addralign(uint64_t addralign
)
3618 gold_assert((addralign
& (addralign
- 1)) == 0);
3619 this->p2align_
= ffsll(static_cast<long long>(addralign
));
3623 // Return the current required size, without finalization.
3625 current_data_size() const;
3627 // Return the required size.
3631 // Whether this is an input section.
3633 is_input_section() const
3635 return (this->shndx_
!= OUTPUT_SECTION_CODE
3636 && this->shndx_
!= MERGE_DATA_SECTION_CODE
3637 && this->shndx_
!= MERGE_STRING_SECTION_CODE
3638 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
3641 // Return whether this is a merge section which matches the
3644 is_merge_section(bool is_string
, uint64_t entsize
,
3645 uint64_t addralign
) const
3647 return (this->shndx_
== (is_string
3648 ? MERGE_STRING_SECTION_CODE
3649 : MERGE_DATA_SECTION_CODE
)
3650 && this->u1_
.entsize
== entsize
3651 && this->addralign() == addralign
);
3654 // Return whether this is a merge section for some input section.
3656 is_merge_section() const
3658 return (this->shndx_
== MERGE_DATA_SECTION_CODE
3659 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3662 // Return whether this is a relaxed input section.
3664 is_relaxed_input_section() const
3665 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
3667 // Return whether this is a generic Output_section_data.
3669 is_output_section_data() const
3671 return this->shndx_
== OUTPUT_SECTION_CODE
;
3674 // Return the object for an input section.
3678 // Return the input section index for an input section.
3682 // For non-input-sections, return the associated Output_section_data
3684 Output_section_data
*
3685 output_section_data() const
3687 gold_assert(!this->is_input_section());
3688 return this->u2_
.posd
;
3691 // For a merge section, return the Output_merge_base pointer.
3693 output_merge_base() const
3695 gold_assert(this->is_merge_section());
3696 return this->u2_
.pomb
;
3699 // Return the Output_relaxed_input_section object.
3700 Output_relaxed_input_section
*
3701 relaxed_input_section() const
3703 gold_assert(this->is_relaxed_input_section());
3704 return this->u2_
.poris
;
3707 // Set the output section.
3709 set_output_section(Output_section
* os
)
3711 gold_assert(!this->is_input_section());
3712 Output_section_data
* posd
=
3713 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
3714 posd
->set_output_section(os
);
3717 // Set the address and file offset. This is called during
3718 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3719 // the enclosing section.
3721 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
3722 off_t section_file_offset
);
3724 // Reset the address and file offset.
3726 reset_address_and_file_offset();
3728 // Finalize the data size.
3730 finalize_data_size();
3732 // Add an input section, for SHF_MERGE sections.
3734 add_input_section(Relobj
* object
, unsigned int shndx
)
3736 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
3737 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3738 return this->u2_
.posd
->add_input_section(object
, shndx
);
3741 // Given an input OBJECT, an input section index SHNDX within that
3742 // object, and an OFFSET relative to the start of that input
3743 // section, return whether or not the output offset is known. If
3744 // this function returns true, it sets *POUTPUT to the offset in
3745 // the output section, relative to the start of the input section
3746 // in the output section. *POUTPUT may be different from OFFSET
3747 // for a merged section.
3749 output_offset(const Relobj
* object
, unsigned int shndx
,
3750 section_offset_type offset
,
3751 section_offset_type
* poutput
) const;
3753 // Return whether this is the merge section for the input section
3756 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3758 // Write out the data. This does nothing for an input section.
3760 write(Output_file
*);
3762 // Write the data to a buffer. This does nothing for an input
3765 write_to_buffer(unsigned char*);
3767 // Print to a map file.
3769 print_to_mapfile(Mapfile
*) const;
3771 // Print statistics about merge sections to stderr.
3773 print_merge_stats(const char* section_name
)
3775 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3776 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3777 this->u2_
.posd
->print_merge_stats(section_name
);
3781 // Code values which appear in shndx_. If the value is not one of
3782 // these codes, it is the input section index in the object file.
3785 // An Output_section_data.
3786 OUTPUT_SECTION_CODE
= -1U,
3787 // An Output_section_data for an SHF_MERGE section with
3788 // SHF_STRINGS not set.
3789 MERGE_DATA_SECTION_CODE
= -2U,
3790 // An Output_section_data for an SHF_MERGE section with
3792 MERGE_STRING_SECTION_CODE
= -3U,
3793 // An Output_section_data for a relaxed input section.
3794 RELAXED_INPUT_SECTION_CODE
= -4U
3797 // For an ordinary input section, this is the section index in the
3798 // input file. For an Output_section_data, this is
3799 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3800 // MERGE_STRING_SECTION_CODE.
3801 unsigned int shndx_
;
3802 // The required alignment, stored as a power of 2.
3803 unsigned int p2align_
;
3806 // For an ordinary input section, the section size.
3808 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3809 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3815 // For an ordinary input section, the object which holds the
3818 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3819 // MERGE_STRING_SECTION_CODE, the data.
3820 Output_section_data
* posd
;
3821 Output_merge_base
* pomb
;
3822 // For RELAXED_INPUT_SECTION_CODE, the data.
3823 Output_relaxed_input_section
* poris
;
3825 // The line number of the pattern it matches in the --section-ordering-file
3826 // file. It is 0 if does not match any pattern.
3827 unsigned int section_order_index_
;
3830 // Store the list of input sections for this Output_section into the
3831 // list passed in. This removes the input sections, leaving only
3832 // any Output_section_data elements. This returns the size of those
3833 // Output_section_data elements. ADDRESS is the address of this
3834 // output section. FILL is the fill value to use, in case there are
3835 // any spaces between the remaining Output_section_data elements.
3837 get_input_sections(uint64_t address
, const std::string
& fill
,
3838 std::list
<Input_section
>*);
3840 // Add a script input section. A script input section can either be
3841 // a plain input section or a sub-class of Output_section_data.
3843 add_script_input_section(const Input_section
& input_section
);
3845 // Set the current size of the output section.
3847 set_current_data_size(off_t size
)
3848 { this->set_current_data_size_for_child(size
); }
3850 // End of linker script support.
3852 // Save states before doing section layout.
3853 // This is used for relaxation.
3857 // Restore states prior to section layout.
3865 // Convert existing input sections to relaxed input sections.
3867 convert_input_sections_to_relaxed_sections(
3868 const std::vector
<Output_relaxed_input_section
*>& sections
);
3870 // Find a relaxed input section to an input section in OBJECT
3871 // with index SHNDX. Return NULL if none is found.
3872 const Output_relaxed_input_section
*
3873 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
3875 // Whether section offsets need adjustment due to relaxation.
3877 section_offsets_need_adjustment() const
3878 { return this->section_offsets_need_adjustment_
; }
3880 // Set section_offsets_need_adjustment to be true.
3882 set_section_offsets_need_adjustment()
3883 { this->section_offsets_need_adjustment_
= true; }
3885 // Set section_offsets_need_adjustment to be false.
3887 clear_section_offsets_need_adjustment()
3888 { this->section_offsets_need_adjustment_
= false; }
3890 // Adjust section offsets of input sections in this. This is
3891 // requires if relaxation caused some input sections to change sizes.
3893 adjust_section_offsets();
3895 // Whether this is a NOLOAD section.
3898 { return this->is_noload_
; }
3903 { this->is_noload_
= true; }
3905 // Print merge statistics to stderr.
3907 print_merge_stats();
3909 // Set a fixed layout for the section. Used for incremental update links.
3911 set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
, off_t sh_size
,
3912 uint64_t sh_addralign
);
3914 // Return TRUE if the section has a fixed layout.
3916 has_fixed_layout() const
3917 { return this->has_fixed_layout_
; }
3919 // Set flag to allow patch space for this section. Used for full
3920 // incremental links.
3922 set_is_patch_space_allowed()
3923 { this->is_patch_space_allowed_
= true; }
3925 // Set a fill method to use for free space left in the output section
3926 // during incremental links.
3928 set_free_space_fill(Output_fill
* free_space_fill
)
3930 this->free_space_fill_
= free_space_fill
;
3931 this->free_list_
.set_min_hole_size(free_space_fill
->minimum_hole_size());
3934 // Reserve space within the fixed layout for the section. Used for
3935 // incremental update links.
3937 reserve(uint64_t sh_offset
, uint64_t sh_size
);
3939 // Allocate space from the free list for the section. Used for
3940 // incremental update links.
3942 allocate(off_t len
, uint64_t addralign
);
3944 typedef std::vector
<Input_section
> Input_section_list
;
3946 // Allow access to the input sections.
3947 const Input_section_list
&
3948 input_sections() const
3949 { return this->input_sections_
; }
3953 { return this->input_sections_
; }
3956 // Return the output section--i.e., the object itself.
3961 const Output_section
*
3962 do_output_section() const
3965 // Return the section index in the output file.
3967 do_out_shndx() const
3969 gold_assert(this->out_shndx_
!= -1U);
3970 return this->out_shndx_
;
3973 // Set the output section index.
3975 do_set_out_shndx(unsigned int shndx
)
3977 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
3978 this->out_shndx_
= shndx
;
3981 // Update the data size of the Output_section. For a typical
3982 // Output_section, there is nothing to do, but if there are any
3983 // Output_section_data objects we need to do a trial layout
3988 // Set the final data size of the Output_section. For a typical
3989 // Output_section, there is nothing to do, but if there are any
3990 // Output_section_data objects we need to set their final addresses
3993 set_final_data_size();
3995 // Reset the address and file offset.
3997 do_reset_address_and_file_offset();
3999 // Return true if address and file offset already have reset values. In
4000 // other words, calling reset_address_and_file_offset will not change them.
4002 do_address_and_file_offset_have_reset_values() const;
4004 // Write the data to the file. For a typical Output_section, this
4005 // does nothing: the data is written out by calling Object::Relocate
4006 // on each input object. But if there are any Output_section_data
4007 // objects we do need to write them out here.
4009 do_write(Output_file
*);
4011 // Return the address alignment--function required by parent class.
4013 do_addralign() const
4014 { return this->addralign_
; }
4016 // Return whether there is a load address.
4018 do_has_load_address() const
4019 { return this->has_load_address_
; }
4021 // Return the load address.
4023 do_load_address() const
4025 gold_assert(this->has_load_address_
);
4026 return this->load_address_
;
4029 // Return whether this is an Output_section.
4031 do_is_section() const
4034 // Return whether this is a section of the specified type.
4036 do_is_section_type(elfcpp::Elf_Word type
) const
4037 { return this->type_
== type
; }
4039 // Return whether the specified section flag is set.
4041 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
4042 { return (this->flags_
& flag
) != 0; }
4044 // Set the TLS offset. Called only for SHT_TLS sections.
4046 do_set_tls_offset(uint64_t tls_base
);
4048 // Return the TLS offset, relative to the base of the TLS segment.
4049 // Valid only for SHT_TLS sections.
4051 do_tls_offset() const
4052 { return this->tls_offset_
; }
4054 // This may be implemented by a child class.
4056 do_finalize_name(Layout
*)
4059 // Print to the map file.
4061 do_print_to_mapfile(Mapfile
*) const;
4063 // Record that this section requires postprocessing after all
4064 // relocations have been applied. This is called by a child class.
4066 set_requires_postprocessing()
4068 this->requires_postprocessing_
= true;
4069 this->after_input_sections_
= true;
4072 // Write all the data of an Output_section into the postprocessing
4075 write_to_postprocessing_buffer();
4077 // Whether this always keeps an input section list
4079 always_keeps_input_sections() const
4080 { return this->always_keeps_input_sections_
; }
4082 // Always keep an input section list.
4084 set_always_keeps_input_sections()
4086 gold_assert(this->current_data_size_for_child() == 0);
4087 this->always_keeps_input_sections_
= true;
4091 // We only save enough information to undo the effects of section layout.
4092 class Checkpoint_output_section
4095 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
4096 const Input_section_list
& input_sections
,
4097 off_t first_input_offset
,
4098 bool attached_input_sections_are_sorted
)
4099 : addralign_(addralign
), flags_(flags
),
4100 input_sections_(input_sections
),
4101 input_sections_size_(input_sections_
.size()),
4102 input_sections_copy_(), first_input_offset_(first_input_offset
),
4103 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
4107 ~Checkpoint_output_section()
4110 // Return the address alignment.
4113 { return this->addralign_
; }
4116 set_addralign(uint64_t val
)
4117 { this->addralign_
= val
; }
4119 // Return the section flags.
4122 { return this->flags_
; }
4124 // Return a reference to the input section list copy.
4127 { return &this->input_sections_copy_
; }
4129 // Return the size of input_sections at the time when checkpoint is
4132 input_sections_size() const
4133 { return this->input_sections_size_
; }
4135 // Whether input sections are copied.
4137 input_sections_saved() const
4138 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
4141 first_input_offset() const
4142 { return this->first_input_offset_
; }
4145 attached_input_sections_are_sorted() const
4146 { return this->attached_input_sections_are_sorted_
; }
4148 // Save input sections.
4150 save_input_sections()
4152 this->input_sections_copy_
.reserve(this->input_sections_size_
);
4153 this->input_sections_copy_
.clear();
4154 Input_section_list::const_iterator p
= this->input_sections_
.begin();
4155 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
4156 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
4157 this->input_sections_copy_
.push_back(*p
);
4161 // The section alignment.
4162 uint64_t addralign_
;
4163 // The section flags.
4164 elfcpp::Elf_Xword flags_
;
4165 // Reference to the input sections to be checkpointed.
4166 const Input_section_list
& input_sections_
;
4167 // Size of the checkpointed portion of input_sections_;
4168 size_t input_sections_size_
;
4169 // Copy of input sections.
4170 Input_section_list input_sections_copy_
;
4171 // The offset of the first entry in input_sections_.
4172 off_t first_input_offset_
;
4173 // True if the input sections attached to this output section have
4174 // already been sorted.
4175 bool attached_input_sections_are_sorted_
;
4178 // This class is used to sort the input sections.
4179 class Input_section_sort_entry
;
4181 // This is the sort comparison function for ctors and dtors.
4182 struct Input_section_sort_compare
4185 operator()(const Input_section_sort_entry
&,
4186 const Input_section_sort_entry
&) const;
4189 // This is the sort comparison function for .init_array and .fini_array.
4190 struct Input_section_sort_init_fini_compare
4193 operator()(const Input_section_sort_entry
&,
4194 const Input_section_sort_entry
&) const;
4197 // This is the sort comparison function when a section order is specified
4198 // from an input file.
4199 struct Input_section_sort_section_order_index_compare
4202 operator()(const Input_section_sort_entry
&,
4203 const Input_section_sort_entry
&) const;
4206 // This is the sort comparison function for .text to sort sections with
4207 // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4208 struct Input_section_sort_section_prefix_special_ordering_compare
4211 operator()(const Input_section_sort_entry
&,
4212 const Input_section_sort_entry
&) const;
4215 // This is the sort comparison function for sorting sections by name.
4216 struct Input_section_sort_section_name_compare
4219 operator()(const Input_section_sort_entry
&,
4220 const Input_section_sort_entry
&) const;
4223 // Fill data. This is used to fill in data between input sections.
4224 // It is also used for data statements (BYTE, WORD, etc.) in linker
4225 // scripts. When we have to keep track of the input sections, we
4226 // can use an Output_data_const, but we don't want to have to keep
4227 // track of input sections just to implement fills.
4231 Fill(off_t section_offset
, off_t length
)
4232 : section_offset_(section_offset
),
4233 length_(convert_to_section_size_type(length
))
4236 // Return section offset.
4238 section_offset() const
4239 { return this->section_offset_
; }
4241 // Return fill length.
4244 { return this->length_
; }
4247 // The offset within the output section.
4248 off_t section_offset_
;
4249 // The length of the space to fill.
4250 section_size_type length_
;
4253 typedef std::vector
<Fill
> Fill_list
;
4255 // Map used during relaxation of existing sections. This map
4256 // a section id an input section list index. We assume that
4257 // Input_section_list is a vector.
4258 typedef Unordered_map
<Section_id
, size_t, Section_id_hash
> Relaxation_map
;
4260 // Add a new output section by Input_section.
4262 add_output_section_data(Input_section
*);
4264 // Add an SHF_MERGE input section. Returns true if the section was
4265 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4266 // stores information about the merged input sections.
4268 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
4269 uint64_t entsize
, uint64_t addralign
,
4270 bool keeps_input_sections
);
4272 // Add an output SHF_MERGE section POSD to this output section.
4273 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4274 // ENTSIZE is the entity size. This returns the entry added to
4277 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
4280 // Find the merge section into which an input section with index SHNDX in
4281 // OBJECT has been added. Return NULL if none found.
4282 Output_section_data
*
4283 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
4285 // Build a relaxation map.
4287 build_relaxation_map(
4288 const Input_section_list
& input_sections
,
4290 Relaxation_map
* map
) const;
4292 // Convert input sections in an input section list into relaxed sections.
4294 convert_input_sections_in_list_to_relaxed_sections(
4295 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
4296 const Relaxation_map
& map
,
4297 Input_section_list
* input_sections
);
4299 // Build the lookup maps for merge and relaxed input sections.
4301 build_lookup_maps() const;
4303 // Most of these fields are only valid after layout.
4305 // The name of the section. This will point into a Stringpool.
4307 // The section address is in the parent class.
4308 // The section alignment.
4309 uint64_t addralign_
;
4310 // The section entry size.
4312 // The load address. This is only used when using a linker script
4313 // with a SECTIONS clause. The has_load_address_ field indicates
4314 // whether this field is valid.
4315 uint64_t load_address_
;
4316 // The file offset is in the parent class.
4317 // Set the section link field to the index of this section.
4318 const Output_data
* link_section_
;
4319 // If link_section_ is NULL, this is the link field.
4321 // Set the section info field to the index of this section.
4322 const Output_section
* info_section_
;
4323 // If info_section_ is NULL, set the info field to the symbol table
4324 // index of this symbol.
4325 const Symbol
* info_symndx_
;
4326 // If info_section_ and info_symndx_ are NULL, this is the section
4329 // The section type.
4330 const elfcpp::Elf_Word type_
;
4331 // The section flags.
4332 elfcpp::Elf_Xword flags_
;
4333 // The order of this section in the output segment.
4334 Output_section_order order_
;
4335 // The section index.
4336 unsigned int out_shndx_
;
4337 // If there is a STT_SECTION for this output section in the normal
4338 // symbol table, this is the symbol index. This starts out as zero.
4339 // It is initialized in Layout::finalize() to be the index, or -1U
4340 // if there isn't one.
4341 unsigned int symtab_index_
;
4342 // If there is a STT_SECTION for this output section in the dynamic
4343 // symbol table, this is the symbol index. This starts out as zero.
4344 // It is initialized in Layout::finalize() to be the index, or -1U
4345 // if there isn't one.
4346 unsigned int dynsym_index_
;
4347 // The input sections. This will be empty in cases where we don't
4348 // need to keep track of them.
4349 Input_section_list input_sections_
;
4350 // The offset of the first entry in input_sections_.
4351 off_t first_input_offset_
;
4352 // The fill data. This is separate from input_sections_ because we
4353 // often will need fill sections without needing to keep track of
4356 // If the section requires postprocessing, this buffer holds the
4357 // section contents during relocation.
4358 unsigned char* postprocessing_buffer_
;
4359 // Whether this output section needs a STT_SECTION symbol in the
4360 // normal symbol table. This will be true if there is a relocation
4362 bool needs_symtab_index_
: 1;
4363 // Whether this output section needs a STT_SECTION symbol in the
4364 // dynamic symbol table. This will be true if there is a dynamic
4365 // relocation which needs it.
4366 bool needs_dynsym_index_
: 1;
4367 // Whether the link field of this output section should point to the
4368 // normal symbol table.
4369 bool should_link_to_symtab_
: 1;
4370 // Whether the link field of this output section should point to the
4371 // dynamic symbol table.
4372 bool should_link_to_dynsym_
: 1;
4373 // Whether this section should be written after all the input
4374 // sections are complete.
4375 bool after_input_sections_
: 1;
4376 // Whether this section requires post processing after all
4377 // relocations have been applied.
4378 bool requires_postprocessing_
: 1;
4379 // Whether an input section was mapped to this output section
4380 // because of a SECTIONS clause in a linker script.
4381 bool found_in_sections_clause_
: 1;
4382 // Whether this section has an explicitly specified load address.
4383 bool has_load_address_
: 1;
4384 // True if the info_section_ field means the section index of the
4385 // section, false if it means the symbol index of the corresponding
4387 bool info_uses_section_index_
: 1;
4388 // True if input sections attached to this output section have to be
4389 // sorted according to a specified order.
4390 bool input_section_order_specified_
: 1;
4391 // True if the input sections attached to this output section may
4393 bool may_sort_attached_input_sections_
: 1;
4394 // True if the input sections attached to this output section must
4396 bool must_sort_attached_input_sections_
: 1;
4397 // True if the input sections attached to this output section have
4398 // already been sorted.
4399 bool attached_input_sections_are_sorted_
: 1;
4400 // True if this section holds relro data.
4402 // True if this is a small section.
4403 bool is_small_section_
: 1;
4404 // True if this is a large section.
4405 bool is_large_section_
: 1;
4406 // Whether code-fills are generated at write.
4407 bool generate_code_fills_at_write_
: 1;
4408 // Whether the entry size field should be zero.
4409 bool is_entsize_zero_
: 1;
4410 // Whether section offsets need adjustment due to relaxation.
4411 bool section_offsets_need_adjustment_
: 1;
4412 // Whether this is a NOLOAD section.
4413 bool is_noload_
: 1;
4414 // Whether this always keeps input section.
4415 bool always_keeps_input_sections_
: 1;
4416 // Whether this section has a fixed layout, for incremental update links.
4417 bool has_fixed_layout_
: 1;
4418 // True if we can add patch space to this section.
4419 bool is_patch_space_allowed_
: 1;
4420 // True if this output section goes into a unique segment.
4421 bool is_unique_segment_
: 1;
4422 // For SHT_TLS sections, the offset of this section relative to the base
4423 // of the TLS segment.
4424 uint64_t tls_offset_
;
4425 // Additional segment flags, specified via linker plugin, when mapping some
4426 // input sections to unique segments.
4427 uint64_t extra_segment_flags_
;
4428 // Segment alignment specified via linker plugin, when mapping some
4429 // input sections to unique segments.
4430 uint64_t segment_alignment_
;
4431 // Saved checkpoint.
4432 Checkpoint_output_section
* checkpoint_
;
4433 // Fast lookup maps for merged and relaxed input sections.
4434 Output_section_lookup_maps
* lookup_maps_
;
4435 // List of available regions within the section, for incremental
4437 Free_list free_list_
;
4438 // Method for filling chunks of free space.
4439 Output_fill
* free_space_fill_
;
4440 // Amount added as patch space for incremental linking.
4444 // An output segment. PT_LOAD segments are built from collections of
4445 // output sections. Other segments typically point within PT_LOAD
4446 // segments, and are built directly as needed.
4448 // NOTE: We want to use the copy constructor for this class. During
4449 // relaxation, we may try built the segments multiple times. We do
4450 // that by copying the original segment list before lay-out, doing
4451 // a trial lay-out and roll-back to the saved copied if we need to
4452 // to the lay-out again.
4454 class Output_segment
4457 // Create an output segment, specifying the type and flags.
4458 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
4460 // Return the virtual address.
4463 { return this->vaddr_
; }
4465 // Return the physical address.
4468 { return this->paddr_
; }
4470 // Return the segment type.
4473 { return this->type_
; }
4475 // Return the segment flags.
4478 { return this->flags_
; }
4480 // Return the memory size.
4483 { return this->memsz_
; }
4485 // Return the file size.
4488 { return this->filesz_
; }
4490 // Return the file offset.
4493 { return this->offset_
; }
4495 // Whether this is a segment created to hold large data sections.
4497 is_large_data_segment() const
4498 { return this->is_large_data_segment_
; }
4500 // Record that this is a segment created to hold large data
4503 set_is_large_data_segment()
4504 { this->is_large_data_segment_
= true; }
4507 is_unique_segment() const
4508 { return this->is_unique_segment_
; }
4510 // Mark segment as unique, happens when linker plugins request that
4511 // certain input sections be mapped to unique segments.
4513 set_is_unique_segment()
4514 { this->is_unique_segment_
= true; }
4516 // Return the maximum alignment of the Output_data.
4518 maximum_alignment();
4520 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4521 // the segment flags to use.
4523 add_output_section_to_load(Layout
* layout
, Output_section
* os
,
4524 elfcpp::Elf_Word seg_flags
);
4526 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4527 // is the segment flags to use.
4529 add_output_section_to_nonload(Output_section
* os
,
4530 elfcpp::Elf_Word seg_flags
);
4532 // Remove an Output_section from this segment. It is an error if it
4535 remove_output_section(Output_section
* os
);
4537 // Add an Output_data (which need not be an Output_section) to the
4538 // start of this segment.
4540 add_initial_output_data(Output_data
*);
4542 // Return true if this segment has any sections which hold actual
4543 // data, rather than being a BSS section.
4545 has_any_data_sections() const;
4547 // Whether this segment has a dynamic relocs.
4549 has_dynamic_reloc() const;
4551 // Return the first section.
4553 first_section() const;
4555 // Return the address of the first section.
4557 first_section_load_address() const
4559 const Output_section
* os
= this->first_section();
4560 return os
->has_load_address() ? os
->load_address() : os
->address();
4563 // Return whether the addresses have been set already.
4565 are_addresses_set() const
4566 { return this->are_addresses_set_
; }
4568 // Set the addresses.
4570 set_addresses(uint64_t vaddr
, uint64_t paddr
)
4572 this->vaddr_
= vaddr
;
4573 this->paddr_
= paddr
;
4574 this->are_addresses_set_
= true;
4577 // Update the flags for the flags of an output section added to this
4580 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
4582 // The ELF ABI specifies that a PT_TLS segment should always have
4583 // PF_R as the flags.
4584 if (this->type() != elfcpp::PT_TLS
)
4585 this->flags_
|= flags
;
4588 // Set the segment flags. This is only used if we have a PHDRS
4589 // clause which explicitly specifies the flags.
4591 set_flags(elfcpp::Elf_Word flags
)
4592 { this->flags_
= flags
; }
4594 // Set the address of the segment to ADDR and the offset to *POFF
4595 // and set the addresses and offsets of all contained output
4596 // sections accordingly. Set the section indexes of all contained
4597 // output sections starting with *PSHNDX. If RESET is true, first
4598 // reset the addresses of the contained sections. Return the
4599 // address of the immediately following segment. Update *POFF and
4600 // *PSHNDX. This should only be called for a PT_LOAD segment.
4602 set_section_addresses(const Target
*, Layout
*, bool reset
, uint64_t addr
,
4603 unsigned int* increase_relro
, bool* has_relro
,
4604 off_t
* poff
, unsigned int* pshndx
);
4606 // Set the minimum alignment of this segment. This may be adjusted
4607 // upward based on the section alignments.
4609 set_minimum_p_align(uint64_t align
)
4611 if (align
> this->min_p_align_
)
4612 this->min_p_align_
= align
;
4615 // Set the offset of this segment based on the section. This should
4616 // only be called for a non-PT_LOAD segment.
4618 set_offset(unsigned int increase
);
4620 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4624 // Return the number of output sections.
4626 output_section_count() const;
4628 // Return the section attached to the list segment with the lowest
4629 // load address. This is used when handling a PHDRS clause in a
4632 section_with_lowest_load_address() const;
4634 // Write the segment header into *OPHDR.
4635 template<int size
, bool big_endian
>
4637 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
4639 // Write the section headers of associated sections into V.
4640 template<int size
, bool big_endian
>
4642 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
4643 unsigned int* pshndx
) const;
4645 // Print the output sections in the map file.
4647 print_sections_to_mapfile(Mapfile
*) const;
4650 typedef std::vector
<Output_data
*> Output_data_list
;
4652 // Find the maximum alignment in an Output_data_list.
4654 maximum_alignment_list(const Output_data_list
*);
4656 // Return whether the first data section is a relro section.
4658 is_first_section_relro() const;
4660 // Set the section addresses in an Output_data_list.
4662 set_section_list_addresses(Layout
*, bool reset
, Output_data_list
*,
4663 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
4666 // Return the number of Output_sections in an Output_data_list.
4668 output_section_count_list(const Output_data_list
*) const;
4670 // Return whether an Output_data_list has a dynamic reloc.
4672 has_dynamic_reloc_list(const Output_data_list
*) const;
4674 // Find the section with the lowest load address in an
4675 // Output_data_list.
4677 lowest_load_address_in_list(const Output_data_list
* pdl
,
4678 Output_section
** found
,
4679 uint64_t* found_lma
) const;
4681 // Find the first and last entries by address.
4683 find_first_and_last_list(const Output_data_list
* pdl
,
4684 const Output_data
** pfirst
,
4685 const Output_data
** plast
) const;
4687 // Write the section headers in the list into V.
4688 template<int size
, bool big_endian
>
4690 write_section_headers_list(const Layout
*, const Stringpool
*,
4691 const Output_data_list
*, unsigned char* v
,
4692 unsigned int* pshdx
) const;
4694 // Print a section list to the mapfile.
4696 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
4698 // NOTE: We want to use the copy constructor. Currently, shallow copy
4699 // works for us so we do not need to write our own copy constructor.
4701 // The list of output data attached to this segment.
4702 Output_data_list output_lists_
[ORDER_MAX
];
4703 // The segment virtual address.
4705 // The segment physical address.
4707 // The size of the segment in memory.
4709 // The maximum section alignment. The is_max_align_known_ field
4710 // indicates whether this has been finalized.
4711 uint64_t max_align_
;
4712 // The required minimum value for the p_align field. This is used
4713 // for PT_LOAD segments. Note that this does not mean that
4714 // addresses should be aligned to this value; it means the p_paddr
4715 // and p_vaddr fields must be congruent modulo this value. For
4716 // non-PT_LOAD segments, the dynamic linker works more efficiently
4717 // if the p_align field has the more conventional value, although it
4718 // can align as needed.
4719 uint64_t min_p_align_
;
4720 // The offset of the segment data within the file.
4722 // The size of the segment data in the file.
4724 // The segment type;
4725 elfcpp::Elf_Word type_
;
4726 // The segment flags.
4727 elfcpp::Elf_Word flags_
;
4728 // Whether we have finalized max_align_.
4729 bool is_max_align_known_
: 1;
4730 // Whether vaddr and paddr were set by a linker script.
4731 bool are_addresses_set_
: 1;
4732 // Whether this segment holds large data sections.
4733 bool is_large_data_segment_
: 1;
4734 // Whether this was marked as a unique segment via a linker plugin.
4735 bool is_unique_segment_
: 1;
4738 // This class represents the output file.
4743 Output_file(const char* name
);
4745 // Indicate that this is a temporary file which should not be
4749 { this->is_temporary_
= true; }
4751 // Try to open an existing file. Returns false if the file doesn't
4752 // exist, has a size of 0 or can't be mmaped. This method is
4753 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4754 // that file as the base for incremental linking.
4756 open_base_file(const char* base_name
, bool writable
);
4758 // Open the output file. FILE_SIZE is the final size of the file.
4759 // If the file already exists, it is deleted/truncated. This method
4760 // is thread-unsafe.
4762 open(off_t file_size
);
4764 // Resize the output file. This method is thread-unsafe.
4766 resize(off_t file_size
);
4768 // Close the output file (flushing all buffered data) and make sure
4769 // there are no errors. This method is thread-unsafe.
4773 // Return the size of this file.
4776 { return this->file_size_
; }
4778 // Return the name of this file.
4781 { return this->name_
; }
4783 // We currently always use mmap which makes the view handling quite
4784 // simple. In the future we may support other approaches.
4786 // Write data to the output file.
4788 write(off_t offset
, const void* data
, size_t len
)
4789 { memcpy(this->base_
+ offset
, data
, len
); }
4791 // Get a buffer to use to write to the file, given the offset into
4792 // the file and the size.
4794 get_output_view(off_t start
, size_t size
)
4796 gold_assert(start
>= 0
4797 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
4798 return this->base_
+ start
;
4801 // VIEW must have been returned by get_output_view. Write the
4802 // buffer to the file, passing in the offset and the size.
4804 write_output_view(off_t
, size_t, unsigned char*)
4807 // Get a read/write buffer. This is used when we want to write part
4808 // of the file, read it in, and write it again.
4810 get_input_output_view(off_t start
, size_t size
)
4811 { return this->get_output_view(start
, size
); }
4813 // Write a read/write buffer back to the file.
4815 write_input_output_view(off_t
, size_t, unsigned char*)
4818 // Get a read buffer. This is used when we just want to read part
4819 // of the file back it in.
4820 const unsigned char*
4821 get_input_view(off_t start
, size_t size
)
4822 { return this->get_output_view(start
, size
); }
4824 // Release a read bfufer.
4826 free_input_view(off_t
, size_t, const unsigned char*)
4830 // Map the file into memory or, if that fails, allocate anonymous
4835 // Allocate anonymous memory for the file.
4839 // Map the file into memory.
4841 map_no_anonymous(bool);
4843 // Unmap the file from memory (and flush to disk buffers).
4853 // Base of file mapped into memory.
4854 unsigned char* base_
;
4855 // True iff base_ points to a memory buffer rather than an output file.
4856 bool map_is_anonymous_
;
4857 // True if base_ was allocated using new rather than mmap.
4858 bool map_is_allocated_
;
4859 // True if this is a temporary file which should not be output.
4863 } // End namespace gold.
4865 #endif // !defined(GOLD_OUTPUT_H)