1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "reloc-types.h"
36 class General_options;
41 class Relocatable_relocs;
43 template<int size, bool big_endian>
45 template<int size, bool big_endian>
48 // An abtract class for data which has to go into the output file.
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
68 gold_assert(this->is_address_valid_);
69 return this->address_;
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
88 gold_assert(this->is_offset_valid_);
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
95 reset_address_and_file_offset()
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
103 // Return the required alignment.
106 { return this->do_addralign(); }
108 // Return whether this has a load address.
110 has_load_address() const
111 { return this->do_has_load_address(); }
113 // Return the load address.
116 { return this->do_load_address(); }
118 // Return whether this is an Output_section.
121 { return this->do_is_section(); }
123 // Return whether this is an Output_section of the specified type.
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
128 // Return whether this is an Output_section with the specified flag
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
134 // Return the output section that this goes in, if there is one.
137 { return this->do_output_section(); }
139 // Return the output section index, if there is an output section.
142 { return this->do_out_shndx(); }
144 // Set the output section index, if this is an output section.
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
153 set_address_and_file_offset(uint64_t addr, off_t off)
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
162 set_address(uint64_t addr)
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
169 // Set the file offset.
171 set_file_offset(off_t off)
173 gold_assert(!this->is_offset_valid_);
175 this->is_offset_valid_ = true;
178 // Finalize the data size.
182 if (!this->is_data_size_valid_)
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
190 // Set the TLS offset. Called only for SHT_TLS sections.
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
199 { return this->do_tls_offset(); }
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
204 write(Output_file* file)
205 { this->do_write(file); }
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
211 { Output_data::allocated_sizes_are_fixed = true; }
213 // Used to check that layout has been done.
216 { return Output_data::allocated_sizes_are_fixed; }
218 // Count the number of dynamic relocations applied to this section.
221 { ++this->dynamic_reloc_count_; }
223 // Return the number of dynamic relocations applied to this section.
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
228 // Whether the address is valid.
230 is_address_valid() const
231 { return this->is_address_valid_; }
233 // Whether the file offset is valid.
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
238 // Whether the data size is valid.
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
244 // Functions that child classes may or in some cases must implement.
246 // Write the data to the output file.
248 do_write(Output_file*) = 0;
250 // Return the required alignment.
252 do_addralign() const = 0;
254 // Return whether this has a load address.
256 do_has_load_address() const
259 // Return the load address.
261 do_load_address() const
262 { gold_unreachable(); }
264 // Return whether this is an Output_section.
266 do_is_section() const
269 // Return whether this is an Output_section of the specified type.
270 // This only needs to be implement by Output_section.
272 do_is_section_type(elfcpp::Elf_Word) const
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
278 do_is_section_flag_set(elfcpp::Elf_Xword) const
281 // Return the output section, if there is one.
282 virtual Output_section*
286 // Return the output section index, if there is an output section.
289 { gold_unreachable(); }
291 // Set the output section index, if this is an output section.
293 do_set_out_shndx(unsigned int)
294 { gold_unreachable(); }
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
300 set_final_data_size()
301 { gold_unreachable(); }
303 // A hook for resetting the address and file offset.
305 do_reset_address_and_file_offset()
308 // Set the TLS offset. Called only for SHT_TLS sections.
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
316 do_tls_offset() const
317 { gold_unreachable(); }
319 // Functions that child classes may call.
321 // Set the size of the data.
323 set_data_size(off_t data_size)
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
333 current_data_size_for_child() const
334 { return this->data_size_; }
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
339 set_current_data_size_for_child(off_t data_size)
341 gold_assert(!this->is_data_size_valid_);
342 this->data_size_ = data_size;
345 // Return default alignment for the target size.
349 // Return default alignment for a specified size--32 or 64.
351 default_alignment_for_size(int size);
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
357 // This is used for verification, to make sure that we don't try to
358 // change any sizes of allocated sections after we set the section
360 static bool allocated_sizes_are_fixed;
362 // Memory address in output file.
364 // Size of data in output file.
366 // File offset of contents in output file.
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
378 // Output the section headers.
380 class Output_section_headers : public Output_data
383 Output_section_headers(const Layout*,
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
386 const Layout::Section_list*,
390 // Write the data to the file.
392 do_write(Output_file*);
394 // Return the required alignment.
397 { return Output_data::default_alignment(); }
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
403 do_sized_write(Output_file*);
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
407 const Layout::Section_list* section_list_;
408 const Layout::Section_list* unattached_section_list_;
409 const Stringpool* secnamepool_;
412 // Output the segment headers.
414 class Output_segment_headers : public Output_data
417 Output_segment_headers(const Layout::Segment_list& segment_list);
420 // Write the data to the file.
422 do_write(Output_file*);
424 // Return the required alignment.
427 { return Output_data::default_alignment(); }
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
433 do_sized_write(Output_file*);
435 const Layout::Segment_list& segment_list_;
438 // Output the ELF file header.
440 class Output_file_header : public Output_data
443 Output_file_header(const Target*,
445 const Output_segment_headers*,
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
454 // Write the data to the file.
456 do_write(Output_file*);
458 // Return the required alignment.
461 { return Output_data::default_alignment(); }
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
467 do_sized_write(Output_file*);
469 // Return the value to use for the entry address.
471 typename elfcpp::Elf_types<size>::Elf_Addr
474 const Target* target_;
475 const Symbol_table* symtab_;
476 const Output_segment_headers* segment_header_;
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
482 // Output sections are mainly comprised of input sections. However,
483 // there are cases where we have data to write out which is not in an
484 // input section. Output_section_data is used in such cases. This is
485 // an abstract base class.
487 class Output_section_data : public Output_data
490 Output_section_data(off_t data_size, uint64_t addralign)
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
494 Output_section_data(uint64_t addralign)
495 : Output_data(), output_section_(NULL), addralign_(addralign)
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
503 // Record the output section.
505 set_output_section(Output_section* os);
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
523 { return this->do_output_offset(object, shndx, offset, poutput); }
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
545 // The child class must implement do_write.
547 // The child class may implement specific adjustments to the output
550 do_adjust_output_section(Output_section*)
553 // May be implemented by child class. Return true if the section
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
559 // The child class may implement output_offset.
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
565 // The child class may implement is_merge_section_for.
567 do_is_merge_section_for(const Relobj*, unsigned int) const
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
577 // Print merge statistics.
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
582 // Return the required alignment.
585 { return this->addralign_; }
587 // Return the output section.
590 { return this->output_section_; }
592 // Return the section index of the output section.
594 do_out_shndx() const;
596 // Set the alignment.
598 set_addralign(uint64_t addralign)
599 { this->addralign_ = addralign; }
602 // The output section for this section.
603 Output_section* output_section_;
604 // The required alignment.
608 // Some Output_section_data classes build up their data step by step,
609 // rather than all at once. This class provides an interface for
612 class Output_section_data_build : public Output_section_data
615 Output_section_data_build(uint64_t addralign)
616 : Output_section_data(addralign)
619 // Get the current data size.
621 current_data_size() const
622 { return this->current_data_size_for_child(); }
624 // Set the current data size.
626 set_current_data_size(off_t data_size)
627 { this->set_current_data_size_for_child(data_size); }
630 // Set the final data size.
632 set_final_data_size()
633 { this->set_data_size(this->current_data_size_for_child()); }
636 // A simple case of Output_data in which we have constant data to
639 class Output_data_const : public Output_section_data
642 Output_data_const(const std::string& data, uint64_t addralign)
643 : Output_section_data(data.size(), addralign), data_(data)
646 Output_data_const(const char* p, off_t len, uint64_t addralign)
647 : Output_section_data(len, addralign), data_(p, len)
650 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
651 : Output_section_data(len, addralign),
652 data_(reinterpret_cast<const char*>(p), len)
656 // Write the data to the output file.
658 do_write(Output_file*);
660 // Write the data to a buffer.
662 do_write_to_buffer(unsigned char* buffer)
663 { memcpy(buffer, this->data_.data(), this->data_.size()); }
669 // Another version of Output_data with constant data, in which the
670 // buffer is allocated by the caller.
672 class Output_data_const_buffer : public Output_section_data
675 Output_data_const_buffer(const unsigned char* p, off_t len,
677 : Output_section_data(len, addralign), p_(p)
681 // Write the data the output file.
683 do_write(Output_file*);
685 // Write the data to a buffer.
687 do_write_to_buffer(unsigned char* buffer)
688 { memcpy(buffer, this->p_, this->data_size()); }
691 const unsigned char* p_;
694 // A place holder for a fixed amount of data written out via some
697 class Output_data_fixed_space : public Output_section_data
700 Output_data_fixed_space(off_t data_size, uint64_t addralign)
701 : Output_section_data(data_size, addralign)
705 // Write out the data--the actual data must be written out
708 do_write(Output_file*)
712 // A place holder for variable sized data written out via some other
715 class Output_data_space : public Output_section_data_build
718 explicit Output_data_space(uint64_t addralign)
719 : Output_section_data_build(addralign)
722 // Set the alignment.
724 set_space_alignment(uint64_t align)
725 { this->set_addralign(align); }
728 // Write out the data--the actual data must be written out
731 do_write(Output_file*)
735 // A string table which goes into an output section.
737 class Output_data_strtab : public Output_section_data
740 Output_data_strtab(Stringpool* strtab)
741 : Output_section_data(1), strtab_(strtab)
745 // This is called to set the address and file offset. Here we make
746 // sure that the Stringpool is finalized.
748 set_final_data_size();
750 // Write out the data.
752 do_write(Output_file*);
754 // Write the data to a buffer.
756 do_write_to_buffer(unsigned char* buffer)
757 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
763 // This POD class is used to represent a single reloc in the output
764 // file. This could be a private class within Output_data_reloc, but
765 // the templatization is complex enough that I broke it out into a
766 // separate class. The class is templatized on either elfcpp::SHT_REL
767 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
768 // relocation or an ordinary relocation.
770 // A relocation can be against a global symbol, a local symbol, an
771 // output section, or the undefined symbol at index 0. We represent
772 // the latter by using a NULL global symbol.
774 template<int sh_type, bool dynamic, int size, bool big_endian>
777 template<bool dynamic, int size, bool big_endian>
778 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
783 // An uninitialized entry. We need this because we want to put
784 // instances of this class into an STL container.
786 : local_sym_index_(INVALID_CODE)
789 // A reloc against a global symbol.
791 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
792 Address address, bool is_relative);
794 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
795 unsigned int shndx, Address address, bool is_relative);
797 // A reloc against a local symbol.
799 Output_reloc(Sized_relobj<size, big_endian>* relobj,
800 unsigned int local_sym_index, unsigned int type,
801 Output_data* od, Address address, bool is_relative);
803 Output_reloc(Sized_relobj<size, big_endian>* relobj,
804 unsigned int local_sym_index, unsigned int type,
805 unsigned int shndx, Address address, bool is_relative);
807 // A reloc against the STT_SECTION symbol of an output section.
809 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
812 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
813 unsigned int shndx, Address address);
815 // Return TRUE if this is a RELATIVE relocation.
818 { return this->is_relative_; }
820 // Get the value of the symbol referred to by a Rel relocation.
823 symbol_value() const;
825 // Write the reloc entry to an output view.
827 write(unsigned char* pov) const;
829 // Write the offset and info fields to Write_rel.
830 template<typename Write_rel>
831 void write_rel(Write_rel*) const;
834 // Return the symbol index. We can't do a double template
835 // specialization, so we do a secondary template here.
837 get_symbol_index() const;
839 // Codes for local_sym_index_.
846 // Invalid uninitialized entry.
852 // For a local symbol, the object. We will never generate a
853 // relocation against a local symbol in a dynamic object; that
854 // doesn't make sense. And our callers will always be
855 // templatized, so we use Sized_relobj here.
856 Sized_relobj<size, big_endian>* relobj;
857 // For a global symbol, the symbol. If this is NULL, it indicates
858 // a relocation against the undefined 0 symbol.
860 // For a relocation against an output section, the output section.
865 // If shndx_ is not INVALID CODE, the object which holds the input
866 // section being used to specify the reloc address.
868 // If shndx_ is INVALID_CODE, the output data being used to
869 // specify the reloc address. This may be NULL if the reloc
870 // address is absolute.
873 // The address offset within the input section or the Output_data.
875 // For a local symbol, the local symbol index. This is GSYM_CODE
876 // for a global symbol, or INVALID_CODE for an uninitialized value.
877 unsigned int local_sym_index_;
878 // The reloc type--a processor specific code.
879 unsigned int type_ : 31;
880 // True if the relocation is a RELATIVE relocation.
881 bool is_relative_ : 1;
882 // If the reloc address is an input section in an object, the
883 // section index. This is INVALID_CODE if the reloc address is
884 // specified in some other way.
888 // The SHT_RELA version of Output_reloc<>. This is just derived from
889 // the SHT_REL version of Output_reloc, but it adds an addend.
891 template<bool dynamic, int size, bool big_endian>
892 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
895 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
896 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
898 // An uninitialized entry.
903 // A reloc against a global symbol.
905 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
906 Address address, Addend addend, bool is_relative)
907 : rel_(gsym, type, od, address, is_relative), addend_(addend)
910 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
911 unsigned int shndx, Address address, Addend addend,
913 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
916 // A reloc against a local symbol.
918 Output_reloc(Sized_relobj<size, big_endian>* relobj,
919 unsigned int local_sym_index, unsigned int type,
920 Output_data* od, Address address,
921 Addend addend, bool is_relative)
922 : rel_(relobj, local_sym_index, type, od, address, is_relative),
926 Output_reloc(Sized_relobj<size, big_endian>* relobj,
927 unsigned int local_sym_index, unsigned int type,
928 unsigned int shndx, Address address,
929 Addend addend, bool is_relative)
930 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
934 // A reloc against the STT_SECTION symbol of an output section.
936 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
937 Address address, Addend addend)
938 : rel_(os, type, od, address), addend_(addend)
941 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
942 unsigned int shndx, Address address, Addend addend)
943 : rel_(os, type, relobj, shndx, address), addend_(addend)
946 // Write the reloc entry to an output view.
948 write(unsigned char* pov) const;
952 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
957 // Output_data_reloc is used to manage a section containing relocs.
958 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
959 // indicates whether this is a dynamic relocation or a normal
960 // relocation. Output_data_reloc_base is a base class.
961 // Output_data_reloc is the real class, which we specialize based on
964 template<int sh_type, bool dynamic, int size, bool big_endian>
965 class Output_data_reloc_base : public Output_section_data_build
968 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
969 typedef typename Output_reloc_type::Address Address;
970 static const int reloc_size =
971 Reloc_types<sh_type, size, big_endian>::reloc_size;
973 // Construct the section.
974 Output_data_reloc_base()
975 : Output_section_data_build(Output_data::default_alignment_for_size(size))
979 // Write out the data.
981 do_write(Output_file*);
983 // Set the entry size and the link.
985 do_adjust_output_section(Output_section *os);
987 // Add a relocation entry.
989 add(Output_data *od, const Output_reloc_type& reloc)
991 this->relocs_.push_back(reloc);
992 this->set_current_data_size(this->relocs_.size() * reloc_size);
993 od->add_dynamic_reloc();
997 typedef std::vector<Output_reloc_type> Relocs;
1002 // The class which callers actually create.
1004 template<int sh_type, bool dynamic, int size, bool big_endian>
1005 class Output_data_reloc;
1007 // The SHT_REL version of Output_data_reloc.
1009 template<bool dynamic, int size, bool big_endian>
1010 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1011 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1014 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1018 typedef typename Base::Output_reloc_type Output_reloc_type;
1019 typedef typename Output_reloc_type::Address Address;
1022 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1025 // Add a reloc against a global symbol.
1028 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1029 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1032 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1033 unsigned int shndx, Address address)
1034 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1037 // Add a RELATIVE reloc against a global symbol. The final relocation
1038 // will not reference the symbol.
1041 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1043 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1046 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1047 Relobj* relobj, unsigned int shndx, Address address)
1048 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1051 // Add a reloc against a local symbol.
1054 add_local(Sized_relobj<size, big_endian>* relobj,
1055 unsigned int local_sym_index, unsigned int type,
1056 Output_data* od, Address address)
1057 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1061 add_local(Sized_relobj<size, big_endian>* relobj,
1062 unsigned int local_sym_index, unsigned int type,
1063 Output_data* od, unsigned int shndx, Address address)
1064 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1067 // Add a RELATIVE reloc against a local symbol.
1070 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1071 unsigned int local_sym_index, unsigned int type,
1072 Output_data* od, Address address)
1073 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1077 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1078 unsigned int local_sym_index, unsigned int type,
1079 Output_data* od, unsigned int shndx, Address address)
1080 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1083 // A reloc against the STT_SECTION symbol of an output section.
1084 // OS is the Output_section that the relocation refers to; OD is
1085 // the Output_data object being relocated.
1088 add_output_section(Output_section* os, unsigned int type,
1089 Output_data* od, Address address)
1090 { this->add(od, Output_reloc_type(os, type, od, address)); }
1093 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1094 Relobj* relobj, unsigned int shndx, Address address)
1095 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1098 // The SHT_RELA version of Output_data_reloc.
1100 template<bool dynamic, int size, bool big_endian>
1101 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1102 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1105 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1109 typedef typename Base::Output_reloc_type Output_reloc_type;
1110 typedef typename Output_reloc_type::Address Address;
1111 typedef typename Output_reloc_type::Addend Addend;
1114 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1117 // Add a reloc against a global symbol.
1120 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1121 Address address, Addend addend)
1122 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1126 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1127 unsigned int shndx, Address address,
1129 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1132 // Add a RELATIVE reloc against a global symbol. The final output
1133 // relocation will not reference the symbol, but we must keep the symbol
1134 // information long enough to set the addend of the relocation correctly
1135 // when it is written.
1138 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1139 Address address, Addend addend)
1140 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1143 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1144 Relobj* relobj, unsigned int shndx, Address address,
1146 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1149 // Add a reloc against a local symbol.
1152 add_local(Sized_relobj<size, big_endian>* relobj,
1153 unsigned int local_sym_index, unsigned int type,
1154 Output_data* od, Address address, Addend addend)
1156 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1161 add_local(Sized_relobj<size, big_endian>* relobj,
1162 unsigned int local_sym_index, unsigned int type,
1163 Output_data* od, unsigned int shndx, Address address,
1166 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1167 address, addend, false));
1170 // Add a RELATIVE reloc against a local symbol.
1173 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1174 unsigned int local_sym_index, unsigned int type,
1175 Output_data* od, Address address, Addend addend)
1177 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1182 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1183 unsigned int local_sym_index, unsigned int type,
1184 Output_data* od, unsigned int shndx, Address address,
1187 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1188 address, addend, true));
1191 // A reloc against the STT_SECTION symbol of an output section.
1194 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1195 Address address, Addend addend)
1196 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1199 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1200 unsigned int shndx, Address address, Addend addend)
1201 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1205 // Output_relocatable_relocs represents a relocation section in a
1206 // relocatable link. The actual data is written out in the target
1207 // hook relocate_for_relocatable. This just saves space for it.
1209 template<int sh_type, int size, bool big_endian>
1210 class Output_relocatable_relocs : public Output_section_data
1213 Output_relocatable_relocs(Relocatable_relocs* rr)
1214 : Output_section_data(Output_data::default_alignment_for_size(size)),
1219 set_final_data_size();
1221 // Write out the data. There is nothing to do here.
1223 do_write(Output_file*)
1227 // The relocs associated with this input section.
1228 Relocatable_relocs* rr_;
1231 // Handle a GROUP section.
1233 template<int size, bool big_endian>
1234 class Output_data_group : public Output_section_data
1237 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1238 section_size_type entry_count,
1239 const elfcpp::Elf_Word* contents);
1242 do_write(Output_file*);
1245 // The input object.
1246 Sized_relobj<size, big_endian>* relobj_;
1247 // The group flag word.
1248 elfcpp::Elf_Word flags_;
1249 // The section indexes of the input sections in this group.
1250 std::vector<unsigned int> input_sections_;
1253 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1254 // for one symbol--either a global symbol or a local symbol in an
1255 // object. The target specific code adds entries to the GOT as
1258 template<int size, bool big_endian>
1259 class Output_data_got : public Output_section_data_build
1262 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1263 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1264 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1267 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1271 // Add an entry for a global symbol to the GOT. Return true if this
1272 // is a new GOT entry, false if the symbol was already in the GOT.
1274 add_global(Symbol* gsym);
1276 // Add an entry for a global symbol to the GOT, and add a dynamic
1277 // relocation of type R_TYPE for the GOT entry.
1279 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1282 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1284 // Add an entry for a local symbol to the GOT. This returns true if
1285 // this is a new GOT entry, false if the symbol already has a GOT
1288 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1290 // Add an entry for a global symbol to the GOT, and add a dynamic
1291 // relocation of type R_TYPE for the GOT entry.
1293 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1294 unsigned int sym_index, Rel_dyn* rel_dyn,
1295 unsigned int r_type);
1298 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1299 unsigned int sym_index, Rela_dyn* rela_dyn,
1300 unsigned int r_type);
1302 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1303 // Return true if this is a new GOT entry, false if the symbol was
1304 // already in the GOT.
1306 add_global_tls(Symbol* gsym, bool need_pair);
1308 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1309 // relocation of type R_TYPE.
1311 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1312 unsigned int r_type);
1315 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1316 unsigned int r_type);
1318 // Add a pair of entries for a global TLS symbol to the GOT, and add
1319 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1321 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1322 unsigned int mod_r_type,
1323 unsigned int dtv_r_type);
1326 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1327 unsigned int mod_r_type,
1328 unsigned int dtv_r_type);
1330 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1331 // This returns true if this is a new GOT entry, false if the symbol
1332 // already has a GOT entry.
1334 add_local_tls(Sized_relobj<size, big_endian>* object,
1335 unsigned int sym_index, bool need_pair);
1337 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1338 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1339 // Because this is a local symbol, the first GOT entry can be relocated
1340 // relative to a section symbol, and the second GOT entry will have an
1341 // dtv-relative value that can be computed at link time.
1343 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1344 unsigned int sym_index, unsigned int shndx,
1345 bool need_pair, Rel_dyn* rel_dyn,
1346 unsigned int r_type);
1349 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1350 unsigned int sym_index, unsigned int shndx,
1351 bool need_pair, Rela_dyn* rela_dyn,
1352 unsigned int r_type);
1354 // Add a constant to the GOT. This returns the offset of the new
1355 // entry from the start of the GOT.
1357 add_constant(Valtype constant)
1359 this->entries_.push_back(Got_entry(constant));
1360 this->set_got_size();
1361 return this->last_got_offset();
1365 // Write out the GOT table.
1367 do_write(Output_file*);
1370 // This POD class holds a single GOT entry.
1374 // Create a zero entry.
1376 : local_sym_index_(CONSTANT_CODE)
1377 { this->u_.constant = 0; }
1379 // Create a global symbol entry.
1380 explicit Got_entry(Symbol* gsym)
1381 : local_sym_index_(GSYM_CODE)
1382 { this->u_.gsym = gsym; }
1384 // Create a local symbol entry.
1385 Got_entry(Sized_relobj<size, big_endian>* object,
1386 unsigned int local_sym_index)
1387 : local_sym_index_(local_sym_index)
1389 gold_assert(local_sym_index != GSYM_CODE
1390 && local_sym_index != CONSTANT_CODE);
1391 this->u_.object = object;
1394 // Create a constant entry. The constant is a host value--it will
1395 // be swapped, if necessary, when it is written out.
1396 explicit Got_entry(Valtype constant)
1397 : local_sym_index_(CONSTANT_CODE)
1398 { this->u_.constant = constant; }
1400 // Write the GOT entry to an output view.
1402 write(unsigned char* pov) const;
1413 // For a local symbol, the object.
1414 Sized_relobj<size, big_endian>* object;
1415 // For a global symbol, the symbol.
1417 // For a constant, the constant.
1420 // For a local symbol, the local symbol index. This is GSYM_CODE
1421 // for a global symbol, or CONSTANT_CODE for a constant.
1422 unsigned int local_sym_index_;
1425 typedef std::vector<Got_entry> Got_entries;
1427 // Return the offset into the GOT of GOT entry I.
1429 got_offset(unsigned int i) const
1430 { return i * (size / 8); }
1432 // Return the offset into the GOT of the last entry added.
1434 last_got_offset() const
1435 { return this->got_offset(this->entries_.size() - 1); }
1437 // Set the size of the section.
1440 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1442 // The list of GOT entries.
1443 Got_entries entries_;
1446 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1449 class Output_data_dynamic : public Output_section_data
1452 Output_data_dynamic(Stringpool* pool)
1453 : Output_section_data(Output_data::default_alignment()),
1454 entries_(), pool_(pool)
1457 // Add a new dynamic entry with a fixed numeric value.
1459 add_constant(elfcpp::DT tag, unsigned int val)
1460 { this->add_entry(Dynamic_entry(tag, val)); }
1462 // Add a new dynamic entry with the address of output data.
1464 add_section_address(elfcpp::DT tag, const Output_data* od)
1465 { this->add_entry(Dynamic_entry(tag, od, false)); }
1467 // Add a new dynamic entry with the size of output data.
1469 add_section_size(elfcpp::DT tag, const Output_data* od)
1470 { this->add_entry(Dynamic_entry(tag, od, true)); }
1472 // Add a new dynamic entry with the address of a symbol.
1474 add_symbol(elfcpp::DT tag, const Symbol* sym)
1475 { this->add_entry(Dynamic_entry(tag, sym)); }
1477 // Add a new dynamic entry with a string.
1479 add_string(elfcpp::DT tag, const char* str)
1480 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1483 add_string(elfcpp::DT tag, const std::string& str)
1484 { this->add_string(tag, str.c_str()); }
1487 // Adjust the output section to set the entry size.
1489 do_adjust_output_section(Output_section*);
1491 // Set the final data size.
1493 set_final_data_size();
1495 // Write out the dynamic entries.
1497 do_write(Output_file*);
1500 // This POD class holds a single dynamic entry.
1504 // Create an entry with a fixed numeric value.
1505 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1506 : tag_(tag), classification_(DYNAMIC_NUMBER)
1507 { this->u_.val = val; }
1509 // Create an entry with the size or address of a section.
1510 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1512 classification_(section_size
1513 ? DYNAMIC_SECTION_SIZE
1514 : DYNAMIC_SECTION_ADDRESS)
1515 { this->u_.od = od; }
1517 // Create an entry with the address of a symbol.
1518 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1519 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1520 { this->u_.sym = sym; }
1522 // Create an entry with a string.
1523 Dynamic_entry(elfcpp::DT tag, const char* str)
1524 : tag_(tag), classification_(DYNAMIC_STRING)
1525 { this->u_.str = str; }
1527 // Write the dynamic entry to an output view.
1528 template<int size, bool big_endian>
1530 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1538 DYNAMIC_SECTION_ADDRESS,
1540 DYNAMIC_SECTION_SIZE,
1549 // For DYNAMIC_NUMBER.
1551 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1552 const Output_data* od;
1553 // For DYNAMIC_SYMBOL.
1555 // For DYNAMIC_STRING.
1560 // The type of entry.
1561 Classification classification_;
1564 // Add an entry to the list.
1566 add_entry(const Dynamic_entry& entry)
1567 { this->entries_.push_back(entry); }
1569 // Sized version of write function.
1570 template<int size, bool big_endian>
1572 sized_write(Output_file* of);
1574 // The type of the list of entries.
1575 typedef std::vector<Dynamic_entry> Dynamic_entries;
1578 Dynamic_entries entries_;
1579 // The pool used for strings.
1583 // An output section. We don't expect to have too many output
1584 // sections, so we don't bother to do a template on the size.
1586 class Output_section : public Output_data
1589 // Create an output section, giving the name, type, and flags.
1590 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1591 virtual ~Output_section();
1593 // Add a new input section SHNDX, named NAME, with header SHDR, from
1594 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1595 // which applies to this section, or 0 if none, or -1U if more than
1596 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1597 // in a linker script; in that case we need to keep track of input
1598 // sections associated with an output section. Return the offset
1599 // within the output section.
1600 template<int size, bool big_endian>
1602 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1604 const elfcpp::Shdr<size, big_endian>& shdr,
1605 unsigned int reloc_shndx, bool have_sections_script);
1607 // Add generated data POSD to this output section.
1609 add_output_section_data(Output_section_data* posd);
1611 // Return the section name.
1614 { return this->name_; }
1616 // Return the section type.
1619 { return this->type_; }
1621 // Return the section flags.
1624 { return this->flags_; }
1626 // Return the entsize field.
1629 { return this->entsize_; }
1631 // Set the entsize field.
1633 set_entsize(uint64_t v);
1635 // Set the load address.
1637 set_load_address(uint64_t load_address)
1639 this->load_address_ = load_address;
1640 this->has_load_address_ = true;
1643 // Set the link field to the output section index of a section.
1645 set_link_section(const Output_data* od)
1647 gold_assert(this->link_ == 0
1648 && !this->should_link_to_symtab_
1649 && !this->should_link_to_dynsym_);
1650 this->link_section_ = od;
1653 // Set the link field to a constant.
1655 set_link(unsigned int v)
1657 gold_assert(this->link_section_ == NULL
1658 && !this->should_link_to_symtab_
1659 && !this->should_link_to_dynsym_);
1663 // Record that this section should link to the normal symbol table.
1665 set_should_link_to_symtab()
1667 gold_assert(this->link_section_ == NULL
1669 && !this->should_link_to_dynsym_);
1670 this->should_link_to_symtab_ = true;
1673 // Record that this section should link to the dynamic symbol table.
1675 set_should_link_to_dynsym()
1677 gold_assert(this->link_section_ == NULL
1679 && !this->should_link_to_symtab_);
1680 this->should_link_to_dynsym_ = true;
1683 // Return the info field.
1687 gold_assert(this->info_section_ == NULL
1688 && this->info_symndx_ == NULL);
1692 // Set the info field to the output section index of a section.
1694 set_info_section(const Output_section* os)
1696 gold_assert((this->info_section_ == NULL
1697 || (this->info_section_ == os
1698 && this->info_uses_section_index_))
1699 && this->info_symndx_ == NULL
1700 && this->info_ == 0);
1701 this->info_section_ = os;
1702 this->info_uses_section_index_= true;
1705 // Set the info field to the symbol table index of a symbol.
1707 set_info_symndx(const Symbol* sym)
1709 gold_assert(this->info_section_ == NULL
1710 && (this->info_symndx_ == NULL
1711 || this->info_symndx_ == sym)
1712 && this->info_ == 0);
1713 this->info_symndx_ = sym;
1716 // Set the info field to the symbol table index of a section symbol.
1718 set_info_section_symndx(const Output_section* os)
1720 gold_assert((this->info_section_ == NULL
1721 || (this->info_section_ == os
1722 && !this->info_uses_section_index_))
1723 && this->info_symndx_ == NULL
1724 && this->info_ == 0);
1725 this->info_section_ = os;
1726 this->info_uses_section_index_ = false;
1729 // Set the info field to a constant.
1731 set_info(unsigned int v)
1733 gold_assert(this->info_section_ == NULL
1734 && this->info_symndx_ == NULL
1735 && (this->info_ == 0
1736 || this->info_ == v));
1740 // Set the addralign field.
1742 set_addralign(uint64_t v)
1743 { this->addralign_ = v; }
1745 // Indicate that we need a symtab index.
1747 set_needs_symtab_index()
1748 { this->needs_symtab_index_ = true; }
1750 // Return whether we need a symtab index.
1752 needs_symtab_index() const
1753 { return this->needs_symtab_index_; }
1755 // Get the symtab index.
1757 symtab_index() const
1759 gold_assert(this->symtab_index_ != 0);
1760 return this->symtab_index_;
1763 // Set the symtab index.
1765 set_symtab_index(unsigned int index)
1767 gold_assert(index != 0);
1768 this->symtab_index_ = index;
1771 // Indicate that we need a dynsym index.
1773 set_needs_dynsym_index()
1774 { this->needs_dynsym_index_ = true; }
1776 // Return whether we need a dynsym index.
1778 needs_dynsym_index() const
1779 { return this->needs_dynsym_index_; }
1781 // Get the dynsym index.
1783 dynsym_index() const
1785 gold_assert(this->dynsym_index_ != 0);
1786 return this->dynsym_index_;
1789 // Set the dynsym index.
1791 set_dynsym_index(unsigned int index)
1793 gold_assert(index != 0);
1794 this->dynsym_index_ = index;
1797 // Return whether this section should be written after all the input
1798 // sections are complete.
1800 after_input_sections() const
1801 { return this->after_input_sections_; }
1803 // Record that this section should be written after all the input
1804 // sections are complete.
1806 set_after_input_sections()
1807 { this->after_input_sections_ = true; }
1809 // Return whether this section requires postprocessing after all
1810 // relocations have been applied.
1812 requires_postprocessing() const
1813 { return this->requires_postprocessing_; }
1815 // If a section requires postprocessing, return the buffer to use.
1817 postprocessing_buffer() const
1819 gold_assert(this->postprocessing_buffer_ != NULL);
1820 return this->postprocessing_buffer_;
1823 // If a section requires postprocessing, create the buffer to use.
1825 create_postprocessing_buffer();
1827 // If a section requires postprocessing, this is the size of the
1828 // buffer to which relocations should be applied.
1830 postprocessing_buffer_size() const
1831 { return this->current_data_size_for_child(); }
1833 // Modify the section name. This is only permitted for an
1834 // unallocated section, and only before the size has been finalized.
1835 // Otherwise the name will not get into Layout::namepool_.
1837 set_name(const char* newname)
1839 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1840 gold_assert(!this->is_data_size_valid());
1841 this->name_ = newname;
1844 // Return whether the offset OFFSET in the input section SHNDX in
1845 // object OBJECT is being included in the link.
1847 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1848 off_t offset) const;
1850 // Return the offset within the output section of OFFSET relative to
1851 // the start of input section SHNDX in object OBJECT.
1853 output_offset(const Relobj* object, unsigned int shndx,
1854 section_offset_type offset) const;
1856 // Return the output virtual address of OFFSET relative to the start
1857 // of input section SHNDX in object OBJECT.
1859 output_address(const Relobj* object, unsigned int shndx,
1860 off_t offset) const;
1862 // Return the output address of the start of the merged section for
1863 // input section SHNDX in object OBJECT. This is not necessarily
1864 // the offset corresponding to input offset 0 in the section, since
1865 // the section may be mapped arbitrarily.
1867 starting_output_address(const Relobj* object, unsigned int shndx) const;
1869 // Record that this output section was found in the SECTIONS clause
1870 // of a linker script.
1872 set_found_in_sections_clause()
1873 { this->found_in_sections_clause_ = true; }
1875 // Return whether this output section was found in the SECTIONS
1876 // clause of a linker script.
1878 found_in_sections_clause() const
1879 { return this->found_in_sections_clause_; }
1881 // Write the section header into *OPHDR.
1882 template<int size, bool big_endian>
1884 write_header(const Layout*, const Stringpool*,
1885 elfcpp::Shdr_write<size, big_endian>*) const;
1887 // The next few calls are for linker script support.
1889 // Store the list of input sections for this Output_section into the
1890 // list passed in. This removes the input sections, leaving only
1891 // any Output_section_data elements. This returns the size of those
1892 // Output_section_data elements. ADDRESS is the address of this
1893 // output section. FILL is the fill value to use, in case there are
1894 // any spaces between the remaining Output_section_data elements.
1896 get_input_sections(uint64_t address, const std::string& fill,
1897 std::list<std::pair<Relobj*, unsigned int > >*);
1899 // Add an input section from a script.
1901 add_input_section_for_script(Relobj* object, unsigned int shndx,
1902 off_t data_size, uint64_t addralign);
1904 // Set the current size of the output section.
1906 set_current_data_size(off_t size)
1907 { this->set_current_data_size_for_child(size); }
1909 // Get the current size of the output section.
1911 current_data_size() const
1912 { return this->current_data_size_for_child(); }
1914 // End of linker script support.
1916 // Print merge statistics to stderr.
1918 print_merge_stats();
1921 // Return the output section--i.e., the object itself.
1926 // Return the section index in the output file.
1928 do_out_shndx() const
1930 gold_assert(this->out_shndx_ != -1U);
1931 return this->out_shndx_;
1934 // Set the output section index.
1936 do_set_out_shndx(unsigned int shndx)
1938 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
1939 this->out_shndx_ = shndx;
1942 // Set the final data size of the Output_section. For a typical
1943 // Output_section, there is nothing to do, but if there are any
1944 // Output_section_data objects we need to set their final addresses
1947 set_final_data_size();
1949 // Reset the address and file offset.
1951 do_reset_address_and_file_offset();
1953 // Write the data to the file. For a typical Output_section, this
1954 // does nothing: the data is written out by calling Object::Relocate
1955 // on each input object. But if there are any Output_section_data
1956 // objects we do need to write them out here.
1958 do_write(Output_file*);
1960 // Return the address alignment--function required by parent class.
1962 do_addralign() const
1963 { return this->addralign_; }
1965 // Return whether there is a load address.
1967 do_has_load_address() const
1968 { return this->has_load_address_; }
1970 // Return the load address.
1972 do_load_address() const
1974 gold_assert(this->has_load_address_);
1975 return this->load_address_;
1978 // Return whether this is an Output_section.
1980 do_is_section() const
1983 // Return whether this is a section of the specified type.
1985 do_is_section_type(elfcpp::Elf_Word type) const
1986 { return this->type_ == type; }
1988 // Return whether the specified section flag is set.
1990 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1991 { return (this->flags_ & flag) != 0; }
1993 // Set the TLS offset. Called only for SHT_TLS sections.
1995 do_set_tls_offset(uint64_t tls_base);
1997 // Return the TLS offset, relative to the base of the TLS segment.
1998 // Valid only for SHT_TLS sections.
2000 do_tls_offset() const
2001 { return this->tls_offset_; }
2003 // This may be implemented by a child class.
2005 do_finalize_name(Layout*)
2008 // Record that this section requires postprocessing after all
2009 // relocations have been applied. This is called by a child class.
2011 set_requires_postprocessing()
2013 this->requires_postprocessing_ = true;
2014 this->after_input_sections_ = true;
2017 // Write all the data of an Output_section into the postprocessing
2020 write_to_postprocessing_buffer();
2023 // In some cases we need to keep a list of the input sections
2024 // associated with this output section. We only need the list if we
2025 // might have to change the offsets of the input section within the
2026 // output section after we add the input section. The ordinary
2027 // input sections will be written out when we process the object
2028 // file, and as such we don't need to track them here. We do need
2029 // to track Output_section_data objects here. We store instances of
2030 // this structure in a std::vector, so it must be a POD. There can
2031 // be many instances of this structure, so we use a union to save
2037 : shndx_(0), p2align_(0)
2039 this->u1_.data_size = 0;
2040 this->u2_.object = NULL;
2043 // For an ordinary input section.
2044 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2047 p2align_(ffsll(static_cast<long long>(addralign)))
2049 gold_assert(shndx != OUTPUT_SECTION_CODE
2050 && shndx != MERGE_DATA_SECTION_CODE
2051 && shndx != MERGE_STRING_SECTION_CODE);
2052 this->u1_.data_size = data_size;
2053 this->u2_.object = object;
2056 // For a non-merge output section.
2057 Input_section(Output_section_data* posd)
2058 : shndx_(OUTPUT_SECTION_CODE),
2059 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2061 this->u1_.data_size = 0;
2062 this->u2_.posd = posd;
2065 // For a merge section.
2066 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2068 ? MERGE_STRING_SECTION_CODE
2069 : MERGE_DATA_SECTION_CODE),
2070 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2072 this->u1_.entsize = entsize;
2073 this->u2_.posd = posd;
2076 // The required alignment.
2080 return (this->p2align_ == 0
2082 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2085 // Return the required size.
2089 // Whether this is an input section.
2091 is_input_section() const
2093 return (this->shndx_ != OUTPUT_SECTION_CODE
2094 && this->shndx_ != MERGE_DATA_SECTION_CODE
2095 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2098 // Return whether this is a merge section which matches the
2101 is_merge_section(bool is_string, uint64_t entsize,
2102 uint64_t addralign) const
2104 return (this->shndx_ == (is_string
2105 ? MERGE_STRING_SECTION_CODE
2106 : MERGE_DATA_SECTION_CODE)
2107 && this->u1_.entsize == entsize
2108 && this->addralign() == addralign);
2111 // Return the object for an input section.
2115 gold_assert(this->is_input_section());
2116 return this->u2_.object;
2119 // Return the input section index for an input section.
2123 gold_assert(this->is_input_section());
2124 return this->shndx_;
2127 // Set the output section.
2129 set_output_section(Output_section* os)
2131 gold_assert(!this->is_input_section());
2132 this->u2_.posd->set_output_section(os);
2135 // Set the address and file offset. This is called during
2136 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2137 // the enclosing section.
2139 set_address_and_file_offset(uint64_t address, off_t file_offset,
2140 off_t section_file_offset);
2142 // Reset the address and file offset.
2144 reset_address_and_file_offset();
2146 // Finalize the data size.
2148 finalize_data_size();
2150 // Add an input section, for SHF_MERGE sections.
2152 add_input_section(Relobj* object, unsigned int shndx)
2154 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2155 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2156 return this->u2_.posd->add_input_section(object, shndx);
2159 // Given an input OBJECT, an input section index SHNDX within that
2160 // object, and an OFFSET relative to the start of that input
2161 // section, return whether or not the output offset is known. If
2162 // this function returns true, it sets *POUTPUT to the offset in
2163 // the output section, relative to the start of the input section
2164 // in the output section. *POUTPUT may be different from OFFSET
2165 // for a merged section.
2167 output_offset(const Relobj* object, unsigned int shndx,
2168 section_offset_type offset,
2169 section_offset_type *poutput) const;
2171 // Return whether this is the merge section for the input section
2174 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2176 // Write out the data. This does nothing for an input section.
2178 write(Output_file*);
2180 // Write the data to a buffer. This does nothing for an input
2183 write_to_buffer(unsigned char*);
2185 // Print statistics about merge sections to stderr.
2187 print_merge_stats(const char* section_name)
2189 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2190 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2191 this->u2_.posd->print_merge_stats(section_name);
2195 // Code values which appear in shndx_. If the value is not one of
2196 // these codes, it is the input section index in the object file.
2199 // An Output_section_data.
2200 OUTPUT_SECTION_CODE = -1U,
2201 // An Output_section_data for an SHF_MERGE section with
2202 // SHF_STRINGS not set.
2203 MERGE_DATA_SECTION_CODE = -2U,
2204 // An Output_section_data for an SHF_MERGE section with
2206 MERGE_STRING_SECTION_CODE = -3U
2209 // For an ordinary input section, this is the section index in the
2210 // input file. For an Output_section_data, this is
2211 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2212 // MERGE_STRING_SECTION_CODE.
2213 unsigned int shndx_;
2214 // The required alignment, stored as a power of 2.
2215 unsigned int p2align_;
2218 // For an ordinary input section, the section size.
2220 // For OUTPUT_SECTION_CODE, this is not used. For
2221 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2227 // For an ordinary input section, the object which holds the
2230 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2231 // MERGE_STRING_SECTION_CODE, the data.
2232 Output_section_data* posd;
2236 typedef std::vector<Input_section> Input_section_list;
2238 // Fill data. This is used to fill in data between input sections.
2239 // It is also used for data statements (BYTE, WORD, etc.) in linker
2240 // scripts. When we have to keep track of the input sections, we
2241 // can use an Output_data_const, but we don't want to have to keep
2242 // track of input sections just to implement fills.
2246 Fill(off_t section_offset, off_t length)
2247 : section_offset_(section_offset),
2248 length_(convert_to_section_size_type(length))
2251 // Return section offset.
2253 section_offset() const
2254 { return this->section_offset_; }
2256 // Return fill length.
2259 { return this->length_; }
2262 // The offset within the output section.
2263 off_t section_offset_;
2264 // The length of the space to fill.
2265 section_size_type length_;
2268 typedef std::vector<Fill> Fill_list;
2270 // Add a new output section by Input_section.
2272 add_output_section_data(Input_section*);
2274 // Add an SHF_MERGE input section. Returns true if the section was
2277 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2278 uint64_t entsize, uint64_t addralign);
2280 // Add an output SHF_MERGE section POSD to this output section.
2281 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2282 // ENTSIZE is the entity size. This returns the entry added to
2285 add_output_merge_section(Output_section_data* posd, bool is_string,
2288 // Most of these fields are only valid after layout.
2290 // The name of the section. This will point into a Stringpool.
2292 // The section address is in the parent class.
2293 // The section alignment.
2294 uint64_t addralign_;
2295 // The section entry size.
2297 // The load address. This is only used when using a linker script
2298 // with a SECTIONS clause. The has_load_address_ field indicates
2299 // whether this field is valid.
2300 uint64_t load_address_;
2301 // The file offset is in the parent class.
2302 // Set the section link field to the index of this section.
2303 const Output_data* link_section_;
2304 // If link_section_ is NULL, this is the link field.
2306 // Set the section info field to the index of this section.
2307 const Output_section* info_section_;
2308 // If info_section_ is NULL, set the info field to the symbol table
2309 // index of this symbol.
2310 const Symbol* info_symndx_;
2311 // If info_section_ and info_symndx_ are NULL, this is the section
2314 // The section type.
2315 const elfcpp::Elf_Word type_;
2316 // The section flags.
2317 elfcpp::Elf_Xword flags_;
2318 // The section index.
2319 unsigned int out_shndx_;
2320 // If there is a STT_SECTION for this output section in the normal
2321 // symbol table, this is the symbol index. This starts out as zero.
2322 // It is initialized in Layout::finalize() to be the index, or -1U
2323 // if there isn't one.
2324 unsigned int symtab_index_;
2325 // If there is a STT_SECTION for this output section in the dynamic
2326 // symbol table, this is the symbol index. This starts out as zero.
2327 // It is initialized in Layout::finalize() to be the index, or -1U
2328 // if there isn't one.
2329 unsigned int dynsym_index_;
2330 // The input sections. This will be empty in cases where we don't
2331 // need to keep track of them.
2332 Input_section_list input_sections_;
2333 // The offset of the first entry in input_sections_.
2334 off_t first_input_offset_;
2335 // The fill data. This is separate from input_sections_ because we
2336 // often will need fill sections without needing to keep track of
2339 // If the section requires postprocessing, this buffer holds the
2340 // section contents during relocation.
2341 unsigned char* postprocessing_buffer_;
2342 // Whether this output section needs a STT_SECTION symbol in the
2343 // normal symbol table. This will be true if there is a relocation
2345 bool needs_symtab_index_ : 1;
2346 // Whether this output section needs a STT_SECTION symbol in the
2347 // dynamic symbol table. This will be true if there is a dynamic
2348 // relocation which needs it.
2349 bool needs_dynsym_index_ : 1;
2350 // Whether the link field of this output section should point to the
2351 // normal symbol table.
2352 bool should_link_to_symtab_ : 1;
2353 // Whether the link field of this output section should point to the
2354 // dynamic symbol table.
2355 bool should_link_to_dynsym_ : 1;
2356 // Whether this section should be written after all the input
2357 // sections are complete.
2358 bool after_input_sections_ : 1;
2359 // Whether this section requires post processing after all
2360 // relocations have been applied.
2361 bool requires_postprocessing_ : 1;
2362 // Whether an input section was mapped to this output section
2363 // because of a SECTIONS clause in a linker script.
2364 bool found_in_sections_clause_ : 1;
2365 // Whether this section has an explicitly specified load address.
2366 bool has_load_address_ : 1;
2367 // True if the info_section_ field means the section index of the
2368 // section, false if it means the symbol index of the corresponding
2370 bool info_uses_section_index_ : 1;
2371 // For SHT_TLS sections, the offset of this section relative to the base
2372 // of the TLS segment.
2373 uint64_t tls_offset_;
2376 // An output segment. PT_LOAD segments are built from collections of
2377 // output sections. Other segments typically point within PT_LOAD
2378 // segments, and are built directly as needed.
2380 class Output_segment
2383 // Create an output segment, specifying the type and flags.
2384 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2386 // Return the virtual address.
2389 { return this->vaddr_; }
2391 // Return the physical address.
2394 { return this->paddr_; }
2396 // Return the segment type.
2399 { return this->type_; }
2401 // Return the segment flags.
2404 { return this->flags_; }
2406 // Return the memory size.
2409 { return this->memsz_; }
2411 // Return the file size.
2414 { return this->filesz_; }
2416 // Return the file offset.
2419 { return this->offset_; }
2421 // Return the maximum alignment of the Output_data.
2423 maximum_alignment();
2425 // Add an Output_section to this segment.
2427 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2428 { this->add_output_section(os, seg_flags, false); }
2430 // Add an Output_section to the start of this segment.
2432 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2433 { this->add_output_section(os, seg_flags, true); }
2435 // Add an Output_data (which is not an Output_section) to the start
2438 add_initial_output_data(Output_data*);
2440 // Return the number of dynamic relocations applied to this segment.
2442 dynamic_reloc_count() const;
2444 // Return the address of the first section.
2446 first_section_load_address() const;
2448 // Return whether the addresses have been set already.
2450 are_addresses_set() const
2451 { return this->are_addresses_set_; }
2453 // Set the addresses.
2455 set_addresses(uint64_t vaddr, uint64_t paddr)
2457 this->vaddr_ = vaddr;
2458 this->paddr_ = paddr;
2459 this->are_addresses_set_ = true;
2462 // Set the segment flags. This is only used if we have a PHDRS
2463 // clause which explicitly specifies the flags.
2465 set_flags(elfcpp::Elf_Word flags)
2466 { this->flags_ = flags; }
2468 // Set the address of the segment to ADDR and the offset to *POFF
2469 // and set the addresses and offsets of all contained output
2470 // sections accordingly. Set the section indexes of all contained
2471 // output sections starting with *PSHNDX. If RESET is true, first
2472 // reset the addresses of the contained sections. Return the
2473 // address of the immediately following segment. Update *POFF and
2474 // *PSHNDX. This should only be called for a PT_LOAD segment.
2476 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2477 unsigned int* pshndx);
2479 // Set the minimum alignment of this segment. This may be adjusted
2480 // upward based on the section alignments.
2482 set_minimum_p_align(uint64_t align)
2483 { this->min_p_align_ = align; }
2485 // Set the offset of this segment based on the section. This should
2486 // only be called for a non-PT_LOAD segment.
2490 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2494 // Return the number of output sections.
2496 output_section_count() const;
2498 // Return the section attached to the list segment with the lowest
2499 // load address. This is used when handling a PHDRS clause in a
2502 section_with_lowest_load_address() const;
2504 // Write the segment header into *OPHDR.
2505 template<int size, bool big_endian>
2507 write_header(elfcpp::Phdr_write<size, big_endian>*);
2509 // Write the section headers of associated sections into V.
2510 template<int size, bool big_endian>
2512 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2513 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2516 Output_segment(const Output_segment&);
2517 Output_segment& operator=(const Output_segment&);
2519 typedef std::list<Output_data*> Output_data_list;
2521 // Add an Output_section to this segment, specifying front or back.
2523 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2526 // Find the maximum alignment in an Output_data_list.
2528 maximum_alignment_list(const Output_data_list*);
2530 // Set the section addresses in an Output_data_list.
2532 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2533 off_t* poff, unsigned int* pshndx);
2535 // Return the number of Output_sections in an Output_data_list.
2537 output_section_count_list(const Output_data_list*) const;
2539 // Return the number of dynamic relocs in an Output_data_list.
2541 dynamic_reloc_count_list(const Output_data_list*) const;
2543 // Find the section with the lowest load address in an
2544 // Output_data_list.
2546 lowest_load_address_in_list(const Output_data_list* pdl,
2547 Output_section** found,
2548 uint64_t* found_lma) const;
2550 // Write the section headers in the list into V.
2551 template<int size, bool big_endian>
2553 write_section_headers_list(const Layout*, const Stringpool*,
2554 const Output_data_list*, unsigned char* v,
2555 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2557 // The list of output data with contents attached to this segment.
2558 Output_data_list output_data_;
2559 // The list of output data without contents attached to this segment.
2560 Output_data_list output_bss_;
2561 // The segment virtual address.
2563 // The segment physical address.
2565 // The size of the segment in memory.
2567 // The maximum section alignment. The is_max_align_known_ field
2568 // indicates whether this has been finalized.
2569 uint64_t max_align_;
2570 // The required minimum value for the p_align field. This is used
2571 // for PT_LOAD segments. Note that this does not mean that
2572 // addresses should be aligned to this value; it means the p_paddr
2573 // and p_vaddr fields must be congruent modulo this value. For
2574 // non-PT_LOAD segments, the dynamic linker works more efficiently
2575 // if the p_align field has the more conventional value, although it
2576 // can align as needed.
2577 uint64_t min_p_align_;
2578 // The offset of the segment data within the file.
2580 // The size of the segment data in the file.
2582 // The segment type;
2583 elfcpp::Elf_Word type_;
2584 // The segment flags.
2585 elfcpp::Elf_Word flags_;
2586 // Whether we have finalized max_align_.
2587 bool is_max_align_known_ : 1;
2588 // Whether vaddr and paddr were set by a linker script.
2589 bool are_addresses_set_ : 1;
2592 // This class represents the output file.
2597 Output_file(const char* name);
2599 // Indicate that this is a temporary file which should not be
2603 { this->is_temporary_ = true; }
2605 // Open the output file. FILE_SIZE is the final size of the file.
2607 open(off_t file_size);
2609 // Resize the output file.
2611 resize(off_t file_size);
2613 // Close the output file (flushing all buffered data) and make sure
2614 // there are no errors.
2618 // We currently always use mmap which makes the view handling quite
2619 // simple. In the future we may support other approaches.
2621 // Write data to the output file.
2623 write(off_t offset, const void* data, size_t len)
2624 { memcpy(this->base_ + offset, data, len); }
2626 // Get a buffer to use to write to the file, given the offset into
2627 // the file and the size.
2629 get_output_view(off_t start, size_t size)
2631 gold_assert(start >= 0
2632 && start + static_cast<off_t>(size) <= this->file_size_);
2633 return this->base_ + start;
2636 // VIEW must have been returned by get_output_view. Write the
2637 // buffer to the file, passing in the offset and the size.
2639 write_output_view(off_t, size_t, unsigned char*)
2642 // Get a read/write buffer. This is used when we want to write part
2643 // of the file, read it in, and write it again.
2645 get_input_output_view(off_t start, size_t size)
2646 { return this->get_output_view(start, size); }
2648 // Write a read/write buffer back to the file.
2650 write_input_output_view(off_t, size_t, unsigned char*)
2653 // Get a read buffer. This is used when we just want to read part
2654 // of the file back it in.
2655 const unsigned char*
2656 get_input_view(off_t start, size_t size)
2657 { return this->get_output_view(start, size); }
2659 // Release a read bfufer.
2661 free_input_view(off_t, size_t, const unsigned char*)
2665 // Map the file into memory and return a pointer to the map.
2669 // Unmap the file from memory (and flush to disk buffers).
2679 // Base of file mapped into memory.
2680 unsigned char* base_;
2681 // True iff base_ points to a memory buffer rather than an output file.
2682 bool map_is_anonymous_;
2683 // True if this is a temporary file which should not be output.
2687 } // End namespace gold.
2689 #endif // !defined(GOLD_OUTPUT_H)