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
,
708 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
709 unsigned int shndx
, Address address
);
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
);
717 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
718 unsigned int local_sym_index
, unsigned int type
,
719 unsigned int shndx
, Address address
);
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 // Write the reloc entry to an output view.
731 write(unsigned char* pov
) const;
733 // Write the offset and info fields to Write_rel.
734 template<typename Write_rel
>
735 void write_rel(Write_rel
*) const;
738 // Return the symbol index. We can't do a double template
739 // specialization, so we do a secondary template here.
741 get_symbol_index() const;
743 // Codes for local_sym_index_.
750 // Invalid uninitialized entry.
756 // For a local symbol, the object. We will never generate a
757 // relocation against a local symbol in a dynamic object; that
758 // doesn't make sense. And our callers will always be
759 // templatized, so we use Sized_relobj here.
760 Sized_relobj
<size
, big_endian
>* relobj
;
761 // For a global symbol, the symbol. If this is NULL, it indicates
762 // a relocation against the undefined 0 symbol.
764 // For a relocation against an output section, the output section.
769 // If shndx_ is not INVALID CODE, the object which holds the input
770 // section being used to specify the reloc address.
772 // If shndx_ is INVALID_CODE, the output data being used to
773 // specify the reloc address. This may be NULL if the reloc
774 // address is absolute.
777 // The address offset within the input section or the Output_data.
779 // For a local symbol, the local symbol index. This is GSYM_CODE
780 // for a global symbol, or INVALID_CODE for an uninitialized value.
781 unsigned int local_sym_index_
;
782 // The reloc type--a processor specific code.
784 // If the reloc address is an input section in an object, the
785 // section index. This is INVALID_CODE if the reloc address is
786 // specified in some other way.
790 // The SHT_RELA version of Output_reloc<>. This is just derived from
791 // the SHT_REL version of Output_reloc, but it adds an addend.
793 template<bool dynamic
, int size
, bool big_endian
>
794 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
797 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
798 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
800 // An uninitialized entry.
805 // A reloc against a global symbol.
807 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
808 Address address
, Addend addend
)
809 : rel_(gsym
, type
, od
, address
), addend_(addend
)
812 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
813 unsigned int shndx
, Address address
, Addend addend
)
814 : rel_(gsym
, type
, relobj
, shndx
, address
), addend_(addend
)
817 // A reloc against a local symbol.
819 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
820 unsigned int local_sym_index
,
821 unsigned int type
, Output_data
* od
, Address address
,
823 : rel_(relobj
, local_sym_index
, type
, od
, address
), addend_(addend
)
826 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
827 unsigned int local_sym_index
,
832 : rel_(relobj
, local_sym_index
, type
, shndx
, address
),
836 // A reloc against the STT_SECTION symbol of an output section.
838 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
839 Address address
, Addend addend
)
840 : rel_(os
, type
, od
, address
), addend_(addend
)
843 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
844 unsigned int shndx
, Address address
, Addend addend
)
845 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
848 // Write the reloc entry to an output view.
850 write(unsigned char* pov
) const;
854 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
859 // Output_data_reloc is used to manage a section containing relocs.
860 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
861 // indicates whether this is a dynamic relocation or a normal
862 // relocation. Output_data_reloc_base is a base class.
863 // Output_data_reloc is the real class, which we specialize based on
866 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
867 class Output_data_reloc_base
: public Output_section_data_build
870 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
871 typedef typename
Output_reloc_type::Address Address
;
872 static const int reloc_size
=
873 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
875 // Construct the section.
876 Output_data_reloc_base()
877 : Output_section_data_build(Output_data::default_alignment_for_size(size
))
881 // Write out the data.
883 do_write(Output_file
*);
885 // Set the entry size and the link.
887 do_adjust_output_section(Output_section
*os
);
889 // Add a relocation entry.
891 add(Output_data
*od
, const Output_reloc_type
& reloc
)
893 this->relocs_
.push_back(reloc
);
894 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
895 od
->add_dynamic_reloc();
899 typedef std::vector
<Output_reloc_type
> Relocs
;
904 // The class which callers actually create.
906 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
907 class Output_data_reloc
;
909 // The SHT_REL version of Output_data_reloc.
911 template<bool dynamic
, int size
, bool big_endian
>
912 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
913 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
916 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
920 typedef typename
Base::Output_reloc_type Output_reloc_type
;
921 typedef typename
Output_reloc_type::Address Address
;
924 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
927 // Add a reloc against a global symbol.
930 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
931 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
)); }
934 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
935 unsigned int shndx
, Address address
)
936 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
)); }
938 // Add a reloc against a local symbol.
941 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
942 unsigned int local_sym_index
, unsigned int type
,
943 Output_data
* od
, Address address
)
944 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
948 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
949 unsigned int local_sym_index
, unsigned int type
,
950 Output_data
* od
, unsigned int shndx
, Address address
)
951 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
955 // A reloc against the STT_SECTION symbol of an output section.
956 // OS is the Output_section that the relocation refers to; OD is
957 // the Output_data object being relocated.
960 add_output_section(Output_section
* os
, unsigned int type
,
961 Output_data
* od
, Address address
)
962 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
965 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
966 Relobj
* relobj
, unsigned int shndx
, Address address
)
967 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
970 // The SHT_RELA version of Output_data_reloc.
972 template<bool dynamic
, int size
, bool big_endian
>
973 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
974 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
977 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
981 typedef typename
Base::Output_reloc_type Output_reloc_type
;
982 typedef typename
Output_reloc_type::Address Address
;
983 typedef typename
Output_reloc_type::Addend Addend
;
986 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
989 // Add a reloc against a global symbol.
992 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
993 Address address
, Addend addend
)
994 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
997 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
998 unsigned int shndx
, Address address
,
1000 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1003 // Add a reloc against a local symbol.
1006 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1007 unsigned int local_sym_index
, unsigned int type
,
1008 Output_data
* od
, Address address
, Addend addend
)
1010 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1015 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1016 unsigned int local_sym_index
, unsigned int type
,
1017 Output_data
* od
, unsigned int shndx
, Address address
,
1020 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1024 // A reloc against the STT_SECTION symbol of an output section.
1027 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1028 Address address
, Addend addend
)
1029 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1032 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
1033 unsigned int shndx
, Address address
, Addend addend
)
1034 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1038 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1039 // for one symbol--either a global symbol or a local symbol in an
1040 // object. The target specific code adds entries to the GOT as
1043 template<int size
, bool big_endian
>
1044 class Output_data_got
: public Output_section_data_build
1047 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1048 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1049 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1052 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1056 // Add an entry for a global symbol to the GOT. Return true if this
1057 // is a new GOT entry, false if the symbol was already in the GOT.
1059 add_global(Symbol
* gsym
);
1061 // Add an entry for a global symbol to the GOT, and add a dynamic
1062 // relocation of type R_TYPE for the GOT entry.
1064 add_global_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
, unsigned int r_type
);
1067 add_global_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
, unsigned int r_type
);
1069 // Add an entry for a local symbol to the GOT. This returns true if
1070 // this is a new GOT entry, false if the symbol already has a GOT
1073 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
1075 // Add an entry for a global symbol to the GOT, and add a dynamic
1076 // relocation of type R_TYPE for the GOT entry.
1078 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1079 unsigned int sym_index
, Rel_dyn
* rel_dyn
,
1080 unsigned int r_type
);
1083 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1084 unsigned int sym_index
, Rela_dyn
* rela_dyn
,
1085 unsigned int r_type
);
1087 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1088 // Return true if this is a new GOT entry, false if the symbol was
1089 // already in the GOT.
1091 add_global_tls(Symbol
* gsym
, bool need_pair
);
1093 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1094 // relocation of type R_TYPE.
1096 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1097 unsigned int r_type
);
1100 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1101 unsigned int r_type
);
1103 // Add a pair of entries for a global TLS symbol to the GOT, and add
1104 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1106 add_global_tls_with_rel(Symbol
* gsym
, Rel_dyn
* rel_dyn
,
1107 unsigned int mod_r_type
,
1108 unsigned int dtv_r_type
);
1111 add_global_tls_with_rela(Symbol
* gsym
, Rela_dyn
* rela_dyn
,
1112 unsigned int mod_r_type
,
1113 unsigned int dtv_r_type
);
1115 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1116 // This returns true if this is a new GOT entry, false if the symbol
1117 // already has a GOT entry.
1119 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
1120 unsigned int sym_index
, bool need_pair
);
1122 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1123 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1124 // Because this is a local symbol, the first GOT entry can be relocated
1125 // relative to a section symbol, and the second GOT entry will have an
1126 // dtv-relative value that can be computed at link time.
1128 add_local_tls_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1129 unsigned int sym_index
, unsigned int shndx
,
1130 bool need_pair
, Rel_dyn
* rel_dyn
,
1131 unsigned int r_type
);
1134 add_local_tls_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1135 unsigned int sym_index
, unsigned int shndx
,
1136 bool need_pair
, Rela_dyn
* rela_dyn
,
1137 unsigned int r_type
);
1139 // Add a constant to the GOT. This returns the offset of the new
1140 // entry from the start of the GOT.
1142 add_constant(Valtype constant
)
1144 this->entries_
.push_back(Got_entry(constant
));
1145 this->set_got_size();
1146 return this->last_got_offset();
1150 // Write out the GOT table.
1152 do_write(Output_file
*);
1155 // This POD class holds a single GOT entry.
1159 // Create a zero entry.
1161 : local_sym_index_(CONSTANT_CODE
)
1162 { this->u_
.constant
= 0; }
1164 // Create a global symbol entry.
1165 explicit Got_entry(Symbol
* gsym
)
1166 : local_sym_index_(GSYM_CODE
)
1167 { this->u_
.gsym
= gsym
; }
1169 // Create a local symbol entry.
1170 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1171 unsigned int local_sym_index
)
1172 : local_sym_index_(local_sym_index
)
1174 gold_assert(local_sym_index
!= GSYM_CODE
1175 && local_sym_index
!= CONSTANT_CODE
);
1176 this->u_
.object
= object
;
1179 // Create a constant entry. The constant is a host value--it will
1180 // be swapped, if necessary, when it is written out.
1181 explicit Got_entry(Valtype constant
)
1182 : local_sym_index_(CONSTANT_CODE
)
1183 { this->u_
.constant
= constant
; }
1185 // Write the GOT entry to an output view.
1187 write(unsigned char* pov
) const;
1198 // For a local symbol, the object.
1199 Sized_relobj
<size
, big_endian
>* object
;
1200 // For a global symbol, the symbol.
1202 // For a constant, the constant.
1205 // For a local symbol, the local symbol index. This is GSYM_CODE
1206 // for a global symbol, or CONSTANT_CODE for a constant.
1207 unsigned int local_sym_index_
;
1210 typedef std::vector
<Got_entry
> Got_entries
;
1212 // Return the offset into the GOT of GOT entry I.
1214 got_offset(unsigned int i
) const
1215 { return i
* (size
/ 8); }
1217 // Return the offset into the GOT of the last entry added.
1219 last_got_offset() const
1220 { return this->got_offset(this->entries_
.size() - 1); }
1222 // Set the size of the section.
1225 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1227 // The list of GOT entries.
1228 Got_entries entries_
;
1231 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1234 class Output_data_dynamic
: public Output_section_data
1237 Output_data_dynamic(Stringpool
* pool
)
1238 : Output_section_data(Output_data::default_alignment()),
1239 entries_(), pool_(pool
)
1242 // Add a new dynamic entry with a fixed numeric value.
1244 add_constant(elfcpp::DT tag
, unsigned int val
)
1245 { this->add_entry(Dynamic_entry(tag
, val
)); }
1247 // Add a new dynamic entry with the address of output data.
1249 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1250 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1252 // Add a new dynamic entry with the size of output data.
1254 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1255 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1257 // Add a new dynamic entry with the address of a symbol.
1259 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1260 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1262 // Add a new dynamic entry with a string.
1264 add_string(elfcpp::DT tag
, const char* str
)
1265 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1268 add_string(elfcpp::DT tag
, const std::string
& str
)
1269 { this->add_string(tag
, str
.c_str()); }
1272 // Adjust the output section to set the entry size.
1274 do_adjust_output_section(Output_section
*);
1276 // Set the final data size.
1278 set_final_data_size();
1280 // Write out the dynamic entries.
1282 do_write(Output_file
*);
1285 // This POD class holds a single dynamic entry.
1289 // Create an entry with a fixed numeric value.
1290 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1291 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1292 { this->u_
.val
= val
; }
1294 // Create an entry with the size or address of a section.
1295 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1297 classification_(section_size
1298 ? DYNAMIC_SECTION_SIZE
1299 : DYNAMIC_SECTION_ADDRESS
)
1300 { this->u_
.od
= od
; }
1302 // Create an entry with the address of a symbol.
1303 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1304 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1305 { this->u_
.sym
= sym
; }
1307 // Create an entry with a string.
1308 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1309 : tag_(tag
), classification_(DYNAMIC_STRING
)
1310 { this->u_
.str
= str
; }
1312 // Write the dynamic entry to an output view.
1313 template<int size
, bool big_endian
>
1315 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1323 DYNAMIC_SECTION_ADDRESS
,
1325 DYNAMIC_SECTION_SIZE
,
1334 // For DYNAMIC_NUMBER.
1336 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1337 const Output_data
* od
;
1338 // For DYNAMIC_SYMBOL.
1340 // For DYNAMIC_STRING.
1345 // The type of entry.
1346 Classification classification_
;
1349 // Add an entry to the list.
1351 add_entry(const Dynamic_entry
& entry
)
1352 { this->entries_
.push_back(entry
); }
1354 // Sized version of write function.
1355 template<int size
, bool big_endian
>
1357 sized_write(Output_file
* of
);
1359 // The type of the list of entries.
1360 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1363 Dynamic_entries entries_
;
1364 // The pool used for strings.
1368 // An output section. We don't expect to have too many output
1369 // sections, so we don't bother to do a template on the size.
1371 class Output_section
: public Output_data
1374 // Create an output section, giving the name, type, and flags.
1375 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1376 virtual ~Output_section();
1378 // Add a new input section SHNDX, named NAME, with header SHDR, from
1379 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1380 // which applies to this section, or 0 if none, or -1U if more than
1381 // one. Return the offset within the output section.
1382 template<int size
, bool big_endian
>
1384 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1386 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1387 unsigned int reloc_shndx
);
1389 // Add generated data POSD to this output section.
1391 add_output_section_data(Output_section_data
* posd
);
1393 // Return the section name.
1396 { return this->name_
; }
1398 // Return the section type.
1401 { return this->type_
; }
1403 // Return the section flags.
1406 { return this->flags_
; }
1408 // Return the entsize field.
1411 { return this->entsize_
; }
1413 // Set the entsize field.
1415 set_entsize(uint64_t v
);
1417 // Set the link field to the output section index of a section.
1419 set_link_section(const Output_data
* od
)
1421 gold_assert(this->link_
== 0
1422 && !this->should_link_to_symtab_
1423 && !this->should_link_to_dynsym_
);
1424 this->link_section_
= od
;
1427 // Set the link field to a constant.
1429 set_link(unsigned int v
)
1431 gold_assert(this->link_section_
== NULL
1432 && !this->should_link_to_symtab_
1433 && !this->should_link_to_dynsym_
);
1437 // Record that this section should link to the normal symbol table.
1439 set_should_link_to_symtab()
1441 gold_assert(this->link_section_
== NULL
1443 && !this->should_link_to_dynsym_
);
1444 this->should_link_to_symtab_
= true;
1447 // Record that this section should link to the dynamic symbol table.
1449 set_should_link_to_dynsym()
1451 gold_assert(this->link_section_
== NULL
1453 && !this->should_link_to_symtab_
);
1454 this->should_link_to_dynsym_
= true;
1457 // Return the info field.
1461 gold_assert(this->info_section_
== NULL
);
1465 // Set the info field to the output section index of a section.
1467 set_info_section(const Output_data
* od
)
1469 gold_assert(this->info_
== 0);
1470 this->info_section_
= od
;
1473 // Set the info field to a constant.
1475 set_info(unsigned int v
)
1477 gold_assert(this->info_section_
== NULL
);
1481 // Set the addralign field.
1483 set_addralign(uint64_t v
)
1484 { this->addralign_
= v
; }
1486 // Indicate that we need a symtab index.
1488 set_needs_symtab_index()
1489 { this->needs_symtab_index_
= true; }
1491 // Return whether we need a symtab index.
1493 needs_symtab_index() const
1494 { return this->needs_symtab_index_
; }
1496 // Get the symtab index.
1498 symtab_index() const
1500 gold_assert(this->symtab_index_
!= 0);
1501 return this->symtab_index_
;
1504 // Set the symtab index.
1506 set_symtab_index(unsigned int index
)
1508 gold_assert(index
!= 0);
1509 this->symtab_index_
= index
;
1512 // Indicate that we need a dynsym index.
1514 set_needs_dynsym_index()
1515 { this->needs_dynsym_index_
= true; }
1517 // Return whether we need a dynsym index.
1519 needs_dynsym_index() const
1520 { return this->needs_dynsym_index_
; }
1522 // Get the dynsym index.
1524 dynsym_index() const
1526 gold_assert(this->dynsym_index_
!= 0);
1527 return this->dynsym_index_
;
1530 // Set the dynsym index.
1532 set_dynsym_index(unsigned int index
)
1534 gold_assert(index
!= 0);
1535 this->dynsym_index_
= index
;
1538 // Return whether this section should be written after all the input
1539 // sections are complete.
1541 after_input_sections() const
1542 { return this->after_input_sections_
; }
1544 // Record that this section should be written after all the input
1545 // sections are complete.
1547 set_after_input_sections()
1548 { this->after_input_sections_
= true; }
1550 // Return whether this section requires postprocessing after all
1551 // relocations have been applied.
1553 requires_postprocessing() const
1554 { return this->requires_postprocessing_
; }
1556 // If a section requires postprocessing, return the buffer to use.
1558 postprocessing_buffer() const
1560 gold_assert(this->postprocessing_buffer_
!= NULL
);
1561 return this->postprocessing_buffer_
;
1564 // If a section requires postprocessing, create the buffer to use.
1566 create_postprocessing_buffer();
1568 // If a section requires postprocessing, this is the size of the
1569 // buffer to which relocations should be applied.
1571 postprocessing_buffer_size() const
1572 { return this->current_data_size_for_child(); }
1574 // Return whether the offset OFFSET in the input section SHNDX in
1575 // object OBJECT is being included in the link.
1577 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1578 off_t offset
) const;
1580 // Return the offset within the output section of OFFSET relative to
1581 // the start of input section SHNDX in object OBJECT.
1583 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
) const;
1585 // Return the output virtual address of OFFSET relative to the start
1586 // of input section SHNDX in object OBJECT.
1588 output_address(const Relobj
* object
, unsigned int shndx
,
1589 off_t offset
) const;
1591 // Write the section header into *OPHDR.
1592 template<int size
, bool big_endian
>
1594 write_header(const Layout
*, const Stringpool
*,
1595 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1598 // Return the section index in the output file.
1600 do_out_shndx() const
1602 gold_assert(this->out_shndx_
!= -1U);
1603 return this->out_shndx_
;
1606 // Set the output section index.
1608 do_set_out_shndx(unsigned int shndx
)
1610 gold_assert(this->out_shndx_
== -1U);
1611 this->out_shndx_
= shndx
;
1614 // Set the final data size of the Output_section. For a typical
1615 // Output_section, there is nothing to do, but if there are any
1616 // Output_section_data objects we need to set their final addresses
1619 set_final_data_size();
1621 // Write the data to the file. For a typical Output_section, this
1622 // does nothing: the data is written out by calling Object::Relocate
1623 // on each input object. But if there are any Output_section_data
1624 // objects we do need to write them out here.
1626 do_write(Output_file
*);
1628 // Return the address alignment--function required by parent class.
1630 do_addralign() const
1631 { return this->addralign_
; }
1633 // Return whether this is an Output_section.
1635 do_is_section() const
1638 // Return whether this is a section of the specified type.
1640 do_is_section_type(elfcpp::Elf_Word type
) const
1641 { return this->type_
== type
; }
1643 // Return whether the specified section flag is set.
1645 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1646 { return (this->flags_
& flag
) != 0; }
1648 // Set the TLS offset. Called only for SHT_TLS sections.
1650 do_set_tls_offset(uint64_t tls_base
);
1652 // Return the TLS offset, relative to the base of the TLS segment.
1653 // Valid only for SHT_TLS sections.
1655 do_tls_offset() const
1656 { return this->tls_offset_
; }
1658 // Modify the section name. This is only permitted for an
1659 // unallocated section, and only before the size has been finalized.
1660 // Otherwise the name will not get into Layout::namepool_.
1662 set_name(const char* newname
)
1664 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
1665 gold_assert(!this->is_data_size_valid());
1666 this->name_
= newname
;
1669 // This may be implemented by a child class.
1671 do_finalize_name(Layout
*)
1674 // Record that this section requires postprocessing after all
1675 // relocations have been applied. This is called by a child class.
1677 set_requires_postprocessing()
1679 this->requires_postprocessing_
= true;
1680 this->after_input_sections_
= true;
1683 // Write all the data of an Output_section into the postprocessing
1686 write_to_postprocessing_buffer();
1689 // In some cases we need to keep a list of the input sections
1690 // associated with this output section. We only need the list if we
1691 // might have to change the offsets of the input section within the
1692 // output section after we add the input section. The ordinary
1693 // input sections will be written out when we process the object
1694 // file, and as such we don't need to track them here. We do need
1695 // to track Output_section_data objects here. We store instances of
1696 // this structure in a std::vector, so it must be a POD. There can
1697 // be many instances of this structure, so we use a union to save
1703 : shndx_(0), p2align_(0)
1705 this->u1_
.data_size
= 0;
1706 this->u2_
.object
= NULL
;
1709 // For an ordinary input section.
1710 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1713 p2align_(ffsll(static_cast<long long>(addralign
)))
1715 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1716 && shndx
!= MERGE_DATA_SECTION_CODE
1717 && shndx
!= MERGE_STRING_SECTION_CODE
);
1718 this->u1_
.data_size
= data_size
;
1719 this->u2_
.object
= object
;
1722 // For a non-merge output section.
1723 Input_section(Output_section_data
* posd
)
1724 : shndx_(OUTPUT_SECTION_CODE
),
1725 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1727 this->u1_
.data_size
= 0;
1728 this->u2_
.posd
= posd
;
1731 // For a merge section.
1732 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1734 ? MERGE_STRING_SECTION_CODE
1735 : MERGE_DATA_SECTION_CODE
),
1736 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1738 this->u1_
.entsize
= entsize
;
1739 this->u2_
.posd
= posd
;
1742 // The required alignment.
1746 return (this->p2align_
== 0
1748 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1751 // Return the required size.
1755 // Return whether this is a merge section which matches the
1758 is_merge_section(bool is_string
, uint64_t entsize
,
1759 uint64_t addralign
) const
1761 return (this->shndx_
== (is_string
1762 ? MERGE_STRING_SECTION_CODE
1763 : MERGE_DATA_SECTION_CODE
)
1764 && this->u1_
.entsize
== entsize
1765 && this->addralign() == addralign
);
1768 // Set the output section.
1770 set_output_section(Output_section
* os
)
1772 gold_assert(!this->is_input_section());
1773 this->u2_
.posd
->set_output_section(os
);
1776 // Set the address and file offset. This is called during
1777 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
1778 // the enclosing section.
1780 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
1781 off_t section_file_offset
);
1783 // Finalize the data size.
1785 finalize_data_size();
1787 // Add an input section, for SHF_MERGE sections.
1789 add_input_section(Relobj
* object
, unsigned int shndx
)
1791 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1792 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1793 return this->u2_
.posd
->add_input_section(object
, shndx
);
1796 // Given an input OBJECT, an input section index SHNDX within that
1797 // object, and an OFFSET relative to the start of that input
1798 // section, return whether or not the output offset is known. If
1799 // this function returns true, it sets *POUTPUT to the output
1802 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1803 off_t
*poutput
) const;
1805 // Write out the data. This does nothing for an input section.
1807 write(Output_file
*);
1809 // Write the data to a buffer. This does nothing for an input
1812 write_to_buffer(unsigned char*);
1815 // Code values which appear in shndx_. If the value is not one of
1816 // these codes, it is the input section index in the object file.
1819 // An Output_section_data.
1820 OUTPUT_SECTION_CODE
= -1U,
1821 // An Output_section_data for an SHF_MERGE section with
1822 // SHF_STRINGS not set.
1823 MERGE_DATA_SECTION_CODE
= -2U,
1824 // An Output_section_data for an SHF_MERGE section with
1826 MERGE_STRING_SECTION_CODE
= -3U
1829 // Whether this is an input section.
1831 is_input_section() const
1833 return (this->shndx_
!= OUTPUT_SECTION_CODE
1834 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1835 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1838 // For an ordinary input section, this is the section index in the
1839 // input file. For an Output_section_data, this is
1840 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1841 // MERGE_STRING_SECTION_CODE.
1842 unsigned int shndx_
;
1843 // The required alignment, stored as a power of 2.
1844 unsigned int p2align_
;
1847 // For an ordinary input section, the section size.
1849 // For OUTPUT_SECTION_CODE, this is not used. For
1850 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1856 // For an ordinary input section, the object which holds the
1859 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1860 // MERGE_STRING_SECTION_CODE, the data.
1861 Output_section_data
* posd
;
1865 typedef std::vector
<Input_section
> Input_section_list
;
1867 // Fill data. This is used to fill in data between input sections.
1868 // When we have to keep track of the input sections, we can use an
1869 // Output_data_const, but we don't want to have to keep track of
1870 // input sections just to implement fills. For a fill we record the
1871 // offset, and the actual data to be written out.
1875 Fill(off_t section_offset
, off_t length
)
1876 : section_offset_(section_offset
), length_(length
)
1879 // Return section offset.
1881 section_offset() const
1882 { return this->section_offset_
; }
1884 // Return fill length.
1887 { return this->length_
; }
1890 // The offset within the output section.
1891 off_t section_offset_
;
1892 // The length of the space to fill.
1896 typedef std::vector
<Fill
> Fill_list
;
1898 // Add a new output section by Input_section.
1900 add_output_section_data(Input_section
*);
1902 // Add an SHF_MERGE input section. Returns true if the section was
1905 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1906 uint64_t entsize
, uint64_t addralign
);
1908 // Add an output SHF_MERGE section POSD to this output section.
1909 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1910 // ENTSIZE is the entity size. This returns the entry added to
1913 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1916 // Most of these fields are only valid after layout.
1918 // The name of the section. This will point into a Stringpool.
1920 // The section address is in the parent class.
1921 // The section alignment.
1922 uint64_t addralign_
;
1923 // The section entry size.
1925 // The file offset is in the parent class.
1926 // Set the section link field to the index of this section.
1927 const Output_data
* link_section_
;
1928 // If link_section_ is NULL, this is the link field.
1930 // Set the section info field to the index of this section.
1931 const Output_data
* info_section_
;
1932 // If info_section_ is NULL, this is the section info field.
1934 // The section type.
1935 const elfcpp::Elf_Word type_
;
1936 // The section flags.
1937 const elfcpp::Elf_Xword flags_
;
1938 // The section index.
1939 unsigned int out_shndx_
;
1940 // If there is a STT_SECTION for this output section in the normal
1941 // symbol table, this is the symbol index. This starts out as zero.
1942 // It is initialized in Layout::finalize() to be the index, or -1U
1943 // if there isn't one.
1944 unsigned int symtab_index_
;
1945 // If there is a STT_SECTION for this output section in the dynamic
1946 // symbol table, this is the symbol index. This starts out as zero.
1947 // It is initialized in Layout::finalize() to be the index, or -1U
1948 // if there isn't one.
1949 unsigned int dynsym_index_
;
1950 // The input sections. This will be empty in cases where we don't
1951 // need to keep track of them.
1952 Input_section_list input_sections_
;
1953 // The offset of the first entry in input_sections_.
1954 off_t first_input_offset_
;
1955 // The fill data. This is separate from input_sections_ because we
1956 // often will need fill sections without needing to keep track of
1959 // If the section requires postprocessing, this buffer holds the
1960 // section contents during relocation.
1961 unsigned char* postprocessing_buffer_
;
1962 // Whether this output section needs a STT_SECTION symbol in the
1963 // normal symbol table. This will be true if there is a relocation
1965 bool needs_symtab_index_
: 1;
1966 // Whether this output section needs a STT_SECTION symbol in the
1967 // dynamic symbol table. This will be true if there is a dynamic
1968 // relocation which needs it.
1969 bool needs_dynsym_index_
: 1;
1970 // Whether the link field of this output section should point to the
1971 // normal symbol table.
1972 bool should_link_to_symtab_
: 1;
1973 // Whether the link field of this output section should point to the
1974 // dynamic symbol table.
1975 bool should_link_to_dynsym_
: 1;
1976 // Whether this section should be written after all the input
1977 // sections are complete.
1978 bool after_input_sections_
: 1;
1979 // Whether this section requires post processing after all
1980 // relocations have been applied.
1981 bool requires_postprocessing_
: 1;
1982 // For SHT_TLS sections, the offset of this section relative to the base
1983 // of the TLS segment.
1984 uint64_t tls_offset_
;
1987 // An output segment. PT_LOAD segments are built from collections of
1988 // output sections. Other segments typically point within PT_LOAD
1989 // segments, and are built directly as needed.
1991 class Output_segment
1994 // Create an output segment, specifying the type and flags.
1995 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1997 // Return the virtual address.
2000 { return this->vaddr_
; }
2002 // Return the physical address.
2005 { return this->paddr_
; }
2007 // Return the segment type.
2010 { return this->type_
; }
2012 // Return the segment flags.
2015 { return this->flags_
; }
2017 // Return the memory size.
2020 { return this->memsz_
; }
2022 // Return the file size.
2025 { return this->filesz_
; }
2027 // Return the maximum alignment of the Output_data.
2031 // Add an Output_section to this segment.
2033 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2034 { this->add_output_section(os
, seg_flags
, false); }
2036 // Add an Output_section to the start of this segment.
2038 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
2039 { this->add_output_section(os
, seg_flags
, true); }
2041 // Add an Output_data (which is not an Output_section) to the start
2044 add_initial_output_data(Output_data
*);
2046 // Return the number of dynamic relocations applied to this segment.
2048 dynamic_reloc_count() const;
2050 // Set the address of the segment to ADDR and the offset to *POFF
2051 // (aligned if necessary), and set the addresses and offsets of all
2052 // contained output sections accordingly. Set the section indexes
2053 // of all contained output sections starting with *PSHNDX. Return
2054 // the address of the immediately following segment. Update *POFF
2055 // and *PSHNDX. This should only be called for a PT_LOAD segment.
2057 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
2059 // Set the minimum alignment of this segment. This may be adjusted
2060 // upward based on the section alignments.
2062 set_minimum_addralign(uint64_t align
)
2064 gold_assert(!this->is_align_known_
);
2065 this->align_
= align
;
2068 // Set the offset of this segment based on the section. This should
2069 // only be called for a non-PT_LOAD segment.
2073 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2077 // Return the number of output sections.
2079 output_section_count() const;
2081 // Write the segment header into *OPHDR.
2082 template<int size
, bool big_endian
>
2084 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
2086 // Write the section headers of associated sections into V.
2087 template<int size
, bool big_endian
>
2089 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
2090 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
2093 Output_segment(const Output_segment
&);
2094 Output_segment
& operator=(const Output_segment
&);
2096 typedef std::list
<Output_data
*> Output_data_list
;
2098 // Add an Output_section to this segment, specifying front or back.
2100 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
2103 // Find the maximum alignment in an Output_data_list.
2105 maximum_alignment(const Output_data_list
*);
2107 // Set the section addresses in an Output_data_list.
2109 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
2110 unsigned int* pshndx
);
2112 // Return the number of Output_sections in an Output_data_list.
2114 output_section_count_list(const Output_data_list
*) const;
2116 // Return the number of dynamic relocs in an Output_data_list.
2118 dynamic_reloc_count_list(const Output_data_list
*) const;
2120 // Write the section headers in the list into V.
2121 template<int size
, bool big_endian
>
2123 write_section_headers_list(const Layout
*, const Stringpool
*,
2124 const Output_data_list
*, unsigned char* v
,
2125 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
2127 // The list of output data with contents attached to this segment.
2128 Output_data_list output_data_
;
2129 // The list of output data without contents attached to this segment.
2130 Output_data_list output_bss_
;
2131 // The segment virtual address.
2133 // The segment physical address.
2135 // The size of the segment in memory.
2137 // The segment alignment. The is_align_known_ field indicates
2138 // whether this has been finalized. It can be set to a minimum
2139 // value before it is finalized.
2141 // The offset of the segment data within the file.
2143 // The size of the segment data in the file.
2145 // The segment type;
2146 elfcpp::Elf_Word type_
;
2147 // The segment flags.
2148 elfcpp::Elf_Word flags_
;
2149 // Whether we have finalized align_.
2150 bool is_align_known_
;
2153 // This class represents the output file.
2158 Output_file(const General_options
& options
, Target
*);
2160 // Get a pointer to the target.
2163 { return this->target_
; }
2165 // Open the output file. FILE_SIZE is the final size of the file.
2167 open(off_t file_size
);
2169 // Resize the output file.
2171 resize(off_t file_size
);
2173 // Close the output file (flushing all buffered data) and make sure
2174 // there are no errors.
2178 // We currently always use mmap which makes the view handling quite
2179 // simple. In the future we may support other approaches.
2181 // Write data to the output file.
2183 write(off_t offset
, const void* data
, off_t len
)
2184 { memcpy(this->base_
+ offset
, data
, len
); }
2186 // Get a buffer to use to write to the file, given the offset into
2187 // the file and the size.
2189 get_output_view(off_t start
, off_t size
)
2191 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
2192 return this->base_
+ start
;
2195 // VIEW must have been returned by get_output_view. Write the
2196 // buffer to the file, passing in the offset and the size.
2198 write_output_view(off_t
, off_t
, unsigned char*)
2201 // Get a read/write buffer. This is used when we want to write part
2202 // of the file, read it in, and write it again.
2204 get_input_output_view(off_t start
, off_t size
)
2205 { return this->get_output_view(start
, size
); }
2207 // Write a read/write buffer back to the file.
2209 write_input_output_view(off_t
, off_t
, unsigned char*)
2212 // Get a read buffer. This is used when we just want to read part
2213 // of the file back it in.
2214 const unsigned char*
2215 get_input_view(off_t start
, off_t size
)
2216 { return this->get_output_view(start
, size
); }
2218 // Release a read bfufer.
2220 free_input_view(off_t
, off_t
, const unsigned char*)
2224 // Map the file into memory and return a pointer to the map.
2228 // Unmap the file from memory (and flush to disk buffers).
2234 const General_options
& options_
;
2243 // Base of file mapped into memory.
2244 unsigned char* base_
;
2245 // True iff base_ points to a memory buffer rather than an output file.
2246 bool map_is_anonymous_
;
2249 } // End namespace gold.
2251 #endif // !defined(GOLD_OUTPUT_H)