1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "reloc-types.h"
36 class General_options;
42 template<int size, bool big_endian>
44 template<int size, bool big_endian>
47 // An abtract class for data which has to go into the output file.
52 explicit Output_data(off_t data_size = 0)
53 : address_(0), data_size_(data_size), offset_(-1)
59 // Return the address. This is only valid after Layout::finalize is
63 { return this->address_; }
65 // Return the size of the data. This must be valid after
66 // Layout::finalize calls set_address, but need not be valid before
70 { return this->data_size_; }
72 // Return the file offset. This is only valid after
73 // Layout::finalize is finished.
76 { return this->offset_; }
78 // Return the required alignment.
81 { return this->do_addralign(); }
83 // Return whether this is an Output_section.
86 { return this->do_is_section(); }
88 // Return whether this is an Output_section of the specified type.
90 is_section_type(elfcpp::Elf_Word stt) const
91 { return this->do_is_section_type(stt); }
93 // Return whether this is an Output_section with the specified flag
96 is_section_flag_set(elfcpp::Elf_Xword shf) const
97 { return this->do_is_section_flag_set(shf); }
99 // Return the output section index, if there is an output section.
102 { return this->do_out_shndx(); }
104 // Set the output section index, if this is an output section.
106 set_out_shndx(unsigned int shndx)
107 { this->do_set_out_shndx(shndx); }
109 // Set the address and file offset of this data. This is called
110 // during Layout::finalize.
112 set_address(uint64_t addr, off_t off);
114 // Write the data to the output file. This is called after
115 // Layout::finalize is complete.
117 write(Output_file* file)
118 { this->do_write(file); }
120 // This is called by Layout::finalize to note that all sizes must
124 { Output_data::sizes_are_fixed = true; }
126 // Used to check that layout has been done.
129 { return Output_data::sizes_are_fixed; }
132 // Functions that child classes may or in some cases must implement.
134 // Write the data to the output file.
136 do_write(Output_file*) = 0;
138 // Return the required alignment.
140 do_addralign() const = 0;
142 // Return whether this is an Output_section.
144 do_is_section() const
147 // Return whether this is an Output_section of the specified type.
148 // This only needs to be implement by Output_section.
150 do_is_section_type(elfcpp::Elf_Word) const
153 // Return whether this is an Output_section with the specific flag
154 // set. This only needs to be implemented by Output_section.
156 do_is_section_flag_set(elfcpp::Elf_Xword) const
159 // Return the output section index, if there is an output section.
162 { gold_unreachable(); }
164 // Set the output section index, if this is an output section.
166 do_set_out_shndx(unsigned int)
167 { gold_unreachable(); }
169 // Set the address and file offset of the data. This only needs to
170 // be implemented if the child needs to know. The child class can
171 // set its size in this call.
173 do_set_address(uint64_t, off_t)
176 // Functions that child classes may call.
178 // Set the size of the data.
180 set_data_size(off_t data_size)
182 gold_assert(!Output_data::sizes_are_fixed);
183 this->data_size_ = data_size;
186 // Return default alignment for the target size.
190 // Return default alignment for a specified size--32 or 64.
192 default_alignment_for_size(int size);
195 Output_data(const Output_data&);
196 Output_data& operator=(const Output_data&);
198 // This is used for verification, to make sure that we don't try to
199 // change any sizes after we set the section addresses.
200 static bool sizes_are_fixed;
202 // Memory address in file (not always meaningful).
204 // Size of data in file.
206 // Offset within file.
210 // Output the section headers.
212 class Output_section_headers : public Output_data
215 Output_section_headers(const Layout*,
216 const Layout::Segment_list*,
217 const Layout::Section_list*,
220 // Write the data to the file.
222 do_write(Output_file*);
224 // Return the required alignment.
227 { return Output_data::default_alignment(); }
230 // Write the data to the file with the right size and endianness.
231 template<int size, bool big_endian>
233 do_sized_write(Output_file*);
235 const Layout* layout_;
236 const Layout::Segment_list* segment_list_;
237 const Layout::Section_list* unattached_section_list_;
238 const Stringpool* secnamepool_;
241 // Output the segment headers.
243 class Output_segment_headers : public Output_data
246 Output_segment_headers(const Layout::Segment_list& segment_list);
248 // Write the data to the file.
250 do_write(Output_file*);
252 // Return the required alignment.
255 { return Output_data::default_alignment(); }
258 // Write the data to the file with the right size and endianness.
259 template<int size, bool big_endian>
261 do_sized_write(Output_file*);
263 const Layout::Segment_list& segment_list_;
266 // Output the ELF file header.
268 class Output_file_header : public Output_data
271 Output_file_header(const Target*,
273 const Output_segment_headers*);
275 // Add information about the section headers. We lay out the ELF
276 // file header before we create the section headers.
277 void set_section_info(const Output_section_headers*,
278 const Output_section* shstrtab);
280 // Write the data to the file.
282 do_write(Output_file*);
284 // Return the required alignment.
287 { return Output_data::default_alignment(); }
289 // Set the address and offset--we only implement this for error
292 do_set_address(uint64_t, off_t off) const
293 { gold_assert(off == 0); }
296 // Write the data to the file with the right size and endianness.
297 template<int size, bool big_endian>
299 do_sized_write(Output_file*);
301 const Target* target_;
302 const Symbol_table* symtab_;
303 const Output_segment_headers* segment_header_;
304 const Output_section_headers* section_header_;
305 const Output_section* shstrtab_;
308 // Output sections are mainly comprised of input sections. However,
309 // there are cases where we have data to write out which is not in an
310 // input section. Output_section_data is used in such cases. This is
311 // an abstract base class.
313 class Output_section_data : public Output_data
316 Output_section_data(off_t data_size, uint64_t addralign)
317 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
320 Output_section_data(uint64_t addralign)
321 : Output_data(0), output_section_(NULL), addralign_(addralign)
324 // Return the output section.
325 const Output_section*
326 output_section() const
327 { return this->output_section_; }
329 // Record the output section.
331 set_output_section(Output_section* os);
333 // Add an input section, for SHF_MERGE sections. This returns true
334 // if the section was handled.
336 add_input_section(Relobj* object, unsigned int shndx)
337 { return this->do_add_input_section(object, shndx); }
339 // Given an input OBJECT, an input section index SHNDX within that
340 // object, and an OFFSET relative to the start of that input
341 // section, return whether or not the corresponding offset within
342 // the output section is known. If this function returns true, it
343 // sets *POUTPUT to the output offset. The value -1 indicates that
344 // this input offset is being discarded.
346 output_offset(const Relobj* object, unsigned int shndx, off_t offset,
347 off_t *poutput) const
348 { return this->do_output_offset(object, shndx, offset, poutput); }
351 // The child class must implement do_write.
353 // The child class may implement specific adjustments to the output
356 do_adjust_output_section(Output_section*)
359 // May be implemented by child class. Return true if the section
362 do_add_input_section(Relobj*, unsigned int)
363 { gold_unreachable(); }
365 // The child class may implement output_offset.
367 do_output_offset(const Relobj*, unsigned int, off_t, off_t*) const
370 // Return the required alignment.
373 { return this->addralign_; }
375 // Return the section index of the output section.
377 do_out_shndx() const;
379 // Set the alignment.
381 set_addralign(uint64_t addralign)
382 { this->addralign_ = addralign; }
385 // The output section for this section.
386 const Output_section* output_section_;
387 // The required alignment.
391 // A simple case of Output_data in which we have constant data to
394 class Output_data_const : public Output_section_data
397 Output_data_const(const std::string& data, uint64_t addralign)
398 : Output_section_data(data.size(), addralign), data_(data)
401 Output_data_const(const char* p, off_t len, uint64_t addralign)
402 : Output_section_data(len, addralign), data_(p, len)
405 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
406 : Output_section_data(len, addralign),
407 data_(reinterpret_cast<const char*>(p), len)
412 add_data(const std::string& add)
414 this->data_.append(add);
415 this->set_data_size(this->data_.size());
418 // Write the data to the output file.
420 do_write(Output_file*);
426 // Another version of Output_data with constant data, in which the
427 // buffer is allocated by the caller.
429 class Output_data_const_buffer : public Output_section_data
432 Output_data_const_buffer(const unsigned char* p, off_t len,
434 : Output_section_data(len, addralign), p_(p)
437 // Write the data the output file.
439 do_write(Output_file*);
442 const unsigned char* p_;
445 // A place holder for data written out via some other mechanism.
447 class Output_data_space : public Output_section_data
450 Output_data_space(off_t data_size, uint64_t addralign)
451 : Output_section_data(data_size, addralign)
454 explicit Output_data_space(uint64_t addralign)
455 : Output_section_data(addralign)
460 set_space_size(off_t space_size)
461 { this->set_data_size(space_size); }
463 // Set the alignment.
465 set_space_alignment(uint64_t align)
466 { this->set_addralign(align); }
468 // Write out the data--this must be handled elsewhere.
470 do_write(Output_file*)
474 // A string table which goes into an output section.
476 class Output_data_strtab : public Output_section_data
479 Output_data_strtab(Stringpool* strtab)
480 : Output_section_data(1), strtab_(strtab)
483 // This is called to set the address and file offset. Here we make
484 // sure that the Stringpool is finalized.
486 do_set_address(uint64_t, off_t);
488 // Write out the data.
490 do_write(Output_file*);
496 // This POD class is used to represent a single reloc in the output
497 // file. This could be a private class within Output_data_reloc, but
498 // the templatization is complex enough that I broke it out into a
499 // separate class. The class is templatized on either elfcpp::SHT_REL
500 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
501 // relocation or an ordinary relocation.
503 // A relocation can be against a global symbol, a local symbol, an
504 // output section, or the undefined symbol at index 0. We represent
505 // the latter by using a NULL global symbol.
507 template<int sh_type, bool dynamic, int size, bool big_endian>
510 template<bool dynamic, int size, bool big_endian>
511 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
514 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
516 // An uninitialized entry. We need this because we want to put
517 // instances of this class into an STL container.
519 : local_sym_index_(INVALID_CODE)
522 // A reloc against a global symbol.
524 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
526 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
529 this->u1_.gsym = gsym;
533 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
534 unsigned int shndx, Address address)
535 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
538 gold_assert(shndx != INVALID_CODE);
539 this->u1_.gsym = gsym;
540 this->u2_.relobj = relobj;
543 // A reloc against a local symbol.
545 Output_reloc(Sized_relobj<size, big_endian>* relobj,
546 unsigned int local_sym_index,
550 : address_(address), local_sym_index_(local_sym_index), type_(type),
553 gold_assert(local_sym_index != GSYM_CODE
554 && local_sym_index != INVALID_CODE);
555 this->u1_.relobj = relobj;
559 Output_reloc(Sized_relobj<size, big_endian>* relobj,
560 unsigned int local_sym_index,
564 : address_(address), local_sym_index_(local_sym_index), type_(type),
567 gold_assert(local_sym_index != GSYM_CODE
568 && local_sym_index != INVALID_CODE);
569 gold_assert(shndx != INVALID_CODE);
570 this->u1_.relobj = relobj;
571 this->u2_.relobj = relobj;
574 // A reloc against the STT_SECTION symbol of an output section.
576 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
578 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
585 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
586 unsigned int shndx, Address address)
587 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
590 gold_assert(shndx != INVALID_CODE);
592 this->u2_.relobj = relobj;
595 // Write the reloc entry to an output view.
597 write(unsigned char* pov) const;
599 // Write the offset and info fields to Write_rel.
600 template<typename Write_rel>
601 void write_rel(Write_rel*) const;
604 // Return the symbol index. We can't do a double template
605 // specialization, so we do a secondary template here.
607 get_symbol_index() const;
609 // Codes for local_sym_index_.
616 // Invalid uninitialized entry.
622 // For a local symbol, the object. We will never generate a
623 // relocation against a local symbol in a dynamic object; that
624 // doesn't make sense. And our callers will always be
625 // templatized, so we use Sized_relobj here.
626 Sized_relobj<size, big_endian>* relobj;
627 // For a global symbol, the symbol. If this is NULL, it indicates
628 // a relocation against the undefined 0 symbol.
630 // For a relocation against an output section, the output section.
635 // If shndx_ is not INVALID CODE, the object which holds the input
636 // section being used to specify the reloc address.
638 // If shndx_ is INVALID_CODE, the output data being used to
639 // specify the reloc address. This may be NULL if the reloc
640 // address is absolute.
643 // The address offset within the input section or the Output_data.
645 // For a local symbol, the local symbol index. This is GSYM_CODE
646 // for a global symbol, or INVALID_CODE for an uninitialized value.
647 unsigned int local_sym_index_;
648 // The reloc type--a processor specific code.
650 // If the reloc address is an input section in an object, the
651 // section index. This is INVALID_CODE if the reloc address is
652 // specified in some other way.
656 // The SHT_RELA version of Output_reloc<>. This is just derived from
657 // the SHT_REL version of Output_reloc, but it adds an addend.
659 template<bool dynamic, int size, bool big_endian>
660 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
664 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
666 // An uninitialized entry.
671 // A reloc against a global symbol.
673 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
674 Address address, Addend addend)
675 : rel_(gsym, type, od, address), addend_(addend)
678 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
679 unsigned int shndx, Address address, Addend addend)
680 : rel_(gsym, type, relobj, shndx, address), addend_(addend)
683 // A reloc against a local symbol.
685 Output_reloc(Sized_relobj<size, big_endian>* relobj,
686 unsigned int local_sym_index,
687 unsigned int type, Output_data* od, Address address,
689 : rel_(relobj, local_sym_index, type, od, address), addend_(addend)
692 Output_reloc(Sized_relobj<size, big_endian>* relobj,
693 unsigned int local_sym_index,
698 : rel_(relobj, local_sym_index, type, shndx, address),
702 // A reloc against the STT_SECTION symbol of an output section.
704 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
705 Address address, Addend addend)
706 : rel_(os, type, od, address), addend_(addend)
709 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
710 unsigned int shndx, Address address, Addend addend)
711 : rel_(os, type, relobj, shndx, address), addend_(addend)
714 // Write the reloc entry to an output view.
716 write(unsigned char* pov) const;
720 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
725 // Output_data_reloc is used to manage a section containing relocs.
726 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
727 // indicates whether this is a dynamic relocation or a normal
728 // relocation. Output_data_reloc_base is a base class.
729 // Output_data_reloc is the real class, which we specialize based on
732 template<int sh_type, bool dynamic, int size, bool big_endian>
733 class Output_data_reloc_base : public Output_section_data
736 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
737 typedef typename Output_reloc_type::Address Address;
738 static const int reloc_size =
739 Reloc_types<sh_type, size, big_endian>::reloc_size;
741 // Construct the section.
742 Output_data_reloc_base()
743 : Output_section_data(Output_data::default_alignment_for_size(size))
746 // Write out the data.
748 do_write(Output_file*);
751 // Set the entry size and the link.
753 do_adjust_output_section(Output_section *os);
755 // Add a relocation entry.
757 add(const Output_reloc_type& reloc)
759 this->relocs_.push_back(reloc);
760 this->set_data_size(this->relocs_.size() * reloc_size);
764 typedef std::vector<Output_reloc_type> Relocs;
769 // The class which callers actually create.
771 template<int sh_type, bool dynamic, int size, bool big_endian>
772 class Output_data_reloc;
774 // The SHT_REL version of Output_data_reloc.
776 template<bool dynamic, int size, bool big_endian>
777 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
778 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
781 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
785 typedef typename Base::Output_reloc_type Output_reloc_type;
786 typedef typename Output_reloc_type::Address Address;
789 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
792 // Add a reloc against a global symbol.
795 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
796 { this->add(Output_reloc_type(gsym, type, od, address)); }
799 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
800 unsigned int shndx, Address address)
801 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address)); }
803 // Add a reloc against a local symbol.
806 add_local(Sized_relobj<size, big_endian>* relobj,
807 unsigned int local_sym_index, unsigned int type,
808 Output_data* od, Address address)
809 { this->add(Output_reloc_type(relobj, local_sym_index, type, od, address)); }
812 add_local(Sized_relobj<size, big_endian>* relobj,
813 unsigned int local_sym_index, unsigned int type,
814 unsigned int shndx, Address address)
815 { this->add(Output_reloc_type(relobj, local_sym_index, type, shndx,
819 // A reloc against the STT_SECTION symbol of an output section.
822 add_output_section(Output_section* os, unsigned int type,
823 Output_data* od, Address address)
824 { this->add(Output_reloc_type(os, type, od, address)); }
827 add_output_section(Output_section* os, unsigned int type,
828 Relobj* relobj, unsigned int shndx, Address address)
829 { this->add(Output_reloc_type(os, type, relobj, shndx, address)); }
832 // The SHT_RELA version of Output_data_reloc.
834 template<bool dynamic, int size, bool big_endian>
835 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
836 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
839 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
843 typedef typename Base::Output_reloc_type Output_reloc_type;
844 typedef typename Output_reloc_type::Address Address;
845 typedef typename Output_reloc_type::Addend Addend;
848 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
851 // Add a reloc against a global symbol.
854 add_global(Symbol* gsym, unsigned int type, Output_data* od,
855 Address address, Addend addend)
856 { this->add(Output_reloc_type(gsym, type, od, address, addend)); }
859 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
860 unsigned int shndx, Address address, Addend addend)
861 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address, addend)); }
863 // Add a reloc against a local symbol.
866 add_local(Sized_relobj<size, big_endian>* relobj,
867 unsigned int local_sym_index, unsigned int type,
868 Output_data* od, Address address, Addend addend)
870 this->add(Output_reloc_type(relobj, local_sym_index, type, od, address,
875 add_local(Sized_relobj<size, big_endian>* relobj,
876 unsigned int local_sym_index, unsigned int type,
877 unsigned int shndx, Address address, Addend addend)
879 this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, address,
883 // A reloc against the STT_SECTION symbol of an output section.
886 add_output_section(Output_section* os, unsigned int type, Output_data* od,
887 Address address, Addend addend)
888 { this->add(Output_reloc_type(os, type, od, address, addend)); }
891 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
892 unsigned int shndx, Address address, Addend addend)
893 { this->add(Output_reloc_type(os, type, relobj, shndx, address, addend)); }
896 // Output_data_got is used to manage a GOT. Each entry in the GOT is
897 // for one symbol--either a global symbol or a local symbol in an
898 // object. The target specific code adds entries to the GOT as
901 template<int size, bool big_endian>
902 class Output_data_got : public Output_section_data
905 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
908 : Output_section_data(Output_data::default_alignment_for_size(size)),
912 // Add an entry for a global symbol to the GOT. Return true if this
913 // is a new GOT entry, false if the symbol was already in the GOT.
915 add_global(Symbol* gsym);
917 // Add an entry for a local symbol to the GOT. This returns true if
918 // this is a new GOT entry, false if the symbol already has a GOT
921 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
923 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
924 // Return true if this is a new GOT entry, false if the symbol was
925 // already in the GOT.
927 add_global_tls(Symbol* gsym, bool need_pair);
929 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
930 // This returns true if this is a new GOT entry, false if the symbol
931 // already has a GOT entry.
933 add_local_tls(Sized_relobj<size, big_endian>* object,
934 unsigned int sym_index, bool need_pair);
936 // Add a constant to the GOT. This returns the offset of the new
937 // entry from the start of the GOT.
939 add_constant(Valtype constant)
941 this->entries_.push_back(Got_entry(constant));
942 this->set_got_size();
943 return this->last_got_offset();
946 // Write out the GOT table.
948 do_write(Output_file*);
951 // This POD class holds a single GOT entry.
955 // Create a zero entry.
957 : local_sym_index_(CONSTANT_CODE)
958 { this->u_.constant = 0; }
960 // Create a global symbol entry.
961 explicit Got_entry(Symbol* gsym)
962 : local_sym_index_(GSYM_CODE)
963 { this->u_.gsym = gsym; }
965 // Create a local symbol entry.
966 Got_entry(Sized_relobj<size, big_endian>* object,
967 unsigned int local_sym_index)
968 : local_sym_index_(local_sym_index)
970 gold_assert(local_sym_index != GSYM_CODE
971 && local_sym_index != CONSTANT_CODE);
972 this->u_.object = object;
975 // Create a constant entry. The constant is a host value--it will
976 // be swapped, if necessary, when it is written out.
977 explicit Got_entry(Valtype constant)
978 : local_sym_index_(CONSTANT_CODE)
979 { this->u_.constant = constant; }
981 // Write the GOT entry to an output view.
983 write(unsigned char* pov) const;
994 // For a local symbol, the object.
995 Sized_relobj<size, big_endian>* object;
996 // For a global symbol, the symbol.
998 // For a constant, the constant.
1001 // For a local symbol, the local symbol index. This is GSYM_CODE
1002 // for a global symbol, or CONSTANT_CODE for a constant.
1003 unsigned int local_sym_index_;
1006 typedef std::vector<Got_entry> Got_entries;
1008 // Return the offset into the GOT of GOT entry I.
1010 got_offset(unsigned int i) const
1011 { return i * (size / 8); }
1013 // Return the offset into the GOT of the last entry added.
1015 last_got_offset() const
1016 { return this->got_offset(this->entries_.size() - 1); }
1018 // Set the size of the section.
1021 { this->set_data_size(this->got_offset(this->entries_.size())); }
1023 // The list of GOT entries.
1024 Got_entries entries_;
1027 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1030 class Output_data_dynamic : public Output_section_data
1033 Output_data_dynamic(Stringpool* pool)
1034 : Output_section_data(Output_data::default_alignment()),
1035 entries_(), pool_(pool)
1038 // Add a new dynamic entry with a fixed numeric value.
1040 add_constant(elfcpp::DT tag, unsigned int val)
1041 { this->add_entry(Dynamic_entry(tag, val)); }
1043 // Add a new dynamic entry with the address of output data.
1045 add_section_address(elfcpp::DT tag, const Output_data* od)
1046 { this->add_entry(Dynamic_entry(tag, od, false)); }
1048 // Add a new dynamic entry with the size of output data.
1050 add_section_size(elfcpp::DT tag, const Output_data* od)
1051 { this->add_entry(Dynamic_entry(tag, od, true)); }
1053 // Add a new dynamic entry with the address of a symbol.
1055 add_symbol(elfcpp::DT tag, const Symbol* sym)
1056 { this->add_entry(Dynamic_entry(tag, sym)); }
1058 // Add a new dynamic entry with a string.
1060 add_string(elfcpp::DT tag, const char* str)
1061 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1064 add_string(elfcpp::DT tag, const std::string& str)
1065 { this->add_string(tag, str.c_str()); }
1067 // Set the final data size.
1069 do_set_address(uint64_t, off_t);
1071 // Write out the dynamic entries.
1073 do_write(Output_file*);
1076 // Adjust the output section to set the entry size.
1078 do_adjust_output_section(Output_section*);
1081 // This POD class holds a single dynamic entry.
1085 // Create an entry with a fixed numeric value.
1086 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1087 : tag_(tag), classification_(DYNAMIC_NUMBER)
1088 { this->u_.val = val; }
1090 // Create an entry with the size or address of a section.
1091 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1093 classification_(section_size
1094 ? DYNAMIC_SECTION_SIZE
1095 : DYNAMIC_SECTION_ADDRESS)
1096 { this->u_.od = od; }
1098 // Create an entry with the address of a symbol.
1099 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1100 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1101 { this->u_.sym = sym; }
1103 // Create an entry with a string.
1104 Dynamic_entry(elfcpp::DT tag, const char* str)
1105 : tag_(tag), classification_(DYNAMIC_STRING)
1106 { this->u_.str = str; }
1108 // Write the dynamic entry to an output view.
1109 template<int size, bool big_endian>
1111 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1119 DYNAMIC_SECTION_ADDRESS,
1121 DYNAMIC_SECTION_SIZE,
1130 // For DYNAMIC_NUMBER.
1132 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1133 const Output_data* od;
1134 // For DYNAMIC_SYMBOL.
1136 // For DYNAMIC_STRING.
1141 // The type of entry.
1142 Classification classification_;
1145 // Add an entry to the list.
1147 add_entry(const Dynamic_entry& entry)
1148 { this->entries_.push_back(entry); }
1150 // Sized version of write function.
1151 template<int size, bool big_endian>
1153 sized_write(Output_file* of);
1155 // The type of the list of entries.
1156 typedef std::vector<Dynamic_entry> Dynamic_entries;
1159 Dynamic_entries entries_;
1160 // The pool used for strings.
1164 // An output section. We don't expect to have too many output
1165 // sections, so we don't bother to do a template on the size.
1167 class Output_section : public Output_data
1170 // Create an output section, giving the name, type, and flags.
1171 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1172 virtual ~Output_section();
1174 // Add a new input section SHNDX, named NAME, with header SHDR, from
1175 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1176 // which applies to this section, or 0 if none, or -1U if more than
1177 // one. Return the offset within the output section.
1178 template<int size, bool big_endian>
1180 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1182 const elfcpp::Shdr<size, big_endian>& shdr,
1183 unsigned int reloc_shndx);
1185 // Add generated data POSD to this output section.
1187 add_output_section_data(Output_section_data* posd);
1189 // Return the section name.
1192 { return this->name_; }
1194 // Return the section type.
1197 { return this->type_; }
1199 // Return the section flags.
1202 { return this->flags_; }
1204 // Return the section index in the output file.
1206 do_out_shndx() const
1208 gold_assert(this->out_shndx_ != -1U);
1209 return this->out_shndx_;
1212 // Set the output section index.
1214 do_set_out_shndx(unsigned int shndx)
1216 gold_assert(this->out_shndx_ == -1U);
1217 this->out_shndx_ = shndx;
1220 // Return the entsize field.
1223 { return this->entsize_; }
1225 // Set the entsize field.
1227 set_entsize(uint64_t v);
1229 // Set the link field to the output section index of a section.
1231 set_link_section(const Output_data* od)
1233 gold_assert(this->link_ == 0
1234 && !this->should_link_to_symtab_
1235 && !this->should_link_to_dynsym_);
1236 this->link_section_ = od;
1239 // Set the link field to a constant.
1241 set_link(unsigned int v)
1243 gold_assert(this->link_section_ == NULL
1244 && !this->should_link_to_symtab_
1245 && !this->should_link_to_dynsym_);
1249 // Record that this section should link to the normal symbol table.
1251 set_should_link_to_symtab()
1253 gold_assert(this->link_section_ == NULL
1255 && !this->should_link_to_dynsym_);
1256 this->should_link_to_symtab_ = true;
1259 // Record that this section should link to the dynamic symbol table.
1261 set_should_link_to_dynsym()
1263 gold_assert(this->link_section_ == NULL
1265 && !this->should_link_to_symtab_);
1266 this->should_link_to_dynsym_ = true;
1269 // Return the info field.
1273 gold_assert(this->info_section_ == NULL);
1277 // Set the info field to the output section index of a section.
1279 set_info_section(const Output_data* od)
1281 gold_assert(this->info_ == 0);
1282 this->info_section_ = od;
1285 // Set the info field to a constant.
1287 set_info(unsigned int v)
1289 gold_assert(this->info_section_ == NULL);
1293 // Set the addralign field.
1295 set_addralign(uint64_t v)
1296 { this->addralign_ = v; }
1298 // Indicate that we need a symtab index.
1300 set_needs_symtab_index()
1301 { this->needs_symtab_index_ = true; }
1303 // Return whether we need a symtab index.
1305 needs_symtab_index() const
1306 { return this->needs_symtab_index_; }
1308 // Get the symtab index.
1310 symtab_index() const
1312 gold_assert(this->symtab_index_ != 0);
1313 return this->symtab_index_;
1316 // Set the symtab index.
1318 set_symtab_index(unsigned int index)
1320 gold_assert(index != 0);
1321 this->symtab_index_ = index;
1324 // Indicate that we need a dynsym index.
1326 set_needs_dynsym_index()
1327 { this->needs_dynsym_index_ = true; }
1329 // Return whether we need a dynsym index.
1331 needs_dynsym_index() const
1332 { return this->needs_dynsym_index_; }
1334 // Get the dynsym index.
1336 dynsym_index() const
1338 gold_assert(this->dynsym_index_ != 0);
1339 return this->dynsym_index_;
1342 // Set the dynsym index.
1344 set_dynsym_index(unsigned int index)
1346 gold_assert(index != 0);
1347 this->dynsym_index_ = index;
1350 // Return whether this section should be written after all the input
1351 // sections are complete.
1353 after_input_sections() const
1354 { return this->after_input_sections_; }
1356 // Record that this section should be written after all the input
1357 // sections are complete.
1359 set_after_input_sections()
1360 { this->after_input_sections_ = true; }
1362 // Return whether the offset OFFSET in the input section SHNDX in
1363 // object OBJECT is being included in the link.
1365 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1366 off_t offset) const;
1368 // Return the offset within the output section of OFFSET relative to
1369 // the start of input section SHNDX in object OBJECT.
1371 output_offset(const Relobj* object, unsigned int shndx, off_t offset) const;
1373 // Return the output virtual address of OFFSET relative to the start
1374 // of input section SHNDX in object OBJECT.
1376 output_address(const Relobj* object, unsigned int shndx,
1377 off_t offset) const;
1379 // Set the address of the Output_section. For a typical
1380 // Output_section, there is nothing to do, but if there are any
1381 // Output_section_data objects we need to set the final addresses
1384 do_set_address(uint64_t, off_t);
1386 // Write the data to the file. For a typical Output_section, this
1387 // does nothing: the data is written out by calling Object::Relocate
1388 // on each input object. But if there are any Output_section_data
1389 // objects we do need to write them out here.
1391 do_write(Output_file*);
1393 // Return the address alignment--function required by parent class.
1395 do_addralign() const
1396 { return this->addralign_; }
1398 // Return whether this is an Output_section.
1400 do_is_section() const
1403 // Return whether this is a section of the specified type.
1405 do_is_section_type(elfcpp::Elf_Word type) const
1406 { return this->type_ == type; }
1408 // Return whether the specified section flag is set.
1410 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1411 { return (this->flags_ & flag) != 0; }
1413 // Write the section header into *OPHDR.
1414 template<int size, bool big_endian>
1416 write_header(const Layout*, const Stringpool*,
1417 elfcpp::Shdr_write<size, big_endian>*) const;
1420 // In some cases we need to keep a list of the input sections
1421 // associated with this output section. We only need the list if we
1422 // might have to change the offsets of the input section within the
1423 // output section after we add the input section. The ordinary
1424 // input sections will be written out when we process the object
1425 // file, and as such we don't need to track them here. We do need
1426 // to track Output_section_data objects here. We store instances of
1427 // this structure in a std::vector, so it must be a POD. There can
1428 // be many instances of this structure, so we use a union to save
1434 : shndx_(0), p2align_(0)
1436 this->u1_.data_size = 0;
1437 this->u2_.object = NULL;
1440 // For an ordinary input section.
1441 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1444 p2align_(ffsll(static_cast<long long>(addralign)))
1446 gold_assert(shndx != OUTPUT_SECTION_CODE
1447 && shndx != MERGE_DATA_SECTION_CODE
1448 && shndx != MERGE_STRING_SECTION_CODE);
1449 this->u1_.data_size = data_size;
1450 this->u2_.object = object;
1453 // For a non-merge output section.
1454 Input_section(Output_section_data* posd)
1455 : shndx_(OUTPUT_SECTION_CODE),
1456 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1458 this->u1_.data_size = 0;
1459 this->u2_.posd = posd;
1462 // For a merge section.
1463 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1465 ? MERGE_STRING_SECTION_CODE
1466 : MERGE_DATA_SECTION_CODE),
1467 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1469 this->u1_.entsize = entsize;
1470 this->u2_.posd = posd;
1473 // The required alignment.
1477 return (this->p2align_ == 0
1479 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1482 // Return the required size.
1486 // Return whether this is a merge section which matches the
1489 is_merge_section(bool is_string, uint64_t entsize,
1490 uint64_t addralign) const
1492 return (this->shndx_ == (is_string
1493 ? MERGE_STRING_SECTION_CODE
1494 : MERGE_DATA_SECTION_CODE)
1495 && this->u1_.entsize == entsize
1496 && this->addralign() == addralign);
1499 // Set the output section.
1501 set_output_section(Output_section* os)
1503 gold_assert(!this->is_input_section());
1504 this->u2_.posd->set_output_section(os);
1507 // Set the address and file offset. This is called during
1508 // Layout::finalize. SECOFF is the file offset of the enclosing
1511 set_address(uint64_t addr, off_t off, off_t secoff);
1513 // Add an input section, for SHF_MERGE sections.
1515 add_input_section(Relobj* object, unsigned int shndx)
1517 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
1518 || this->shndx_ == MERGE_STRING_SECTION_CODE);
1519 return this->u2_.posd->add_input_section(object, shndx);
1522 // Given an input OBJECT, an input section index SHNDX within that
1523 // object, and an OFFSET relative to the start of that input
1524 // section, return whether or not the output offset is known. If
1525 // this function returns true, it sets *POUTPUT to the output
1528 output_offset(const Relobj* object, unsigned int shndx, off_t offset,
1529 off_t *poutput) const;
1531 // Write out the data. This does nothing for an input section.
1533 write(Output_file*);
1536 // Code values which appear in shndx_. If the value is not one of
1537 // these codes, it is the input section index in the object file.
1540 // An Output_section_data.
1541 OUTPUT_SECTION_CODE = -1U,
1542 // An Output_section_data for an SHF_MERGE section with
1543 // SHF_STRINGS not set.
1544 MERGE_DATA_SECTION_CODE = -2U,
1545 // An Output_section_data for an SHF_MERGE section with
1547 MERGE_STRING_SECTION_CODE = -3U
1550 // Whether this is an input section.
1552 is_input_section() const
1554 return (this->shndx_ != OUTPUT_SECTION_CODE
1555 && this->shndx_ != MERGE_DATA_SECTION_CODE
1556 && this->shndx_ != MERGE_STRING_SECTION_CODE);
1559 // For an ordinary input section, this is the section index in the
1560 // input file. For an Output_section_data, this is
1561 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1562 // MERGE_STRING_SECTION_CODE.
1563 unsigned int shndx_;
1564 // The required alignment, stored as a power of 2.
1565 unsigned int p2align_;
1568 // For an ordinary input section, the section size.
1570 // For OUTPUT_SECTION_CODE, this is not used. For
1571 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1577 // For an ordinary input section, the object which holds the
1580 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1581 // MERGE_STRING_SECTION_CODE, the data.
1582 Output_section_data* posd;
1586 typedef std::vector<Input_section> Input_section_list;
1588 // Fill data. This is used to fill in data between input sections.
1589 // When we have to keep track of the input sections, we can use an
1590 // Output_data_const, but we don't want to have to keep track of
1591 // input sections just to implement fills. For a fill we record the
1592 // offset, and the actual data to be written out.
1596 Fill(off_t section_offset, off_t length)
1597 : section_offset_(section_offset), length_(length)
1600 // Return section offset.
1602 section_offset() const
1603 { return this->section_offset_; }
1605 // Return fill length.
1608 { return this->length_; }
1611 // The offset within the output section.
1612 off_t section_offset_;
1613 // The length of the space to fill.
1617 typedef std::vector<Fill> Fill_list;
1619 // Add a new output section by Input_section.
1621 add_output_section_data(Input_section*);
1623 // Add an SHF_MERGE input section. Returns true if the section was
1626 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
1627 uint64_t entsize, uint64_t addralign);
1629 // Add an output SHF_MERGE section POSD to this output section.
1630 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1631 // ENTSIZE is the entity size. This returns the entry added to
1634 add_output_merge_section(Output_section_data* posd, bool is_string,
1637 // Most of these fields are only valid after layout.
1639 // The name of the section. This will point into a Stringpool.
1641 // The section address is in the parent class.
1642 // The section alignment.
1643 uint64_t addralign_;
1644 // The section entry size.
1646 // The file offset is in the parent class.
1647 // Set the section link field to the index of this section.
1648 const Output_data* link_section_;
1649 // If link_section_ is NULL, this is the link field.
1651 // Set the section info field to the index of this section.
1652 const Output_data* info_section_;
1653 // If info_section_ is NULL, this is the section info field.
1655 // The section type.
1656 elfcpp::Elf_Word type_;
1657 // The section flags.
1658 elfcpp::Elf_Xword flags_;
1659 // The section index.
1660 unsigned int out_shndx_;
1661 // If there is a STT_SECTION for this output section in the normal
1662 // symbol table, this is the symbol index. This starts out as zero.
1663 // It is initialized in Layout::finalize() to be the index, or -1U
1664 // if there isn't one.
1665 unsigned int symtab_index_;
1666 // If there is a STT_SECTION for this output section in the dynamic
1667 // symbol table, this is the symbol index. This starts out as zero.
1668 // It is initialized in Layout::finalize() to be the index, or -1U
1669 // if there isn't one.
1670 unsigned int dynsym_index_;
1671 // The input sections. This will be empty in cases where we don't
1672 // need to keep track of them.
1673 Input_section_list input_sections_;
1674 // The offset of the first entry in input_sections_.
1675 off_t first_input_offset_;
1676 // The fill data. This is separate from input_sections_ because we
1677 // often will need fill sections without needing to keep track of
1680 // Whether this output section needs a STT_SECTION symbol in the
1681 // normal symbol table. This will be true if there is a relocation
1683 bool needs_symtab_index_ : 1;
1684 // Whether this output section needs a STT_SECTION symbol in the
1685 // dynamic symbol table. This will be true if there is a dynamic
1686 // relocation which needs it.
1687 bool needs_dynsym_index_ : 1;
1688 // Whether the link field of this output section should point to the
1689 // normal symbol table.
1690 bool should_link_to_symtab_ : 1;
1691 // Whether the link field of this output section should point to the
1692 // dynamic symbol table.
1693 bool should_link_to_dynsym_ : 1;
1694 // Whether this section should be written after all the input
1695 // sections are complete.
1696 bool after_input_sections_ : 1;
1699 // An output segment. PT_LOAD segments are built from collections of
1700 // output sections. Other segments typically point within PT_LOAD
1701 // segments, and are built directly as needed.
1703 class Output_segment
1706 // Create an output segment, specifying the type and flags.
1707 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
1709 // Return the virtual address.
1712 { return this->vaddr_; }
1714 // Return the physical address.
1717 { return this->paddr_; }
1719 // Return the segment type.
1722 { return this->type_; }
1724 // Return the segment flags.
1727 { return this->flags_; }
1729 // Return the memory size.
1732 { return this->memsz_; }
1734 // Return the file size.
1737 { return this->filesz_; }
1739 // Return the maximum alignment of the Output_data.
1743 // Add an Output_section to this segment.
1745 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1746 { this->add_output_section(os, seg_flags, false); }
1748 // Add an Output_section to the start of this segment.
1750 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1751 { this->add_output_section(os, seg_flags, true); }
1753 // Add an Output_data (which is not an Output_section) to the start
1756 add_initial_output_data(Output_data*);
1758 // Set the address of the segment to ADDR and the offset to *POFF
1759 // (aligned if necessary), and set the addresses and offsets of all
1760 // contained output sections accordingly. Set the section indexes
1761 // of all contained output sections starting with *PSHNDX. Return
1762 // the address of the immediately following segment. Update *POFF
1763 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1765 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
1767 // Set the minimum alignment of this segment. This may be adjusted
1768 // upward based on the section alignments.
1770 set_minimum_addralign(uint64_t align)
1772 gold_assert(!this->is_align_known_);
1773 this->align_ = align;
1776 // Set the offset of this segment based on the section. This should
1777 // only be called for a non-PT_LOAD segment.
1781 // Return the number of output sections.
1783 output_section_count() const;
1785 // Write the segment header into *OPHDR.
1786 template<int size, bool big_endian>
1788 write_header(elfcpp::Phdr_write<size, big_endian>*);
1790 // Write the section headers of associated sections into V.
1791 template<int size, bool big_endian>
1793 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
1794 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
1797 Output_segment(const Output_segment&);
1798 Output_segment& operator=(const Output_segment&);
1800 typedef std::list<Output_data*> Output_data_list;
1802 // Add an Output_section to this segment, specifying front or back.
1804 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
1807 // Find the maximum alignment in an Output_data_list.
1809 maximum_alignment(const Output_data_list*);
1811 // Set the section addresses in an Output_data_list.
1813 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
1814 unsigned int* pshndx);
1816 // Return the number of Output_sections in an Output_data_list.
1818 output_section_count_list(const Output_data_list*) const;
1820 // Write the section headers in the list into V.
1821 template<int size, bool big_endian>
1823 write_section_headers_list(const Layout*, const Stringpool*,
1824 const Output_data_list*, unsigned char* v,
1825 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
1827 // The list of output data with contents attached to this segment.
1828 Output_data_list output_data_;
1829 // The list of output data without contents attached to this segment.
1830 Output_data_list output_bss_;
1831 // The segment virtual address.
1833 // The segment physical address.
1835 // The size of the segment in memory.
1837 // The segment alignment. The is_align_known_ field indicates
1838 // whether this has been finalized. It can be set to a minimum
1839 // value before it is finalized.
1841 // The offset of the segment data within the file.
1843 // The size of the segment data in the file.
1845 // The segment type;
1846 elfcpp::Elf_Word type_;
1847 // The segment flags.
1848 elfcpp::Elf_Word flags_;
1849 // Whether we have finalized align_.
1850 bool is_align_known_;
1853 // This class represents the output file.
1858 Output_file(const General_options& options, Target*);
1860 // Get a pointer to the target.
1863 { return this->target_; }
1865 // Open the output file. FILE_SIZE is the final size of the file.
1867 open(off_t file_size);
1869 // Close the output file and make sure there are no error.
1873 // We currently always use mmap which makes the view handling quite
1874 // simple. In the future we may support other approaches.
1876 // Write data to the output file.
1878 write(off_t offset, const void* data, off_t len)
1879 { memcpy(this->base_ + offset, data, len); }
1881 // Get a buffer to use to write to the file, given the offset into
1882 // the file and the size.
1884 get_output_view(off_t start, off_t size)
1886 gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
1887 return this->base_ + start;
1890 // VIEW must have been returned by get_output_view. Write the
1891 // buffer to the file, passing in the offset and the size.
1893 write_output_view(off_t, off_t, unsigned char*)
1896 // Get a read/write buffer. This is used when we want to write part
1897 // of the file, read it in, and write it again.
1899 get_input_output_view(off_t start, off_t size)
1900 { return this->get_output_view(start, size); }
1902 // Write a read/write buffer back to the file.
1904 write_input_output_view(off_t, off_t, unsigned char*)
1907 // Get a read buffer. This is used when we just want to read part
1908 // of the file back it in.
1909 const unsigned char*
1910 get_input_view(off_t start, off_t size)
1911 { return this->get_output_view(start, size); }
1913 // Release a read bfufer.
1915 free_input_view(off_t, off_t, const unsigned char*)
1920 const General_options& options_;
1929 // Base of file mapped into memory.
1930 unsigned char* base_;
1933 } // End namespace gold.
1935 #endif // !defined(GOLD_OUTPUT_H)