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(off_t data_size
= 0)
53 : address_(0), data_size_(data_size
), offset_(-1),
54 dynamic_reloc_count_(0)
60 // Return the address. This is only valid after Layout::finalize is
64 { return this->address_
; }
66 // Return the size of the data. This must be valid after
67 // Layout::finalize calls set_address, but need not be valid before
71 { return this->data_size_
; }
73 // Return the file offset. This is only valid after
74 // Layout::finalize is finished.
77 { return this->offset_
; }
79 // Return the required alignment.
82 { return this->do_addralign(); }
84 // Return whether this is an Output_section.
87 { return this->do_is_section(); }
89 // Return whether this is an Output_section of the specified type.
91 is_section_type(elfcpp::Elf_Word stt
) const
92 { return this->do_is_section_type(stt
); }
94 // Return whether this is an Output_section with the specified flag
97 is_section_flag_set(elfcpp::Elf_Xword shf
) const
98 { return this->do_is_section_flag_set(shf
); }
100 // Return the output section index, if there is an output section.
103 { return this->do_out_shndx(); }
105 // Set the output section index, if this is an output section.
107 set_out_shndx(unsigned int shndx
)
108 { this->do_set_out_shndx(shndx
); }
110 // Set the address and file offset of this data. This is called
111 // during Layout::finalize.
113 set_address(uint64_t addr
, off_t off
);
115 // Write the data to the output file. This is called after
116 // Layout::finalize is complete.
118 write(Output_file
* file
)
119 { this->do_write(file
); }
121 // This is called by Layout::finalize to note that all sizes must
125 { Output_data::sizes_are_fixed
= true; }
127 // Used to check that layout has been done.
130 { return Output_data::sizes_are_fixed
; }
132 // Count the number of dynamic relocations applied to this section.
135 { ++this->dynamic_reloc_count_
; }
137 // Return the number of dynamic relocations applied to this section.
139 dynamic_reloc_count() const
140 { return this->dynamic_reloc_count_
; }
143 // Functions that child classes may or in some cases must implement.
145 // Write the data to the output file.
147 do_write(Output_file
*) = 0;
149 // Return the required alignment.
151 do_addralign() const = 0;
153 // Return whether this is an Output_section.
155 do_is_section() const
158 // Return whether this is an Output_section of the specified type.
159 // This only needs to be implement by Output_section.
161 do_is_section_type(elfcpp::Elf_Word
) const
164 // Return whether this is an Output_section with the specific flag
165 // set. This only needs to be implemented by Output_section.
167 do_is_section_flag_set(elfcpp::Elf_Xword
) const
170 // Return the output section index, if there is an output section.
173 { gold_unreachable(); }
175 // Set the output section index, if this is an output section.
177 do_set_out_shndx(unsigned int)
178 { gold_unreachable(); }
180 // Set the address and file offset of the data. This only needs to
181 // be implemented if the child needs to know. The child class can
182 // set its size in this call.
184 do_set_address(uint64_t, off_t
)
187 // Functions that child classes may call.
189 // Set the size of the data.
191 set_data_size(off_t data_size
)
193 gold_assert(!Output_data::sizes_are_fixed
);
194 this->data_size_
= data_size
;
197 // Return default alignment for the target size.
201 // Return default alignment for a specified size--32 or 64.
203 default_alignment_for_size(int size
);
206 Output_data(const Output_data
&);
207 Output_data
& operator=(const Output_data
&);
209 // This is used for verification, to make sure that we don't try to
210 // change any sizes after we set the section addresses.
211 static bool sizes_are_fixed
;
213 // Memory address in file (not always meaningful).
215 // Size of data in file.
217 // Offset within file.
219 // Count of dynamic relocations applied to this section.
220 unsigned int dynamic_reloc_count_
;
223 // Output the section headers.
225 class Output_section_headers
: public Output_data
228 Output_section_headers(const Layout
*,
229 const Layout::Segment_list
*,
230 const Layout::Section_list
*,
233 // Write the data to the file.
235 do_write(Output_file
*);
237 // Return the required alignment.
240 { return Output_data::default_alignment(); }
243 // Write the data to the file with the right size and endianness.
244 template<int size
, bool big_endian
>
246 do_sized_write(Output_file
*);
248 const Layout
* layout_
;
249 const Layout::Segment_list
* segment_list_
;
250 const Layout::Section_list
* unattached_section_list_
;
251 const Stringpool
* secnamepool_
;
254 // Output the segment headers.
256 class Output_segment_headers
: public Output_data
259 Output_segment_headers(const Layout::Segment_list
& segment_list
);
261 // Write the data to the file.
263 do_write(Output_file
*);
265 // Return the required alignment.
268 { return Output_data::default_alignment(); }
271 // Write the data to the file with the right size and endianness.
272 template<int size
, bool big_endian
>
274 do_sized_write(Output_file
*);
276 const Layout::Segment_list
& segment_list_
;
279 // Output the ELF file header.
281 class Output_file_header
: public Output_data
284 Output_file_header(const Target
*,
286 const Output_segment_headers
*);
288 // Add information about the section headers. We lay out the ELF
289 // file header before we create the section headers.
290 void set_section_info(const Output_section_headers
*,
291 const Output_section
* shstrtab
);
293 // Write the data to the file.
295 do_write(Output_file
*);
297 // Return the required alignment.
300 { return Output_data::default_alignment(); }
302 // Set the address and offset--we only implement this for error
305 do_set_address(uint64_t, off_t off
) const
306 { gold_assert(off
== 0); }
309 // Write the data to the file with the right size and endianness.
310 template<int size
, bool big_endian
>
312 do_sized_write(Output_file
*);
314 const Target
* target_
;
315 const Symbol_table
* symtab_
;
316 const Output_segment_headers
* segment_header_
;
317 const Output_section_headers
* section_header_
;
318 const Output_section
* shstrtab_
;
321 // Output sections are mainly comprised of input sections. However,
322 // there are cases where we have data to write out which is not in an
323 // input section. Output_section_data is used in such cases. This is
324 // an abstract base class.
326 class Output_section_data
: public Output_data
329 Output_section_data(off_t data_size
, uint64_t addralign
)
330 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
333 Output_section_data(uint64_t addralign
)
334 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
337 // Return the output section.
338 const Output_section
*
339 output_section() const
340 { return this->output_section_
; }
342 // Record the output section.
344 set_output_section(Output_section
* os
);
346 // Add an input section, for SHF_MERGE sections. This returns true
347 // if the section was handled.
349 add_input_section(Relobj
* object
, unsigned int shndx
)
350 { return this->do_add_input_section(object
, shndx
); }
352 // Given an input OBJECT, an input section index SHNDX within that
353 // object, and an OFFSET relative to the start of that input
354 // section, return whether or not the corresponding offset within
355 // the output section is known. If this function returns true, it
356 // sets *POUTPUT to the output offset. The value -1 indicates that
357 // this input offset is being discarded.
359 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
360 off_t
*poutput
) const
361 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
364 // The child class must implement do_write.
366 // The child class may implement specific adjustments to the output
369 do_adjust_output_section(Output_section
*)
372 // May be implemented by child class. Return true if the section
375 do_add_input_section(Relobj
*, unsigned int)
376 { gold_unreachable(); }
378 // The child class may implement output_offset.
380 do_output_offset(const Relobj
*, unsigned int, off_t
, off_t
*) const
383 // Return the required alignment.
386 { return this->addralign_
; }
388 // Return the section index of the output section.
390 do_out_shndx() const;
392 // Set the alignment.
394 set_addralign(uint64_t addralign
)
395 { this->addralign_
= addralign
; }
398 // The output section for this section.
399 const Output_section
* output_section_
;
400 // The required alignment.
404 // A simple case of Output_data in which we have constant data to
407 class Output_data_const
: public Output_section_data
410 Output_data_const(const std::string
& data
, uint64_t addralign
)
411 : Output_section_data(data
.size(), addralign
), data_(data
)
414 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
415 : Output_section_data(len
, addralign
), data_(p
, len
)
418 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
419 : Output_section_data(len
, addralign
),
420 data_(reinterpret_cast<const char*>(p
), len
)
425 add_data(const std::string
& add
)
427 this->data_
.append(add
);
428 this->set_data_size(this->data_
.size());
431 // Write the data to the output file.
433 do_write(Output_file
*);
439 // Another version of Output_data with constant data, in which the
440 // buffer is allocated by the caller.
442 class Output_data_const_buffer
: public Output_section_data
445 Output_data_const_buffer(const unsigned char* p
, off_t len
,
447 : Output_section_data(len
, addralign
), p_(p
)
450 // Write the data the output file.
452 do_write(Output_file
*);
455 const unsigned char* p_
;
458 // A place holder for data written out via some other mechanism.
460 class Output_data_space
: public Output_section_data
463 Output_data_space(off_t data_size
, uint64_t addralign
)
464 : Output_section_data(data_size
, addralign
)
467 explicit Output_data_space(uint64_t addralign
)
468 : Output_section_data(addralign
)
473 set_space_size(off_t space_size
)
474 { this->set_data_size(space_size
); }
476 // Set the alignment.
478 set_space_alignment(uint64_t align
)
479 { this->set_addralign(align
); }
481 // Write out the data--this must be handled elsewhere.
483 do_write(Output_file
*)
487 // A string table which goes into an output section.
489 class Output_data_strtab
: public Output_section_data
492 Output_data_strtab(Stringpool
* strtab
)
493 : Output_section_data(1), strtab_(strtab
)
496 // This is called to set the address and file offset. Here we make
497 // sure that the Stringpool is finalized.
499 do_set_address(uint64_t, off_t
);
501 // Write out the data.
503 do_write(Output_file
*);
509 // This POD class is used to represent a single reloc in the output
510 // file. This could be a private class within Output_data_reloc, but
511 // the templatization is complex enough that I broke it out into a
512 // separate class. The class is templatized on either elfcpp::SHT_REL
513 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
514 // relocation or an ordinary relocation.
516 // A relocation can be against a global symbol, a local symbol, an
517 // output section, or the undefined symbol at index 0. We represent
518 // the latter by using a NULL global symbol.
520 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
523 template<bool dynamic
, int size
, bool big_endian
>
524 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
527 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
529 // An uninitialized entry. We need this because we want to put
530 // instances of this class into an STL container.
532 : local_sym_index_(INVALID_CODE
)
535 // A reloc against a global symbol.
537 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
539 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
542 this->u1_
.gsym
= gsym
;
546 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
547 unsigned int shndx
, Address address
)
548 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
551 gold_assert(shndx
!= INVALID_CODE
);
552 this->u1_
.gsym
= gsym
;
553 this->u2_
.relobj
= relobj
;
556 // A reloc against a local symbol.
558 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
559 unsigned int local_sym_index
,
563 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
566 gold_assert(local_sym_index
!= GSYM_CODE
567 && local_sym_index
!= INVALID_CODE
);
568 this->u1_
.relobj
= relobj
;
572 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
573 unsigned int local_sym_index
,
577 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
580 gold_assert(local_sym_index
!= GSYM_CODE
581 && local_sym_index
!= INVALID_CODE
);
582 gold_assert(shndx
!= INVALID_CODE
);
583 this->u1_
.relobj
= relobj
;
584 this->u2_
.relobj
= relobj
;
587 // A reloc against the STT_SECTION symbol of an output section.
589 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
591 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
598 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
599 unsigned int shndx
, Address address
)
600 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
603 gold_assert(shndx
!= INVALID_CODE
);
605 this->u2_
.relobj
= relobj
;
608 // Write the reloc entry to an output view.
610 write(unsigned char* pov
) const;
612 // Write the offset and info fields to Write_rel.
613 template<typename Write_rel
>
614 void write_rel(Write_rel
*) const;
617 // Return the symbol index. We can't do a double template
618 // specialization, so we do a secondary template here.
620 get_symbol_index() const;
622 // Codes for local_sym_index_.
629 // Invalid uninitialized entry.
635 // For a local symbol, the object. We will never generate a
636 // relocation against a local symbol in a dynamic object; that
637 // doesn't make sense. And our callers will always be
638 // templatized, so we use Sized_relobj here.
639 Sized_relobj
<size
, big_endian
>* relobj
;
640 // For a global symbol, the symbol. If this is NULL, it indicates
641 // a relocation against the undefined 0 symbol.
643 // For a relocation against an output section, the output section.
648 // If shndx_ is not INVALID CODE, the object which holds the input
649 // section being used to specify the reloc address.
651 // If shndx_ is INVALID_CODE, the output data being used to
652 // specify the reloc address. This may be NULL if the reloc
653 // address is absolute.
656 // The address offset within the input section or the Output_data.
658 // For a local symbol, the local symbol index. This is GSYM_CODE
659 // for a global symbol, or INVALID_CODE for an uninitialized value.
660 unsigned int local_sym_index_
;
661 // The reloc type--a processor specific code.
663 // If the reloc address is an input section in an object, the
664 // section index. This is INVALID_CODE if the reloc address is
665 // specified in some other way.
669 // The SHT_RELA version of Output_reloc<>. This is just derived from
670 // the SHT_REL version of Output_reloc, but it adds an addend.
672 template<bool dynamic
, int size
, bool big_endian
>
673 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
676 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
677 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
679 // An uninitialized entry.
684 // A reloc against a global symbol.
686 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
687 Address address
, Addend addend
)
688 : rel_(gsym
, type
, od
, address
), addend_(addend
)
691 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
692 unsigned int shndx
, Address address
, Addend addend
)
693 : rel_(gsym
, type
, relobj
, shndx
, address
), addend_(addend
)
696 // A reloc against a local symbol.
698 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
699 unsigned int local_sym_index
,
700 unsigned int type
, Output_data
* od
, Address address
,
702 : rel_(relobj
, local_sym_index
, type
, od
, address
), addend_(addend
)
705 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
706 unsigned int local_sym_index
,
711 : rel_(relobj
, local_sym_index
, type
, shndx
, address
),
715 // A reloc against the STT_SECTION symbol of an output section.
717 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
718 Address address
, Addend addend
)
719 : rel_(os
, type
, od
, address
), addend_(addend
)
722 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
723 unsigned int shndx
, Address address
, Addend addend
)
724 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
727 // Write the reloc entry to an output view.
729 write(unsigned char* pov
) const;
733 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
738 // Output_data_reloc is used to manage a section containing relocs.
739 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
740 // indicates whether this is a dynamic relocation or a normal
741 // relocation. Output_data_reloc_base is a base class.
742 // Output_data_reloc is the real class, which we specialize based on
745 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
746 class Output_data_reloc_base
: public Output_section_data
749 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
750 typedef typename
Output_reloc_type::Address Address
;
751 static const int reloc_size
=
752 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
754 // Construct the section.
755 Output_data_reloc_base()
756 : Output_section_data(Output_data::default_alignment_for_size(size
))
759 // Write out the data.
761 do_write(Output_file
*);
764 // Set the entry size and the link.
766 do_adjust_output_section(Output_section
*os
);
768 // Add a relocation entry.
770 add(Output_data
*od
, const Output_reloc_type
& reloc
)
772 this->relocs_
.push_back(reloc
);
773 this->set_data_size(this->relocs_
.size() * reloc_size
);
774 od
->add_dynamic_reloc();
778 typedef std::vector
<Output_reloc_type
> Relocs
;
783 // The class which callers actually create.
785 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
786 class Output_data_reloc
;
788 // The SHT_REL version of Output_data_reloc.
790 template<bool dynamic
, int size
, bool big_endian
>
791 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
792 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
795 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
799 typedef typename
Base::Output_reloc_type Output_reloc_type
;
800 typedef typename
Output_reloc_type::Address Address
;
803 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
806 // Add a reloc against a global symbol.
809 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
810 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
)); }
813 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
814 unsigned int shndx
, Address address
)
815 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
)); }
817 // Add a reloc against a local symbol.
820 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
821 unsigned int local_sym_index
, unsigned int type
,
822 Output_data
* od
, Address address
)
823 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
827 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
828 unsigned int local_sym_index
, unsigned int type
,
829 Output_data
* od
, unsigned int shndx
, Address address
)
830 { this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
834 // A reloc against the STT_SECTION symbol of an output section.
835 // OS is the Output_section that the relocation refers to; OD is
836 // the Output_data object being relocated.
839 add_output_section(Output_section
* os
, unsigned int type
,
840 Output_data
* od
, Address address
)
841 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
844 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
845 Relobj
* relobj
, unsigned int shndx
, Address address
)
846 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
849 // The SHT_RELA version of Output_data_reloc.
851 template<bool dynamic
, int size
, bool big_endian
>
852 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
853 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
856 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
860 typedef typename
Base::Output_reloc_type Output_reloc_type
;
861 typedef typename
Output_reloc_type::Address Address
;
862 typedef typename
Output_reloc_type::Addend Addend
;
865 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
868 // Add a reloc against a global symbol.
871 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
872 Address address
, Addend addend
)
873 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
876 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Relobj
* relobj
,
877 unsigned int shndx
, Address address
,
879 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
882 // Add a reloc against a local symbol.
885 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
886 unsigned int local_sym_index
, unsigned int type
,
887 Output_data
* od
, Address address
, Addend addend
)
889 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
894 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
895 unsigned int local_sym_index
, unsigned int type
,
896 Output_data
* od
, unsigned int shndx
, Address address
,
899 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
903 // A reloc against the STT_SECTION symbol of an output section.
906 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
907 Address address
, Addend addend
)
908 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
911 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
912 unsigned int shndx
, Address address
, Addend addend
)
913 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
917 // Output_data_got is used to manage a GOT. Each entry in the GOT is
918 // for one symbol--either a global symbol or a local symbol in an
919 // object. The target specific code adds entries to the GOT as
922 template<int size
, bool big_endian
>
923 class Output_data_got
: public Output_section_data
926 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
929 : Output_section_data(Output_data::default_alignment_for_size(size
)),
933 // Add an entry for a global symbol to the GOT. Return true if this
934 // is a new GOT entry, false if the symbol was already in the GOT.
936 add_global(Symbol
* gsym
);
938 // Add an entry for a local symbol to the GOT. This returns true if
939 // this is a new GOT entry, false if the symbol already has a GOT
942 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
944 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
945 // Return true if this is a new GOT entry, false if the symbol was
946 // already in the GOT.
948 add_global_tls(Symbol
* gsym
, bool need_pair
);
950 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
951 // This returns true if this is a new GOT entry, false if the symbol
952 // already has a GOT entry.
954 add_local_tls(Sized_relobj
<size
, big_endian
>* object
,
955 unsigned int sym_index
, bool need_pair
);
957 // Add a constant to the GOT. This returns the offset of the new
958 // entry from the start of the GOT.
960 add_constant(Valtype constant
)
962 this->entries_
.push_back(Got_entry(constant
));
963 this->set_got_size();
964 return this->last_got_offset();
967 // Write out the GOT table.
969 do_write(Output_file
*);
972 // This POD class holds a single GOT entry.
976 // Create a zero entry.
978 : local_sym_index_(CONSTANT_CODE
)
979 { this->u_
.constant
= 0; }
981 // Create a global symbol entry.
982 explicit Got_entry(Symbol
* gsym
)
983 : local_sym_index_(GSYM_CODE
)
984 { this->u_
.gsym
= gsym
; }
986 // Create a local symbol entry.
987 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
988 unsigned int local_sym_index
)
989 : local_sym_index_(local_sym_index
)
991 gold_assert(local_sym_index
!= GSYM_CODE
992 && local_sym_index
!= CONSTANT_CODE
);
993 this->u_
.object
= object
;
996 // Create a constant entry. The constant is a host value--it will
997 // be swapped, if necessary, when it is written out.
998 explicit Got_entry(Valtype constant
)
999 : local_sym_index_(CONSTANT_CODE
)
1000 { this->u_
.constant
= constant
; }
1002 // Write the GOT entry to an output view.
1004 write(unsigned char* pov
) const;
1015 // For a local symbol, the object.
1016 Sized_relobj
<size
, big_endian
>* object
;
1017 // For a global symbol, the symbol.
1019 // For a constant, the constant.
1022 // For a local symbol, the local symbol index. This is GSYM_CODE
1023 // for a global symbol, or CONSTANT_CODE for a constant.
1024 unsigned int local_sym_index_
;
1027 typedef std::vector
<Got_entry
> Got_entries
;
1029 // Return the offset into the GOT of GOT entry I.
1031 got_offset(unsigned int i
) const
1032 { return i
* (size
/ 8); }
1034 // Return the offset into the GOT of the last entry added.
1036 last_got_offset() const
1037 { return this->got_offset(this->entries_
.size() - 1); }
1039 // Set the size of the section.
1042 { this->set_data_size(this->got_offset(this->entries_
.size())); }
1044 // The list of GOT entries.
1045 Got_entries entries_
;
1048 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1051 class Output_data_dynamic
: public Output_section_data
1054 Output_data_dynamic(Stringpool
* pool
)
1055 : Output_section_data(Output_data::default_alignment()),
1056 entries_(), pool_(pool
)
1059 // Add a new dynamic entry with a fixed numeric value.
1061 add_constant(elfcpp::DT tag
, unsigned int val
)
1062 { this->add_entry(Dynamic_entry(tag
, val
)); }
1064 // Add a new dynamic entry with the address of output data.
1066 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1067 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1069 // Add a new dynamic entry with the size of output data.
1071 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1072 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1074 // Add a new dynamic entry with the address of a symbol.
1076 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1077 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1079 // Add a new dynamic entry with a string.
1081 add_string(elfcpp::DT tag
, const char* str
)
1082 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1085 add_string(elfcpp::DT tag
, const std::string
& str
)
1086 { this->add_string(tag
, str
.c_str()); }
1088 // Set the final data size.
1090 do_set_address(uint64_t, off_t
);
1092 // Write out the dynamic entries.
1094 do_write(Output_file
*);
1097 // Adjust the output section to set the entry size.
1099 do_adjust_output_section(Output_section
*);
1102 // This POD class holds a single dynamic entry.
1106 // Create an entry with a fixed numeric value.
1107 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1108 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1109 { this->u_
.val
= val
; }
1111 // Create an entry with the size or address of a section.
1112 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1114 classification_(section_size
1115 ? DYNAMIC_SECTION_SIZE
1116 : DYNAMIC_SECTION_ADDRESS
)
1117 { this->u_
.od
= od
; }
1119 // Create an entry with the address of a symbol.
1120 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1121 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1122 { this->u_
.sym
= sym
; }
1124 // Create an entry with a string.
1125 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1126 : tag_(tag
), classification_(DYNAMIC_STRING
)
1127 { this->u_
.str
= str
; }
1129 // Write the dynamic entry to an output view.
1130 template<int size
, bool big_endian
>
1132 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1140 DYNAMIC_SECTION_ADDRESS
,
1142 DYNAMIC_SECTION_SIZE
,
1151 // For DYNAMIC_NUMBER.
1153 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1154 const Output_data
* od
;
1155 // For DYNAMIC_SYMBOL.
1157 // For DYNAMIC_STRING.
1162 // The type of entry.
1163 Classification classification_
;
1166 // Add an entry to the list.
1168 add_entry(const Dynamic_entry
& entry
)
1169 { this->entries_
.push_back(entry
); }
1171 // Sized version of write function.
1172 template<int size
, bool big_endian
>
1174 sized_write(Output_file
* of
);
1176 // The type of the list of entries.
1177 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1180 Dynamic_entries entries_
;
1181 // The pool used for strings.
1185 // An output section. We don't expect to have too many output
1186 // sections, so we don't bother to do a template on the size.
1188 class Output_section
: public Output_data
1191 // Create an output section, giving the name, type, and flags.
1192 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1193 virtual ~Output_section();
1195 // Add a new input section SHNDX, named NAME, with header SHDR, from
1196 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1197 // which applies to this section, or 0 if none, or -1U if more than
1198 // one. Return the offset within the output section.
1199 template<int size
, bool big_endian
>
1201 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1203 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1204 unsigned int reloc_shndx
);
1206 // Add generated data POSD to this output section.
1208 add_output_section_data(Output_section_data
* posd
);
1210 // Return the section name.
1213 { return this->name_
; }
1215 // Return the section type.
1218 { return this->type_
; }
1220 // Return the section flags.
1223 { return this->flags_
; }
1225 // Return the section index in the output file.
1227 do_out_shndx() const
1229 gold_assert(this->out_shndx_
!= -1U);
1230 return this->out_shndx_
;
1233 // Set the output section index.
1235 do_set_out_shndx(unsigned int shndx
)
1237 gold_assert(this->out_shndx_
== -1U);
1238 this->out_shndx_
= shndx
;
1241 // Return the entsize field.
1244 { return this->entsize_
; }
1246 // Set the entsize field.
1248 set_entsize(uint64_t v
);
1250 // Set the link field to the output section index of a section.
1252 set_link_section(const Output_data
* od
)
1254 gold_assert(this->link_
== 0
1255 && !this->should_link_to_symtab_
1256 && !this->should_link_to_dynsym_
);
1257 this->link_section_
= od
;
1260 // Set the link field to a constant.
1262 set_link(unsigned int v
)
1264 gold_assert(this->link_section_
== NULL
1265 && !this->should_link_to_symtab_
1266 && !this->should_link_to_dynsym_
);
1270 // Record that this section should link to the normal symbol table.
1272 set_should_link_to_symtab()
1274 gold_assert(this->link_section_
== NULL
1276 && !this->should_link_to_dynsym_
);
1277 this->should_link_to_symtab_
= true;
1280 // Record that this section should link to the dynamic symbol table.
1282 set_should_link_to_dynsym()
1284 gold_assert(this->link_section_
== NULL
1286 && !this->should_link_to_symtab_
);
1287 this->should_link_to_dynsym_
= true;
1290 // Return the info field.
1294 gold_assert(this->info_section_
== NULL
);
1298 // Set the info field to the output section index of a section.
1300 set_info_section(const Output_data
* od
)
1302 gold_assert(this->info_
== 0);
1303 this->info_section_
= od
;
1306 // Set the info field to a constant.
1308 set_info(unsigned int v
)
1310 gold_assert(this->info_section_
== NULL
);
1314 // Set the addralign field.
1316 set_addralign(uint64_t v
)
1317 { this->addralign_
= v
; }
1319 // Indicate that we need a symtab index.
1321 set_needs_symtab_index()
1322 { this->needs_symtab_index_
= true; }
1324 // Return whether we need a symtab index.
1326 needs_symtab_index() const
1327 { return this->needs_symtab_index_
; }
1329 // Get the symtab index.
1331 symtab_index() const
1333 gold_assert(this->symtab_index_
!= 0);
1334 return this->symtab_index_
;
1337 // Set the symtab index.
1339 set_symtab_index(unsigned int index
)
1341 gold_assert(index
!= 0);
1342 this->symtab_index_
= index
;
1345 // Indicate that we need a dynsym index.
1347 set_needs_dynsym_index()
1348 { this->needs_dynsym_index_
= true; }
1350 // Return whether we need a dynsym index.
1352 needs_dynsym_index() const
1353 { return this->needs_dynsym_index_
; }
1355 // Get the dynsym index.
1357 dynsym_index() const
1359 gold_assert(this->dynsym_index_
!= 0);
1360 return this->dynsym_index_
;
1363 // Set the dynsym index.
1365 set_dynsym_index(unsigned int index
)
1367 gold_assert(index
!= 0);
1368 this->dynsym_index_
= index
;
1371 // Return whether this section should be written after all the input
1372 // sections are complete.
1374 after_input_sections() const
1375 { return this->after_input_sections_
; }
1377 // Record that this section should be written after all the input
1378 // sections are complete.
1380 set_after_input_sections()
1381 { this->after_input_sections_
= true; }
1383 // Return whether the offset OFFSET in the input section SHNDX in
1384 // object OBJECT is being included in the link.
1386 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1387 off_t offset
) const;
1389 // Return the offset within the output section of OFFSET relative to
1390 // the start of input section SHNDX in object OBJECT.
1392 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
) const;
1394 // Return the output virtual address of OFFSET relative to the start
1395 // of input section SHNDX in object OBJECT.
1397 output_address(const Relobj
* object
, unsigned int shndx
,
1398 off_t offset
) const;
1400 // Set the address of the Output_section. For a typical
1401 // Output_section, there is nothing to do, but if there are any
1402 // Output_section_data objects we need to set the final addresses
1405 do_set_address(uint64_t, off_t
);
1407 // Write the data to the file. For a typical Output_section, this
1408 // does nothing: the data is written out by calling Object::Relocate
1409 // on each input object. But if there are any Output_section_data
1410 // objects we do need to write them out here.
1412 do_write(Output_file
*);
1414 // Return the address alignment--function required by parent class.
1416 do_addralign() const
1417 { return this->addralign_
; }
1419 // Return whether this is an Output_section.
1421 do_is_section() const
1424 // Return whether this is a section of the specified type.
1426 do_is_section_type(elfcpp::Elf_Word type
) const
1427 { return this->type_
== type
; }
1429 // Return whether the specified section flag is set.
1431 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1432 { return (this->flags_
& flag
) != 0; }
1434 // Write the section header into *OPHDR.
1435 template<int size
, bool big_endian
>
1437 write_header(const Layout
*, const Stringpool
*,
1438 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1441 // In some cases we need to keep a list of the input sections
1442 // associated with this output section. We only need the list if we
1443 // might have to change the offsets of the input section within the
1444 // output section after we add the input section. The ordinary
1445 // input sections will be written out when we process the object
1446 // file, and as such we don't need to track them here. We do need
1447 // to track Output_section_data objects here. We store instances of
1448 // this structure in a std::vector, so it must be a POD. There can
1449 // be many instances of this structure, so we use a union to save
1455 : shndx_(0), p2align_(0)
1457 this->u1_
.data_size
= 0;
1458 this->u2_
.object
= NULL
;
1461 // For an ordinary input section.
1462 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1465 p2align_(ffsll(static_cast<long long>(addralign
)))
1467 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1468 && shndx
!= MERGE_DATA_SECTION_CODE
1469 && shndx
!= MERGE_STRING_SECTION_CODE
);
1470 this->u1_
.data_size
= data_size
;
1471 this->u2_
.object
= object
;
1474 // For a non-merge output section.
1475 Input_section(Output_section_data
* posd
)
1476 : shndx_(OUTPUT_SECTION_CODE
),
1477 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1479 this->u1_
.data_size
= 0;
1480 this->u2_
.posd
= posd
;
1483 // For a merge section.
1484 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1486 ? MERGE_STRING_SECTION_CODE
1487 : MERGE_DATA_SECTION_CODE
),
1488 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1490 this->u1_
.entsize
= entsize
;
1491 this->u2_
.posd
= posd
;
1494 // The required alignment.
1498 return (this->p2align_
== 0
1500 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1503 // Return the required size.
1507 // Return whether this is a merge section which matches the
1510 is_merge_section(bool is_string
, uint64_t entsize
,
1511 uint64_t addralign
) const
1513 return (this->shndx_
== (is_string
1514 ? MERGE_STRING_SECTION_CODE
1515 : MERGE_DATA_SECTION_CODE
)
1516 && this->u1_
.entsize
== entsize
1517 && this->addralign() == addralign
);
1520 // Set the output section.
1522 set_output_section(Output_section
* os
)
1524 gold_assert(!this->is_input_section());
1525 this->u2_
.posd
->set_output_section(os
);
1528 // Set the address and file offset. This is called during
1529 // Layout::finalize. SECOFF is the file offset of the enclosing
1532 set_address(uint64_t addr
, off_t off
, off_t secoff
);
1534 // Add an input section, for SHF_MERGE sections.
1536 add_input_section(Relobj
* object
, unsigned int shndx
)
1538 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1539 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1540 return this->u2_
.posd
->add_input_section(object
, shndx
);
1543 // Given an input OBJECT, an input section index SHNDX within that
1544 // object, and an OFFSET relative to the start of that input
1545 // section, return whether or not the output offset is known. If
1546 // this function returns true, it sets *POUTPUT to the output
1549 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1550 off_t
*poutput
) const;
1552 // Write out the data. This does nothing for an input section.
1554 write(Output_file
*);
1557 // Code values which appear in shndx_. If the value is not one of
1558 // these codes, it is the input section index in the object file.
1561 // An Output_section_data.
1562 OUTPUT_SECTION_CODE
= -1U,
1563 // An Output_section_data for an SHF_MERGE section with
1564 // SHF_STRINGS not set.
1565 MERGE_DATA_SECTION_CODE
= -2U,
1566 // An Output_section_data for an SHF_MERGE section with
1568 MERGE_STRING_SECTION_CODE
= -3U
1571 // Whether this is an input section.
1573 is_input_section() const
1575 return (this->shndx_
!= OUTPUT_SECTION_CODE
1576 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1577 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1580 // For an ordinary input section, this is the section index in the
1581 // input file. For an Output_section_data, this is
1582 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1583 // MERGE_STRING_SECTION_CODE.
1584 unsigned int shndx_
;
1585 // The required alignment, stored as a power of 2.
1586 unsigned int p2align_
;
1589 // For an ordinary input section, the section size.
1591 // For OUTPUT_SECTION_CODE, this is not used. For
1592 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1598 // For an ordinary input section, the object which holds the
1601 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1602 // MERGE_STRING_SECTION_CODE, the data.
1603 Output_section_data
* posd
;
1607 typedef std::vector
<Input_section
> Input_section_list
;
1609 // Fill data. This is used to fill in data between input sections.
1610 // When we have to keep track of the input sections, we can use an
1611 // Output_data_const, but we don't want to have to keep track of
1612 // input sections just to implement fills. For a fill we record the
1613 // offset, and the actual data to be written out.
1617 Fill(off_t section_offset
, off_t length
)
1618 : section_offset_(section_offset
), length_(length
)
1621 // Return section offset.
1623 section_offset() const
1624 { return this->section_offset_
; }
1626 // Return fill length.
1629 { return this->length_
; }
1632 // The offset within the output section.
1633 off_t section_offset_
;
1634 // The length of the space to fill.
1638 typedef std::vector
<Fill
> Fill_list
;
1640 // Add a new output section by Input_section.
1642 add_output_section_data(Input_section
*);
1644 // Add an SHF_MERGE input section. Returns true if the section was
1647 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1648 uint64_t entsize
, uint64_t addralign
);
1650 // Add an output SHF_MERGE section POSD to this output section.
1651 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1652 // ENTSIZE is the entity size. This returns the entry added to
1655 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1658 // Most of these fields are only valid after layout.
1660 // The name of the section. This will point into a Stringpool.
1662 // The section address is in the parent class.
1663 // The section alignment.
1664 uint64_t addralign_
;
1665 // The section entry size.
1667 // The file offset is in the parent class.
1668 // Set the section link field to the index of this section.
1669 const Output_data
* link_section_
;
1670 // If link_section_ is NULL, this is the link field.
1672 // Set the section info field to the index of this section.
1673 const Output_data
* info_section_
;
1674 // If info_section_ is NULL, this is the section info field.
1676 // The section type.
1677 elfcpp::Elf_Word type_
;
1678 // The section flags.
1679 elfcpp::Elf_Xword flags_
;
1680 // The section index.
1681 unsigned int out_shndx_
;
1682 // If there is a STT_SECTION for this output section in the normal
1683 // symbol table, this is the symbol index. This starts out as zero.
1684 // It is initialized in Layout::finalize() to be the index, or -1U
1685 // if there isn't one.
1686 unsigned int symtab_index_
;
1687 // If there is a STT_SECTION for this output section in the dynamic
1688 // symbol table, this is the symbol index. This starts out as zero.
1689 // It is initialized in Layout::finalize() to be the index, or -1U
1690 // if there isn't one.
1691 unsigned int dynsym_index_
;
1692 // The input sections. This will be empty in cases where we don't
1693 // need to keep track of them.
1694 Input_section_list input_sections_
;
1695 // The offset of the first entry in input_sections_.
1696 off_t first_input_offset_
;
1697 // The fill data. This is separate from input_sections_ because we
1698 // often will need fill sections without needing to keep track of
1701 // Whether this output section needs a STT_SECTION symbol in the
1702 // normal symbol table. This will be true if there is a relocation
1704 bool needs_symtab_index_
: 1;
1705 // Whether this output section needs a STT_SECTION symbol in the
1706 // dynamic symbol table. This will be true if there is a dynamic
1707 // relocation which needs it.
1708 bool needs_dynsym_index_
: 1;
1709 // Whether the link field of this output section should point to the
1710 // normal symbol table.
1711 bool should_link_to_symtab_
: 1;
1712 // Whether the link field of this output section should point to the
1713 // dynamic symbol table.
1714 bool should_link_to_dynsym_
: 1;
1715 // Whether this section should be written after all the input
1716 // sections are complete.
1717 bool after_input_sections_
: 1;
1720 // An output segment. PT_LOAD segments are built from collections of
1721 // output sections. Other segments typically point within PT_LOAD
1722 // segments, and are built directly as needed.
1724 class Output_segment
1727 // Create an output segment, specifying the type and flags.
1728 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1730 // Return the virtual address.
1733 { return this->vaddr_
; }
1735 // Return the physical address.
1738 { return this->paddr_
; }
1740 // Return the segment type.
1743 { return this->type_
; }
1745 // Return the segment flags.
1748 { return this->flags_
; }
1750 // Return the memory size.
1753 { return this->memsz_
; }
1755 // Return the file size.
1758 { return this->filesz_
; }
1760 // Return the maximum alignment of the Output_data.
1764 // Add an Output_section to this segment.
1766 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1767 { this->add_output_section(os
, seg_flags
, false); }
1769 // Add an Output_section to the start of this segment.
1771 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1772 { this->add_output_section(os
, seg_flags
, true); }
1774 // Add an Output_data (which is not an Output_section) to the start
1777 add_initial_output_data(Output_data
*);
1779 // Return the number of dynamic relocations applied to this segment.
1781 dynamic_reloc_count() const;
1783 // Set the address of the segment to ADDR and the offset to *POFF
1784 // (aligned if necessary), and set the addresses and offsets of all
1785 // contained output sections accordingly. Set the section indexes
1786 // of all contained output sections starting with *PSHNDX. Return
1787 // the address of the immediately following segment. Update *POFF
1788 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1790 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
1792 // Set the minimum alignment of this segment. This may be adjusted
1793 // upward based on the section alignments.
1795 set_minimum_addralign(uint64_t align
)
1797 gold_assert(!this->is_align_known_
);
1798 this->align_
= align
;
1801 // Set the offset of this segment based on the section. This should
1802 // only be called for a non-PT_LOAD segment.
1806 // Return the number of output sections.
1808 output_section_count() const;
1810 // Write the segment header into *OPHDR.
1811 template<int size
, bool big_endian
>
1813 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
1815 // Write the section headers of associated sections into V.
1816 template<int size
, bool big_endian
>
1818 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
1819 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
1822 Output_segment(const Output_segment
&);
1823 Output_segment
& operator=(const Output_segment
&);
1825 typedef std::list
<Output_data
*> Output_data_list
;
1827 // Add an Output_section to this segment, specifying front or back.
1829 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
1832 // Find the maximum alignment in an Output_data_list.
1834 maximum_alignment(const Output_data_list
*);
1836 // Set the section addresses in an Output_data_list.
1838 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
1839 unsigned int* pshndx
);
1841 // Return the number of Output_sections in an Output_data_list.
1843 output_section_count_list(const Output_data_list
*) const;
1845 // Return the number of dynamic relocs in an Output_data_list.
1847 dynamic_reloc_count_list(const Output_data_list
*) const;
1849 // Write the section headers in the list into V.
1850 template<int size
, bool big_endian
>
1852 write_section_headers_list(const Layout
*, const Stringpool
*,
1853 const Output_data_list
*, unsigned char* v
,
1854 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
1856 // The list of output data with contents attached to this segment.
1857 Output_data_list output_data_
;
1858 // The list of output data without contents attached to this segment.
1859 Output_data_list output_bss_
;
1860 // The segment virtual address.
1862 // The segment physical address.
1864 // The size of the segment in memory.
1866 // The segment alignment. The is_align_known_ field indicates
1867 // whether this has been finalized. It can be set to a minimum
1868 // value before it is finalized.
1870 // The offset of the segment data within the file.
1872 // The size of the segment data in the file.
1874 // The segment type;
1875 elfcpp::Elf_Word type_
;
1876 // The segment flags.
1877 elfcpp::Elf_Word flags_
;
1878 // Whether we have finalized align_.
1879 bool is_align_known_
;
1882 // This class represents the output file.
1887 Output_file(const General_options
& options
, Target
*);
1889 // Get a pointer to the target.
1892 { return this->target_
; }
1894 // Open the output file. FILE_SIZE is the final size of the file.
1896 open(off_t file_size
);
1898 // Close the output file and make sure there are no error.
1902 // We currently always use mmap which makes the view handling quite
1903 // simple. In the future we may support other approaches.
1905 // Write data to the output file.
1907 write(off_t offset
, const void* data
, off_t len
)
1908 { memcpy(this->base_
+ offset
, data
, len
); }
1910 // Get a buffer to use to write to the file, given the offset into
1911 // the file and the size.
1913 get_output_view(off_t start
, off_t size
)
1915 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
1916 return this->base_
+ start
;
1919 // VIEW must have been returned by get_output_view. Write the
1920 // buffer to the file, passing in the offset and the size.
1922 write_output_view(off_t
, off_t
, unsigned char*)
1925 // Get a read/write buffer. This is used when we want to write part
1926 // of the file, read it in, and write it again.
1928 get_input_output_view(off_t start
, off_t size
)
1929 { return this->get_output_view(start
, size
); }
1931 // Write a read/write buffer back to the file.
1933 write_input_output_view(off_t
, off_t
, unsigned char*)
1936 // Get a read buffer. This is used when we just want to read part
1937 // of the file back it in.
1938 const unsigned char*
1939 get_input_view(off_t start
, off_t size
)
1940 { return this->get_output_view(start
, size
); }
1942 // Release a read bfufer.
1944 free_input_view(off_t
, off_t
, const unsigned char*)
1949 const General_options
& options_
;
1958 // Base of file mapped into memory.
1959 unsigned char* base_
;
1962 } // End namespace gold.
1964 #endif // !defined(GOLD_OUTPUT_H)