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
;
42 template<int size
, bool big_endian
>
44 template<int size
, bool big_endian
>
47 // An abtract class for data which has to go into the output file.
52 explicit Output_data()
53 : address_(0), data_size_(0), offset_(-1),
54 is_address_valid_(false), is_data_size_valid_(false),
55 is_offset_valid_(false),
56 dynamic_reloc_count_(0)
62 // Return the address. For allocated sections, this is only valid
63 // after Layout::finalize is finished.
67 gold_assert(this->is_address_valid_
);
68 return this->address_
;
71 // Return the size of the data. For allocated sections, this must
72 // be valid after Layout::finalize calls set_address, but need not
73 // be valid before then.
77 gold_assert(this->is_data_size_valid_
);
78 return this->data_size_
;
81 // Return the file offset. This is only valid after
82 // Layout::finalize is finished. For some non-allocated sections,
83 // it may not be valid until near the end of the link.
87 gold_assert(this->is_offset_valid_
);
91 // Return the required alignment.
94 { return this->do_addralign(); }
96 // Return whether this is an Output_section.
99 { return this->do_is_section(); }
101 // Return whether this is an Output_section of the specified type.
103 is_section_type(elfcpp::Elf_Word stt
) const
104 { return this->do_is_section_type(stt
); }
106 // Return whether this is an Output_section with the specified flag
109 is_section_flag_set(elfcpp::Elf_Xword shf
) const
110 { return this->do_is_section_flag_set(shf
); }
112 // Return the output section index, if there is an output section.
115 { return this->do_out_shndx(); }
117 // Set the output section index, if this is an output section.
119 set_out_shndx(unsigned int shndx
)
120 { this->do_set_out_shndx(shndx
); }
122 // Set the address and file offset of this data, and finalize the
123 // size of the data. This is called during Layout::finalize for
124 // allocated sections.
126 set_address_and_file_offset(uint64_t addr
, off_t off
)
128 this->set_address(addr
);
129 this->set_file_offset(off
);
130 this->finalize_data_size();
135 set_address(uint64_t addr
)
137 gold_assert(!this->is_address_valid_
);
138 this->address_
= addr
;
139 this->is_address_valid_
= true;
142 // Set the file offset.
144 set_file_offset(off_t off
)
146 gold_assert(!this->is_offset_valid_
);
148 this->is_offset_valid_
= true;
151 // Finalize the data size.
155 if (!this->is_data_size_valid_
)
157 // Tell the child class to set the data size.
158 this->set_final_data_size();
159 gold_assert(this->is_data_size_valid_
);
163 // Set the TLS offset. Called only for SHT_TLS sections.
165 set_tls_offset(uint64_t tls_base
)
166 { this->do_set_tls_offset(tls_base
); }
168 // Return the TLS offset, relative to the base of the TLS segment.
169 // Valid only for SHT_TLS sections.
172 { return this->do_tls_offset(); }
174 // Write the data to the output file. This is called after
175 // Layout::finalize is complete.
177 write(Output_file
* file
)
178 { this->do_write(file
); }
180 // This is called by Layout::finalize to note that the sizes of
181 // allocated sections must now be fixed.
184 { Output_data::allocated_sizes_are_fixed
= true; }
186 // Used to check that layout has been done.
189 { return Output_data::allocated_sizes_are_fixed
; }
191 // Count the number of dynamic relocations applied to this section.
194 { ++this->dynamic_reloc_count_
; }
196 // Return the number of dynamic relocations applied to this section.
198 dynamic_reloc_count() const
199 { return this->dynamic_reloc_count_
; }
202 // Functions that child classes may or in some cases must implement.
204 // Write the data to the output file.
206 do_write(Output_file
*) = 0;
208 // Return the required alignment.
210 do_addralign() const = 0;
212 // Return whether this is an Output_section.
214 do_is_section() const
217 // Return whether this is an Output_section of the specified type.
218 // This only needs to be implement by Output_section.
220 do_is_section_type(elfcpp::Elf_Word
) const
223 // Return whether this is an Output_section with the specific flag
224 // set. This only needs to be implemented by Output_section.
226 do_is_section_flag_set(elfcpp::Elf_Xword
) const
229 // Return the output section index, if there is an output section.
232 { gold_unreachable(); }
234 // Set the output section index, if this is an output section.
236 do_set_out_shndx(unsigned int)
237 { gold_unreachable(); }
239 // This is a hook for derived classes to set the data size. This is
240 // called by finalize_data_size, normally called during
241 // Layout::finalize, when the section address is set.
243 set_final_data_size()
244 { gold_unreachable(); }
246 // Set the TLS offset. Called only for SHT_TLS sections.
248 do_set_tls_offset(uint64_t)
249 { gold_unreachable(); }
251 // Return the TLS offset, relative to the base of the TLS segment.
252 // Valid only for SHT_TLS sections.
254 do_tls_offset() const
255 { gold_unreachable(); }
257 // Functions that child classes may call.
259 // Whether the address is valid.
261 is_address_valid() const
262 { return this->is_address_valid_
; }
264 // Whether the file offset is valid.
266 is_offset_valid() const
267 { return this->is_offset_valid_
; }
269 // Whether the data size is valid.
271 is_data_size_valid() const
272 { return this->is_data_size_valid_
; }
274 // Set the size of the data.
276 set_data_size(off_t data_size
)
278 gold_assert(!this->is_data_size_valid_
);
279 this->data_size_
= data_size
;
280 this->is_data_size_valid_
= true;
283 // Get the current data size--this is for the convenience of
284 // sections which build up their size over time.
286 current_data_size_for_child() const
287 { return this->data_size_
; }
289 // Set the current data size--this is for the convenience of
290 // sections which build up their size over time.
292 set_current_data_size_for_child(off_t data_size
)
294 gold_assert(!this->is_data_size_valid_
);
295 this->data_size_
= data_size
;
298 // Return default alignment for the target size.
302 // Return default alignment for a specified size--32 or 64.
304 default_alignment_for_size(int size
);
307 Output_data(const Output_data
&);
308 Output_data
& operator=(const Output_data
&);
310 // This is used for verification, to make sure that we don't try to
311 // change any sizes of allocated sections after we set the section
313 static bool allocated_sizes_are_fixed
;
315 // Memory address in output file.
317 // Size of data in output file.
319 // File offset of contents in output file.
321 // Whether address_ is valid.
322 bool is_address_valid_
;
323 // Whether data_size_ is valid.
324 bool is_data_size_valid_
;
325 // Whether offset_ is valid.
326 bool is_offset_valid_
;
327 // Count of dynamic relocations applied to this section.
328 unsigned int dynamic_reloc_count_
;
331 // Output the section headers.
333 class Output_section_headers
: public Output_data
336 Output_section_headers(const Layout
*,
337 const Layout::Segment_list
*,
338 const Layout::Section_list
*,
342 // Write the data to the file.
344 do_write(Output_file
*);
346 // Return the required alignment.
349 { return Output_data::default_alignment(); }
352 // Write the data to the file with the right size and endianness.
353 template<int size
, bool big_endian
>
355 do_sized_write(Output_file
*);
357 const Layout
* layout_
;
358 const Layout::Segment_list
* segment_list_
;
359 const Layout::Section_list
* unattached_section_list_
;
360 const Stringpool
* secnamepool_
;
363 // Output the segment headers.
365 class Output_segment_headers
: public Output_data
368 Output_segment_headers(const Layout::Segment_list
& segment_list
);
371 // Write the data to the file.
373 do_write(Output_file
*);
375 // Return the required alignment.
378 { return Output_data::default_alignment(); }
381 // Write the data to the file with the right size and endianness.
382 template<int size
, bool big_endian
>
384 do_sized_write(Output_file
*);
386 const Layout::Segment_list
& segment_list_
;
389 // Output the ELF file header.
391 class Output_file_header
: public Output_data
394 Output_file_header(const Target
*,
396 const Output_segment_headers
*);
398 // Add information about the section headers. We lay out the ELF
399 // file header before we create the section headers.
400 void set_section_info(const Output_section_headers
*,
401 const Output_section
* shstrtab
);
404 // Write the data to the file.
406 do_write(Output_file
*);
408 // Return the required alignment.
411 { return Output_data::default_alignment(); }
414 // Write the data to the file with the right size and endianness.
415 template<int size
, bool big_endian
>
417 do_sized_write(Output_file
*);
419 const Target
* target_
;
420 const Symbol_table
* symtab_
;
421 const Output_segment_headers
* segment_header_
;
422 const Output_section_headers
* section_header_
;
423 const Output_section
* shstrtab_
;
426 // Output sections are mainly comprised of input sections. However,
427 // there are cases where we have data to write out which is not in an
428 // input section. Output_section_data is used in such cases. This is
429 // an abstract base class.
431 class Output_section_data
: public Output_data
434 Output_section_data(off_t data_size
, uint64_t addralign
)
435 : Output_data(), output_section_(NULL
), addralign_(addralign
)
436 { this->set_data_size(data_size
); }
438 Output_section_data(uint64_t addralign
)
439 : Output_data(), output_section_(NULL
), addralign_(addralign
)
442 // Return the output section.
443 const Output_section
*
444 output_section() const
445 { return this->output_section_
; }
447 // Record the output section.
449 set_output_section(Output_section
* os
);
451 // Add an input section, for SHF_MERGE sections. This returns true
452 // if the section was handled.
454 add_input_section(Relobj
* object
, unsigned int shndx
)
455 { return this->do_add_input_section(object
, shndx
); }
457 // Given an input OBJECT, an input section index SHNDX within that
458 // object, and an OFFSET relative to the start of that input
459 // section, return whether or not the corresponding offset within
460 // the output section is known. If this function returns true, it
461 // sets *POUTPUT to the output offset. The value -1 indicates that
462 // this input offset is being discarded.
464 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
465 off_t
*poutput
) const
466 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
468 // Write the contents to a buffer. This is used for sections which
469 // require postprocessing, such as compression.
471 write_to_buffer(unsigned char* buffer
)
472 { this->do_write_to_buffer(buffer
); }
475 // The child class must implement do_write.
477 // The child class may implement specific adjustments to the output
480 do_adjust_output_section(Output_section
*)
483 // May be implemented by child class. Return true if the section
486 do_add_input_section(Relobj
*, unsigned int)
487 { gold_unreachable(); }
489 // The child class may implement output_offset.
491 do_output_offset(const Relobj
*, unsigned int, off_t
, off_t
*) const
494 // The child class may implement write_to_buffer. Most child
495 // classes can not appear in a compressed section, and they do not
498 do_write_to_buffer(unsigned char*)
499 { gold_unreachable(); }
501 // Return the required alignment.
504 { return this->addralign_
; }
506 // Return the section index of the output section.
508 do_out_shndx() const;
510 // Set the alignment.
512 set_addralign(uint64_t addralign
)
513 { this->addralign_
= addralign
; }
516 // The output section for this section.
517 const Output_section
* output_section_
;
518 // The required alignment.
522 // Some Output_section_data classes build up their data step by step,
523 // rather than all at once. This class provides an interface for
526 class Output_section_data_build
: public Output_section_data
529 Output_section_data_build(uint64_t addralign
)
530 : Output_section_data(addralign
)
533 // Get the current data size.
535 current_data_size() const
536 { return this->current_data_size_for_child(); }
538 // Set the current data size.
540 set_current_data_size(off_t data_size
)
541 { this->set_current_data_size_for_child(data_size
); }
544 // Set the final data size.
546 set_final_data_size()
547 { this->set_data_size(this->current_data_size_for_child()); }
550 // A simple case of Output_data in which we have constant data to
553 class Output_data_const
: public Output_section_data
556 Output_data_const(const std::string
& data
, uint64_t addralign
)
557 : Output_section_data(data
.size(), addralign
), data_(data
)
560 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
561 : Output_section_data(len
, addralign
), data_(p
, len
)
564 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
565 : Output_section_data(len
, addralign
),
566 data_(reinterpret_cast<const char*>(p
), len
)
570 // Write the data to the output file.
572 do_write(Output_file
*);
574 // Write the data to a buffer.
576 do_write_to_buffer(unsigned char* buffer
)
577 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
583 // Another version of Output_data with constant data, in which the
584 // buffer is allocated by the caller.
586 class Output_data_const_buffer
: public Output_section_data
589 Output_data_const_buffer(const unsigned char* p
, off_t len
,
591 : Output_section_data(len
, addralign
), p_(p
)
595 // Write the data the output file.
597 do_write(Output_file
*);
599 // Write the data to a buffer.
601 do_write_to_buffer(unsigned char* buffer
)
602 { memcpy(buffer
, this->p_
, this->data_size()); }
605 const unsigned char* p_
;
608 // A place holder for a fixed amount of data written out via some
611 class Output_data_fixed_space
: public Output_section_data
614 Output_data_fixed_space(off_t data_size
, uint64_t addralign
)
615 : Output_section_data(data_size
, addralign
)
619 // Write out the data--the actual data must be written out
622 do_write(Output_file
*)
626 // A place holder for variable sized data written out via some other
629 class Output_data_space
: public Output_section_data_build
632 explicit Output_data_space(uint64_t addralign
)
633 : Output_section_data_build(addralign
)
636 // Set the alignment.
638 set_space_alignment(uint64_t align
)
639 { this->set_addralign(align
); }
642 // Write out the data--the actual data must be written out
645 do_write(Output_file
*)
649 // A string table which goes into an output section.
651 class Output_data_strtab
: public Output_section_data
654 Output_data_strtab(Stringpool
* strtab
)
655 : Output_section_data(1), strtab_(strtab
)
659 // This is called to set the address and file offset. Here we make
660 // sure that the Stringpool is finalized.
662 set_final_data_size();
664 // Write out the data.
666 do_write(Output_file
*);
668 // Write the data to a buffer.
670 do_write_to_buffer(unsigned char* buffer
)
671 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
677 // This POD class is used to represent a single reloc in the output
678 // file. This could be a private class within Output_data_reloc, but
679 // the templatization is complex enough that I broke it out into a
680 // separate class. The class is templatized on either elfcpp::SHT_REL
681 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
682 // relocation or an ordinary relocation.
684 // A relocation can be against a global symbol, a local symbol, an
685 // output section, or the undefined symbol at index 0. We represent
686 // the latter by using a NULL global symbol.
688 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
691 template<bool dynamic
, int size
, bool big_endian
>
692 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
695 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
697 // An uninitialized entry. We need this because we want to put
698 // instances of this class into an STL container.
700 : local_sym_index_(INVALID_CODE
)
703 // A reloc against a global symbol.
705 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
706 Address address
, bool is_relative
);
708 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
709 unsigned int shndx
, Address address
, bool is_relative
);
711 // A reloc against a local symbol.
713 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
714 unsigned int local_sym_index
, unsigned int type
,
715 Output_data
* od
, Address address
, bool is_relative
);
717 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
718 unsigned int local_sym_index
, unsigned int type
,
719 unsigned int shndx
, Address address
, bool is_relative
);
721 // A reloc against the STT_SECTION symbol of an output section.
723 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
726 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
727 unsigned int shndx
, Address address
);
729 // Return TRUE if this is a RELATIVE relocation.
732 { return this->is_relative_
; }
734 // Get the value of the symbol referred to by a Rel relocation.
737 symbol_value() const;
739 // Write the reloc entry to an output view.
741 write(unsigned char* pov
) const;
743 // Write the offset and info fields to Write_rel.
744 template<typename Write_rel
>
745 void write_rel(Write_rel
*) const;
748 // Return the symbol index. We can't do a double template
749 // specialization, so we do a secondary template here.
751 get_symbol_index() const;
753 // Codes for local_sym_index_.
760 // Invalid uninitialized entry.
766 // For a local symbol, the object. We will never generate a
767 // relocation against a local symbol in a dynamic object; that
768 // doesn't make sense. And our callers will always be
769 // templatized, so we use Sized_relobj here.
770 Sized_relobj
<size
, big_endian
>* relobj
;
771 // For a global symbol, the symbol. If this is NULL, it indicates
772 // a relocation against the undefined 0 symbol.
774 // For a relocation against an output section, the output section.
779 // If shndx_ is not INVALID CODE, the object which holds the input
780 // section being used to specify the reloc address.
782 // If shndx_ is INVALID_CODE, the output data being used to
783 // specify the reloc address. This may be NULL if the reloc
784 // address is absolute.
787 // The address offset within the input section or the Output_data.
789 // For a local symbol, the local symbol index. This is GSYM_CODE
790 // for a global symbol, or INVALID_CODE for an uninitialized value.
791 unsigned int local_sym_index_
;
792 // The reloc type--a processor specific code.
793 unsigned int type_
: 31;
794 // True if the relocation is a RELATIVE relocation.
795 bool is_relative_
: 1;
796 // If the reloc address is an input section in an object, the
797 // section index. This is INVALID_CODE if the reloc address is
798 // specified in some other way.
802 // The SHT_RELA version of Output_reloc<>. This is just derived from
803 // the SHT_REL version of Output_reloc, but it adds an addend.
805 template<bool dynamic
, int size
, bool big_endian
>
806 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
809 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
810 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
812 // An uninitialized entry.
817 // A reloc against a global symbol.
819 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
820 Address address
, Addend addend
, bool is_relative
)
821 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
824 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
825 unsigned int shndx
, Address address
, Addend addend
,
827 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
830 // A reloc against a local symbol.
832 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
833 unsigned int local_sym_index
, unsigned int type
,
834 Output_data
* od
, Address address
,
835 Addend addend
, bool is_relative
)
836 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
),
840 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
841 unsigned int local_sym_index
, unsigned int type
,
842 unsigned int shndx
, Address address
,
843 Addend addend
, bool is_relative
)
844 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
),
848 // A reloc against the STT_SECTION symbol of an output section.
850 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
851 Address address
, Addend addend
)
852 : rel_(os
, type
, od
, address
), addend_(addend
)
855 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
856 unsigned int shndx
, Address address
, Addend addend
)
857 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
860 // Write the reloc entry to an output view.
862 write(unsigned char* pov
) const;
866 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
871 // Output_data_reloc is used to manage a section containing relocs.
872 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
873 // indicates whether this is a dynamic relocation or a normal
874 // relocation. Output_data_reloc_base is a base class.
875 // Output_data_reloc is the real class, which we specialize based on
878 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
879 class Output_data_reloc_base
: public Output_section_data_build
882 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
883 typedef typename
Output_reloc_type::Address Address
;
884 static const int reloc_size
=
885 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
887 // Construct the section.
888 Output_data_reloc_base()
889 : Output_section_data_build(Output_data::default_alignment_for_size(size
))
893 // Write out the data.
895 do_write(Output_file
*);
897 // Set the entry size and the link.
899 do_adjust_output_section(Output_section
*os
);
901 // Add a relocation entry.
903 add(Output_data
*od
, const Output_reloc_type
& reloc
)
905 this->relocs_
.push_back(reloc
);
906 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
907 od
->add_dynamic_reloc();
911 typedef std::vector
<Output_reloc_type
> Relocs
;
916 // The class which callers actually create.
918 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
919 class Output_data_reloc
;
921 // The SHT_REL version of Output_data_reloc.
923 template<bool dynamic
, int size
, bool big_endian
>
924 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
925 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
928 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
932 typedef typename
Base::Output_reloc_type Output_reloc_type
;
933 typedef typename
Output_reloc_type::Address Address
;
936 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
939 // Add a reloc against a global symbol.
942 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
943 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
946 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
947 unsigned int shndx
, Address address
)
948 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
951 // Add a RELATIVE reloc against a global symbol. The final relocation
952 // will not reference the symbol.
955 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
957 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
960 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
961 Relobj
* relobj
, unsigned int shndx
, Address address
)
962 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
965 // Add a reloc against a local symbol.
968 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
969 unsigned int local_sym_index
, unsigned int type
,
970 Output_data
* od
, Address address
)
971 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
975 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
976 unsigned int local_sym_index
, unsigned int type
,
977 Output_data
* od
, unsigned int shndx
, Address address
)
978 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
981 // Add a RELATIVE reloc against a local symbol.
984 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
985 unsigned int local_sym_index
, unsigned int type
,
986 Output_data
* od
, Address address
)
987 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
991 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
992 unsigned int local_sym_index
, unsigned int type
,
993 Output_data
* od
, unsigned int shndx
, Address address
)
994 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
997 // A reloc against the STT_SECTION symbol of an output section.
998 // OS is the Output_section that the relocation refers to; OD is
999 // the Output_data object being relocated.
1002 add_output_section(Output_section
* os
, unsigned int type
,
1003 Output_data
* od
, Address address
)
1004 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1007 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1008 Relobj
* relobj
, unsigned int shndx
, Address address
)
1009 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1012 // The SHT_RELA version of Output_data_reloc.
1014 template<bool dynamic
, int size
, bool big_endian
>
1015 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1016 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1019 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1023 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1024 typedef typename
Output_reloc_type::Address Address
;
1025 typedef typename
Output_reloc_type::Addend Addend
;
1028 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
1031 // Add a reloc against a global symbol.
1034 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1035 Address address
, Addend addend
)
1036 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1040 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
1041 unsigned int shndx
, Address address
,
1043 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1046 // Add a RELATIVE reloc against a global symbol. The final output
1047 // relocation will not reference the symbol, but we must keep the symbol
1048 // information long enough to set the addend of the relocation correctly
1049 // when it is written.
1052 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1053 Address address
, Addend addend
)
1054 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1057 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1058 Relobj
* relobj
, unsigned int shndx
, Address address
,
1060 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1063 // Add a reloc against a local symbol.
1066 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1067 unsigned int local_sym_index
, unsigned int type
,
1068 Output_data
* od
, Address address
, Addend addend
)
1070 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1075 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1076 unsigned int local_sym_index
, unsigned int type
,
1077 Output_data
* od
, unsigned int shndx
, Address address
,
1080 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1081 address
, addend
, false));
1084 // Add a RELATIVE reloc against a local symbol.
1087 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1088 unsigned int local_sym_index
, unsigned int type
,
1089 Output_data
* od
, Address address
, Addend addend
)
1091 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1096 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1097 unsigned int local_sym_index
, unsigned int type
,
1098 Output_data
* od
, unsigned int shndx
, Address address
,
1101 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1102 address
, addend
, true));
1105 // A reloc against the STT_SECTION symbol of an output section.
1108 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1109 Address address
, Addend addend
)
1110 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1113 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1114 unsigned int shndx
, Address address
, Addend addend
)
1115 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1119 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1120 // for one symbol--either a global symbol or a local symbol in an
1121 // object. The target specific code adds entries to the GOT as
1124 template<int size
, bool big_endian
>
1125 class Output_data_got
: public Output_section_data_build
1128 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1129 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1130 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1133 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1137 // Add an entry for a global symbol to the GOT. Return true if this
1138 // is a new GOT entry, false if the symbol was already in the GOT.
1140 add_global(Symbol
* gsym
);
1142 // Add an entry for a global symbol to the GOT, and add a dynamic
1143 // relocation of type R_TYPE for the GOT entry.
1145 add_global_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
, unsigned int r_type
);
1148 add_global_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
, unsigned int r_type
);
1150 // Add an entry for a local symbol to the GOT. This returns true if
1151 // this is a new GOT entry, false if the symbol already has a GOT
1154 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
1156 // Add an entry for a global symbol to the GOT, and add a dynamic
1157 // relocation of type R_TYPE for the GOT entry.
1159 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1160 unsigned int sym_index
, Rel_dyn
* rel_dyn
,
1161 unsigned int r_type
);
1164 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1165 unsigned int sym_index
, Rela_dyn
* rela_dyn
,
1166 unsigned int r_type
);
1168 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1169 // Return true if this is a new GOT entry, false if the symbol was
1170 // already in the GOT.
1172 add_global_tls(Symbol
* gsym
, bool need_pair
);
1174 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1175 // relocation of type R_TYPE.
1177 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1178 unsigned int r_type
);
1181 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1182 unsigned int r_type
);
1184 // Add a pair of entries for a global TLS symbol to the GOT, and add
1185 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1187 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1188 unsigned int mod_r_type
,
1189 unsigned int dtv_r_type
);
1192 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1193 unsigned int mod_r_type
,
1194 unsigned int dtv_r_type
);
1196 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1197 // This returns true if this is a new GOT entry, false if the symbol
1198 // already has a GOT entry.
1200 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
1201 unsigned int sym_index
, bool need_pair
);
1203 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1204 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1205 // Because this is a local symbol, the first GOT entry can be relocated
1206 // relative to a section symbol, and the second GOT entry will have an
1207 // dtv-relative value that can be computed at link time.
1209 add_local_tls_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1210 unsigned int sym_index
, unsigned int shndx
,
1211 bool need_pair
, Rel_dyn
* rel_dyn
,
1212 unsigned int r_type
);
1215 add_local_tls_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1216 unsigned int sym_index
, unsigned int shndx
,
1217 bool need_pair
, Rela_dyn
* rela_dyn
,
1218 unsigned int r_type
);
1220 // Add a constant to the GOT. This returns the offset of the new
1221 // entry from the start of the GOT.
1223 add_constant(Valtype constant
)
1225 this->entries_
.push_back(Got_entry(constant
));
1226 this->set_got_size();
1227 return this->last_got_offset();
1231 // Write out the GOT table.
1233 do_write(Output_file
*);
1236 // This POD class holds a single GOT entry.
1240 // Create a zero entry.
1242 : local_sym_index_(CONSTANT_CODE
)
1243 { this->u_
.constant
= 0; }
1245 // Create a global symbol entry.
1246 explicit Got_entry(Symbol
* gsym
)
1247 : local_sym_index_(GSYM_CODE
)
1248 { this->u_
.gsym
= gsym
; }
1250 // Create a local symbol entry.
1251 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1252 unsigned int local_sym_index
)
1253 : local_sym_index_(local_sym_index
)
1255 gold_assert(local_sym_index
!= GSYM_CODE
1256 && local_sym_index
!= CONSTANT_CODE
);
1257 this->u_
.object
= object
;
1260 // Create a constant entry. The constant is a host value--it will
1261 // be swapped, if necessary, when it is written out.
1262 explicit Got_entry(Valtype constant
)
1263 : local_sym_index_(CONSTANT_CODE
)
1264 { this->u_
.constant
= constant
; }
1266 // Write the GOT entry to an output view.
1268 write(unsigned char* pov
) const;
1279 // For a local symbol, the object.
1280 Sized_relobj
<size
, big_endian
>* object
;
1281 // For a global symbol, the symbol.
1283 // For a constant, the constant.
1286 // For a local symbol, the local symbol index. This is GSYM_CODE
1287 // for a global symbol, or CONSTANT_CODE for a constant.
1288 unsigned int local_sym_index_
;
1291 typedef std::vector
<Got_entry
> Got_entries
;
1293 // Return the offset into the GOT of GOT entry I.
1295 got_offset(unsigned int i
) const
1296 { return i
* (size
/ 8); }
1298 // Return the offset into the GOT of the last entry added.
1300 last_got_offset() const
1301 { return this->got_offset(this->entries_
.size() - 1); }
1303 // Set the size of the section.
1306 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1308 // The list of GOT entries.
1309 Got_entries entries_
;
1312 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1315 class Output_data_dynamic
: public Output_section_data
1318 Output_data_dynamic(Stringpool
* pool
)
1319 : Output_section_data(Output_data::default_alignment()),
1320 entries_(), pool_(pool
)
1323 // Add a new dynamic entry with a fixed numeric value.
1325 add_constant(elfcpp::DT tag
, unsigned int val
)
1326 { this->add_entry(Dynamic_entry(tag
, val
)); }
1328 // Add a new dynamic entry with the address of output data.
1330 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1331 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1333 // Add a new dynamic entry with the size of output data.
1335 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1336 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1338 // Add a new dynamic entry with the address of a symbol.
1340 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1341 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1343 // Add a new dynamic entry with a string.
1345 add_string(elfcpp::DT tag
, const char* str
)
1346 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1349 add_string(elfcpp::DT tag
, const std::string
& str
)
1350 { this->add_string(tag
, str
.c_str()); }
1353 // Adjust the output section to set the entry size.
1355 do_adjust_output_section(Output_section
*);
1357 // Set the final data size.
1359 set_final_data_size();
1361 // Write out the dynamic entries.
1363 do_write(Output_file
*);
1366 // This POD class holds a single dynamic entry.
1370 // Create an entry with a fixed numeric value.
1371 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1372 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1373 { this->u_
.val
= val
; }
1375 // Create an entry with the size or address of a section.
1376 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1378 classification_(section_size
1379 ? DYNAMIC_SECTION_SIZE
1380 : DYNAMIC_SECTION_ADDRESS
)
1381 { this->u_
.od
= od
; }
1383 // Create an entry with the address of a symbol.
1384 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1385 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1386 { this->u_
.sym
= sym
; }
1388 // Create an entry with a string.
1389 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1390 : tag_(tag
), classification_(DYNAMIC_STRING
)
1391 { this->u_
.str
= str
; }
1393 // Write the dynamic entry to an output view.
1394 template<int size
, bool big_endian
>
1396 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1404 DYNAMIC_SECTION_ADDRESS
,
1406 DYNAMIC_SECTION_SIZE
,
1415 // For DYNAMIC_NUMBER.
1417 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1418 const Output_data
* od
;
1419 // For DYNAMIC_SYMBOL.
1421 // For DYNAMIC_STRING.
1426 // The type of entry.
1427 Classification classification_
;
1430 // Add an entry to the list.
1432 add_entry(const Dynamic_entry
& entry
)
1433 { this->entries_
.push_back(entry
); }
1435 // Sized version of write function.
1436 template<int size
, bool big_endian
>
1438 sized_write(Output_file
* of
);
1440 // The type of the list of entries.
1441 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1444 Dynamic_entries entries_
;
1445 // The pool used for strings.
1449 // An output section. We don't expect to have too many output
1450 // sections, so we don't bother to do a template on the size.
1452 class Output_section
: public Output_data
1455 // Create an output section, giving the name, type, and flags.
1456 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1457 virtual ~Output_section();
1459 // Add a new input section SHNDX, named NAME, with header SHDR, from
1460 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1461 // which applies to this section, or 0 if none, or -1U if more than
1462 // one. Return the offset within the output section.
1463 template<int size
, bool big_endian
>
1465 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1467 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1468 unsigned int reloc_shndx
);
1470 // Add generated data POSD to this output section.
1472 add_output_section_data(Output_section_data
* posd
);
1474 // Return the section name.
1477 { return this->name_
; }
1479 // Return the section type.
1482 { return this->type_
; }
1484 // Return the section flags.
1487 { return this->flags_
; }
1489 // Return the entsize field.
1492 { return this->entsize_
; }
1494 // Set the entsize field.
1496 set_entsize(uint64_t v
);
1498 // Set the link field to the output section index of a section.
1500 set_link_section(const Output_data
* od
)
1502 gold_assert(this->link_
== 0
1503 && !this->should_link_to_symtab_
1504 && !this->should_link_to_dynsym_
);
1505 this->link_section_
= od
;
1508 // Set the link field to a constant.
1510 set_link(unsigned int v
)
1512 gold_assert(this->link_section_
== NULL
1513 && !this->should_link_to_symtab_
1514 && !this->should_link_to_dynsym_
);
1518 // Record that this section should link to the normal symbol table.
1520 set_should_link_to_symtab()
1522 gold_assert(this->link_section_
== NULL
1524 && !this->should_link_to_dynsym_
);
1525 this->should_link_to_symtab_
= true;
1528 // Record that this section should link to the dynamic symbol table.
1530 set_should_link_to_dynsym()
1532 gold_assert(this->link_section_
== NULL
1534 && !this->should_link_to_symtab_
);
1535 this->should_link_to_dynsym_
= true;
1538 // Return the info field.
1542 gold_assert(this->info_section_
== NULL
);
1546 // Set the info field to the output section index of a section.
1548 set_info_section(const Output_data
* od
)
1550 gold_assert(this->info_
== 0);
1551 this->info_section_
= od
;
1554 // Set the info field to a constant.
1556 set_info(unsigned int v
)
1558 gold_assert(this->info_section_
== NULL
);
1562 // Set the addralign field.
1564 set_addralign(uint64_t v
)
1565 { this->addralign_
= v
; }
1567 // Indicate that we need a symtab index.
1569 set_needs_symtab_index()
1570 { this->needs_symtab_index_
= true; }
1572 // Return whether we need a symtab index.
1574 needs_symtab_index() const
1575 { return this->needs_symtab_index_
; }
1577 // Get the symtab index.
1579 symtab_index() const
1581 gold_assert(this->symtab_index_
!= 0);
1582 return this->symtab_index_
;
1585 // Set the symtab index.
1587 set_symtab_index(unsigned int index
)
1589 gold_assert(index
!= 0);
1590 this->symtab_index_
= index
;
1593 // Indicate that we need a dynsym index.
1595 set_needs_dynsym_index()
1596 { this->needs_dynsym_index_
= true; }
1598 // Return whether we need a dynsym index.
1600 needs_dynsym_index() const
1601 { return this->needs_dynsym_index_
; }
1603 // Get the dynsym index.
1605 dynsym_index() const
1607 gold_assert(this->dynsym_index_
!= 0);
1608 return this->dynsym_index_
;
1611 // Set the dynsym index.
1613 set_dynsym_index(unsigned int index
)
1615 gold_assert(index
!= 0);
1616 this->dynsym_index_
= index
;
1619 // Return whether this section should be written after all the input
1620 // sections are complete.
1622 after_input_sections() const
1623 { return this->after_input_sections_
; }
1625 // Record that this section should be written after all the input
1626 // sections are complete.
1628 set_after_input_sections()
1629 { this->after_input_sections_
= true; }
1631 // Return whether this section requires postprocessing after all
1632 // relocations have been applied.
1634 requires_postprocessing() const
1635 { return this->requires_postprocessing_
; }
1637 // If a section requires postprocessing, return the buffer to use.
1639 postprocessing_buffer() const
1641 gold_assert(this->postprocessing_buffer_
!= NULL
);
1642 return this->postprocessing_buffer_
;
1645 // If a section requires postprocessing, create the buffer to use.
1647 create_postprocessing_buffer();
1649 // If a section requires postprocessing, this is the size of the
1650 // buffer to which relocations should be applied.
1652 postprocessing_buffer_size() const
1653 { return this->current_data_size_for_child(); }
1655 // Return whether the offset OFFSET in the input section SHNDX in
1656 // object OBJECT is being included in the link.
1658 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1659 off_t offset
) const;
1661 // Return the offset within the output section of OFFSET relative to
1662 // the start of input section SHNDX in object OBJECT.
1664 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
) const;
1666 // Return the output virtual address of OFFSET relative to the start
1667 // of input section SHNDX in object OBJECT.
1669 output_address(const Relobj
* object
, unsigned int shndx
,
1670 off_t offset
) const;
1672 // Write the section header into *OPHDR.
1673 template<int size
, bool big_endian
>
1675 write_header(const Layout
*, const Stringpool
*,
1676 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1679 // Return the section index in the output file.
1681 do_out_shndx() const
1683 gold_assert(this->out_shndx_
!= -1U);
1684 return this->out_shndx_
;
1687 // Set the output section index.
1689 do_set_out_shndx(unsigned int shndx
)
1691 gold_assert(this->out_shndx_
== -1U);
1692 this->out_shndx_
= shndx
;
1695 // Set the final data size of the Output_section. For a typical
1696 // Output_section, there is nothing to do, but if there are any
1697 // Output_section_data objects we need to set their final addresses
1700 set_final_data_size();
1702 // Write the data to the file. For a typical Output_section, this
1703 // does nothing: the data is written out by calling Object::Relocate
1704 // on each input object. But if there are any Output_section_data
1705 // objects we do need to write them out here.
1707 do_write(Output_file
*);
1709 // Return the address alignment--function required by parent class.
1711 do_addralign() const
1712 { return this->addralign_
; }
1714 // Return whether this is an Output_section.
1716 do_is_section() const
1719 // Return whether this is a section of the specified type.
1721 do_is_section_type(elfcpp::Elf_Word type
) const
1722 { return this->type_
== type
; }
1724 // Return whether the specified section flag is set.
1726 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1727 { return (this->flags_
& flag
) != 0; }
1729 // Set the TLS offset. Called only for SHT_TLS sections.
1731 do_set_tls_offset(uint64_t tls_base
);
1733 // Return the TLS offset, relative to the base of the TLS segment.
1734 // Valid only for SHT_TLS sections.
1736 do_tls_offset() const
1737 { return this->tls_offset_
; }
1739 // Modify the section name. This is only permitted for an
1740 // unallocated section, and only before the size has been finalized.
1741 // Otherwise the name will not get into Layout::namepool_.
1743 set_name(const char* newname
)
1745 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
1746 gold_assert(!this->is_data_size_valid());
1747 this->name_
= newname
;
1750 // This may be implemented by a child class.
1752 do_finalize_name(Layout
*)
1755 // Record that this section requires postprocessing after all
1756 // relocations have been applied. This is called by a child class.
1758 set_requires_postprocessing()
1760 this->requires_postprocessing_
= true;
1761 this->after_input_sections_
= true;
1764 // Write all the data of an Output_section into the postprocessing
1767 write_to_postprocessing_buffer();
1770 // In some cases we need to keep a list of the input sections
1771 // associated with this output section. We only need the list if we
1772 // might have to change the offsets of the input section within the
1773 // output section after we add the input section. The ordinary
1774 // input sections will be written out when we process the object
1775 // file, and as such we don't need to track them here. We do need
1776 // to track Output_section_data objects here. We store instances of
1777 // this structure in a std::vector, so it must be a POD. There can
1778 // be many instances of this structure, so we use a union to save
1784 : shndx_(0), p2align_(0)
1786 this->u1_
.data_size
= 0;
1787 this->u2_
.object
= NULL
;
1790 // For an ordinary input section.
1791 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1794 p2align_(ffsll(static_cast<long long>(addralign
)))
1796 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1797 && shndx
!= MERGE_DATA_SECTION_CODE
1798 && shndx
!= MERGE_STRING_SECTION_CODE
);
1799 this->u1_
.data_size
= data_size
;
1800 this->u2_
.object
= object
;
1803 // For a non-merge output section.
1804 Input_section(Output_section_data
* posd
)
1805 : shndx_(OUTPUT_SECTION_CODE
),
1806 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1808 this->u1_
.data_size
= 0;
1809 this->u2_
.posd
= posd
;
1812 // For a merge section.
1813 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1815 ? MERGE_STRING_SECTION_CODE
1816 : MERGE_DATA_SECTION_CODE
),
1817 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1819 this->u1_
.entsize
= entsize
;
1820 this->u2_
.posd
= posd
;
1823 // The required alignment.
1827 return (this->p2align_
== 0
1829 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1832 // Return the required size.
1836 // Return whether this is a merge section which matches the
1839 is_merge_section(bool is_string
, uint64_t entsize
,
1840 uint64_t addralign
) const
1842 return (this->shndx_
== (is_string
1843 ? MERGE_STRING_SECTION_CODE
1844 : MERGE_DATA_SECTION_CODE
)
1845 && this->u1_
.entsize
== entsize
1846 && this->addralign() == addralign
);
1849 // Set the output section.
1851 set_output_section(Output_section
* os
)
1853 gold_assert(!this->is_input_section());
1854 this->u2_
.posd
->set_output_section(os
);
1857 // Set the address and file offset. This is called during
1858 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
1859 // the enclosing section.
1861 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
1862 off_t section_file_offset
);
1864 // Finalize the data size.
1866 finalize_data_size();
1868 // Add an input section, for SHF_MERGE sections.
1870 add_input_section(Relobj
* object
, unsigned int shndx
)
1872 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1873 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1874 return this->u2_
.posd
->add_input_section(object
, shndx
);
1877 // Given an input OBJECT, an input section index SHNDX within that
1878 // object, and an OFFSET relative to the start of that input
1879 // section, return whether or not the output offset is known. If
1880 // this function returns true, it sets *POUTPUT to the output
1883 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1884 off_t
*poutput
) const;
1886 // Write out the data. This does nothing for an input section.
1888 write(Output_file
*);
1890 // Write the data to a buffer. This does nothing for an input
1893 write_to_buffer(unsigned char*);
1896 // Code values which appear in shndx_. If the value is not one of
1897 // these codes, it is the input section index in the object file.
1900 // An Output_section_data.
1901 OUTPUT_SECTION_CODE
= -1U,
1902 // An Output_section_data for an SHF_MERGE section with
1903 // SHF_STRINGS not set.
1904 MERGE_DATA_SECTION_CODE
= -2U,
1905 // An Output_section_data for an SHF_MERGE section with
1907 MERGE_STRING_SECTION_CODE
= -3U
1910 // Whether this is an input section.
1912 is_input_section() const
1914 return (this->shndx_
!= OUTPUT_SECTION_CODE
1915 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1916 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1919 // For an ordinary input section, this is the section index in the
1920 // input file. For an Output_section_data, this is
1921 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1922 // MERGE_STRING_SECTION_CODE.
1923 unsigned int shndx_
;
1924 // The required alignment, stored as a power of 2.
1925 unsigned int p2align_
;
1928 // For an ordinary input section, the section size.
1930 // For OUTPUT_SECTION_CODE, this is not used. For
1931 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1937 // For an ordinary input section, the object which holds the
1940 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1941 // MERGE_STRING_SECTION_CODE, the data.
1942 Output_section_data
* posd
;
1946 typedef std::vector
<Input_section
> Input_section_list
;
1948 // Fill data. This is used to fill in data between input sections.
1949 // When we have to keep track of the input sections, we can use an
1950 // Output_data_const, but we don't want to have to keep track of
1951 // input sections just to implement fills. For a fill we record the
1952 // offset, and the actual data to be written out.
1956 Fill(off_t section_offset
, off_t length
)
1957 : section_offset_(section_offset
), length_(length
)
1960 // Return section offset.
1962 section_offset() const
1963 { return this->section_offset_
; }
1965 // Return fill length.
1968 { return this->length_
; }
1971 // The offset within the output section.
1972 off_t section_offset_
;
1973 // The length of the space to fill.
1977 typedef std::vector
<Fill
> Fill_list
;
1979 // Add a new output section by Input_section.
1981 add_output_section_data(Input_section
*);
1983 // Add an SHF_MERGE input section. Returns true if the section was
1986 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1987 uint64_t entsize
, uint64_t addralign
);
1989 // Add an output SHF_MERGE section POSD to this output section.
1990 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1991 // ENTSIZE is the entity size. This returns the entry added to
1994 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1997 // Most of these fields are only valid after layout.
1999 // The name of the section. This will point into a Stringpool.
2001 // The section address is in the parent class.
2002 // The section alignment.
2003 uint64_t addralign_
;
2004 // The section entry size.
2006 // The file offset is in the parent class.
2007 // Set the section link field to the index of this section.
2008 const Output_data
* link_section_
;
2009 // If link_section_ is NULL, this is the link field.
2011 // Set the section info field to the index of this section.
2012 const Output_data
* info_section_
;
2013 // If info_section_ is NULL, this is the section info field.
2015 // The section type.
2016 const elfcpp::Elf_Word type_
;
2017 // The section flags.
2018 const elfcpp::Elf_Xword flags_
;
2019 // The section index.
2020 unsigned int out_shndx_
;
2021 // If there is a STT_SECTION for this output section in the normal
2022 // symbol table, this is the symbol index. This starts out as zero.
2023 // It is initialized in Layout::finalize() to be the index, or -1U
2024 // if there isn't one.
2025 unsigned int symtab_index_
;
2026 // If there is a STT_SECTION for this output section in the dynamic
2027 // symbol table, this is the symbol index. This starts out as zero.
2028 // It is initialized in Layout::finalize() to be the index, or -1U
2029 // if there isn't one.
2030 unsigned int dynsym_index_
;
2031 // The input sections. This will be empty in cases where we don't
2032 // need to keep track of them.
2033 Input_section_list input_sections_
;
2034 // The offset of the first entry in input_sections_.
2035 off_t first_input_offset_
;
2036 // The fill data. This is separate from input_sections_ because we
2037 // often will need fill sections without needing to keep track of
2040 // If the section requires postprocessing, this buffer holds the
2041 // section contents during relocation.
2042 unsigned char* postprocessing_buffer_
;
2043 // Whether this output section needs a STT_SECTION symbol in the
2044 // normal symbol table. This will be true if there is a relocation
2046 bool needs_symtab_index_
: 1;
2047 // Whether this output section needs a STT_SECTION symbol in the
2048 // dynamic symbol table. This will be true if there is a dynamic
2049 // relocation which needs it.
2050 bool needs_dynsym_index_
: 1;
2051 // Whether the link field of this output section should point to the
2052 // normal symbol table.
2053 bool should_link_to_symtab_
: 1;
2054 // Whether the link field of this output section should point to the
2055 // dynamic symbol table.
2056 bool should_link_to_dynsym_
: 1;
2057 // Whether this section should be written after all the input
2058 // sections are complete.
2059 bool after_input_sections_
: 1;
2060 // Whether this section requires post processing after all
2061 // relocations have been applied.
2062 bool requires_postprocessing_
: 1;
2063 // For SHT_TLS sections, the offset of this section relative to the base
2064 // of the TLS segment.
2065 uint64_t tls_offset_
;
2068 // An output segment. PT_LOAD segments are built from collections of
2069 // output sections. Other segments typically point within PT_LOAD
2070 // segments, and are built directly as needed.
2072 class Output_segment
2075 // Create an output segment, specifying the type and flags.
2076 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
2078 // Return the virtual address.
2081 { return this->vaddr_
; }
2083 // Return the physical address.
2086 { return this->paddr_
; }
2088 // Return the segment type.
2091 { return this->type_
; }
2093 // Return the segment flags.
2096 { return this->flags_
; }
2098 // Return the memory size.
2101 { return this->memsz_
; }
2103 // Return the file size.
2106 { return this->filesz_
; }
2108 // Return the maximum alignment of the Output_data.
2112 // Add an Output_section to this segment.
2114 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2115 { this->add_output_section(os
, seg_flags
, false); }
2117 // Add an Output_section to the start of this segment.
2119 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2120 { this->add_output_section(os
, seg_flags
, true); }
2122 // Add an Output_data (which is not an Output_section) to the start
2125 add_initial_output_data(Output_data
*);
2127 // Return the number of dynamic relocations applied to this segment.
2129 dynamic_reloc_count() const;
2131 // Set the address of the segment to ADDR and the offset to *POFF
2132 // (aligned if necessary), and set the addresses and offsets of all
2133 // contained output sections accordingly. Set the section indexes
2134 // of all contained output sections starting with *PSHNDX. Return
2135 // the address of the immediately following segment. Update *POFF
2136 // and *PSHNDX. This should only be called for a PT_LOAD segment.
2138 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
2140 // Set the minimum alignment of this segment. This may be adjusted
2141 // upward based on the section alignments.
2143 set_minimum_addralign(uint64_t align
)
2145 gold_assert(!this->is_align_known_
);
2146 this->align_
= align
;
2149 // Set the offset of this segment based on the section. This should
2150 // only be called for a non-PT_LOAD segment.
2154 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2158 // Return the number of output sections.
2160 output_section_count() const;
2162 // Write the segment header into *OPHDR.
2163 template<int size
, bool big_endian
>
2165 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2167 // Write the section headers of associated sections into V.
2168 template<int size
, bool big_endian
>
2170 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2171 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
2174 Output_segment(const Output_segment
&);
2175 Output_segment
& operator=(const Output_segment
&);
2177 typedef std::list
<Output_data
*> Output_data_list
;
2179 // Add an Output_section to this segment, specifying front or back.
2181 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2184 // Find the maximum alignment in an Output_data_list.
2186 maximum_alignment(const Output_data_list
*);
2188 // Set the section addresses in an Output_data_list.
2190 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
2191 unsigned int* pshndx
);
2193 // Return the number of Output_sections in an Output_data_list.
2195 output_section_count_list(const Output_data_list
*) const;
2197 // Return the number of dynamic relocs in an Output_data_list.
2199 dynamic_reloc_count_list(const Output_data_list
*) const;
2201 // Write the section headers in the list into V.
2202 template<int size
, bool big_endian
>
2204 write_section_headers_list(const Layout
*, const Stringpool
*,
2205 const Output_data_list
*, unsigned char* v
,
2206 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
2208 // The list of output data with contents attached to this segment.
2209 Output_data_list output_data_
;
2210 // The list of output data without contents attached to this segment.
2211 Output_data_list output_bss_
;
2212 // The segment virtual address.
2214 // The segment physical address.
2216 // The size of the segment in memory.
2218 // The segment alignment. The is_align_known_ field indicates
2219 // whether this has been finalized. It can be set to a minimum
2220 // value before it is finalized.
2222 // The offset of the segment data within the file.
2224 // The size of the segment data in the file.
2226 // The segment type;
2227 elfcpp::Elf_Word type_
;
2228 // The segment flags.
2229 elfcpp::Elf_Word flags_
;
2230 // Whether we have finalized align_.
2231 bool is_align_known_
;
2234 // This class represents the output file.
2239 Output_file(const General_options
& options
, Target
*);
2241 // Get a pointer to the target.
2244 { return this->target_
; }
2246 // Open the output file. FILE_SIZE is the final size of the file.
2248 open(off_t file_size
);
2250 // Resize the output file.
2252 resize(off_t file_size
);
2254 // Close the output file (flushing all buffered data) and make sure
2255 // there are no errors.
2259 // We currently always use mmap which makes the view handling quite
2260 // simple. In the future we may support other approaches.
2262 // Write data to the output file.
2264 write(off_t offset
, const void* data
, off_t len
)
2265 { memcpy(this->base_
+ offset
, data
, len
); }
2267 // Get a buffer to use to write to the file, given the offset into
2268 // the file and the size.
2270 get_output_view(off_t start
, off_t size
)
2272 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
2273 return this->base_
+ start
;
2276 // VIEW must have been returned by get_output_view. Write the
2277 // buffer to the file, passing in the offset and the size.
2279 write_output_view(off_t
, off_t
, unsigned char*)
2282 // Get a read/write buffer. This is used when we want to write part
2283 // of the file, read it in, and write it again.
2285 get_input_output_view(off_t start
, off_t size
)
2286 { return this->get_output_view(start
, size
); }
2288 // Write a read/write buffer back to the file.
2290 write_input_output_view(off_t
, off_t
, unsigned char*)
2293 // Get a read buffer. This is used when we just want to read part
2294 // of the file back it in.
2295 const unsigned char*
2296 get_input_view(off_t start
, off_t size
)
2297 { return this->get_output_view(start
, size
); }
2299 // Release a read bfufer.
2301 free_input_view(off_t
, off_t
, const unsigned char*)
2305 // Map the file into memory and return a pointer to the map.
2309 // Unmap the file from memory (and flush to disk buffers).
2315 const General_options
& options_
;
2324 // Base of file mapped into memory.
2325 unsigned char* base_
;
2326 // True iff base_ points to a memory buffer rather than an output file.
2327 bool map_is_anonymous_
;
2330 } // End namespace gold.
2332 #endif // !defined(GOLD_OUTPUT_H)