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 index, if there is an output section.
137 { return this->do_out_shndx(); }
139 // Set the output section index, if this is an output section.
141 set_out_shndx(unsigned int shndx)
142 { this->do_set_out_shndx(shndx); }
144 // Set the address and file offset of this data, and finalize the
145 // size of the data. This is called during Layout::finalize for
146 // allocated sections.
148 set_address_and_file_offset(uint64_t addr, off_t off)
150 this->set_address(addr);
151 this->set_file_offset(off);
152 this->finalize_data_size();
157 set_address(uint64_t addr)
159 gold_assert(!this->is_address_valid_);
160 this->address_ = addr;
161 this->is_address_valid_ = true;
164 // Set the file offset.
166 set_file_offset(off_t off)
168 gold_assert(!this->is_offset_valid_);
170 this->is_offset_valid_ = true;
173 // Finalize the data size.
177 if (!this->is_data_size_valid_)
179 // Tell the child class to set the data size.
180 this->set_final_data_size();
181 gold_assert(this->is_data_size_valid_);
185 // Set the TLS offset. Called only for SHT_TLS sections.
187 set_tls_offset(uint64_t tls_base)
188 { this->do_set_tls_offset(tls_base); }
190 // Return the TLS offset, relative to the base of the TLS segment.
191 // Valid only for SHT_TLS sections.
194 { return this->do_tls_offset(); }
196 // Write the data to the output file. This is called after
197 // Layout::finalize is complete.
199 write(Output_file* file)
200 { this->do_write(file); }
202 // This is called by Layout::finalize to note that the sizes of
203 // allocated sections must now be fixed.
206 { Output_data::allocated_sizes_are_fixed = true; }
208 // Used to check that layout has been done.
211 { return Output_data::allocated_sizes_are_fixed; }
213 // Count the number of dynamic relocations applied to this section.
216 { ++this->dynamic_reloc_count_; }
218 // Return the number of dynamic relocations applied to this section.
220 dynamic_reloc_count() const
221 { return this->dynamic_reloc_count_; }
223 // Whether the address is valid.
225 is_address_valid() const
226 { return this->is_address_valid_; }
228 // Whether the file offset is valid.
230 is_offset_valid() const
231 { return this->is_offset_valid_; }
233 // Whether the data size is valid.
235 is_data_size_valid() const
236 { return this->is_data_size_valid_; }
239 // Functions that child classes may or in some cases must implement.
241 // Write the data to the output file.
243 do_write(Output_file*) = 0;
245 // Return the required alignment.
247 do_addralign() const = 0;
249 // Return whether this has a load address.
251 do_has_load_address() const
254 // Return the load address.
256 do_load_address() const
257 { gold_unreachable(); }
259 // Return whether this is an Output_section.
261 do_is_section() const
264 // Return whether this is an Output_section of the specified type.
265 // This only needs to be implement by Output_section.
267 do_is_section_type(elfcpp::Elf_Word) const
270 // Return whether this is an Output_section with the specific flag
271 // set. This only needs to be implemented by Output_section.
273 do_is_section_flag_set(elfcpp::Elf_Xword) const
276 // Return the output section index, if there is an output section.
279 { gold_unreachable(); }
281 // Set the output section index, if this is an output section.
283 do_set_out_shndx(unsigned int)
284 { gold_unreachable(); }
286 // This is a hook for derived classes to set the data size. This is
287 // called by finalize_data_size, normally called during
288 // Layout::finalize, when the section address is set.
290 set_final_data_size()
291 { gold_unreachable(); }
293 // A hook for resetting the address and file offset.
295 do_reset_address_and_file_offset()
298 // Set the TLS offset. Called only for SHT_TLS sections.
300 do_set_tls_offset(uint64_t)
301 { gold_unreachable(); }
303 // Return the TLS offset, relative to the base of the TLS segment.
304 // Valid only for SHT_TLS sections.
306 do_tls_offset() const
307 { gold_unreachable(); }
309 // Functions that child classes may call.
311 // Set the size of the data.
313 set_data_size(off_t data_size)
315 gold_assert(!this->is_data_size_valid_);
316 this->data_size_ = data_size;
317 this->is_data_size_valid_ = true;
320 // Get the current data size--this is for the convenience of
321 // sections which build up their size over time.
323 current_data_size_for_child() const
324 { return this->data_size_; }
326 // Set the current data size--this is for the convenience of
327 // sections which build up their size over time.
329 set_current_data_size_for_child(off_t data_size)
331 gold_assert(!this->is_data_size_valid_);
332 this->data_size_ = data_size;
335 // Return default alignment for the target size.
339 // Return default alignment for a specified size--32 or 64.
341 default_alignment_for_size(int size);
344 Output_data(const Output_data&);
345 Output_data& operator=(const Output_data&);
347 // This is used for verification, to make sure that we don't try to
348 // change any sizes of allocated sections after we set the section
350 static bool allocated_sizes_are_fixed;
352 // Memory address in output file.
354 // Size of data in output file.
356 // File offset of contents in output file.
358 // Whether address_ is valid.
359 bool is_address_valid_;
360 // Whether data_size_ is valid.
361 bool is_data_size_valid_;
362 // Whether offset_ is valid.
363 bool is_offset_valid_;
364 // Count of dynamic relocations applied to this section.
365 unsigned int dynamic_reloc_count_;
368 // Output the section headers.
370 class Output_section_headers : public Output_data
373 Output_section_headers(const Layout*,
374 const Layout::Segment_list*,
375 const Layout::Section_list*,
376 const Layout::Section_list*,
380 // Write the data to the file.
382 do_write(Output_file*);
384 // Return the required alignment.
387 { return Output_data::default_alignment(); }
390 // Write the data to the file with the right size and endianness.
391 template<int size, bool big_endian>
393 do_sized_write(Output_file*);
395 const Layout* layout_;
396 const Layout::Segment_list* segment_list_;
397 const Layout::Section_list* section_list_;
398 const Layout::Section_list* unattached_section_list_;
399 const Stringpool* secnamepool_;
402 // Output the segment headers.
404 class Output_segment_headers : public Output_data
407 Output_segment_headers(const Layout::Segment_list& segment_list);
410 // Write the data to the file.
412 do_write(Output_file*);
414 // Return the required alignment.
417 { return Output_data::default_alignment(); }
420 // Write the data to the file with the right size and endianness.
421 template<int size, bool big_endian>
423 do_sized_write(Output_file*);
425 const Layout::Segment_list& segment_list_;
428 // Output the ELF file header.
430 class Output_file_header : public Output_data
433 Output_file_header(const Target*,
435 const Output_segment_headers*,
438 // Add information about the section headers. We lay out the ELF
439 // file header before we create the section headers.
440 void set_section_info(const Output_section_headers*,
441 const Output_section* shstrtab);
444 // Write the data to the file.
446 do_write(Output_file*);
448 // Return the required alignment.
451 { return Output_data::default_alignment(); }
454 // Write the data to the file with the right size and endianness.
455 template<int size, bool big_endian>
457 do_sized_write(Output_file*);
459 // Return the value to use for the entry address.
461 typename elfcpp::Elf_types<size>::Elf_Addr
464 const Target* target_;
465 const Symbol_table* symtab_;
466 const Output_segment_headers* segment_header_;
467 const Output_section_headers* section_header_;
468 const Output_section* shstrtab_;
472 // Output sections are mainly comprised of input sections. However,
473 // there are cases where we have data to write out which is not in an
474 // input section. Output_section_data is used in such cases. This is
475 // an abstract base class.
477 class Output_section_data : public Output_data
480 Output_section_data(off_t data_size, uint64_t addralign)
481 : Output_data(), output_section_(NULL), addralign_(addralign)
482 { this->set_data_size(data_size); }
484 Output_section_data(uint64_t addralign)
485 : Output_data(), output_section_(NULL), addralign_(addralign)
488 // Return the output section.
489 const Output_section*
490 output_section() const
491 { return this->output_section_; }
493 // Record the output section.
495 set_output_section(Output_section* os);
497 // Add an input section, for SHF_MERGE sections. This returns true
498 // if the section was handled.
500 add_input_section(Relobj* object, unsigned int shndx)
501 { return this->do_add_input_section(object, shndx); }
503 // Given an input OBJECT, an input section index SHNDX within that
504 // object, and an OFFSET relative to the start of that input
505 // section, return whether or not the corresponding offset within
506 // the output section is known. If this function returns true, it
507 // sets *POUTPUT to the output offset. The value -1 indicates that
508 // this input offset is being discarded.
510 output_offset(const Relobj* object, unsigned int shndx,
511 section_offset_type offset,
512 section_offset_type *poutput) const
513 { return this->do_output_offset(object, shndx, offset, poutput); }
515 // Return whether this is the merge section for the input section
516 // SHNDX in OBJECT. This should return true when output_offset
517 // would return true for some values of OFFSET.
519 is_merge_section_for(const Relobj* object, unsigned int shndx) const
520 { return this->do_is_merge_section_for(object, shndx); }
522 // Write the contents to a buffer. This is used for sections which
523 // require postprocessing, such as compression.
525 write_to_buffer(unsigned char* buffer)
526 { this->do_write_to_buffer(buffer); }
528 // Print merge stats to stderr. This should only be called for
529 // SHF_MERGE sections.
531 print_merge_stats(const char* section_name)
532 { this->do_print_merge_stats(section_name); }
535 // The child class must implement do_write.
537 // The child class may implement specific adjustments to the output
540 do_adjust_output_section(Output_section*)
543 // May be implemented by child class. Return true if the section
546 do_add_input_section(Relobj*, unsigned int)
547 { gold_unreachable(); }
549 // The child class may implement output_offset.
551 do_output_offset(const Relobj*, unsigned int, section_offset_type,
552 section_offset_type*) const
555 // The child class may implement is_merge_section_for.
557 do_is_merge_section_for(const Relobj*, unsigned int) const
560 // The child class may implement write_to_buffer. Most child
561 // classes can not appear in a compressed section, and they do not
564 do_write_to_buffer(unsigned char*)
565 { gold_unreachable(); }
567 // Print merge statistics.
569 do_print_merge_stats(const char*)
570 { gold_unreachable(); }
572 // Return the required alignment.
575 { return this->addralign_; }
577 // Return the section index of the output section.
579 do_out_shndx() const;
581 // Set the alignment.
583 set_addralign(uint64_t addralign)
584 { this->addralign_ = addralign; }
587 // The output section for this section.
588 const Output_section* output_section_;
589 // The required alignment.
593 // Some Output_section_data classes build up their data step by step,
594 // rather than all at once. This class provides an interface for
597 class Output_section_data_build : public Output_section_data
600 Output_section_data_build(uint64_t addralign)
601 : Output_section_data(addralign)
604 // Get the current data size.
606 current_data_size() const
607 { return this->current_data_size_for_child(); }
609 // Set the current data size.
611 set_current_data_size(off_t data_size)
612 { this->set_current_data_size_for_child(data_size); }
615 // Set the final data size.
617 set_final_data_size()
618 { this->set_data_size(this->current_data_size_for_child()); }
621 // A simple case of Output_data in which we have constant data to
624 class Output_data_const : public Output_section_data
627 Output_data_const(const std::string& data, uint64_t addralign)
628 : Output_section_data(data.size(), addralign), data_(data)
631 Output_data_const(const char* p, off_t len, uint64_t addralign)
632 : Output_section_data(len, addralign), data_(p, len)
635 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
636 : Output_section_data(len, addralign),
637 data_(reinterpret_cast<const char*>(p), len)
641 // Write the data to the output file.
643 do_write(Output_file*);
645 // Write the data to a buffer.
647 do_write_to_buffer(unsigned char* buffer)
648 { memcpy(buffer, this->data_.data(), this->data_.size()); }
654 // Another version of Output_data with constant data, in which the
655 // buffer is allocated by the caller.
657 class Output_data_const_buffer : public Output_section_data
660 Output_data_const_buffer(const unsigned char* p, off_t len,
662 : Output_section_data(len, addralign), p_(p)
666 // Write the data the output file.
668 do_write(Output_file*);
670 // Write the data to a buffer.
672 do_write_to_buffer(unsigned char* buffer)
673 { memcpy(buffer, this->p_, this->data_size()); }
676 const unsigned char* p_;
679 // A place holder for a fixed amount of data written out via some
682 class Output_data_fixed_space : public Output_section_data
685 Output_data_fixed_space(off_t data_size, uint64_t addralign)
686 : Output_section_data(data_size, addralign)
690 // Write out the data--the actual data must be written out
693 do_write(Output_file*)
697 // A place holder for variable sized data written out via some other
700 class Output_data_space : public Output_section_data_build
703 explicit Output_data_space(uint64_t addralign)
704 : Output_section_data_build(addralign)
707 // Set the alignment.
709 set_space_alignment(uint64_t align)
710 { this->set_addralign(align); }
713 // Write out the data--the actual data must be written out
716 do_write(Output_file*)
720 // A string table which goes into an output section.
722 class Output_data_strtab : public Output_section_data
725 Output_data_strtab(Stringpool* strtab)
726 : Output_section_data(1), strtab_(strtab)
730 // This is called to set the address and file offset. Here we make
731 // sure that the Stringpool is finalized.
733 set_final_data_size();
735 // Write out the data.
737 do_write(Output_file*);
739 // Write the data to a buffer.
741 do_write_to_buffer(unsigned char* buffer)
742 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
748 // This POD class is used to represent a single reloc in the output
749 // file. This could be a private class within Output_data_reloc, but
750 // the templatization is complex enough that I broke it out into a
751 // separate class. The class is templatized on either elfcpp::SHT_REL
752 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
753 // relocation or an ordinary relocation.
755 // A relocation can be against a global symbol, a local symbol, an
756 // output section, or the undefined symbol at index 0. We represent
757 // the latter by using a NULL global symbol.
759 template<int sh_type, bool dynamic, int size, bool big_endian>
762 template<bool dynamic, int size, bool big_endian>
763 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
766 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
768 // An uninitialized entry. We need this because we want to put
769 // instances of this class into an STL container.
771 : local_sym_index_(INVALID_CODE)
774 // A reloc against a global symbol.
776 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
777 Address address, bool is_relative);
779 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
780 unsigned int shndx, Address address, bool is_relative);
782 // A reloc against a local symbol.
784 Output_reloc(Sized_relobj<size, big_endian>* relobj,
785 unsigned int local_sym_index, unsigned int type,
786 Output_data* od, Address address, bool is_relative);
788 Output_reloc(Sized_relobj<size, big_endian>* relobj,
789 unsigned int local_sym_index, unsigned int type,
790 unsigned int shndx, Address address, bool is_relative);
792 // A reloc against the STT_SECTION symbol of an output section.
794 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
797 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
798 unsigned int shndx, Address address);
800 // Return TRUE if this is a RELATIVE relocation.
803 { return this->is_relative_; }
805 // Get the value of the symbol referred to by a Rel relocation.
808 symbol_value() const;
810 // Write the reloc entry to an output view.
812 write(unsigned char* pov) const;
814 // Write the offset and info fields to Write_rel.
815 template<typename Write_rel>
816 void write_rel(Write_rel*) const;
819 // Return the symbol index. We can't do a double template
820 // specialization, so we do a secondary template here.
822 get_symbol_index() const;
824 // Codes for local_sym_index_.
831 // Invalid uninitialized entry.
837 // For a local symbol, the object. We will never generate a
838 // relocation against a local symbol in a dynamic object; that
839 // doesn't make sense. And our callers will always be
840 // templatized, so we use Sized_relobj here.
841 Sized_relobj<size, big_endian>* relobj;
842 // For a global symbol, the symbol. If this is NULL, it indicates
843 // a relocation against the undefined 0 symbol.
845 // For a relocation against an output section, the output section.
850 // If shndx_ is not INVALID CODE, the object which holds the input
851 // section being used to specify the reloc address.
853 // If shndx_ is INVALID_CODE, the output data being used to
854 // specify the reloc address. This may be NULL if the reloc
855 // address is absolute.
858 // The address offset within the input section or the Output_data.
860 // For a local symbol, the local symbol index. This is GSYM_CODE
861 // for a global symbol, or INVALID_CODE for an uninitialized value.
862 unsigned int local_sym_index_;
863 // The reloc type--a processor specific code.
864 unsigned int type_ : 31;
865 // True if the relocation is a RELATIVE relocation.
866 bool is_relative_ : 1;
867 // If the reloc address is an input section in an object, the
868 // section index. This is INVALID_CODE if the reloc address is
869 // specified in some other way.
873 // The SHT_RELA version of Output_reloc<>. This is just derived from
874 // the SHT_REL version of Output_reloc, but it adds an addend.
876 template<bool dynamic, int size, bool big_endian>
877 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
880 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
881 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
883 // An uninitialized entry.
888 // A reloc against a global symbol.
890 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
891 Address address, Addend addend, bool is_relative)
892 : rel_(gsym, type, od, address, is_relative), addend_(addend)
895 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
896 unsigned int shndx, Address address, Addend addend,
898 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
901 // A reloc against a local symbol.
903 Output_reloc(Sized_relobj<size, big_endian>* relobj,
904 unsigned int local_sym_index, unsigned int type,
905 Output_data* od, Address address,
906 Addend addend, bool is_relative)
907 : rel_(relobj, local_sym_index, type, od, address, is_relative),
911 Output_reloc(Sized_relobj<size, big_endian>* relobj,
912 unsigned int local_sym_index, unsigned int type,
913 unsigned int shndx, Address address,
914 Addend addend, bool is_relative)
915 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
919 // A reloc against the STT_SECTION symbol of an output section.
921 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
922 Address address, Addend addend)
923 : rel_(os, type, od, address), addend_(addend)
926 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
927 unsigned int shndx, Address address, Addend addend)
928 : rel_(os, type, relobj, shndx, address), addend_(addend)
931 // Write the reloc entry to an output view.
933 write(unsigned char* pov) const;
937 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
942 // Output_data_reloc is used to manage a section containing relocs.
943 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
944 // indicates whether this is a dynamic relocation or a normal
945 // relocation. Output_data_reloc_base is a base class.
946 // Output_data_reloc is the real class, which we specialize based on
949 template<int sh_type, bool dynamic, int size, bool big_endian>
950 class Output_data_reloc_base : public Output_section_data_build
953 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
954 typedef typename Output_reloc_type::Address Address;
955 static const int reloc_size =
956 Reloc_types<sh_type, size, big_endian>::reloc_size;
958 // Construct the section.
959 Output_data_reloc_base()
960 : Output_section_data_build(Output_data::default_alignment_for_size(size))
964 // Write out the data.
966 do_write(Output_file*);
968 // Set the entry size and the link.
970 do_adjust_output_section(Output_section *os);
972 // Add a relocation entry.
974 add(Output_data *od, const Output_reloc_type& reloc)
976 this->relocs_.push_back(reloc);
977 this->set_current_data_size(this->relocs_.size() * reloc_size);
978 od->add_dynamic_reloc();
982 typedef std::vector<Output_reloc_type> Relocs;
987 // The class which callers actually create.
989 template<int sh_type, bool dynamic, int size, bool big_endian>
990 class Output_data_reloc;
992 // The SHT_REL version of Output_data_reloc.
994 template<bool dynamic, int size, bool big_endian>
995 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
996 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
999 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1003 typedef typename Base::Output_reloc_type Output_reloc_type;
1004 typedef typename Output_reloc_type::Address Address;
1007 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1010 // Add a reloc against a global symbol.
1013 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1014 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1017 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1018 unsigned int shndx, Address address)
1019 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1022 // Add a RELATIVE reloc against a global symbol. The final relocation
1023 // will not reference the symbol.
1026 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1028 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1031 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1032 Relobj* relobj, unsigned int shndx, Address address)
1033 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1036 // Add a reloc against a local symbol.
1039 add_local(Sized_relobj<size, big_endian>* relobj,
1040 unsigned int local_sym_index, unsigned int type,
1041 Output_data* od, Address address)
1042 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1046 add_local(Sized_relobj<size, big_endian>* relobj,
1047 unsigned int local_sym_index, unsigned int type,
1048 Output_data* od, unsigned int shndx, Address address)
1049 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1052 // Add a RELATIVE reloc against a local symbol.
1055 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1056 unsigned int local_sym_index, unsigned int type,
1057 Output_data* od, Address address)
1058 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1062 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1063 unsigned int local_sym_index, unsigned int type,
1064 Output_data* od, unsigned int shndx, Address address)
1065 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1068 // A reloc against the STT_SECTION symbol of an output section.
1069 // OS is the Output_section that the relocation refers to; OD is
1070 // the Output_data object being relocated.
1073 add_output_section(Output_section* os, unsigned int type,
1074 Output_data* od, Address address)
1075 { this->add(od, Output_reloc_type(os, type, od, address)); }
1078 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1079 Relobj* relobj, unsigned int shndx, Address address)
1080 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1083 // The SHT_RELA version of Output_data_reloc.
1085 template<bool dynamic, int size, bool big_endian>
1086 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1087 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1090 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1094 typedef typename Base::Output_reloc_type Output_reloc_type;
1095 typedef typename Output_reloc_type::Address Address;
1096 typedef typename Output_reloc_type::Addend Addend;
1099 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1102 // Add a reloc against a global symbol.
1105 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1106 Address address, Addend addend)
1107 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1111 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1112 unsigned int shndx, Address address,
1114 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1117 // Add a RELATIVE reloc against a global symbol. The final output
1118 // relocation will not reference the symbol, but we must keep the symbol
1119 // information long enough to set the addend of the relocation correctly
1120 // when it is written.
1123 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1124 Address address, Addend addend)
1125 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1128 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1129 Relobj* relobj, unsigned int shndx, Address address,
1131 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1134 // Add a reloc against a local symbol.
1137 add_local(Sized_relobj<size, big_endian>* relobj,
1138 unsigned int local_sym_index, unsigned int type,
1139 Output_data* od, Address address, Addend addend)
1141 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1146 add_local(Sized_relobj<size, big_endian>* relobj,
1147 unsigned int local_sym_index, unsigned int type,
1148 Output_data* od, unsigned int shndx, Address address,
1151 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1152 address, addend, false));
1155 // Add a RELATIVE reloc against a local symbol.
1158 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1159 unsigned int local_sym_index, unsigned int type,
1160 Output_data* od, Address address, Addend addend)
1162 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1167 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1168 unsigned int local_sym_index, unsigned int type,
1169 Output_data* od, unsigned int shndx, Address address,
1172 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1173 address, addend, true));
1176 // A reloc against the STT_SECTION symbol of an output section.
1179 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1180 Address address, Addend addend)
1181 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1184 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1185 unsigned int shndx, Address address, Addend addend)
1186 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1190 // Output_relocatable_relocs represents a relocation section in a
1191 // relocatable link. The actual data is written out in the target
1192 // hook relocate_for_relocatable. This just saves space for it.
1194 template<int sh_type, int size, bool big_endian>
1195 class Output_relocatable_relocs : public Output_section_data
1198 Output_relocatable_relocs(Relocatable_relocs* rr)
1199 : Output_section_data(Output_data::default_alignment_for_size(size)),
1204 set_final_data_size();
1206 // Write out the data. There is nothing to do here.
1208 do_write(Output_file*)
1212 // The relocs associated with this input section.
1213 Relocatable_relocs* rr_;
1216 // Handle a GROUP section.
1218 template<int size, bool big_endian>
1219 class Output_data_group : public Output_section_data
1222 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1223 section_size_type entry_count,
1224 const elfcpp::Elf_Word* contents);
1227 do_write(Output_file*);
1230 // The input object.
1231 Sized_relobj<size, big_endian>* relobj_;
1232 // The group flag word.
1233 elfcpp::Elf_Word flags_;
1234 // The section indexes of the input sections in this group.
1235 std::vector<unsigned int> input_sections_;
1238 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1239 // for one symbol--either a global symbol or a local symbol in an
1240 // object. The target specific code adds entries to the GOT as
1243 template<int size, bool big_endian>
1244 class Output_data_got : public Output_section_data_build
1247 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1248 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1249 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1252 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1256 // Add an entry for a global symbol to the GOT. Return true if this
1257 // is a new GOT entry, false if the symbol was already in the GOT.
1259 add_global(Symbol* gsym);
1261 // Add an entry for a global symbol to the GOT, and add a dynamic
1262 // relocation of type R_TYPE for the GOT entry.
1264 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1267 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1269 // Add an entry for a local symbol to the GOT. This returns true if
1270 // this is a new GOT entry, false if the symbol already has a GOT
1273 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1275 // Add an entry for a global symbol to the GOT, and add a dynamic
1276 // relocation of type R_TYPE for the GOT entry.
1278 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1279 unsigned int sym_index, Rel_dyn* rel_dyn,
1280 unsigned int r_type);
1283 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1284 unsigned int sym_index, Rela_dyn* rela_dyn,
1285 unsigned int r_type);
1287 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1288 // Return true if this is a new GOT entry, false if the symbol was
1289 // already in the GOT.
1291 add_global_tls(Symbol* gsym, bool need_pair);
1293 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1294 // relocation of type R_TYPE.
1296 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1297 unsigned int r_type);
1300 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1301 unsigned int r_type);
1303 // Add a pair of entries for a global TLS symbol to the GOT, and add
1304 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1306 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1307 unsigned int mod_r_type,
1308 unsigned int dtv_r_type);
1311 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1312 unsigned int mod_r_type,
1313 unsigned int dtv_r_type);
1315 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1316 // This returns true if this is a new GOT entry, false if the symbol
1317 // already has a GOT entry.
1319 add_local_tls(Sized_relobj<size, big_endian>* object,
1320 unsigned int sym_index, bool need_pair);
1322 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1323 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1324 // Because this is a local symbol, the first GOT entry can be relocated
1325 // relative to a section symbol, and the second GOT entry will have an
1326 // dtv-relative value that can be computed at link time.
1328 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1329 unsigned int sym_index, unsigned int shndx,
1330 bool need_pair, Rel_dyn* rel_dyn,
1331 unsigned int r_type);
1334 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1335 unsigned int sym_index, unsigned int shndx,
1336 bool need_pair, Rela_dyn* rela_dyn,
1337 unsigned int r_type);
1339 // Add a constant to the GOT. This returns the offset of the new
1340 // entry from the start of the GOT.
1342 add_constant(Valtype constant)
1344 this->entries_.push_back(Got_entry(constant));
1345 this->set_got_size();
1346 return this->last_got_offset();
1350 // Write out the GOT table.
1352 do_write(Output_file*);
1355 // This POD class holds a single GOT entry.
1359 // Create a zero entry.
1361 : local_sym_index_(CONSTANT_CODE)
1362 { this->u_.constant = 0; }
1364 // Create a global symbol entry.
1365 explicit Got_entry(Symbol* gsym)
1366 : local_sym_index_(GSYM_CODE)
1367 { this->u_.gsym = gsym; }
1369 // Create a local symbol entry.
1370 Got_entry(Sized_relobj<size, big_endian>* object,
1371 unsigned int local_sym_index)
1372 : local_sym_index_(local_sym_index)
1374 gold_assert(local_sym_index != GSYM_CODE
1375 && local_sym_index != CONSTANT_CODE);
1376 this->u_.object = object;
1379 // Create a constant entry. The constant is a host value--it will
1380 // be swapped, if necessary, when it is written out.
1381 explicit Got_entry(Valtype constant)
1382 : local_sym_index_(CONSTANT_CODE)
1383 { this->u_.constant = constant; }
1385 // Write the GOT entry to an output view.
1387 write(unsigned char* pov) const;
1398 // For a local symbol, the object.
1399 Sized_relobj<size, big_endian>* object;
1400 // For a global symbol, the symbol.
1402 // For a constant, the constant.
1405 // For a local symbol, the local symbol index. This is GSYM_CODE
1406 // for a global symbol, or CONSTANT_CODE for a constant.
1407 unsigned int local_sym_index_;
1410 typedef std::vector<Got_entry> Got_entries;
1412 // Return the offset into the GOT of GOT entry I.
1414 got_offset(unsigned int i) const
1415 { return i * (size / 8); }
1417 // Return the offset into the GOT of the last entry added.
1419 last_got_offset() const
1420 { return this->got_offset(this->entries_.size() - 1); }
1422 // Set the size of the section.
1425 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1427 // The list of GOT entries.
1428 Got_entries entries_;
1431 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1434 class Output_data_dynamic : public Output_section_data
1437 Output_data_dynamic(Stringpool* pool)
1438 : Output_section_data(Output_data::default_alignment()),
1439 entries_(), pool_(pool)
1442 // Add a new dynamic entry with a fixed numeric value.
1444 add_constant(elfcpp::DT tag, unsigned int val)
1445 { this->add_entry(Dynamic_entry(tag, val)); }
1447 // Add a new dynamic entry with the address of output data.
1449 add_section_address(elfcpp::DT tag, const Output_data* od)
1450 { this->add_entry(Dynamic_entry(tag, od, false)); }
1452 // Add a new dynamic entry with the size of output data.
1454 add_section_size(elfcpp::DT tag, const Output_data* od)
1455 { this->add_entry(Dynamic_entry(tag, od, true)); }
1457 // Add a new dynamic entry with the address of a symbol.
1459 add_symbol(elfcpp::DT tag, const Symbol* sym)
1460 { this->add_entry(Dynamic_entry(tag, sym)); }
1462 // Add a new dynamic entry with a string.
1464 add_string(elfcpp::DT tag, const char* str)
1465 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1468 add_string(elfcpp::DT tag, const std::string& str)
1469 { this->add_string(tag, str.c_str()); }
1472 // Adjust the output section to set the entry size.
1474 do_adjust_output_section(Output_section*);
1476 // Set the final data size.
1478 set_final_data_size();
1480 // Write out the dynamic entries.
1482 do_write(Output_file*);
1485 // This POD class holds a single dynamic entry.
1489 // Create an entry with a fixed numeric value.
1490 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1491 : tag_(tag), classification_(DYNAMIC_NUMBER)
1492 { this->u_.val = val; }
1494 // Create an entry with the size or address of a section.
1495 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1497 classification_(section_size
1498 ? DYNAMIC_SECTION_SIZE
1499 : DYNAMIC_SECTION_ADDRESS)
1500 { this->u_.od = od; }
1502 // Create an entry with the address of a symbol.
1503 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1504 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1505 { this->u_.sym = sym; }
1507 // Create an entry with a string.
1508 Dynamic_entry(elfcpp::DT tag, const char* str)
1509 : tag_(tag), classification_(DYNAMIC_STRING)
1510 { this->u_.str = str; }
1512 // Write the dynamic entry to an output view.
1513 template<int size, bool big_endian>
1515 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1523 DYNAMIC_SECTION_ADDRESS,
1525 DYNAMIC_SECTION_SIZE,
1534 // For DYNAMIC_NUMBER.
1536 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1537 const Output_data* od;
1538 // For DYNAMIC_SYMBOL.
1540 // For DYNAMIC_STRING.
1545 // The type of entry.
1546 Classification classification_;
1549 // Add an entry to the list.
1551 add_entry(const Dynamic_entry& entry)
1552 { this->entries_.push_back(entry); }
1554 // Sized version of write function.
1555 template<int size, bool big_endian>
1557 sized_write(Output_file* of);
1559 // The type of the list of entries.
1560 typedef std::vector<Dynamic_entry> Dynamic_entries;
1563 Dynamic_entries entries_;
1564 // The pool used for strings.
1568 // An output section. We don't expect to have too many output
1569 // sections, so we don't bother to do a template on the size.
1571 class Output_section : public Output_data
1574 // Create an output section, giving the name, type, and flags.
1575 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1576 virtual ~Output_section();
1578 // Add a new input section SHNDX, named NAME, with header SHDR, from
1579 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1580 // which applies to this section, or 0 if none, or -1U if more than
1581 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1582 // in a linker script; in that case we need to keep track of input
1583 // sections associated with an output section. Return the offset
1584 // within the output section.
1585 template<int size, bool big_endian>
1587 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1589 const elfcpp::Shdr<size, big_endian>& shdr,
1590 unsigned int reloc_shndx, bool have_sections_script);
1592 // Add generated data POSD to this output section.
1594 add_output_section_data(Output_section_data* posd);
1596 // Return the section name.
1599 { return this->name_; }
1601 // Return the section type.
1604 { return this->type_; }
1606 // Return the section flags.
1609 { return this->flags_; }
1611 // Return the entsize field.
1614 { return this->entsize_; }
1616 // Set the entsize field.
1618 set_entsize(uint64_t v);
1620 // Set the load address.
1622 set_load_address(uint64_t load_address)
1624 this->load_address_ = load_address;
1625 this->has_load_address_ = true;
1628 // Set the link field to the output section index of a section.
1630 set_link_section(const Output_data* od)
1632 gold_assert(this->link_ == 0
1633 && !this->should_link_to_symtab_
1634 && !this->should_link_to_dynsym_);
1635 this->link_section_ = od;
1638 // Set the link field to a constant.
1640 set_link(unsigned int v)
1642 gold_assert(this->link_section_ == NULL
1643 && !this->should_link_to_symtab_
1644 && !this->should_link_to_dynsym_);
1648 // Record that this section should link to the normal symbol table.
1650 set_should_link_to_symtab()
1652 gold_assert(this->link_section_ == NULL
1654 && !this->should_link_to_dynsym_);
1655 this->should_link_to_symtab_ = true;
1658 // Record that this section should link to the dynamic symbol table.
1660 set_should_link_to_dynsym()
1662 gold_assert(this->link_section_ == NULL
1664 && !this->should_link_to_symtab_);
1665 this->should_link_to_dynsym_ = true;
1668 // Return the info field.
1672 gold_assert(this->info_section_ == NULL
1673 && this->info_symndx_ == NULL);
1677 // Set the info field to the output section index of a section.
1679 set_info_section(const Output_section* os)
1681 gold_assert((this->info_section_ == NULL
1682 || (this->info_section_ == os
1683 && this->info_uses_section_index_))
1684 && this->info_symndx_ == NULL
1685 && this->info_ == 0);
1686 this->info_section_ = os;
1687 this->info_uses_section_index_= true;
1690 // Set the info field to the symbol table index of a symbol.
1692 set_info_symndx(const Symbol* sym)
1694 gold_assert(this->info_section_ == NULL
1695 && (this->info_symndx_ == NULL
1696 || this->info_symndx_ == sym)
1697 && this->info_ == 0);
1698 this->info_symndx_ = sym;
1701 // Set the info field to the symbol table index of a section symbol.
1703 set_info_section_symndx(const Output_section* os)
1705 gold_assert((this->info_section_ == NULL
1706 || (this->info_section_ == os
1707 && !this->info_uses_section_index_))
1708 && this->info_symndx_ == NULL
1709 && this->info_ == 0);
1710 this->info_section_ = os;
1711 this->info_uses_section_index_ = false;
1714 // Set the info field to a constant.
1716 set_info(unsigned int v)
1718 gold_assert(this->info_section_ == NULL
1719 && this->info_symndx_ == NULL
1720 && (this->info_ == 0
1721 || this->info_ == v));
1725 // Set the addralign field.
1727 set_addralign(uint64_t v)
1728 { this->addralign_ = v; }
1730 // Indicate that we need a symtab index.
1732 set_needs_symtab_index()
1733 { this->needs_symtab_index_ = true; }
1735 // Return whether we need a symtab index.
1737 needs_symtab_index() const
1738 { return this->needs_symtab_index_; }
1740 // Get the symtab index.
1742 symtab_index() const
1744 gold_assert(this->symtab_index_ != 0);
1745 return this->symtab_index_;
1748 // Set the symtab index.
1750 set_symtab_index(unsigned int index)
1752 gold_assert(index != 0);
1753 this->symtab_index_ = index;
1756 // Indicate that we need a dynsym index.
1758 set_needs_dynsym_index()
1759 { this->needs_dynsym_index_ = true; }
1761 // Return whether we need a dynsym index.
1763 needs_dynsym_index() const
1764 { return this->needs_dynsym_index_; }
1766 // Get the dynsym index.
1768 dynsym_index() const
1770 gold_assert(this->dynsym_index_ != 0);
1771 return this->dynsym_index_;
1774 // Set the dynsym index.
1776 set_dynsym_index(unsigned int index)
1778 gold_assert(index != 0);
1779 this->dynsym_index_ = index;
1782 // Return whether this section should be written after all the input
1783 // sections are complete.
1785 after_input_sections() const
1786 { return this->after_input_sections_; }
1788 // Record that this section should be written after all the input
1789 // sections are complete.
1791 set_after_input_sections()
1792 { this->after_input_sections_ = true; }
1794 // Return whether this section requires postprocessing after all
1795 // relocations have been applied.
1797 requires_postprocessing() const
1798 { return this->requires_postprocessing_; }
1800 // If a section requires postprocessing, return the buffer to use.
1802 postprocessing_buffer() const
1804 gold_assert(this->postprocessing_buffer_ != NULL);
1805 return this->postprocessing_buffer_;
1808 // If a section requires postprocessing, create the buffer to use.
1810 create_postprocessing_buffer();
1812 // If a section requires postprocessing, this is the size of the
1813 // buffer to which relocations should be applied.
1815 postprocessing_buffer_size() const
1816 { return this->current_data_size_for_child(); }
1818 // Modify the section name. This is only permitted for an
1819 // unallocated section, and only before the size has been finalized.
1820 // Otherwise the name will not get into Layout::namepool_.
1822 set_name(const char* newname)
1824 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1825 gold_assert(!this->is_data_size_valid());
1826 this->name_ = newname;
1829 // Return whether the offset OFFSET in the input section SHNDX in
1830 // object OBJECT is being included in the link.
1832 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1833 off_t offset) const;
1835 // Return the offset within the output section of OFFSET relative to
1836 // the start of input section SHNDX in object OBJECT.
1838 output_offset(const Relobj* object, unsigned int shndx,
1839 section_offset_type offset) const;
1841 // Return the output virtual address of OFFSET relative to the start
1842 // of input section SHNDX in object OBJECT.
1844 output_address(const Relobj* object, unsigned int shndx,
1845 off_t offset) const;
1847 // Return the output address of the start of the merged section for
1848 // input section SHNDX in object OBJECT. This is not necessarily
1849 // the offset corresponding to input offset 0 in the section, since
1850 // the section may be mapped arbitrarily.
1852 starting_output_address(const Relobj* object, unsigned int shndx) const;
1854 // Record that this output section was found in the SECTIONS clause
1855 // of a linker script.
1857 set_found_in_sections_clause()
1858 { this->found_in_sections_clause_ = true; }
1860 // Return whether this output section was found in the SECTIONS
1861 // clause of a linker script.
1863 found_in_sections_clause() const
1864 { return this->found_in_sections_clause_; }
1866 // Write the section header into *OPHDR.
1867 template<int size, bool big_endian>
1869 write_header(const Layout*, const Stringpool*,
1870 elfcpp::Shdr_write<size, big_endian>*) const;
1872 // The next few calls are for linker script support.
1874 // Store the list of input sections for this Output_section into the
1875 // list passed in. This removes the input sections, leaving only
1876 // any Output_section_data elements. This returns the size of those
1877 // Output_section_data elements. ADDRESS is the address of this
1878 // output section. FILL is the fill value to use, in case there are
1879 // any spaces between the remaining Output_section_data elements.
1881 get_input_sections(uint64_t address, const std::string& fill,
1882 std::list<std::pair<Relobj*, unsigned int > >*);
1884 // Add an input section from a script.
1886 add_input_section_for_script(Relobj* object, unsigned int shndx,
1887 off_t data_size, uint64_t addralign);
1889 // Set the current size of the output section.
1891 set_current_data_size(off_t size)
1892 { this->set_current_data_size_for_child(size); }
1894 // Get the current size of the output section.
1896 current_data_size() const
1897 { return this->current_data_size_for_child(); }
1899 // End of linker script support.
1901 // Print merge statistics to stderr.
1903 print_merge_stats();
1906 // Return the section index in the output file.
1908 do_out_shndx() const
1910 gold_assert(this->out_shndx_ != -1U);
1911 return this->out_shndx_;
1914 // Set the output section index.
1916 do_set_out_shndx(unsigned int shndx)
1918 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
1919 this->out_shndx_ = shndx;
1922 // Set the final data size of the Output_section. For a typical
1923 // Output_section, there is nothing to do, but if there are any
1924 // Output_section_data objects we need to set their final addresses
1927 set_final_data_size();
1929 // Reset the address and file offset.
1931 do_reset_address_and_file_offset();
1933 // Write the data to the file. For a typical Output_section, this
1934 // does nothing: the data is written out by calling Object::Relocate
1935 // on each input object. But if there are any Output_section_data
1936 // objects we do need to write them out here.
1938 do_write(Output_file*);
1940 // Return the address alignment--function required by parent class.
1942 do_addralign() const
1943 { return this->addralign_; }
1945 // Return whether there is a load address.
1947 do_has_load_address() const
1948 { return this->has_load_address_; }
1950 // Return the load address.
1952 do_load_address() const
1954 gold_assert(this->has_load_address_);
1955 return this->load_address_;
1958 // Return whether this is an Output_section.
1960 do_is_section() const
1963 // Return whether this is a section of the specified type.
1965 do_is_section_type(elfcpp::Elf_Word type) const
1966 { return this->type_ == type; }
1968 // Return whether the specified section flag is set.
1970 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1971 { return (this->flags_ & flag) != 0; }
1973 // Set the TLS offset. Called only for SHT_TLS sections.
1975 do_set_tls_offset(uint64_t tls_base);
1977 // Return the TLS offset, relative to the base of the TLS segment.
1978 // Valid only for SHT_TLS sections.
1980 do_tls_offset() const
1981 { return this->tls_offset_; }
1983 // This may be implemented by a child class.
1985 do_finalize_name(Layout*)
1988 // Record that this section requires postprocessing after all
1989 // relocations have been applied. This is called by a child class.
1991 set_requires_postprocessing()
1993 this->requires_postprocessing_ = true;
1994 this->after_input_sections_ = true;
1997 // Write all the data of an Output_section into the postprocessing
2000 write_to_postprocessing_buffer();
2003 // In some cases we need to keep a list of the input sections
2004 // associated with this output section. We only need the list if we
2005 // might have to change the offsets of the input section within the
2006 // output section after we add the input section. The ordinary
2007 // input sections will be written out when we process the object
2008 // file, and as such we don't need to track them here. We do need
2009 // to track Output_section_data objects here. We store instances of
2010 // this structure in a std::vector, so it must be a POD. There can
2011 // be many instances of this structure, so we use a union to save
2017 : shndx_(0), p2align_(0)
2019 this->u1_.data_size = 0;
2020 this->u2_.object = NULL;
2023 // For an ordinary input section.
2024 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2027 p2align_(ffsll(static_cast<long long>(addralign)))
2029 gold_assert(shndx != OUTPUT_SECTION_CODE
2030 && shndx != MERGE_DATA_SECTION_CODE
2031 && shndx != MERGE_STRING_SECTION_CODE);
2032 this->u1_.data_size = data_size;
2033 this->u2_.object = object;
2036 // For a non-merge output section.
2037 Input_section(Output_section_data* posd)
2038 : shndx_(OUTPUT_SECTION_CODE),
2039 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2041 this->u1_.data_size = 0;
2042 this->u2_.posd = posd;
2045 // For a merge section.
2046 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2048 ? MERGE_STRING_SECTION_CODE
2049 : MERGE_DATA_SECTION_CODE),
2050 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2052 this->u1_.entsize = entsize;
2053 this->u2_.posd = posd;
2056 // The required alignment.
2060 return (this->p2align_ == 0
2062 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2065 // Return the required size.
2069 // Whether this is an input section.
2071 is_input_section() const
2073 return (this->shndx_ != OUTPUT_SECTION_CODE
2074 && this->shndx_ != MERGE_DATA_SECTION_CODE
2075 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2078 // Return whether this is a merge section which matches the
2081 is_merge_section(bool is_string, uint64_t entsize,
2082 uint64_t addralign) const
2084 return (this->shndx_ == (is_string
2085 ? MERGE_STRING_SECTION_CODE
2086 : MERGE_DATA_SECTION_CODE)
2087 && this->u1_.entsize == entsize
2088 && this->addralign() == addralign);
2091 // Return the object for an input section.
2095 gold_assert(this->is_input_section());
2096 return this->u2_.object;
2099 // Return the input section index for an input section.
2103 gold_assert(this->is_input_section());
2104 return this->shndx_;
2107 // Set the output section.
2109 set_output_section(Output_section* os)
2111 gold_assert(!this->is_input_section());
2112 this->u2_.posd->set_output_section(os);
2115 // Set the address and file offset. This is called during
2116 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2117 // the enclosing section.
2119 set_address_and_file_offset(uint64_t address, off_t file_offset,
2120 off_t section_file_offset);
2122 // Reset the address and file offset.
2124 reset_address_and_file_offset();
2126 // Finalize the data size.
2128 finalize_data_size();
2130 // Add an input section, for SHF_MERGE sections.
2132 add_input_section(Relobj* object, unsigned int shndx)
2134 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2135 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2136 return this->u2_.posd->add_input_section(object, shndx);
2139 // Given an input OBJECT, an input section index SHNDX within that
2140 // object, and an OFFSET relative to the start of that input
2141 // section, return whether or not the output offset is known. If
2142 // this function returns true, it sets *POUTPUT to the offset in
2143 // the output section, relative to the start of the input section
2144 // in the output section. *POUTPUT may be different from OFFSET
2145 // for a merged section.
2147 output_offset(const Relobj* object, unsigned int shndx,
2148 section_offset_type offset,
2149 section_offset_type *poutput) const;
2151 // Return whether this is the merge section for the input section
2154 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2156 // Write out the data. This does nothing for an input section.
2158 write(Output_file*);
2160 // Write the data to a buffer. This does nothing for an input
2163 write_to_buffer(unsigned char*);
2165 // Print statistics about merge sections to stderr.
2167 print_merge_stats(const char* section_name)
2169 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2170 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2171 this->u2_.posd->print_merge_stats(section_name);
2175 // Code values which appear in shndx_. If the value is not one of
2176 // these codes, it is the input section index in the object file.
2179 // An Output_section_data.
2180 OUTPUT_SECTION_CODE = -1U,
2181 // An Output_section_data for an SHF_MERGE section with
2182 // SHF_STRINGS not set.
2183 MERGE_DATA_SECTION_CODE = -2U,
2184 // An Output_section_data for an SHF_MERGE section with
2186 MERGE_STRING_SECTION_CODE = -3U
2189 // For an ordinary input section, this is the section index in the
2190 // input file. For an Output_section_data, this is
2191 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2192 // MERGE_STRING_SECTION_CODE.
2193 unsigned int shndx_;
2194 // The required alignment, stored as a power of 2.
2195 unsigned int p2align_;
2198 // For an ordinary input section, the section size.
2200 // For OUTPUT_SECTION_CODE, this is not used. For
2201 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2207 // For an ordinary input section, the object which holds the
2210 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2211 // MERGE_STRING_SECTION_CODE, the data.
2212 Output_section_data* posd;
2216 typedef std::vector<Input_section> Input_section_list;
2218 // Fill data. This is used to fill in data between input sections.
2219 // It is also used for data statements (BYTE, WORD, etc.) in linker
2220 // scripts. When we have to keep track of the input sections, we
2221 // can use an Output_data_const, but we don't want to have to keep
2222 // track of input sections just to implement fills.
2226 Fill(off_t section_offset, off_t length)
2227 : section_offset_(section_offset),
2228 length_(convert_to_section_size_type(length))
2231 // Return section offset.
2233 section_offset() const
2234 { return this->section_offset_; }
2236 // Return fill length.
2239 { return this->length_; }
2242 // The offset within the output section.
2243 off_t section_offset_;
2244 // The length of the space to fill.
2245 section_size_type length_;
2248 typedef std::vector<Fill> Fill_list;
2250 // Add a new output section by Input_section.
2252 add_output_section_data(Input_section*);
2254 // Add an SHF_MERGE input section. Returns true if the section was
2257 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2258 uint64_t entsize, uint64_t addralign);
2260 // Add an output SHF_MERGE section POSD to this output section.
2261 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2262 // ENTSIZE is the entity size. This returns the entry added to
2265 add_output_merge_section(Output_section_data* posd, bool is_string,
2268 // Most of these fields are only valid after layout.
2270 // The name of the section. This will point into a Stringpool.
2272 // The section address is in the parent class.
2273 // The section alignment.
2274 uint64_t addralign_;
2275 // The section entry size.
2277 // The load address. This is only used when using a linker script
2278 // with a SECTIONS clause. The has_load_address_ field indicates
2279 // whether this field is valid.
2280 uint64_t load_address_;
2281 // The file offset is in the parent class.
2282 // Set the section link field to the index of this section.
2283 const Output_data* link_section_;
2284 // If link_section_ is NULL, this is the link field.
2286 // Set the section info field to the index of this section.
2287 const Output_section* info_section_;
2288 // If info_section_ is NULL, set the info field to the symbol table
2289 // index of this symbol.
2290 const Symbol* info_symndx_;
2291 // If info_section_ and info_symndx_ are NULL, this is the section
2294 // The section type.
2295 const elfcpp::Elf_Word type_;
2296 // The section flags.
2297 elfcpp::Elf_Xword flags_;
2298 // The section index.
2299 unsigned int out_shndx_;
2300 // If there is a STT_SECTION for this output section in the normal
2301 // symbol table, this is the symbol index. This starts out as zero.
2302 // It is initialized in Layout::finalize() to be the index, or -1U
2303 // if there isn't one.
2304 unsigned int symtab_index_;
2305 // If there is a STT_SECTION for this output section in the dynamic
2306 // symbol table, this is the symbol index. This starts out as zero.
2307 // It is initialized in Layout::finalize() to be the index, or -1U
2308 // if there isn't one.
2309 unsigned int dynsym_index_;
2310 // The input sections. This will be empty in cases where we don't
2311 // need to keep track of them.
2312 Input_section_list input_sections_;
2313 // The offset of the first entry in input_sections_.
2314 off_t first_input_offset_;
2315 // The fill data. This is separate from input_sections_ because we
2316 // often will need fill sections without needing to keep track of
2319 // If the section requires postprocessing, this buffer holds the
2320 // section contents during relocation.
2321 unsigned char* postprocessing_buffer_;
2322 // Whether this output section needs a STT_SECTION symbol in the
2323 // normal symbol table. This will be true if there is a relocation
2325 bool needs_symtab_index_ : 1;
2326 // Whether this output section needs a STT_SECTION symbol in the
2327 // dynamic symbol table. This will be true if there is a dynamic
2328 // relocation which needs it.
2329 bool needs_dynsym_index_ : 1;
2330 // Whether the link field of this output section should point to the
2331 // normal symbol table.
2332 bool should_link_to_symtab_ : 1;
2333 // Whether the link field of this output section should point to the
2334 // dynamic symbol table.
2335 bool should_link_to_dynsym_ : 1;
2336 // Whether this section should be written after all the input
2337 // sections are complete.
2338 bool after_input_sections_ : 1;
2339 // Whether this section requires post processing after all
2340 // relocations have been applied.
2341 bool requires_postprocessing_ : 1;
2342 // Whether an input section was mapped to this output section
2343 // because of a SECTIONS clause in a linker script.
2344 bool found_in_sections_clause_ : 1;
2345 // Whether this section has an explicitly specified load address.
2346 bool has_load_address_ : 1;
2347 // True if the info_section_ field means the section index of the
2348 // section, false if it means the symbol index of the corresponding
2350 bool info_uses_section_index_ : 1;
2351 // For SHT_TLS sections, the offset of this section relative to the base
2352 // of the TLS segment.
2353 uint64_t tls_offset_;
2356 // An output segment. PT_LOAD segments are built from collections of
2357 // output sections. Other segments typically point within PT_LOAD
2358 // segments, and are built directly as needed.
2360 class Output_segment
2363 // Create an output segment, specifying the type and flags.
2364 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2366 // Return the virtual address.
2369 { return this->vaddr_; }
2371 // Return the physical address.
2374 { return this->paddr_; }
2376 // Return the segment type.
2379 { return this->type_; }
2381 // Return the segment flags.
2384 { return this->flags_; }
2386 // Return the memory size.
2389 { return this->memsz_; }
2391 // Return the file size.
2394 { return this->filesz_; }
2396 // Return the maximum alignment of the Output_data.
2398 maximum_alignment();
2400 // Add an Output_section to this segment.
2402 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2403 { this->add_output_section(os, seg_flags, false); }
2405 // Add an Output_section to the start of this segment.
2407 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2408 { this->add_output_section(os, seg_flags, true); }
2410 // Add an Output_data (which is not an Output_section) to the start
2413 add_initial_output_data(Output_data*);
2415 // Return the number of dynamic relocations applied to this segment.
2417 dynamic_reloc_count() const;
2419 // Return the address of the first section.
2421 first_section_load_address() const;
2423 // Return whether the addresses have been set already.
2425 are_addresses_set() const
2426 { return this->are_addresses_set_; }
2428 // Set the addresses.
2430 set_addresses(uint64_t vaddr, uint64_t paddr)
2432 this->vaddr_ = vaddr;
2433 this->paddr_ = paddr;
2434 this->are_addresses_set_ = true;
2437 // Set the segment flags. This is only used if we have a PHDRS
2438 // clause which explicitly specifies the flags.
2440 set_flags(elfcpp::Elf_Word flags)
2441 { this->flags_ = flags; }
2443 // Set the address of the segment to ADDR and the offset to *POFF
2444 // and set the addresses and offsets of all contained output
2445 // sections accordingly. Set the section indexes of all contained
2446 // output sections starting with *PSHNDX. If RESET is true, first
2447 // reset the addresses of the contained sections. Return the
2448 // address of the immediately following segment. Update *POFF and
2449 // *PSHNDX. This should only be called for a PT_LOAD segment.
2451 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2452 unsigned int* pshndx);
2454 // Set the minimum alignment of this segment. This may be adjusted
2455 // upward based on the section alignments.
2457 set_minimum_p_align(uint64_t align)
2458 { this->min_p_align_ = align; }
2460 // Set the offset of this segment based on the section. This should
2461 // only be called for a non-PT_LOAD segment.
2465 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2469 // Return the number of output sections.
2471 output_section_count() const;
2473 // Return the section attached to the list segment with the lowest
2474 // load address. This is used when handling a PHDRS clause in a
2477 section_with_lowest_load_address() const;
2479 // Write the segment header into *OPHDR.
2480 template<int size, bool big_endian>
2482 write_header(elfcpp::Phdr_write<size, big_endian>*);
2484 // Write the section headers of associated sections into V.
2485 template<int size, bool big_endian>
2487 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2488 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2491 Output_segment(const Output_segment&);
2492 Output_segment& operator=(const Output_segment&);
2494 typedef std::list<Output_data*> Output_data_list;
2496 // Add an Output_section to this segment, specifying front or back.
2498 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2501 // Find the maximum alignment in an Output_data_list.
2503 maximum_alignment_list(const Output_data_list*);
2505 // Set the section addresses in an Output_data_list.
2507 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2508 off_t* poff, unsigned int* pshndx);
2510 // Return the number of Output_sections in an Output_data_list.
2512 output_section_count_list(const Output_data_list*) const;
2514 // Return the number of dynamic relocs in an Output_data_list.
2516 dynamic_reloc_count_list(const Output_data_list*) const;
2518 // Find the section with the lowest load address in an
2519 // Output_data_list.
2521 lowest_load_address_in_list(const Output_data_list* pdl,
2522 Output_section** found,
2523 uint64_t* found_lma) const;
2525 // Write the section headers in the list into V.
2526 template<int size, bool big_endian>
2528 write_section_headers_list(const Layout*, const Stringpool*,
2529 const Output_data_list*, unsigned char* v,
2530 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2532 // The list of output data with contents attached to this segment.
2533 Output_data_list output_data_;
2534 // The list of output data without contents attached to this segment.
2535 Output_data_list output_bss_;
2536 // The segment virtual address.
2538 // The segment physical address.
2540 // The size of the segment in memory.
2542 // The maximum section alignment. The is_max_align_known_ field
2543 // indicates whether this has been finalized.
2544 uint64_t max_align_;
2545 // The required minimum value for the p_align field. This is used
2546 // for PT_LOAD segments. Note that this does not mean that
2547 // addresses should be aligned to this value; it means the p_paddr
2548 // and p_vaddr fields must be congruent modulo this value. For
2549 // non-PT_LOAD segments, the dynamic linker works more efficiently
2550 // if the p_align field has the more conventional value, although it
2551 // can align as needed.
2552 uint64_t min_p_align_;
2553 // The offset of the segment data within the file.
2555 // The size of the segment data in the file.
2557 // The segment type;
2558 elfcpp::Elf_Word type_;
2559 // The segment flags.
2560 elfcpp::Elf_Word flags_;
2561 // Whether we have finalized max_align_.
2562 bool is_max_align_known_ : 1;
2563 // Whether vaddr and paddr were set by a linker script.
2564 bool are_addresses_set_ : 1;
2567 // This class represents the output file.
2572 Output_file(const char* name);
2574 // Open the output file. FILE_SIZE is the final size of the file.
2576 open(off_t file_size);
2578 // Resize the output file.
2580 resize(off_t file_size);
2582 // Close the output file (flushing all buffered data) and make sure
2583 // there are no errors.
2587 // We currently always use mmap which makes the view handling quite
2588 // simple. In the future we may support other approaches.
2590 // Write data to the output file.
2592 write(off_t offset, const void* data, size_t len)
2593 { memcpy(this->base_ + offset, data, len); }
2595 // Get a buffer to use to write to the file, given the offset into
2596 // the file and the size.
2598 get_output_view(off_t start, size_t size)
2600 gold_assert(start >= 0
2601 && start + static_cast<off_t>(size) <= this->file_size_);
2602 return this->base_ + start;
2605 // VIEW must have been returned by get_output_view. Write the
2606 // buffer to the file, passing in the offset and the size.
2608 write_output_view(off_t, size_t, unsigned char*)
2611 // Get a read/write buffer. This is used when we want to write part
2612 // of the file, read it in, and write it again.
2614 get_input_output_view(off_t start, size_t size)
2615 { return this->get_output_view(start, size); }
2617 // Write a read/write buffer back to the file.
2619 write_input_output_view(off_t, size_t, unsigned char*)
2622 // Get a read buffer. This is used when we just want to read part
2623 // of the file back it in.
2624 const unsigned char*
2625 get_input_view(off_t start, size_t size)
2626 { return this->get_output_view(start, size); }
2628 // Release a read bfufer.
2630 free_input_view(off_t, size_t, const unsigned char*)
2634 // Map the file into memory and return a pointer to the map.
2638 // Unmap the file from memory (and flush to disk buffers).
2648 // Base of file mapped into memory.
2649 unsigned char* base_;
2650 // True iff base_ points to a memory buffer rather than an output file.
2651 bool map_is_anonymous_;
2654 } // End namespace gold.
2656 #endif // !defined(GOLD_OUTPUT_H)