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"
32 #include "parameters.h"
37 class General_options;
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(off_t data_size = 0)
54 : address_(0), data_size_(data_size), offset_(-1)
60 // Return the address. This is only valid after Layout::finalize is
64 { return this->address_; }
66 // Return the size of the data. This must be valid after
67 // Layout::finalize calls set_address, but need not be valid before
71 { return this->data_size_; }
73 // Return the file offset. This is only valid after
74 // Layout::finalize is finished.
77 { return this->offset_; }
79 // Return the required alignment.
82 { return this->do_addralign(); }
84 // Return whether this is an Output_section.
87 { return this->do_is_section(); }
89 // Return whether this is an Output_section of the specified type.
91 is_section_type(elfcpp::Elf_Word stt) const
92 { return this->do_is_section_type(stt); }
94 // Return whether this is an Output_section with the specified flag
97 is_section_flag_set(elfcpp::Elf_Xword shf) const
98 { return this->do_is_section_flag_set(shf); }
100 // Return the output section index, if there is an output section.
103 { return this->do_out_shndx(); }
105 // Set the output section index, if this is an output section.
107 set_out_shndx(unsigned int shndx)
108 { this->do_set_out_shndx(shndx); }
110 // Set the address and file offset of this data. This is called
111 // during Layout::finalize.
113 set_address(uint64_t addr, off_t off);
115 // Write the data to the output file. This is called after
116 // Layout::finalize is complete.
118 write(Output_file* file)
119 { this->do_write(file); }
121 // This is called by Layout::finalize to note that all sizes must
125 { Output_data::sizes_are_fixed = true; }
128 // Functions that child classes may or in some cases must implement.
130 // Write the data to the output file.
132 do_write(Output_file*) = 0;
134 // Return the required alignment.
136 do_addralign() const = 0;
138 // Return whether this is an Output_section.
140 do_is_section() const
143 // Return whether this is an Output_section of the specified type.
144 // This only needs to be implement by Output_section.
146 do_is_section_type(elfcpp::Elf_Word) const
149 // Return whether this is an Output_section with the specific flag
150 // set. This only needs to be implemented by Output_section.
152 do_is_section_flag_set(elfcpp::Elf_Xword) const
155 // Return the output section index, if there is an output section.
158 { gold_unreachable(); }
160 // Set the output section index, if this is an output section.
162 do_set_out_shndx(unsigned int)
163 { gold_unreachable(); }
165 // Set the address and file offset of the data. This only needs to
166 // be implemented if the child needs to know. The child class can
167 // set its size in this call.
169 do_set_address(uint64_t, off_t)
172 // Functions that child classes may call.
174 // Set the size of the data.
176 set_data_size(off_t data_size)
178 gold_assert(!Output_data::sizes_are_fixed);
179 this->data_size_ = data_size;
182 // Return default alignment for a size--32 or 64.
184 default_alignment(int size);
187 Output_data(const Output_data&);
188 Output_data& operator=(const Output_data&);
190 // This is used for verification, to make sure that we don't try to
191 // change any sizes after we set the section addresses.
192 static bool sizes_are_fixed;
194 // Memory address in file (not always meaningful).
196 // Size of data in file.
198 // Offset within file.
202 // Output the section headers.
204 class Output_section_headers : public Output_data
207 Output_section_headers(const Layout*,
208 const Layout::Segment_list*,
209 const Layout::Section_list*,
212 // Write the data to the file.
214 do_write(Output_file*);
216 // Return the required alignment.
219 { return Output_data::default_alignment(parameters->get_size()); }
222 // Write the data to the file with the right size and endianness.
223 template<int size, bool big_endian>
225 do_sized_write(Output_file*);
227 const Layout* layout_;
228 const Layout::Segment_list* segment_list_;
229 const Layout::Section_list* unattached_section_list_;
230 const Stringpool* secnamepool_;
233 // Output the segment headers.
235 class Output_segment_headers : public Output_data
238 Output_segment_headers(const Layout::Segment_list& segment_list);
240 // Write the data to the file.
242 do_write(Output_file*);
244 // Return the required alignment.
247 { return Output_data::default_alignment(parameters->get_size()); }
250 // Write the data to the file with the right size and endianness.
251 template<int size, bool big_endian>
253 do_sized_write(Output_file*);
255 const Layout::Segment_list& segment_list_;
258 // Output the ELF file header.
260 class Output_file_header : public Output_data
263 Output_file_header(const Target*,
265 const Output_segment_headers*);
267 // Add information about the section headers. We lay out the ELF
268 // file header before we create the section headers.
269 void set_section_info(const Output_section_headers*,
270 const Output_section* shstrtab);
272 // Write the data to the file.
274 do_write(Output_file*);
276 // Return the required alignment.
279 { return Output_data::default_alignment(parameters->get_size()); }
281 // Set the address and offset--we only implement this for error
284 do_set_address(uint64_t, off_t off) const
285 { gold_assert(off == 0); }
288 // Write the data to the file with the right size and endianness.
289 template<int size, bool big_endian>
291 do_sized_write(Output_file*);
293 const Target* target_;
294 const Symbol_table* symtab_;
295 const Output_segment_headers* segment_header_;
296 const Output_section_headers* section_header_;
297 const Output_section* shstrtab_;
300 // Output sections are mainly comprised of input sections. However,
301 // there are cases where we have data to write out which is not in an
302 // input section. Output_section_data is used in such cases. This is
303 // an abstract base class.
305 class Output_section_data : public Output_data
308 Output_section_data(off_t data_size, uint64_t addralign)
309 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
312 Output_section_data(uint64_t addralign)
313 : Output_data(0), output_section_(NULL), addralign_(addralign)
316 // Return the output section.
317 const Output_section*
318 output_section() const
319 { return this->output_section_; }
321 // Record the output section.
323 set_output_section(Output_section* os);
325 // Add an input section, for SHF_MERGE sections. This returns true
326 // if the section was handled.
328 add_input_section(Relobj* object, unsigned int shndx)
329 { return this->do_add_input_section(object, shndx); }
331 // Given an input OBJECT, an input section index SHNDX within that
332 // object, and an OFFSET relative to the start of that input
333 // section, return whether or not the output address is known.
334 // OUTPUT_SECTION_ADDRESS is the address of the output section which
335 // this is a part of. If this function returns true, it sets
336 // *POUTPUT to the output address.
338 output_address(const Relobj* object, unsigned int shndx, off_t offset,
339 uint64_t output_section_address, uint64_t *poutput) const
341 return this->do_output_address(object, shndx, offset,
342 output_section_address, poutput);
346 // The child class must implement do_write.
348 // The child class may implement specific adjustments to the output
351 do_adjust_output_section(Output_section*)
354 // May be implemented by child class. Return true if the section
357 do_add_input_section(Relobj*, unsigned int)
358 { gold_unreachable(); }
360 // The child class may implement output_address.
362 do_output_address(const Relobj*, unsigned int, off_t, uint64_t,
366 // Return the required alignment.
369 { return this->addralign_; }
371 // Return the section index of the output section.
373 do_out_shndx() const;
375 // Set the alignment.
377 set_addralign(uint64_t addralign)
378 { this->addralign_ = addralign; }
381 // The output section for this section.
382 const Output_section* output_section_;
383 // The required alignment.
387 // A simple case of Output_data in which we have constant data to
390 class Output_data_const : public Output_section_data
393 Output_data_const(const std::string& data, uint64_t addralign)
394 : Output_section_data(data.size(), addralign), data_(data)
397 Output_data_const(const char* p, off_t len, uint64_t addralign)
398 : Output_section_data(len, addralign), data_(p, len)
401 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
402 : Output_section_data(len, addralign),
403 data_(reinterpret_cast<const char*>(p), len)
408 add_data(const std::string& add)
410 this->data_.append(add);
411 this->set_data_size(this->data_.size());
414 // Write the data to the output file.
416 do_write(Output_file*);
422 // Another version of Output_data with constant data, in which the
423 // buffer is allocated by the caller.
425 class Output_data_const_buffer : public Output_section_data
428 Output_data_const_buffer(const unsigned char* p, off_t len,
430 : Output_section_data(len, addralign), p_(p)
433 // Write the data the output file.
435 do_write(Output_file*);
438 const unsigned char* p_;
441 // A place holder for data written out via some other mechanism.
443 class Output_data_space : public Output_section_data
446 Output_data_space(off_t data_size, uint64_t addralign)
447 : Output_section_data(data_size, addralign)
450 explicit Output_data_space(uint64_t addralign)
451 : Output_section_data(addralign)
456 set_space_size(off_t space_size)
457 { this->set_data_size(space_size); }
459 // Set the alignment.
461 set_space_alignment(uint64_t align)
462 { this->set_addralign(align); }
464 // Write out the data--this must be handled elsewhere.
466 do_write(Output_file*)
470 // A string table which goes into an output section.
472 class Output_data_strtab : public Output_section_data
475 Output_data_strtab(Stringpool* strtab)
476 : Output_section_data(1), strtab_(strtab)
479 // This is called to set the address and file offset. Here we make
480 // sure that the Stringpool is finalized.
482 do_set_address(uint64_t, off_t);
484 // Write out the data.
486 do_write(Output_file*);
492 // This POD class is used to represent a single reloc in the output
493 // file. This could be a private class within Output_data_reloc, but
494 // the templatization is complex enough that I broke it out into a
495 // separate class. The class is templatized on either elfcpp::SHT_REL
496 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
497 // relocation or an ordinary relocation.
499 // A relocation can be against a global symbol, a local symbol, an
500 // output section, or the undefined symbol at index 0. We represent
501 // the latter by using a NULL global symbol.
503 template<int sh_type, bool dynamic, int size, bool big_endian>
506 template<bool dynamic, int size, bool big_endian>
507 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
510 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
512 // An uninitialized entry. We need this because we want to put
513 // instances of this class into an STL container.
515 : local_sym_index_(INVALID_CODE)
518 // A reloc against a global symbol.
520 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
522 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
525 this->u1_.gsym = gsym;
529 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
530 unsigned int shndx, Address address)
531 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
534 gold_assert(shndx != INVALID_CODE);
535 this->u1_.gsym = gsym;
536 this->u2_.relobj = relobj;
539 // A reloc against a local symbol.
541 Output_reloc(Sized_relobj<size, big_endian>* relobj,
542 unsigned int local_sym_index,
546 : address_(address), local_sym_index_(local_sym_index), type_(type),
549 gold_assert(local_sym_index != GSYM_CODE
550 && local_sym_index != INVALID_CODE);
551 this->u1_.relobj = relobj;
555 Output_reloc(Sized_relobj<size, big_endian>* relobj,
556 unsigned int local_sym_index,
560 : address_(address), local_sym_index_(local_sym_index), type_(type),
563 gold_assert(local_sym_index != GSYM_CODE
564 && local_sym_index != INVALID_CODE);
565 gold_assert(shndx != INVALID_CODE);
566 this->u1_.relobj = relobj;
567 this->u2_.relobj = relobj;
570 // A reloc against the STT_SECTION symbol of an output section.
572 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
574 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
581 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
582 unsigned int shndx, Address address)
583 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
586 gold_assert(shndx != INVALID_CODE);
588 this->u2_.relobj = relobj;
591 // Write the reloc entry to an output view.
593 write(unsigned char* pov) const;
595 // Write the offset and info fields to Write_rel.
596 template<typename Write_rel>
597 void write_rel(Write_rel*) const;
600 // Return the symbol index. We can't do a double template
601 // specialization, so we do a secondary template here.
603 get_symbol_index() const;
605 // Codes for local_sym_index_.
612 // Invalid uninitialized entry.
618 // For a local symbol, the object. We will never generate a
619 // relocation against a local symbol in a dynamic object; that
620 // doesn't make sense. And our callers will always be
621 // templatized, so we use Sized_relobj here.
622 Sized_relobj<size, big_endian>* relobj;
623 // For a global symbol, the symbol. If this is NULL, it indicates
624 // a relocation against the undefined 0 symbol.
626 // For a relocation against an output section, the output section.
631 // If shndx_ is not INVALID CODE, the object which holds the input
632 // section being used to specify the reloc address.
634 // If shndx_ is INVALID_CODE, the output data being used to
635 // specify the reloc address. This may be NULL if the reloc
636 // address is absolute.
639 // The address offset within the input section or the Output_data.
641 // For a local symbol, the local symbol index. This is GSYM_CODE
642 // for a global symbol, or INVALID_CODE for an uninitialized value.
643 unsigned int local_sym_index_;
644 // The reloc type--a processor specific code.
646 // If the reloc address is an input section in an object, the
647 // section index. This is INVALID_CODE if the reloc address is
648 // specified in some other way.
652 // The SHT_RELA version of Output_reloc<>. This is just derived from
653 // the SHT_REL version of Output_reloc, but it adds an addend.
655 template<bool dynamic, int size, bool big_endian>
656 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
659 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
660 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
662 // An uninitialized entry.
667 // A reloc against a global symbol.
669 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
670 Address address, Addend addend)
671 : rel_(gsym, type, od, address), addend_(addend)
674 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
675 unsigned int shndx, Address address, Addend addend)
676 : rel_(gsym, type, relobj, shndx, address), addend_(addend)
679 // A reloc against a local symbol.
681 Output_reloc(Sized_relobj<size, big_endian>* relobj,
682 unsigned int local_sym_index,
683 unsigned int type, Output_data* od, Address address,
685 : rel_(relobj, local_sym_index, type, od, address), addend_(addend)
688 Output_reloc(Sized_relobj<size, big_endian>* relobj,
689 unsigned int local_sym_index,
694 : rel_(relobj, local_sym_index, type, shndx, address),
698 // A reloc against the STT_SECTION symbol of an output section.
700 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
701 Address address, Addend addend)
702 : rel_(os, type, od, address), addend_(addend)
705 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
706 unsigned int shndx, Address address, Addend addend)
707 : rel_(os, type, relobj, shndx, address), addend_(addend)
710 // Write the reloc entry to an output view.
712 write(unsigned char* pov) const;
716 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
721 // Output_data_reloc is used to manage a section containing relocs.
722 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
723 // indicates whether this is a dynamic relocation or a normal
724 // relocation. Output_data_reloc_base is a base class.
725 // Output_data_reloc is the real class, which we specialize based on
728 template<int sh_type, bool dynamic, int size, bool big_endian>
729 class Output_data_reloc_base : public Output_section_data
732 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
733 typedef typename Output_reloc_type::Address Address;
734 static const int reloc_size =
735 Reloc_types<sh_type, size, big_endian>::reloc_size;
737 // Construct the section.
738 Output_data_reloc_base()
739 : Output_section_data(Output_data::default_alignment(size))
742 // Write out the data.
744 do_write(Output_file*);
747 // Set the entry size and the link.
749 do_adjust_output_section(Output_section *os);
751 // Add a relocation entry.
753 add(const Output_reloc_type& reloc)
755 this->relocs_.push_back(reloc);
756 this->set_data_size(this->relocs_.size() * reloc_size);
760 typedef std::vector<Output_reloc_type> Relocs;
765 // The class which callers actually create.
767 template<int sh_type, bool dynamic, int size, bool big_endian>
768 class Output_data_reloc;
770 // The SHT_REL version of Output_data_reloc.
772 template<bool dynamic, int size, bool big_endian>
773 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
774 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
777 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
781 typedef typename Base::Output_reloc_type Output_reloc_type;
782 typedef typename Output_reloc_type::Address Address;
785 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
788 // Add a reloc against a global symbol.
791 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
792 { this->add(Output_reloc_type(gsym, type, od, address)); }
795 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
796 unsigned int shndx, Address address)
797 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address)); }
799 // Add a reloc against a local symbol.
802 add_local(Sized_relobj<size, big_endian>* relobj,
803 unsigned int local_sym_index, unsigned int type,
804 Output_data* od, Address address)
805 { this->add(Output_reloc_type(relobj, local_sym_index, type, od, address)); }
808 add_local(Sized_relobj<size, big_endian>* relobj,
809 unsigned int local_sym_index, unsigned int type,
810 unsigned int shndx, Address address)
811 { this->add(Output_reloc_type(relobj, local_sym_index, type, shndx,
815 // A reloc against the STT_SECTION symbol of an output section.
818 add_output_section(Output_section* os, unsigned int type,
819 Output_data* od, Address address)
820 { this->add(Output_reloc_type(os, type, od, address)); }
823 add_output_section(Output_section* os, unsigned int type,
824 Relobj* relobj, unsigned int shndx, Address address)
825 { this->add(Output_reloc_type(os, type, relobj, shndx, address)); }
828 // The SHT_RELA version of Output_data_reloc.
830 template<bool dynamic, int size, bool big_endian>
831 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
832 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
835 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
839 typedef typename Base::Output_reloc_type Output_reloc_type;
840 typedef typename Output_reloc_type::Address Address;
841 typedef typename Output_reloc_type::Addend Addend;
844 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
847 // Add a reloc against a global symbol.
850 add_global(Symbol* gsym, unsigned int type, Output_data* od,
851 Address address, Addend addend)
852 { this->add(Output_reloc_type(gsym, type, od, address, addend)); }
855 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
856 unsigned int shndx, Address address, Addend addend)
857 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address, addend)); }
859 // Add a reloc against a local symbol.
862 add_local(Sized_relobj<size, big_endian>* relobj,
863 unsigned int local_sym_index, unsigned int type,
864 Output_data* od, Address address, Addend addend)
866 this->add(Output_reloc_type(relobj, local_sym_index, type, od, address,
871 add_local(Sized_relobj<size, big_endian>* relobj,
872 unsigned int local_sym_index, unsigned int type,
873 unsigned int shndx, Address address, Addend addend)
875 this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, address,
879 // A reloc against the STT_SECTION symbol of an output section.
882 add_output_section(Output_section* os, unsigned int type, Output_data* od,
883 Address address, Addend addend)
884 { this->add(Output_reloc_type(os, type, od, address, addend)); }
887 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
888 unsigned int shndx, Address address, Addend addend)
889 { this->add(Output_reloc_type(os, type, relobj, shndx, address, addend)); }
892 // Output_data_got is used to manage a GOT. Each entry in the GOT is
893 // for one symbol--either a global symbol or a local symbol in an
894 // object. The target specific code adds entries to the GOT as
897 template<int size, bool big_endian>
898 class Output_data_got : public Output_section_data
901 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
904 : Output_section_data(Output_data::default_alignment(size)), entries_()
907 // Add an entry for a global symbol to the GOT. Return true if this
908 // is a new GOT entry, false if the symbol was already in the GOT.
910 add_global(Symbol* gsym);
912 // Add an entry for a local symbol to the GOT. This returns true if
913 // this is a new GOT entry, false if the symbol already has a GOT
916 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
918 // Add a constant to the GOT. This returns the offset of the new
919 // entry from the start of the GOT.
921 add_constant(Valtype constant)
923 this->entries_.push_back(Got_entry(constant));
924 this->set_got_size();
925 return this->last_got_offset();
928 // Write out the GOT table.
930 do_write(Output_file*);
933 // This POD class holds a single GOT entry.
937 // Create a zero entry.
939 : local_sym_index_(CONSTANT_CODE)
940 { this->u_.constant = 0; }
942 // Create a global symbol entry.
943 explicit Got_entry(Symbol* gsym)
944 : local_sym_index_(GSYM_CODE)
945 { this->u_.gsym = gsym; }
947 // Create a local symbol entry.
948 Got_entry(Sized_relobj<size, big_endian>* object,
949 unsigned int local_sym_index)
950 : local_sym_index_(local_sym_index)
952 gold_assert(local_sym_index != GSYM_CODE
953 && local_sym_index != CONSTANT_CODE);
954 this->u_.object = object;
957 // Create a constant entry. The constant is a host value--it will
958 // be swapped, if necessary, when it is written out.
959 explicit Got_entry(Valtype constant)
960 : local_sym_index_(CONSTANT_CODE)
961 { this->u_.constant = constant; }
963 // Write the GOT entry to an output view.
965 write(unsigned char* pov) const;
976 // For a local symbol, the object.
977 Sized_relobj<size, big_endian>* object;
978 // For a global symbol, the symbol.
980 // For a constant, the constant.
983 // For a local symbol, the local symbol index. This is GSYM_CODE
984 // for a global symbol, or CONSTANT_CODE for a constant.
985 unsigned int local_sym_index_;
988 typedef std::vector<Got_entry> Got_entries;
990 // Return the offset into the GOT of GOT entry I.
992 got_offset(unsigned int i) const
993 { return i * (size / 8); }
995 // Return the offset into the GOT of the last entry added.
997 last_got_offset() const
998 { return this->got_offset(this->entries_.size() - 1); }
1000 // Set the size of the section.
1003 { this->set_data_size(this->got_offset(this->entries_.size())); }
1005 // The list of GOT entries.
1006 Got_entries entries_;
1009 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1012 class Output_data_dynamic : public Output_section_data
1015 Output_data_dynamic(Stringpool* pool)
1016 : Output_section_data(Output_data::default_alignment(
1017 parameters->get_size())),
1018 entries_(), pool_(pool)
1021 // Add a new dynamic entry with a fixed numeric value.
1023 add_constant(elfcpp::DT tag, unsigned int val)
1024 { this->add_entry(Dynamic_entry(tag, val)); }
1026 // Add a new dynamic entry with the address of output data.
1028 add_section_address(elfcpp::DT tag, const Output_data* od)
1029 { this->add_entry(Dynamic_entry(tag, od, false)); }
1031 // Add a new dynamic entry with the size of output data.
1033 add_section_size(elfcpp::DT tag, const Output_data* od)
1034 { this->add_entry(Dynamic_entry(tag, od, true)); }
1036 // Add a new dynamic entry with the address of a symbol.
1038 add_symbol(elfcpp::DT tag, const Symbol* sym)
1039 { this->add_entry(Dynamic_entry(tag, sym)); }
1041 // Add a new dynamic entry with a string.
1043 add_string(elfcpp::DT tag, const char* str)
1044 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1047 add_string(elfcpp::DT tag, const std::string& str)
1048 { this->add_string(tag, str.c_str()); }
1050 // Set the final data size.
1052 do_set_address(uint64_t, off_t);
1054 // Write out the dynamic entries.
1056 do_write(Output_file*);
1059 // Adjust the output section to set the entry size.
1061 do_adjust_output_section(Output_section*);
1064 // This POD class holds a single dynamic entry.
1068 // Create an entry with a fixed numeric value.
1069 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1070 : tag_(tag), classification_(DYNAMIC_NUMBER)
1071 { this->u_.val = val; }
1073 // Create an entry with the size or address of a section.
1074 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1076 classification_(section_size
1077 ? DYNAMIC_SECTION_SIZE
1078 : DYNAMIC_SECTION_ADDRESS)
1079 { this->u_.od = od; }
1081 // Create an entry with the address of a symbol.
1082 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1083 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1084 { this->u_.sym = sym; }
1086 // Create an entry with a string.
1087 Dynamic_entry(elfcpp::DT tag, const char* str)
1088 : tag_(tag), classification_(DYNAMIC_STRING)
1089 { this->u_.str = str; }
1091 // Write the dynamic entry to an output view.
1092 template<int size, bool big_endian>
1094 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1102 DYNAMIC_SECTION_ADDRESS,
1104 DYNAMIC_SECTION_SIZE,
1113 // For DYNAMIC_NUMBER.
1115 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1116 const Output_data* od;
1117 // For DYNAMIC_SYMBOL.
1119 // For DYNAMIC_STRING.
1124 // The type of entry.
1125 Classification classification_;
1128 // Add an entry to the list.
1130 add_entry(const Dynamic_entry& entry)
1131 { this->entries_.push_back(entry); }
1133 // Sized version of write function.
1134 template<int size, bool big_endian>
1136 sized_write(Output_file* of);
1138 // The type of the list of entries.
1139 typedef std::vector<Dynamic_entry> Dynamic_entries;
1142 Dynamic_entries entries_;
1143 // The pool used for strings.
1147 // An output section. We don't expect to have too many output
1148 // sections, so we don't bother to do a template on the size.
1150 class Output_section : public Output_data
1153 // Create an output section, giving the name, type, and flags.
1154 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1155 virtual ~Output_section();
1157 // Add a new input section SHNDX, named NAME, with header SHDR, from
1158 // object OBJECT. Return the offset within the output section.
1159 template<int size, bool big_endian>
1161 add_input_section(Relobj* object, unsigned int shndx, const char *name,
1162 const elfcpp::Shdr<size, big_endian>& shdr);
1164 // Add generated data POSD to this output section.
1166 add_output_section_data(Output_section_data* posd);
1168 // Return the section name.
1171 { return this->name_; }
1173 // Return the section type.
1176 { return this->type_; }
1178 // Return the section flags.
1181 { return this->flags_; }
1183 // Return the section index in the output file.
1185 do_out_shndx() const
1186 { return this->out_shndx_; }
1188 // Set the output section index.
1190 do_set_out_shndx(unsigned int shndx)
1191 { this->out_shndx_ = shndx; }
1193 // Return the entsize field.
1196 { return this->entsize_; }
1198 // Set the entsize field.
1200 set_entsize(uint64_t v);
1202 // Set the link field to the output section index of a section.
1204 set_link_section(const Output_data* od)
1206 gold_assert(this->link_ == 0
1207 && !this->should_link_to_symtab_
1208 && !this->should_link_to_dynsym_);
1209 this->link_section_ = od;
1212 // Set the link field to a constant.
1214 set_link(unsigned int v)
1216 gold_assert(this->link_section_ == NULL
1217 && !this->should_link_to_symtab_
1218 && !this->should_link_to_dynsym_);
1222 // Record that this section should link to the normal symbol table.
1224 set_should_link_to_symtab()
1226 gold_assert(this->link_section_ == NULL
1228 && !this->should_link_to_dynsym_);
1229 this->should_link_to_symtab_ = true;
1232 // Record that this section should link to the dynamic symbol table.
1234 set_should_link_to_dynsym()
1236 gold_assert(this->link_section_ == NULL
1238 && !this->should_link_to_symtab_);
1239 this->should_link_to_dynsym_ = true;
1242 // Return the info field.
1246 gold_assert(this->info_section_ == NULL);
1250 // Set the info field to the output section index of a section.
1252 set_info_section(const Output_data* od)
1254 gold_assert(this->info_ == 0);
1255 this->info_section_ = od;
1258 // Set the info field to a constant.
1260 set_info(unsigned int v)
1262 gold_assert(this->info_section_ == NULL);
1266 // Set the addralign field.
1268 set_addralign(uint64_t v)
1269 { this->addralign_ = v; }
1271 // Indicate that we need a symtab index.
1273 set_needs_symtab_index()
1274 { this->needs_symtab_index_ = true; }
1276 // Return whether we need a symtab index.
1278 needs_symtab_index() const
1279 { return this->needs_symtab_index_; }
1281 // Get the symtab index.
1283 symtab_index() const
1285 gold_assert(this->symtab_index_ != 0);
1286 return this->symtab_index_;
1289 // Set the symtab index.
1291 set_symtab_index(unsigned int index)
1293 gold_assert(index != 0);
1294 this->symtab_index_ = index;
1297 // Indicate that we need a dynsym index.
1299 set_needs_dynsym_index()
1300 { this->needs_dynsym_index_ = true; }
1302 // Return whether we need a dynsym index.
1304 needs_dynsym_index() const
1305 { return this->needs_dynsym_index_; }
1307 // Get the dynsym index.
1309 dynsym_index() const
1311 gold_assert(this->dynsym_index_ != 0);
1312 return this->dynsym_index_;
1315 // Set the dynsym index.
1317 set_dynsym_index(unsigned int index)
1319 gold_assert(index != 0);
1320 this->dynsym_index_ = index;
1323 // Return the output virtual address of OFFSET relative to the start
1324 // of input section SHNDX in object OBJECT.
1326 output_address(const Relobj* object, unsigned int shndx,
1327 off_t offset) const;
1329 // Set the address of the Output_section. For a typical
1330 // Output_section, there is nothing to do, but if there are any
1331 // Output_section_data objects we need to set the final addresses
1334 do_set_address(uint64_t, off_t);
1336 // Write the data to the file. For a typical Output_section, this
1337 // does nothing: the data is written out by calling Object::Relocate
1338 // on each input object. But if there are any Output_section_data
1339 // objects we do need to write them out here.
1341 do_write(Output_file*);
1343 // Return the address alignment--function required by parent class.
1345 do_addralign() const
1346 { return this->addralign_; }
1348 // Return whether this is an Output_section.
1350 do_is_section() const
1353 // Return whether this is a section of the specified type.
1355 do_is_section_type(elfcpp::Elf_Word type) const
1356 { return this->type_ == type; }
1358 // Return whether the specified section flag is set.
1360 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1361 { return (this->flags_ & flag) != 0; }
1363 // Write the section header into *OPHDR.
1364 template<int size, bool big_endian>
1366 write_header(const Layout*, const Stringpool*,
1367 elfcpp::Shdr_write<size, big_endian>*) const;
1370 // In some cases we need to keep a list of the input sections
1371 // associated with this output section. We only need the list if we
1372 // might have to change the offsets of the input section within the
1373 // output section after we add the input section. The ordinary
1374 // input sections will be written out when we process the object
1375 // file, and as such we don't need to track them here. We do need
1376 // to track Output_section_data objects here. We store instances of
1377 // this structure in a std::vector, so it must be a POD. There can
1378 // be many instances of this structure, so we use a union to save
1384 : shndx_(0), p2align_(0)
1386 this->u1_.data_size = 0;
1387 this->u2_.object = NULL;
1390 // For an ordinary input section.
1391 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1394 p2align_(ffsll(static_cast<long long>(addralign)))
1396 gold_assert(shndx != OUTPUT_SECTION_CODE
1397 && shndx != MERGE_DATA_SECTION_CODE
1398 && shndx != MERGE_STRING_SECTION_CODE);
1399 this->u1_.data_size = data_size;
1400 this->u2_.object = object;
1403 // For a non-merge output section.
1404 Input_section(Output_section_data* posd)
1405 : shndx_(OUTPUT_SECTION_CODE),
1406 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1408 this->u1_.data_size = 0;
1409 this->u2_.posd = posd;
1412 // For a merge section.
1413 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1415 ? MERGE_STRING_SECTION_CODE
1416 : MERGE_DATA_SECTION_CODE),
1417 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1419 this->u1_.entsize = entsize;
1420 this->u2_.posd = posd;
1423 // The required alignment.
1427 return (this->p2align_ == 0
1429 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1432 // Return the required size.
1436 // Return whether this is a merge section which matches the
1439 is_merge_section(bool is_string, uint64_t entsize,
1440 uint64_t addralign) const
1442 return (this->shndx_ == (is_string
1443 ? MERGE_STRING_SECTION_CODE
1444 : MERGE_DATA_SECTION_CODE)
1445 && this->u1_.entsize == entsize
1446 && this->addralign() == addralign);
1449 // Set the output section.
1451 set_output_section(Output_section* os)
1453 gold_assert(!this->is_input_section());
1454 this->u2_.posd->set_output_section(os);
1457 // Set the address and file offset. This is called during
1458 // Layout::finalize. SECOFF is the file offset of the enclosing
1461 set_address(uint64_t addr, off_t off, off_t secoff);
1463 // Add an input section, for SHF_MERGE sections.
1465 add_input_section(Relobj* object, unsigned int shndx)
1467 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
1468 || this->shndx_ == MERGE_STRING_SECTION_CODE);
1469 return this->u2_.posd->add_input_section(object, shndx);
1472 // Given an input OBJECT, an input section index SHNDX within that
1473 // object, and an OFFSET relative to the start of that input
1474 // section, return whether or not the output address is known.
1475 // OUTPUT_SECTION_ADDRESS is the address of the output section
1476 // which this is a part of. If this function returns true, it
1477 // sets *POUTPUT to the output address.
1479 output_address(const Relobj* object, unsigned int shndx, off_t offset,
1480 uint64_t output_section_address, uint64_t *poutput) const;
1482 // Write out the data. This does nothing for an input section.
1484 write(Output_file*);
1487 // Code values which appear in shndx_. If the value is not one of
1488 // these codes, it is the input section index in the object file.
1491 // An Output_section_data.
1492 OUTPUT_SECTION_CODE = -1U,
1493 // An Output_section_data for an SHF_MERGE section with
1494 // SHF_STRINGS not set.
1495 MERGE_DATA_SECTION_CODE = -2U,
1496 // An Output_section_data for an SHF_MERGE section with
1498 MERGE_STRING_SECTION_CODE = -3U
1501 // Whether this is an input section.
1503 is_input_section() const
1505 return (this->shndx_ != OUTPUT_SECTION_CODE
1506 && this->shndx_ != MERGE_DATA_SECTION_CODE
1507 && this->shndx_ != MERGE_STRING_SECTION_CODE);
1510 // For an ordinary input section, this is the section index in the
1511 // input file. For an Output_section_data, this is
1512 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1513 // MERGE_STRING_SECTION_CODE.
1514 unsigned int shndx_;
1515 // The required alignment, stored as a power of 2.
1516 unsigned int p2align_;
1519 // For an ordinary input section, the section size.
1521 // For OUTPUT_SECTION_CODE, this is not used. For
1522 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1528 // For an ordinary input section, the object which holds the
1531 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1532 // MERGE_STRING_SECTION_CODE, the data.
1533 Output_section_data* posd;
1537 typedef std::vector<Input_section> Input_section_list;
1539 // Fill data. This is used to fill in data between input sections.
1540 // When we have to keep track of the input sections, we can use an
1541 // Output_data_const, but we don't want to have to keep track of
1542 // input sections just to implement fills. For a fill we record the
1543 // offset, and the actual data to be written out.
1547 Fill(off_t section_offset, off_t length)
1548 : section_offset_(section_offset), length_(length)
1551 // Return section offset.
1553 section_offset() const
1554 { return this->section_offset_; }
1556 // Return fill length.
1559 { return this->length_; }
1562 // The offset within the output section.
1563 off_t section_offset_;
1564 // The length of the space to fill.
1568 typedef std::vector<Fill> Fill_list;
1570 // Add a new output section by Input_section.
1572 add_output_section_data(Input_section*);
1574 // Add an SHF_MERGE input section. Returns true if the section was
1577 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
1578 uint64_t entsize, uint64_t addralign);
1580 // Add an output SHF_MERGE section POSD to this output section.
1581 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1582 // ENTSIZE is the entity size. This returns the entry added to
1585 add_output_merge_section(Output_section_data* posd, bool is_string,
1588 // Most of these fields are only valid after layout.
1590 // The name of the section. This will point into a Stringpool.
1592 // The section address is in the parent class.
1593 // The section alignment.
1594 uint64_t addralign_;
1595 // The section entry size.
1597 // The file offset is in the parent class.
1598 // Set the section link field to the index of this section.
1599 const Output_data* link_section_;
1600 // If link_section_ is NULL, this is the link field.
1602 // Set the section info field to the index of this section.
1603 const Output_data* info_section_;
1604 // If info_section_ is NULL, this is the section info field.
1606 // The section type.
1607 elfcpp::Elf_Word type_;
1608 // The section flags.
1609 elfcpp::Elf_Xword flags_;
1610 // The section index.
1611 unsigned int out_shndx_;
1612 // If there is a STT_SECTION for this output section in the normal
1613 // symbol table, this is the symbol index. This starts out as zero.
1614 // It is initialized in Layout::finalize() to be the index, or -1U
1615 // if there isn't one.
1616 unsigned int symtab_index_;
1617 // If there is a STT_SECTION for this output section in the dynamic
1618 // symbol table, this is the symbol index. This starts out as zero.
1619 // It is initialized in Layout::finalize() to be the index, or -1U
1620 // if there isn't one.
1621 unsigned int dynsym_index_;
1622 // The input sections. This will be empty in cases where we don't
1623 // need to keep track of them.
1624 Input_section_list input_sections_;
1625 // The offset of the first entry in input_sections_.
1626 off_t first_input_offset_;
1627 // The fill data. This is separate from input_sections_ because we
1628 // often will need fill sections without needing to keep track of
1631 // Whether this output section needs a STT_SECTION symbol in the
1632 // normal symbol table. This will be true if there is a relocation
1634 bool needs_symtab_index_ : 1;
1635 // Whether this output section needs a STT_SECTION symbol in the
1636 // dynamic symbol table. This will be true if there is a dynamic
1637 // relocation which needs it.
1638 bool needs_dynsym_index_ : 1;
1639 // Whether the link field of this output section should point to the
1640 // normal symbol table.
1641 bool should_link_to_symtab_ : 1;
1642 // Whether the link field of this output section should point to the
1643 // dynamic symbol table.
1644 bool should_link_to_dynsym_ : 1;
1647 // An output segment. PT_LOAD segments are built from collections of
1648 // output sections. Other segments typically point within PT_LOAD
1649 // segments, and are built directly as needed.
1651 class Output_segment
1654 // Create an output segment, specifying the type and flags.
1655 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
1657 // Return the virtual address.
1660 { return this->vaddr_; }
1662 // Return the physical address.
1665 { return this->paddr_; }
1667 // Return the segment type.
1670 { return this->type_; }
1672 // Return the segment flags.
1675 { return this->flags_; }
1677 // Return the memory size.
1680 { return this->memsz_; }
1682 // Return the file size.
1685 { return this->filesz_; }
1687 // Return the maximum alignment of the Output_data.
1691 // Add an Output_section to this segment.
1693 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1694 { this->add_output_section(os, seg_flags, false); }
1696 // Add an Output_section to the start of this segment.
1698 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1699 { this->add_output_section(os, seg_flags, true); }
1701 // Add an Output_data (which is not an Output_section) to the start
1704 add_initial_output_data(Output_data*);
1706 // Set the address of the segment to ADDR and the offset to *POFF
1707 // (aligned if necessary), and set the addresses and offsets of all
1708 // contained output sections accordingly. Set the section indexes
1709 // of all contained output sections starting with *PSHNDX. Return
1710 // the address of the immediately following segment. Update *POFF
1711 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1713 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
1715 // Set the minimum alignment of this segment. This may be adjusted
1716 // upward based on the section alignments.
1718 set_minimum_addralign(uint64_t align)
1720 gold_assert(!this->is_align_known_);
1721 this->align_ = align;
1724 // Set the offset of this segment based on the section. This should
1725 // only be called for a non-PT_LOAD segment.
1729 // Return the number of output sections.
1731 output_section_count() const;
1733 // Write the segment header into *OPHDR.
1734 template<int size, bool big_endian>
1736 write_header(elfcpp::Phdr_write<size, big_endian>*);
1738 // Write the section headers of associated sections into V.
1739 template<int size, bool big_endian>
1741 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
1742 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
1745 Output_segment(const Output_segment&);
1746 Output_segment& operator=(const Output_segment&);
1748 typedef std::list<Output_data*> Output_data_list;
1750 // Add an Output_section to this segment, specifying front or back.
1752 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
1755 // Find the maximum alignment in an Output_data_list.
1757 maximum_alignment(const Output_data_list*);
1759 // Set the section addresses in an Output_data_list.
1761 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
1762 unsigned int* pshndx);
1764 // Return the number of Output_sections in an Output_data_list.
1766 output_section_count_list(const Output_data_list*) const;
1768 // Write the section headers in the list into V.
1769 template<int size, bool big_endian>
1771 write_section_headers_list(const Layout*, const Stringpool*,
1772 const Output_data_list*, unsigned char* v,
1773 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
1775 // The list of output data with contents attached to this segment.
1776 Output_data_list output_data_;
1777 // The list of output data without contents attached to this segment.
1778 Output_data_list output_bss_;
1779 // The segment virtual address.
1781 // The segment physical address.
1783 // The size of the segment in memory.
1785 // The segment alignment. The is_align_known_ field indicates
1786 // whether this has been finalized. It can be set to a minimum
1787 // value before it is finalized.
1789 // The offset of the segment data within the file.
1791 // The size of the segment data in the file.
1793 // The segment type;
1794 elfcpp::Elf_Word type_;
1795 // The segment flags.
1796 elfcpp::Elf_Word flags_;
1797 // Whether we have finalized align_.
1798 bool is_align_known_;
1801 // This class represents the output file.
1806 Output_file(const General_options& options, Target*);
1808 // Get a pointer to the target.
1811 { return this->target_; }
1813 // Open the output file. FILE_SIZE is the final size of the file.
1815 open(off_t file_size);
1817 // Close the output file and make sure there are no error.
1821 // We currently always use mmap which makes the view handling quite
1822 // simple. In the future we may support other approaches.
1824 // Write data to the output file.
1826 write(off_t offset, const void* data, off_t len)
1827 { memcpy(this->base_ + offset, data, len); }
1829 // Get a buffer to use to write to the file, given the offset into
1830 // the file and the size.
1832 get_output_view(off_t start, off_t size)
1834 gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
1835 return this->base_ + start;
1838 // VIEW must have been returned by get_output_view. Write the
1839 // buffer to the file, passing in the offset and the size.
1841 write_output_view(off_t, off_t, unsigned char*)
1846 const General_options& options_;
1855 // Base of file mapped into memory.
1856 unsigned char* base_;
1859 } // End namespace gold.
1861 #endif // !defined(GOLD_OUTPUT_H)