1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007 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.
31 #include "reloc-types.h"
36 class General_options
;
41 class Relocatable_relocs
;
43 template<int size
, bool big_endian
>
45 template<int size
, bool big_endian
>
48 // An abtract class for data which has to go into the output file.
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
68 gold_assert(this->is_address_valid_
);
69 return this->address_
;
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
78 gold_assert(this->is_data_size_valid_
);
79 return this->data_size_
;
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
88 gold_assert(this->is_offset_valid_
);
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
95 reset_address_and_file_offset()
97 this->is_address_valid_
= false;
98 this->is_offset_valid_
= false;
99 this->is_data_size_valid_
= false;
100 this->do_reset_address_and_file_offset();
103 // Return the required alignment.
106 { return this->do_addralign(); }
108 // Return whether this has a load address.
110 has_load_address() const
111 { return this->do_has_load_address(); }
113 // Return the load address.
116 { return this->do_load_address(); }
118 // Return whether this is an Output_section.
121 { return this->do_is_section(); }
123 // Return whether this is an Output_section of the specified type.
125 is_section_type(elfcpp::Elf_Word stt
) const
126 { return this->do_is_section_type(stt
); }
128 // Return whether this is an Output_section with the specified flag
131 is_section_flag_set(elfcpp::Elf_Xword shf
) const
132 { return this->do_is_section_flag_set(shf
); }
134 // Return the output section index, if there is an output section.
137 { return this->do_out_shndx(); }
139 // Set the output section index, if this is an output section.
141 set_out_shndx(unsigned int shndx
)
142 { this->do_set_out_shndx(shndx
); }
144 // Set the address and file offset of this data, and finalize the
145 // size of the data. This is called during Layout::finalize for
146 // allocated sections.
148 set_address_and_file_offset(uint64_t addr
, off_t off
)
150 this->set_address(addr
);
151 this->set_file_offset(off
);
152 this->finalize_data_size();
157 set_address(uint64_t addr
)
159 gold_assert(!this->is_address_valid_
);
160 this->address_
= addr
;
161 this->is_address_valid_
= true;
164 // Set the file offset.
166 set_file_offset(off_t off
)
168 gold_assert(!this->is_offset_valid_
);
170 this->is_offset_valid_
= true;
173 // Finalize the data size.
177 if (!this->is_data_size_valid_
)
179 // Tell the child class to set the data size.
180 this->set_final_data_size();
181 gold_assert(this->is_data_size_valid_
);
185 // Set the TLS offset. Called only for SHT_TLS sections.
187 set_tls_offset(uint64_t tls_base
)
188 { this->do_set_tls_offset(tls_base
); }
190 // Return the TLS offset, relative to the base of the TLS segment.
191 // Valid only for SHT_TLS sections.
194 { return this->do_tls_offset(); }
196 // Write the data to the output file. This is called after
197 // Layout::finalize is complete.
199 write(Output_file
* file
)
200 { this->do_write(file
); }
202 // This is called by Layout::finalize to note that the sizes of
203 // allocated sections must now be fixed.
206 { Output_data::allocated_sizes_are_fixed
= true; }
208 // Used to check that layout has been done.
211 { return Output_data::allocated_sizes_are_fixed
; }
213 // Count the number of dynamic relocations applied to this section.
216 { ++this->dynamic_reloc_count_
; }
218 // Return the number of dynamic relocations applied to this section.
220 dynamic_reloc_count() const
221 { return this->dynamic_reloc_count_
; }
223 // Whether the address is valid.
225 is_address_valid() const
226 { return this->is_address_valid_
; }
228 // Whether the file offset is valid.
230 is_offset_valid() const
231 { return this->is_offset_valid_
; }
233 // Whether the data size is valid.
235 is_data_size_valid() const
236 { return this->is_data_size_valid_
; }
239 // Functions that child classes may or in some cases must implement.
241 // Write the data to the output file.
243 do_write(Output_file
*) = 0;
245 // Return the required alignment.
247 do_addralign() const = 0;
249 // Return whether this has a load address.
251 do_has_load_address() const
254 // Return the load address.
256 do_load_address() const
257 { gold_unreachable(); }
259 // Return whether this is an Output_section.
261 do_is_section() const
264 // Return whether this is an Output_section of the specified type.
265 // This only needs to be implement by Output_section.
267 do_is_section_type(elfcpp::Elf_Word
) const
270 // Return whether this is an Output_section with the specific flag
271 // set. This only needs to be implemented by Output_section.
273 do_is_section_flag_set(elfcpp::Elf_Xword
) const
276 // Return the output section index, if there is an output section.
279 { gold_unreachable(); }
281 // Set the output section index, if this is an output section.
283 do_set_out_shndx(unsigned int)
284 { gold_unreachable(); }
286 // This is a hook for derived classes to set the data size. This is
287 // called by finalize_data_size, normally called during
288 // Layout::finalize, when the section address is set.
290 set_final_data_size()
291 { gold_unreachable(); }
293 // A hook for resetting the address and file offset.
295 do_reset_address_and_file_offset()
298 // Set the TLS offset. Called only for SHT_TLS sections.
300 do_set_tls_offset(uint64_t)
301 { gold_unreachable(); }
303 // Return the TLS offset, relative to the base of the TLS segment.
304 // Valid only for SHT_TLS sections.
306 do_tls_offset() const
307 { gold_unreachable(); }
309 // Functions that child classes may call.
311 // Set the size of the data.
313 set_data_size(off_t data_size
)
315 gold_assert(!this->is_data_size_valid_
);
316 this->data_size_
= data_size
;
317 this->is_data_size_valid_
= true;
320 // Get the current data size--this is for the convenience of
321 // sections which build up their size over time.
323 current_data_size_for_child() const
324 { return this->data_size_
; }
326 // Set the current data size--this is for the convenience of
327 // sections which build up their size over time.
329 set_current_data_size_for_child(off_t data_size
)
331 gold_assert(!this->is_data_size_valid_
);
332 this->data_size_
= data_size
;
335 // Return default alignment for the target size.
339 // Return default alignment for a specified size--32 or 64.
341 default_alignment_for_size(int size
);
344 Output_data(const Output_data
&);
345 Output_data
& operator=(const Output_data
&);
347 // This is used for verification, to make sure that we don't try to
348 // change any sizes of allocated sections after we set the section
350 static bool allocated_sizes_are_fixed
;
352 // Memory address in output file.
354 // Size of data in output file.
356 // File offset of contents in output file.
358 // Whether address_ is valid.
359 bool is_address_valid_
;
360 // Whether data_size_ is valid.
361 bool is_data_size_valid_
;
362 // Whether offset_ is valid.
363 bool is_offset_valid_
;
364 // Count of dynamic relocations applied to this section.
365 unsigned int dynamic_reloc_count_
;
368 // Output the section headers.
370 class Output_section_headers
: public Output_data
373 Output_section_headers(const Layout
*,
374 const Layout::Segment_list
*,
375 const Layout::Section_list
*,
376 const Layout::Section_list
*,
380 // Write the data to the file.
382 do_write(Output_file
*);
384 // Return the required alignment.
387 { return Output_data::default_alignment(); }
390 // Write the data to the file with the right size and endianness.
391 template<int size
, bool big_endian
>
393 do_sized_write(Output_file
*);
395 const Layout
* layout_
;
396 const Layout::Segment_list
* segment_list_
;
397 const Layout::Section_list
* section_list_
;
398 const Layout::Section_list
* unattached_section_list_
;
399 const Stringpool
* secnamepool_
;
402 // Output the segment headers.
404 class Output_segment_headers
: public Output_data
407 Output_segment_headers(const Layout::Segment_list
& segment_list
);
410 // Write the data to the file.
412 do_write(Output_file
*);
414 // Return the required alignment.
417 { return Output_data::default_alignment(); }
420 // Write the data to the file with the right size and endianness.
421 template<int size
, bool big_endian
>
423 do_sized_write(Output_file
*);
425 const Layout::Segment_list
& segment_list_
;
428 // Output the ELF file header.
430 class Output_file_header
: public Output_data
433 Output_file_header(const Target
*,
435 const Output_segment_headers
*,
438 // Add information about the section headers. We lay out the ELF
439 // file header before we create the section headers.
440 void set_section_info(const Output_section_headers
*,
441 const Output_section
* shstrtab
);
444 // Write the data to the file.
446 do_write(Output_file
*);
448 // Return the required alignment.
451 { return Output_data::default_alignment(); }
454 // Write the data to the file with the right size and endianness.
455 template<int size
, bool big_endian
>
457 do_sized_write(Output_file
*);
459 // Return the value to use for the entry address.
461 typename
elfcpp::Elf_types
<size
>::Elf_Addr
464 const Target
* target_
;
465 const Symbol_table
* symtab_
;
466 const Output_segment_headers
* segment_header_
;
467 const Output_section_headers
* section_header_
;
468 const Output_section
* shstrtab_
;
472 // Output sections are mainly comprised of input sections. However,
473 // there are cases where we have data to write out which is not in an
474 // input section. Output_section_data is used in such cases. This is
475 // an abstract base class.
477 class Output_section_data
: public Output_data
480 Output_section_data(off_t data_size
, uint64_t addralign
)
481 : Output_data(), output_section_(NULL
), addralign_(addralign
)
482 { this->set_data_size(data_size
); }
484 Output_section_data(uint64_t addralign
)
485 : Output_data(), output_section_(NULL
), addralign_(addralign
)
488 // Return the output section.
489 const Output_section
*
490 output_section() const
491 { return this->output_section_
; }
493 // Record the output section.
495 set_output_section(Output_section
* os
);
497 // Add an input section, for SHF_MERGE sections. This returns true
498 // if the section was handled.
500 add_input_section(Relobj
* object
, unsigned int shndx
)
501 { return this->do_add_input_section(object
, shndx
); }
503 // Given an input OBJECT, an input section index SHNDX within that
504 // object, and an OFFSET relative to the start of that input
505 // section, return whether or not the corresponding offset within
506 // the output section is known. If this function returns true, it
507 // sets *POUTPUT to the output offset. The value -1 indicates that
508 // this input offset is being discarded.
510 output_offset(const Relobj
* object
, unsigned int shndx
,
511 section_offset_type offset
,
512 section_offset_type
*poutput
) const
513 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
515 // Return whether this is the merge section for the input section
516 // SHNDX in OBJECT. This should return true when output_offset
517 // would return true for some values of OFFSET.
519 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
520 { return this->do_is_merge_section_for(object
, shndx
); }
522 // Write the contents to a buffer. This is used for sections which
523 // require postprocessing, such as compression.
525 write_to_buffer(unsigned char* buffer
)
526 { this->do_write_to_buffer(buffer
); }
528 // Print merge stats to stderr. This should only be called for
529 // SHF_MERGE sections.
531 print_merge_stats(const char* section_name
)
532 { this->do_print_merge_stats(section_name
); }
535 // The child class must implement do_write.
537 // The child class may implement specific adjustments to the output
540 do_adjust_output_section(Output_section
*)
543 // May be implemented by child class. Return true if the section
546 do_add_input_section(Relobj
*, unsigned int)
547 { gold_unreachable(); }
549 // The child class may implement output_offset.
551 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
552 section_offset_type
*) const
555 // The child class may implement is_merge_section_for.
557 do_is_merge_section_for(const Relobj
*, unsigned int) const
560 // The child class may implement write_to_buffer. Most child
561 // classes can not appear in a compressed section, and they do not
564 do_write_to_buffer(unsigned char*)
565 { gold_unreachable(); }
567 // Print merge statistics.
569 do_print_merge_stats(const char*)
570 { gold_unreachable(); }
572 // Return the required alignment.
575 { return this->addralign_
; }
577 // Return the section index of the output section.
579 do_out_shndx() const;
581 // Set the alignment.
583 set_addralign(uint64_t addralign
)
584 { this->addralign_
= addralign
; }
587 // The output section for this section.
588 const Output_section
* output_section_
;
589 // The required alignment.
593 // Some Output_section_data classes build up their data step by step,
594 // rather than all at once. This class provides an interface for
597 class Output_section_data_build
: public Output_section_data
600 Output_section_data_build(uint64_t addralign
)
601 : Output_section_data(addralign
)
604 // Get the current data size.
606 current_data_size() const
607 { return this->current_data_size_for_child(); }
609 // Set the current data size.
611 set_current_data_size(off_t data_size
)
612 { this->set_current_data_size_for_child(data_size
); }
615 // Set the final data size.
617 set_final_data_size()
618 { this->set_data_size(this->current_data_size_for_child()); }
621 // A simple case of Output_data in which we have constant data to
624 class Output_data_const
: public Output_section_data
627 Output_data_const(const std::string
& data
, uint64_t addralign
)
628 : Output_section_data(data
.size(), addralign
), data_(data
)
631 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
632 : Output_section_data(len
, addralign
), data_(p
, len
)
635 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
636 : Output_section_data(len
, addralign
),
637 data_(reinterpret_cast<const char*>(p
), len
)
641 // Write the data to the output file.
643 do_write(Output_file
*);
645 // Write the data to a buffer.
647 do_write_to_buffer(unsigned char* buffer
)
648 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
654 // Another version of Output_data with constant data, in which the
655 // buffer is allocated by the caller.
657 class Output_data_const_buffer
: public Output_section_data
660 Output_data_const_buffer(const unsigned char* p
, off_t len
,
662 : Output_section_data(len
, addralign
), p_(p
)
666 // Write the data the output file.
668 do_write(Output_file
*);
670 // Write the data to a buffer.
672 do_write_to_buffer(unsigned char* buffer
)
673 { memcpy(buffer
, this->p_
, this->data_size()); }
676 const unsigned char* p_
;
679 // A place holder for a fixed amount of data written out via some
682 class Output_data_fixed_space
: public Output_section_data
685 Output_data_fixed_space(off_t data_size
, uint64_t addralign
)
686 : Output_section_data(data_size
, addralign
)
690 // Write out the data--the actual data must be written out
693 do_write(Output_file
*)
697 // A place holder for variable sized data written out via some other
700 class Output_data_space
: public Output_section_data_build
703 explicit Output_data_space(uint64_t addralign
)
704 : Output_section_data_build(addralign
)
707 // Set the alignment.
709 set_space_alignment(uint64_t align
)
710 { this->set_addralign(align
); }
713 // Write out the data--the actual data must be written out
716 do_write(Output_file
*)
720 // A string table which goes into an output section.
722 class Output_data_strtab
: public Output_section_data
725 Output_data_strtab(Stringpool
* strtab
)
726 : Output_section_data(1), strtab_(strtab
)
730 // This is called to set the address and file offset. Here we make
731 // sure that the Stringpool is finalized.
733 set_final_data_size();
735 // Write out the data.
737 do_write(Output_file
*);
739 // Write the data to a buffer.
741 do_write_to_buffer(unsigned char* buffer
)
742 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
748 // This POD class is used to represent a single reloc in the output
749 // file. This could be a private class within Output_data_reloc, but
750 // the templatization is complex enough that I broke it out into a
751 // separate class. The class is templatized on either elfcpp::SHT_REL
752 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
753 // relocation or an ordinary relocation.
755 // A relocation can be against a global symbol, a local symbol, an
756 // output section, or the undefined symbol at index 0. We represent
757 // the latter by using a NULL global symbol.
759 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
762 template<bool dynamic
, int size
, bool big_endian
>
763 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
766 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
768 // An uninitialized entry. We need this because we want to put
769 // instances of this class into an STL container.
771 : local_sym_index_(INVALID_CODE
)
774 // A reloc against a global symbol.
776 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
777 Address address
, bool is_relative
);
779 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
780 unsigned int shndx
, Address address
, bool is_relative
);
782 // A reloc against a local symbol.
784 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
785 unsigned int local_sym_index
, unsigned int type
,
786 Output_data
* od
, Address address
, bool is_relative
);
788 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
789 unsigned int local_sym_index
, unsigned int type
,
790 unsigned int shndx
, Address address
, bool is_relative
);
792 // A reloc against the STT_SECTION symbol of an output section.
794 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
797 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
798 unsigned int shndx
, Address address
);
800 // Return TRUE if this is a RELATIVE relocation.
803 { return this->is_relative_
; }
805 // Get the value of the symbol referred to by a Rel relocation.
808 symbol_value() const;
810 // Write the reloc entry to an output view.
812 write(unsigned char* pov
) const;
814 // Write the offset and info fields to Write_rel.
815 template<typename Write_rel
>
816 void write_rel(Write_rel
*) const;
819 // Return the symbol index. We can't do a double template
820 // specialization, so we do a secondary template here.
822 get_symbol_index() const;
824 // Codes for local_sym_index_.
831 // Invalid uninitialized entry.
837 // For a local symbol, the object. We will never generate a
838 // relocation against a local symbol in a dynamic object; that
839 // doesn't make sense. And our callers will always be
840 // templatized, so we use Sized_relobj here.
841 Sized_relobj
<size
, big_endian
>* relobj
;
842 // For a global symbol, the symbol. If this is NULL, it indicates
843 // a relocation against the undefined 0 symbol.
845 // For a relocation against an output section, the output section.
850 // If shndx_ is not INVALID CODE, the object which holds the input
851 // section being used to specify the reloc address.
853 // If shndx_ is INVALID_CODE, the output data being used to
854 // specify the reloc address. This may be NULL if the reloc
855 // address is absolute.
858 // The address offset within the input section or the Output_data.
860 // For a local symbol, the local symbol index. This is GSYM_CODE
861 // for a global symbol, or INVALID_CODE for an uninitialized value.
862 unsigned int local_sym_index_
;
863 // The reloc type--a processor specific code.
864 unsigned int type_
: 31;
865 // True if the relocation is a RELATIVE relocation.
866 bool is_relative_
: 1;
867 // If the reloc address is an input section in an object, the
868 // section index. This is INVALID_CODE if the reloc address is
869 // specified in some other way.
873 // The SHT_RELA version of Output_reloc<>. This is just derived from
874 // the SHT_REL version of Output_reloc, but it adds an addend.
876 template<bool dynamic
, int size
, bool big_endian
>
877 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
880 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
881 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
883 // An uninitialized entry.
888 // A reloc against a global symbol.
890 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
891 Address address
, Addend addend
, bool is_relative
)
892 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
895 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
896 unsigned int shndx
, Address address
, Addend addend
,
898 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
901 // A reloc against a local symbol.
903 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
904 unsigned int local_sym_index
, unsigned int type
,
905 Output_data
* od
, Address address
,
906 Addend addend
, bool is_relative
)
907 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
),
911 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
912 unsigned int local_sym_index
, unsigned int type
,
913 unsigned int shndx
, Address address
,
914 Addend addend
, bool is_relative
)
915 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
),
919 // A reloc against the STT_SECTION symbol of an output section.
921 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
922 Address address
, Addend addend
)
923 : rel_(os
, type
, od
, address
), addend_(addend
)
926 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
927 unsigned int shndx
, Address address
, Addend addend
)
928 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
931 // Write the reloc entry to an output view.
933 write(unsigned char* pov
) const;
937 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
942 // Output_data_reloc is used to manage a section containing relocs.
943 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
944 // indicates whether this is a dynamic relocation or a normal
945 // relocation. Output_data_reloc_base is a base class.
946 // Output_data_reloc is the real class, which we specialize based on
949 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
950 class Output_data_reloc_base
: public Output_section_data_build
953 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
954 typedef typename
Output_reloc_type::Address Address
;
955 static const int reloc_size
=
956 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
958 // Construct the section.
959 Output_data_reloc_base()
960 : Output_section_data_build(Output_data::default_alignment_for_size(size
))
964 // Write out the data.
966 do_write(Output_file
*);
968 // Set the entry size and the link.
970 do_adjust_output_section(Output_section
*os
);
972 // Add a relocation entry.
974 add(Output_data
*od
, const Output_reloc_type
& reloc
)
976 this->relocs_
.push_back(reloc
);
977 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
978 od
->add_dynamic_reloc();
982 typedef std::vector
<Output_reloc_type
> Relocs
;
987 // The class which callers actually create.
989 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
990 class Output_data_reloc
;
992 // The SHT_REL version of Output_data_reloc.
994 template<bool dynamic
, int size
, bool big_endian
>
995 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
996 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
999 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1003 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1004 typedef typename
Output_reloc_type::Address Address
;
1007 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
1010 // Add a reloc against a global symbol.
1013 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1014 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1017 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1018 unsigned int shndx
, Address address
)
1019 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1022 // Add a RELATIVE reloc against a global symbol. The final relocation
1023 // will not reference the symbol.
1026 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1028 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1031 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1032 Relobj
* relobj
, unsigned int shndx
, Address address
)
1033 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1036 // Add a reloc against a local symbol.
1039 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1040 unsigned int local_sym_index
, unsigned int type
,
1041 Output_data
* od
, Address address
)
1042 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1046 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1047 unsigned int local_sym_index
, unsigned int type
,
1048 Output_data
* od
, unsigned int shndx
, Address address
)
1049 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1052 // Add a RELATIVE reloc against a local symbol.
1055 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1056 unsigned int local_sym_index
, unsigned int type
,
1057 Output_data
* od
, Address address
)
1058 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1062 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1063 unsigned int local_sym_index
, unsigned int type
,
1064 Output_data
* od
, unsigned int shndx
, Address address
)
1065 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1068 // A reloc against the STT_SECTION symbol of an output section.
1069 // OS is the Output_section that the relocation refers to; OD is
1070 // the Output_data object being relocated.
1073 add_output_section(Output_section
* os
, unsigned int type
,
1074 Output_data
* od
, Address address
)
1075 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1078 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1079 Relobj
* relobj
, unsigned int shndx
, Address address
)
1080 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1083 // The SHT_RELA version of Output_data_reloc.
1085 template<bool dynamic
, int size
, bool big_endian
>
1086 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1087 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1090 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1094 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1095 typedef typename
Output_reloc_type::Address Address
;
1096 typedef typename
Output_reloc_type::Addend Addend
;
1099 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
1102 // Add a reloc against a global symbol.
1105 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1106 Address address
, Addend addend
)
1107 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1111 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1112 unsigned int shndx
, Address address
,
1114 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1117 // Add a RELATIVE reloc against a global symbol. The final output
1118 // relocation will not reference the symbol, but we must keep the symbol
1119 // information long enough to set the addend of the relocation correctly
1120 // when it is written.
1123 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1124 Address address
, Addend addend
)
1125 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1128 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1129 Relobj
* relobj
, unsigned int shndx
, Address address
,
1131 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1134 // Add a reloc against a local symbol.
1137 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1138 unsigned int local_sym_index
, unsigned int type
,
1139 Output_data
* od
, Address address
, Addend addend
)
1141 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1146 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1147 unsigned int local_sym_index
, unsigned int type
,
1148 Output_data
* od
, unsigned int shndx
, Address address
,
1151 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1152 address
, addend
, false));
1155 // Add a RELATIVE reloc against a local symbol.
1158 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1159 unsigned int local_sym_index
, unsigned int type
,
1160 Output_data
* od
, Address address
, Addend addend
)
1162 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1167 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1168 unsigned int local_sym_index
, unsigned int type
,
1169 Output_data
* od
, unsigned int shndx
, Address address
,
1172 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1173 address
, addend
, true));
1176 // A reloc against the STT_SECTION symbol of an output section.
1179 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1180 Address address
, Addend addend
)
1181 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1184 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1185 unsigned int shndx
, Address address
, Addend addend
)
1186 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1190 // Output_relocatable_relocs represents a relocation section in a
1191 // relocatable link. The actual data is written out in the target
1192 // hook relocate_for_relocatable. This just saves space for it.
1194 template<int sh_type
, int size
, bool big_endian
>
1195 class Output_relocatable_relocs
: public Output_section_data
1198 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1199 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1204 set_final_data_size();
1206 // Write out the data. There is nothing to do here.
1208 do_write(Output_file
*)
1212 // The relocs associated with this input section.
1213 Relocatable_relocs
* rr_
;
1216 // Handle a GROUP section.
1218 template<int size
, bool big_endian
>
1219 class Output_data_group
: public Output_section_data
1222 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1223 section_size_type entry_count
,
1224 const elfcpp::Elf_Word
* contents
);
1227 do_write(Output_file
*);
1230 // The input object.
1231 Sized_relobj
<size
, big_endian
>* relobj_
;
1232 // The group flag word.
1233 elfcpp::Elf_Word flags_
;
1234 // The section indexes of the input sections in this group.
1235 std::vector
<unsigned int> input_sections_
;
1238 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1239 // for one symbol--either a global symbol or a local symbol in an
1240 // object. The target specific code adds entries to the GOT as
1243 template<int size
, bool big_endian
>
1244 class Output_data_got
: public Output_section_data_build
1247 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1248 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1249 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1252 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1256 // Add an entry for a global symbol to the GOT. Return true if this
1257 // is a new GOT entry, false if the symbol was already in the GOT.
1259 add_global(Symbol
* gsym
);
1261 // Add an entry for a global symbol to the GOT, and add a dynamic
1262 // relocation of type R_TYPE for the GOT entry.
1264 add_global_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
, unsigned int r_type
);
1267 add_global_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
, unsigned int r_type
);
1269 // Add an entry for a local symbol to the GOT. This returns true if
1270 // this is a new GOT entry, false if the symbol already has a GOT
1273 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
1275 // Add an entry for a global symbol to the GOT, and add a dynamic
1276 // relocation of type R_TYPE for the GOT entry.
1278 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1279 unsigned int sym_index
, Rel_dyn
* rel_dyn
,
1280 unsigned int r_type
);
1283 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1284 unsigned int sym_index
, Rela_dyn
* rela_dyn
,
1285 unsigned int r_type
);
1287 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1288 // Return true if this is a new GOT entry, false if the symbol was
1289 // already in the GOT.
1291 add_global_tls(Symbol
* gsym
, bool need_pair
);
1293 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1294 // relocation of type R_TYPE.
1296 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1297 unsigned int r_type
);
1300 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1301 unsigned int r_type
);
1303 // Add a pair of entries for a global TLS symbol to the GOT, and add
1304 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1306 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1307 unsigned int mod_r_type
,
1308 unsigned int dtv_r_type
);
1311 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1312 unsigned int mod_r_type
,
1313 unsigned int dtv_r_type
);
1315 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1316 // This returns true if this is a new GOT entry, false if the symbol
1317 // already has a GOT entry.
1319 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
1320 unsigned int sym_index
, bool need_pair
);
1322 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1323 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1324 // Because this is a local symbol, the first GOT entry can be relocated
1325 // relative to a section symbol, and the second GOT entry will have an
1326 // dtv-relative value that can be computed at link time.
1328 add_local_tls_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1329 unsigned int sym_index
, unsigned int shndx
,
1330 bool need_pair
, Rel_dyn
* rel_dyn
,
1331 unsigned int r_type
);
1334 add_local_tls_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1335 unsigned int sym_index
, unsigned int shndx
,
1336 bool need_pair
, Rela_dyn
* rela_dyn
,
1337 unsigned int r_type
);
1339 // Add a constant to the GOT. This returns the offset of the new
1340 // entry from the start of the GOT.
1342 add_constant(Valtype constant
)
1344 this->entries_
.push_back(Got_entry(constant
));
1345 this->set_got_size();
1346 return this->last_got_offset();
1350 // Write out the GOT table.
1352 do_write(Output_file
*);
1355 // This POD class holds a single GOT entry.
1359 // Create a zero entry.
1361 : local_sym_index_(CONSTANT_CODE
)
1362 { this->u_
.constant
= 0; }
1364 // Create a global symbol entry.
1365 explicit Got_entry(Symbol
* gsym
)
1366 : local_sym_index_(GSYM_CODE
)
1367 { this->u_
.gsym
= gsym
; }
1369 // Create a local symbol entry.
1370 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1371 unsigned int local_sym_index
)
1372 : local_sym_index_(local_sym_index
)
1374 gold_assert(local_sym_index
!= GSYM_CODE
1375 && local_sym_index
!= CONSTANT_CODE
);
1376 this->u_
.object
= object
;
1379 // Create a constant entry. The constant is a host value--it will
1380 // be swapped, if necessary, when it is written out.
1381 explicit Got_entry(Valtype constant
)
1382 : local_sym_index_(CONSTANT_CODE
)
1383 { this->u_
.constant
= constant
; }
1385 // Write the GOT entry to an output view.
1387 write(unsigned char* pov
) const;
1398 // For a local symbol, the object.
1399 Sized_relobj
<size
, big_endian
>* object
;
1400 // For a global symbol, the symbol.
1402 // For a constant, the constant.
1405 // For a local symbol, the local symbol index. This is GSYM_CODE
1406 // for a global symbol, or CONSTANT_CODE for a constant.
1407 unsigned int local_sym_index_
;
1410 typedef std::vector
<Got_entry
> Got_entries
;
1412 // Return the offset into the GOT of GOT entry I.
1414 got_offset(unsigned int i
) const
1415 { return i
* (size
/ 8); }
1417 // Return the offset into the GOT of the last entry added.
1419 last_got_offset() const
1420 { return this->got_offset(this->entries_
.size() - 1); }
1422 // Set the size of the section.
1425 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1427 // The list of GOT entries.
1428 Got_entries entries_
;
1431 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1434 class Output_data_dynamic
: public Output_section_data
1437 Output_data_dynamic(Stringpool
* pool
)
1438 : Output_section_data(Output_data::default_alignment()),
1439 entries_(), pool_(pool
)
1442 // Add a new dynamic entry with a fixed numeric value.
1444 add_constant(elfcpp::DT tag
, unsigned int val
)
1445 { this->add_entry(Dynamic_entry(tag
, val
)); }
1447 // Add a new dynamic entry with the address of output data.
1449 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1450 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1452 // Add a new dynamic entry with the size of output data.
1454 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1455 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1457 // Add a new dynamic entry with the address of a symbol.
1459 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1460 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1462 // Add a new dynamic entry with a string.
1464 add_string(elfcpp::DT tag
, const char* str
)
1465 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1468 add_string(elfcpp::DT tag
, const std::string
& str
)
1469 { this->add_string(tag
, str
.c_str()); }
1472 // Adjust the output section to set the entry size.
1474 do_adjust_output_section(Output_section
*);
1476 // Set the final data size.
1478 set_final_data_size();
1480 // Write out the dynamic entries.
1482 do_write(Output_file
*);
1485 // This POD class holds a single dynamic entry.
1489 // Create an entry with a fixed numeric value.
1490 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1491 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1492 { this->u_
.val
= val
; }
1494 // Create an entry with the size or address of a section.
1495 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1497 classification_(section_size
1498 ? DYNAMIC_SECTION_SIZE
1499 : DYNAMIC_SECTION_ADDRESS
)
1500 { this->u_
.od
= od
; }
1502 // Create an entry with the address of a symbol.
1503 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1504 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1505 { this->u_
.sym
= sym
; }
1507 // Create an entry with a string.
1508 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1509 : tag_(tag
), classification_(DYNAMIC_STRING
)
1510 { this->u_
.str
= str
; }
1512 // Write the dynamic entry to an output view.
1513 template<int size
, bool big_endian
>
1515 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1523 DYNAMIC_SECTION_ADDRESS
,
1525 DYNAMIC_SECTION_SIZE
,
1534 // For DYNAMIC_NUMBER.
1536 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1537 const Output_data
* od
;
1538 // For DYNAMIC_SYMBOL.
1540 // For DYNAMIC_STRING.
1545 // The type of entry.
1546 Classification classification_
;
1549 // Add an entry to the list.
1551 add_entry(const Dynamic_entry
& entry
)
1552 { this->entries_
.push_back(entry
); }
1554 // Sized version of write function.
1555 template<int size
, bool big_endian
>
1557 sized_write(Output_file
* of
);
1559 // The type of the list of entries.
1560 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1563 Dynamic_entries entries_
;
1564 // The pool used for strings.
1568 // An output section. We don't expect to have too many output
1569 // sections, so we don't bother to do a template on the size.
1571 class Output_section
: public Output_data
1574 // Create an output section, giving the name, type, and flags.
1575 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1576 virtual ~Output_section();
1578 // Add a new input section SHNDX, named NAME, with header SHDR, from
1579 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1580 // which applies to this section, or 0 if none, or -1U if more than
1581 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1582 // in a linker script; in that case we need to keep track of input
1583 // sections associated with an output section. Return the offset
1584 // within the output section.
1585 template<int size
, bool big_endian
>
1587 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1589 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1590 unsigned int reloc_shndx
, bool have_sections_script
);
1592 // Add generated data POSD to this output section.
1594 add_output_section_data(Output_section_data
* posd
);
1596 // Return the section name.
1599 { return this->name_
; }
1601 // Return the section type.
1604 { return this->type_
; }
1606 // Return the section flags.
1609 { return this->flags_
; }
1611 // Return the entsize field.
1614 { return this->entsize_
; }
1616 // Set the entsize field.
1618 set_entsize(uint64_t v
);
1620 // Set the load address.
1622 set_load_address(uint64_t load_address
)
1624 this->load_address_
= load_address
;
1625 this->has_load_address_
= true;
1628 // Set the link field to the output section index of a section.
1630 set_link_section(const Output_data
* od
)
1632 gold_assert(this->link_
== 0
1633 && !this->should_link_to_symtab_
1634 && !this->should_link_to_dynsym_
);
1635 this->link_section_
= od
;
1638 // Set the link field to a constant.
1640 set_link(unsigned int v
)
1642 gold_assert(this->link_section_
== NULL
1643 && !this->should_link_to_symtab_
1644 && !this->should_link_to_dynsym_
);
1648 // Record that this section should link to the normal symbol table.
1650 set_should_link_to_symtab()
1652 gold_assert(this->link_section_
== NULL
1654 && !this->should_link_to_dynsym_
);
1655 this->should_link_to_symtab_
= true;
1658 // Record that this section should link to the dynamic symbol table.
1660 set_should_link_to_dynsym()
1662 gold_assert(this->link_section_
== NULL
1664 && !this->should_link_to_symtab_
);
1665 this->should_link_to_dynsym_
= true;
1668 // Return the info field.
1672 gold_assert(this->info_section_
== NULL
);
1676 // Set the info field to the output section index of a section.
1678 set_info_section(const Output_data
* od
)
1680 gold_assert(this->info_symndx_
== NULL
&& this->info_
== 0);
1681 this->info_section_
= od
;
1684 // Set the info field to the symbol table index of a symbol.
1686 set_info_symndx(const Symbol
* sym
)
1688 gold_assert(this->info_section_
== NULL
&& this->info_
== 0);
1689 this->info_symndx_
= sym
;
1692 // Set the info field to a constant.
1694 set_info(unsigned int v
)
1696 gold_assert(this->info_section_
== NULL
&& this->info_symndx_
== NULL
);
1700 // Set the addralign field.
1702 set_addralign(uint64_t v
)
1703 { this->addralign_
= v
; }
1705 // Indicate that we need a symtab index.
1707 set_needs_symtab_index()
1708 { this->needs_symtab_index_
= true; }
1710 // Return whether we need a symtab index.
1712 needs_symtab_index() const
1713 { return this->needs_symtab_index_
; }
1715 // Get the symtab index.
1717 symtab_index() const
1719 gold_assert(this->symtab_index_
!= 0);
1720 return this->symtab_index_
;
1723 // Set the symtab index.
1725 set_symtab_index(unsigned int index
)
1727 gold_assert(index
!= 0);
1728 this->symtab_index_
= index
;
1731 // Indicate that we need a dynsym index.
1733 set_needs_dynsym_index()
1734 { this->needs_dynsym_index_
= true; }
1736 // Return whether we need a dynsym index.
1738 needs_dynsym_index() const
1739 { return this->needs_dynsym_index_
; }
1741 // Get the dynsym index.
1743 dynsym_index() const
1745 gold_assert(this->dynsym_index_
!= 0);
1746 return this->dynsym_index_
;
1749 // Set the dynsym index.
1751 set_dynsym_index(unsigned int index
)
1753 gold_assert(index
!= 0);
1754 this->dynsym_index_
= index
;
1757 // Return whether this section should be written after all the input
1758 // sections are complete.
1760 after_input_sections() const
1761 { return this->after_input_sections_
; }
1763 // Record that this section should be written after all the input
1764 // sections are complete.
1766 set_after_input_sections()
1767 { this->after_input_sections_
= true; }
1769 // Return whether this section requires postprocessing after all
1770 // relocations have been applied.
1772 requires_postprocessing() const
1773 { return this->requires_postprocessing_
; }
1775 // If a section requires postprocessing, return the buffer to use.
1777 postprocessing_buffer() const
1779 gold_assert(this->postprocessing_buffer_
!= NULL
);
1780 return this->postprocessing_buffer_
;
1783 // If a section requires postprocessing, create the buffer to use.
1785 create_postprocessing_buffer();
1787 // If a section requires postprocessing, this is the size of the
1788 // buffer to which relocations should be applied.
1790 postprocessing_buffer_size() const
1791 { return this->current_data_size_for_child(); }
1793 // Return whether the offset OFFSET in the input section SHNDX in
1794 // object OBJECT is being included in the link.
1796 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1797 off_t offset
) const;
1799 // Return the offset within the output section of OFFSET relative to
1800 // the start of input section SHNDX in object OBJECT.
1802 output_offset(const Relobj
* object
, unsigned int shndx
,
1803 section_offset_type offset
) const;
1805 // Return the output virtual address of OFFSET relative to the start
1806 // of input section SHNDX in object OBJECT.
1808 output_address(const Relobj
* object
, unsigned int shndx
,
1809 off_t offset
) const;
1811 // Return the output address of the start of the merged section for
1812 // input section SHNDX in object OBJECT. This is not necessarily
1813 // the offset corresponding to input offset 0 in the section, since
1814 // the section may be mapped arbitrarily.
1816 starting_output_address(const Relobj
* object
, unsigned int shndx
) const;
1818 // Record that this output section was found in the SECTIONS clause
1819 // of a linker script.
1821 set_found_in_sections_clause()
1822 { this->found_in_sections_clause_
= true; }
1824 // Return whether this output section was found in the SECTIONS
1825 // clause of a linker script.
1827 found_in_sections_clause() const
1828 { return this->found_in_sections_clause_
; }
1830 // Write the section header into *OPHDR.
1831 template<int size
, bool big_endian
>
1833 write_header(const Layout
*, const Stringpool
*,
1834 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1836 // The next few calls are for linker script support.
1838 // Store the list of input sections for this Output_section into the
1839 // list passed in. This removes the input sections, leaving only
1840 // any Output_section_data elements. This returns the size of those
1841 // Output_section_data elements. ADDRESS is the address of this
1842 // output section. FILL is the fill value to use, in case there are
1843 // any spaces between the remaining Output_section_data elements.
1845 get_input_sections(uint64_t address
, const std::string
& fill
,
1846 std::list
<std::pair
<Relobj
*, unsigned int > >*);
1848 // Add an input section from a script.
1850 add_input_section_for_script(Relobj
* object
, unsigned int shndx
,
1851 off_t data_size
, uint64_t addralign
);
1853 // Set the current size of the output section.
1855 set_current_data_size(off_t size
)
1856 { this->set_current_data_size_for_child(size
); }
1858 // Get the current size of the output section.
1860 current_data_size() const
1861 { return this->current_data_size_for_child(); }
1863 // End of linker script support.
1865 // Print merge statistics to stderr.
1867 print_merge_stats();
1870 // Return the section index in the output file.
1872 do_out_shndx() const
1874 gold_assert(this->out_shndx_
!= -1U);
1875 return this->out_shndx_
;
1878 // Set the output section index.
1880 do_set_out_shndx(unsigned int shndx
)
1882 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
1883 this->out_shndx_
= shndx
;
1886 // Set the final data size of the Output_section. For a typical
1887 // Output_section, there is nothing to do, but if there are any
1888 // Output_section_data objects we need to set their final addresses
1891 set_final_data_size();
1893 // Reset the address and file offset.
1895 do_reset_address_and_file_offset();
1897 // Write the data to the file. For a typical Output_section, this
1898 // does nothing: the data is written out by calling Object::Relocate
1899 // on each input object. But if there are any Output_section_data
1900 // objects we do need to write them out here.
1902 do_write(Output_file
*);
1904 // Return the address alignment--function required by parent class.
1906 do_addralign() const
1907 { return this->addralign_
; }
1909 // Return whether there is a load address.
1911 do_has_load_address() const
1912 { return this->has_load_address_
; }
1914 // Return the load address.
1916 do_load_address() const
1918 gold_assert(this->has_load_address_
);
1919 return this->load_address_
;
1922 // Return whether this is an Output_section.
1924 do_is_section() const
1927 // Return whether this is a section of the specified type.
1929 do_is_section_type(elfcpp::Elf_Word type
) const
1930 { return this->type_
== type
; }
1932 // Return whether the specified section flag is set.
1934 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1935 { return (this->flags_
& flag
) != 0; }
1937 // Set the TLS offset. Called only for SHT_TLS sections.
1939 do_set_tls_offset(uint64_t tls_base
);
1941 // Return the TLS offset, relative to the base of the TLS segment.
1942 // Valid only for SHT_TLS sections.
1944 do_tls_offset() const
1945 { return this->tls_offset_
; }
1947 // Modify the section name. This is only permitted for an
1948 // unallocated section, and only before the size has been finalized.
1949 // Otherwise the name will not get into Layout::namepool_.
1951 set_name(const char* newname
)
1953 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
1954 gold_assert(!this->is_data_size_valid());
1955 this->name_
= newname
;
1958 // This may be implemented by a child class.
1960 do_finalize_name(Layout
*)
1963 // Record that this section requires postprocessing after all
1964 // relocations have been applied. This is called by a child class.
1966 set_requires_postprocessing()
1968 this->requires_postprocessing_
= true;
1969 this->after_input_sections_
= true;
1972 // Write all the data of an Output_section into the postprocessing
1975 write_to_postprocessing_buffer();
1978 // In some cases we need to keep a list of the input sections
1979 // associated with this output section. We only need the list if we
1980 // might have to change the offsets of the input section within the
1981 // output section after we add the input section. The ordinary
1982 // input sections will be written out when we process the object
1983 // file, and as such we don't need to track them here. We do need
1984 // to track Output_section_data objects here. We store instances of
1985 // this structure in a std::vector, so it must be a POD. There can
1986 // be many instances of this structure, so we use a union to save
1992 : shndx_(0), p2align_(0)
1994 this->u1_
.data_size
= 0;
1995 this->u2_
.object
= NULL
;
1998 // For an ordinary input section.
1999 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2002 p2align_(ffsll(static_cast<long long>(addralign
)))
2004 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2005 && shndx
!= MERGE_DATA_SECTION_CODE
2006 && shndx
!= MERGE_STRING_SECTION_CODE
);
2007 this->u1_
.data_size
= data_size
;
2008 this->u2_
.object
= object
;
2011 // For a non-merge output section.
2012 Input_section(Output_section_data
* posd
)
2013 : shndx_(OUTPUT_SECTION_CODE
),
2014 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2016 this->u1_
.data_size
= 0;
2017 this->u2_
.posd
= posd
;
2020 // For a merge section.
2021 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2023 ? MERGE_STRING_SECTION_CODE
2024 : MERGE_DATA_SECTION_CODE
),
2025 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
2027 this->u1_
.entsize
= entsize
;
2028 this->u2_
.posd
= posd
;
2031 // The required alignment.
2035 return (this->p2align_
== 0
2037 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2040 // Return the required size.
2044 // Whether this is an input section.
2046 is_input_section() const
2048 return (this->shndx_
!= OUTPUT_SECTION_CODE
2049 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2050 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
2053 // Return whether this is a merge section which matches the
2056 is_merge_section(bool is_string
, uint64_t entsize
,
2057 uint64_t addralign
) const
2059 return (this->shndx_
== (is_string
2060 ? MERGE_STRING_SECTION_CODE
2061 : MERGE_DATA_SECTION_CODE
)
2062 && this->u1_
.entsize
== entsize
2063 && this->addralign() == addralign
);
2066 // Return the object for an input section.
2070 gold_assert(this->is_input_section());
2071 return this->u2_
.object
;
2074 // Return the input section index for an input section.
2078 gold_assert(this->is_input_section());
2079 return this->shndx_
;
2082 // Set the output section.
2084 set_output_section(Output_section
* os
)
2086 gold_assert(!this->is_input_section());
2087 this->u2_
.posd
->set_output_section(os
);
2090 // Set the address and file offset. This is called during
2091 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2092 // the enclosing section.
2094 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2095 off_t section_file_offset
);
2097 // Reset the address and file offset.
2099 reset_address_and_file_offset();
2101 // Finalize the data size.
2103 finalize_data_size();
2105 // Add an input section, for SHF_MERGE sections.
2107 add_input_section(Relobj
* object
, unsigned int shndx
)
2109 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2110 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2111 return this->u2_
.posd
->add_input_section(object
, shndx
);
2114 // Given an input OBJECT, an input section index SHNDX within that
2115 // object, and an OFFSET relative to the start of that input
2116 // section, return whether or not the output offset is known. If
2117 // this function returns true, it sets *POUTPUT to the offset in
2118 // the output section, relative to the start of the input section
2119 // in the output section. *POUTPUT may be different from OFFSET
2120 // for a merged section.
2122 output_offset(const Relobj
* object
, unsigned int shndx
,
2123 section_offset_type offset
,
2124 section_offset_type
*poutput
) const;
2126 // Return whether this is the merge section for the input section
2129 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2131 // Write out the data. This does nothing for an input section.
2133 write(Output_file
*);
2135 // Write the data to a buffer. This does nothing for an input
2138 write_to_buffer(unsigned char*);
2140 // Print statistics about merge sections to stderr.
2142 print_merge_stats(const char* section_name
)
2144 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2145 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2146 this->u2_
.posd
->print_merge_stats(section_name
);
2150 // Code values which appear in shndx_. If the value is not one of
2151 // these codes, it is the input section index in the object file.
2154 // An Output_section_data.
2155 OUTPUT_SECTION_CODE
= -1U,
2156 // An Output_section_data for an SHF_MERGE section with
2157 // SHF_STRINGS not set.
2158 MERGE_DATA_SECTION_CODE
= -2U,
2159 // An Output_section_data for an SHF_MERGE section with
2161 MERGE_STRING_SECTION_CODE
= -3U
2164 // For an ordinary input section, this is the section index in the
2165 // input file. For an Output_section_data, this is
2166 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2167 // MERGE_STRING_SECTION_CODE.
2168 unsigned int shndx_
;
2169 // The required alignment, stored as a power of 2.
2170 unsigned int p2align_
;
2173 // For an ordinary input section, the section size.
2175 // For OUTPUT_SECTION_CODE, this is not used. For
2176 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2182 // For an ordinary input section, the object which holds the
2185 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2186 // MERGE_STRING_SECTION_CODE, the data.
2187 Output_section_data
* posd
;
2191 typedef std::vector
<Input_section
> Input_section_list
;
2193 // Fill data. This is used to fill in data between input sections.
2194 // It is also used for data statements (BYTE, WORD, etc.) in linker
2195 // scripts. When we have to keep track of the input sections, we
2196 // can use an Output_data_const, but we don't want to have to keep
2197 // track of input sections just to implement fills.
2201 Fill(off_t section_offset
, off_t length
)
2202 : section_offset_(section_offset
),
2203 length_(convert_to_section_size_type(length
))
2206 // Return section offset.
2208 section_offset() const
2209 { return this->section_offset_
; }
2211 // Return fill length.
2214 { return this->length_
; }
2217 // The offset within the output section.
2218 off_t section_offset_
;
2219 // The length of the space to fill.
2220 section_size_type length_
;
2223 typedef std::vector
<Fill
> Fill_list
;
2225 // Add a new output section by Input_section.
2227 add_output_section_data(Input_section
*);
2229 // Add an SHF_MERGE input section. Returns true if the section was
2232 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
2233 uint64_t entsize
, uint64_t addralign
);
2235 // Add an output SHF_MERGE section POSD to this output section.
2236 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2237 // ENTSIZE is the entity size. This returns the entry added to
2240 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
2243 // Most of these fields are only valid after layout.
2245 // The name of the section. This will point into a Stringpool.
2247 // The section address is in the parent class.
2248 // The section alignment.
2249 uint64_t addralign_
;
2250 // The section entry size.
2252 // The load address. This is only used when using a linker script
2253 // with a SECTIONS clause. The has_load_address_ field indicates
2254 // whether this field is valid.
2255 uint64_t load_address_
;
2256 // The file offset is in the parent class.
2257 // Set the section link field to the index of this section.
2258 const Output_data
* link_section_
;
2259 // If link_section_ is NULL, this is the link field.
2261 // Set the section info field to the index of this section.
2262 const Output_data
* info_section_
;
2263 // If info_section_ is NULL, set the info field to the symbol table
2264 // index of this symbol.
2265 const Symbol
* info_symndx_
;
2266 // If info_section_ and info_symndx_ are NULL, this is the section
2269 // The section type.
2270 const elfcpp::Elf_Word type_
;
2271 // The section flags.
2272 elfcpp::Elf_Xword flags_
;
2273 // The section index.
2274 unsigned int out_shndx_
;
2275 // If there is a STT_SECTION for this output section in the normal
2276 // symbol table, this is the symbol index. This starts out as zero.
2277 // It is initialized in Layout::finalize() to be the index, or -1U
2278 // if there isn't one.
2279 unsigned int symtab_index_
;
2280 // If there is a STT_SECTION for this output section in the dynamic
2281 // symbol table, this is the symbol index. This starts out as zero.
2282 // It is initialized in Layout::finalize() to be the index, or -1U
2283 // if there isn't one.
2284 unsigned int dynsym_index_
;
2285 // The input sections. This will be empty in cases where we don't
2286 // need to keep track of them.
2287 Input_section_list input_sections_
;
2288 // The offset of the first entry in input_sections_.
2289 off_t first_input_offset_
;
2290 // The fill data. This is separate from input_sections_ because we
2291 // often will need fill sections without needing to keep track of
2294 // If the section requires postprocessing, this buffer holds the
2295 // section contents during relocation.
2296 unsigned char* postprocessing_buffer_
;
2297 // Whether this output section needs a STT_SECTION symbol in the
2298 // normal symbol table. This will be true if there is a relocation
2300 bool needs_symtab_index_
: 1;
2301 // Whether this output section needs a STT_SECTION symbol in the
2302 // dynamic symbol table. This will be true if there is a dynamic
2303 // relocation which needs it.
2304 bool needs_dynsym_index_
: 1;
2305 // Whether the link field of this output section should point to the
2306 // normal symbol table.
2307 bool should_link_to_symtab_
: 1;
2308 // Whether the link field of this output section should point to the
2309 // dynamic symbol table.
2310 bool should_link_to_dynsym_
: 1;
2311 // Whether this section should be written after all the input
2312 // sections are complete.
2313 bool after_input_sections_
: 1;
2314 // Whether this section requires post processing after all
2315 // relocations have been applied.
2316 bool requires_postprocessing_
: 1;
2317 // Whether an input section was mapped to this output section
2318 // because of a SECTIONS clause in a linker script.
2319 bool found_in_sections_clause_
: 1;
2320 // Whether this section has an explicitly specified load address.
2321 bool has_load_address_
: 1;
2322 // For SHT_TLS sections, the offset of this section relative to the base
2323 // of the TLS segment.
2324 uint64_t tls_offset_
;
2327 // An output segment. PT_LOAD segments are built from collections of
2328 // output sections. Other segments typically point within PT_LOAD
2329 // segments, and are built directly as needed.
2331 class Output_segment
2334 // Create an output segment, specifying the type and flags.
2335 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2337 // Return the virtual address.
2340 { return this->vaddr_
; }
2342 // Return the physical address.
2345 { return this->paddr_
; }
2347 // Return the segment type.
2350 { return this->type_
; }
2352 // Return the segment flags.
2355 { return this->flags_
; }
2357 // Return the memory size.
2360 { return this->memsz_
; }
2362 // Return the file size.
2365 { return this->filesz_
; }
2367 // Return the maximum alignment of the Output_data.
2369 maximum_alignment();
2371 // Add an Output_section to this segment.
2373 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2374 { this->add_output_section(os
, seg_flags
, false); }
2376 // Add an Output_section to the start of this segment.
2378 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2379 { this->add_output_section(os
, seg_flags
, true); }
2381 // Add an Output_data (which is not an Output_section) to the start
2384 add_initial_output_data(Output_data
*);
2386 // Return the number of dynamic relocations applied to this segment.
2388 dynamic_reloc_count() const;
2390 // Return the address of the first section.
2392 first_section_load_address() const;
2394 // Return whether the addresses have been set already.
2396 are_addresses_set() const
2397 { return this->are_addresses_set_
; }
2399 // Set the addresses.
2401 set_addresses(uint64_t vaddr
, uint64_t paddr
)
2403 this->vaddr_
= vaddr
;
2404 this->paddr_
= paddr
;
2405 this->are_addresses_set_
= true;
2408 // Set the segment flags. This is only used if we have a PHDRS
2409 // clause which explicitly specifies the flags.
2411 set_flags(elfcpp::Elf_Word flags
)
2412 { this->flags_
= flags
; }
2414 // Set the address of the segment to ADDR and the offset to *POFF
2415 // and set the addresses and offsets of all contained output
2416 // sections accordingly. Set the section indexes of all contained
2417 // output sections starting with *PSHNDX. If RESET is true, first
2418 // reset the addresses of the contained sections. Return the
2419 // address of the immediately following segment. Update *POFF and
2420 // *PSHNDX. This should only be called for a PT_LOAD segment.
2422 set_section_addresses(bool reset
, uint64_t addr
, off_t
* poff
,
2423 unsigned int* pshndx
);
2425 // Set the minimum alignment of this segment. This may be adjusted
2426 // upward based on the section alignments.
2428 set_minimum_p_align(uint64_t align
)
2429 { this->min_p_align_
= align
; }
2431 // Set the offset of this segment based on the section. This should
2432 // only be called for a non-PT_LOAD segment.
2436 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2440 // Return the number of output sections.
2442 output_section_count() const;
2444 // Return the section attached to the list segment with the lowest
2445 // load address. This is used when handling a PHDRS clause in a
2448 section_with_lowest_load_address() const;
2450 // Write the segment header into *OPHDR.
2451 template<int size
, bool big_endian
>
2453 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2455 // Write the section headers of associated sections into V.
2456 template<int size
, bool big_endian
>
2458 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2459 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
2462 Output_segment(const Output_segment
&);
2463 Output_segment
& operator=(const Output_segment
&);
2465 typedef std::list
<Output_data
*> Output_data_list
;
2467 // Add an Output_section to this segment, specifying front or back.
2469 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2472 // Find the maximum alignment in an Output_data_list.
2474 maximum_alignment_list(const Output_data_list
*);
2476 // Set the section addresses in an Output_data_list.
2478 set_section_list_addresses(bool reset
, Output_data_list
*, uint64_t addr
,
2479 off_t
* poff
, unsigned int* pshndx
);
2481 // Return the number of Output_sections in an Output_data_list.
2483 output_section_count_list(const Output_data_list
*) const;
2485 // Return the number of dynamic relocs in an Output_data_list.
2487 dynamic_reloc_count_list(const Output_data_list
*) const;
2489 // Find the section with the lowest load address in an
2490 // Output_data_list.
2492 lowest_load_address_in_list(const Output_data_list
* pdl
,
2493 Output_section
** found
,
2494 uint64_t* found_lma
) const;
2496 // Write the section headers in the list into V.
2497 template<int size
, bool big_endian
>
2499 write_section_headers_list(const Layout
*, const Stringpool
*,
2500 const Output_data_list
*, unsigned char* v
,
2501 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
2503 // The list of output data with contents attached to this segment.
2504 Output_data_list output_data_
;
2505 // The list of output data without contents attached to this segment.
2506 Output_data_list output_bss_
;
2507 // The segment virtual address.
2509 // The segment physical address.
2511 // The size of the segment in memory.
2513 // The maximum section alignment. The is_max_align_known_ field
2514 // indicates whether this has been finalized.
2515 uint64_t max_align_
;
2516 // The required minimum value for the p_align field. This is used
2517 // for PT_LOAD segments. Note that this does not mean that
2518 // addresses should be aligned to this value; it means the p_paddr
2519 // and p_vaddr fields must be congruent modulo this value. For
2520 // non-PT_LOAD segments, the dynamic linker works more efficiently
2521 // if the p_align field has the more conventional value, although it
2522 // can align as needed.
2523 uint64_t min_p_align_
;
2524 // The offset of the segment data within the file.
2526 // The size of the segment data in the file.
2528 // The segment type;
2529 elfcpp::Elf_Word type_
;
2530 // The segment flags.
2531 elfcpp::Elf_Word flags_
;
2532 // Whether we have finalized max_align_.
2533 bool is_max_align_known_
: 1;
2534 // Whether vaddr and paddr were set by a linker script.
2535 bool are_addresses_set_
: 1;
2538 // This class represents the output file.
2543 Output_file(const char* name
);
2545 // Open the output file. FILE_SIZE is the final size of the file.
2547 open(off_t file_size
);
2549 // Resize the output file.
2551 resize(off_t file_size
);
2553 // Close the output file (flushing all buffered data) and make sure
2554 // there are no errors.
2558 // We currently always use mmap which makes the view handling quite
2559 // simple. In the future we may support other approaches.
2561 // Write data to the output file.
2563 write(off_t offset
, const void* data
, size_t len
)
2564 { memcpy(this->base_
+ offset
, data
, len
); }
2566 // Get a buffer to use to write to the file, given the offset into
2567 // the file and the size.
2569 get_output_view(off_t start
, size_t size
)
2571 gold_assert(start
>= 0
2572 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
2573 return this->base_
+ start
;
2576 // VIEW must have been returned by get_output_view. Write the
2577 // buffer to the file, passing in the offset and the size.
2579 write_output_view(off_t
, size_t, unsigned char*)
2582 // Get a read/write buffer. This is used when we want to write part
2583 // of the file, read it in, and write it again.
2585 get_input_output_view(off_t start
, size_t size
)
2586 { return this->get_output_view(start
, size
); }
2588 // Write a read/write buffer back to the file.
2590 write_input_output_view(off_t
, size_t, unsigned char*)
2593 // Get a read buffer. This is used when we just want to read part
2594 // of the file back it in.
2595 const unsigned char*
2596 get_input_view(off_t start
, size_t size
)
2597 { return this->get_output_view(start
, size
); }
2599 // Release a read bfufer.
2601 free_input_view(off_t
, size_t, const unsigned char*)
2605 // Map the file into memory and return a pointer to the map.
2609 // Unmap the file from memory (and flush to disk buffers).
2619 // Base of file mapped into memory.
2620 unsigned char* base_
;
2621 // True iff base_ points to a memory buffer rather than an output file.
2622 bool map_is_anonymous_
;
2625 } // End namespace gold.
2627 #endif // !defined(GOLD_OUTPUT_H)