1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "reloc-types.h"
37 class General_options;
42 class Relocatable_relocs;
44 template<int size, bool big_endian>
46 template<int size, bool big_endian>
49 // An abtract class for data which has to go into the output file.
54 explicit Output_data()
55 : address_(0), data_size_(0), offset_(-1),
56 is_address_valid_(false), is_data_size_valid_(false),
57 is_offset_valid_(false), is_data_size_fixed_(false),
58 dynamic_reloc_count_(0)
64 // Return the address. For allocated sections, this is only valid
65 // after Layout::finalize is finished.
69 gold_assert(this->is_address_valid_);
70 return this->address_;
73 // Return the size of the data. For allocated sections, this must
74 // be valid after Layout::finalize calls set_address, but need not
75 // be valid before then.
79 gold_assert(this->is_data_size_valid_);
80 return this->data_size_;
83 // Return true if data size is fixed.
85 is_data_size_fixed() const
86 { return this->is_data_size_fixed_; }
88 // Return the file offset. This is only valid after
89 // Layout::finalize is finished. For some non-allocated sections,
90 // it may not be valid until near the end of the link.
94 gold_assert(this->is_offset_valid_);
98 // Reset the address and file offset. This essentially disables the
99 // sanity testing about duplicate and unknown settings.
101 reset_address_and_file_offset()
103 this->is_address_valid_ = false;
104 this->is_offset_valid_ = false;
105 if (!this->is_data_size_fixed_)
106 this->is_data_size_valid_ = false;
107 this->do_reset_address_and_file_offset();
110 // Return true if address and file offset already have reset values. In
111 // other words, calling reset_address_and_file_offset will not change them.
113 address_and_file_offset_have_reset_values() const
114 { return this->do_address_and_file_offset_have_reset_values(); }
116 // Return the required alignment.
119 { return this->do_addralign(); }
121 // Return whether this has a load address.
123 has_load_address() const
124 { return this->do_has_load_address(); }
126 // Return the load address.
129 { return this->do_load_address(); }
131 // Return whether this is an Output_section.
134 { return this->do_is_section(); }
136 // Return whether this is an Output_section of the specified type.
138 is_section_type(elfcpp::Elf_Word stt) const
139 { return this->do_is_section_type(stt); }
141 // Return whether this is an Output_section with the specified flag
144 is_section_flag_set(elfcpp::Elf_Xword shf) const
145 { return this->do_is_section_flag_set(shf); }
147 // Return the output section that this goes in, if there is one.
150 { return this->do_output_section(); }
152 // Return the output section index, if there is an output section.
155 { return this->do_out_shndx(); }
157 // Set the output section index, if this is an output section.
159 set_out_shndx(unsigned int shndx)
160 { this->do_set_out_shndx(shndx); }
162 // Set the address and file offset of this data, and finalize the
163 // size of the data. This is called during Layout::finalize for
164 // allocated sections.
166 set_address_and_file_offset(uint64_t addr, off_t off)
168 this->set_address(addr);
169 this->set_file_offset(off);
170 this->finalize_data_size();
175 set_address(uint64_t addr)
177 gold_assert(!this->is_address_valid_);
178 this->address_ = addr;
179 this->is_address_valid_ = true;
182 // Set the file offset.
184 set_file_offset(off_t off)
186 gold_assert(!this->is_offset_valid_);
188 this->is_offset_valid_ = true;
191 // Finalize the data size.
195 if (!this->is_data_size_valid_)
197 // Tell the child class to set the data size.
198 this->set_final_data_size();
199 gold_assert(this->is_data_size_valid_);
203 // Set the TLS offset. Called only for SHT_TLS sections.
205 set_tls_offset(uint64_t tls_base)
206 { this->do_set_tls_offset(tls_base); }
208 // Return the TLS offset, relative to the base of the TLS segment.
209 // Valid only for SHT_TLS sections.
212 { return this->do_tls_offset(); }
214 // Write the data to the output file. This is called after
215 // Layout::finalize is complete.
217 write(Output_file* file)
218 { this->do_write(file); }
220 // This is called by Layout::finalize to note that the sizes of
221 // allocated sections must now be fixed.
224 { Output_data::allocated_sizes_are_fixed = true; }
226 // Used to check that layout has been done.
229 { return Output_data::allocated_sizes_are_fixed; }
231 // Count the number of dynamic relocations applied to this section.
234 { ++this->dynamic_reloc_count_; }
236 // Return the number of dynamic relocations applied to this section.
238 dynamic_reloc_count() const
239 { return this->dynamic_reloc_count_; }
241 // Whether the address is valid.
243 is_address_valid() const
244 { return this->is_address_valid_; }
246 // Whether the file offset is valid.
248 is_offset_valid() const
249 { return this->is_offset_valid_; }
251 // Whether the data size is valid.
253 is_data_size_valid() const
254 { return this->is_data_size_valid_; }
256 // Print information to the map file.
258 print_to_mapfile(Mapfile* mapfile) const
259 { return this->do_print_to_mapfile(mapfile); }
262 // Functions that child classes may or in some cases must implement.
264 // Write the data to the output file.
266 do_write(Output_file*) = 0;
268 // Return the required alignment.
270 do_addralign() const = 0;
272 // Return whether this has a load address.
274 do_has_load_address() const
277 // Return the load address.
279 do_load_address() const
280 { gold_unreachable(); }
282 // Return whether this is an Output_section.
284 do_is_section() const
287 // Return whether this is an Output_section of the specified type.
288 // This only needs to be implement by Output_section.
290 do_is_section_type(elfcpp::Elf_Word) const
293 // Return whether this is an Output_section with the specific flag
294 // set. This only needs to be implemented by Output_section.
296 do_is_section_flag_set(elfcpp::Elf_Xword) const
299 // Return the output section, if there is one.
300 virtual Output_section*
304 // Return the output section index, if there is an output section.
307 { gold_unreachable(); }
309 // Set the output section index, if this is an output section.
311 do_set_out_shndx(unsigned int)
312 { gold_unreachable(); }
314 // This is a hook for derived classes to set the data size. This is
315 // called by finalize_data_size, normally called during
316 // Layout::finalize, when the section address is set.
318 set_final_data_size()
319 { gold_unreachable(); }
321 // A hook for resetting the address and file offset.
323 do_reset_address_and_file_offset()
326 // Return true if address and file offset already have reset values. In
327 // other words, calling reset_address_and_file_offset will not change them.
328 // A child class overriding do_reset_address_and_file_offset may need to
329 // also override this.
331 do_address_and_file_offset_have_reset_values() const
332 { return !this->is_address_valid_ && !this->is_offset_valid_; }
334 // Set the TLS offset. Called only for SHT_TLS sections.
336 do_set_tls_offset(uint64_t)
337 { gold_unreachable(); }
339 // Return the TLS offset, relative to the base of the TLS segment.
340 // Valid only for SHT_TLS sections.
342 do_tls_offset() const
343 { gold_unreachable(); }
345 // Print to the map file. This only needs to be implemented by
346 // classes which may appear in a PT_LOAD segment.
348 do_print_to_mapfile(Mapfile*) const
349 { gold_unreachable(); }
351 // Functions that child classes may call.
353 // Reset the address. The Output_section class needs this when an
354 // SHF_ALLOC input section is added to an output section which was
355 // formerly not SHF_ALLOC.
357 mark_address_invalid()
358 { this->is_address_valid_ = false; }
360 // Set the size of the data.
362 set_data_size(off_t data_size)
364 gold_assert(!this->is_data_size_valid_
365 && !this->is_data_size_fixed_);
366 this->data_size_ = data_size;
367 this->is_data_size_valid_ = true;
370 // Fix the data size. Once it is fixed, it cannot be changed
371 // and the data size remains always valid.
375 gold_assert(this->is_data_size_valid_);
376 this->is_data_size_fixed_ = true;
379 // Get the current data size--this is for the convenience of
380 // sections which build up their size over time.
382 current_data_size_for_child() const
383 { return this->data_size_; }
385 // Set the current data size--this is for the convenience of
386 // sections which build up their size over time.
388 set_current_data_size_for_child(off_t data_size)
390 gold_assert(!this->is_data_size_valid_);
391 this->data_size_ = data_size;
394 // Return default alignment for the target size.
398 // Return default alignment for a specified size--32 or 64.
400 default_alignment_for_size(int size);
403 Output_data(const Output_data&);
404 Output_data& operator=(const Output_data&);
406 // This is used for verification, to make sure that we don't try to
407 // change any sizes of allocated sections after we set the section
409 static bool allocated_sizes_are_fixed;
411 // Memory address in output file.
413 // Size of data in output file.
415 // File offset of contents in output file.
417 // Whether address_ is valid.
418 bool is_address_valid_;
419 // Whether data_size_ is valid.
420 bool is_data_size_valid_;
421 // Whether offset_ is valid.
422 bool is_offset_valid_;
423 // Whether data size is fixed.
424 bool is_data_size_fixed_;
425 // Count of dynamic relocations applied to this section.
426 unsigned int dynamic_reloc_count_;
429 // Output the section headers.
431 class Output_section_headers : public Output_data
434 Output_section_headers(const Layout*,
435 const Layout::Segment_list*,
436 const Layout::Section_list*,
437 const Layout::Section_list*,
439 const Output_section*);
442 // Write the data to the file.
444 do_write(Output_file*);
446 // Return the required alignment.
449 { return Output_data::default_alignment(); }
451 // Write to a map file.
453 do_print_to_mapfile(Mapfile* mapfile) const
454 { mapfile->print_output_data(this, _("** section headers")); }
456 // Set final data size.
458 set_final_data_size()
459 { this->set_data_size(this->do_size()); }
462 // Write the data to the file with the right size and endianness.
463 template<int size, bool big_endian>
465 do_sized_write(Output_file*);
467 // Compute data size.
471 const Layout* layout_;
472 const Layout::Segment_list* segment_list_;
473 const Layout::Section_list* section_list_;
474 const Layout::Section_list* unattached_section_list_;
475 const Stringpool* secnamepool_;
476 const Output_section* shstrtab_section_;
479 // Output the segment headers.
481 class Output_segment_headers : public Output_data
484 Output_segment_headers(const Layout::Segment_list& segment_list);
487 // Write the data to the file.
489 do_write(Output_file*);
491 // Return the required alignment.
494 { return Output_data::default_alignment(); }
496 // Write to a map file.
498 do_print_to_mapfile(Mapfile* mapfile) const
499 { mapfile->print_output_data(this, _("** segment headers")); }
501 // Set final data size.
503 set_final_data_size()
504 { this->set_data_size(this->do_size()); }
507 // Write the data to the file with the right size and endianness.
508 template<int size, bool big_endian>
510 do_sized_write(Output_file*);
512 // Compute the current size.
516 const Layout::Segment_list& segment_list_;
519 // Output the ELF file header.
521 class Output_file_header : public Output_data
524 Output_file_header(const Target*,
526 const Output_segment_headers*,
529 // Add information about the section headers. We lay out the ELF
530 // file header before we create the section headers.
531 void set_section_info(const Output_section_headers*,
532 const Output_section* shstrtab);
535 // Write the data to the file.
537 do_write(Output_file*);
539 // Return the required alignment.
542 { return Output_data::default_alignment(); }
544 // Write to a map file.
546 do_print_to_mapfile(Mapfile* mapfile) const
547 { mapfile->print_output_data(this, _("** file header")); }
549 // Set final data size.
551 set_final_data_size(void)
552 { this->set_data_size(this->do_size()); }
555 // Write the data to the file with the right size and endianness.
556 template<int size, bool big_endian>
558 do_sized_write(Output_file*);
560 // Return the value to use for the entry address.
562 typename elfcpp::Elf_types<size>::Elf_Addr
565 // Compute the current data size.
569 const Target* target_;
570 const Symbol_table* symtab_;
571 const Output_segment_headers* segment_header_;
572 const Output_section_headers* section_header_;
573 const Output_section* shstrtab_;
577 // Output sections are mainly comprised of input sections. However,
578 // there are cases where we have data to write out which is not in an
579 // input section. Output_section_data is used in such cases. This is
580 // an abstract base class.
582 class Output_section_data : public Output_data
585 Output_section_data(off_t data_size, uint64_t addralign,
586 bool is_data_size_fixed)
587 : Output_data(), output_section_(NULL), addralign_(addralign)
589 this->set_data_size(data_size);
590 if (is_data_size_fixed)
591 this->fix_data_size();
594 Output_section_data(uint64_t addralign)
595 : Output_data(), output_section_(NULL), addralign_(addralign)
598 // Return the output section.
599 const Output_section*
600 output_section() const
601 { return this->output_section_; }
603 // Record the output section.
605 set_output_section(Output_section* os);
607 // Add an input section, for SHF_MERGE sections. This returns true
608 // if the section was handled.
610 add_input_section(Relobj* object, unsigned int shndx)
611 { return this->do_add_input_section(object, shndx); }
613 // Given an input OBJECT, an input section index SHNDX within that
614 // object, and an OFFSET relative to the start of that input
615 // section, return whether or not the corresponding offset within
616 // the output section is known. If this function returns true, it
617 // sets *POUTPUT to the output offset. The value -1 indicates that
618 // this input offset is being discarded.
620 output_offset(const Relobj* object, unsigned int shndx,
621 section_offset_type offset,
622 section_offset_type *poutput) const
623 { return this->do_output_offset(object, shndx, offset, poutput); }
625 // Return whether this is the merge section for the input section
626 // SHNDX in OBJECT. This should return true when output_offset
627 // would return true for some values of OFFSET.
629 is_merge_section_for(const Relobj* object, unsigned int shndx) const
630 { return this->do_is_merge_section_for(object, shndx); }
632 // Write the contents to a buffer. This is used for sections which
633 // require postprocessing, such as compression.
635 write_to_buffer(unsigned char* buffer)
636 { this->do_write_to_buffer(buffer); }
638 // Print merge stats to stderr. This should only be called for
639 // SHF_MERGE sections.
641 print_merge_stats(const char* section_name)
642 { this->do_print_merge_stats(section_name); }
645 // The child class must implement do_write.
647 // The child class may implement specific adjustments to the output
650 do_adjust_output_section(Output_section*)
653 // May be implemented by child class. Return true if the section
656 do_add_input_section(Relobj*, unsigned int)
657 { gold_unreachable(); }
659 // The child class may implement output_offset.
661 do_output_offset(const Relobj*, unsigned int, section_offset_type,
662 section_offset_type*) const
665 // The child class may implement is_merge_section_for.
667 do_is_merge_section_for(const Relobj*, unsigned int) const
670 // The child class may implement write_to_buffer. Most child
671 // classes can not appear in a compressed section, and they do not
674 do_write_to_buffer(unsigned char*)
675 { gold_unreachable(); }
677 // Print merge statistics.
679 do_print_merge_stats(const char*)
680 { gold_unreachable(); }
682 // Return the required alignment.
685 { return this->addralign_; }
687 // Return the output section.
690 { return this->output_section_; }
692 // Return the section index of the output section.
694 do_out_shndx() const;
696 // Set the alignment.
698 set_addralign(uint64_t addralign);
701 // The output section for this section.
702 Output_section* output_section_;
703 // The required alignment.
707 // Some Output_section_data classes build up their data step by step,
708 // rather than all at once. This class provides an interface for
711 class Output_section_data_build : public Output_section_data
714 Output_section_data_build(uint64_t addralign)
715 : Output_section_data(addralign)
718 // Get the current data size.
720 current_data_size() const
721 { return this->current_data_size_for_child(); }
723 // Set the current data size.
725 set_current_data_size(off_t data_size)
726 { this->set_current_data_size_for_child(data_size); }
729 // Set the final data size.
731 set_final_data_size()
732 { this->set_data_size(this->current_data_size_for_child()); }
735 // A simple case of Output_data in which we have constant data to
738 class Output_data_const : public Output_section_data
741 Output_data_const(const std::string& data, uint64_t addralign)
742 : Output_section_data(data.size(), addralign, true), data_(data)
745 Output_data_const(const char* p, off_t len, uint64_t addralign)
746 : Output_section_data(len, addralign, true), data_(p, len)
749 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
750 : Output_section_data(len, addralign, true),
751 data_(reinterpret_cast<const char*>(p), len)
755 // Write the data to the output file.
757 do_write(Output_file*);
759 // Write the data to a buffer.
761 do_write_to_buffer(unsigned char* buffer)
762 { memcpy(buffer, this->data_.data(), this->data_.size()); }
764 // Write to a map file.
766 do_print_to_mapfile(Mapfile* mapfile) const
767 { mapfile->print_output_data(this, _("** fill")); }
773 // Another version of Output_data with constant data, in which the
774 // buffer is allocated by the caller.
776 class Output_data_const_buffer : public Output_section_data
779 Output_data_const_buffer(const unsigned char* p, off_t len,
780 uint64_t addralign, const char* map_name)
781 : Output_section_data(len, addralign, true),
782 p_(p), map_name_(map_name)
786 // Write the data the output file.
788 do_write(Output_file*);
790 // Write the data to a buffer.
792 do_write_to_buffer(unsigned char* buffer)
793 { memcpy(buffer, this->p_, this->data_size()); }
795 // Write to a map file.
797 do_print_to_mapfile(Mapfile* mapfile) const
798 { mapfile->print_output_data(this, _(this->map_name_)); }
801 // The data to output.
802 const unsigned char* p_;
803 // Name to use in a map file. Maps are a rarely used feature, but
804 // the space usage is minor as aren't very many of these objects.
805 const char* map_name_;
808 // A place holder for a fixed amount of data written out via some
811 class Output_data_fixed_space : public Output_section_data
814 Output_data_fixed_space(off_t data_size, uint64_t addralign,
815 const char* map_name)
816 : Output_section_data(data_size, addralign, true),
821 // Write out the data--the actual data must be written out
824 do_write(Output_file*)
827 // Write to a map file.
829 do_print_to_mapfile(Mapfile* mapfile) const
830 { mapfile->print_output_data(this, _(this->map_name_)); }
833 // Name to use in a map file. Maps are a rarely used feature, but
834 // the space usage is minor as aren't very many of these objects.
835 const char* map_name_;
838 // A place holder for variable sized data written out via some other
841 class Output_data_space : public Output_section_data_build
844 explicit Output_data_space(uint64_t addralign, const char* map_name)
845 : Output_section_data_build(addralign),
849 // Set the alignment.
851 set_space_alignment(uint64_t align)
852 { this->set_addralign(align); }
855 // Write out the data--the actual data must be written out
858 do_write(Output_file*)
861 // Write to a map file.
863 do_print_to_mapfile(Mapfile* mapfile) const
864 { mapfile->print_output_data(this, _(this->map_name_)); }
867 // Name to use in a map file. Maps are a rarely used feature, but
868 // the space usage is minor as aren't very many of these objects.
869 const char* map_name_;
872 // Fill fixed space with zeroes. This is just like
873 // Output_data_fixed_space, except that the map name is known.
875 class Output_data_zero_fill : public Output_section_data
878 Output_data_zero_fill(off_t data_size, uint64_t addralign)
879 : Output_section_data(data_size, addralign, true)
883 // There is no data to write out.
885 do_write(Output_file*)
888 // Write to a map file.
890 do_print_to_mapfile(Mapfile* mapfile) const
891 { mapfile->print_output_data(this, "** zero fill"); }
894 // A string table which goes into an output section.
896 class Output_data_strtab : public Output_section_data
899 Output_data_strtab(Stringpool* strtab)
900 : Output_section_data(1), strtab_(strtab)
904 // This is called to set the address and file offset. Here we make
905 // sure that the Stringpool is finalized.
907 set_final_data_size();
909 // Write out the data.
911 do_write(Output_file*);
913 // Write the data to a buffer.
915 do_write_to_buffer(unsigned char* buffer)
916 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
918 // Write to a map file.
920 do_print_to_mapfile(Mapfile* mapfile) const
921 { mapfile->print_output_data(this, _("** string table")); }
927 // This POD class is used to represent a single reloc in the output
928 // file. This could be a private class within Output_data_reloc, but
929 // the templatization is complex enough that I broke it out into a
930 // separate class. The class is templatized on either elfcpp::SHT_REL
931 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
932 // relocation or an ordinary relocation.
934 // A relocation can be against a global symbol, a local symbol, a
935 // local section symbol, an output section, or the undefined symbol at
936 // index 0. We represent the latter by using a NULL global symbol.
938 template<int sh_type, bool dynamic, int size, bool big_endian>
941 template<bool dynamic, int size, bool big_endian>
942 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
945 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
946 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
948 static const Address invalid_address = static_cast<Address>(0) - 1;
950 // An uninitialized entry. We need this because we want to put
951 // instances of this class into an STL container.
953 : local_sym_index_(INVALID_CODE)
956 // We have a bunch of different constructors. They come in pairs
957 // depending on how the address of the relocation is specified. It
958 // can either be an offset in an Output_data or an offset in an
961 // A reloc against a global symbol.
963 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
964 Address address, bool is_relative);
966 Output_reloc(Symbol* gsym, unsigned int type,
967 Sized_relobj<size, big_endian>* relobj,
968 unsigned int shndx, Address address, bool is_relative);
970 // A reloc against a local symbol or local section symbol.
972 Output_reloc(Sized_relobj<size, big_endian>* relobj,
973 unsigned int local_sym_index, unsigned int type,
974 Output_data* od, Address address, bool is_relative,
975 bool is_section_symbol);
977 Output_reloc(Sized_relobj<size, big_endian>* relobj,
978 unsigned int local_sym_index, unsigned int type,
979 unsigned int shndx, Address address, bool is_relative,
980 bool is_section_symbol);
982 // A reloc against the STT_SECTION symbol of an output section.
984 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
987 Output_reloc(Output_section* os, unsigned int type,
988 Sized_relobj<size, big_endian>* relobj,
989 unsigned int shndx, Address address);
991 // Return TRUE if this is a RELATIVE relocation.
994 { return this->is_relative_; }
996 // Return whether this is against a local section symbol.
998 is_local_section_symbol() const
1000 return (this->local_sym_index_ != GSYM_CODE
1001 && this->local_sym_index_ != SECTION_CODE
1002 && this->local_sym_index_ != INVALID_CODE
1003 && this->is_section_symbol_);
1006 // For a local section symbol, return the offset of the input
1007 // section within the output section. ADDEND is the addend being
1008 // applied to the input section.
1010 local_section_offset(Addend addend) const;
1012 // Get the value of the symbol referred to by a Rel relocation when
1013 // we are adding the given ADDEND.
1015 symbol_value(Addend addend) const;
1017 // Write the reloc entry to an output view.
1019 write(unsigned char* pov) const;
1021 // Write the offset and info fields to Write_rel.
1022 template<typename Write_rel>
1023 void write_rel(Write_rel*) const;
1025 // This is used when sorting dynamic relocs. Return -1 to sort this
1026 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1028 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1031 // Return whether this reloc should be sorted before the argument
1032 // when sorting dynamic relocs.
1034 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1036 { return this->compare(r2) < 0; }
1039 // Record that we need a dynamic symbol index.
1041 set_needs_dynsym_index();
1043 // Return the symbol index.
1045 get_symbol_index() const;
1047 // Return the output address.
1049 get_address() const;
1051 // Codes for local_sym_index_.
1058 // Invalid uninitialized entry.
1064 // For a local symbol or local section symbol
1065 // (this->local_sym_index_ >= 0), the object. We will never
1066 // generate a relocation against a local symbol in a dynamic
1067 // object; that doesn't make sense. And our callers will always
1068 // be templatized, so we use Sized_relobj here.
1069 Sized_relobj<size, big_endian>* relobj;
1070 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1071 // symbol. If this is NULL, it indicates a relocation against the
1072 // undefined 0 symbol.
1074 // For a relocation against an output section
1075 // (this->local_sym_index_ == SECTION_CODE), the output section.
1080 // If this->shndx_ is not INVALID CODE, the object which holds the
1081 // input section being used to specify the reloc address.
1082 Sized_relobj<size, big_endian>* relobj;
1083 // If this->shndx_ is INVALID_CODE, the output data being used to
1084 // specify the reloc address. This may be NULL if the reloc
1085 // address is absolute.
1088 // The address offset within the input section or the Output_data.
1090 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1091 // relocation against an output section, or INVALID_CODE for an
1092 // uninitialized value. Otherwise, for a local symbol
1093 // (this->is_section_symbol_ is false), the local symbol index. For
1094 // a local section symbol (this->is_section_symbol_ is true), the
1095 // section index in the input file.
1096 unsigned int local_sym_index_;
1097 // The reloc type--a processor specific code.
1098 unsigned int type_ : 30;
1099 // True if the relocation is a RELATIVE relocation.
1100 bool is_relative_ : 1;
1101 // True if the relocation is against a section symbol.
1102 bool is_section_symbol_ : 1;
1103 // If the reloc address is an input section in an object, the
1104 // section index. This is INVALID_CODE if the reloc address is
1105 // specified in some other way.
1106 unsigned int shndx_;
1109 // The SHT_RELA version of Output_reloc<>. This is just derived from
1110 // the SHT_REL version of Output_reloc, but it adds an addend.
1112 template<bool dynamic, int size, bool big_endian>
1113 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1116 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1117 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1119 // An uninitialized entry.
1124 // A reloc against a global symbol.
1126 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1127 Address address, Addend addend, bool is_relative)
1128 : rel_(gsym, type, od, address, is_relative), addend_(addend)
1131 Output_reloc(Symbol* gsym, unsigned int type,
1132 Sized_relobj<size, big_endian>* relobj,
1133 unsigned int shndx, Address address, Addend addend,
1135 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1138 // A reloc against a local symbol.
1140 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1141 unsigned int local_sym_index, unsigned int type,
1142 Output_data* od, Address address,
1143 Addend addend, bool is_relative, bool is_section_symbol)
1144 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1149 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1150 unsigned int local_sym_index, unsigned int type,
1151 unsigned int shndx, Address address,
1152 Addend addend, bool is_relative, bool is_section_symbol)
1153 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1158 // A reloc against the STT_SECTION symbol of an output section.
1160 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1161 Address address, Addend addend)
1162 : rel_(os, type, od, address), addend_(addend)
1165 Output_reloc(Output_section* os, unsigned int type,
1166 Sized_relobj<size, big_endian>* relobj,
1167 unsigned int shndx, Address address, Addend addend)
1168 : rel_(os, type, relobj, shndx, address), addend_(addend)
1171 // Write the reloc entry to an output view.
1173 write(unsigned char* pov) const;
1175 // Return whether this reloc should be sorted before the argument
1176 // when sorting dynamic relocs.
1178 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1181 int i = this->rel_.compare(r2.rel_);
1187 return this->addend_ < r2.addend_;
1192 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1197 // Output_data_reloc is used to manage a section containing relocs.
1198 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1199 // indicates whether this is a dynamic relocation or a normal
1200 // relocation. Output_data_reloc_base is a base class.
1201 // Output_data_reloc is the real class, which we specialize based on
1204 template<int sh_type, bool dynamic, int size, bool big_endian>
1205 class Output_data_reloc_base : public Output_section_data_build
1208 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1209 typedef typename Output_reloc_type::Address Address;
1210 static const int reloc_size =
1211 Reloc_types<sh_type, size, big_endian>::reloc_size;
1213 // Construct the section.
1214 Output_data_reloc_base(bool sort_relocs)
1215 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1216 sort_relocs_(sort_relocs)
1220 // Write out the data.
1222 do_write(Output_file*);
1224 // Set the entry size and the link.
1226 do_adjust_output_section(Output_section *os);
1228 // Write to a map file.
1230 do_print_to_mapfile(Mapfile* mapfile) const
1232 mapfile->print_output_data(this,
1234 ? _("** dynamic relocs")
1238 // Add a relocation entry.
1240 add(Output_data *od, const Output_reloc_type& reloc)
1242 this->relocs_.push_back(reloc);
1243 this->set_current_data_size(this->relocs_.size() * reloc_size);
1244 od->add_dynamic_reloc();
1248 typedef std::vector<Output_reloc_type> Relocs;
1250 // The class used to sort the relocations.
1251 struct Sort_relocs_comparison
1254 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1255 { return r1.sort_before(r2); }
1258 // The relocations in this section.
1260 // Whether to sort the relocations when writing them out, to make
1261 // the dynamic linker more efficient.
1265 // The class which callers actually create.
1267 template<int sh_type, bool dynamic, int size, bool big_endian>
1268 class Output_data_reloc;
1270 // The SHT_REL version of Output_data_reloc.
1272 template<bool dynamic, int size, bool big_endian>
1273 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1274 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1277 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1281 typedef typename Base::Output_reloc_type Output_reloc_type;
1282 typedef typename Output_reloc_type::Address Address;
1284 Output_data_reloc(bool sr)
1285 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1288 // Add a reloc against a global symbol.
1291 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1292 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1295 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1296 Sized_relobj<size, big_endian>* relobj,
1297 unsigned int shndx, Address address)
1298 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1301 // These are to simplify the Copy_relocs class.
1304 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1307 gold_assert(addend == 0);
1308 this->add_global(gsym, type, od, address);
1312 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1313 Sized_relobj<size, big_endian>* relobj,
1314 unsigned int shndx, Address address, Address addend)
1316 gold_assert(addend == 0);
1317 this->add_global(gsym, type, od, relobj, shndx, address);
1320 // Add a RELATIVE reloc against a global symbol. The final relocation
1321 // will not reference the symbol.
1324 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1326 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1329 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1330 Sized_relobj<size, big_endian>* relobj,
1331 unsigned int shndx, Address address)
1333 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1337 // Add a reloc against a local symbol.
1340 add_local(Sized_relobj<size, big_endian>* relobj,
1341 unsigned int local_sym_index, unsigned int type,
1342 Output_data* od, Address address)
1344 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1345 address, false, false));
1349 add_local(Sized_relobj<size, big_endian>* relobj,
1350 unsigned int local_sym_index, unsigned int type,
1351 Output_data* od, unsigned int shndx, Address address)
1353 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1354 address, false, false));
1357 // Add a RELATIVE reloc against a local symbol.
1360 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1361 unsigned int local_sym_index, unsigned int type,
1362 Output_data* od, Address address)
1364 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1365 address, true, false));
1369 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1370 unsigned int local_sym_index, unsigned int type,
1371 Output_data* od, unsigned int shndx, Address address)
1373 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1374 address, true, false));
1377 // Add a reloc against a local section symbol. This will be
1378 // converted into a reloc against the STT_SECTION symbol of the
1382 add_local_section(Sized_relobj<size, big_endian>* relobj,
1383 unsigned int input_shndx, unsigned int type,
1384 Output_data* od, Address address)
1386 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1387 address, false, true));
1391 add_local_section(Sized_relobj<size, big_endian>* relobj,
1392 unsigned int input_shndx, unsigned int type,
1393 Output_data* od, unsigned int shndx, Address address)
1395 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1396 address, false, true));
1399 // A reloc against the STT_SECTION symbol of an output section.
1400 // OS is the Output_section that the relocation refers to; OD is
1401 // the Output_data object being relocated.
1404 add_output_section(Output_section* os, unsigned int type,
1405 Output_data* od, Address address)
1406 { this->add(od, Output_reloc_type(os, type, od, address)); }
1409 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1410 Sized_relobj<size, big_endian>* relobj,
1411 unsigned int shndx, Address address)
1412 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1415 // The SHT_RELA version of Output_data_reloc.
1417 template<bool dynamic, int size, bool big_endian>
1418 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1419 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1422 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1426 typedef typename Base::Output_reloc_type Output_reloc_type;
1427 typedef typename Output_reloc_type::Address Address;
1428 typedef typename Output_reloc_type::Addend Addend;
1430 Output_data_reloc(bool sr)
1431 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1434 // Add a reloc against a global symbol.
1437 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1438 Address address, Addend addend)
1439 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1443 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1444 Sized_relobj<size, big_endian>* relobj,
1445 unsigned int shndx, Address address,
1447 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1450 // Add a RELATIVE reloc against a global symbol. The final output
1451 // relocation will not reference the symbol, but we must keep the symbol
1452 // information long enough to set the addend of the relocation correctly
1453 // when it is written.
1456 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1457 Address address, Addend addend)
1458 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1461 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1462 Sized_relobj<size, big_endian>* relobj,
1463 unsigned int shndx, Address address, Addend addend)
1464 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1467 // Add a reloc against a local symbol.
1470 add_local(Sized_relobj<size, big_endian>* relobj,
1471 unsigned int local_sym_index, unsigned int type,
1472 Output_data* od, Address address, Addend addend)
1474 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1475 addend, false, false));
1479 add_local(Sized_relobj<size, big_endian>* relobj,
1480 unsigned int local_sym_index, unsigned int type,
1481 Output_data* od, unsigned int shndx, Address address,
1484 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1485 address, addend, false, false));
1488 // Add a RELATIVE reloc against a local symbol.
1491 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1492 unsigned int local_sym_index, unsigned int type,
1493 Output_data* od, Address address, Addend addend)
1495 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1496 addend, true, false));
1500 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1501 unsigned int local_sym_index, unsigned int type,
1502 Output_data* od, unsigned int shndx, Address address,
1505 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1506 address, addend, true, false));
1509 // Add a reloc against a local section symbol. This will be
1510 // converted into a reloc against the STT_SECTION symbol of the
1514 add_local_section(Sized_relobj<size, big_endian>* relobj,
1515 unsigned int input_shndx, unsigned int type,
1516 Output_data* od, Address address, Addend addend)
1518 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1519 addend, false, true));
1523 add_local_section(Sized_relobj<size, big_endian>* relobj,
1524 unsigned int input_shndx, unsigned int type,
1525 Output_data* od, unsigned int shndx, Address address,
1528 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1529 address, addend, false, true));
1532 // A reloc against the STT_SECTION symbol of an output section.
1535 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1536 Address address, Addend addend)
1537 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1540 add_output_section(Output_section* os, unsigned int type,
1541 Sized_relobj<size, big_endian>* relobj,
1542 unsigned int shndx, Address address, Addend addend)
1543 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1547 // Output_relocatable_relocs represents a relocation section in a
1548 // relocatable link. The actual data is written out in the target
1549 // hook relocate_for_relocatable. This just saves space for it.
1551 template<int sh_type, int size, bool big_endian>
1552 class Output_relocatable_relocs : public Output_section_data
1555 Output_relocatable_relocs(Relocatable_relocs* rr)
1556 : Output_section_data(Output_data::default_alignment_for_size(size)),
1561 set_final_data_size();
1563 // Write out the data. There is nothing to do here.
1565 do_write(Output_file*)
1568 // Write to a map file.
1570 do_print_to_mapfile(Mapfile* mapfile) const
1571 { mapfile->print_output_data(this, _("** relocs")); }
1574 // The relocs associated with this input section.
1575 Relocatable_relocs* rr_;
1578 // Handle a GROUP section.
1580 template<int size, bool big_endian>
1581 class Output_data_group : public Output_section_data
1584 // The constructor clears *INPUT_SHNDXES.
1585 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1586 section_size_type entry_count,
1587 elfcpp::Elf_Word flags,
1588 std::vector<unsigned int>* input_shndxes);
1591 do_write(Output_file*);
1593 // Write to a map file.
1595 do_print_to_mapfile(Mapfile* mapfile) const
1596 { mapfile->print_output_data(this, _("** group")); }
1598 // Set final data size.
1600 set_final_data_size()
1601 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1604 // The input object.
1605 Sized_relobj<size, big_endian>* relobj_;
1606 // The group flag word.
1607 elfcpp::Elf_Word flags_;
1608 // The section indexes of the input sections in this group.
1609 std::vector<unsigned int> input_shndxes_;
1612 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1613 // for one symbol--either a global symbol or a local symbol in an
1614 // object. The target specific code adds entries to the GOT as
1617 template<int size, bool big_endian>
1618 class Output_data_got : public Output_section_data_build
1621 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1622 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1623 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1626 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1630 // Add an entry for a global symbol to the GOT. Return true if this
1631 // is a new GOT entry, false if the symbol was already in the GOT.
1633 add_global(Symbol* gsym, unsigned int got_type);
1635 // Add an entry for a global symbol to the GOT, and add a dynamic
1636 // relocation of type R_TYPE for the GOT entry.
1638 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1639 Rel_dyn* rel_dyn, unsigned int r_type);
1642 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1643 Rela_dyn* rela_dyn, unsigned int r_type);
1645 // Add a pair of entries for a global symbol to the GOT, and add
1646 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1648 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1649 Rel_dyn* rel_dyn, unsigned int r_type_1,
1650 unsigned int r_type_2);
1653 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1654 Rela_dyn* rela_dyn, unsigned int r_type_1,
1655 unsigned int r_type_2);
1657 // Add an entry for a local symbol to the GOT. This returns true if
1658 // this is a new GOT entry, false if the symbol already has a GOT
1661 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1662 unsigned int got_type);
1664 // Add an entry for a local symbol to the GOT, and add a dynamic
1665 // relocation of type R_TYPE for the GOT entry.
1667 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1668 unsigned int sym_index, unsigned int got_type,
1669 Rel_dyn* rel_dyn, unsigned int r_type);
1672 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1673 unsigned int sym_index, unsigned int got_type,
1674 Rela_dyn* rela_dyn, unsigned int r_type);
1676 // Add a pair of entries for a local symbol to the GOT, and add
1677 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1679 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1680 unsigned int sym_index, unsigned int shndx,
1681 unsigned int got_type, Rel_dyn* rel_dyn,
1682 unsigned int r_type_1, unsigned int r_type_2);
1685 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1686 unsigned int sym_index, unsigned int shndx,
1687 unsigned int got_type, Rela_dyn* rela_dyn,
1688 unsigned int r_type_1, unsigned int r_type_2);
1690 // Add a constant to the GOT. This returns the offset of the new
1691 // entry from the start of the GOT.
1693 add_constant(Valtype constant)
1695 this->entries_.push_back(Got_entry(constant));
1696 this->set_got_size();
1697 return this->last_got_offset();
1701 // Write out the GOT table.
1703 do_write(Output_file*);
1705 // Write to a map file.
1707 do_print_to_mapfile(Mapfile* mapfile) const
1708 { mapfile->print_output_data(this, _("** GOT")); }
1711 // This POD class holds a single GOT entry.
1715 // Create a zero entry.
1717 : local_sym_index_(CONSTANT_CODE)
1718 { this->u_.constant = 0; }
1720 // Create a global symbol entry.
1721 explicit Got_entry(Symbol* gsym)
1722 : local_sym_index_(GSYM_CODE)
1723 { this->u_.gsym = gsym; }
1725 // Create a local symbol entry.
1726 Got_entry(Sized_relobj<size, big_endian>* object,
1727 unsigned int local_sym_index)
1728 : local_sym_index_(local_sym_index)
1730 gold_assert(local_sym_index != GSYM_CODE
1731 && local_sym_index != CONSTANT_CODE);
1732 this->u_.object = object;
1735 // Create a constant entry. The constant is a host value--it will
1736 // be swapped, if necessary, when it is written out.
1737 explicit Got_entry(Valtype constant)
1738 : local_sym_index_(CONSTANT_CODE)
1739 { this->u_.constant = constant; }
1741 // Write the GOT entry to an output view.
1743 write(unsigned char* pov) const;
1754 // For a local symbol, the object.
1755 Sized_relobj<size, big_endian>* object;
1756 // For a global symbol, the symbol.
1758 // For a constant, the constant.
1761 // For a local symbol, the local symbol index. This is GSYM_CODE
1762 // for a global symbol, or CONSTANT_CODE for a constant.
1763 unsigned int local_sym_index_;
1766 typedef std::vector<Got_entry> Got_entries;
1768 // Return the offset into the GOT of GOT entry I.
1770 got_offset(unsigned int i) const
1771 { return i * (size / 8); }
1773 // Return the offset into the GOT of the last entry added.
1775 last_got_offset() const
1776 { return this->got_offset(this->entries_.size() - 1); }
1778 // Set the size of the section.
1781 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1783 // The list of GOT entries.
1784 Got_entries entries_;
1787 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1790 class Output_data_dynamic : public Output_section_data
1793 Output_data_dynamic(Stringpool* pool)
1794 : Output_section_data(Output_data::default_alignment()),
1795 entries_(), pool_(pool)
1798 // Add a new dynamic entry with a fixed numeric value.
1800 add_constant(elfcpp::DT tag, unsigned int val)
1801 { this->add_entry(Dynamic_entry(tag, val)); }
1803 // Add a new dynamic entry with the address of output data.
1805 add_section_address(elfcpp::DT tag, const Output_data* od)
1806 { this->add_entry(Dynamic_entry(tag, od, false)); }
1808 // Add a new dynamic entry with the address of output data
1809 // plus a constant offset.
1811 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1812 unsigned int offset)
1813 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1815 // Add a new dynamic entry with the size of output data.
1817 add_section_size(elfcpp::DT tag, const Output_data* od)
1818 { this->add_entry(Dynamic_entry(tag, od, true)); }
1820 // Add a new dynamic entry with the address of a symbol.
1822 add_symbol(elfcpp::DT tag, const Symbol* sym)
1823 { this->add_entry(Dynamic_entry(tag, sym)); }
1825 // Add a new dynamic entry with a string.
1827 add_string(elfcpp::DT tag, const char* str)
1828 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1831 add_string(elfcpp::DT tag, const std::string& str)
1832 { this->add_string(tag, str.c_str()); }
1835 // Adjust the output section to set the entry size.
1837 do_adjust_output_section(Output_section*);
1839 // Set the final data size.
1841 set_final_data_size();
1843 // Write out the dynamic entries.
1845 do_write(Output_file*);
1847 // Write to a map file.
1849 do_print_to_mapfile(Mapfile* mapfile) const
1850 { mapfile->print_output_data(this, _("** dynamic")); }
1853 // This POD class holds a single dynamic entry.
1857 // Create an entry with a fixed numeric value.
1858 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1859 : tag_(tag), offset_(DYNAMIC_NUMBER)
1860 { this->u_.val = val; }
1862 // Create an entry with the size or address of a section.
1863 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1865 offset_(section_size
1866 ? DYNAMIC_SECTION_SIZE
1867 : DYNAMIC_SECTION_ADDRESS)
1868 { this->u_.od = od; }
1870 // Create an entry with the address of a section plus a constant offset.
1871 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1874 { this->u_.od = od; }
1876 // Create an entry with the address of a symbol.
1877 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1878 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1879 { this->u_.sym = sym; }
1881 // Create an entry with a string.
1882 Dynamic_entry(elfcpp::DT tag, const char* str)
1883 : tag_(tag), offset_(DYNAMIC_STRING)
1884 { this->u_.str = str; }
1886 // Return the tag of this entry.
1889 { return this->tag_; }
1891 // Write the dynamic entry to an output view.
1892 template<int size, bool big_endian>
1894 write(unsigned char* pov, const Stringpool*) const;
1897 // Classification is encoded in the OFFSET field.
1901 DYNAMIC_SECTION_ADDRESS = 0,
1903 DYNAMIC_NUMBER = -1U,
1905 DYNAMIC_SECTION_SIZE = -2U,
1907 DYNAMIC_SYMBOL = -3U,
1909 DYNAMIC_STRING = -4U
1910 // Any other value indicates a section address plus OFFSET.
1915 // For DYNAMIC_NUMBER.
1917 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1918 const Output_data* od;
1919 // For DYNAMIC_SYMBOL.
1921 // For DYNAMIC_STRING.
1926 // The type of entry (Classification) or offset within a section.
1927 unsigned int offset_;
1930 // Add an entry to the list.
1932 add_entry(const Dynamic_entry& entry)
1933 { this->entries_.push_back(entry); }
1935 // Sized version of write function.
1936 template<int size, bool big_endian>
1938 sized_write(Output_file* of);
1940 // The type of the list of entries.
1941 typedef std::vector<Dynamic_entry> Dynamic_entries;
1944 Dynamic_entries entries_;
1945 // The pool used for strings.
1949 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1950 // which may be required if the object file has more than
1951 // SHN_LORESERVE sections.
1953 class Output_symtab_xindex : public Output_section_data
1956 Output_symtab_xindex(size_t symcount)
1957 : Output_section_data(symcount * 4, 4, true),
1961 // Add an entry: symbol number SYMNDX has section SHNDX.
1963 add(unsigned int symndx, unsigned int shndx)
1964 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
1968 do_write(Output_file*);
1970 // Write to a map file.
1972 do_print_to_mapfile(Mapfile* mapfile) const
1973 { mapfile->print_output_data(this, _("** symtab xindex")); }
1976 template<bool big_endian>
1978 endian_do_write(unsigned char*);
1980 // It is likely that most symbols will not require entries. Rather
1981 // than keep a vector for all symbols, we keep pairs of symbol index
1982 // and section index.
1983 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
1985 // The entries we need.
1986 Xindex_entries entries_;
1989 // A relaxed input section.
1990 class Output_relaxed_input_section : public Output_section_data
1993 // We would like to call relobj->section_addralign(shndx) to get the
1994 // alignment but we do not want the constructor to fail. So callers
1995 // are repsonsible for ensuring that.
1996 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
1998 : Output_section_data(addralign), relobj_(relobj), shndx_(shndx)
2001 // Return the Relobj of this relaxed input section.
2004 { return this->relobj_; }
2006 // Return the section index of this relaxed input section.
2009 { return this->shndx_; }
2013 unsigned int shndx_;
2016 // An output section. We don't expect to have too many output
2017 // sections, so we don't bother to do a template on the size.
2019 class Output_section : public Output_data
2022 // Create an output section, giving the name, type, and flags.
2023 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2024 virtual ~Output_section();
2026 // Add a new input section SHNDX, named NAME, with header SHDR, from
2027 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2028 // which applies to this section, or 0 if none, or -1 if more than
2029 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2030 // in a linker script; in that case we need to keep track of input
2031 // sections associated with an output section. Return the offset
2032 // within the output section.
2033 template<int size, bool big_endian>
2035 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2037 const elfcpp::Shdr<size, big_endian>& shdr,
2038 unsigned int reloc_shndx, bool have_sections_script);
2040 // Add generated data POSD to this output section.
2042 add_output_section_data(Output_section_data* posd);
2044 // Return the section name.
2047 { return this->name_; }
2049 // Return the section type.
2052 { return this->type_; }
2054 // Return the section flags.
2057 { return this->flags_; }
2059 // Update the output section flags based on input section flags.
2061 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2063 // Return the entsize field.
2066 { return this->entsize_; }
2068 // Set the entsize field.
2070 set_entsize(uint64_t v);
2072 // Set the load address.
2074 set_load_address(uint64_t load_address)
2076 this->load_address_ = load_address;
2077 this->has_load_address_ = true;
2080 // Set the link field to the output section index of a section.
2082 set_link_section(const Output_data* od)
2084 gold_assert(this->link_ == 0
2085 && !this->should_link_to_symtab_
2086 && !this->should_link_to_dynsym_);
2087 this->link_section_ = od;
2090 // Set the link field to a constant.
2092 set_link(unsigned int v)
2094 gold_assert(this->link_section_ == NULL
2095 && !this->should_link_to_symtab_
2096 && !this->should_link_to_dynsym_);
2100 // Record that this section should link to the normal symbol table.
2102 set_should_link_to_symtab()
2104 gold_assert(this->link_section_ == NULL
2106 && !this->should_link_to_dynsym_);
2107 this->should_link_to_symtab_ = true;
2110 // Record that this section should link to the dynamic symbol table.
2112 set_should_link_to_dynsym()
2114 gold_assert(this->link_section_ == NULL
2116 && !this->should_link_to_symtab_);
2117 this->should_link_to_dynsym_ = true;
2120 // Return the info field.
2124 gold_assert(this->info_section_ == NULL
2125 && this->info_symndx_ == NULL);
2129 // Set the info field to the output section index of a section.
2131 set_info_section(const Output_section* os)
2133 gold_assert((this->info_section_ == NULL
2134 || (this->info_section_ == os
2135 && this->info_uses_section_index_))
2136 && this->info_symndx_ == NULL
2137 && this->info_ == 0);
2138 this->info_section_ = os;
2139 this->info_uses_section_index_= true;
2142 // Set the info field to the symbol table index of a symbol.
2144 set_info_symndx(const Symbol* sym)
2146 gold_assert(this->info_section_ == NULL
2147 && (this->info_symndx_ == NULL
2148 || this->info_symndx_ == sym)
2149 && this->info_ == 0);
2150 this->info_symndx_ = sym;
2153 // Set the info field to the symbol table index of a section symbol.
2155 set_info_section_symndx(const Output_section* os)
2157 gold_assert((this->info_section_ == NULL
2158 || (this->info_section_ == os
2159 && !this->info_uses_section_index_))
2160 && this->info_symndx_ == NULL
2161 && this->info_ == 0);
2162 this->info_section_ = os;
2163 this->info_uses_section_index_ = false;
2166 // Set the info field to a constant.
2168 set_info(unsigned int v)
2170 gold_assert(this->info_section_ == NULL
2171 && this->info_symndx_ == NULL
2172 && (this->info_ == 0
2173 || this->info_ == v));
2177 // Set the addralign field.
2179 set_addralign(uint64_t v)
2180 { this->addralign_ = v; }
2182 // Whether the output section index has been set.
2184 has_out_shndx() const
2185 { return this->out_shndx_ != -1U; }
2187 // Indicate that we need a symtab index.
2189 set_needs_symtab_index()
2190 { this->needs_symtab_index_ = true; }
2192 // Return whether we need a symtab index.
2194 needs_symtab_index() const
2195 { return this->needs_symtab_index_; }
2197 // Get the symtab index.
2199 symtab_index() const
2201 gold_assert(this->symtab_index_ != 0);
2202 return this->symtab_index_;
2205 // Set the symtab index.
2207 set_symtab_index(unsigned int index)
2209 gold_assert(index != 0);
2210 this->symtab_index_ = index;
2213 // Indicate that we need a dynsym index.
2215 set_needs_dynsym_index()
2216 { this->needs_dynsym_index_ = true; }
2218 // Return whether we need a dynsym index.
2220 needs_dynsym_index() const
2221 { return this->needs_dynsym_index_; }
2223 // Get the dynsym index.
2225 dynsym_index() const
2227 gold_assert(this->dynsym_index_ != 0);
2228 return this->dynsym_index_;
2231 // Set the dynsym index.
2233 set_dynsym_index(unsigned int index)
2235 gold_assert(index != 0);
2236 this->dynsym_index_ = index;
2239 // Return whether the input sections sections attachd to this output
2240 // section may require sorting. This is used to handle constructor
2241 // priorities compatibly with GNU ld.
2243 may_sort_attached_input_sections() const
2244 { return this->may_sort_attached_input_sections_; }
2246 // Record that the input sections attached to this output section
2247 // may require sorting.
2249 set_may_sort_attached_input_sections()
2250 { this->may_sort_attached_input_sections_ = true; }
2252 // Return whether the input sections attached to this output section
2253 // require sorting. This is used to handle constructor priorities
2254 // compatibly with GNU ld.
2256 must_sort_attached_input_sections() const
2257 { return this->must_sort_attached_input_sections_; }
2259 // Record that the input sections attached to this output section
2262 set_must_sort_attached_input_sections()
2263 { this->must_sort_attached_input_sections_ = true; }
2265 // Return whether this section holds relro data--data which has
2266 // dynamic relocations but which may be marked read-only after the
2267 // dynamic relocations have been completed.
2270 { return this->is_relro_; }
2272 // Record that this section holds relro data.
2275 { this->is_relro_ = true; }
2277 // Record that this section does not hold relro data.
2280 { this->is_relro_ = false; }
2282 // True if this section holds relro local data--relro data for which
2283 // the dynamic relocations are all RELATIVE relocations.
2285 is_relro_local() const
2286 { return this->is_relro_local_; }
2288 // Record that this section holds relro local data.
2290 set_is_relro_local()
2291 { this->is_relro_local_ = true; }
2293 // True if this is a small section: a section which holds small
2296 is_small_section() const
2297 { return this->is_small_section_; }
2299 // Record that this is a small section.
2301 set_is_small_section()
2302 { this->is_small_section_ = true; }
2304 // True if this is a large section: a section which holds large
2307 is_large_section() const
2308 { return this->is_large_section_; }
2310 // Record that this is a large section.
2312 set_is_large_section()
2313 { this->is_large_section_ = true; }
2315 // True if this is a large data (not BSS) section.
2317 is_large_data_section()
2318 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2320 // Return whether this section should be written after all the input
2321 // sections are complete.
2323 after_input_sections() const
2324 { return this->after_input_sections_; }
2326 // Record that this section should be written after all the input
2327 // sections are complete.
2329 set_after_input_sections()
2330 { this->after_input_sections_ = true; }
2332 // Return whether this section requires postprocessing after all
2333 // relocations have been applied.
2335 requires_postprocessing() const
2336 { return this->requires_postprocessing_; }
2338 // If a section requires postprocessing, return the buffer to use.
2340 postprocessing_buffer() const
2342 gold_assert(this->postprocessing_buffer_ != NULL);
2343 return this->postprocessing_buffer_;
2346 // If a section requires postprocessing, create the buffer to use.
2348 create_postprocessing_buffer();
2350 // If a section requires postprocessing, this is the size of the
2351 // buffer to which relocations should be applied.
2353 postprocessing_buffer_size() const
2354 { return this->current_data_size_for_child(); }
2356 // Modify the section name. This is only permitted for an
2357 // unallocated section, and only before the size has been finalized.
2358 // Otherwise the name will not get into Layout::namepool_.
2360 set_name(const char* newname)
2362 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2363 gold_assert(!this->is_data_size_valid());
2364 this->name_ = newname;
2367 // Return whether the offset OFFSET in the input section SHNDX in
2368 // object OBJECT is being included in the link.
2370 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2371 off_t offset) const;
2373 // Return the offset within the output section of OFFSET relative to
2374 // the start of input section SHNDX in object OBJECT.
2376 output_offset(const Relobj* object, unsigned int shndx,
2377 section_offset_type offset) const;
2379 // Return the output virtual address of OFFSET relative to the start
2380 // of input section SHNDX in object OBJECT.
2382 output_address(const Relobj* object, unsigned int shndx,
2383 off_t offset) const;
2385 // Look for the merged section for input section SHNDX in object
2386 // OBJECT. If found, return true, and set *ADDR to the address of
2387 // the start of the merged section. This is not necessary the
2388 // output offset corresponding to input offset 0 in the section,
2389 // since the section may be mapped arbitrarily.
2391 find_starting_output_address(const Relobj* object, unsigned int shndx,
2392 uint64_t* addr) const;
2394 // Record that this output section was found in the SECTIONS clause
2395 // of a linker script.
2397 set_found_in_sections_clause()
2398 { this->found_in_sections_clause_ = true; }
2400 // Return whether this output section was found in the SECTIONS
2401 // clause of a linker script.
2403 found_in_sections_clause() const
2404 { return this->found_in_sections_clause_; }
2406 // Write the section header into *OPHDR.
2407 template<int size, bool big_endian>
2409 write_header(const Layout*, const Stringpool*,
2410 elfcpp::Shdr_write<size, big_endian>*) const;
2412 // The next few calls are for linker script support.
2414 // We need to export the input sections to linker scripts. Previously
2415 // we export a pair of Relobj pointer and section index. We now need to
2416 // handle relaxed input sections as well. So we use this class.
2417 class Simple_input_section
2420 static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2423 Simple_input_section(Relobj *relobj, unsigned int shndx)
2426 gold_assert(shndx != invalid_shndx);
2427 this->u_.relobj = relobj;
2430 Simple_input_section(Output_relaxed_input_section* section)
2431 : shndx_(invalid_shndx)
2432 { this->u_.relaxed_input_section = section; }
2434 // Whether this is a relaxed section.
2436 is_relaxed_input_section() const
2437 { return this->shndx_ == invalid_shndx; }
2439 // Return object of an input section.
2443 return ((this->shndx_ != invalid_shndx)
2445 : this->u_.relaxed_input_section->relobj());
2448 // Return index of an input section.
2452 return ((this->shndx_ != invalid_shndx)
2454 : this->u_.relaxed_input_section->shndx());
2457 // Return the Output_relaxed_input_section object of a relaxed section.
2458 Output_relaxed_input_section*
2459 relaxed_input_section() const
2461 gold_assert(this->shndx_ == invalid_shndx);
2462 return this->u_.relaxed_input_section;
2466 // Pointer to either an Relobj or an Output_relaxed_input_section.
2470 Output_relaxed_input_section* relaxed_input_section;
2472 // Section index for an non-relaxed section or invalid_shndx for
2473 // a relaxed section.
2474 unsigned int shndx_;
2477 // Store the list of input sections for this Output_section into the
2478 // list passed in. This removes the input sections, leaving only
2479 // any Output_section_data elements. This returns the size of those
2480 // Output_section_data elements. ADDRESS is the address of this
2481 // output section. FILL is the fill value to use, in case there are
2482 // any spaces between the remaining Output_section_data elements.
2484 get_input_sections(uint64_t address, const std::string& fill,
2485 std::list<Simple_input_section>*);
2487 // Add an input section from a script.
2489 add_input_section_for_script(const Simple_input_section& input_section,
2490 off_t data_size, uint64_t addralign);
2492 // Set the current size of the output section.
2494 set_current_data_size(off_t size)
2495 { this->set_current_data_size_for_child(size); }
2497 // Get the current size of the output section.
2499 current_data_size() const
2500 { return this->current_data_size_for_child(); }
2502 // End of linker script support.
2504 // Save states before doing section layout.
2505 // This is used for relaxation.
2509 // Restore states prior to section layout.
2513 // Print merge statistics to stderr.
2515 print_merge_stats();
2518 // Return the output section--i.e., the object itself.
2523 // Return the section index in the output file.
2525 do_out_shndx() const
2527 gold_assert(this->out_shndx_ != -1U);
2528 return this->out_shndx_;
2531 // Set the output section index.
2533 do_set_out_shndx(unsigned int shndx)
2535 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2536 this->out_shndx_ = shndx;
2539 // Set the final data size of the Output_section. For a typical
2540 // Output_section, there is nothing to do, but if there are any
2541 // Output_section_data objects we need to set their final addresses
2544 set_final_data_size();
2546 // Reset the address and file offset.
2548 do_reset_address_and_file_offset();
2550 // Return true if address and file offset already have reset values. In
2551 // other words, calling reset_address_and_file_offset will not change them.
2553 do_address_and_file_offset_have_reset_values() const;
2555 // Write the data to the file. For a typical Output_section, this
2556 // does nothing: the data is written out by calling Object::Relocate
2557 // on each input object. But if there are any Output_section_data
2558 // objects we do need to write them out here.
2560 do_write(Output_file*);
2562 // Return the address alignment--function required by parent class.
2564 do_addralign() const
2565 { return this->addralign_; }
2567 // Return whether there is a load address.
2569 do_has_load_address() const
2570 { return this->has_load_address_; }
2572 // Return the load address.
2574 do_load_address() const
2576 gold_assert(this->has_load_address_);
2577 return this->load_address_;
2580 // Return whether this is an Output_section.
2582 do_is_section() const
2585 // Return whether this is a section of the specified type.
2587 do_is_section_type(elfcpp::Elf_Word type) const
2588 { return this->type_ == type; }
2590 // Return whether the specified section flag is set.
2592 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2593 { return (this->flags_ & flag) != 0; }
2595 // Set the TLS offset. Called only for SHT_TLS sections.
2597 do_set_tls_offset(uint64_t tls_base);
2599 // Return the TLS offset, relative to the base of the TLS segment.
2600 // Valid only for SHT_TLS sections.
2602 do_tls_offset() const
2603 { return this->tls_offset_; }
2605 // This may be implemented by a child class.
2607 do_finalize_name(Layout*)
2610 // Print to the map file.
2612 do_print_to_mapfile(Mapfile*) const;
2614 // Record that this section requires postprocessing after all
2615 // relocations have been applied. This is called by a child class.
2617 set_requires_postprocessing()
2619 this->requires_postprocessing_ = true;
2620 this->after_input_sections_ = true;
2623 // Write all the data of an Output_section into the postprocessing
2626 write_to_postprocessing_buffer();
2629 // In some cases we need to keep a list of the input sections
2630 // associated with this output section. We only need the list if we
2631 // might have to change the offsets of the input section within the
2632 // output section after we add the input section. The ordinary
2633 // input sections will be written out when we process the object
2634 // file, and as such we don't need to track them here. We do need
2635 // to track Output_section_data objects here. We store instances of
2636 // this structure in a std::vector, so it must be a POD. There can
2637 // be many instances of this structure, so we use a union to save
2643 : shndx_(0), p2align_(0)
2645 this->u1_.data_size = 0;
2646 this->u2_.object = NULL;
2649 // For an ordinary input section.
2650 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2653 p2align_(ffsll(static_cast<long long>(addralign)))
2655 gold_assert(shndx != OUTPUT_SECTION_CODE
2656 && shndx != MERGE_DATA_SECTION_CODE
2657 && shndx != MERGE_STRING_SECTION_CODE
2658 && shndx != RELAXED_INPUT_SECTION_CODE);
2659 this->u1_.data_size = data_size;
2660 this->u2_.object = object;
2663 // For a non-merge output section.
2664 Input_section(Output_section_data* posd)
2665 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2667 this->u1_.data_size = 0;
2668 this->u2_.posd = posd;
2671 // For a merge section.
2672 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2674 ? MERGE_STRING_SECTION_CODE
2675 : MERGE_DATA_SECTION_CODE),
2678 this->u1_.entsize = entsize;
2679 this->u2_.posd = posd;
2682 // For a relaxed input section.
2683 Input_section(Output_relaxed_input_section *psection)
2684 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2686 this->u1_.data_size = 0;
2687 this->u2_.poris = psection;
2690 // The required alignment.
2694 if (!this->is_input_section())
2695 return this->u2_.posd->addralign();
2696 return (this->p2align_ == 0
2698 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2701 // Return the required size.
2705 // Whether this is an input section.
2707 is_input_section() const
2709 return (this->shndx_ != OUTPUT_SECTION_CODE
2710 && this->shndx_ != MERGE_DATA_SECTION_CODE
2711 && this->shndx_ != MERGE_STRING_SECTION_CODE
2712 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
2715 // Return whether this is a merge section which matches the
2718 is_merge_section(bool is_string, uint64_t entsize,
2719 uint64_t addralign) const
2721 return (this->shndx_ == (is_string
2722 ? MERGE_STRING_SECTION_CODE
2723 : MERGE_DATA_SECTION_CODE)
2724 && this->u1_.entsize == entsize
2725 && this->addralign() == addralign);
2728 // Return whether this is a relaxed input section.
2730 is_relaxed_input_section() const
2731 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2733 // Return whether this is a generic Output_section_data.
2735 is_output_section_data() const
2737 return this->shndx_ == OUTPUT_SECTION_CODE;
2740 // Return the object for an input section.
2744 gold_assert(this->is_input_section());
2745 return this->u2_.object;
2748 // Return the input section index for an input section.
2752 gold_assert(this->is_input_section());
2753 return this->shndx_;
2756 // For non-input-sections, return the associated Output_section_data
2758 Output_section_data*
2759 output_section_data() const
2761 gold_assert(!this->is_input_section());
2762 return this->u2_.posd;
2765 // Return the Output_relaxed_input_section object.
2766 Output_relaxed_input_section*
2767 relaxed_input_section() const
2769 gold_assert(this->is_relaxed_input_section());
2770 return this->u2_.poris;
2773 // Set the output section.
2775 set_output_section(Output_section* os)
2777 gold_assert(!this->is_input_section());
2778 Output_section_data *posd =
2779 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2780 posd->set_output_section(os);
2783 // Set the address and file offset. This is called during
2784 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2785 // the enclosing section.
2787 set_address_and_file_offset(uint64_t address, off_t file_offset,
2788 off_t section_file_offset);
2790 // Reset the address and file offset.
2792 reset_address_and_file_offset();
2794 // Finalize the data size.
2796 finalize_data_size();
2798 // Add an input section, for SHF_MERGE sections.
2800 add_input_section(Relobj* object, unsigned int shndx)
2802 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2803 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2804 return this->u2_.posd->add_input_section(object, shndx);
2807 // Given an input OBJECT, an input section index SHNDX within that
2808 // object, and an OFFSET relative to the start of that input
2809 // section, return whether or not the output offset is known. If
2810 // this function returns true, it sets *POUTPUT to the offset in
2811 // the output section, relative to the start of the input section
2812 // in the output section. *POUTPUT may be different from OFFSET
2813 // for a merged section.
2815 output_offset(const Relobj* object, unsigned int shndx,
2816 section_offset_type offset,
2817 section_offset_type *poutput) const;
2819 // Return whether this is the merge section for the input section
2822 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2824 // Write out the data. This does nothing for an input section.
2826 write(Output_file*);
2828 // Write the data to a buffer. This does nothing for an input
2831 write_to_buffer(unsigned char*);
2833 // Print to a map file.
2835 print_to_mapfile(Mapfile*) const;
2837 // Print statistics about merge sections to stderr.
2839 print_merge_stats(const char* section_name)
2841 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2842 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2843 this->u2_.posd->print_merge_stats(section_name);
2847 // Code values which appear in shndx_. If the value is not one of
2848 // these codes, it is the input section index in the object file.
2851 // An Output_section_data.
2852 OUTPUT_SECTION_CODE = -1U,
2853 // An Output_section_data for an SHF_MERGE section with
2854 // SHF_STRINGS not set.
2855 MERGE_DATA_SECTION_CODE = -2U,
2856 // An Output_section_data for an SHF_MERGE section with
2858 MERGE_STRING_SECTION_CODE = -3U,
2859 // An Output_section_data for a relaxed input section.
2860 RELAXED_INPUT_SECTION_CODE = -4U
2863 // For an ordinary input section, this is the section index in the
2864 // input file. For an Output_section_data, this is
2865 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2866 // MERGE_STRING_SECTION_CODE.
2867 unsigned int shndx_;
2868 // The required alignment, stored as a power of 2.
2869 unsigned int p2align_;
2872 // For an ordinary input section, the section size.
2874 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2875 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2881 // For an ordinary input section, the object which holds the
2884 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2885 // MERGE_STRING_SECTION_CODE, the data.
2886 Output_section_data* posd;
2887 // For RELAXED_INPUT_SECTION_CODE, the data.
2888 Output_relaxed_input_section* poris;
2892 typedef std::vector<Input_section> Input_section_list;
2894 // We only save enough information to undo the effects of section layout.
2895 class Checkpoint_output_section
2898 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
2899 const Input_section_list& input_sections,
2900 off_t first_input_offset,
2901 bool attached_input_sections_are_sorted)
2902 : addralign_(addralign), flags_(flags),
2903 input_sections_(input_sections),
2904 input_sections_size_(input_sections_.size()),
2905 input_sections_copy_(), first_input_offset_(first_input_offset),
2906 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
2910 ~Checkpoint_output_section()
2913 // Return the address alignment.
2916 { return this->addralign_; }
2918 // Return the section flags.
2921 { return this->flags_; }
2923 // Return a reference to the input section list copy.
2924 const Input_section_list&
2925 input_sections() const
2926 { return this->input_sections_copy_; }
2928 // Return the size of input_sections at the time when checkpoint is
2931 input_sections_size() const
2932 { return this->input_sections_size_; }
2934 // Whether input sections are copied.
2936 input_sections_saved() const
2937 { return this->input_sections_copy_.size() == this->input_sections_size_; }
2940 first_input_offset() const
2941 { return this->first_input_offset_; }
2944 attached_input_sections_are_sorted() const
2945 { return this->attached_input_sections_are_sorted_; }
2947 // Save input sections.
2949 save_input_sections()
2951 this->input_sections_copy_.reserve(this->input_sections_size_);
2952 this->input_sections_copy_.clear();
2953 Input_section_list::const_iterator p = this->input_sections_.begin();
2954 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
2955 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
2956 this->input_sections_copy_.push_back(*p);
2960 // The section alignment.
2961 uint64_t addralign_;
2962 // The section flags.
2963 elfcpp::Elf_Xword flags_;
2964 // Reference to the input sections to be checkpointed.
2965 const Input_section_list& input_sections_;
2966 // Size of the checkpointed portion of input_sections_;
2967 size_t input_sections_size_;
2968 // Copy of input sections.
2969 Input_section_list input_sections_copy_;
2970 // The offset of the first entry in input_sections_.
2971 off_t first_input_offset_;
2972 // True if the input sections attached to this output section have
2973 // already been sorted.
2974 bool attached_input_sections_are_sorted_;
2978 // This class is used to sort the input sections.
2979 class Input_section_sort_entry;
2981 // This is the sort comparison function.
2982 struct Input_section_sort_compare
2985 operator()(const Input_section_sort_entry&,
2986 const Input_section_sort_entry&) const;
2989 // Fill data. This is used to fill in data between input sections.
2990 // It is also used for data statements (BYTE, WORD, etc.) in linker
2991 // scripts. When we have to keep track of the input sections, we
2992 // can use an Output_data_const, but we don't want to have to keep
2993 // track of input sections just to implement fills.
2997 Fill(off_t section_offset, off_t length)
2998 : section_offset_(section_offset),
2999 length_(convert_to_section_size_type(length))
3002 // Return section offset.
3004 section_offset() const
3005 { return this->section_offset_; }
3007 // Return fill length.
3010 { return this->length_; }
3013 // The offset within the output section.
3014 off_t section_offset_;
3015 // The length of the space to fill.
3016 section_size_type length_;
3019 typedef std::vector<Fill> Fill_list;
3021 // Add a new output section by Input_section.
3023 add_output_section_data(Input_section*);
3025 // Add an SHF_MERGE input section. Returns true if the section was
3028 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3029 uint64_t entsize, uint64_t addralign);
3031 // Add an output SHF_MERGE section POSD to this output section.
3032 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3033 // ENTSIZE is the entity size. This returns the entry added to
3036 add_output_merge_section(Output_section_data* posd, bool is_string,
3039 // Relax an existing input section.
3041 relax_input_section(Output_relaxed_input_section*);
3043 // Sort the attached input sections.
3045 sort_attached_input_sections();
3047 // Most of these fields are only valid after layout.
3049 // The name of the section. This will point into a Stringpool.
3051 // The section address is in the parent class.
3052 // The section alignment.
3053 uint64_t addralign_;
3054 // The section entry size.
3056 // The load address. This is only used when using a linker script
3057 // with a SECTIONS clause. The has_load_address_ field indicates
3058 // whether this field is valid.
3059 uint64_t load_address_;
3060 // The file offset is in the parent class.
3061 // Set the section link field to the index of this section.
3062 const Output_data* link_section_;
3063 // If link_section_ is NULL, this is the link field.
3065 // Set the section info field to the index of this section.
3066 const Output_section* info_section_;
3067 // If info_section_ is NULL, set the info field to the symbol table
3068 // index of this symbol.
3069 const Symbol* info_symndx_;
3070 // If info_section_ and info_symndx_ are NULL, this is the section
3073 // The section type.
3074 const elfcpp::Elf_Word type_;
3075 // The section flags.
3076 elfcpp::Elf_Xword flags_;
3077 // The section index.
3078 unsigned int out_shndx_;
3079 // If there is a STT_SECTION for this output section in the normal
3080 // symbol table, this is the symbol index. This starts out as zero.
3081 // It is initialized in Layout::finalize() to be the index, or -1U
3082 // if there isn't one.
3083 unsigned int symtab_index_;
3084 // If there is a STT_SECTION for this output section in the dynamic
3085 // symbol table, this is the symbol index. This starts out as zero.
3086 // It is initialized in Layout::finalize() to be the index, or -1U
3087 // if there isn't one.
3088 unsigned int dynsym_index_;
3089 // The input sections. This will be empty in cases where we don't
3090 // need to keep track of them.
3091 Input_section_list input_sections_;
3092 // The offset of the first entry in input_sections_.
3093 off_t first_input_offset_;
3094 // The fill data. This is separate from input_sections_ because we
3095 // often will need fill sections without needing to keep track of
3098 // If the section requires postprocessing, this buffer holds the
3099 // section contents during relocation.
3100 unsigned char* postprocessing_buffer_;
3101 // Whether this output section needs a STT_SECTION symbol in the
3102 // normal symbol table. This will be true if there is a relocation
3104 bool needs_symtab_index_ : 1;
3105 // Whether this output section needs a STT_SECTION symbol in the
3106 // dynamic symbol table. This will be true if there is a dynamic
3107 // relocation which needs it.
3108 bool needs_dynsym_index_ : 1;
3109 // Whether the link field of this output section should point to the
3110 // normal symbol table.
3111 bool should_link_to_symtab_ : 1;
3112 // Whether the link field of this output section should point to the
3113 // dynamic symbol table.
3114 bool should_link_to_dynsym_ : 1;
3115 // Whether this section should be written after all the input
3116 // sections are complete.
3117 bool after_input_sections_ : 1;
3118 // Whether this section requires post processing after all
3119 // relocations have been applied.
3120 bool requires_postprocessing_ : 1;
3121 // Whether an input section was mapped to this output section
3122 // because of a SECTIONS clause in a linker script.
3123 bool found_in_sections_clause_ : 1;
3124 // Whether this section has an explicitly specified load address.
3125 bool has_load_address_ : 1;
3126 // True if the info_section_ field means the section index of the
3127 // section, false if it means the symbol index of the corresponding
3129 bool info_uses_section_index_ : 1;
3130 // True if the input sections attached to this output section may
3132 bool may_sort_attached_input_sections_ : 1;
3133 // True if the input sections attached to this output section must
3135 bool must_sort_attached_input_sections_ : 1;
3136 // True if the input sections attached to this output section have
3137 // already been sorted.
3138 bool attached_input_sections_are_sorted_ : 1;
3139 // True if this section holds relro data.
3141 // True if this section holds relro local data.
3142 bool is_relro_local_ : 1;
3143 // True if this is a small section.
3144 bool is_small_section_ : 1;
3145 // True if this is a large section.
3146 bool is_large_section_ : 1;
3147 // For SHT_TLS sections, the offset of this section relative to the base
3148 // of the TLS segment.
3149 uint64_t tls_offset_;
3150 // Saved checkpoint.
3151 Checkpoint_output_section* checkpoint_;
3154 // An output segment. PT_LOAD segments are built from collections of
3155 // output sections. Other segments typically point within PT_LOAD
3156 // segments, and are built directly as needed.
3158 // NOTE: We want to use the copy constructor for this class. During
3159 // relaxation, we may try built the segments multiple times. We do
3160 // that by copying the original segment list before lay-out, doing
3161 // a trial lay-out and roll-back to the saved copied if we need to
3162 // to the lay-out again.
3164 class Output_segment
3167 // Create an output segment, specifying the type and flags.
3168 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3170 // Return the virtual address.
3173 { return this->vaddr_; }
3175 // Return the physical address.
3178 { return this->paddr_; }
3180 // Return the segment type.
3183 { return this->type_; }
3185 // Return the segment flags.
3188 { return this->flags_; }
3190 // Return the memory size.
3193 { return this->memsz_; }
3195 // Return the file size.
3198 { return this->filesz_; }
3200 // Return the file offset.
3203 { return this->offset_; }
3205 // Whether this is a segment created to hold large data sections.
3207 is_large_data_segment() const
3208 { return this->is_large_data_segment_; }
3210 // Record that this is a segment created to hold large data
3213 set_is_large_data_segment()
3214 { this->is_large_data_segment_ = true; }
3216 // Return the maximum alignment of the Output_data.
3218 maximum_alignment();
3220 // Add an Output_section to this segment.
3222 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags);
3224 // Remove an Output_section from this segment. It is an error if it
3227 remove_output_section(Output_section* os);
3229 // Add an Output_data (which is not an Output_section) to the start
3232 add_initial_output_data(Output_data*);
3234 // Return true if this segment has any sections which hold actual
3235 // data, rather than being a BSS section.
3237 has_any_data_sections() const
3238 { return !this->output_data_.empty(); }
3240 // Return the number of dynamic relocations applied to this segment.
3242 dynamic_reloc_count() const;
3244 // Return the address of the first section.
3246 first_section_load_address() const;
3248 // Return whether the addresses have been set already.
3250 are_addresses_set() const
3251 { return this->are_addresses_set_; }
3253 // Set the addresses.
3255 set_addresses(uint64_t vaddr, uint64_t paddr)
3257 this->vaddr_ = vaddr;
3258 this->paddr_ = paddr;
3259 this->are_addresses_set_ = true;
3262 // Set the segment flags. This is only used if we have a PHDRS
3263 // clause which explicitly specifies the flags.
3265 set_flags(elfcpp::Elf_Word flags)
3266 { this->flags_ = flags; }
3268 // Set the address of the segment to ADDR and the offset to *POFF
3269 // and set the addresses and offsets of all contained output
3270 // sections accordingly. Set the section indexes of all contained
3271 // output sections starting with *PSHNDX. If RESET is true, first
3272 // reset the addresses of the contained sections. Return the
3273 // address of the immediately following segment. Update *POFF and
3274 // *PSHNDX. This should only be called for a PT_LOAD segment.
3276 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
3277 unsigned int* pshndx);
3279 // Set the minimum alignment of this segment. This may be adjusted
3280 // upward based on the section alignments.
3282 set_minimum_p_align(uint64_t align)
3283 { this->min_p_align_ = align; }
3285 // Set the offset of this segment based on the section. This should
3286 // only be called for a non-PT_LOAD segment.
3290 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3294 // Return the number of output sections.
3296 output_section_count() const;
3298 // Return the section attached to the list segment with the lowest
3299 // load address. This is used when handling a PHDRS clause in a
3302 section_with_lowest_load_address() const;
3304 // Write the segment header into *OPHDR.
3305 template<int size, bool big_endian>
3307 write_header(elfcpp::Phdr_write<size, big_endian>*);
3309 // Write the section headers of associated sections into V.
3310 template<int size, bool big_endian>
3312 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3313 unsigned int* pshndx) const;
3315 // Print the output sections in the map file.
3317 print_sections_to_mapfile(Mapfile*) const;
3320 typedef std::list<Output_data*> Output_data_list;
3322 // Find the maximum alignment in an Output_data_list.
3324 maximum_alignment_list(const Output_data_list*);
3326 // Return whether the first data section is a relro section.
3328 is_first_section_relro() const;
3330 // Set the section addresses in an Output_data_list.
3332 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3333 uint64_t addr, off_t* poff, unsigned int* pshndx,
3334 bool* in_tls, bool* in_relro);
3336 // Return the number of Output_sections in an Output_data_list.
3338 output_section_count_list(const Output_data_list*) const;
3340 // Return the number of dynamic relocs in an Output_data_list.
3342 dynamic_reloc_count_list(const Output_data_list*) const;
3344 // Find the section with the lowest load address in an
3345 // Output_data_list.
3347 lowest_load_address_in_list(const Output_data_list* pdl,
3348 Output_section** found,
3349 uint64_t* found_lma) const;
3351 // Write the section headers in the list into V.
3352 template<int size, bool big_endian>
3354 write_section_headers_list(const Layout*, const Stringpool*,
3355 const Output_data_list*, unsigned char* v,
3356 unsigned int* pshdx) const;
3358 // Print a section list to the mapfile.
3360 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3362 // NOTE: We want to use the copy constructor. Currently, shallow copy
3363 // works for us so we do not need to write our own copy constructor.
3365 // The list of output data with contents attached to this segment.
3366 Output_data_list output_data_;
3367 // The list of output data without contents attached to this segment.
3368 Output_data_list output_bss_;
3369 // The segment virtual address.
3371 // The segment physical address.
3373 // The size of the segment in memory.
3375 // The maximum section alignment. The is_max_align_known_ field
3376 // indicates whether this has been finalized.
3377 uint64_t max_align_;
3378 // The required minimum value for the p_align field. This is used
3379 // for PT_LOAD segments. Note that this does not mean that
3380 // addresses should be aligned to this value; it means the p_paddr
3381 // and p_vaddr fields must be congruent modulo this value. For
3382 // non-PT_LOAD segments, the dynamic linker works more efficiently
3383 // if the p_align field has the more conventional value, although it
3384 // can align as needed.
3385 uint64_t min_p_align_;
3386 // The offset of the segment data within the file.
3388 // The size of the segment data in the file.
3390 // The segment type;
3391 elfcpp::Elf_Word type_;
3392 // The segment flags.
3393 elfcpp::Elf_Word flags_;
3394 // Whether we have finalized max_align_.
3395 bool is_max_align_known_ : 1;
3396 // Whether vaddr and paddr were set by a linker script.
3397 bool are_addresses_set_ : 1;
3398 // Whether this segment holds large data sections.
3399 bool is_large_data_segment_ : 1;
3402 // This class represents the output file.
3407 Output_file(const char* name);
3409 // Indicate that this is a temporary file which should not be
3413 { this->is_temporary_ = true; }
3415 // Try to open an existing file. Returns false if the file doesn't
3416 // exist, has a size of 0 or can't be mmaped. This method is
3419 open_for_modification();
3421 // Open the output file. FILE_SIZE is the final size of the file.
3422 // If the file already exists, it is deleted/truncated. This method
3423 // is thread-unsafe.
3425 open(off_t file_size);
3427 // Resize the output file. This method is thread-unsafe.
3429 resize(off_t file_size);
3431 // Close the output file (flushing all buffered data) and make sure
3432 // there are no errors. This method is thread-unsafe.
3436 // Return the size of this file.
3439 { return this->file_size_; }
3441 // We currently always use mmap which makes the view handling quite
3442 // simple. In the future we may support other approaches.
3444 // Write data to the output file.
3446 write(off_t offset, const void* data, size_t len)
3447 { memcpy(this->base_ + offset, data, len); }
3449 // Get a buffer to use to write to the file, given the offset into
3450 // the file and the size.
3452 get_output_view(off_t start, size_t size)
3454 gold_assert(start >= 0
3455 && start + static_cast<off_t>(size) <= this->file_size_);
3456 return this->base_ + start;
3459 // VIEW must have been returned by get_output_view. Write the
3460 // buffer to the file, passing in the offset and the size.
3462 write_output_view(off_t, size_t, unsigned char*)
3465 // Get a read/write buffer. This is used when we want to write part
3466 // of the file, read it in, and write it again.
3468 get_input_output_view(off_t start, size_t size)
3469 { return this->get_output_view(start, size); }
3471 // Write a read/write buffer back to the file.
3473 write_input_output_view(off_t, size_t, unsigned char*)
3476 // Get a read buffer. This is used when we just want to read part
3477 // of the file back it in.
3478 const unsigned char*
3479 get_input_view(off_t start, size_t size)
3480 { return this->get_output_view(start, size); }
3482 // Release a read bfufer.
3484 free_input_view(off_t, size_t, const unsigned char*)
3488 // Map the file into memory or, if that fails, allocate anonymous
3493 // Allocate anonymous memory for the file.
3497 // Map the file into memory.
3501 // Unmap the file from memory (and flush to disk buffers).
3511 // Base of file mapped into memory.
3512 unsigned char* base_;
3513 // True iff base_ points to a memory buffer rather than an output file.
3514 bool map_is_anonymous_;
3515 // True if this is a temporary file which should not be output.
3519 } // End namespace gold.
3521 #endif // !defined(GOLD_OUTPUT_H)