1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "reloc-types.h"
36 class General_options;
41 class Relocatable_relocs;
43 template<int size, bool big_endian>
45 template<int size, bool big_endian>
48 // An abtract class for data which has to go into the output file.
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
68 gold_assert(this->is_address_valid_);
69 return this->address_;
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
88 gold_assert(this->is_offset_valid_);
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
95 reset_address_and_file_offset()
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
103 // Return the required alignment.
106 { return this->do_addralign(); }
108 // Return whether this has a load address.
110 has_load_address() const
111 { return this->do_has_load_address(); }
113 // Return the load address.
116 { return this->do_load_address(); }
118 // Return whether this is an Output_section.
121 { return this->do_is_section(); }
123 // Return whether this is an Output_section of the specified type.
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
128 // Return whether this is an Output_section with the specified flag
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
134 // Return the output section that this goes in, if there is one.
137 { return this->do_output_section(); }
139 // Return the output section index, if there is an output section.
142 { return this->do_out_shndx(); }
144 // Set the output section index, if this is an output section.
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
153 set_address_and_file_offset(uint64_t addr, off_t off)
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
162 set_address(uint64_t addr)
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
169 // Set the file offset.
171 set_file_offset(off_t off)
173 gold_assert(!this->is_offset_valid_);
175 this->is_offset_valid_ = true;
178 // Finalize the data size.
182 if (!this->is_data_size_valid_)
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
190 // Set the TLS offset. Called only for SHT_TLS sections.
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
199 { return this->do_tls_offset(); }
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
204 write(Output_file* file)
205 { this->do_write(file); }
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
211 { Output_data::allocated_sizes_are_fixed = true; }
213 // Used to check that layout has been done.
216 { return Output_data::allocated_sizes_are_fixed; }
218 // Count the number of dynamic relocations applied to this section.
221 { ++this->dynamic_reloc_count_; }
223 // Return the number of dynamic relocations applied to this section.
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
228 // Whether the address is valid.
230 is_address_valid() const
231 { return this->is_address_valid_; }
233 // Whether the file offset is valid.
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
238 // Whether the data size is valid.
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
244 // Functions that child classes may or in some cases must implement.
246 // Write the data to the output file.
248 do_write(Output_file*) = 0;
250 // Return the required alignment.
252 do_addralign() const = 0;
254 // Return whether this has a load address.
256 do_has_load_address() const
259 // Return the load address.
261 do_load_address() const
262 { gold_unreachable(); }
264 // Return whether this is an Output_section.
266 do_is_section() const
269 // Return whether this is an Output_section of the specified type.
270 // This only needs to be implement by Output_section.
272 do_is_section_type(elfcpp::Elf_Word) const
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
278 do_is_section_flag_set(elfcpp::Elf_Xword) const
281 // Return the output section, if there is one.
282 virtual Output_section*
286 // Return the output section index, if there is an output section.
289 { gold_unreachable(); }
291 // Set the output section index, if this is an output section.
293 do_set_out_shndx(unsigned int)
294 { gold_unreachable(); }
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
300 set_final_data_size()
301 { gold_unreachable(); }
303 // A hook for resetting the address and file offset.
305 do_reset_address_and_file_offset()
308 // Set the TLS offset. Called only for SHT_TLS sections.
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
316 do_tls_offset() const
317 { gold_unreachable(); }
319 // Functions that child classes may call.
321 // Set the size of the data.
323 set_data_size(off_t data_size)
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
333 current_data_size_for_child() const
334 { return this->data_size_; }
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
339 set_current_data_size_for_child(off_t data_size)
341 gold_assert(!this->is_data_size_valid_);
342 this->data_size_ = data_size;
345 // Return default alignment for the target size.
349 // Return default alignment for a specified size--32 or 64.
351 default_alignment_for_size(int size);
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
357 // This is used for verification, to make sure that we don't try to
358 // change any sizes of allocated sections after we set the section
360 static bool allocated_sizes_are_fixed;
362 // Memory address in output file.
364 // Size of data in output file.
366 // File offset of contents in output file.
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
378 // Output the section headers.
380 class Output_section_headers : public Output_data
383 Output_section_headers(const Layout*,
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
386 const Layout::Section_list*,
390 // Write the data to the file.
392 do_write(Output_file*);
394 // Return the required alignment.
397 { return Output_data::default_alignment(); }
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
403 do_sized_write(Output_file*);
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
407 const Layout::Section_list* section_list_;
408 const Layout::Section_list* unattached_section_list_;
409 const Stringpool* secnamepool_;
412 // Output the segment headers.
414 class Output_segment_headers : public Output_data
417 Output_segment_headers(const Layout::Segment_list& segment_list);
420 // Write the data to the file.
422 do_write(Output_file*);
424 // Return the required alignment.
427 { return Output_data::default_alignment(); }
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
433 do_sized_write(Output_file*);
435 const Layout::Segment_list& segment_list_;
438 // Output the ELF file header.
440 class Output_file_header : public Output_data
443 Output_file_header(const Target*,
445 const Output_segment_headers*,
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
454 // Write the data to the file.
456 do_write(Output_file*);
458 // Return the required alignment.
461 { return Output_data::default_alignment(); }
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
467 do_sized_write(Output_file*);
469 // Return the value to use for the entry address.
471 typename elfcpp::Elf_types<size>::Elf_Addr
474 const Target* target_;
475 const Symbol_table* symtab_;
476 const Output_segment_headers* segment_header_;
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
482 // Output sections are mainly comprised of input sections. However,
483 // there are cases where we have data to write out which is not in an
484 // input section. Output_section_data is used in such cases. This is
485 // an abstract base class.
487 class Output_section_data : public Output_data
490 Output_section_data(off_t data_size, uint64_t addralign)
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
494 Output_section_data(uint64_t addralign)
495 : Output_data(), output_section_(NULL), addralign_(addralign)
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
503 // Record the output section.
505 set_output_section(Output_section* os);
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
523 { return this->do_output_offset(object, shndx, offset, poutput); }
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
545 // The child class must implement do_write.
547 // The child class may implement specific adjustments to the output
550 do_adjust_output_section(Output_section*)
553 // May be implemented by child class. Return true if the section
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
559 // The child class may implement output_offset.
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
565 // The child class may implement is_merge_section_for.
567 do_is_merge_section_for(const Relobj*, unsigned int) const
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
577 // Print merge statistics.
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
582 // Return the required alignment.
585 { return this->addralign_; }
587 // Return the output section.
590 { return this->output_section_; }
592 // Return the section index of the output section.
594 do_out_shndx() const;
596 // Set the alignment.
598 set_addralign(uint64_t addralign);
601 // The output section for this section.
602 Output_section* output_section_;
603 // The required alignment.
607 // Some Output_section_data classes build up their data step by step,
608 // rather than all at once. This class provides an interface for
611 class Output_section_data_build : public Output_section_data
614 Output_section_data_build(uint64_t addralign)
615 : Output_section_data(addralign)
618 // Get the current data size.
620 current_data_size() const
621 { return this->current_data_size_for_child(); }
623 // Set the current data size.
625 set_current_data_size(off_t data_size)
626 { this->set_current_data_size_for_child(data_size); }
629 // Set the final data size.
631 set_final_data_size()
632 { this->set_data_size(this->current_data_size_for_child()); }
635 // A simple case of Output_data in which we have constant data to
638 class Output_data_const : public Output_section_data
641 Output_data_const(const std::string& data, uint64_t addralign)
642 : Output_section_data(data.size(), addralign), data_(data)
645 Output_data_const(const char* p, off_t len, uint64_t addralign)
646 : Output_section_data(len, addralign), data_(p, len)
649 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
650 : Output_section_data(len, addralign),
651 data_(reinterpret_cast<const char*>(p), len)
655 // Write the data to the output file.
657 do_write(Output_file*);
659 // Write the data to a buffer.
661 do_write_to_buffer(unsigned char* buffer)
662 { memcpy(buffer, this->data_.data(), this->data_.size()); }
668 // Another version of Output_data with constant data, in which the
669 // buffer is allocated by the caller.
671 class Output_data_const_buffer : public Output_section_data
674 Output_data_const_buffer(const unsigned char* p, off_t len,
676 : Output_section_data(len, addralign), p_(p)
680 // Write the data the output file.
682 do_write(Output_file*);
684 // Write the data to a buffer.
686 do_write_to_buffer(unsigned char* buffer)
687 { memcpy(buffer, this->p_, this->data_size()); }
690 const unsigned char* p_;
693 // A place holder for a fixed amount of data written out via some
696 class Output_data_fixed_space : public Output_section_data
699 Output_data_fixed_space(off_t data_size, uint64_t addralign)
700 : Output_section_data(data_size, addralign)
704 // Write out the data--the actual data must be written out
707 do_write(Output_file*)
711 // A place holder for variable sized data written out via some other
714 class Output_data_space : public Output_section_data_build
717 explicit Output_data_space(uint64_t addralign)
718 : Output_section_data_build(addralign)
721 // Set the alignment.
723 set_space_alignment(uint64_t align)
724 { this->set_addralign(align); }
727 // Write out the data--the actual data must be written out
730 do_write(Output_file*)
734 // A string table which goes into an output section.
736 class Output_data_strtab : public Output_section_data
739 Output_data_strtab(Stringpool* strtab)
740 : Output_section_data(1), strtab_(strtab)
744 // This is called to set the address and file offset. Here we make
745 // sure that the Stringpool is finalized.
747 set_final_data_size();
749 // Write out the data.
751 do_write(Output_file*);
753 // Write the data to a buffer.
755 do_write_to_buffer(unsigned char* buffer)
756 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
762 // This POD class is used to represent a single reloc in the output
763 // file. This could be a private class within Output_data_reloc, but
764 // the templatization is complex enough that I broke it out into a
765 // separate class. The class is templatized on either elfcpp::SHT_REL
766 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
767 // relocation or an ordinary relocation.
769 // A relocation can be against a global symbol, a local symbol, a
770 // local section symbol, an output section, or the undefined symbol at
771 // index 0. We represent the latter by using a NULL global symbol.
773 template<int sh_type, bool dynamic, int size, bool big_endian>
776 template<bool dynamic, int size, bool big_endian>
777 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
780 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
783 // An uninitialized entry. We need this because we want to put
784 // instances of this class into an STL container.
786 : local_sym_index_(INVALID_CODE)
789 // We have a bunch of different constructors. They come in pairs
790 // depending on how the address of the relocation is specified. It
791 // can either be an offset in an Output_data or an offset in an
794 // A reloc against a global symbol.
796 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
797 Address address, bool is_relative);
799 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
800 unsigned int shndx, Address address, bool is_relative);
802 // A reloc against a local symbol or local section symbol.
804 Output_reloc(Sized_relobj<size, big_endian>* relobj,
805 unsigned int local_sym_index, unsigned int type,
806 Output_data* od, Address address, bool is_relative,
807 bool is_section_symbol);
809 Output_reloc(Sized_relobj<size, big_endian>* relobj,
810 unsigned int local_sym_index, unsigned int type,
811 unsigned int shndx, Address address, bool is_relative,
812 bool is_section_symbol);
814 // A reloc against the STT_SECTION symbol of an output section.
816 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
819 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
820 unsigned int shndx, Address address);
822 // Return TRUE if this is a RELATIVE relocation.
825 { return this->is_relative_; }
827 // Return whether this is against a local section symbol.
829 is_local_section_symbol() const
831 return (this->local_sym_index_ != GSYM_CODE
832 && this->local_sym_index_ != SECTION_CODE
833 && this->local_sym_index_ != INVALID_CODE
834 && this->is_section_symbol_);
837 // For a local section symbol, return the offset of the input
838 // section within the output section. ADDEND is the addend being
839 // applied to the input section.
841 local_section_offset(Addend addend) const;
843 // Get the value of the symbol referred to by a Rel relocation when
844 // we are adding the given ADDEND.
846 symbol_value(Addend addend) const;
848 // Write the reloc entry to an output view.
850 write(unsigned char* pov) const;
852 // Write the offset and info fields to Write_rel.
853 template<typename Write_rel>
854 void write_rel(Write_rel*) const;
857 // Record that we need a dynamic symbol index.
859 set_needs_dynsym_index();
861 // Return the symbol index.
863 get_symbol_index() const;
865 // Codes for local_sym_index_.
872 // Invalid uninitialized entry.
878 // For a local symbol or local section symbol
879 // (this->local_sym_index_ >= 0), the object. We will never
880 // generate a relocation against a local symbol in a dynamic
881 // object; that doesn't make sense. And our callers will always
882 // be templatized, so we use Sized_relobj here.
883 Sized_relobj<size, big_endian>* relobj;
884 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
885 // symbol. If this is NULL, it indicates a relocation against the
886 // undefined 0 symbol.
888 // For a relocation against an output section
889 // (this->local_sym_index_ == SECTION_CODE), the output section.
894 // If this->shndx_ is not INVALID CODE, the object which holds the
895 // input section being used to specify the reloc address.
897 // If this->shndx_ is INVALID_CODE, the output data being used to
898 // specify the reloc address. This may be NULL if the reloc
899 // address is absolute.
902 // The address offset within the input section or the Output_data.
904 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
905 // relocation against an output section, or INVALID_CODE for an
906 // uninitialized value. Otherwise, for a local symbol
907 // (this->is_section_symbol_ is false), the local symbol index. For
908 // a local section symbol (this->is_section_symbol_ is true), the
909 // section index in the input file.
910 unsigned int local_sym_index_;
911 // The reloc type--a processor specific code.
912 unsigned int type_ : 30;
913 // True if the relocation is a RELATIVE relocation.
914 bool is_relative_ : 1;
915 // True if the relocation is against a section symbol.
916 bool is_section_symbol_ : 1;
917 // If the reloc address is an input section in an object, the
918 // section index. This is INVALID_CODE if the reloc address is
919 // specified in some other way.
923 // The SHT_RELA version of Output_reloc<>. This is just derived from
924 // the SHT_REL version of Output_reloc, but it adds an addend.
926 template<bool dynamic, int size, bool big_endian>
927 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
930 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
931 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
933 // An uninitialized entry.
938 // A reloc against a global symbol.
940 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
941 Address address, Addend addend, bool is_relative)
942 : rel_(gsym, type, od, address, is_relative), addend_(addend)
945 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
946 unsigned int shndx, Address address, Addend addend,
948 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
951 // A reloc against a local symbol.
953 Output_reloc(Sized_relobj<size, big_endian>* relobj,
954 unsigned int local_sym_index, unsigned int type,
955 Output_data* od, Address address,
956 Addend addend, bool is_relative, bool is_section_symbol)
957 : rel_(relobj, local_sym_index, type, od, address, is_relative,
962 Output_reloc(Sized_relobj<size, big_endian>* relobj,
963 unsigned int local_sym_index, unsigned int type,
964 unsigned int shndx, Address address,
965 Addend addend, bool is_relative, bool is_section_symbol)
966 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
971 // A reloc against the STT_SECTION symbol of an output section.
973 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
974 Address address, Addend addend)
975 : rel_(os, type, od, address), addend_(addend)
978 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
979 unsigned int shndx, Address address, Addend addend)
980 : rel_(os, type, relobj, shndx, address), addend_(addend)
983 // Write the reloc entry to an output view.
985 write(unsigned char* pov) const;
989 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
994 // Output_data_reloc is used to manage a section containing relocs.
995 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
996 // indicates whether this is a dynamic relocation or a normal
997 // relocation. Output_data_reloc_base is a base class.
998 // Output_data_reloc is the real class, which we specialize based on
1001 template<int sh_type, bool dynamic, int size, bool big_endian>
1002 class Output_data_reloc_base : public Output_section_data_build
1005 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1006 typedef typename Output_reloc_type::Address Address;
1007 static const int reloc_size =
1008 Reloc_types<sh_type, size, big_endian>::reloc_size;
1010 // Construct the section.
1011 Output_data_reloc_base()
1012 : Output_section_data_build(Output_data::default_alignment_for_size(size))
1016 // Write out the data.
1018 do_write(Output_file*);
1020 // Set the entry size and the link.
1022 do_adjust_output_section(Output_section *os);
1024 // Add a relocation entry.
1026 add(Output_data *od, const Output_reloc_type& reloc)
1028 this->relocs_.push_back(reloc);
1029 this->set_current_data_size(this->relocs_.size() * reloc_size);
1030 od->add_dynamic_reloc();
1034 typedef std::vector<Output_reloc_type> Relocs;
1039 // The class which callers actually create.
1041 template<int sh_type, bool dynamic, int size, bool big_endian>
1042 class Output_data_reloc;
1044 // The SHT_REL version of Output_data_reloc.
1046 template<bool dynamic, int size, bool big_endian>
1047 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1048 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1051 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1055 typedef typename Base::Output_reloc_type Output_reloc_type;
1056 typedef typename Output_reloc_type::Address Address;
1059 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1062 // Add a reloc against a global symbol.
1065 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1066 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1069 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1070 unsigned int shndx, Address address)
1071 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1074 // Add a RELATIVE reloc against a global symbol. The final relocation
1075 // will not reference the symbol.
1078 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1080 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1083 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1084 Relobj* relobj, unsigned int shndx, Address address)
1086 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1090 // Add a reloc against a local symbol.
1093 add_local(Sized_relobj<size, big_endian>* relobj,
1094 unsigned int local_sym_index, unsigned int type,
1095 Output_data* od, Address address)
1097 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1098 address, false, false));
1102 add_local(Sized_relobj<size, big_endian>* relobj,
1103 unsigned int local_sym_index, unsigned int type,
1104 Output_data* od, unsigned int shndx, Address address)
1106 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1107 address, false, false));
1110 // Add a RELATIVE reloc against a local symbol.
1113 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1114 unsigned int local_sym_index, unsigned int type,
1115 Output_data* od, Address address)
1117 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1118 address, true, false));
1122 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1123 unsigned int local_sym_index, unsigned int type,
1124 Output_data* od, unsigned int shndx, Address address)
1126 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1127 address, true, false));
1130 // Add a reloc against a local section symbol. This will be
1131 // converted into a reloc against the STT_SECTION symbol of the
1135 add_local_section(Sized_relobj<size, big_endian>* relobj,
1136 unsigned int input_shndx, unsigned int type,
1137 Output_data* od, Address address)
1139 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1140 address, false, true));
1144 add_local_section(Sized_relobj<size, big_endian>* relobj,
1145 unsigned int input_shndx, unsigned int type,
1146 Output_data* od, unsigned int shndx, Address address)
1148 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1149 address, false, true));
1152 // A reloc against the STT_SECTION symbol of an output section.
1153 // OS is the Output_section that the relocation refers to; OD is
1154 // the Output_data object being relocated.
1157 add_output_section(Output_section* os, unsigned int type,
1158 Output_data* od, Address address)
1159 { this->add(od, Output_reloc_type(os, type, od, address)); }
1162 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1163 Relobj* relobj, unsigned int shndx, Address address)
1164 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1167 // The SHT_RELA version of Output_data_reloc.
1169 template<bool dynamic, int size, bool big_endian>
1170 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1171 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1174 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1178 typedef typename Base::Output_reloc_type Output_reloc_type;
1179 typedef typename Output_reloc_type::Address Address;
1180 typedef typename Output_reloc_type::Addend Addend;
1183 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1186 // Add a reloc against a global symbol.
1189 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1190 Address address, Addend addend)
1191 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1195 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1196 unsigned int shndx, Address address,
1198 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1201 // Add a RELATIVE reloc against a global symbol. The final output
1202 // relocation will not reference the symbol, but we must keep the symbol
1203 // information long enough to set the addend of the relocation correctly
1204 // when it is written.
1207 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1208 Address address, Addend addend)
1209 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1212 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1213 Relobj* relobj, unsigned int shndx, Address address,
1215 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1218 // Add a reloc against a local symbol.
1221 add_local(Sized_relobj<size, big_endian>* relobj,
1222 unsigned int local_sym_index, unsigned int type,
1223 Output_data* od, Address address, Addend addend)
1225 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1226 addend, false, false));
1230 add_local(Sized_relobj<size, big_endian>* relobj,
1231 unsigned int local_sym_index, unsigned int type,
1232 Output_data* od, unsigned int shndx, Address address,
1235 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1236 address, addend, false, false));
1239 // Add a RELATIVE reloc against a local symbol.
1242 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1243 unsigned int local_sym_index, unsigned int type,
1244 Output_data* od, Address address, Addend addend)
1246 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1247 addend, true, false));
1251 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1252 unsigned int local_sym_index, unsigned int type,
1253 Output_data* od, unsigned int shndx, Address address,
1256 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1257 address, addend, true, false));
1260 // Add a reloc against a local section symbol. This will be
1261 // converted into a reloc against the STT_SECTION symbol of the
1265 add_local_section(Sized_relobj<size, big_endian>* relobj,
1266 unsigned int input_shndx, unsigned int type,
1267 Output_data* od, Address address, Addend addend)
1269 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1270 addend, false, true));
1274 add_local_section(Sized_relobj<size, big_endian>* relobj,
1275 unsigned int input_shndx, unsigned int type,
1276 Output_data* od, unsigned int shndx, Address address,
1279 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1280 address, addend, false, true));
1283 // A reloc against the STT_SECTION symbol of an output section.
1286 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1287 Address address, Addend addend)
1288 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1291 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1292 unsigned int shndx, Address address, Addend addend)
1293 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1297 // Output_relocatable_relocs represents a relocation section in a
1298 // relocatable link. The actual data is written out in the target
1299 // hook relocate_for_relocatable. This just saves space for it.
1301 template<int sh_type, int size, bool big_endian>
1302 class Output_relocatable_relocs : public Output_section_data
1305 Output_relocatable_relocs(Relocatable_relocs* rr)
1306 : Output_section_data(Output_data::default_alignment_for_size(size)),
1311 set_final_data_size();
1313 // Write out the data. There is nothing to do here.
1315 do_write(Output_file*)
1319 // The relocs associated with this input section.
1320 Relocatable_relocs* rr_;
1323 // Handle a GROUP section.
1325 template<int size, bool big_endian>
1326 class Output_data_group : public Output_section_data
1329 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1330 section_size_type entry_count,
1331 const elfcpp::Elf_Word* contents);
1334 do_write(Output_file*);
1337 // The input object.
1338 Sized_relobj<size, big_endian>* relobj_;
1339 // The group flag word.
1340 elfcpp::Elf_Word flags_;
1341 // The section indexes of the input sections in this group.
1342 std::vector<unsigned int> input_sections_;
1345 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1346 // for one symbol--either a global symbol or a local symbol in an
1347 // object. The target specific code adds entries to the GOT as
1350 template<int size, bool big_endian>
1351 class Output_data_got : public Output_section_data_build
1354 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1355 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1356 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1359 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1363 // Add an entry for a global symbol to the GOT. Return true if this
1364 // is a new GOT entry, false if the symbol was already in the GOT.
1366 add_global(Symbol* gsym, unsigned int got_type);
1368 // Add an entry for a global symbol to the GOT, and add a dynamic
1369 // relocation of type R_TYPE for the GOT entry.
1371 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1372 Rel_dyn* rel_dyn, unsigned int r_type);
1375 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1376 Rela_dyn* rela_dyn, unsigned int r_type);
1378 // Add a pair of entries for a global symbol to the GOT, and add
1379 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1381 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1382 Rel_dyn* rel_dyn, unsigned int r_type_1,
1383 unsigned int r_type_2);
1386 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1387 Rela_dyn* rela_dyn, unsigned int r_type_1,
1388 unsigned int r_type_2);
1390 // Add an entry for a local symbol to the GOT. This returns true if
1391 // this is a new GOT entry, false if the symbol already has a GOT
1394 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1395 unsigned int got_type);
1397 // Add an entry for a local symbol to the GOT, and add a dynamic
1398 // relocation of type R_TYPE for the GOT entry.
1400 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1401 unsigned int sym_index, unsigned int got_type,
1402 Rel_dyn* rel_dyn, unsigned int r_type);
1405 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1406 unsigned int sym_index, unsigned int got_type,
1407 Rela_dyn* rela_dyn, unsigned int r_type);
1409 // Add a pair of entries for a local symbol to the GOT, and add
1410 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1412 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1413 unsigned int sym_index, unsigned int shndx,
1414 unsigned int got_type, Rel_dyn* rel_dyn,
1415 unsigned int r_type_1, unsigned int r_type_2);
1418 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1419 unsigned int sym_index, unsigned int shndx,
1420 unsigned int got_type, Rela_dyn* rela_dyn,
1421 unsigned int r_type_1, unsigned int r_type_2);
1423 // Add a constant to the GOT. This returns the offset of the new
1424 // entry from the start of the GOT.
1426 add_constant(Valtype constant)
1428 this->entries_.push_back(Got_entry(constant));
1429 this->set_got_size();
1430 return this->last_got_offset();
1434 // Write out the GOT table.
1436 do_write(Output_file*);
1439 // This POD class holds a single GOT entry.
1443 // Create a zero entry.
1445 : local_sym_index_(CONSTANT_CODE)
1446 { this->u_.constant = 0; }
1448 // Create a global symbol entry.
1449 explicit Got_entry(Symbol* gsym)
1450 : local_sym_index_(GSYM_CODE)
1451 { this->u_.gsym = gsym; }
1453 // Create a local symbol entry.
1454 Got_entry(Sized_relobj<size, big_endian>* object,
1455 unsigned int local_sym_index)
1456 : local_sym_index_(local_sym_index)
1458 gold_assert(local_sym_index != GSYM_CODE
1459 && local_sym_index != CONSTANT_CODE);
1460 this->u_.object = object;
1463 // Create a constant entry. The constant is a host value--it will
1464 // be swapped, if necessary, when it is written out.
1465 explicit Got_entry(Valtype constant)
1466 : local_sym_index_(CONSTANT_CODE)
1467 { this->u_.constant = constant; }
1469 // Write the GOT entry to an output view.
1471 write(unsigned char* pov) const;
1482 // For a local symbol, the object.
1483 Sized_relobj<size, big_endian>* object;
1484 // For a global symbol, the symbol.
1486 // For a constant, the constant.
1489 // For a local symbol, the local symbol index. This is GSYM_CODE
1490 // for a global symbol, or CONSTANT_CODE for a constant.
1491 unsigned int local_sym_index_;
1494 typedef std::vector<Got_entry> Got_entries;
1496 // Return the offset into the GOT of GOT entry I.
1498 got_offset(unsigned int i) const
1499 { return i * (size / 8); }
1501 // Return the offset into the GOT of the last entry added.
1503 last_got_offset() const
1504 { return this->got_offset(this->entries_.size() - 1); }
1506 // Set the size of the section.
1509 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1511 // The list of GOT entries.
1512 Got_entries entries_;
1515 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1518 class Output_data_dynamic : public Output_section_data
1521 Output_data_dynamic(Stringpool* pool)
1522 : Output_section_data(Output_data::default_alignment()),
1523 entries_(), pool_(pool)
1526 // Add a new dynamic entry with a fixed numeric value.
1528 add_constant(elfcpp::DT tag, unsigned int val)
1529 { this->add_entry(Dynamic_entry(tag, val)); }
1531 // Add a new dynamic entry with the address of output data.
1533 add_section_address(elfcpp::DT tag, const Output_data* od)
1534 { this->add_entry(Dynamic_entry(tag, od, false)); }
1536 // Add a new dynamic entry with the address of output data
1537 // plus a constant offset.
1539 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1540 unsigned int offset)
1541 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1543 // Add a new dynamic entry with the size of output data.
1545 add_section_size(elfcpp::DT tag, const Output_data* od)
1546 { this->add_entry(Dynamic_entry(tag, od, true)); }
1548 // Add a new dynamic entry with the address of a symbol.
1550 add_symbol(elfcpp::DT tag, const Symbol* sym)
1551 { this->add_entry(Dynamic_entry(tag, sym)); }
1553 // Add a new dynamic entry with a string.
1555 add_string(elfcpp::DT tag, const char* str)
1556 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1559 add_string(elfcpp::DT tag, const std::string& str)
1560 { this->add_string(tag, str.c_str()); }
1563 // Adjust the output section to set the entry size.
1565 do_adjust_output_section(Output_section*);
1567 // Set the final data size.
1569 set_final_data_size();
1571 // Write out the dynamic entries.
1573 do_write(Output_file*);
1576 // This POD class holds a single dynamic entry.
1580 // Create an entry with a fixed numeric value.
1581 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1582 : tag_(tag), offset_(DYNAMIC_NUMBER)
1583 { this->u_.val = val; }
1585 // Create an entry with the size or address of a section.
1586 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1588 offset_(section_size
1589 ? DYNAMIC_SECTION_SIZE
1590 : DYNAMIC_SECTION_ADDRESS)
1591 { this->u_.od = od; }
1593 // Create an entry with the address of a section plus a constant offset.
1594 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1597 { this->u_.od = od; }
1599 // Create an entry with the address of a symbol.
1600 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1601 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1602 { this->u_.sym = sym; }
1604 // Create an entry with a string.
1605 Dynamic_entry(elfcpp::DT tag, const char* str)
1606 : tag_(tag), offset_(DYNAMIC_STRING)
1607 { this->u_.str = str; }
1609 // Write the dynamic entry to an output view.
1610 template<int size, bool big_endian>
1612 write(unsigned char* pov, const Stringpool*) const;
1615 // Classification is encoded in the OFFSET field.
1619 DYNAMIC_SECTION_ADDRESS = 0,
1621 DYNAMIC_NUMBER = -1U,
1623 DYNAMIC_SECTION_SIZE = -2U,
1625 DYNAMIC_SYMBOL = -3U,
1627 DYNAMIC_STRING = -4U
1628 // Any other value indicates a section address plus OFFSET.
1633 // For DYNAMIC_NUMBER.
1635 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1636 const Output_data* od;
1637 // For DYNAMIC_SYMBOL.
1639 // For DYNAMIC_STRING.
1644 // The type of entry (Classification) or offset within a section.
1645 unsigned int offset_;
1648 // Add an entry to the list.
1650 add_entry(const Dynamic_entry& entry)
1651 { this->entries_.push_back(entry); }
1653 // Sized version of write function.
1654 template<int size, bool big_endian>
1656 sized_write(Output_file* of);
1658 // The type of the list of entries.
1659 typedef std::vector<Dynamic_entry> Dynamic_entries;
1662 Dynamic_entries entries_;
1663 // The pool used for strings.
1667 // An output section. We don't expect to have too many output
1668 // sections, so we don't bother to do a template on the size.
1670 class Output_section : public Output_data
1673 // Create an output section, giving the name, type, and flags.
1674 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1675 virtual ~Output_section();
1677 // Add a new input section SHNDX, named NAME, with header SHDR, from
1678 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1679 // which applies to this section, or 0 if none, or -1U if more than
1680 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1681 // in a linker script; in that case we need to keep track of input
1682 // sections associated with an output section. Return the offset
1683 // within the output section.
1684 template<int size, bool big_endian>
1686 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1688 const elfcpp::Shdr<size, big_endian>& shdr,
1689 unsigned int reloc_shndx, bool have_sections_script);
1691 // Add generated data POSD to this output section.
1693 add_output_section_data(Output_section_data* posd);
1695 // Return the section name.
1698 { return this->name_; }
1700 // Return the section type.
1703 { return this->type_; }
1705 // Return the section flags.
1708 { return this->flags_; }
1710 // Set the section flags. This may only be used with the Layout
1711 // code when it is prepared to move the section to a different
1714 set_flags(elfcpp::Elf_Xword flags)
1715 { this->flags_ = flags; }
1717 // Return the entsize field.
1720 { return this->entsize_; }
1722 // Set the entsize field.
1724 set_entsize(uint64_t v);
1726 // Set the load address.
1728 set_load_address(uint64_t load_address)
1730 this->load_address_ = load_address;
1731 this->has_load_address_ = true;
1734 // Set the link field to the output section index of a section.
1736 set_link_section(const Output_data* od)
1738 gold_assert(this->link_ == 0
1739 && !this->should_link_to_symtab_
1740 && !this->should_link_to_dynsym_);
1741 this->link_section_ = od;
1744 // Set the link field to a constant.
1746 set_link(unsigned int v)
1748 gold_assert(this->link_section_ == NULL
1749 && !this->should_link_to_symtab_
1750 && !this->should_link_to_dynsym_);
1754 // Record that this section should link to the normal symbol table.
1756 set_should_link_to_symtab()
1758 gold_assert(this->link_section_ == NULL
1760 && !this->should_link_to_dynsym_);
1761 this->should_link_to_symtab_ = true;
1764 // Record that this section should link to the dynamic symbol table.
1766 set_should_link_to_dynsym()
1768 gold_assert(this->link_section_ == NULL
1770 && !this->should_link_to_symtab_);
1771 this->should_link_to_dynsym_ = true;
1774 // Return the info field.
1778 gold_assert(this->info_section_ == NULL
1779 && this->info_symndx_ == NULL);
1783 // Set the info field to the output section index of a section.
1785 set_info_section(const Output_section* os)
1787 gold_assert((this->info_section_ == NULL
1788 || (this->info_section_ == os
1789 && this->info_uses_section_index_))
1790 && this->info_symndx_ == NULL
1791 && this->info_ == 0);
1792 this->info_section_ = os;
1793 this->info_uses_section_index_= true;
1796 // Set the info field to the symbol table index of a symbol.
1798 set_info_symndx(const Symbol* sym)
1800 gold_assert(this->info_section_ == NULL
1801 && (this->info_symndx_ == NULL
1802 || this->info_symndx_ == sym)
1803 && this->info_ == 0);
1804 this->info_symndx_ = sym;
1807 // Set the info field to the symbol table index of a section symbol.
1809 set_info_section_symndx(const Output_section* os)
1811 gold_assert((this->info_section_ == NULL
1812 || (this->info_section_ == os
1813 && !this->info_uses_section_index_))
1814 && this->info_symndx_ == NULL
1815 && this->info_ == 0);
1816 this->info_section_ = os;
1817 this->info_uses_section_index_ = false;
1820 // Set the info field to a constant.
1822 set_info(unsigned int v)
1824 gold_assert(this->info_section_ == NULL
1825 && this->info_symndx_ == NULL
1826 && (this->info_ == 0
1827 || this->info_ == v));
1831 // Set the addralign field.
1833 set_addralign(uint64_t v)
1834 { this->addralign_ = v; }
1836 // Indicate that we need a symtab index.
1838 set_needs_symtab_index()
1839 { this->needs_symtab_index_ = true; }
1841 // Return whether we need a symtab index.
1843 needs_symtab_index() const
1844 { return this->needs_symtab_index_; }
1846 // Get the symtab index.
1848 symtab_index() const
1850 gold_assert(this->symtab_index_ != 0);
1851 return this->symtab_index_;
1854 // Set the symtab index.
1856 set_symtab_index(unsigned int index)
1858 gold_assert(index != 0);
1859 this->symtab_index_ = index;
1862 // Indicate that we need a dynsym index.
1864 set_needs_dynsym_index()
1865 { this->needs_dynsym_index_ = true; }
1867 // Return whether we need a dynsym index.
1869 needs_dynsym_index() const
1870 { return this->needs_dynsym_index_; }
1872 // Get the dynsym index.
1874 dynsym_index() const
1876 gold_assert(this->dynsym_index_ != 0);
1877 return this->dynsym_index_;
1880 // Set the dynsym index.
1882 set_dynsym_index(unsigned int index)
1884 gold_assert(index != 0);
1885 this->dynsym_index_ = index;
1888 // Return whether the input sections sections attachd to this output
1889 // section may require sorting. This is used to handle constructor
1890 // priorities compatibly with GNU ld.
1892 may_sort_attached_input_sections() const
1893 { return this->may_sort_attached_input_sections_; }
1895 // Record that the input sections attached to this output section
1896 // may require sorting.
1898 set_may_sort_attached_input_sections()
1899 { this->may_sort_attached_input_sections_ = true; }
1901 // Return whether the input sections attached to this output section
1902 // require sorting. This is used to handle constructor priorities
1903 // compatibly with GNU ld.
1905 must_sort_attached_input_sections() const
1906 { return this->must_sort_attached_input_sections_; }
1908 // Record that the input sections attached to this output section
1911 set_must_sort_attached_input_sections()
1912 { this->must_sort_attached_input_sections_ = true; }
1914 // Return whether this section should be written after all the input
1915 // sections are complete.
1917 after_input_sections() const
1918 { return this->after_input_sections_; }
1920 // Record that this section should be written after all the input
1921 // sections are complete.
1923 set_after_input_sections()
1924 { this->after_input_sections_ = true; }
1926 // Return whether this section requires postprocessing after all
1927 // relocations have been applied.
1929 requires_postprocessing() const
1930 { return this->requires_postprocessing_; }
1932 // If a section requires postprocessing, return the buffer to use.
1934 postprocessing_buffer() const
1936 gold_assert(this->postprocessing_buffer_ != NULL);
1937 return this->postprocessing_buffer_;
1940 // If a section requires postprocessing, create the buffer to use.
1942 create_postprocessing_buffer();
1944 // If a section requires postprocessing, this is the size of the
1945 // buffer to which relocations should be applied.
1947 postprocessing_buffer_size() const
1948 { return this->current_data_size_for_child(); }
1950 // Modify the section name. This is only permitted for an
1951 // unallocated section, and only before the size has been finalized.
1952 // Otherwise the name will not get into Layout::namepool_.
1954 set_name(const char* newname)
1956 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1957 gold_assert(!this->is_data_size_valid());
1958 this->name_ = newname;
1961 // Return whether the offset OFFSET in the input section SHNDX in
1962 // object OBJECT is being included in the link.
1964 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1965 off_t offset) const;
1967 // Return the offset within the output section of OFFSET relative to
1968 // the start of input section SHNDX in object OBJECT.
1970 output_offset(const Relobj* object, unsigned int shndx,
1971 section_offset_type offset) const;
1973 // Return the output virtual address of OFFSET relative to the start
1974 // of input section SHNDX in object OBJECT.
1976 output_address(const Relobj* object, unsigned int shndx,
1977 off_t offset) const;
1979 // Return the output address of the start of the merged section for
1980 // input section SHNDX in object OBJECT. This is not necessarily
1981 // the offset corresponding to input offset 0 in the section, since
1982 // the section may be mapped arbitrarily.
1984 starting_output_address(const Relobj* object, unsigned int shndx) const;
1986 // Record that this output section was found in the SECTIONS clause
1987 // of a linker script.
1989 set_found_in_sections_clause()
1990 { this->found_in_sections_clause_ = true; }
1992 // Return whether this output section was found in the SECTIONS
1993 // clause of a linker script.
1995 found_in_sections_clause() const
1996 { return this->found_in_sections_clause_; }
1998 // Write the section header into *OPHDR.
1999 template<int size, bool big_endian>
2001 write_header(const Layout*, const Stringpool*,
2002 elfcpp::Shdr_write<size, big_endian>*) const;
2004 // The next few calls are for linker script support.
2006 // Store the list of input sections for this Output_section into the
2007 // list passed in. This removes the input sections, leaving only
2008 // any Output_section_data elements. This returns the size of those
2009 // Output_section_data elements. ADDRESS is the address of this
2010 // output section. FILL is the fill value to use, in case there are
2011 // any spaces between the remaining Output_section_data elements.
2013 get_input_sections(uint64_t address, const std::string& fill,
2014 std::list<std::pair<Relobj*, unsigned int > >*);
2016 // Add an input section from a script.
2018 add_input_section_for_script(Relobj* object, unsigned int shndx,
2019 off_t data_size, uint64_t addralign);
2021 // Set the current size of the output section.
2023 set_current_data_size(off_t size)
2024 { this->set_current_data_size_for_child(size); }
2026 // Get the current size of the output section.
2028 current_data_size() const
2029 { return this->current_data_size_for_child(); }
2031 // End of linker script support.
2033 // Print merge statistics to stderr.
2035 print_merge_stats();
2038 // Return the output section--i.e., the object itself.
2043 // Return the section index in the output file.
2045 do_out_shndx() const
2047 gold_assert(this->out_shndx_ != -1U);
2048 return this->out_shndx_;
2051 // Set the output section index.
2053 do_set_out_shndx(unsigned int shndx)
2055 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2056 this->out_shndx_ = shndx;
2059 // Set the final data size of the Output_section. For a typical
2060 // Output_section, there is nothing to do, but if there are any
2061 // Output_section_data objects we need to set their final addresses
2064 set_final_data_size();
2066 // Reset the address and file offset.
2068 do_reset_address_and_file_offset();
2070 // Write the data to the file. For a typical Output_section, this
2071 // does nothing: the data is written out by calling Object::Relocate
2072 // on each input object. But if there are any Output_section_data
2073 // objects we do need to write them out here.
2075 do_write(Output_file*);
2077 // Return the address alignment--function required by parent class.
2079 do_addralign() const
2080 { return this->addralign_; }
2082 // Return whether there is a load address.
2084 do_has_load_address() const
2085 { return this->has_load_address_; }
2087 // Return the load address.
2089 do_load_address() const
2091 gold_assert(this->has_load_address_);
2092 return this->load_address_;
2095 // Return whether this is an Output_section.
2097 do_is_section() const
2100 // Return whether this is a section of the specified type.
2102 do_is_section_type(elfcpp::Elf_Word type) const
2103 { return this->type_ == type; }
2105 // Return whether the specified section flag is set.
2107 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2108 { return (this->flags_ & flag) != 0; }
2110 // Set the TLS offset. Called only for SHT_TLS sections.
2112 do_set_tls_offset(uint64_t tls_base);
2114 // Return the TLS offset, relative to the base of the TLS segment.
2115 // Valid only for SHT_TLS sections.
2117 do_tls_offset() const
2118 { return this->tls_offset_; }
2120 // This may be implemented by a child class.
2122 do_finalize_name(Layout*)
2125 // Record that this section requires postprocessing after all
2126 // relocations have been applied. This is called by a child class.
2128 set_requires_postprocessing()
2130 this->requires_postprocessing_ = true;
2131 this->after_input_sections_ = true;
2134 // Write all the data of an Output_section into the postprocessing
2137 write_to_postprocessing_buffer();
2140 // In some cases we need to keep a list of the input sections
2141 // associated with this output section. We only need the list if we
2142 // might have to change the offsets of the input section within the
2143 // output section after we add the input section. The ordinary
2144 // input sections will be written out when we process the object
2145 // file, and as such we don't need to track them here. We do need
2146 // to track Output_section_data objects here. We store instances of
2147 // this structure in a std::vector, so it must be a POD. There can
2148 // be many instances of this structure, so we use a union to save
2154 : shndx_(0), p2align_(0)
2156 this->u1_.data_size = 0;
2157 this->u2_.object = NULL;
2160 // For an ordinary input section.
2161 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2164 p2align_(ffsll(static_cast<long long>(addralign)))
2166 gold_assert(shndx != OUTPUT_SECTION_CODE
2167 && shndx != MERGE_DATA_SECTION_CODE
2168 && shndx != MERGE_STRING_SECTION_CODE);
2169 this->u1_.data_size = data_size;
2170 this->u2_.object = object;
2173 // For a non-merge output section.
2174 Input_section(Output_section_data* posd)
2175 : shndx_(OUTPUT_SECTION_CODE),
2176 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2178 this->u1_.data_size = 0;
2179 this->u2_.posd = posd;
2182 // For a merge section.
2183 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2185 ? MERGE_STRING_SECTION_CODE
2186 : MERGE_DATA_SECTION_CODE),
2187 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2189 this->u1_.entsize = entsize;
2190 this->u2_.posd = posd;
2193 // The required alignment.
2197 return (this->p2align_ == 0
2199 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2202 // Return the required size.
2206 // Whether this is an input section.
2208 is_input_section() const
2210 return (this->shndx_ != OUTPUT_SECTION_CODE
2211 && this->shndx_ != MERGE_DATA_SECTION_CODE
2212 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2215 // Return whether this is a merge section which matches the
2218 is_merge_section(bool is_string, uint64_t entsize,
2219 uint64_t addralign) const
2221 return (this->shndx_ == (is_string
2222 ? MERGE_STRING_SECTION_CODE
2223 : MERGE_DATA_SECTION_CODE)
2224 && this->u1_.entsize == entsize
2225 && this->addralign() == addralign);
2228 // Return the object for an input section.
2232 gold_assert(this->is_input_section());
2233 return this->u2_.object;
2236 // Return the input section index for an input section.
2240 gold_assert(this->is_input_section());
2241 return this->shndx_;
2244 // Set the output section.
2246 set_output_section(Output_section* os)
2248 gold_assert(!this->is_input_section());
2249 this->u2_.posd->set_output_section(os);
2252 // Set the address and file offset. This is called during
2253 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2254 // the enclosing section.
2256 set_address_and_file_offset(uint64_t address, off_t file_offset,
2257 off_t section_file_offset);
2259 // Reset the address and file offset.
2261 reset_address_and_file_offset();
2263 // Finalize the data size.
2265 finalize_data_size();
2267 // Add an input section, for SHF_MERGE sections.
2269 add_input_section(Relobj* object, unsigned int shndx)
2271 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2272 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2273 return this->u2_.posd->add_input_section(object, shndx);
2276 // Given an input OBJECT, an input section index SHNDX within that
2277 // object, and an OFFSET relative to the start of that input
2278 // section, return whether or not the output offset is known. If
2279 // this function returns true, it sets *POUTPUT to the offset in
2280 // the output section, relative to the start of the input section
2281 // in the output section. *POUTPUT may be different from OFFSET
2282 // for a merged section.
2284 output_offset(const Relobj* object, unsigned int shndx,
2285 section_offset_type offset,
2286 section_offset_type *poutput) const;
2288 // Return whether this is the merge section for the input section
2291 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2293 // Write out the data. This does nothing for an input section.
2295 write(Output_file*);
2297 // Write the data to a buffer. This does nothing for an input
2300 write_to_buffer(unsigned char*);
2302 // Print statistics about merge sections to stderr.
2304 print_merge_stats(const char* section_name)
2306 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2307 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2308 this->u2_.posd->print_merge_stats(section_name);
2312 // Code values which appear in shndx_. If the value is not one of
2313 // these codes, it is the input section index in the object file.
2316 // An Output_section_data.
2317 OUTPUT_SECTION_CODE = -1U,
2318 // An Output_section_data for an SHF_MERGE section with
2319 // SHF_STRINGS not set.
2320 MERGE_DATA_SECTION_CODE = -2U,
2321 // An Output_section_data for an SHF_MERGE section with
2323 MERGE_STRING_SECTION_CODE = -3U
2326 // For an ordinary input section, this is the section index in the
2327 // input file. For an Output_section_data, this is
2328 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2329 // MERGE_STRING_SECTION_CODE.
2330 unsigned int shndx_;
2331 // The required alignment, stored as a power of 2.
2332 unsigned int p2align_;
2335 // For an ordinary input section, the section size.
2337 // For OUTPUT_SECTION_CODE, this is not used. For
2338 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2344 // For an ordinary input section, the object which holds the
2347 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2348 // MERGE_STRING_SECTION_CODE, the data.
2349 Output_section_data* posd;
2353 typedef std::vector<Input_section> Input_section_list;
2355 // This class is used to sort the input sections.
2356 class Input_section_sort_entry;
2358 // This is the sort comparison function.
2359 struct Input_section_sort_compare
2362 operator()(const Input_section_sort_entry&,
2363 const Input_section_sort_entry&) const;
2366 // Fill data. This is used to fill in data between input sections.
2367 // It is also used for data statements (BYTE, WORD, etc.) in linker
2368 // scripts. When we have to keep track of the input sections, we
2369 // can use an Output_data_const, but we don't want to have to keep
2370 // track of input sections just to implement fills.
2374 Fill(off_t section_offset, off_t length)
2375 : section_offset_(section_offset),
2376 length_(convert_to_section_size_type(length))
2379 // Return section offset.
2381 section_offset() const
2382 { return this->section_offset_; }
2384 // Return fill length.
2387 { return this->length_; }
2390 // The offset within the output section.
2391 off_t section_offset_;
2392 // The length of the space to fill.
2393 section_size_type length_;
2396 typedef std::vector<Fill> Fill_list;
2398 // Add a new output section by Input_section.
2400 add_output_section_data(Input_section*);
2402 // Add an SHF_MERGE input section. Returns true if the section was
2405 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2406 uint64_t entsize, uint64_t addralign);
2408 // Add an output SHF_MERGE section POSD to this output section.
2409 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2410 // ENTSIZE is the entity size. This returns the entry added to
2413 add_output_merge_section(Output_section_data* posd, bool is_string,
2416 // Sort the attached input sections.
2418 sort_attached_input_sections();
2420 // Most of these fields are only valid after layout.
2422 // The name of the section. This will point into a Stringpool.
2424 // The section address is in the parent class.
2425 // The section alignment.
2426 uint64_t addralign_;
2427 // The section entry size.
2429 // The load address. This is only used when using a linker script
2430 // with a SECTIONS clause. The has_load_address_ field indicates
2431 // whether this field is valid.
2432 uint64_t load_address_;
2433 // The file offset is in the parent class.
2434 // Set the section link field to the index of this section.
2435 const Output_data* link_section_;
2436 // If link_section_ is NULL, this is the link field.
2438 // Set the section info field to the index of this section.
2439 const Output_section* info_section_;
2440 // If info_section_ is NULL, set the info field to the symbol table
2441 // index of this symbol.
2442 const Symbol* info_symndx_;
2443 // If info_section_ and info_symndx_ are NULL, this is the section
2446 // The section type.
2447 const elfcpp::Elf_Word type_;
2448 // The section flags.
2449 elfcpp::Elf_Xword flags_;
2450 // The section index.
2451 unsigned int out_shndx_;
2452 // If there is a STT_SECTION for this output section in the normal
2453 // symbol table, this is the symbol index. This starts out as zero.
2454 // It is initialized in Layout::finalize() to be the index, or -1U
2455 // if there isn't one.
2456 unsigned int symtab_index_;
2457 // If there is a STT_SECTION for this output section in the dynamic
2458 // symbol table, this is the symbol index. This starts out as zero.
2459 // It is initialized in Layout::finalize() to be the index, or -1U
2460 // if there isn't one.
2461 unsigned int dynsym_index_;
2462 // The input sections. This will be empty in cases where we don't
2463 // need to keep track of them.
2464 Input_section_list input_sections_;
2465 // The offset of the first entry in input_sections_.
2466 off_t first_input_offset_;
2467 // The fill data. This is separate from input_sections_ because we
2468 // often will need fill sections without needing to keep track of
2471 // If the section requires postprocessing, this buffer holds the
2472 // section contents during relocation.
2473 unsigned char* postprocessing_buffer_;
2474 // Whether this output section needs a STT_SECTION symbol in the
2475 // normal symbol table. This will be true if there is a relocation
2477 bool needs_symtab_index_ : 1;
2478 // Whether this output section needs a STT_SECTION symbol in the
2479 // dynamic symbol table. This will be true if there is a dynamic
2480 // relocation which needs it.
2481 bool needs_dynsym_index_ : 1;
2482 // Whether the link field of this output section should point to the
2483 // normal symbol table.
2484 bool should_link_to_symtab_ : 1;
2485 // Whether the link field of this output section should point to the
2486 // dynamic symbol table.
2487 bool should_link_to_dynsym_ : 1;
2488 // Whether this section should be written after all the input
2489 // sections are complete.
2490 bool after_input_sections_ : 1;
2491 // Whether this section requires post processing after all
2492 // relocations have been applied.
2493 bool requires_postprocessing_ : 1;
2494 // Whether an input section was mapped to this output section
2495 // because of a SECTIONS clause in a linker script.
2496 bool found_in_sections_clause_ : 1;
2497 // Whether this section has an explicitly specified load address.
2498 bool has_load_address_ : 1;
2499 // True if the info_section_ field means the section index of the
2500 // section, false if it means the symbol index of the corresponding
2502 bool info_uses_section_index_ : 1;
2503 // True if the input sections attached to this output section may
2505 bool may_sort_attached_input_sections_ : 1;
2506 // True if the input sections attached to this output section must
2508 bool must_sort_attached_input_sections_ : 1;
2509 // True if the input sections attached to this output section have
2510 // already been sorted.
2511 bool attached_input_sections_are_sorted_ : 1;
2512 // For SHT_TLS sections, the offset of this section relative to the base
2513 // of the TLS segment.
2514 uint64_t tls_offset_;
2517 // An output segment. PT_LOAD segments are built from collections of
2518 // output sections. Other segments typically point within PT_LOAD
2519 // segments, and are built directly as needed.
2521 class Output_segment
2524 // Create an output segment, specifying the type and flags.
2525 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2527 // Return the virtual address.
2530 { return this->vaddr_; }
2532 // Return the physical address.
2535 { return this->paddr_; }
2537 // Return the segment type.
2540 { return this->type_; }
2542 // Return the segment flags.
2545 { return this->flags_; }
2547 // Return the memory size.
2550 { return this->memsz_; }
2552 // Return the file size.
2555 { return this->filesz_; }
2557 // Return the file offset.
2560 { return this->offset_; }
2562 // Return the maximum alignment of the Output_data.
2564 maximum_alignment();
2566 // Add an Output_section to this segment.
2568 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2569 { this->add_output_section(os, seg_flags, false); }
2571 // Add an Output_section to the start of this segment.
2573 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2574 { this->add_output_section(os, seg_flags, true); }
2576 // Remove an Output_section from this segment. It is an error if it
2579 remove_output_section(Output_section* os);
2581 // Add an Output_data (which is not an Output_section) to the start
2584 add_initial_output_data(Output_data*);
2586 // Return true if this segment has any sections which hold actual
2587 // data, rather than being a BSS section.
2589 has_any_data_sections() const
2590 { return !this->output_data_.empty(); }
2592 // Return the number of dynamic relocations applied to this segment.
2594 dynamic_reloc_count() const;
2596 // Return the address of the first section.
2598 first_section_load_address() const;
2600 // Return whether the addresses have been set already.
2602 are_addresses_set() const
2603 { return this->are_addresses_set_; }
2605 // Set the addresses.
2607 set_addresses(uint64_t vaddr, uint64_t paddr)
2609 this->vaddr_ = vaddr;
2610 this->paddr_ = paddr;
2611 this->are_addresses_set_ = true;
2614 // Set the segment flags. This is only used if we have a PHDRS
2615 // clause which explicitly specifies the flags.
2617 set_flags(elfcpp::Elf_Word flags)
2618 { this->flags_ = flags; }
2620 // Set the address of the segment to ADDR and the offset to *POFF
2621 // and set the addresses and offsets of all contained output
2622 // sections accordingly. Set the section indexes of all contained
2623 // output sections starting with *PSHNDX. If RESET is true, first
2624 // reset the addresses of the contained sections. Return the
2625 // address of the immediately following segment. Update *POFF and
2626 // *PSHNDX. This should only be called for a PT_LOAD segment.
2628 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
2629 unsigned int* pshndx);
2631 // Set the minimum alignment of this segment. This may be adjusted
2632 // upward based on the section alignments.
2634 set_minimum_p_align(uint64_t align)
2635 { this->min_p_align_ = align; }
2637 // Set the offset of this segment based on the section. This should
2638 // only be called for a non-PT_LOAD segment.
2642 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2646 // Return the number of output sections.
2648 output_section_count() const;
2650 // Return the section attached to the list segment with the lowest
2651 // load address. This is used when handling a PHDRS clause in a
2654 section_with_lowest_load_address() const;
2656 // Write the segment header into *OPHDR.
2657 template<int size, bool big_endian>
2659 write_header(elfcpp::Phdr_write<size, big_endian>*);
2661 // Write the section headers of associated sections into V.
2662 template<int size, bool big_endian>
2664 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2665 unsigned int* pshndx) const;
2668 Output_segment(const Output_segment&);
2669 Output_segment& operator=(const Output_segment&);
2671 typedef std::list<Output_data*> Output_data_list;
2673 // Add an Output_section to this segment, specifying front or back.
2675 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2678 // Find the maximum alignment in an Output_data_list.
2680 maximum_alignment_list(const Output_data_list*);
2682 // Set the section addresses in an Output_data_list.
2684 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2685 uint64_t addr, off_t* poff, unsigned int* pshndx,
2688 // Return the number of Output_sections in an Output_data_list.
2690 output_section_count_list(const Output_data_list*) const;
2692 // Return the number of dynamic relocs in an Output_data_list.
2694 dynamic_reloc_count_list(const Output_data_list*) const;
2696 // Find the section with the lowest load address in an
2697 // Output_data_list.
2699 lowest_load_address_in_list(const Output_data_list* pdl,
2700 Output_section** found,
2701 uint64_t* found_lma) const;
2703 // Write the section headers in the list into V.
2704 template<int size, bool big_endian>
2706 write_section_headers_list(const Layout*, const Stringpool*,
2707 const Output_data_list*, unsigned char* v,
2708 unsigned int* pshdx) const;
2710 // The list of output data with contents attached to this segment.
2711 Output_data_list output_data_;
2712 // The list of output data without contents attached to this segment.
2713 Output_data_list output_bss_;
2714 // The segment virtual address.
2716 // The segment physical address.
2718 // The size of the segment in memory.
2720 // The maximum section alignment. The is_max_align_known_ field
2721 // indicates whether this has been finalized.
2722 uint64_t max_align_;
2723 // The required minimum value for the p_align field. This is used
2724 // for PT_LOAD segments. Note that this does not mean that
2725 // addresses should be aligned to this value; it means the p_paddr
2726 // and p_vaddr fields must be congruent modulo this value. For
2727 // non-PT_LOAD segments, the dynamic linker works more efficiently
2728 // if the p_align field has the more conventional value, although it
2729 // can align as needed.
2730 uint64_t min_p_align_;
2731 // The offset of the segment data within the file.
2733 // The size of the segment data in the file.
2735 // The segment type;
2736 elfcpp::Elf_Word type_;
2737 // The segment flags.
2738 elfcpp::Elf_Word flags_;
2739 // Whether we have finalized max_align_.
2740 bool is_max_align_known_ : 1;
2741 // Whether vaddr and paddr were set by a linker script.
2742 bool are_addresses_set_ : 1;
2745 // This class represents the output file.
2750 Output_file(const char* name);
2752 // Indicate that this is a temporary file which should not be
2756 { this->is_temporary_ = true; }
2758 // Open the output file. FILE_SIZE is the final size of the file.
2760 open(off_t file_size);
2762 // Resize the output file.
2764 resize(off_t file_size);
2766 // Close the output file (flushing all buffered data) and make sure
2767 // there are no errors.
2771 // We currently always use mmap which makes the view handling quite
2772 // simple. In the future we may support other approaches.
2774 // Write data to the output file.
2776 write(off_t offset, const void* data, size_t len)
2777 { memcpy(this->base_ + offset, data, len); }
2779 // Get a buffer to use to write to the file, given the offset into
2780 // the file and the size.
2782 get_output_view(off_t start, size_t size)
2784 gold_assert(start >= 0
2785 && start + static_cast<off_t>(size) <= this->file_size_);
2786 return this->base_ + start;
2789 // VIEW must have been returned by get_output_view. Write the
2790 // buffer to the file, passing in the offset and the size.
2792 write_output_view(off_t, size_t, unsigned char*)
2795 // Get a read/write buffer. This is used when we want to write part
2796 // of the file, read it in, and write it again.
2798 get_input_output_view(off_t start, size_t size)
2799 { return this->get_output_view(start, size); }
2801 // Write a read/write buffer back to the file.
2803 write_input_output_view(off_t, size_t, unsigned char*)
2806 // Get a read buffer. This is used when we just want to read part
2807 // of the file back it in.
2808 const unsigned char*
2809 get_input_view(off_t start, size_t size)
2810 { return this->get_output_view(start, size); }
2812 // Release a read bfufer.
2814 free_input_view(off_t, size_t, const unsigned char*)
2818 // Map the file into memory and return a pointer to the map.
2822 // Unmap the file from memory (and flush to disk buffers).
2832 // Base of file mapped into memory.
2833 unsigned char* base_;
2834 // True iff base_ points to a memory buffer rather than an output file.
2835 bool map_is_anonymous_;
2836 // True if this is a temporary file which should not be output.
2840 } // End namespace gold.
2842 #endif // !defined(GOLD_OUTPUT_H)