1 // output.h -- manage the output file for gold -*- C++ -*-
11 #include "reloc-types.h"
16 class General_options;
22 template<int size, bool big_endian>
24 template<int size, bool big_endian>
27 // An abtract class for data which has to go into the output file.
32 explicit Output_data(off_t data_size = 0)
33 : address_(0), data_size_(data_size), offset_(-1)
39 // Return the address. This is only valid after Layout::finalize is
43 { return this->address_; }
45 // Return the size of the data. This must be valid after
46 // Layout::finalize calls set_address, but need not be valid before
50 { return this->data_size_; }
52 // Return the file offset. This is only valid after
53 // Layout::finalize is finished.
56 { return this->offset_; }
58 // Return the required alignment.
61 { return this->do_addralign(); }
63 // Return whether this is an Output_section.
66 { return this->do_is_section(); }
68 // Return whether this is an Output_section of the specified type.
70 is_section_type(elfcpp::Elf_Word stt) const
71 { return this->do_is_section_type(stt); }
73 // Return whether this is an Output_section with the specified flag
76 is_section_flag_set(elfcpp::Elf_Xword shf) const
77 { return this->do_is_section_flag_set(shf); }
79 // Return the output section index, if there is an output section.
82 { return this->do_out_shndx(); }
84 // Set the output section index, if this is an output section.
86 set_out_shndx(unsigned int shndx)
87 { this->do_set_out_shndx(shndx); }
89 // Set the address and file offset of this data. This is called
90 // during Layout::finalize.
92 set_address(uint64_t addr, off_t off);
94 // Write the data to the output file. This is called after
95 // Layout::finalize is complete.
97 write(Output_file* file)
98 { this->do_write(file); }
100 // This is called by Layout::finalize to note that all sizes must
104 { Output_data::sizes_are_fixed = true; }
107 // Functions that child classes may or in some cases must implement.
109 // Write the data to the output file.
111 do_write(Output_file*) = 0;
113 // Return the required alignment.
115 do_addralign() const = 0;
117 // Return whether this is an Output_section.
119 do_is_section() const
122 // Return whether this is an Output_section of the specified type.
123 // This only needs to be implement by Output_section.
125 do_is_section_type(elfcpp::Elf_Word) const
128 // Return whether this is an Output_section with the specific flag
129 // set. This only needs to be implemented by Output_section.
131 do_is_section_flag_set(elfcpp::Elf_Xword) const
134 // Return the output section index, if there is an output section.
137 { gold_unreachable(); }
139 // Set the output section index, if this is an output section.
141 do_set_out_shndx(unsigned int)
142 { gold_unreachable(); }
144 // Set the address and file offset of the data. This only needs to
145 // be implemented if the child needs to know. The child class can
146 // set its size in this call.
148 do_set_address(uint64_t, off_t)
151 // Functions that child classes may call.
153 // Set the size of the data.
155 set_data_size(off_t data_size)
157 gold_assert(!Output_data::sizes_are_fixed);
158 this->data_size_ = data_size;
161 // Return default alignment for a size--32 or 64.
163 default_alignment(int size);
166 Output_data(const Output_data&);
167 Output_data& operator=(const Output_data&);
169 // This is used for verification, to make sure that we don't try to
170 // change any sizes after we set the section addresses.
171 static bool sizes_are_fixed;
173 // Memory address in file (not always meaningful).
175 // Size of data in file.
177 // Offset within file.
181 // Output the section headers.
183 class Output_section_headers : public Output_data
186 Output_section_headers(int size,
188 const Layout::Segment_list&,
189 const Layout::Section_list&,
192 // Write the data to the file.
194 do_write(Output_file*);
196 // Return the required alignment.
199 { return Output_data::default_alignment(this->size_); }
202 // Write the data to the file with the right size and endianness.
203 template<int size, bool big_endian>
205 do_sized_write(Output_file*);
209 const Layout::Segment_list& segment_list_;
210 const Layout::Section_list& unattached_section_list_;
211 const Stringpool* secnamepool_;
214 // Output the segment headers.
216 class Output_segment_headers : public Output_data
219 Output_segment_headers(int size, bool big_endian,
220 const Layout::Segment_list& segment_list);
222 // Write the data to the file.
224 do_write(Output_file*);
226 // Return the required alignment.
229 { return Output_data::default_alignment(this->size_); }
232 // Write the data to the file with the right size and endianness.
233 template<int size, bool big_endian>
235 do_sized_write(Output_file*);
239 const Layout::Segment_list& segment_list_;
242 // Output the ELF file header.
244 class Output_file_header : public Output_data
247 Output_file_header(int size,
249 const General_options&,
252 const Output_segment_headers*);
254 // Add information about the section headers. We lay out the ELF
255 // file header before we create the section headers.
256 void set_section_info(const Output_section_headers*,
257 const Output_section* shstrtab);
259 // Write the data to the file.
261 do_write(Output_file*);
263 // Return the required alignment.
266 { return Output_data::default_alignment(this->size_); }
268 // Set the address and offset--we only implement this for error
271 do_set_address(uint64_t, off_t off) const
272 { gold_assert(off == 0); }
275 // Write the data to the file with the right size and endianness.
276 template<int size, bool big_endian>
278 do_sized_write(Output_file*);
282 const General_options& options_;
283 const Target* target_;
284 const Symbol_table* symtab_;
285 const Output_segment_headers* segment_header_;
286 const Output_section_headers* section_header_;
287 const Output_section* shstrtab_;
290 // Output sections are mainly comprised of input sections. However,
291 // there are cases where we have data to write out which is not in an
292 // input section. Output_section_data is used in such cases. This is
293 // an abstract base class.
295 class Output_section_data : public Output_data
298 Output_section_data(off_t data_size, uint64_t addralign)
299 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
302 Output_section_data(uint64_t addralign)
303 : Output_data(0), output_section_(NULL), addralign_(addralign)
306 // Record the output section.
308 set_output_section(Output_section* os)
310 gold_assert(this->output_section_ == NULL);
311 this->output_section_ = os;
315 // The child class must implement do_write.
317 // Return the required alignment.
320 { return this->addralign_; }
322 // Return the section index of the output section.
324 do_out_shndx() const;
326 // Set the alignment.
328 set_addralign(uint64_t addralign)
329 { this->addralign_ = addralign; }
332 // The output section for this section.
333 const Output_section* output_section_;
334 // The required alignment.
338 // A simple case of Output_data in which we have constant data to
341 class Output_data_const : public Output_section_data
344 Output_data_const(const std::string& data, uint64_t addralign)
345 : Output_section_data(data.size(), addralign), data_(data)
348 Output_data_const(const char* p, off_t len, uint64_t addralign)
349 : Output_section_data(len, addralign), data_(p, len)
352 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
353 : Output_section_data(len, addralign),
354 data_(reinterpret_cast<const char*>(p), len)
359 add_data(const std::string& add)
361 this->data_.append(add);
362 this->set_data_size(this->data_.size());
365 // Write the data to the output file.
367 do_write(Output_file*);
373 // Another version of Output_data with constant data, in which the
374 // buffer is allocated by the caller.
376 class Output_data_const_buffer : public Output_section_data
379 Output_data_const_buffer(const unsigned char* p, off_t len,
381 : Output_section_data(len, addralign), p_(p)
384 // Write the data the output file.
386 do_write(Output_file*);
389 const unsigned char* p_;
392 // A place holder for data written out via some other mechanism.
394 class Output_data_space : public Output_section_data
397 Output_data_space(off_t data_size, uint64_t addralign)
398 : Output_section_data(data_size, addralign)
401 explicit Output_data_space(uint64_t addralign)
402 : Output_section_data(addralign)
407 set_space_size(off_t space_size)
408 { this->set_data_size(space_size); }
410 // Set the alignment.
412 set_space_alignment(uint64_t align)
413 { this->set_addralign(align); }
415 // Write out the data--this must be handled elsewhere.
417 do_write(Output_file*)
421 // A string table which goes into an output section.
423 class Output_data_strtab : public Output_section_data
426 Output_data_strtab(Stringpool* strtab)
427 : Output_section_data(1), strtab_(strtab)
430 // This is called to set the address and file offset. Here we make
431 // sure that the Stringpool is finalized.
433 do_set_address(uint64_t, off_t);
435 // Write out the data.
437 do_write(Output_file*);
443 // This POD class is used to represent a single reloc in the output
444 // file. This could be a private class within Output_data_reloc, but
445 // the templatization is complex enough that I broke it out into a
446 // separate class. The class is templatized on either elfcpp::SHT_REL
447 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
448 // relocation or an ordinary relocation.
450 // A relocation can be against a global symbol, a local symbol, an
451 // output section, or the undefined symbol at index 0. We represent
452 // the latter by using a NULL global symbol.
454 template<int sh_type, bool dynamic, int size, bool big_endian>
457 template<bool dynamic, int size, bool big_endian>
458 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
461 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
463 // An uninitialized entry. We need this because we want to put
464 // instances of this class into an STL container.
466 : local_sym_index_(INVALID_CODE)
469 // A reloc against a global symbol.
471 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
473 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
476 this->u1_.gsym = gsym;
480 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
481 unsigned int shndx, Address address)
482 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
485 gold_assert(shndx != INVALID_CODE);
486 this->u1_.gsym = gsym;
487 this->u2_.relobj = relobj;
490 // A reloc against a local symbol.
492 Output_reloc(Sized_relobj<size, big_endian>* relobj,
493 unsigned int local_sym_index,
497 : address_(address), local_sym_index_(local_sym_index), type_(type),
500 gold_assert(local_sym_index != GSYM_CODE
501 && local_sym_index != INVALID_CODE);
502 this->u1_.relobj = relobj;
506 Output_reloc(Sized_relobj<size, big_endian>* relobj,
507 unsigned int local_sym_index,
511 : address_(address), local_sym_index_(local_sym_index), type_(type),
514 gold_assert(local_sym_index != GSYM_CODE
515 && local_sym_index != INVALID_CODE);
516 gold_assert(shndx != INVALID_CODE);
517 this->u1_.relobj = relobj;
518 this->u2_.relobj = relobj;
521 // A reloc against the STT_SECTION symbol of an output section.
523 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
525 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
532 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
533 unsigned int shndx, Address address)
534 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
537 gold_assert(shndx != INVALID_CODE);
539 this->u2_.relobj = relobj;
542 // Write the reloc entry to an output view.
544 write(unsigned char* pov) const;
546 // Write the offset and info fields to Write_rel.
547 template<typename Write_rel>
548 void write_rel(Write_rel*) const;
551 // Return the symbol index. We can't do a double template
552 // specialization, so we do a secondary template here.
554 get_symbol_index() const;
556 // Codes for local_sym_index_.
563 // Invalid uninitialized entry.
569 // For a local symbol, the object. We will never generate a
570 // relocation against a local symbol in a dynamic object; that
571 // doesn't make sense. And our callers will always be
572 // templatized, so we use Sized_relobj here.
573 Sized_relobj<size, big_endian>* relobj;
574 // For a global symbol, the symbol. If this is NULL, it indicates
575 // a relocation against the undefined 0 symbol.
577 // For a relocation against an output section, the output section.
582 // If shndx_ is not INVALID CODE, the object which holds the input
583 // section being used to specify the reloc address.
585 // If shndx_ is INVALID_CODE, the output data being used to
586 // specify the reloc address. This may be NULL if the reloc
587 // address is absolute.
590 // The address offset within the input section or the Output_data.
592 // For a local symbol, the local symbol index. This is GSYM_CODE
593 // for a global symbol, or INVALID_CODE for an uninitialized value.
594 unsigned int local_sym_index_;
595 // The reloc type--a processor specific code.
597 // If the reloc address is an input section in an object, the
598 // section index. This is INVALID_CODE if the reloc address is
599 // specified in some other way.
603 // The SHT_RELA version of Output_reloc<>. This is just derived from
604 // the SHT_REL version of Output_reloc, but it adds an addend.
606 template<bool dynamic, int size, bool big_endian>
607 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
610 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
611 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
613 // An uninitialized entry.
618 // A reloc against a global symbol.
620 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
621 Address address, Addend addend)
622 : rel_(gsym, type, od, address), addend_(addend)
625 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
626 unsigned int shndx, Address address, Addend addend)
627 : rel_(gsym, type, relobj, shndx, address), addend_(addend)
630 // A reloc against a local symbol.
632 Output_reloc(Sized_relobj<size, big_endian>* relobj,
633 unsigned int local_sym_index,
634 unsigned int type, Output_data* od, Address address,
636 : rel_(relobj, local_sym_index, type, od, address), addend_(addend)
639 Output_reloc(Sized_relobj<size, big_endian>* relobj,
640 unsigned int local_sym_index,
645 : rel_(relobj, local_sym_index, type, shndx, address),
649 // A reloc against the STT_SECTION symbol of an output section.
651 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
652 Address address, Addend addend)
653 : rel_(os, type, od, address), addend_(addend)
656 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
657 unsigned int shndx, Address address, Addend addend)
658 : rel_(os, type, relobj, shndx, address), addend_(addend)
661 // Write the reloc entry to an output view.
663 write(unsigned char* pov) const;
667 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
672 // Output_data_reloc is used to manage a section containing relocs.
673 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
674 // indicates whether this is a dynamic relocation or a normal
675 // relocation. Output_data_reloc_base is a base class.
676 // Output_data_reloc is the real class, which we specialize based on
679 template<int sh_type, bool dynamic, int size, bool big_endian>
680 class Output_data_reloc_base : public Output_section_data
683 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
684 typedef typename Output_reloc_type::Address Address;
685 static const int reloc_size =
686 Reloc_types<sh_type, size, big_endian>::reloc_size;
688 // Construct the section.
689 Output_data_reloc_base()
690 : Output_section_data(Output_data::default_alignment(size))
693 // Write out the data.
695 do_write(Output_file*);
698 // Add a relocation entry.
700 add(const Output_reloc_type& reloc)
702 this->relocs_.push_back(reloc);
703 this->set_data_size(this->relocs_.size() * reloc_size);
707 typedef std::vector<Output_reloc_type> Relocs;
712 // The class which callers actually create.
714 template<int sh_type, bool dynamic, int size, bool big_endian>
715 class Output_data_reloc;
717 // The SHT_REL version of Output_data_reloc.
719 template<bool dynamic, int size, bool big_endian>
720 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
721 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
724 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
728 typedef typename Base::Output_reloc_type Output_reloc_type;
729 typedef typename Output_reloc_type::Address Address;
732 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
735 // Add a reloc against a global symbol.
738 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
739 { this->add(Output_reloc_type(gsym, type, od, address)); }
742 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
743 unsigned int shndx, Address address)
744 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address)); }
746 // Add a reloc against a local symbol.
749 add_local(Sized_relobj<size, big_endian>* relobj,
750 unsigned int local_sym_index, unsigned int type,
751 Output_data* od, Address address)
752 { this->add(Output_reloc_type(relobj, local_sym_index, type, od, address)); }
755 add_local(Sized_relobj<size, big_endian>* relobj,
756 unsigned int local_sym_index, unsigned int type,
757 unsigned int shndx, Address address)
758 { this->add(Output_reloc_type(relobj, local_sym_index, type, shndx,
762 // A reloc against the STT_SECTION symbol of an output section.
765 add_output_section(Output_section* os, unsigned int type,
766 Output_data* od, Address address)
767 { this->add(Output_reloc_type(os, type, od, address)); }
770 add_output_section(Output_section* os, unsigned int type,
771 Relobj* relobj, unsigned int shndx, Address address)
772 { this->add(Output_reloc_type(os, type, relobj, shndx, address)); }
775 // The SHT_RELA version of Output_data_reloc.
777 template<bool dynamic, int size, bool big_endian>
778 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
779 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
782 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
786 typedef typename Base::Output_reloc_type Output_reloc_type;
787 typedef typename Output_reloc_type::Address Address;
788 typedef typename Output_reloc_type::Addend Addend;
791 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
794 // Add a reloc against a global symbol.
797 add_global(Symbol* gsym, unsigned int type, Output_data* od,
798 Address address, Addend addend)
799 { this->add(Output_reloc_type(gsym, type, od, address, addend)); }
802 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
803 unsigned int shndx, Address address, Addend addend)
804 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address, addend)); }
806 // Add a reloc against a local symbol.
809 add_local(Sized_relobj<size, big_endian>* relobj,
810 unsigned int local_sym_index, unsigned int type,
811 Output_data* od, Address address, Addend addend)
813 this->add(Output_reloc_type(relobj, local_sym_index, type, od, address,
818 add_local(Sized_relobj<size, big_endian>* relobj,
819 unsigned int local_sym_index, unsigned int type,
820 unsigned int shndx, Address address, Addend addend)
822 this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, address,
826 // A reloc against the STT_SECTION symbol of an output section.
829 add_output_section(Output_section* os, unsigned int type, Output_data* od,
830 Address address, Addend addend)
831 { this->add(Output_reloc_type(os, type, od, address, addend)); }
834 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
835 unsigned int shndx, Address address, Addend addend)
836 { this->add(Output_reloc_type(os, type, relobj, shndx, address, addend)); }
839 // Output_data_got is used to manage a GOT. Each entry in the GOT is
840 // for one symbol--either a global symbol or a local symbol in an
841 // object. The target specific code adds entries to the GOT as
844 template<int size, bool big_endian>
845 class Output_data_got : public Output_section_data
848 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
850 Output_data_got(const General_options* options)
851 : Output_section_data(Output_data::default_alignment(size)),
852 options_(options), entries_()
855 // Add an entry for a global symbol to the GOT. Return true if this
856 // is a new GOT entry, false if the symbol was already in the GOT.
858 add_global(Symbol* gsym);
860 // Add an entry for a local symbol to the GOT. This returns the
861 // offset of the new entry from the start of the GOT.
863 add_local(Object* object, unsigned int sym_index)
865 this->entries_.push_back(Got_entry(object, sym_index));
866 this->set_got_size();
867 return this->last_got_offset();
870 // Add a constant to the GOT. This returns the offset of the new
871 // entry from the start of the GOT.
873 add_constant(Valtype constant)
875 this->entries_.push_back(Got_entry(constant));
876 this->set_got_size();
877 return this->last_got_offset();
880 // Write out the GOT table.
882 do_write(Output_file*);
885 // This POD class holds a single GOT entry.
889 // Create a zero entry.
891 : local_sym_index_(CONSTANT_CODE)
892 { this->u_.constant = 0; }
894 // Create a global symbol entry.
895 explicit Got_entry(Symbol* gsym)
896 : local_sym_index_(GSYM_CODE)
897 { this->u_.gsym = gsym; }
899 // Create a local symbol entry.
900 Got_entry(Object* object, unsigned int local_sym_index)
901 : local_sym_index_(local_sym_index)
903 gold_assert(local_sym_index != GSYM_CODE
904 && local_sym_index != CONSTANT_CODE);
905 this->u_.object = object;
908 // Create a constant entry. The constant is a host value--it will
909 // be swapped, if necessary, when it is written out.
910 explicit Got_entry(Valtype constant)
911 : local_sym_index_(CONSTANT_CODE)
912 { this->u_.constant = constant; }
914 // Write the GOT entry to an output view.
916 write(const General_options*, unsigned char* pov) const;
927 // For a local symbol, the object.
929 // For a global symbol, the symbol.
931 // For a constant, the constant.
934 // For a local symbol, the local symbol index. This is GSYM_CODE
935 // for a global symbol, or CONSTANT_CODE for a constant.
936 unsigned int local_sym_index_;
939 typedef std::vector<Got_entry> Got_entries;
941 // Return the offset into the GOT of GOT entry I.
943 got_offset(unsigned int i) const
944 { return i * (size / 8); }
946 // Return the offset into the GOT of the last entry added.
948 last_got_offset() const
949 { return this->got_offset(this->entries_.size() - 1); }
951 // Set the size of the section.
954 { this->set_data_size(this->got_offset(this->entries_.size())); }
957 const General_options* options_;
958 // The list of GOT entries.
959 Got_entries entries_;
962 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
965 class Output_data_dynamic : public Output_section_data
968 Output_data_dynamic(const Target* target, Stringpool* pool)
969 : Output_section_data(Output_data::default_alignment(target->get_size())),
970 target_(target), entries_(), pool_(pool)
973 // Add a new dynamic entry with a fixed numeric value.
975 add_constant(elfcpp::DT tag, unsigned int val)
976 { this->add_entry(Dynamic_entry(tag, val)); }
978 // Add a new dynamic entry with the address of a section.
980 add_section_address(elfcpp::DT tag, Output_section* os)
981 { this->add_entry(Dynamic_entry(tag, os, false)); }
983 // Add a new dynamic entry with the size of a section.
985 add_section_size(elfcpp::DT tag, Output_section* os)
986 { this->add_entry(Dynamic_entry(tag, os, true)); }
988 // Add a new dynamic entry with the address of a symbol.
990 add_symbol(elfcpp::DT tag, Symbol* sym)
991 { this->add_entry(Dynamic_entry(tag, sym)); }
993 // Add a new dynamic entry with a string.
995 add_string(elfcpp::DT tag, const char* str)
996 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, NULL))); }
998 // Set the final data size.
1000 do_set_address(uint64_t, off_t);
1002 // Write out the dynamic entries.
1004 do_write(Output_file*);
1007 // This POD class holds a single dynamic entry.
1011 // Create an entry with a fixed numeric value.
1012 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1013 : tag_(tag), classification_(DYNAMIC_NUMBER)
1014 { this->u_.val = val; }
1016 // Create an entry with the size or address of a section.
1017 Dynamic_entry(elfcpp::DT tag, Output_section* os, bool section_size)
1019 classification_(section_size
1020 ? DYNAMIC_SECTION_SIZE
1021 : DYNAMIC_SECTION_ADDRESS)
1022 { this->u_.os = os; }
1024 // Create an entry with the address of a symbol.
1025 Dynamic_entry(elfcpp::DT tag, Symbol* sym)
1026 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1027 { this->u_.sym = sym; }
1029 // Create an entry with a string.
1030 Dynamic_entry(elfcpp::DT tag, const char* str)
1031 : tag_(tag), classification_(DYNAMIC_STRING)
1032 { this->u_.str = str; }
1034 // Write the dynamic entry to an output view.
1035 template<int size, bool big_endian>
1037 write(unsigned char* pov, const Stringpool*) const;
1045 DYNAMIC_SECTION_ADDRESS,
1047 DYNAMIC_SECTION_SIZE,
1056 // For DYNAMIC_NUMBER.
1058 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1060 // For DYNAMIC_SYMBOL.
1062 // For DYNAMIC_STRING.
1067 // The type of entry.
1068 Classification classification_;
1071 // Add an entry to the list.
1073 add_entry(const Dynamic_entry& entry)
1074 { this->entries_.push_back(entry); }
1076 // Sized version of write function.
1077 template<int size, bool big_endian>
1079 sized_write(Output_file* of);
1081 // The type of the list of entries.
1082 typedef std::vector<Dynamic_entry> Dynamic_entries;
1085 const Target* target_;
1087 Dynamic_entries entries_;
1088 // The pool used for strings.
1092 // An output section. We don't expect to have too many output
1093 // sections, so we don't bother to do a template on the size.
1095 class Output_section : public Output_data
1098 // Create an output section, giving the name, type, and flags.
1099 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
1101 virtual ~Output_section();
1103 // Add a new input section SHNDX, named NAME, with header SHDR, from
1104 // object OBJECT. Return the offset within the output section.
1105 template<int size, bool big_endian>
1107 add_input_section(Relobj* object, unsigned int shndx, const char *name,
1108 const elfcpp::Shdr<size, big_endian>& shdr);
1110 // Add generated data ODATA to this output section.
1112 add_output_section_data(Output_section_data* posd);
1114 // Return the section name.
1117 { return this->name_; }
1119 // Return the section type.
1122 { return this->type_; }
1124 // Return the section flags.
1127 { return this->flags_; }
1129 // Return the section index in the output file.
1131 do_out_shndx() const
1132 { return this->out_shndx_; }
1134 // Set the output section index.
1136 do_set_out_shndx(unsigned int shndx)
1137 { this->out_shndx_ = shndx; }
1139 // Return the entsize field.
1142 { return this->entsize_; }
1144 // Set the entsize field.
1146 set_entsize(uint64_t v)
1147 { this->entsize_ = v; }
1149 // Set the link field.
1151 set_link(unsigned int v)
1152 { this->link_ = v; }
1154 // Set the info field.
1156 set_info(unsigned int v)
1157 { this->info_ = v; }
1159 // Set the addralign field.
1161 set_addralign(uint64_t v)
1162 { this->addralign_ = v; }
1164 // Indicate that we need a symtab index.
1166 set_needs_symtab_index()
1167 { this->needs_symtab_index_ = true; }
1169 // Return whether we need a symtab index.
1171 needs_symtab_index() const
1172 { return this->needs_symtab_index_; }
1174 // Get the symtab index.
1176 symtab_index() const
1178 gold_assert(this->symtab_index_ != 0);
1179 return this->symtab_index_;
1182 // Set the symtab index.
1184 set_symtab_index(unsigned int index)
1186 gold_assert(index != 0);
1187 this->symtab_index_ = index;
1190 // Indicate that we need a dynsym index.
1192 set_needs_dynsym_index()
1193 { this->needs_dynsym_index_ = true; }
1195 // Return whether we need a dynsym index.
1197 needs_dynsym_index() const
1198 { return this->needs_dynsym_index_; }
1200 // Get the dynsym index.
1202 dynsym_index() const
1204 gold_assert(this->dynsym_index_ != 0);
1205 return this->dynsym_index_;
1208 // Set the dynsym index.
1210 set_dynsym_index(unsigned int index)
1212 gold_assert(index != 0);
1213 this->dynsym_index_ = index;
1216 // Set the address of the Output_section. For a typical
1217 // Output_section, there is nothing to do, but if there are any
1218 // Output_section_data objects we need to set the final addresses
1221 do_set_address(uint64_t, off_t);
1223 // Write the data to the file. For a typical Output_section, this
1224 // does nothing: the data is written out by calling Object::Relocate
1225 // on each input object. But if there are any Output_section_data
1226 // objects we do need to write them out here.
1228 do_write(Output_file*);
1230 // Return the address alignment--function required by parent class.
1232 do_addralign() const
1233 { return this->addralign_; }
1235 // Return whether this is an Output_section.
1237 do_is_section() const
1240 // Return whether this is a section of the specified type.
1242 do_is_section_type(elfcpp::Elf_Word type) const
1243 { return this->type_ == type; }
1245 // Return whether the specified section flag is set.
1247 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1248 { return (this->flags_ & flag) != 0; }
1250 // Write the section header into *OPHDR.
1251 template<int size, bool big_endian>
1253 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
1256 // In some cases we need to keep a list of the input sections
1257 // associated with this output section. We only need the list if we
1258 // might have to change the offsets of the input section within the
1259 // output section after we add the input section. The ordinary
1260 // input sections will be written out when we process the object
1261 // file, and as such we don't need to track them here. We do need
1262 // to track Output_section_data objects here. We store instances of
1263 // this structure in a std::vector, so it must be a POD. There can
1264 // be many instances of this structure, so we use a union to save
1270 : shndx_(0), p2align_(0), data_size_(0)
1271 { this->u_.object = NULL; }
1273 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1276 p2align_(ffsll(static_cast<long long>(addralign))),
1277 data_size_(data_size)
1279 gold_assert(shndx != -1U);
1280 this->u_.object = object;
1283 Input_section(Output_section_data* posd)
1285 p2align_(ffsll(static_cast<long long>(posd->addralign()))),
1287 { this->u_.posd = posd; }
1289 // The required alignment.
1293 return (this->p2align_ == 0
1295 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1298 // Return the required size.
1302 // Set the address and file offset. This is called during
1303 // Layout::finalize. SECOFF is the file offset of the enclosing
1306 set_address(uint64_t addr, off_t off, off_t secoff);
1308 // Write out the data. This does nothing for an input section.
1310 write(Output_file*);
1313 // Whether this is an input section.
1315 is_input_section() const
1316 { return this->shndx_ != -1U; }
1318 // For an ordinary input section, this is the section index in
1319 // the input file. For an Output_section_data, this is -1U.
1320 unsigned int shndx_;
1321 // The required alignment, stored as a power of 2.
1322 unsigned int p2align_;
1323 // For an ordinary input section, the section size.
1327 // If shndx_ != -1U, this points to the object which holds the
1330 // If shndx_ == -1U, this is the data to write out.
1331 Output_section_data* posd;
1335 typedef std::vector<Input_section> Input_section_list;
1337 // Most of these fields are only valid after layout.
1339 // The name of the section. This will point into a Stringpool.
1341 // The section address is in the parent class.
1342 // The section alignment.
1343 uint64_t addralign_;
1344 // The section entry size.
1346 // The file offset is in the parent class.
1347 // The section link field.
1349 // The section info field.
1351 // The section type.
1352 elfcpp::Elf_Word type_;
1353 // The section flags.
1354 elfcpp::Elf_Xword flags_;
1355 // The section index.
1356 unsigned int out_shndx_;
1357 // If there is a STT_SECTION for this output section in the normal
1358 // symbol table, this is the symbol index. This starts out as zero.
1359 // It is initialized in Layout::finalize() to be the index, or -1U
1360 // if there isn't one.
1361 unsigned int symtab_index_;
1362 // If there is a STT_SECTION for this output section in the dynamic
1363 // symbol table, this is the symbol index. This starts out as zero.
1364 // It is initialized in Layout::finalize() to be the index, or -1U
1365 // if there isn't one.
1366 unsigned int dynsym_index_;
1367 // The input sections. This will be empty in cases where we don't
1368 // need to keep track of them.
1369 Input_section_list input_sections_;
1370 // The offset of the first entry in input_sections_.
1371 off_t first_input_offset_;
1372 // Whether we permit adding data.
1373 bool may_add_data_ : 1;
1374 // Whether this output section needs a STT_SECTION symbol in the
1375 // normal symbol table. This will be true if there is a relocation
1377 bool needs_symtab_index_ : 1;
1378 // Whether this output section needs a STT_SECTION symbol in the
1379 // dynamic symbol table. This will be true if there is a dynamic
1380 // relocation which needs it.
1381 bool needs_dynsym_index_ : 1;
1384 // An output segment. PT_LOAD segments are built from collections of
1385 // output sections. Other segments typically point within PT_LOAD
1386 // segments, and are built directly as needed.
1388 class Output_segment
1391 // Create an output segment, specifying the type and flags.
1392 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
1394 // Return the virtual address.
1397 { return this->vaddr_; }
1399 // Return the physical address.
1402 { return this->paddr_; }
1404 // Return the segment type.
1407 { return this->type_; }
1409 // Return the segment flags.
1412 { return this->flags_; }
1414 // Return the memory size.
1417 { return this->memsz_; }
1419 // Return the file size.
1422 { return this->filesz_; }
1424 // Return the maximum alignment of the Output_data.
1428 // Add an Output_section to this segment.
1430 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1431 { this->add_output_section(os, seg_flags, false); }
1433 // Add an Output_section to the start of this segment.
1435 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1436 { this->add_output_section(os, seg_flags, true); }
1438 // Add an Output_data (which is not an Output_section) to the start
1441 add_initial_output_data(Output_data*);
1443 // Set the address of the segment to ADDR and the offset to *POFF
1444 // (aligned if necessary), and set the addresses and offsets of all
1445 // contained output sections accordingly. Set the section indexes
1446 // of all contained output sections starting with *PSHNDX. Return
1447 // the address of the immediately following segment. Update *POFF
1448 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1450 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
1452 // Set the offset of this segment based on the section. This should
1453 // only be called for a non-PT_LOAD segment.
1457 // Return the number of output sections.
1459 output_section_count() const;
1461 // Write the segment header into *OPHDR.
1462 template<int size, bool big_endian>
1464 write_header(elfcpp::Phdr_write<size, big_endian>*);
1466 // Write the section headers of associated sections into V.
1467 template<int size, bool big_endian>
1469 write_section_headers(const Stringpool*,
1471 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
1474 Output_segment(const Output_segment&);
1475 Output_segment& operator=(const Output_segment&);
1477 typedef std::list<Output_data*> Output_data_list;
1479 // Add an Output_section to this segment, specifying front or back.
1481 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
1484 // Find the maximum alignment in an Output_data_list.
1486 maximum_alignment(const Output_data_list*);
1488 // Set the section addresses in an Output_data_list.
1490 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
1491 unsigned int* pshndx);
1493 // Return the number of Output_sections in an Output_data_list.
1495 output_section_count_list(const Output_data_list*) const;
1497 // Write the section headers in the list into V.
1498 template<int size, bool big_endian>
1500 write_section_headers_list(const Stringpool*, const Output_data_list*,
1502 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
1504 // The list of output data with contents attached to this segment.
1505 Output_data_list output_data_;
1506 // The list of output data without contents attached to this segment.
1507 Output_data_list output_bss_;
1508 // The segment virtual address.
1510 // The segment physical address.
1512 // The size of the segment in memory.
1514 // The segment alignment.
1516 // The offset of the segment data within the file.
1518 // The size of the segment data in the file.
1520 // The segment type;
1521 elfcpp::Elf_Word type_;
1522 // The segment flags.
1523 elfcpp::Elf_Word flags_;
1524 // Whether we have set align_.
1525 bool is_align_known_;
1528 // This class represents the output file.
1533 Output_file(const General_options& options);
1535 // Open the output file. FILE_SIZE is the final size of the file.
1537 open(off_t file_size);
1539 // Close the output file and make sure there are no error.
1543 // We currently always use mmap which makes the view handling quite
1544 // simple. In the future we may support other approaches.
1546 // Write data to the output file.
1548 write(off_t offset, const void* data, off_t len)
1549 { memcpy(this->base_ + offset, data, len); }
1551 // Get a buffer to use to write to the file, given the offset into
1552 // the file and the size.
1554 get_output_view(off_t start, off_t size)
1556 gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
1557 return this->base_ + start;
1560 // VIEW must have been returned by get_output_view. Write the
1561 // buffer to the file, passing in the offset and the size.
1563 write_output_view(off_t, off_t, unsigned char*)
1568 const General_options& options_;
1575 // Base of file mapped into memory.
1576 unsigned char* base_;
1579 } // End namespace gold.
1581 #endif // !defined(GOLD_OUTPUT_H)