1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "reloc-types.h"
37 class General_options
;
41 class Output_merge_base
;
43 class Relocatable_relocs
;
45 template<int size
, bool big_endian
>
47 template<int size
, bool big_endian
>
50 // This class specifies an input section. It is used as a key type
53 class Input_section_specifier
56 Input_section_specifier(const Relobj
* relobj
, unsigned int shndx
)
57 : relobj_(relobj
), shndx_(shndx
)
60 // Return Relobj of this.
63 { return this->relobj_
; }
65 // Return section index of this.
68 { return this->shndx_
; }
70 // Whether this equals to another specifier ISS.
72 eq(const Input_section_specifier
& iss
) const
73 { return this->relobj_
== iss
.relobj_
&& this->shndx_
== iss
.shndx_
; }
75 // Compute a hash value of this.
79 return (gold::string_hash
<char>(this->relobj_
->name().c_str())
83 // Functors for containers.
87 operator()(const Input_section_specifier
& iss1
,
88 const Input_section_specifier
& iss2
) const
89 { return iss1
.eq(iss2
); }
95 operator()(const Input_section_specifier
& iss
) const
96 { return iss
.hash_value(); }
101 const Relobj
* relobj_
;
106 // An abtract class for data which has to go into the output file.
111 explicit Output_data()
112 : address_(0), data_size_(0), offset_(-1),
113 is_address_valid_(false), is_data_size_valid_(false),
114 is_offset_valid_(false), is_data_size_fixed_(false),
115 dynamic_reloc_count_(0)
121 // Return the address. For allocated sections, this is only valid
122 // after Layout::finalize is finished.
126 gold_assert(this->is_address_valid_
);
127 return this->address_
;
130 // Return the size of the data. For allocated sections, this must
131 // be valid after Layout::finalize calls set_address, but need not
132 // be valid before then.
136 gold_assert(this->is_data_size_valid_
);
137 return this->data_size_
;
140 // Return true if data size is fixed.
142 is_data_size_fixed() const
143 { return this->is_data_size_fixed_
; }
145 // Return the file offset. This is only valid after
146 // Layout::finalize is finished. For some non-allocated sections,
147 // it may not be valid until near the end of the link.
151 gold_assert(this->is_offset_valid_
);
152 return this->offset_
;
155 // Reset the address and file offset. This essentially disables the
156 // sanity testing about duplicate and unknown settings.
158 reset_address_and_file_offset()
160 this->is_address_valid_
= false;
161 this->is_offset_valid_
= false;
162 if (!this->is_data_size_fixed_
)
163 this->is_data_size_valid_
= false;
164 this->do_reset_address_and_file_offset();
167 // Return true if address and file offset already have reset values. In
168 // other words, calling reset_address_and_file_offset will not change them.
170 address_and_file_offset_have_reset_values() const
171 { return this->do_address_and_file_offset_have_reset_values(); }
173 // Return the required alignment.
176 { return this->do_addralign(); }
178 // Return whether this has a load address.
180 has_load_address() const
181 { return this->do_has_load_address(); }
183 // Return the load address.
186 { return this->do_load_address(); }
188 // Return whether this is an Output_section.
191 { return this->do_is_section(); }
193 // Return whether this is an Output_section of the specified type.
195 is_section_type(elfcpp::Elf_Word stt
) const
196 { return this->do_is_section_type(stt
); }
198 // Return whether this is an Output_section with the specified flag
201 is_section_flag_set(elfcpp::Elf_Xword shf
) const
202 { return this->do_is_section_flag_set(shf
); }
204 // Return the output section that this goes in, if there is one.
207 { return this->do_output_section(); }
209 const Output_section
*
210 output_section() const
211 { return this->do_output_section(); }
213 // Return the output section index, if there is an output section.
216 { return this->do_out_shndx(); }
218 // Set the output section index, if this is an output section.
220 set_out_shndx(unsigned int shndx
)
221 { this->do_set_out_shndx(shndx
); }
223 // Set the address and file offset of this data, and finalize the
224 // size of the data. This is called during Layout::finalize for
225 // allocated sections.
227 set_address_and_file_offset(uint64_t addr
, off_t off
)
229 this->set_address(addr
);
230 this->set_file_offset(off
);
231 this->finalize_data_size();
236 set_address(uint64_t addr
)
238 gold_assert(!this->is_address_valid_
);
239 this->address_
= addr
;
240 this->is_address_valid_
= true;
243 // Set the file offset.
245 set_file_offset(off_t off
)
247 gold_assert(!this->is_offset_valid_
);
249 this->is_offset_valid_
= true;
252 // Finalize the data size.
256 if (!this->is_data_size_valid_
)
258 // Tell the child class to set the data size.
259 this->set_final_data_size();
260 gold_assert(this->is_data_size_valid_
);
264 // Set the TLS offset. Called only for SHT_TLS sections.
266 set_tls_offset(uint64_t tls_base
)
267 { this->do_set_tls_offset(tls_base
); }
269 // Return the TLS offset, relative to the base of the TLS segment.
270 // Valid only for SHT_TLS sections.
273 { return this->do_tls_offset(); }
275 // Write the data to the output file. This is called after
276 // Layout::finalize is complete.
278 write(Output_file
* file
)
279 { this->do_write(file
); }
281 // This is called by Layout::finalize to note that the sizes of
282 // allocated sections must now be fixed.
285 { Output_data::allocated_sizes_are_fixed
= true; }
287 // Used to check that layout has been done.
290 { return Output_data::allocated_sizes_are_fixed
; }
292 // Count the number of dynamic relocations applied to this section.
295 { ++this->dynamic_reloc_count_
; }
297 // Return the number of dynamic relocations applied to this section.
299 dynamic_reloc_count() const
300 { return this->dynamic_reloc_count_
; }
302 // Whether the address is valid.
304 is_address_valid() const
305 { return this->is_address_valid_
; }
307 // Whether the file offset is valid.
309 is_offset_valid() const
310 { return this->is_offset_valid_
; }
312 // Whether the data size is valid.
314 is_data_size_valid() const
315 { return this->is_data_size_valid_
; }
317 // Print information to the map file.
319 print_to_mapfile(Mapfile
* mapfile
) const
320 { return this->do_print_to_mapfile(mapfile
); }
323 // Functions that child classes may or in some cases must implement.
325 // Write the data to the output file.
327 do_write(Output_file
*) = 0;
329 // Return the required alignment.
331 do_addralign() const = 0;
333 // Return whether this has a load address.
335 do_has_load_address() const
338 // Return the load address.
340 do_load_address() const
341 { gold_unreachable(); }
343 // Return whether this is an Output_section.
345 do_is_section() const
348 // Return whether this is an Output_section of the specified type.
349 // This only needs to be implement by Output_section.
351 do_is_section_type(elfcpp::Elf_Word
) const
354 // Return whether this is an Output_section with the specific flag
355 // set. This only needs to be implemented by Output_section.
357 do_is_section_flag_set(elfcpp::Elf_Xword
) const
360 // Return the output section, if there is one.
361 virtual Output_section
*
365 virtual const Output_section
*
366 do_output_section() const
369 // Return the output section index, if there is an output section.
372 { gold_unreachable(); }
374 // Set the output section index, if this is an output section.
376 do_set_out_shndx(unsigned int)
377 { gold_unreachable(); }
379 // This is a hook for derived classes to set the data size. This is
380 // called by finalize_data_size, normally called during
381 // Layout::finalize, when the section address is set.
383 set_final_data_size()
384 { gold_unreachable(); }
386 // A hook for resetting the address and file offset.
388 do_reset_address_and_file_offset()
391 // Return true if address and file offset already have reset values. In
392 // other words, calling reset_address_and_file_offset will not change them.
393 // A child class overriding do_reset_address_and_file_offset may need to
394 // also override this.
396 do_address_and_file_offset_have_reset_values() const
397 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
399 // Set the TLS offset. Called only for SHT_TLS sections.
401 do_set_tls_offset(uint64_t)
402 { gold_unreachable(); }
404 // Return the TLS offset, relative to the base of the TLS segment.
405 // Valid only for SHT_TLS sections.
407 do_tls_offset() const
408 { gold_unreachable(); }
410 // Print to the map file. This only needs to be implemented by
411 // classes which may appear in a PT_LOAD segment.
413 do_print_to_mapfile(Mapfile
*) const
414 { gold_unreachable(); }
416 // Functions that child classes may call.
418 // Reset the address. The Output_section class needs this when an
419 // SHF_ALLOC input section is added to an output section which was
420 // formerly not SHF_ALLOC.
422 mark_address_invalid()
423 { this->is_address_valid_
= false; }
425 // Set the size of the data.
427 set_data_size(off_t data_size
)
429 gold_assert(!this->is_data_size_valid_
430 && !this->is_data_size_fixed_
);
431 this->data_size_
= data_size
;
432 this->is_data_size_valid_
= true;
435 // Fix the data size. Once it is fixed, it cannot be changed
436 // and the data size remains always valid.
440 gold_assert(this->is_data_size_valid_
);
441 this->is_data_size_fixed_
= true;
444 // Get the current data size--this is for the convenience of
445 // sections which build up their size over time.
447 current_data_size_for_child() const
448 { return this->data_size_
; }
450 // Set the current data size--this is for the convenience of
451 // sections which build up their size over time.
453 set_current_data_size_for_child(off_t data_size
)
455 gold_assert(!this->is_data_size_valid_
);
456 this->data_size_
= data_size
;
459 // Return default alignment for the target size.
463 // Return default alignment for a specified size--32 or 64.
465 default_alignment_for_size(int size
);
468 Output_data(const Output_data
&);
469 Output_data
& operator=(const Output_data
&);
471 // This is used for verification, to make sure that we don't try to
472 // change any sizes of allocated sections after we set the section
474 static bool allocated_sizes_are_fixed
;
476 // Memory address in output file.
478 // Size of data in output file.
480 // File offset of contents in output file.
482 // Whether address_ is valid.
483 bool is_address_valid_
;
484 // Whether data_size_ is valid.
485 bool is_data_size_valid_
;
486 // Whether offset_ is valid.
487 bool is_offset_valid_
;
488 // Whether data size is fixed.
489 bool is_data_size_fixed_
;
490 // Count of dynamic relocations applied to this section.
491 unsigned int dynamic_reloc_count_
;
494 // Output the section headers.
496 class Output_section_headers
: public Output_data
499 Output_section_headers(const Layout
*,
500 const Layout::Segment_list
*,
501 const Layout::Section_list
*,
502 const Layout::Section_list
*,
504 const Output_section
*);
507 // Write the data to the file.
509 do_write(Output_file
*);
511 // Return the required alignment.
514 { return Output_data::default_alignment(); }
516 // Write to a map file.
518 do_print_to_mapfile(Mapfile
* mapfile
) const
519 { mapfile
->print_output_data(this, _("** section headers")); }
521 // Set final data size.
523 set_final_data_size()
524 { this->set_data_size(this->do_size()); }
527 // Write the data to the file with the right size and endianness.
528 template<int size
, bool big_endian
>
530 do_sized_write(Output_file
*);
532 // Compute data size.
536 const Layout
* layout_
;
537 const Layout::Segment_list
* segment_list_
;
538 const Layout::Section_list
* section_list_
;
539 const Layout::Section_list
* unattached_section_list_
;
540 const Stringpool
* secnamepool_
;
541 const Output_section
* shstrtab_section_
;
544 // Output the segment headers.
546 class Output_segment_headers
: public Output_data
549 Output_segment_headers(const Layout::Segment_list
& segment_list
);
552 // Write the data to the file.
554 do_write(Output_file
*);
556 // Return the required alignment.
559 { return Output_data::default_alignment(); }
561 // Write to a map file.
563 do_print_to_mapfile(Mapfile
* mapfile
) const
564 { mapfile
->print_output_data(this, _("** segment headers")); }
566 // Set final data size.
568 set_final_data_size()
569 { this->set_data_size(this->do_size()); }
572 // Write the data to the file with the right size and endianness.
573 template<int size
, bool big_endian
>
575 do_sized_write(Output_file
*);
577 // Compute the current size.
581 const Layout::Segment_list
& segment_list_
;
584 // Output the ELF file header.
586 class Output_file_header
: public Output_data
589 Output_file_header(const Target
*,
591 const Output_segment_headers
*,
594 // Add information about the section headers. We lay out the ELF
595 // file header before we create the section headers.
596 void set_section_info(const Output_section_headers
*,
597 const Output_section
* shstrtab
);
600 // Write the data to the file.
602 do_write(Output_file
*);
604 // Return the required alignment.
607 { return Output_data::default_alignment(); }
609 // Write to a map file.
611 do_print_to_mapfile(Mapfile
* mapfile
) const
612 { mapfile
->print_output_data(this, _("** file header")); }
614 // Set final data size.
616 set_final_data_size(void)
617 { this->set_data_size(this->do_size()); }
620 // Write the data to the file with the right size and endianness.
621 template<int size
, bool big_endian
>
623 do_sized_write(Output_file
*);
625 // Return the value to use for the entry address.
627 typename
elfcpp::Elf_types
<size
>::Elf_Addr
630 // Compute the current data size.
634 const Target
* target_
;
635 const Symbol_table
* symtab_
;
636 const Output_segment_headers
* segment_header_
;
637 const Output_section_headers
* section_header_
;
638 const Output_section
* shstrtab_
;
642 // Output sections are mainly comprised of input sections. However,
643 // there are cases where we have data to write out which is not in an
644 // input section. Output_section_data is used in such cases. This is
645 // an abstract base class.
647 class Output_section_data
: public Output_data
650 Output_section_data(off_t data_size
, uint64_t addralign
,
651 bool is_data_size_fixed
)
652 : Output_data(), output_section_(NULL
), addralign_(addralign
)
654 this->set_data_size(data_size
);
655 if (is_data_size_fixed
)
656 this->fix_data_size();
659 Output_section_data(uint64_t addralign
)
660 : Output_data(), output_section_(NULL
), addralign_(addralign
)
663 // Return the output section.
664 const Output_section
*
665 output_section() const
666 { return this->output_section_
; }
668 // Record the output section.
670 set_output_section(Output_section
* os
);
672 // Add an input section, for SHF_MERGE sections. This returns true
673 // if the section was handled.
675 add_input_section(Relobj
* object
, unsigned int shndx
)
676 { return this->do_add_input_section(object
, shndx
); }
678 // Given an input OBJECT, an input section index SHNDX within that
679 // object, and an OFFSET relative to the start of that input
680 // section, return whether or not the corresponding offset within
681 // the output section is known. If this function returns true, it
682 // sets *POUTPUT to the output offset. The value -1 indicates that
683 // this input offset is being discarded.
685 output_offset(const Relobj
* object
, unsigned int shndx
,
686 section_offset_type offset
,
687 section_offset_type
*poutput
) const
688 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
690 // Return whether this is the merge section for the input section
691 // SHNDX in OBJECT. This should return true when output_offset
692 // would return true for some values of OFFSET.
694 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
695 { return this->do_is_merge_section_for(object
, shndx
); }
697 // Write the contents to a buffer. This is used for sections which
698 // require postprocessing, such as compression.
700 write_to_buffer(unsigned char* buffer
)
701 { this->do_write_to_buffer(buffer
); }
703 // Print merge stats to stderr. This should only be called for
704 // SHF_MERGE sections.
706 print_merge_stats(const char* section_name
)
707 { this->do_print_merge_stats(section_name
); }
710 // The child class must implement do_write.
712 // The child class may implement specific adjustments to the output
715 do_adjust_output_section(Output_section
*)
718 // May be implemented by child class. Return true if the section
721 do_add_input_section(Relobj
*, unsigned int)
722 { gold_unreachable(); }
724 // The child class may implement output_offset.
726 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
727 section_offset_type
*) const
730 // The child class may implement is_merge_section_for.
732 do_is_merge_section_for(const Relobj
*, unsigned int) const
735 // The child class may implement write_to_buffer. Most child
736 // classes can not appear in a compressed section, and they do not
739 do_write_to_buffer(unsigned char*)
740 { gold_unreachable(); }
742 // Print merge statistics.
744 do_print_merge_stats(const char*)
745 { gold_unreachable(); }
747 // Return the required alignment.
750 { return this->addralign_
; }
752 // Return the output section.
755 { return this->output_section_
; }
757 const Output_section
*
758 do_output_section() const
759 { return this->output_section_
; }
761 // Return the section index of the output section.
763 do_out_shndx() const;
765 // Set the alignment.
767 set_addralign(uint64_t addralign
);
770 // The output section for this section.
771 Output_section
* output_section_
;
772 // The required alignment.
776 // Some Output_section_data classes build up their data step by step,
777 // rather than all at once. This class provides an interface for
780 class Output_section_data_build
: public Output_section_data
783 Output_section_data_build(uint64_t addralign
)
784 : Output_section_data(addralign
)
787 // Get the current data size.
789 current_data_size() const
790 { return this->current_data_size_for_child(); }
792 // Set the current data size.
794 set_current_data_size(off_t data_size
)
795 { this->set_current_data_size_for_child(data_size
); }
798 // Set the final data size.
800 set_final_data_size()
801 { this->set_data_size(this->current_data_size_for_child()); }
804 // A simple case of Output_data in which we have constant data to
807 class Output_data_const
: public Output_section_data
810 Output_data_const(const std::string
& data
, uint64_t addralign
)
811 : Output_section_data(data
.size(), addralign
, true), data_(data
)
814 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
815 : Output_section_data(len
, addralign
, true), data_(p
, len
)
818 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
819 : Output_section_data(len
, addralign
, true),
820 data_(reinterpret_cast<const char*>(p
), len
)
824 // Write the data to the output file.
826 do_write(Output_file
*);
828 // Write the data to a buffer.
830 do_write_to_buffer(unsigned char* buffer
)
831 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
833 // Write to a map file.
835 do_print_to_mapfile(Mapfile
* mapfile
) const
836 { mapfile
->print_output_data(this, _("** fill")); }
842 // Another version of Output_data with constant data, in which the
843 // buffer is allocated by the caller.
845 class Output_data_const_buffer
: public Output_section_data
848 Output_data_const_buffer(const unsigned char* p
, off_t len
,
849 uint64_t addralign
, const char* map_name
)
850 : Output_section_data(len
, addralign
, true),
851 p_(p
), map_name_(map_name
)
855 // Write the data the output file.
857 do_write(Output_file
*);
859 // Write the data to a buffer.
861 do_write_to_buffer(unsigned char* buffer
)
862 { memcpy(buffer
, this->p_
, this->data_size()); }
864 // Write to a map file.
866 do_print_to_mapfile(Mapfile
* mapfile
) const
867 { mapfile
->print_output_data(this, _(this->map_name_
)); }
870 // The data to output.
871 const unsigned char* p_
;
872 // Name to use in a map file. Maps are a rarely used feature, but
873 // the space usage is minor as aren't very many of these objects.
874 const char* map_name_
;
877 // A place holder for a fixed amount of data written out via some
880 class Output_data_fixed_space
: public Output_section_data
883 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
884 const char* map_name
)
885 : Output_section_data(data_size
, addralign
, true),
890 // Write out the data--the actual data must be written out
893 do_write(Output_file
*)
896 // Write to a map file.
898 do_print_to_mapfile(Mapfile
* mapfile
) const
899 { mapfile
->print_output_data(this, _(this->map_name_
)); }
902 // Name to use in a map file. Maps are a rarely used feature, but
903 // the space usage is minor as aren't very many of these objects.
904 const char* map_name_
;
907 // A place holder for variable sized data written out via some other
910 class Output_data_space
: public Output_section_data_build
913 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
914 : Output_section_data_build(addralign
),
918 // Set the alignment.
920 set_space_alignment(uint64_t align
)
921 { this->set_addralign(align
); }
924 // Write out the data--the actual data must be written out
927 do_write(Output_file
*)
930 // Write to a map file.
932 do_print_to_mapfile(Mapfile
* mapfile
) const
933 { mapfile
->print_output_data(this, _(this->map_name_
)); }
936 // Name to use in a map file. Maps are a rarely used feature, but
937 // the space usage is minor as aren't very many of these objects.
938 const char* map_name_
;
941 // Fill fixed space with zeroes. This is just like
942 // Output_data_fixed_space, except that the map name is known.
944 class Output_data_zero_fill
: public Output_section_data
947 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
948 : Output_section_data(data_size
, addralign
, true)
952 // There is no data to write out.
954 do_write(Output_file
*)
957 // Write to a map file.
959 do_print_to_mapfile(Mapfile
* mapfile
) const
960 { mapfile
->print_output_data(this, "** zero fill"); }
963 // A string table which goes into an output section.
965 class Output_data_strtab
: public Output_section_data
968 Output_data_strtab(Stringpool
* strtab
)
969 : Output_section_data(1), strtab_(strtab
)
973 // This is called to set the address and file offset. Here we make
974 // sure that the Stringpool is finalized.
976 set_final_data_size();
978 // Write out the data.
980 do_write(Output_file
*);
982 // Write the data to a buffer.
984 do_write_to_buffer(unsigned char* buffer
)
985 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
987 // Write to a map file.
989 do_print_to_mapfile(Mapfile
* mapfile
) const
990 { mapfile
->print_output_data(this, _("** string table")); }
996 // This POD class is used to represent a single reloc in the output
997 // file. This could be a private class within Output_data_reloc, but
998 // the templatization is complex enough that I broke it out into a
999 // separate class. The class is templatized on either elfcpp::SHT_REL
1000 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1001 // relocation or an ordinary relocation.
1003 // A relocation can be against a global symbol, a local symbol, a
1004 // local section symbol, an output section, or the undefined symbol at
1005 // index 0. We represent the latter by using a NULL global symbol.
1007 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1010 template<bool dynamic
, int size
, bool big_endian
>
1011 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1014 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1015 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1017 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1019 // An uninitialized entry. We need this because we want to put
1020 // instances of this class into an STL container.
1022 : local_sym_index_(INVALID_CODE
)
1025 // We have a bunch of different constructors. They come in pairs
1026 // depending on how the address of the relocation is specified. It
1027 // can either be an offset in an Output_data or an offset in an
1030 // A reloc against a global symbol.
1032 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1033 Address address
, bool is_relative
);
1035 Output_reloc(Symbol
* gsym
, unsigned int type
,
1036 Sized_relobj
<size
, big_endian
>* relobj
,
1037 unsigned int shndx
, Address address
, bool is_relative
);
1039 // A reloc against a local symbol or local section symbol.
1041 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1042 unsigned int local_sym_index
, unsigned int type
,
1043 Output_data
* od
, Address address
, bool is_relative
,
1044 bool is_section_symbol
);
1046 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1047 unsigned int local_sym_index
, unsigned int type
,
1048 unsigned int shndx
, Address address
, bool is_relative
,
1049 bool is_section_symbol
);
1051 // A reloc against the STT_SECTION symbol of an output section.
1053 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1056 Output_reloc(Output_section
* os
, unsigned int type
,
1057 Sized_relobj
<size
, big_endian
>* relobj
,
1058 unsigned int shndx
, Address address
);
1060 // Return TRUE if this is a RELATIVE relocation.
1063 { return this->is_relative_
; }
1065 // Return whether this is against a local section symbol.
1067 is_local_section_symbol() const
1069 return (this->local_sym_index_
!= GSYM_CODE
1070 && this->local_sym_index_
!= SECTION_CODE
1071 && this->local_sym_index_
!= INVALID_CODE
1072 && this->is_section_symbol_
);
1075 // For a local section symbol, return the offset of the input
1076 // section within the output section. ADDEND is the addend being
1077 // applied to the input section.
1079 local_section_offset(Addend addend
) const;
1081 // Get the value of the symbol referred to by a Rel relocation when
1082 // we are adding the given ADDEND.
1084 symbol_value(Addend addend
) const;
1086 // Write the reloc entry to an output view.
1088 write(unsigned char* pov
) const;
1090 // Write the offset and info fields to Write_rel.
1091 template<typename Write_rel
>
1092 void write_rel(Write_rel
*) const;
1094 // This is used when sorting dynamic relocs. Return -1 to sort this
1095 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1097 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1100 // Return whether this reloc should be sorted before the argument
1101 // when sorting dynamic relocs.
1103 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1105 { return this->compare(r2
) < 0; }
1108 // Record that we need a dynamic symbol index.
1110 set_needs_dynsym_index();
1112 // Return the symbol index.
1114 get_symbol_index() const;
1116 // Return the output address.
1118 get_address() const;
1120 // Codes for local_sym_index_.
1127 // Invalid uninitialized entry.
1133 // For a local symbol or local section symbol
1134 // (this->local_sym_index_ >= 0), the object. We will never
1135 // generate a relocation against a local symbol in a dynamic
1136 // object; that doesn't make sense. And our callers will always
1137 // be templatized, so we use Sized_relobj here.
1138 Sized_relobj
<size
, big_endian
>* relobj
;
1139 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1140 // symbol. If this is NULL, it indicates a relocation against the
1141 // undefined 0 symbol.
1143 // For a relocation against an output section
1144 // (this->local_sym_index_ == SECTION_CODE), the output section.
1149 // If this->shndx_ is not INVALID CODE, the object which holds the
1150 // input section being used to specify the reloc address.
1151 Sized_relobj
<size
, big_endian
>* relobj
;
1152 // If this->shndx_ is INVALID_CODE, the output data being used to
1153 // specify the reloc address. This may be NULL if the reloc
1154 // address is absolute.
1157 // The address offset within the input section or the Output_data.
1159 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1160 // relocation against an output section, or INVALID_CODE for an
1161 // uninitialized value. Otherwise, for a local symbol
1162 // (this->is_section_symbol_ is false), the local symbol index. For
1163 // a local section symbol (this->is_section_symbol_ is true), the
1164 // section index in the input file.
1165 unsigned int local_sym_index_
;
1166 // The reloc type--a processor specific code.
1167 unsigned int type_
: 30;
1168 // True if the relocation is a RELATIVE relocation.
1169 bool is_relative_
: 1;
1170 // True if the relocation is against a section symbol.
1171 bool is_section_symbol_
: 1;
1172 // If the reloc address is an input section in an object, the
1173 // section index. This is INVALID_CODE if the reloc address is
1174 // specified in some other way.
1175 unsigned int shndx_
;
1178 // The SHT_RELA version of Output_reloc<>. This is just derived from
1179 // the SHT_REL version of Output_reloc, but it adds an addend.
1181 template<bool dynamic
, int size
, bool big_endian
>
1182 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1185 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1186 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1188 // An uninitialized entry.
1193 // A reloc against a global symbol.
1195 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1196 Address address
, Addend addend
, bool is_relative
)
1197 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
1200 Output_reloc(Symbol
* gsym
, unsigned int type
,
1201 Sized_relobj
<size
, big_endian
>* relobj
,
1202 unsigned int shndx
, Address address
, Addend addend
,
1204 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1207 // A reloc against a local symbol.
1209 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1210 unsigned int local_sym_index
, unsigned int type
,
1211 Output_data
* od
, Address address
,
1212 Addend addend
, bool is_relative
, bool is_section_symbol
)
1213 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1218 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1219 unsigned int local_sym_index
, unsigned int type
,
1220 unsigned int shndx
, Address address
,
1221 Addend addend
, bool is_relative
, bool is_section_symbol
)
1222 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1227 // A reloc against the STT_SECTION symbol of an output section.
1229 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1230 Address address
, Addend addend
)
1231 : rel_(os
, type
, od
, address
), addend_(addend
)
1234 Output_reloc(Output_section
* os
, unsigned int type
,
1235 Sized_relobj
<size
, big_endian
>* relobj
,
1236 unsigned int shndx
, Address address
, Addend addend
)
1237 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1240 // Return TRUE if this is a RELATIVE relocation.
1243 { return this->rel_
.is_relative(); }
1245 // Write the reloc entry to an output view.
1247 write(unsigned char* pov
) const;
1249 // Return whether this reloc should be sorted before the argument
1250 // when sorting dynamic relocs.
1252 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1255 int i
= this->rel_
.compare(r2
.rel_
);
1261 return this->addend_
< r2
.addend_
;
1266 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1271 // Output_data_reloc_generic is a non-template base class for
1272 // Output_data_reloc_base. This gives the generic code a way to hold
1273 // a pointer to a reloc section.
1275 class Output_data_reloc_generic
: public Output_section_data_build
1278 Output_data_reloc_generic(int size
, bool sort_relocs
)
1279 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1280 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1283 // Return the number of relative relocs in this section.
1285 relative_reloc_count() const
1286 { return this->relative_reloc_count_
; }
1288 // Whether we should sort the relocs.
1291 { return this->sort_relocs_
; }
1294 // Note that we've added another relative reloc.
1296 bump_relative_reloc_count()
1297 { ++this->relative_reloc_count_
; }
1300 // The number of relative relocs added to this section. This is to
1301 // support DT_RELCOUNT.
1302 size_t relative_reloc_count_
;
1303 // Whether to sort the relocations when writing them out, to make
1304 // the dynamic linker more efficient.
1308 // Output_data_reloc is used to manage a section containing relocs.
1309 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1310 // indicates whether this is a dynamic relocation or a normal
1311 // relocation. Output_data_reloc_base is a base class.
1312 // Output_data_reloc is the real class, which we specialize based on
1315 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1316 class Output_data_reloc_base
: public Output_data_reloc_generic
1319 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1320 typedef typename
Output_reloc_type::Address Address
;
1321 static const int reloc_size
=
1322 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1324 // Construct the section.
1325 Output_data_reloc_base(bool sort_relocs
)
1326 : Output_data_reloc_generic(size
, sort_relocs
)
1330 // Write out the data.
1332 do_write(Output_file
*);
1334 // Set the entry size and the link.
1336 do_adjust_output_section(Output_section
*os
);
1338 // Write to a map file.
1340 do_print_to_mapfile(Mapfile
* mapfile
) const
1342 mapfile
->print_output_data(this,
1344 ? _("** dynamic relocs")
1348 // Add a relocation entry.
1350 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1352 this->relocs_
.push_back(reloc
);
1353 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1354 od
->add_dynamic_reloc();
1355 if (reloc
.is_relative())
1356 this->bump_relative_reloc_count();
1360 typedef std::vector
<Output_reloc_type
> Relocs
;
1362 // The class used to sort the relocations.
1363 struct Sort_relocs_comparison
1366 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1367 { return r1
.sort_before(r2
); }
1370 // The relocations in this section.
1374 // The class which callers actually create.
1376 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1377 class Output_data_reloc
;
1379 // The SHT_REL version of Output_data_reloc.
1381 template<bool dynamic
, int size
, bool big_endian
>
1382 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1383 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1386 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1390 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1391 typedef typename
Output_reloc_type::Address Address
;
1393 Output_data_reloc(bool sr
)
1394 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1397 // Add a reloc against a global symbol.
1400 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1401 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1404 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1405 Sized_relobj
<size
, big_endian
>* relobj
,
1406 unsigned int shndx
, Address address
)
1407 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1410 // These are to simplify the Copy_relocs class.
1413 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1416 gold_assert(addend
== 0);
1417 this->add_global(gsym
, type
, od
, address
);
1421 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1422 Sized_relobj
<size
, big_endian
>* relobj
,
1423 unsigned int shndx
, Address address
, Address addend
)
1425 gold_assert(addend
== 0);
1426 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1429 // Add a RELATIVE reloc against a global symbol. The final relocation
1430 // will not reference the symbol.
1433 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1435 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1438 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1439 Sized_relobj
<size
, big_endian
>* relobj
,
1440 unsigned int shndx
, Address address
)
1442 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1446 // Add a reloc against a local symbol.
1449 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1450 unsigned int local_sym_index
, unsigned int type
,
1451 Output_data
* od
, Address address
)
1453 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1454 address
, false, false));
1458 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1459 unsigned int local_sym_index
, unsigned int type
,
1460 Output_data
* od
, unsigned int shndx
, Address address
)
1462 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1463 address
, false, false));
1466 // Add a RELATIVE reloc against a local symbol.
1469 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1470 unsigned int local_sym_index
, unsigned int type
,
1471 Output_data
* od
, Address address
)
1473 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1474 address
, true, false));
1478 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1479 unsigned int local_sym_index
, unsigned int type
,
1480 Output_data
* od
, unsigned int shndx
, Address address
)
1482 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1483 address
, true, false));
1486 // Add a reloc against a local section symbol. This will be
1487 // converted into a reloc against the STT_SECTION symbol of the
1491 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1492 unsigned int input_shndx
, unsigned int type
,
1493 Output_data
* od
, Address address
)
1495 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1496 address
, false, true));
1500 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1501 unsigned int input_shndx
, unsigned int type
,
1502 Output_data
* od
, unsigned int shndx
, Address address
)
1504 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1505 address
, false, true));
1508 // A reloc against the STT_SECTION symbol of an output section.
1509 // OS is the Output_section that the relocation refers to; OD is
1510 // the Output_data object being relocated.
1513 add_output_section(Output_section
* os
, unsigned int type
,
1514 Output_data
* od
, Address address
)
1515 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1518 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1519 Sized_relobj
<size
, big_endian
>* relobj
,
1520 unsigned int shndx
, Address address
)
1521 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1524 // The SHT_RELA version of Output_data_reloc.
1526 template<bool dynamic
, int size
, bool big_endian
>
1527 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1528 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1531 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1535 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1536 typedef typename
Output_reloc_type::Address Address
;
1537 typedef typename
Output_reloc_type::Addend Addend
;
1539 Output_data_reloc(bool sr
)
1540 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1543 // Add a reloc against a global symbol.
1546 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1547 Address address
, Addend addend
)
1548 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1552 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1553 Sized_relobj
<size
, big_endian
>* relobj
,
1554 unsigned int shndx
, Address address
,
1556 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1559 // Add a RELATIVE reloc against a global symbol. The final output
1560 // relocation will not reference the symbol, but we must keep the symbol
1561 // information long enough to set the addend of the relocation correctly
1562 // when it is written.
1565 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1566 Address address
, Addend addend
)
1567 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1570 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1571 Sized_relobj
<size
, big_endian
>* relobj
,
1572 unsigned int shndx
, Address address
, Addend addend
)
1573 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1576 // Add a reloc against a local symbol.
1579 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1580 unsigned int local_sym_index
, unsigned int type
,
1581 Output_data
* od
, Address address
, Addend addend
)
1583 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1584 addend
, false, false));
1588 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1589 unsigned int local_sym_index
, unsigned int type
,
1590 Output_data
* od
, unsigned int shndx
, Address address
,
1593 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1594 address
, addend
, false, false));
1597 // Add a RELATIVE reloc against a local symbol.
1600 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1601 unsigned int local_sym_index
, unsigned int type
,
1602 Output_data
* od
, Address address
, Addend addend
)
1604 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1605 addend
, true, false));
1609 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1610 unsigned int local_sym_index
, unsigned int type
,
1611 Output_data
* od
, unsigned int shndx
, Address address
,
1614 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1615 address
, addend
, true, false));
1618 // Add a reloc against a local section symbol. This will be
1619 // converted into a reloc against the STT_SECTION symbol of the
1623 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1624 unsigned int input_shndx
, unsigned int type
,
1625 Output_data
* od
, Address address
, Addend addend
)
1627 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1628 addend
, false, true));
1632 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1633 unsigned int input_shndx
, unsigned int type
,
1634 Output_data
* od
, unsigned int shndx
, Address address
,
1637 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1638 address
, addend
, false, true));
1641 // A reloc against the STT_SECTION symbol of an output section.
1644 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1645 Address address
, Addend addend
)
1646 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1649 add_output_section(Output_section
* os
, unsigned int type
,
1650 Sized_relobj
<size
, big_endian
>* relobj
,
1651 unsigned int shndx
, Address address
, Addend addend
)
1652 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1656 // Output_relocatable_relocs represents a relocation section in a
1657 // relocatable link. The actual data is written out in the target
1658 // hook relocate_for_relocatable. This just saves space for it.
1660 template<int sh_type
, int size
, bool big_endian
>
1661 class Output_relocatable_relocs
: public Output_section_data
1664 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1665 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1670 set_final_data_size();
1672 // Write out the data. There is nothing to do here.
1674 do_write(Output_file
*)
1677 // Write to a map file.
1679 do_print_to_mapfile(Mapfile
* mapfile
) const
1680 { mapfile
->print_output_data(this, _("** relocs")); }
1683 // The relocs associated with this input section.
1684 Relocatable_relocs
* rr_
;
1687 // Handle a GROUP section.
1689 template<int size
, bool big_endian
>
1690 class Output_data_group
: public Output_section_data
1693 // The constructor clears *INPUT_SHNDXES.
1694 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1695 section_size_type entry_count
,
1696 elfcpp::Elf_Word flags
,
1697 std::vector
<unsigned int>* input_shndxes
);
1700 do_write(Output_file
*);
1702 // Write to a map file.
1704 do_print_to_mapfile(Mapfile
* mapfile
) const
1705 { mapfile
->print_output_data(this, _("** group")); }
1707 // Set final data size.
1709 set_final_data_size()
1710 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1713 // The input object.
1714 Sized_relobj
<size
, big_endian
>* relobj_
;
1715 // The group flag word.
1716 elfcpp::Elf_Word flags_
;
1717 // The section indexes of the input sections in this group.
1718 std::vector
<unsigned int> input_shndxes_
;
1721 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1722 // for one symbol--either a global symbol or a local symbol in an
1723 // object. The target specific code adds entries to the GOT as
1726 template<int size
, bool big_endian
>
1727 class Output_data_got
: public Output_section_data_build
1730 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1731 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1732 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1735 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1739 // Add an entry for a global symbol to the GOT. Return true if this
1740 // is a new GOT entry, false if the symbol was already in the GOT.
1742 add_global(Symbol
* gsym
, unsigned int got_type
);
1744 // Add an entry for a global symbol to the GOT, and add a dynamic
1745 // relocation of type R_TYPE for the GOT entry.
1747 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1748 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1751 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1752 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1754 // Add a pair of entries for a global symbol to the GOT, and add
1755 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1757 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1758 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1759 unsigned int r_type_2
);
1762 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1763 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1764 unsigned int r_type_2
);
1766 // Add an entry for a local symbol to the GOT. This returns true if
1767 // this is a new GOT entry, false if the symbol already has a GOT
1770 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1771 unsigned int got_type
);
1773 // Add an entry for a local symbol to the GOT, and add a dynamic
1774 // relocation of type R_TYPE for the GOT entry.
1776 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1777 unsigned int sym_index
, unsigned int got_type
,
1778 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1781 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1782 unsigned int sym_index
, unsigned int got_type
,
1783 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1785 // Add a pair of entries for a local symbol to the GOT, and add
1786 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1788 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1789 unsigned int sym_index
, unsigned int shndx
,
1790 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1791 unsigned int r_type_1
, unsigned int r_type_2
);
1794 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1795 unsigned int sym_index
, unsigned int shndx
,
1796 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1797 unsigned int r_type_1
, unsigned int r_type_2
);
1799 // Add a constant to the GOT. This returns the offset of the new
1800 // entry from the start of the GOT.
1802 add_constant(Valtype constant
)
1804 this->entries_
.push_back(Got_entry(constant
));
1805 this->set_got_size();
1806 return this->last_got_offset();
1810 // Write out the GOT table.
1812 do_write(Output_file
*);
1814 // Write to a map file.
1816 do_print_to_mapfile(Mapfile
* mapfile
) const
1817 { mapfile
->print_output_data(this, _("** GOT")); }
1820 // This POD class holds a single GOT entry.
1824 // Create a zero entry.
1826 : local_sym_index_(CONSTANT_CODE
)
1827 { this->u_
.constant
= 0; }
1829 // Create a global symbol entry.
1830 explicit Got_entry(Symbol
* gsym
)
1831 : local_sym_index_(GSYM_CODE
)
1832 { this->u_
.gsym
= gsym
; }
1834 // Create a local symbol entry.
1835 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1836 unsigned int local_sym_index
)
1837 : local_sym_index_(local_sym_index
)
1839 gold_assert(local_sym_index
!= GSYM_CODE
1840 && local_sym_index
!= CONSTANT_CODE
);
1841 this->u_
.object
= object
;
1844 // Create a constant entry. The constant is a host value--it will
1845 // be swapped, if necessary, when it is written out.
1846 explicit Got_entry(Valtype constant
)
1847 : local_sym_index_(CONSTANT_CODE
)
1848 { this->u_
.constant
= constant
; }
1850 // Write the GOT entry to an output view.
1852 write(unsigned char* pov
) const;
1863 // For a local symbol, the object.
1864 Sized_relobj
<size
, big_endian
>* object
;
1865 // For a global symbol, the symbol.
1867 // For a constant, the constant.
1870 // For a local symbol, the local symbol index. This is GSYM_CODE
1871 // for a global symbol, or CONSTANT_CODE for a constant.
1872 unsigned int local_sym_index_
;
1875 typedef std::vector
<Got_entry
> Got_entries
;
1877 // Return the offset into the GOT of GOT entry I.
1879 got_offset(unsigned int i
) const
1880 { return i
* (size
/ 8); }
1882 // Return the offset into the GOT of the last entry added.
1884 last_got_offset() const
1885 { return this->got_offset(this->entries_
.size() - 1); }
1887 // Set the size of the section.
1890 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1892 // The list of GOT entries.
1893 Got_entries entries_
;
1896 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1899 class Output_data_dynamic
: public Output_section_data
1902 Output_data_dynamic(Stringpool
* pool
)
1903 : Output_section_data(Output_data::default_alignment()),
1904 entries_(), pool_(pool
)
1907 // Add a new dynamic entry with a fixed numeric value.
1909 add_constant(elfcpp::DT tag
, unsigned int val
)
1910 { this->add_entry(Dynamic_entry(tag
, val
)); }
1912 // Add a new dynamic entry with the address of output data.
1914 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1915 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1917 // Add a new dynamic entry with the address of output data
1918 // plus a constant offset.
1920 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
1921 unsigned int offset
)
1922 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
1924 // Add a new dynamic entry with the size of output data.
1926 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1927 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1929 // Add a new dynamic entry with the address of a symbol.
1931 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1932 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1934 // Add a new dynamic entry with a string.
1936 add_string(elfcpp::DT tag
, const char* str
)
1937 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1940 add_string(elfcpp::DT tag
, const std::string
& str
)
1941 { this->add_string(tag
, str
.c_str()); }
1944 // Adjust the output section to set the entry size.
1946 do_adjust_output_section(Output_section
*);
1948 // Set the final data size.
1950 set_final_data_size();
1952 // Write out the dynamic entries.
1954 do_write(Output_file
*);
1956 // Write to a map file.
1958 do_print_to_mapfile(Mapfile
* mapfile
) const
1959 { mapfile
->print_output_data(this, _("** dynamic")); }
1962 // This POD class holds a single dynamic entry.
1966 // Create an entry with a fixed numeric value.
1967 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1968 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
1969 { this->u_
.val
= val
; }
1971 // Create an entry with the size or address of a section.
1972 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1974 offset_(section_size
1975 ? DYNAMIC_SECTION_SIZE
1976 : DYNAMIC_SECTION_ADDRESS
)
1977 { this->u_
.od
= od
; }
1979 // Create an entry with the address of a section plus a constant offset.
1980 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
1983 { this->u_
.od
= od
; }
1985 // Create an entry with the address of a symbol.
1986 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1987 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
1988 { this->u_
.sym
= sym
; }
1990 // Create an entry with a string.
1991 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1992 : tag_(tag
), offset_(DYNAMIC_STRING
)
1993 { this->u_
.str
= str
; }
1995 // Return the tag of this entry.
1998 { return this->tag_
; }
2000 // Write the dynamic entry to an output view.
2001 template<int size
, bool big_endian
>
2003 write(unsigned char* pov
, const Stringpool
*) const;
2006 // Classification is encoded in the OFFSET field.
2010 DYNAMIC_SECTION_ADDRESS
= 0,
2012 DYNAMIC_NUMBER
= -1U,
2014 DYNAMIC_SECTION_SIZE
= -2U,
2016 DYNAMIC_SYMBOL
= -3U,
2018 DYNAMIC_STRING
= -4U
2019 // Any other value indicates a section address plus OFFSET.
2024 // For DYNAMIC_NUMBER.
2026 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2027 const Output_data
* od
;
2028 // For DYNAMIC_SYMBOL.
2030 // For DYNAMIC_STRING.
2035 // The type of entry (Classification) or offset within a section.
2036 unsigned int offset_
;
2039 // Add an entry to the list.
2041 add_entry(const Dynamic_entry
& entry
)
2042 { this->entries_
.push_back(entry
); }
2044 // Sized version of write function.
2045 template<int size
, bool big_endian
>
2047 sized_write(Output_file
* of
);
2049 // The type of the list of entries.
2050 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2053 Dynamic_entries entries_
;
2054 // The pool used for strings.
2058 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2059 // which may be required if the object file has more than
2060 // SHN_LORESERVE sections.
2062 class Output_symtab_xindex
: public Output_section_data
2065 Output_symtab_xindex(size_t symcount
)
2066 : Output_section_data(symcount
* 4, 4, true),
2070 // Add an entry: symbol number SYMNDX has section SHNDX.
2072 add(unsigned int symndx
, unsigned int shndx
)
2073 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2077 do_write(Output_file
*);
2079 // Write to a map file.
2081 do_print_to_mapfile(Mapfile
* mapfile
) const
2082 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2085 template<bool big_endian
>
2087 endian_do_write(unsigned char*);
2089 // It is likely that most symbols will not require entries. Rather
2090 // than keep a vector for all symbols, we keep pairs of symbol index
2091 // and section index.
2092 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2094 // The entries we need.
2095 Xindex_entries entries_
;
2098 // A relaxed input section.
2099 class Output_relaxed_input_section
: public Output_section_data_build
2102 // We would like to call relobj->section_addralign(shndx) to get the
2103 // alignment but we do not want the constructor to fail. So callers
2104 // are repsonsible for ensuring that.
2105 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2107 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2110 // Return the Relobj of this relaxed input section.
2113 { return this->relobj_
; }
2115 // Return the section index of this relaxed input section.
2118 { return this->shndx_
; }
2122 unsigned int shndx_
;
2125 // An output section. We don't expect to have too many output
2126 // sections, so we don't bother to do a template on the size.
2128 class Output_section
: public Output_data
2131 // Create an output section, giving the name, type, and flags.
2132 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2133 virtual ~Output_section();
2135 // Add a new input section SHNDX, named NAME, with header SHDR, from
2136 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2137 // which applies to this section, or 0 if none, or -1 if more than
2138 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2139 // in a linker script; in that case we need to keep track of input
2140 // sections associated with an output section. Return the offset
2141 // within the output section.
2142 template<int size
, bool big_endian
>
2144 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
2146 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2147 unsigned int reloc_shndx
, bool have_sections_script
);
2149 // Add generated data POSD to this output section.
2151 add_output_section_data(Output_section_data
* posd
);
2153 // Add a relaxed input section PORIS to this output section.
2155 add_relaxed_input_section(Output_relaxed_input_section
* poris
);
2157 // Return the section name.
2160 { return this->name_
; }
2162 // Return the section type.
2165 { return this->type_
; }
2167 // Return the section flags.
2170 { return this->flags_
; }
2172 // Update the output section flags based on input section flags.
2174 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2176 // Return the entsize field.
2179 { return this->entsize_
; }
2181 // Set the entsize field.
2183 set_entsize(uint64_t v
);
2185 // Set the load address.
2187 set_load_address(uint64_t load_address
)
2189 this->load_address_
= load_address
;
2190 this->has_load_address_
= true;
2193 // Set the link field to the output section index of a section.
2195 set_link_section(const Output_data
* od
)
2197 gold_assert(this->link_
== 0
2198 && !this->should_link_to_symtab_
2199 && !this->should_link_to_dynsym_
);
2200 this->link_section_
= od
;
2203 // Set the link field to a constant.
2205 set_link(unsigned int v
)
2207 gold_assert(this->link_section_
== NULL
2208 && !this->should_link_to_symtab_
2209 && !this->should_link_to_dynsym_
);
2213 // Record that this section should link to the normal symbol table.
2215 set_should_link_to_symtab()
2217 gold_assert(this->link_section_
== NULL
2219 && !this->should_link_to_dynsym_
);
2220 this->should_link_to_symtab_
= true;
2223 // Record that this section should link to the dynamic symbol table.
2225 set_should_link_to_dynsym()
2227 gold_assert(this->link_section_
== NULL
2229 && !this->should_link_to_symtab_
);
2230 this->should_link_to_dynsym_
= true;
2233 // Return the info field.
2237 gold_assert(this->info_section_
== NULL
2238 && this->info_symndx_
== NULL
);
2242 // Set the info field to the output section index of a section.
2244 set_info_section(const Output_section
* os
)
2246 gold_assert((this->info_section_
== NULL
2247 || (this->info_section_
== os
2248 && this->info_uses_section_index_
))
2249 && this->info_symndx_
== NULL
2250 && this->info_
== 0);
2251 this->info_section_
= os
;
2252 this->info_uses_section_index_
= true;
2255 // Set the info field to the symbol table index of a symbol.
2257 set_info_symndx(const Symbol
* sym
)
2259 gold_assert(this->info_section_
== NULL
2260 && (this->info_symndx_
== NULL
2261 || this->info_symndx_
== sym
)
2262 && this->info_
== 0);
2263 this->info_symndx_
= sym
;
2266 // Set the info field to the symbol table index of a section symbol.
2268 set_info_section_symndx(const Output_section
* os
)
2270 gold_assert((this->info_section_
== NULL
2271 || (this->info_section_
== os
2272 && !this->info_uses_section_index_
))
2273 && this->info_symndx_
== NULL
2274 && this->info_
== 0);
2275 this->info_section_
= os
;
2276 this->info_uses_section_index_
= false;
2279 // Set the info field to a constant.
2281 set_info(unsigned int v
)
2283 gold_assert(this->info_section_
== NULL
2284 && this->info_symndx_
== NULL
2285 && (this->info_
== 0
2286 || this->info_
== v
));
2290 // Set the addralign field.
2292 set_addralign(uint64_t v
)
2293 { this->addralign_
= v
; }
2295 // Whether the output section index has been set.
2297 has_out_shndx() const
2298 { return this->out_shndx_
!= -1U; }
2300 // Indicate that we need a symtab index.
2302 set_needs_symtab_index()
2303 { this->needs_symtab_index_
= true; }
2305 // Return whether we need a symtab index.
2307 needs_symtab_index() const
2308 { return this->needs_symtab_index_
; }
2310 // Get the symtab index.
2312 symtab_index() const
2314 gold_assert(this->symtab_index_
!= 0);
2315 return this->symtab_index_
;
2318 // Set the symtab index.
2320 set_symtab_index(unsigned int index
)
2322 gold_assert(index
!= 0);
2323 this->symtab_index_
= index
;
2326 // Indicate that we need a dynsym index.
2328 set_needs_dynsym_index()
2329 { this->needs_dynsym_index_
= true; }
2331 // Return whether we need a dynsym index.
2333 needs_dynsym_index() const
2334 { return this->needs_dynsym_index_
; }
2336 // Get the dynsym index.
2338 dynsym_index() const
2340 gold_assert(this->dynsym_index_
!= 0);
2341 return this->dynsym_index_
;
2344 // Set the dynsym index.
2346 set_dynsym_index(unsigned int index
)
2348 gold_assert(index
!= 0);
2349 this->dynsym_index_
= index
;
2352 // Return whether the input sections sections attachd to this output
2353 // section may require sorting. This is used to handle constructor
2354 // priorities compatibly with GNU ld.
2356 may_sort_attached_input_sections() const
2357 { return this->may_sort_attached_input_sections_
; }
2359 // Record that the input sections attached to this output section
2360 // may require sorting.
2362 set_may_sort_attached_input_sections()
2363 { this->may_sort_attached_input_sections_
= true; }
2365 // Return whether the input sections attached to this output section
2366 // require sorting. This is used to handle constructor priorities
2367 // compatibly with GNU ld.
2369 must_sort_attached_input_sections() const
2370 { return this->must_sort_attached_input_sections_
; }
2372 // Record that the input sections attached to this output section
2375 set_must_sort_attached_input_sections()
2376 { this->must_sort_attached_input_sections_
= true; }
2378 // Return whether this section holds relro data--data which has
2379 // dynamic relocations but which may be marked read-only after the
2380 // dynamic relocations have been completed.
2383 { return this->is_relro_
; }
2385 // Record that this section holds relro data.
2388 { this->is_relro_
= true; }
2390 // Record that this section does not hold relro data.
2393 { this->is_relro_
= false; }
2395 // True if this section holds relro local data--relro data for which
2396 // the dynamic relocations are all RELATIVE relocations.
2398 is_relro_local() const
2399 { return this->is_relro_local_
; }
2401 // Record that this section holds relro local data.
2403 set_is_relro_local()
2404 { this->is_relro_local_
= true; }
2406 // True if this must be the last relro section.
2408 is_last_relro() const
2409 { return this->is_last_relro_
; }
2411 // Record that this must be the last relro section.
2415 gold_assert(this->is_relro_
);
2416 this->is_last_relro_
= true;
2419 // True if this must be the first section following the relro sections.
2421 is_first_non_relro() const
2423 gold_assert(!this->is_relro_
);
2424 return this->is_first_non_relro_
;
2427 // Record that this must be the first non-relro section.
2429 set_is_first_non_relro()
2431 gold_assert(!this->is_relro_
);
2432 this->is_first_non_relro_
= true;
2435 // True if this is a small section: a section which holds small
2438 is_small_section() const
2439 { return this->is_small_section_
; }
2441 // Record that this is a small section.
2443 set_is_small_section()
2444 { this->is_small_section_
= true; }
2446 // True if this is a large section: a section which holds large
2449 is_large_section() const
2450 { return this->is_large_section_
; }
2452 // Record that this is a large section.
2454 set_is_large_section()
2455 { this->is_large_section_
= true; }
2457 // True if this is a large data (not BSS) section.
2459 is_large_data_section()
2460 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2462 // True if this is the .interp section which goes into the PT_INTERP
2466 { return this->is_interp_
; }
2468 // Record that this is the interp section.
2471 { this->is_interp_
= true; }
2473 // True if this is a section used by the dynamic linker.
2475 is_dynamic_linker_section() const
2476 { return this->is_dynamic_linker_section_
; }
2478 // Record that this is a section used by the dynamic linker.
2480 set_is_dynamic_linker_section()
2481 { this->is_dynamic_linker_section_
= true; }
2483 // Return whether this section should be written after all the input
2484 // sections are complete.
2486 after_input_sections() const
2487 { return this->after_input_sections_
; }
2489 // Record that this section should be written after all the input
2490 // sections are complete.
2492 set_after_input_sections()
2493 { this->after_input_sections_
= true; }
2495 // Return whether this section requires postprocessing after all
2496 // relocations have been applied.
2498 requires_postprocessing() const
2499 { return this->requires_postprocessing_
; }
2501 // If a section requires postprocessing, return the buffer to use.
2503 postprocessing_buffer() const
2505 gold_assert(this->postprocessing_buffer_
!= NULL
);
2506 return this->postprocessing_buffer_
;
2509 // If a section requires postprocessing, create the buffer to use.
2511 create_postprocessing_buffer();
2513 // If a section requires postprocessing, this is the size of the
2514 // buffer to which relocations should be applied.
2516 postprocessing_buffer_size() const
2517 { return this->current_data_size_for_child(); }
2519 // Modify the section name. This is only permitted for an
2520 // unallocated section, and only before the size has been finalized.
2521 // Otherwise the name will not get into Layout::namepool_.
2523 set_name(const char* newname
)
2525 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2526 gold_assert(!this->is_data_size_valid());
2527 this->name_
= newname
;
2530 // Return whether the offset OFFSET in the input section SHNDX in
2531 // object OBJECT is being included in the link.
2533 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2534 off_t offset
) const;
2536 // Return the offset within the output section of OFFSET relative to
2537 // the start of input section SHNDX in object OBJECT.
2539 output_offset(const Relobj
* object
, unsigned int shndx
,
2540 section_offset_type offset
) const;
2542 // Return the output virtual address of OFFSET relative to the start
2543 // of input section SHNDX in object OBJECT.
2545 output_address(const Relobj
* object
, unsigned int shndx
,
2546 off_t offset
) const;
2548 // Look for the merged section for input section SHNDX in object
2549 // OBJECT. If found, return true, and set *ADDR to the address of
2550 // the start of the merged section. This is not necessary the
2551 // output offset corresponding to input offset 0 in the section,
2552 // since the section may be mapped arbitrarily.
2554 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2555 uint64_t* addr
) const;
2557 // Record that this output section was found in the SECTIONS clause
2558 // of a linker script.
2560 set_found_in_sections_clause()
2561 { this->found_in_sections_clause_
= true; }
2563 // Return whether this output section was found in the SECTIONS
2564 // clause of a linker script.
2566 found_in_sections_clause() const
2567 { return this->found_in_sections_clause_
; }
2569 // Write the section header into *OPHDR.
2570 template<int size
, bool big_endian
>
2572 write_header(const Layout
*, const Stringpool
*,
2573 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2575 // The next few calls are for linker script support.
2577 // We need to export the input sections to linker scripts. Previously
2578 // we export a pair of Relobj pointer and section index. We now need to
2579 // handle relaxed input sections as well. So we use this class.
2580 class Simple_input_section
2583 static const unsigned int invalid_shndx
= static_cast<unsigned int>(-1);
2586 Simple_input_section(Relobj
*relobj
, unsigned int shndx
)
2589 gold_assert(shndx
!= invalid_shndx
);
2590 this->u_
.relobj
= relobj
;
2593 Simple_input_section(Output_relaxed_input_section
* section
)
2594 : shndx_(invalid_shndx
)
2595 { this->u_
.relaxed_input_section
= section
; }
2597 // Whether this is a relaxed section.
2599 is_relaxed_input_section() const
2600 { return this->shndx_
== invalid_shndx
; }
2602 // Return object of an input section.
2606 return ((this->shndx_
!= invalid_shndx
)
2608 : this->u_
.relaxed_input_section
->relobj());
2611 // Return index of an input section.
2615 return ((this->shndx_
!= invalid_shndx
)
2617 : this->u_
.relaxed_input_section
->shndx());
2620 // Return the Output_relaxed_input_section object of a relaxed section.
2621 Output_relaxed_input_section
*
2622 relaxed_input_section() const
2624 gold_assert(this->shndx_
== invalid_shndx
);
2625 return this->u_
.relaxed_input_section
;
2629 // Pointer to either an Relobj or an Output_relaxed_input_section.
2633 Output_relaxed_input_section
* relaxed_input_section
;
2635 // Section index for an non-relaxed section or invalid_shndx for
2636 // a relaxed section.
2637 unsigned int shndx_
;
2640 // Store the list of input sections for this Output_section into the
2641 // list passed in. This removes the input sections, leaving only
2642 // any Output_section_data elements. This returns the size of those
2643 // Output_section_data elements. ADDRESS is the address of this
2644 // output section. FILL is the fill value to use, in case there are
2645 // any spaces between the remaining Output_section_data elements.
2647 get_input_sections(uint64_t address
, const std::string
& fill
,
2648 std::list
<Simple_input_section
>*);
2650 // Add an input section from a script.
2652 add_input_section_for_script(const Simple_input_section
& input_section
,
2653 off_t data_size
, uint64_t addralign
);
2655 // Set the current size of the output section.
2657 set_current_data_size(off_t size
)
2658 { this->set_current_data_size_for_child(size
); }
2660 // Get the current size of the output section.
2662 current_data_size() const
2663 { return this->current_data_size_for_child(); }
2665 // End of linker script support.
2667 // Save states before doing section layout.
2668 // This is used for relaxation.
2672 // Restore states prior to section layout.
2676 // Convert existing input sections to relaxed input sections.
2678 convert_input_sections_to_relaxed_sections(
2679 const std::vector
<Output_relaxed_input_section
*>& sections
);
2681 // Find a relaxed input section to an input section in OBJECT
2682 // with index SHNDX. Return NULL if none is found.
2683 const Output_relaxed_input_section
*
2684 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
2686 // Print merge statistics to stderr.
2688 print_merge_stats();
2691 // Return the output section--i.e., the object itself.
2696 const Output_section
*
2697 do_output_section() const
2700 // Return the section index in the output file.
2702 do_out_shndx() const
2704 gold_assert(this->out_shndx_
!= -1U);
2705 return this->out_shndx_
;
2708 // Set the output section index.
2710 do_set_out_shndx(unsigned int shndx
)
2712 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2713 this->out_shndx_
= shndx
;
2716 // Set the final data size of the Output_section. For a typical
2717 // Output_section, there is nothing to do, but if there are any
2718 // Output_section_data objects we need to set their final addresses
2721 set_final_data_size();
2723 // Reset the address and file offset.
2725 do_reset_address_and_file_offset();
2727 // Return true if address and file offset already have reset values. In
2728 // other words, calling reset_address_and_file_offset will not change them.
2730 do_address_and_file_offset_have_reset_values() const;
2732 // Write the data to the file. For a typical Output_section, this
2733 // does nothing: the data is written out by calling Object::Relocate
2734 // on each input object. But if there are any Output_section_data
2735 // objects we do need to write them out here.
2737 do_write(Output_file
*);
2739 // Return the address alignment--function required by parent class.
2741 do_addralign() const
2742 { return this->addralign_
; }
2744 // Return whether there is a load address.
2746 do_has_load_address() const
2747 { return this->has_load_address_
; }
2749 // Return the load address.
2751 do_load_address() const
2753 gold_assert(this->has_load_address_
);
2754 return this->load_address_
;
2757 // Return whether this is an Output_section.
2759 do_is_section() const
2762 // Return whether this is a section of the specified type.
2764 do_is_section_type(elfcpp::Elf_Word type
) const
2765 { return this->type_
== type
; }
2767 // Return whether the specified section flag is set.
2769 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2770 { return (this->flags_
& flag
) != 0; }
2772 // Set the TLS offset. Called only for SHT_TLS sections.
2774 do_set_tls_offset(uint64_t tls_base
);
2776 // Return the TLS offset, relative to the base of the TLS segment.
2777 // Valid only for SHT_TLS sections.
2779 do_tls_offset() const
2780 { return this->tls_offset_
; }
2782 // This may be implemented by a child class.
2784 do_finalize_name(Layout
*)
2787 // Print to the map file.
2789 do_print_to_mapfile(Mapfile
*) const;
2791 // Record that this section requires postprocessing after all
2792 // relocations have been applied. This is called by a child class.
2794 set_requires_postprocessing()
2796 this->requires_postprocessing_
= true;
2797 this->after_input_sections_
= true;
2800 // Write all the data of an Output_section into the postprocessing
2803 write_to_postprocessing_buffer();
2805 // In some cases we need to keep a list of the input sections
2806 // associated with this output section. We only need the list if we
2807 // might have to change the offsets of the input section within the
2808 // output section after we add the input section. The ordinary
2809 // input sections will be written out when we process the object
2810 // file, and as such we don't need to track them here. We do need
2811 // to track Output_section_data objects here. We store instances of
2812 // this structure in a std::vector, so it must be a POD. There can
2813 // be many instances of this structure, so we use a union to save
2819 : shndx_(0), p2align_(0)
2821 this->u1_
.data_size
= 0;
2822 this->u2_
.object
= NULL
;
2825 // For an ordinary input section.
2826 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2829 p2align_(ffsll(static_cast<long long>(addralign
)))
2831 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2832 && shndx
!= MERGE_DATA_SECTION_CODE
2833 && shndx
!= MERGE_STRING_SECTION_CODE
2834 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
2835 this->u1_
.data_size
= data_size
;
2836 this->u2_
.object
= object
;
2839 // For a non-merge output section.
2840 Input_section(Output_section_data
* posd
)
2841 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0)
2843 this->u1_
.data_size
= 0;
2844 this->u2_
.posd
= posd
;
2847 // For a merge section.
2848 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2850 ? MERGE_STRING_SECTION_CODE
2851 : MERGE_DATA_SECTION_CODE
),
2854 this->u1_
.entsize
= entsize
;
2855 this->u2_
.posd
= posd
;
2858 // For a relaxed input section.
2859 Input_section(Output_relaxed_input_section
*psection
)
2860 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0)
2862 this->u1_
.data_size
= 0;
2863 this->u2_
.poris
= psection
;
2866 // The required alignment.
2870 if (!this->is_input_section())
2871 return this->u2_
.posd
->addralign();
2872 return (this->p2align_
== 0
2874 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2877 // Return the required size.
2881 // Whether this is an input section.
2883 is_input_section() const
2885 return (this->shndx_
!= OUTPUT_SECTION_CODE
2886 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2887 && this->shndx_
!= MERGE_STRING_SECTION_CODE
2888 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
2891 // Return whether this is a merge section which matches the
2894 is_merge_section(bool is_string
, uint64_t entsize
,
2895 uint64_t addralign
) const
2897 return (this->shndx_
== (is_string
2898 ? MERGE_STRING_SECTION_CODE
2899 : MERGE_DATA_SECTION_CODE
)
2900 && this->u1_
.entsize
== entsize
2901 && this->addralign() == addralign
);
2904 // Return whether this is a relaxed input section.
2906 is_relaxed_input_section() const
2907 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
2909 // Return whether this is a generic Output_section_data.
2911 is_output_section_data() const
2913 return this->shndx_
== OUTPUT_SECTION_CODE
;
2916 // Return the object for an input section.
2920 if (this->is_input_section())
2921 return this->u2_
.object
;
2922 else if (this->is_relaxed_input_section())
2923 return this->u2_
.poris
->relobj();
2928 // Return the input section index for an input section.
2932 if (this->is_input_section())
2933 return this->shndx_
;
2934 else if (this->is_relaxed_input_section())
2935 return this->u2_
.poris
->shndx();
2940 // For non-input-sections, return the associated Output_section_data
2942 Output_section_data
*
2943 output_section_data() const
2945 gold_assert(!this->is_input_section());
2946 return this->u2_
.posd
;
2949 // Return the Output_relaxed_input_section object.
2950 Output_relaxed_input_section
*
2951 relaxed_input_section() const
2953 gold_assert(this->is_relaxed_input_section());
2954 return this->u2_
.poris
;
2957 // Set the output section.
2959 set_output_section(Output_section
* os
)
2961 gold_assert(!this->is_input_section());
2962 Output_section_data
*posd
=
2963 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
2964 posd
->set_output_section(os
);
2967 // Set the address and file offset. This is called during
2968 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2969 // the enclosing section.
2971 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2972 off_t section_file_offset
);
2974 // Reset the address and file offset.
2976 reset_address_and_file_offset();
2978 // Finalize the data size.
2980 finalize_data_size();
2982 // Add an input section, for SHF_MERGE sections.
2984 add_input_section(Relobj
* object
, unsigned int shndx
)
2986 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2987 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2988 return this->u2_
.posd
->add_input_section(object
, shndx
);
2991 // Given an input OBJECT, an input section index SHNDX within that
2992 // object, and an OFFSET relative to the start of that input
2993 // section, return whether or not the output offset is known. If
2994 // this function returns true, it sets *POUTPUT to the offset in
2995 // the output section, relative to the start of the input section
2996 // in the output section. *POUTPUT may be different from OFFSET
2997 // for a merged section.
2999 output_offset(const Relobj
* object
, unsigned int shndx
,
3000 section_offset_type offset
,
3001 section_offset_type
*poutput
) const;
3003 // Return whether this is the merge section for the input section
3006 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3008 // Write out the data. This does nothing for an input section.
3010 write(Output_file
*);
3012 // Write the data to a buffer. This does nothing for an input
3015 write_to_buffer(unsigned char*);
3017 // Print to a map file.
3019 print_to_mapfile(Mapfile
*) const;
3021 // Print statistics about merge sections to stderr.
3023 print_merge_stats(const char* section_name
)
3025 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3026 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3027 this->u2_
.posd
->print_merge_stats(section_name
);
3031 // Code values which appear in shndx_. If the value is not one of
3032 // these codes, it is the input section index in the object file.
3035 // An Output_section_data.
3036 OUTPUT_SECTION_CODE
= -1U,
3037 // An Output_section_data for an SHF_MERGE section with
3038 // SHF_STRINGS not set.
3039 MERGE_DATA_SECTION_CODE
= -2U,
3040 // An Output_section_data for an SHF_MERGE section with
3042 MERGE_STRING_SECTION_CODE
= -3U,
3043 // An Output_section_data for a relaxed input section.
3044 RELAXED_INPUT_SECTION_CODE
= -4U
3047 // For an ordinary input section, this is the section index in the
3048 // input file. For an Output_section_data, this is
3049 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3050 // MERGE_STRING_SECTION_CODE.
3051 unsigned int shndx_
;
3052 // The required alignment, stored as a power of 2.
3053 unsigned int p2align_
;
3056 // For an ordinary input section, the section size.
3058 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3059 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3065 // For an ordinary input section, the object which holds the
3068 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3069 // MERGE_STRING_SECTION_CODE, the data.
3070 Output_section_data
* posd
;
3071 // For RELAXED_INPUT_SECTION_CODE, the data.
3072 Output_relaxed_input_section
* poris
;
3076 typedef std::vector
<Input_section
> Input_section_list
;
3078 // Allow a child class to access the input sections.
3079 const Input_section_list
&
3080 input_sections() const
3081 { return this->input_sections_
; }
3084 // We only save enough information to undo the effects of section layout.
3085 class Checkpoint_output_section
3088 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3089 const Input_section_list
& input_sections
,
3090 off_t first_input_offset
,
3091 bool attached_input_sections_are_sorted
)
3092 : addralign_(addralign
), flags_(flags
),
3093 input_sections_(input_sections
),
3094 input_sections_size_(input_sections_
.size()),
3095 input_sections_copy_(), first_input_offset_(first_input_offset
),
3096 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3100 ~Checkpoint_output_section()
3103 // Return the address alignment.
3106 { return this->addralign_
; }
3108 // Return the section flags.
3111 { return this->flags_
; }
3113 // Return a reference to the input section list copy.
3116 { return &this->input_sections_copy_
; }
3118 // Return the size of input_sections at the time when checkpoint is
3121 input_sections_size() const
3122 { return this->input_sections_size_
; }
3124 // Whether input sections are copied.
3126 input_sections_saved() const
3127 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3130 first_input_offset() const
3131 { return this->first_input_offset_
; }
3134 attached_input_sections_are_sorted() const
3135 { return this->attached_input_sections_are_sorted_
; }
3137 // Save input sections.
3139 save_input_sections()
3141 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3142 this->input_sections_copy_
.clear();
3143 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3144 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3145 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3146 this->input_sections_copy_
.push_back(*p
);
3150 // The section alignment.
3151 uint64_t addralign_
;
3152 // The section flags.
3153 elfcpp::Elf_Xword flags_
;
3154 // Reference to the input sections to be checkpointed.
3155 const Input_section_list
& input_sections_
;
3156 // Size of the checkpointed portion of input_sections_;
3157 size_t input_sections_size_
;
3158 // Copy of input sections.
3159 Input_section_list input_sections_copy_
;
3160 // The offset of the first entry in input_sections_.
3161 off_t first_input_offset_
;
3162 // True if the input sections attached to this output section have
3163 // already been sorted.
3164 bool attached_input_sections_are_sorted_
;
3167 // This class is used to sort the input sections.
3168 class Input_section_sort_entry
;
3170 // This is the sort comparison function.
3171 struct Input_section_sort_compare
3174 operator()(const Input_section_sort_entry
&,
3175 const Input_section_sort_entry
&) const;
3178 // Fill data. This is used to fill in data between input sections.
3179 // It is also used for data statements (BYTE, WORD, etc.) in linker
3180 // scripts. When we have to keep track of the input sections, we
3181 // can use an Output_data_const, but we don't want to have to keep
3182 // track of input sections just to implement fills.
3186 Fill(off_t section_offset
, off_t length
)
3187 : section_offset_(section_offset
),
3188 length_(convert_to_section_size_type(length
))
3191 // Return section offset.
3193 section_offset() const
3194 { return this->section_offset_
; }
3196 // Return fill length.
3199 { return this->length_
; }
3202 // The offset within the output section.
3203 off_t section_offset_
;
3204 // The length of the space to fill.
3205 section_size_type length_
;
3208 typedef std::vector
<Fill
> Fill_list
;
3210 // This class describes properties of merge data sections. It is used
3211 // as a key type for maps.
3212 class Merge_section_properties
3215 Merge_section_properties(bool is_string
, uint64_t entsize
,
3217 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
3220 // Whether this equals to another Merge_section_properties MSP.
3222 eq(const Merge_section_properties
& msp
) const
3224 return ((this->is_string_
== msp
.is_string_
)
3225 && (this->entsize_
== msp
.entsize_
)
3226 && (this->addralign_
== msp
.addralign_
));
3229 // Compute a hash value for this using 64-bit FNV-1a hash.
3233 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
3234 uint64_t prime
= 1099511628211ULL;
3235 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
3236 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
3237 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
3241 // Functors for associative containers.
3245 operator()(const Merge_section_properties
& msp1
,
3246 const Merge_section_properties
& msp2
) const
3247 { return msp1
.eq(msp2
); }
3253 operator()(const Merge_section_properties
& msp
) const
3254 { return msp
.hash_value(); }
3258 // Whether this merge data section is for strings.
3260 // Entsize of this merge data section.
3262 // Address alignment.
3263 uint64_t addralign_
;
3266 // Map that link Merge_section_properties to Output_merge_base.
3267 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
3268 Merge_section_properties::hash
,
3269 Merge_section_properties::equal_to
>
3270 Merge_section_by_properties_map
;
3272 // Map that link Input_section_specifier to Output_section_data.
3273 typedef Unordered_map
<Input_section_specifier
, Output_section_data
*,
3274 Input_section_specifier::hash
,
3275 Input_section_specifier::equal_to
>
3276 Output_section_data_by_input_section_map
;
3278 // Map that link Input_section_specifier to Output_relaxed_input_section.
3279 typedef Unordered_map
<Input_section_specifier
, Output_relaxed_input_section
*,
3280 Input_section_specifier::hash
,
3281 Input_section_specifier::equal_to
>
3282 Output_relaxed_input_section_by_input_section_map
;
3284 // Map used during relaxation of existing sections. This map
3285 // an input section specifier to an input section list index.
3286 // We assume that Input_section_list is a vector.
3287 typedef Unordered_map
<Input_section_specifier
, size_t,
3288 Input_section_specifier::hash
,
3289 Input_section_specifier::equal_to
>
3292 // Add a new output section by Input_section.
3294 add_output_section_data(Input_section
*);
3296 // Add an SHF_MERGE input section. Returns true if the section was
3299 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3300 uint64_t entsize
, uint64_t addralign
);
3302 // Add an output SHF_MERGE section POSD to this output section.
3303 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3304 // ENTSIZE is the entity size. This returns the entry added to
3307 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3310 // Sort the attached input sections.
3312 sort_attached_input_sections();
3314 // Find the merge section into which an input section with index SHNDX in
3315 // OBJECT has been added. Return NULL if none found.
3316 Output_section_data
*
3317 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3319 // Build a relaxation map.
3321 build_relaxation_map(
3322 const Input_section_list
& input_sections
,
3324 Relaxation_map
* map
) const;
3326 // Convert input sections in an input section list into relaxed sections.
3328 convert_input_sections_in_list_to_relaxed_sections(
3329 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3330 const Relaxation_map
& map
,
3331 Input_section_list
* input_sections
);
3333 // Most of these fields are only valid after layout.
3335 // The name of the section. This will point into a Stringpool.
3337 // The section address is in the parent class.
3338 // The section alignment.
3339 uint64_t addralign_
;
3340 // The section entry size.
3342 // The load address. This is only used when using a linker script
3343 // with a SECTIONS clause. The has_load_address_ field indicates
3344 // whether this field is valid.
3345 uint64_t load_address_
;
3346 // The file offset is in the parent class.
3347 // Set the section link field to the index of this section.
3348 const Output_data
* link_section_
;
3349 // If link_section_ is NULL, this is the link field.
3351 // Set the section info field to the index of this section.
3352 const Output_section
* info_section_
;
3353 // If info_section_ is NULL, set the info field to the symbol table
3354 // index of this symbol.
3355 const Symbol
* info_symndx_
;
3356 // If info_section_ and info_symndx_ are NULL, this is the section
3359 // The section type.
3360 const elfcpp::Elf_Word type_
;
3361 // The section flags.
3362 elfcpp::Elf_Xword flags_
;
3363 // The section index.
3364 unsigned int out_shndx_
;
3365 // If there is a STT_SECTION for this output section in the normal
3366 // symbol table, this is the symbol index. This starts out as zero.
3367 // It is initialized in Layout::finalize() to be the index, or -1U
3368 // if there isn't one.
3369 unsigned int symtab_index_
;
3370 // If there is a STT_SECTION for this output section in the dynamic
3371 // symbol table, this is the symbol index. This starts out as zero.
3372 // It is initialized in Layout::finalize() to be the index, or -1U
3373 // if there isn't one.
3374 unsigned int dynsym_index_
;
3375 // The input sections. This will be empty in cases where we don't
3376 // need to keep track of them.
3377 Input_section_list input_sections_
;
3378 // The offset of the first entry in input_sections_.
3379 off_t first_input_offset_
;
3380 // The fill data. This is separate from input_sections_ because we
3381 // often will need fill sections without needing to keep track of
3384 // If the section requires postprocessing, this buffer holds the
3385 // section contents during relocation.
3386 unsigned char* postprocessing_buffer_
;
3387 // Whether this output section needs a STT_SECTION symbol in the
3388 // normal symbol table. This will be true if there is a relocation
3390 bool needs_symtab_index_
: 1;
3391 // Whether this output section needs a STT_SECTION symbol in the
3392 // dynamic symbol table. This will be true if there is a dynamic
3393 // relocation which needs it.
3394 bool needs_dynsym_index_
: 1;
3395 // Whether the link field of this output section should point to the
3396 // normal symbol table.
3397 bool should_link_to_symtab_
: 1;
3398 // Whether the link field of this output section should point to the
3399 // dynamic symbol table.
3400 bool should_link_to_dynsym_
: 1;
3401 // Whether this section should be written after all the input
3402 // sections are complete.
3403 bool after_input_sections_
: 1;
3404 // Whether this section requires post processing after all
3405 // relocations have been applied.
3406 bool requires_postprocessing_
: 1;
3407 // Whether an input section was mapped to this output section
3408 // because of a SECTIONS clause in a linker script.
3409 bool found_in_sections_clause_
: 1;
3410 // Whether this section has an explicitly specified load address.
3411 bool has_load_address_
: 1;
3412 // True if the info_section_ field means the section index of the
3413 // section, false if it means the symbol index of the corresponding
3415 bool info_uses_section_index_
: 1;
3416 // True if the input sections attached to this output section may
3418 bool may_sort_attached_input_sections_
: 1;
3419 // True if the input sections attached to this output section must
3421 bool must_sort_attached_input_sections_
: 1;
3422 // True if the input sections attached to this output section have
3423 // already been sorted.
3424 bool attached_input_sections_are_sorted_
: 1;
3425 // True if this section holds relro data.
3427 // True if this section holds relro local data.
3428 bool is_relro_local_
: 1;
3429 // True if this must be the last relro section.
3430 bool is_last_relro_
: 1;
3431 // True if this must be the first section after the relro sections.
3432 bool is_first_non_relro_
: 1;
3433 // True if this is a small section.
3434 bool is_small_section_
: 1;
3435 // True if this is a large section.
3436 bool is_large_section_
: 1;
3437 // True if this is the .interp section going into the PT_INTERP
3439 bool is_interp_
: 1;
3440 // True if this is section is read by the dynamic linker.
3441 bool is_dynamic_linker_section_
: 1;
3442 // Whether code-fills are generated at write.
3443 bool generate_code_fills_at_write_
: 1;
3444 // Whether the entry size field should be zero.
3445 bool is_entsize_zero_
: 1;
3446 // For SHT_TLS sections, the offset of this section relative to the base
3447 // of the TLS segment.
3448 uint64_t tls_offset_
;
3449 // Saved checkpoint.
3450 Checkpoint_output_section
* checkpoint_
;
3451 // Map from input sections to merge sections.
3452 Output_section_data_by_input_section_map merge_section_map_
;
3453 // Map from merge section properties to merge_sections;
3454 Merge_section_by_properties_map merge_section_by_properties_map_
;
3455 // Map from input sections to relaxed input sections. This is mutable
3456 // because it is updated lazily. We may need to update it in a
3457 // const qualified method.
3458 mutable Output_relaxed_input_section_by_input_section_map
3459 relaxed_input_section_map_
;
3460 // Whether relaxed_input_section_map_ is valid.
3461 mutable bool is_relaxed_input_section_map_valid_
;
3464 // An output segment. PT_LOAD segments are built from collections of
3465 // output sections. Other segments typically point within PT_LOAD
3466 // segments, and are built directly as needed.
3468 // NOTE: We want to use the copy constructor for this class. During
3469 // relaxation, we may try built the segments multiple times. We do
3470 // that by copying the original segment list before lay-out, doing
3471 // a trial lay-out and roll-back to the saved copied if we need to
3472 // to the lay-out again.
3474 class Output_segment
3477 // Create an output segment, specifying the type and flags.
3478 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3480 // Return the virtual address.
3483 { return this->vaddr_
; }
3485 // Return the physical address.
3488 { return this->paddr_
; }
3490 // Return the segment type.
3493 { return this->type_
; }
3495 // Return the segment flags.
3498 { return this->flags_
; }
3500 // Return the memory size.
3503 { return this->memsz_
; }
3505 // Return the file size.
3508 { return this->filesz_
; }
3510 // Return the file offset.
3513 { return this->offset_
; }
3515 // Whether this is a segment created to hold large data sections.
3517 is_large_data_segment() const
3518 { return this->is_large_data_segment_
; }
3520 // Record that this is a segment created to hold large data
3523 set_is_large_data_segment()
3524 { this->is_large_data_segment_
= true; }
3526 // Return the maximum alignment of the Output_data.
3528 maximum_alignment();
3530 // Add the Output_section OS to this segment. SEG_FLAGS is the
3531 // segment flags to use. DO_SORT is true if we should sort the
3532 // placement of the input section for more efficient generated code.
3534 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
,
3537 // Remove an Output_section from this segment. It is an error if it
3540 remove_output_section(Output_section
* os
);
3542 // Add an Output_data (which need not be an Output_section) to the
3543 // start of this segment.
3545 add_initial_output_data(Output_data
*);
3547 // Return true if this segment has any sections which hold actual
3548 // data, rather than being a BSS section.
3550 has_any_data_sections() const
3551 { return !this->output_data_
.empty(); }
3553 // Return the number of dynamic relocations applied to this segment.
3555 dynamic_reloc_count() const;
3557 // Return the address of the first section.
3559 first_section_load_address() const;
3561 // Return whether the addresses have been set already.
3563 are_addresses_set() const
3564 { return this->are_addresses_set_
; }
3566 // Set the addresses.
3568 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3570 this->vaddr_
= vaddr
;
3571 this->paddr_
= paddr
;
3572 this->are_addresses_set_
= true;
3575 // Update the flags for the flags of an output section added to this
3578 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
3580 // The ELF ABI specifies that a PT_TLS segment should always have
3581 // PF_R as the flags.
3582 if (this->type() != elfcpp::PT_TLS
)
3583 this->flags_
|= flags
;
3586 // Set the segment flags. This is only used if we have a PHDRS
3587 // clause which explicitly specifies the flags.
3589 set_flags(elfcpp::Elf_Word flags
)
3590 { this->flags_
= flags
; }
3592 // Set the address of the segment to ADDR and the offset to *POFF
3593 // and set the addresses and offsets of all contained output
3594 // sections accordingly. Set the section indexes of all contained
3595 // output sections starting with *PSHNDX. If RESET is true, first
3596 // reset the addresses of the contained sections. Return the
3597 // address of the immediately following segment. Update *POFF and
3598 // *PSHNDX. This should only be called for a PT_LOAD segment.
3600 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
,
3601 unsigned int increase_relro
, off_t
* poff
,
3602 unsigned int* pshndx
);
3604 // Set the minimum alignment of this segment. This may be adjusted
3605 // upward based on the section alignments.
3607 set_minimum_p_align(uint64_t align
)
3608 { this->min_p_align_
= align
; }
3610 // Set the offset of this segment based on the section. This should
3611 // only be called for a non-PT_LOAD segment.
3613 set_offset(unsigned int increase
);
3615 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3619 // Return the number of output sections.
3621 output_section_count() const;
3623 // Return the section attached to the list segment with the lowest
3624 // load address. This is used when handling a PHDRS clause in a
3627 section_with_lowest_load_address() const;
3629 // Write the segment header into *OPHDR.
3630 template<int size
, bool big_endian
>
3632 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
3634 // Write the section headers of associated sections into V.
3635 template<int size
, bool big_endian
>
3637 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
3638 unsigned int* pshndx
) const;
3640 // Print the output sections in the map file.
3642 print_sections_to_mapfile(Mapfile
*) const;
3645 typedef std::list
<Output_data
*> Output_data_list
;
3647 // Find the maximum alignment in an Output_data_list.
3649 maximum_alignment_list(const Output_data_list
*);
3651 // Return whether the first data section is a relro section.
3653 is_first_section_relro() const;
3655 // Set the section addresses in an Output_data_list.
3657 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
3658 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
3661 // Return the number of Output_sections in an Output_data_list.
3663 output_section_count_list(const Output_data_list
*) const;
3665 // Return the number of dynamic relocs in an Output_data_list.
3667 dynamic_reloc_count_list(const Output_data_list
*) const;
3669 // Find the section with the lowest load address in an
3670 // Output_data_list.
3672 lowest_load_address_in_list(const Output_data_list
* pdl
,
3673 Output_section
** found
,
3674 uint64_t* found_lma
) const;
3676 // Write the section headers in the list into V.
3677 template<int size
, bool big_endian
>
3679 write_section_headers_list(const Layout
*, const Stringpool
*,
3680 const Output_data_list
*, unsigned char* v
,
3681 unsigned int* pshdx
) const;
3683 // Print a section list to the mapfile.
3685 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
3687 // NOTE: We want to use the copy constructor. Currently, shallow copy
3688 // works for us so we do not need to write our own copy constructor.
3690 // The list of output data with contents attached to this segment.
3691 Output_data_list output_data_
;
3692 // The list of output data without contents attached to this segment.
3693 Output_data_list output_bss_
;
3694 // The segment virtual address.
3696 // The segment physical address.
3698 // The size of the segment in memory.
3700 // The maximum section alignment. The is_max_align_known_ field
3701 // indicates whether this has been finalized.
3702 uint64_t max_align_
;
3703 // The required minimum value for the p_align field. This is used
3704 // for PT_LOAD segments. Note that this does not mean that
3705 // addresses should be aligned to this value; it means the p_paddr
3706 // and p_vaddr fields must be congruent modulo this value. For
3707 // non-PT_LOAD segments, the dynamic linker works more efficiently
3708 // if the p_align field has the more conventional value, although it
3709 // can align as needed.
3710 uint64_t min_p_align_
;
3711 // The offset of the segment data within the file.
3713 // The size of the segment data in the file.
3715 // The segment type;
3716 elfcpp::Elf_Word type_
;
3717 // The segment flags.
3718 elfcpp::Elf_Word flags_
;
3719 // Whether we have finalized max_align_.
3720 bool is_max_align_known_
: 1;
3721 // Whether vaddr and paddr were set by a linker script.
3722 bool are_addresses_set_
: 1;
3723 // Whether this segment holds large data sections.
3724 bool is_large_data_segment_
: 1;
3727 // This class represents the output file.
3732 Output_file(const char* name
);
3734 // Indicate that this is a temporary file which should not be
3738 { this->is_temporary_
= true; }
3740 // Try to open an existing file. Returns false if the file doesn't
3741 // exist, has a size of 0 or can't be mmaped. This method is
3744 open_for_modification();
3746 // Open the output file. FILE_SIZE is the final size of the file.
3747 // If the file already exists, it is deleted/truncated. This method
3748 // is thread-unsafe.
3750 open(off_t file_size
);
3752 // Resize the output file. This method is thread-unsafe.
3754 resize(off_t file_size
);
3756 // Close the output file (flushing all buffered data) and make sure
3757 // there are no errors. This method is thread-unsafe.
3761 // Return the size of this file.
3764 { return this->file_size_
; }
3766 // Return the name of this file.
3769 { return this->name_
; }
3771 // We currently always use mmap which makes the view handling quite
3772 // simple. In the future we may support other approaches.
3774 // Write data to the output file.
3776 write(off_t offset
, const void* data
, size_t len
)
3777 { memcpy(this->base_
+ offset
, data
, len
); }
3779 // Get a buffer to use to write to the file, given the offset into
3780 // the file and the size.
3782 get_output_view(off_t start
, size_t size
)
3784 gold_assert(start
>= 0
3785 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
3786 return this->base_
+ start
;
3789 // VIEW must have been returned by get_output_view. Write the
3790 // buffer to the file, passing in the offset and the size.
3792 write_output_view(off_t
, size_t, unsigned char*)
3795 // Get a read/write buffer. This is used when we want to write part
3796 // of the file, read it in, and write it again.
3798 get_input_output_view(off_t start
, size_t size
)
3799 { return this->get_output_view(start
, size
); }
3801 // Write a read/write buffer back to the file.
3803 write_input_output_view(off_t
, size_t, unsigned char*)
3806 // Get a read buffer. This is used when we just want to read part
3807 // of the file back it in.
3808 const unsigned char*
3809 get_input_view(off_t start
, size_t size
)
3810 { return this->get_output_view(start
, size
); }
3812 // Release a read bfufer.
3814 free_input_view(off_t
, size_t, const unsigned char*)
3818 // Map the file into memory or, if that fails, allocate anonymous
3823 // Allocate anonymous memory for the file.
3827 // Map the file into memory.
3831 // Unmap the file from memory (and flush to disk buffers).
3841 // Base of file mapped into memory.
3842 unsigned char* base_
;
3843 // True iff base_ points to a memory buffer rather than an output file.
3844 bool map_is_anonymous_
;
3845 // True if this is a temporary file which should not be output.
3849 } // End namespace gold.
3851 #endif // !defined(GOLD_OUTPUT_H)