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)
59 // Return the address. This is only valid after Layout::finalize is
63 { return this->address_
; }
65 // Return the size of the data. This must be valid after
66 // Layout::finalize calls set_address, but need not be valid before
70 { return this->data_size_
; }
72 // Return the file offset. This is only valid after
73 // Layout::finalize is finished.
76 { return this->offset_
; }
78 // Return the required alignment.
81 { return this->do_addralign(); }
83 // Return whether this is an Output_section.
86 { return this->do_is_section(); }
88 // Return whether this is an Output_section of the specified type.
90 is_section_type(elfcpp::Elf_Word stt
) const
91 { return this->do_is_section_type(stt
); }
93 // Return whether this is an Output_section with the specified flag
96 is_section_flag_set(elfcpp::Elf_Xword shf
) const
97 { return this->do_is_section_flag_set(shf
); }
99 // Return the output section index, if there is an output section.
102 { return this->do_out_shndx(); }
104 // Set the output section index, if this is an output section.
106 set_out_shndx(unsigned int shndx
)
107 { this->do_set_out_shndx(shndx
); }
109 // Set the address and file offset of this data. This is called
110 // during Layout::finalize.
112 set_address(uint64_t addr
, off_t off
);
114 // Write the data to the output file. This is called after
115 // Layout::finalize is complete.
117 write(Output_file
* file
)
118 { this->do_write(file
); }
120 // This is called by Layout::finalize to note that all sizes must
124 { Output_data::sizes_are_fixed
= true; }
126 // Used to check that layout has been done.
129 { return Output_data::sizes_are_fixed
; }
132 // Functions that child classes may or in some cases must implement.
134 // Write the data to the output file.
136 do_write(Output_file
*) = 0;
138 // Return the required alignment.
140 do_addralign() const = 0;
142 // Return whether this is an Output_section.
144 do_is_section() const
147 // Return whether this is an Output_section of the specified type.
148 // This only needs to be implement by Output_section.
150 do_is_section_type(elfcpp::Elf_Word
) const
153 // Return whether this is an Output_section with the specific flag
154 // set. This only needs to be implemented by Output_section.
156 do_is_section_flag_set(elfcpp::Elf_Xword
) const
159 // Return the output section index, if there is an output section.
162 { gold_unreachable(); }
164 // Set the output section index, if this is an output section.
166 do_set_out_shndx(unsigned int)
167 { gold_unreachable(); }
169 // Set the address and file offset of the data. This only needs to
170 // be implemented if the child needs to know. The child class can
171 // set its size in this call.
173 do_set_address(uint64_t, off_t
)
176 // Functions that child classes may call.
178 // Set the size of the data.
180 set_data_size(off_t data_size
)
182 gold_assert(!Output_data::sizes_are_fixed
);
183 this->data_size_
= data_size
;
186 // Return default alignment for the target size.
190 // Return default alignment for a specified size--32 or 64.
192 default_alignment_for_size(int size
);
195 Output_data(const Output_data
&);
196 Output_data
& operator=(const Output_data
&);
198 // This is used for verification, to make sure that we don't try to
199 // change any sizes after we set the section addresses.
200 static bool sizes_are_fixed
;
202 // Memory address in file (not always meaningful).
204 // Size of data in file.
206 // Offset within file.
210 // Output the section headers.
212 class Output_section_headers
: public Output_data
215 Output_section_headers(const Layout
*,
216 const Layout::Segment_list
*,
217 const Layout::Section_list
*,
220 // Write the data to the file.
222 do_write(Output_file
*);
224 // Return the required alignment.
227 { return Output_data::default_alignment(); }
230 // Write the data to the file with the right size and endianness.
231 template<int size
, bool big_endian
>
233 do_sized_write(Output_file
*);
235 const Layout
* layout_
;
236 const Layout::Segment_list
* segment_list_
;
237 const Layout::Section_list
* unattached_section_list_
;
238 const Stringpool
* secnamepool_
;
241 // Output the segment headers.
243 class Output_segment_headers
: public Output_data
246 Output_segment_headers(const Layout::Segment_list
& segment_list
);
248 // Write the data to the file.
250 do_write(Output_file
*);
252 // Return the required alignment.
255 { return Output_data::default_alignment(); }
258 // Write the data to the file with the right size and endianness.
259 template<int size
, bool big_endian
>
261 do_sized_write(Output_file
*);
263 const Layout::Segment_list
& segment_list_
;
266 // Output the ELF file header.
268 class Output_file_header
: public Output_data
271 Output_file_header(const Target
*,
273 const Output_segment_headers
*);
275 // Add information about the section headers. We lay out the ELF
276 // file header before we create the section headers.
277 void set_section_info(const Output_section_headers
*,
278 const Output_section
* shstrtab
);
280 // Write the data to the file.
282 do_write(Output_file
*);
284 // Return the required alignment.
287 { return Output_data::default_alignment(); }
289 // Set the address and offset--we only implement this for error
292 do_set_address(uint64_t, off_t off
) const
293 { gold_assert(off
== 0); }
296 // Write the data to the file with the right size and endianness.
297 template<int size
, bool big_endian
>
299 do_sized_write(Output_file
*);
301 const Target
* target_
;
302 const Symbol_table
* symtab_
;
303 const Output_segment_headers
* segment_header_
;
304 const Output_section_headers
* section_header_
;
305 const Output_section
* shstrtab_
;
308 // Output sections are mainly comprised of input sections. However,
309 // there are cases where we have data to write out which is not in an
310 // input section. Output_section_data is used in such cases. This is
311 // an abstract base class.
313 class Output_section_data
: public Output_data
316 Output_section_data(off_t data_size
, uint64_t addralign
)
317 : Output_data(data_size
), output_section_(NULL
), addralign_(addralign
)
320 Output_section_data(uint64_t addralign
)
321 : Output_data(0), output_section_(NULL
), addralign_(addralign
)
324 // Return the output section.
325 const Output_section
*
326 output_section() const
327 { return this->output_section_
; }
329 // Record the output section.
331 set_output_section(Output_section
* os
);
333 // Add an input section, for SHF_MERGE sections. This returns true
334 // if the section was handled.
336 add_input_section(Relobj
* object
, unsigned int shndx
)
337 { return this->do_add_input_section(object
, shndx
); }
339 // Given an input OBJECT, an input section index SHNDX within that
340 // object, and an OFFSET relative to the start of that input
341 // section, return whether or not the corresponding offset within
342 // the output section is known. If this function returns true, it
343 // sets *POUTPUT to the output offset. The value -1 indicates that
344 // this input offset is being discarded.
346 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
347 off_t
*poutput
) const
348 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
351 // The child class must implement do_write.
353 // The child class may implement specific adjustments to the output
356 do_adjust_output_section(Output_section
*)
359 // May be implemented by child class. Return true if the section
362 do_add_input_section(Relobj
*, unsigned int)
363 { gold_unreachable(); }
365 // The child class may implement output_offset.
367 do_output_offset(const Relobj
*, unsigned int, off_t
, off_t
*) const
370 // Return the required alignment.
373 { return this->addralign_
; }
375 // Return the section index of the output section.
377 do_out_shndx() const;
379 // Set the alignment.
381 set_addralign(uint64_t addralign
)
382 { this->addralign_
= addralign
; }
385 // The output section for this section.
386 const Output_section
* output_section_
;
387 // The required alignment.
391 // A simple case of Output_data in which we have constant data to
394 class Output_data_const
: public Output_section_data
397 Output_data_const(const std::string
& data
, uint64_t addralign
)
398 : Output_section_data(data
.size(), addralign
), data_(data
)
401 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
402 : Output_section_data(len
, addralign
), data_(p
, len
)
405 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
406 : Output_section_data(len
, addralign
),
407 data_(reinterpret_cast<const char*>(p
), len
)
412 add_data(const std::string
& add
)
414 this->data_
.append(add
);
415 this->set_data_size(this->data_
.size());
418 // Write the data to the output file.
420 do_write(Output_file
*);
426 // Another version of Output_data with constant data, in which the
427 // buffer is allocated by the caller.
429 class Output_data_const_buffer
: public Output_section_data
432 Output_data_const_buffer(const unsigned char* p
, off_t len
,
434 : Output_section_data(len
, addralign
), p_(p
)
437 // Write the data the output file.
439 do_write(Output_file
*);
442 const unsigned char* p_
;
445 // A place holder for data written out via some other mechanism.
447 class Output_data_space
: public Output_section_data
450 Output_data_space(off_t data_size
, uint64_t addralign
)
451 : Output_section_data(data_size
, addralign
)
454 explicit Output_data_space(uint64_t addralign
)
455 : Output_section_data(addralign
)
460 set_space_size(off_t space_size
)
461 { this->set_data_size(space_size
); }
463 // Set the alignment.
465 set_space_alignment(uint64_t align
)
466 { this->set_addralign(align
); }
468 // Write out the data--this must be handled elsewhere.
470 do_write(Output_file
*)
474 // A string table which goes into an output section.
476 class Output_data_strtab
: public Output_section_data
479 Output_data_strtab(Stringpool
* strtab
)
480 : Output_section_data(1), strtab_(strtab
)
483 // This is called to set the address and file offset. Here we make
484 // sure that the Stringpool is finalized.
486 do_set_address(uint64_t, off_t
);
488 // Write out the data.
490 do_write(Output_file
*);
496 // This POD class is used to represent a single reloc in the output
497 // file. This could be a private class within Output_data_reloc, but
498 // the templatization is complex enough that I broke it out into a
499 // separate class. The class is templatized on either elfcpp::SHT_REL
500 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
501 // relocation or an ordinary relocation.
503 // A relocation can be against a global symbol, a local symbol, an
504 // output section, or the undefined symbol at index 0. We represent
505 // the latter by using a NULL global symbol.
507 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
510 template<bool dynamic
, int size
, bool big_endian
>
511 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
514 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
516 // An uninitialized entry. We need this because we want to put
517 // instances of this class into an STL container.
519 : local_sym_index_(INVALID_CODE
)
522 // A reloc against a global symbol.
524 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
526 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
529 this->u1_
.gsym
= gsym
;
533 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
534 unsigned int shndx
, Address address
)
535 : address_(address
), local_sym_index_(GSYM_CODE
), type_(type
),
538 gold_assert(shndx
!= INVALID_CODE
);
539 this->u1_
.gsym
= gsym
;
540 this->u2_
.relobj
= relobj
;
543 // A reloc against a local symbol.
545 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
546 unsigned int local_sym_index
,
550 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
553 gold_assert(local_sym_index
!= GSYM_CODE
554 && local_sym_index
!= INVALID_CODE
);
555 this->u1_
.relobj
= relobj
;
559 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
560 unsigned int local_sym_index
,
564 : address_(address
), local_sym_index_(local_sym_index
), type_(type
),
567 gold_assert(local_sym_index
!= GSYM_CODE
568 && local_sym_index
!= INVALID_CODE
);
569 gold_assert(shndx
!= INVALID_CODE
);
570 this->u1_
.relobj
= relobj
;
571 this->u2_
.relobj
= relobj
;
574 // A reloc against the STT_SECTION symbol of an output section.
576 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
578 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
585 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
586 unsigned int shndx
, Address address
)
587 : address_(address
), local_sym_index_(SECTION_CODE
), type_(type
),
590 gold_assert(shndx
!= INVALID_CODE
);
592 this->u2_
.relobj
= relobj
;
595 // Write the reloc entry to an output view.
597 write(unsigned char* pov
) const;
599 // Write the offset and info fields to Write_rel.
600 template<typename Write_rel
>
601 void write_rel(Write_rel
*) const;
604 // Return the symbol index. We can't do a double template
605 // specialization, so we do a secondary template here.
607 get_symbol_index() const;
609 // Codes for local_sym_index_.
616 // Invalid uninitialized entry.
622 // For a local symbol, the object. We will never generate a
623 // relocation against a local symbol in a dynamic object; that
624 // doesn't make sense. And our callers will always be
625 // templatized, so we use Sized_relobj here.
626 Sized_relobj
<size
, big_endian
>* relobj
;
627 // For a global symbol, the symbol. If this is NULL, it indicates
628 // a relocation against the undefined 0 symbol.
630 // For a relocation against an output section, the output section.
635 // If shndx_ is not INVALID CODE, the object which holds the input
636 // section being used to specify the reloc address.
638 // If shndx_ is INVALID_CODE, the output data being used to
639 // specify the reloc address. This may be NULL if the reloc
640 // address is absolute.
643 // The address offset within the input section or the Output_data.
645 // For a local symbol, the local symbol index. This is GSYM_CODE
646 // for a global symbol, or INVALID_CODE for an uninitialized value.
647 unsigned int local_sym_index_
;
648 // The reloc type--a processor specific code.
650 // If the reloc address is an input section in an object, the
651 // section index. This is INVALID_CODE if the reloc address is
652 // specified in some other way.
656 // The SHT_RELA version of Output_reloc<>. This is just derived from
657 // the SHT_REL version of Output_reloc, but it adds an addend.
659 template<bool dynamic
, int size
, bool big_endian
>
660 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
663 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
664 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
666 // An uninitialized entry.
671 // A reloc against a global symbol.
673 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
674 Address address
, Addend addend
)
675 : rel_(gsym
, type
, od
, address
), addend_(addend
)
678 Output_reloc(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
679 unsigned int shndx
, Address address
, Addend addend
)
680 : rel_(gsym
, type
, relobj
, shndx
, address
), addend_(addend
)
683 // A reloc against a local symbol.
685 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
686 unsigned int local_sym_index
,
687 unsigned int type
, Output_data
* od
, Address address
,
689 : rel_(relobj
, local_sym_index
, type
, od
, address
), addend_(addend
)
692 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
693 unsigned int local_sym_index
,
698 : rel_(relobj
, local_sym_index
, type
, shndx
, address
),
702 // A reloc against the STT_SECTION symbol of an output section.
704 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
705 Address address
, Addend addend
)
706 : rel_(os
, type
, od
, address
), addend_(addend
)
709 Output_reloc(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
710 unsigned int shndx
, Address address
, Addend addend
)
711 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
714 // Write the reloc entry to an output view.
716 write(unsigned char* pov
) const;
720 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
725 // Output_data_reloc is used to manage a section containing relocs.
726 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
727 // indicates whether this is a dynamic relocation or a normal
728 // relocation. Output_data_reloc_base is a base class.
729 // Output_data_reloc is the real class, which we specialize based on
732 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
733 class Output_data_reloc_base
: public Output_section_data
736 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
737 typedef typename
Output_reloc_type::Address Address
;
738 static const int reloc_size
=
739 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
741 // Construct the section.
742 Output_data_reloc_base()
743 : Output_section_data(Output_data::default_alignment_for_size(size
))
746 // Write out the data.
748 do_write(Output_file
*);
751 // Set the entry size and the link.
753 do_adjust_output_section(Output_section
*os
);
755 // Add a relocation entry.
757 add(const Output_reloc_type
& reloc
)
759 this->relocs_
.push_back(reloc
);
760 this->set_data_size(this->relocs_
.size() * reloc_size
);
764 typedef std::vector
<Output_reloc_type
> Relocs
;
769 // The class which callers actually create.
771 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
772 class Output_data_reloc
;
774 // The SHT_REL version of Output_data_reloc.
776 template<bool dynamic
, int size
, bool big_endian
>
777 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
778 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
781 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
785 typedef typename
Base::Output_reloc_type Output_reloc_type
;
786 typedef typename
Output_reloc_type::Address Address
;
789 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>()
792 // Add a reloc against a global symbol.
795 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
796 { this->add(Output_reloc_type(gsym
, type
, od
, address
)); }
799 add_global(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
800 unsigned int shndx
, Address address
)
801 { this->add(Output_reloc_type(gsym
, type
, relobj
, shndx
, address
)); }
803 // Add a reloc against a local symbol.
806 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
807 unsigned int local_sym_index
, unsigned int type
,
808 Output_data
* od
, Address address
)
809 { this->add(Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
)); }
812 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
813 unsigned int local_sym_index
, unsigned int type
,
814 unsigned int shndx
, Address address
)
815 { this->add(Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
819 // A reloc against the STT_SECTION symbol of an output section.
822 add_output_section(Output_section
* os
, unsigned int type
,
823 Output_data
* od
, Address address
)
824 { this->add(Output_reloc_type(os
, type
, od
, address
)); }
827 add_output_section(Output_section
* os
, unsigned int type
,
828 Relobj
* relobj
, unsigned int shndx
, Address address
)
829 { this->add(Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
832 // The SHT_RELA version of Output_data_reloc.
834 template<bool dynamic
, int size
, bool big_endian
>
835 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
836 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
839 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
843 typedef typename
Base::Output_reloc_type Output_reloc_type
;
844 typedef typename
Output_reloc_type::Address Address
;
845 typedef typename
Output_reloc_type::Addend Addend
;
848 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>()
851 // Add a reloc against a global symbol.
854 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
855 Address address
, Addend addend
)
856 { this->add(Output_reloc_type(gsym
, type
, od
, address
, addend
)); }
859 add_global(Symbol
* gsym
, unsigned int type
, Relobj
* relobj
,
860 unsigned int shndx
, Address address
, Addend addend
)
861 { this->add(Output_reloc_type(gsym
, type
, relobj
, shndx
, address
, addend
)); }
863 // Add a reloc against a local symbol.
866 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
867 unsigned int local_sym_index
, unsigned int type
,
868 Output_data
* od
, Address address
, Addend addend
)
870 this->add(Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
875 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
876 unsigned int local_sym_index
, unsigned int type
,
877 unsigned int shndx
, Address address
, Addend addend
)
879 this->add(Output_reloc_type(relobj
, local_sym_index
, type
, shndx
, address
,
883 // A reloc against the STT_SECTION symbol of an output section.
886 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
887 Address address
, Addend addend
)
888 { this->add(Output_reloc_type(os
, type
, od
, address
, addend
)); }
891 add_output_section(Output_section
* os
, unsigned int type
, Relobj
* relobj
,
892 unsigned int shndx
, Address address
, Addend addend
)
893 { this->add(Output_reloc_type(os
, type
, relobj
, shndx
, address
, addend
)); }
896 // Output_data_got is used to manage a GOT. Each entry in the GOT is
897 // for one symbol--either a global symbol or a local symbol in an
898 // object. The target specific code adds entries to the GOT as
901 template<int size
, bool big_endian
>
902 class Output_data_got
: public Output_section_data
905 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
908 : Output_section_data(Output_data::default_alignment_for_size(size
)),
912 // Add an entry for a global symbol to the GOT. Return true if this
913 // is a new GOT entry, false if the symbol was already in the GOT.
915 add_global(Symbol
* gsym
);
917 // Add an entry for a local symbol to the GOT. This returns true if
918 // this is a new GOT entry, false if the symbol already has a GOT
921 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
);
923 // Add a constant to the GOT. This returns the offset of the new
924 // entry from the start of the GOT.
926 add_constant(Valtype constant
)
928 this->entries_
.push_back(Got_entry(constant
));
929 this->set_got_size();
930 return this->last_got_offset();
933 // Write out the GOT table.
935 do_write(Output_file
*);
938 // This POD class holds a single GOT entry.
942 // Create a zero entry.
944 : local_sym_index_(CONSTANT_CODE
)
945 { this->u_
.constant
= 0; }
947 // Create a global symbol entry.
948 explicit Got_entry(Symbol
* gsym
)
949 : local_sym_index_(GSYM_CODE
)
950 { this->u_
.gsym
= gsym
; }
952 // Create a local symbol entry.
953 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
954 unsigned int local_sym_index
)
955 : local_sym_index_(local_sym_index
)
957 gold_assert(local_sym_index
!= GSYM_CODE
958 && local_sym_index
!= CONSTANT_CODE
);
959 this->u_
.object
= object
;
962 // Create a constant entry. The constant is a host value--it will
963 // be swapped, if necessary, when it is written out.
964 explicit Got_entry(Valtype constant
)
965 : local_sym_index_(CONSTANT_CODE
)
966 { this->u_
.constant
= constant
; }
968 // Write the GOT entry to an output view.
970 write(unsigned char* pov
) const;
981 // For a local symbol, the object.
982 Sized_relobj
<size
, big_endian
>* object
;
983 // For a global symbol, the symbol.
985 // For a constant, the constant.
988 // For a local symbol, the local symbol index. This is GSYM_CODE
989 // for a global symbol, or CONSTANT_CODE for a constant.
990 unsigned int local_sym_index_
;
993 typedef std::vector
<Got_entry
> Got_entries
;
995 // Return the offset into the GOT of GOT entry I.
997 got_offset(unsigned int i
) const
998 { return i
* (size
/ 8); }
1000 // Return the offset into the GOT of the last entry added.
1002 last_got_offset() const
1003 { return this->got_offset(this->entries_
.size() - 1); }
1005 // Set the size of the section.
1008 { this->set_data_size(this->got_offset(this->entries_
.size())); }
1010 // The list of GOT entries.
1011 Got_entries entries_
;
1014 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1017 class Output_data_dynamic
: public Output_section_data
1020 Output_data_dynamic(Stringpool
* pool
)
1021 : Output_section_data(Output_data::default_alignment()),
1022 entries_(), pool_(pool
)
1025 // Add a new dynamic entry with a fixed numeric value.
1027 add_constant(elfcpp::DT tag
, unsigned int val
)
1028 { this->add_entry(Dynamic_entry(tag
, val
)); }
1030 // Add a new dynamic entry with the address of output data.
1032 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1033 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1035 // Add a new dynamic entry with the size of output data.
1037 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1038 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1040 // Add a new dynamic entry with the address of a symbol.
1042 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1043 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1045 // Add a new dynamic entry with a string.
1047 add_string(elfcpp::DT tag
, const char* str
)
1048 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1051 add_string(elfcpp::DT tag
, const std::string
& str
)
1052 { this->add_string(tag
, str
.c_str()); }
1054 // Set the final data size.
1056 do_set_address(uint64_t, off_t
);
1058 // Write out the dynamic entries.
1060 do_write(Output_file
*);
1063 // Adjust the output section to set the entry size.
1065 do_adjust_output_section(Output_section
*);
1068 // This POD class holds a single dynamic entry.
1072 // Create an entry with a fixed numeric value.
1073 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1074 : tag_(tag
), classification_(DYNAMIC_NUMBER
)
1075 { this->u_
.val
= val
; }
1077 // Create an entry with the size or address of a section.
1078 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1080 classification_(section_size
1081 ? DYNAMIC_SECTION_SIZE
1082 : DYNAMIC_SECTION_ADDRESS
)
1083 { this->u_
.od
= od
; }
1085 // Create an entry with the address of a symbol.
1086 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1087 : tag_(tag
), classification_(DYNAMIC_SYMBOL
)
1088 { this->u_
.sym
= sym
; }
1090 // Create an entry with a string.
1091 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1092 : tag_(tag
), classification_(DYNAMIC_STRING
)
1093 { this->u_
.str
= str
; }
1095 // Write the dynamic entry to an output view.
1096 template<int size
, bool big_endian
>
1098 write(unsigned char* pov
, const Stringpool
* ACCEPT_SIZE_ENDIAN
) const;
1106 DYNAMIC_SECTION_ADDRESS
,
1108 DYNAMIC_SECTION_SIZE
,
1117 // For DYNAMIC_NUMBER.
1119 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1120 const Output_data
* od
;
1121 // For DYNAMIC_SYMBOL.
1123 // For DYNAMIC_STRING.
1128 // The type of entry.
1129 Classification classification_
;
1132 // Add an entry to the list.
1134 add_entry(const Dynamic_entry
& entry
)
1135 { this->entries_
.push_back(entry
); }
1137 // Sized version of write function.
1138 template<int size
, bool big_endian
>
1140 sized_write(Output_file
* of
);
1142 // The type of the list of entries.
1143 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
1146 Dynamic_entries entries_
;
1147 // The pool used for strings.
1151 // An output section. We don't expect to have too many output
1152 // sections, so we don't bother to do a template on the size.
1154 class Output_section
: public Output_data
1157 // Create an output section, giving the name, type, and flags.
1158 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
1159 virtual ~Output_section();
1161 // Add a new input section SHNDX, named NAME, with header SHDR, from
1162 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1163 // which applies to this section, or 0 if none, or -1U if more than
1164 // one. Return the offset within the output section.
1165 template<int size
, bool big_endian
>
1167 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
1169 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
1170 unsigned int reloc_shndx
);
1172 // Add generated data POSD to this output section.
1174 add_output_section_data(Output_section_data
* posd
);
1176 // Return the section name.
1179 { return this->name_
; }
1181 // Return the section type.
1184 { return this->type_
; }
1186 // Return the section flags.
1189 { return this->flags_
; }
1191 // Return the section index in the output file.
1193 do_out_shndx() const
1195 gold_assert(this->out_shndx_
!= -1U);
1196 return this->out_shndx_
;
1199 // Set the output section index.
1201 do_set_out_shndx(unsigned int shndx
)
1203 gold_assert(this->out_shndx_
== -1U);
1204 this->out_shndx_
= shndx
;
1207 // Return the entsize field.
1210 { return this->entsize_
; }
1212 // Set the entsize field.
1214 set_entsize(uint64_t v
);
1216 // Set the link field to the output section index of a section.
1218 set_link_section(const Output_data
* od
)
1220 gold_assert(this->link_
== 0
1221 && !this->should_link_to_symtab_
1222 && !this->should_link_to_dynsym_
);
1223 this->link_section_
= od
;
1226 // Set the link field to a constant.
1228 set_link(unsigned int v
)
1230 gold_assert(this->link_section_
== NULL
1231 && !this->should_link_to_symtab_
1232 && !this->should_link_to_dynsym_
);
1236 // Record that this section should link to the normal symbol table.
1238 set_should_link_to_symtab()
1240 gold_assert(this->link_section_
== NULL
1242 && !this->should_link_to_dynsym_
);
1243 this->should_link_to_symtab_
= true;
1246 // Record that this section should link to the dynamic symbol table.
1248 set_should_link_to_dynsym()
1250 gold_assert(this->link_section_
== NULL
1252 && !this->should_link_to_symtab_
);
1253 this->should_link_to_dynsym_
= true;
1256 // Return the info field.
1260 gold_assert(this->info_section_
== NULL
);
1264 // Set the info field to the output section index of a section.
1266 set_info_section(const Output_data
* od
)
1268 gold_assert(this->info_
== 0);
1269 this->info_section_
= od
;
1272 // Set the info field to a constant.
1274 set_info(unsigned int v
)
1276 gold_assert(this->info_section_
== NULL
);
1280 // Set the addralign field.
1282 set_addralign(uint64_t v
)
1283 { this->addralign_
= v
; }
1285 // Indicate that we need a symtab index.
1287 set_needs_symtab_index()
1288 { this->needs_symtab_index_
= true; }
1290 // Return whether we need a symtab index.
1292 needs_symtab_index() const
1293 { return this->needs_symtab_index_
; }
1295 // Get the symtab index.
1297 symtab_index() const
1299 gold_assert(this->symtab_index_
!= 0);
1300 return this->symtab_index_
;
1303 // Set the symtab index.
1305 set_symtab_index(unsigned int index
)
1307 gold_assert(index
!= 0);
1308 this->symtab_index_
= index
;
1311 // Indicate that we need a dynsym index.
1313 set_needs_dynsym_index()
1314 { this->needs_dynsym_index_
= true; }
1316 // Return whether we need a dynsym index.
1318 needs_dynsym_index() const
1319 { return this->needs_dynsym_index_
; }
1321 // Get the dynsym index.
1323 dynsym_index() const
1325 gold_assert(this->dynsym_index_
!= 0);
1326 return this->dynsym_index_
;
1329 // Set the dynsym index.
1331 set_dynsym_index(unsigned int index
)
1333 gold_assert(index
!= 0);
1334 this->dynsym_index_
= index
;
1337 // Return whether this section should be written after all the input
1338 // sections are complete.
1340 after_input_sections() const
1341 { return this->after_input_sections_
; }
1343 // Record that this section should be written after all the input
1344 // sections are complete.
1346 set_after_input_sections()
1347 { this->after_input_sections_
= true; }
1349 // Return whether the offset OFFSET in the input section SHNDX in
1350 // object OBJECT is being included in the link.
1352 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
1353 off_t offset
) const;
1355 // Return the offset within the output section of OFFSET relative to
1356 // the start of input section SHNDX in object OBJECT.
1358 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
) const;
1360 // Return the output virtual address of OFFSET relative to the start
1361 // of input section SHNDX in object OBJECT.
1363 output_address(const Relobj
* object
, unsigned int shndx
,
1364 off_t offset
) const;
1366 // Set the address of the Output_section. For a typical
1367 // Output_section, there is nothing to do, but if there are any
1368 // Output_section_data objects we need to set the final addresses
1371 do_set_address(uint64_t, off_t
);
1373 // Write the data to the file. For a typical Output_section, this
1374 // does nothing: the data is written out by calling Object::Relocate
1375 // on each input object. But if there are any Output_section_data
1376 // objects we do need to write them out here.
1378 do_write(Output_file
*);
1380 // Return the address alignment--function required by parent class.
1382 do_addralign() const
1383 { return this->addralign_
; }
1385 // Return whether this is an Output_section.
1387 do_is_section() const
1390 // Return whether this is a section of the specified type.
1392 do_is_section_type(elfcpp::Elf_Word type
) const
1393 { return this->type_
== type
; }
1395 // Return whether the specified section flag is set.
1397 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
1398 { return (this->flags_
& flag
) != 0; }
1400 // Write the section header into *OPHDR.
1401 template<int size
, bool big_endian
>
1403 write_header(const Layout
*, const Stringpool
*,
1404 elfcpp::Shdr_write
<size
, big_endian
>*) const;
1407 // In some cases we need to keep a list of the input sections
1408 // associated with this output section. We only need the list if we
1409 // might have to change the offsets of the input section within the
1410 // output section after we add the input section. The ordinary
1411 // input sections will be written out when we process the object
1412 // file, and as such we don't need to track them here. We do need
1413 // to track Output_section_data objects here. We store instances of
1414 // this structure in a std::vector, so it must be a POD. There can
1415 // be many instances of this structure, so we use a union to save
1421 : shndx_(0), p2align_(0)
1423 this->u1_
.data_size
= 0;
1424 this->u2_
.object
= NULL
;
1427 // For an ordinary input section.
1428 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
1431 p2align_(ffsll(static_cast<long long>(addralign
)))
1433 gold_assert(shndx
!= OUTPUT_SECTION_CODE
1434 && shndx
!= MERGE_DATA_SECTION_CODE
1435 && shndx
!= MERGE_STRING_SECTION_CODE
);
1436 this->u1_
.data_size
= data_size
;
1437 this->u2_
.object
= object
;
1440 // For a non-merge output section.
1441 Input_section(Output_section_data
* posd
)
1442 : shndx_(OUTPUT_SECTION_CODE
),
1443 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1445 this->u1_
.data_size
= 0;
1446 this->u2_
.posd
= posd
;
1449 // For a merge section.
1450 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
1452 ? MERGE_STRING_SECTION_CODE
1453 : MERGE_DATA_SECTION_CODE
),
1454 p2align_(ffsll(static_cast<long long>(posd
->addralign())))
1456 this->u1_
.entsize
= entsize
;
1457 this->u2_
.posd
= posd
;
1460 // The required alignment.
1464 return (this->p2align_
== 0
1466 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
1469 // Return the required size.
1473 // Return whether this is a merge section which matches the
1476 is_merge_section(bool is_string
, uint64_t entsize
,
1477 uint64_t addralign
) const
1479 return (this->shndx_
== (is_string
1480 ? MERGE_STRING_SECTION_CODE
1481 : MERGE_DATA_SECTION_CODE
)
1482 && this->u1_
.entsize
== entsize
1483 && this->addralign() == addralign
);
1486 // Set the output section.
1488 set_output_section(Output_section
* os
)
1490 gold_assert(!this->is_input_section());
1491 this->u2_
.posd
->set_output_section(os
);
1494 // Set the address and file offset. This is called during
1495 // Layout::finalize. SECOFF is the file offset of the enclosing
1498 set_address(uint64_t addr
, off_t off
, off_t secoff
);
1500 // Add an input section, for SHF_MERGE sections.
1502 add_input_section(Relobj
* object
, unsigned int shndx
)
1504 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
1505 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
1506 return this->u2_
.posd
->add_input_section(object
, shndx
);
1509 // Given an input OBJECT, an input section index SHNDX within that
1510 // object, and an OFFSET relative to the start of that input
1511 // section, return whether or not the output offset is known. If
1512 // this function returns true, it sets *POUTPUT to the output
1515 output_offset(const Relobj
* object
, unsigned int shndx
, off_t offset
,
1516 off_t
*poutput
) const;
1518 // Write out the data. This does nothing for an input section.
1520 write(Output_file
*);
1523 // Code values which appear in shndx_. If the value is not one of
1524 // these codes, it is the input section index in the object file.
1527 // An Output_section_data.
1528 OUTPUT_SECTION_CODE
= -1U,
1529 // An Output_section_data for an SHF_MERGE section with
1530 // SHF_STRINGS not set.
1531 MERGE_DATA_SECTION_CODE
= -2U,
1532 // An Output_section_data for an SHF_MERGE section with
1534 MERGE_STRING_SECTION_CODE
= -3U
1537 // Whether this is an input section.
1539 is_input_section() const
1541 return (this->shndx_
!= OUTPUT_SECTION_CODE
1542 && this->shndx_
!= MERGE_DATA_SECTION_CODE
1543 && this->shndx_
!= MERGE_STRING_SECTION_CODE
);
1546 // For an ordinary input section, this is the section index in the
1547 // input file. For an Output_section_data, this is
1548 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1549 // MERGE_STRING_SECTION_CODE.
1550 unsigned int shndx_
;
1551 // The required alignment, stored as a power of 2.
1552 unsigned int p2align_
;
1555 // For an ordinary input section, the section size.
1557 // For OUTPUT_SECTION_CODE, this is not used. For
1558 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1564 // For an ordinary input section, the object which holds the
1567 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1568 // MERGE_STRING_SECTION_CODE, the data.
1569 Output_section_data
* posd
;
1573 typedef std::vector
<Input_section
> Input_section_list
;
1575 // Fill data. This is used to fill in data between input sections.
1576 // When we have to keep track of the input sections, we can use an
1577 // Output_data_const, but we don't want to have to keep track of
1578 // input sections just to implement fills. For a fill we record the
1579 // offset, and the actual data to be written out.
1583 Fill(off_t section_offset
, off_t length
)
1584 : section_offset_(section_offset
), length_(length
)
1587 // Return section offset.
1589 section_offset() const
1590 { return this->section_offset_
; }
1592 // Return fill length.
1595 { return this->length_
; }
1598 // The offset within the output section.
1599 off_t section_offset_
;
1600 // The length of the space to fill.
1604 typedef std::vector
<Fill
> Fill_list
;
1606 // Add a new output section by Input_section.
1608 add_output_section_data(Input_section
*);
1610 // Add an SHF_MERGE input section. Returns true if the section was
1613 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
1614 uint64_t entsize
, uint64_t addralign
);
1616 // Add an output SHF_MERGE section POSD to this output section.
1617 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1618 // ENTSIZE is the entity size. This returns the entry added to
1621 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
1624 // Most of these fields are only valid after layout.
1626 // The name of the section. This will point into a Stringpool.
1628 // The section address is in the parent class.
1629 // The section alignment.
1630 uint64_t addralign_
;
1631 // The section entry size.
1633 // The file offset is in the parent class.
1634 // Set the section link field to the index of this section.
1635 const Output_data
* link_section_
;
1636 // If link_section_ is NULL, this is the link field.
1638 // Set the section info field to the index of this section.
1639 const Output_data
* info_section_
;
1640 // If info_section_ is NULL, this is the section info field.
1642 // The section type.
1643 elfcpp::Elf_Word type_
;
1644 // The section flags.
1645 elfcpp::Elf_Xword flags_
;
1646 // The section index.
1647 unsigned int out_shndx_
;
1648 // If there is a STT_SECTION for this output section in the normal
1649 // symbol table, this is the symbol index. This starts out as zero.
1650 // It is initialized in Layout::finalize() to be the index, or -1U
1651 // if there isn't one.
1652 unsigned int symtab_index_
;
1653 // If there is a STT_SECTION for this output section in the dynamic
1654 // symbol table, this is the symbol index. This starts out as zero.
1655 // It is initialized in Layout::finalize() to be the index, or -1U
1656 // if there isn't one.
1657 unsigned int dynsym_index_
;
1658 // The input sections. This will be empty in cases where we don't
1659 // need to keep track of them.
1660 Input_section_list input_sections_
;
1661 // The offset of the first entry in input_sections_.
1662 off_t first_input_offset_
;
1663 // The fill data. This is separate from input_sections_ because we
1664 // often will need fill sections without needing to keep track of
1667 // Whether this output section needs a STT_SECTION symbol in the
1668 // normal symbol table. This will be true if there is a relocation
1670 bool needs_symtab_index_
: 1;
1671 // Whether this output section needs a STT_SECTION symbol in the
1672 // dynamic symbol table. This will be true if there is a dynamic
1673 // relocation which needs it.
1674 bool needs_dynsym_index_
: 1;
1675 // Whether the link field of this output section should point to the
1676 // normal symbol table.
1677 bool should_link_to_symtab_
: 1;
1678 // Whether the link field of this output section should point to the
1679 // dynamic symbol table.
1680 bool should_link_to_dynsym_
: 1;
1681 // Whether this section should be written after all the input
1682 // sections are complete.
1683 bool after_input_sections_
: 1;
1686 // An output segment. PT_LOAD segments are built from collections of
1687 // output sections. Other segments typically point within PT_LOAD
1688 // segments, and are built directly as needed.
1690 class Output_segment
1693 // Create an output segment, specifying the type and flags.
1694 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
1696 // Return the virtual address.
1699 { return this->vaddr_
; }
1701 // Return the physical address.
1704 { return this->paddr_
; }
1706 // Return the segment type.
1709 { return this->type_
; }
1711 // Return the segment flags.
1714 { return this->flags_
; }
1716 // Return the memory size.
1719 { return this->memsz_
; }
1721 // Return the file size.
1724 { return this->filesz_
; }
1726 // Return the maximum alignment of the Output_data.
1730 // Add an Output_section to this segment.
1732 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1733 { this->add_output_section(os
, seg_flags
, false); }
1735 // Add an Output_section to the start of this segment.
1737 add_initial_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
)
1738 { this->add_output_section(os
, seg_flags
, true); }
1740 // Add an Output_data (which is not an Output_section) to the start
1743 add_initial_output_data(Output_data
*);
1745 // Set the address of the segment to ADDR and the offset to *POFF
1746 // (aligned if necessary), and set the addresses and offsets of all
1747 // contained output sections accordingly. Set the section indexes
1748 // of all contained output sections starting with *PSHNDX. Return
1749 // the address of the immediately following segment. Update *POFF
1750 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1752 set_section_addresses(uint64_t addr
, off_t
* poff
, unsigned int* pshndx
);
1754 // Set the minimum alignment of this segment. This may be adjusted
1755 // upward based on the section alignments.
1757 set_minimum_addralign(uint64_t align
)
1759 gold_assert(!this->is_align_known_
);
1760 this->align_
= align
;
1763 // Set the offset of this segment based on the section. This should
1764 // only be called for a non-PT_LOAD segment.
1768 // Return the number of output sections.
1770 output_section_count() const;
1772 // Write the segment header into *OPHDR.
1773 template<int size
, bool big_endian
>
1775 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
1777 // Write the section headers of associated sections into V.
1778 template<int size
, bool big_endian
>
1780 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
1781 unsigned int* pshndx ACCEPT_SIZE_ENDIAN
) const;
1784 Output_segment(const Output_segment
&);
1785 Output_segment
& operator=(const Output_segment
&);
1787 typedef std::list
<Output_data
*> Output_data_list
;
1789 // Add an Output_section to this segment, specifying front or back.
1791 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
,
1794 // Find the maximum alignment in an Output_data_list.
1796 maximum_alignment(const Output_data_list
*);
1798 // Set the section addresses in an Output_data_list.
1800 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
,
1801 unsigned int* pshndx
);
1803 // Return the number of Output_sections in an Output_data_list.
1805 output_section_count_list(const Output_data_list
*) const;
1807 // Write the section headers in the list into V.
1808 template<int size
, bool big_endian
>
1810 write_section_headers_list(const Layout
*, const Stringpool
*,
1811 const Output_data_list
*, unsigned char* v
,
1812 unsigned int* pshdx ACCEPT_SIZE_ENDIAN
) const;
1814 // The list of output data with contents attached to this segment.
1815 Output_data_list output_data_
;
1816 // The list of output data without contents attached to this segment.
1817 Output_data_list output_bss_
;
1818 // The segment virtual address.
1820 // The segment physical address.
1822 // The size of the segment in memory.
1824 // The segment alignment. The is_align_known_ field indicates
1825 // whether this has been finalized. It can be set to a minimum
1826 // value before it is finalized.
1828 // The offset of the segment data within the file.
1830 // The size of the segment data in the file.
1832 // The segment type;
1833 elfcpp::Elf_Word type_
;
1834 // The segment flags.
1835 elfcpp::Elf_Word flags_
;
1836 // Whether we have finalized align_.
1837 bool is_align_known_
;
1840 // This class represents the output file.
1845 Output_file(const General_options
& options
, Target
*);
1847 // Get a pointer to the target.
1850 { return this->target_
; }
1852 // Open the output file. FILE_SIZE is the final size of the file.
1854 open(off_t file_size
);
1856 // Close the output file and make sure there are no error.
1860 // We currently always use mmap which makes the view handling quite
1861 // simple. In the future we may support other approaches.
1863 // Write data to the output file.
1865 write(off_t offset
, const void* data
, off_t len
)
1866 { memcpy(this->base_
+ offset
, data
, len
); }
1868 // Get a buffer to use to write to the file, given the offset into
1869 // the file and the size.
1871 get_output_view(off_t start
, off_t size
)
1873 gold_assert(start
>= 0 && size
>= 0 && start
+ size
<= this->file_size_
);
1874 return this->base_
+ start
;
1877 // VIEW must have been returned by get_output_view. Write the
1878 // buffer to the file, passing in the offset and the size.
1880 write_output_view(off_t
, off_t
, unsigned char*)
1883 // Get a read/write buffer. This is used when we want to write part
1884 // of the file, read it in, and write it again.
1886 get_input_output_view(off_t start
, off_t size
)
1887 { return this->get_output_view(start
, size
); }
1889 // Write a read/write buffer back to the file.
1891 write_input_output_view(off_t
, off_t
, unsigned char*)
1894 // Get a read buffer. This is used when we just want to read part
1895 // of the file back it in.
1896 const unsigned char*
1897 get_input_view(off_t start
, off_t size
)
1898 { return this->get_output_view(start
, size
); }
1900 // Release a read bfufer.
1902 free_input_view(off_t
, off_t
, const unsigned char*)
1907 const General_options
& options_
;
1916 // Base of file mapped into memory.
1917 unsigned char* base_
;
1920 } // End namespace gold.
1922 #endif // !defined(GOLD_OUTPUT_H)