1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
33 #include "reloc-types.h"
38 class General_options;
42 class Output_merge_base;
44 class Relocatable_relocs;
46 template<int size, bool big_endian>
48 template<int size, bool big_endian>
50 template<int size, bool big_endian>
51 class Sized_relobj_file;
53 // An abtract class for data which has to go into the output file.
58 explicit Output_data()
59 : address_(0), data_size_(0), offset_(-1),
60 is_address_valid_(false), is_data_size_valid_(false),
61 is_offset_valid_(false), is_data_size_fixed_(false),
62 has_dynamic_reloc_(false)
68 // Return the address. For allocated sections, this is only valid
69 // after Layout::finalize is finished.
73 gold_assert(this->is_address_valid_);
74 return this->address_;
77 // Return the size of the data. For allocated sections, this must
78 // be valid after Layout::finalize calls set_address, but need not
79 // be valid before then.
83 gold_assert(this->is_data_size_valid_);
84 return this->data_size_;
87 // Get the current data size.
89 current_data_size() const
90 { return this->current_data_size_for_child(); }
92 // Return true if data size is fixed.
94 is_data_size_fixed() const
95 { return this->is_data_size_fixed_; }
97 // Return the file offset. This is only valid after
98 // Layout::finalize is finished. For some non-allocated sections,
99 // it may not be valid until near the end of the link.
103 gold_assert(this->is_offset_valid_);
104 return this->offset_;
107 // Reset the address, file offset and data size. This essentially
108 // disables the sanity testing about duplicate and unknown settings.
110 reset_address_and_file_offset()
112 this->is_address_valid_ = false;
113 this->is_offset_valid_ = false;
114 if (!this->is_data_size_fixed_)
115 this->is_data_size_valid_ = false;
116 this->do_reset_address_and_file_offset();
119 // As above, but just for data size.
123 if (!this->is_data_size_fixed_)
124 this->is_data_size_valid_ = false;
127 // Return true if address and file offset already have reset values. In
128 // other words, calling reset_address_and_file_offset will not change them.
130 address_and_file_offset_have_reset_values() const
131 { return this->do_address_and_file_offset_have_reset_values(); }
133 // Return the required alignment.
136 { return this->do_addralign(); }
138 // Return whether this has a load address.
140 has_load_address() const
141 { return this->do_has_load_address(); }
143 // Return the load address.
146 { return this->do_load_address(); }
148 // Return whether this is an Output_section.
151 { return this->do_is_section(); }
153 // Return whether this is an Output_section of the specified type.
155 is_section_type(elfcpp::Elf_Word stt) const
156 { return this->do_is_section_type(stt); }
158 // Return whether this is an Output_section with the specified flag
161 is_section_flag_set(elfcpp::Elf_Xword shf) const
162 { return this->do_is_section_flag_set(shf); }
164 // Return the output section that this goes in, if there is one.
167 { return this->do_output_section(); }
169 const Output_section*
170 output_section() const
171 { return this->do_output_section(); }
173 // Return the output section index, if there is an output section.
176 { return this->do_out_shndx(); }
178 // Set the output section index, if this is an output section.
180 set_out_shndx(unsigned int shndx)
181 { this->do_set_out_shndx(shndx); }
183 // Set the address and file offset of this data, and finalize the
184 // size of the data. This is called during Layout::finalize for
185 // allocated sections.
187 set_address_and_file_offset(uint64_t addr, off_t off)
189 this->set_address(addr);
190 this->set_file_offset(off);
191 this->finalize_data_size();
196 set_address(uint64_t addr)
198 gold_assert(!this->is_address_valid_);
199 this->address_ = addr;
200 this->is_address_valid_ = true;
203 // Set the file offset.
205 set_file_offset(off_t off)
207 gold_assert(!this->is_offset_valid_);
209 this->is_offset_valid_ = true;
212 // Update the data size without finalizing it.
214 pre_finalize_data_size()
216 if (!this->is_data_size_valid_)
218 // Tell the child class to update the data size.
219 this->update_data_size();
223 // Finalize the data size.
227 if (!this->is_data_size_valid_)
229 // Tell the child class to set the data size.
230 this->set_final_data_size();
231 gold_assert(this->is_data_size_valid_);
235 // Set the TLS offset. Called only for SHT_TLS sections.
237 set_tls_offset(uint64_t tls_base)
238 { this->do_set_tls_offset(tls_base); }
240 // Return the TLS offset, relative to the base of the TLS segment.
241 // Valid only for SHT_TLS sections.
244 { return this->do_tls_offset(); }
246 // Write the data to the output file. This is called after
247 // Layout::finalize is complete.
249 write(Output_file* file)
250 { this->do_write(file); }
252 // This is called by Layout::finalize to note that the sizes of
253 // allocated sections must now be fixed.
256 { Output_data::allocated_sizes_are_fixed = true; }
258 // Used to check that layout has been done.
261 { return Output_data::allocated_sizes_are_fixed; }
263 // Note that a dynamic reloc has been applied to this data.
266 { this->has_dynamic_reloc_ = true; }
268 // Return whether a dynamic reloc has been applied.
270 has_dynamic_reloc() const
271 { return this->has_dynamic_reloc_; }
273 // Whether the address is valid.
275 is_address_valid() const
276 { return this->is_address_valid_; }
278 // Whether the file offset is valid.
280 is_offset_valid() const
281 { return this->is_offset_valid_; }
283 // Whether the data size is valid.
285 is_data_size_valid() const
286 { return this->is_data_size_valid_; }
288 // Print information to the map file.
290 print_to_mapfile(Mapfile* mapfile) const
291 { return this->do_print_to_mapfile(mapfile); }
294 // Functions that child classes may or in some cases must implement.
296 // Write the data to the output file.
298 do_write(Output_file*) = 0;
300 // Return the required alignment.
302 do_addralign() const = 0;
304 // Return whether this has a load address.
306 do_has_load_address() const
309 // Return the load address.
311 do_load_address() const
312 { gold_unreachable(); }
314 // Return whether this is an Output_section.
316 do_is_section() const
319 // Return whether this is an Output_section of the specified type.
320 // This only needs to be implement by Output_section.
322 do_is_section_type(elfcpp::Elf_Word) const
325 // Return whether this is an Output_section with the specific flag
326 // set. This only needs to be implemented by Output_section.
328 do_is_section_flag_set(elfcpp::Elf_Xword) const
331 // Return the output section, if there is one.
332 virtual Output_section*
336 virtual const Output_section*
337 do_output_section() const
340 // Return the output section index, if there is an output section.
343 { gold_unreachable(); }
345 // Set the output section index, if this is an output section.
347 do_set_out_shndx(unsigned int)
348 { gold_unreachable(); }
350 // This is a hook for derived classes to set the preliminary data size.
351 // This is called by pre_finalize_data_size, normally called during
352 // Layout::finalize, before the section address is set, and is used
353 // during an incremental update, when we need to know the size of a
354 // section before allocating space in the output file. For classes
355 // where the current data size is up to date, this default version of
356 // the method can be inherited.
361 // This is a hook for derived classes to set the data size. This is
362 // called by finalize_data_size, normally called during
363 // Layout::finalize, when the section address is set.
365 set_final_data_size()
366 { gold_unreachable(); }
368 // A hook for resetting the address and file offset.
370 do_reset_address_and_file_offset()
373 // Return true if address and file offset already have reset values. In
374 // other words, calling reset_address_and_file_offset will not change them.
375 // A child class overriding do_reset_address_and_file_offset may need to
376 // also override this.
378 do_address_and_file_offset_have_reset_values() const
379 { return !this->is_address_valid_ && !this->is_offset_valid_; }
381 // Set the TLS offset. Called only for SHT_TLS sections.
383 do_set_tls_offset(uint64_t)
384 { gold_unreachable(); }
386 // Return the TLS offset, relative to the base of the TLS segment.
387 // Valid only for SHT_TLS sections.
389 do_tls_offset() const
390 { gold_unreachable(); }
392 // Print to the map file. This only needs to be implemented by
393 // classes which may appear in a PT_LOAD segment.
395 do_print_to_mapfile(Mapfile*) const
396 { gold_unreachable(); }
398 // Functions that child classes may call.
400 // Reset the address. The Output_section class needs this when an
401 // SHF_ALLOC input section is added to an output section which was
402 // formerly not SHF_ALLOC.
404 mark_address_invalid()
405 { this->is_address_valid_ = false; }
407 // Set the size of the data.
409 set_data_size(off_t data_size)
411 gold_assert(!this->is_data_size_valid_
412 && !this->is_data_size_fixed_);
413 this->data_size_ = data_size;
414 this->is_data_size_valid_ = true;
417 // Fix the data size. Once it is fixed, it cannot be changed
418 // and the data size remains always valid.
422 gold_assert(this->is_data_size_valid_);
423 this->is_data_size_fixed_ = true;
426 // Get the current data size--this is for the convenience of
427 // sections which build up their size over time.
429 current_data_size_for_child() const
430 { return this->data_size_; }
432 // Set the current data size--this is for the convenience of
433 // sections which build up their size over time.
435 set_current_data_size_for_child(off_t data_size)
437 gold_assert(!this->is_data_size_valid_);
438 this->data_size_ = data_size;
441 // Return default alignment for the target size.
445 // Return default alignment for a specified size--32 or 64.
447 default_alignment_for_size(int size);
450 Output_data(const Output_data&);
451 Output_data& operator=(const Output_data&);
453 // This is used for verification, to make sure that we don't try to
454 // change any sizes of allocated sections after we set the section
456 static bool allocated_sizes_are_fixed;
458 // Memory address in output file.
460 // Size of data in output file.
462 // File offset of contents in output file.
464 // Whether address_ is valid.
465 bool is_address_valid_ : 1;
466 // Whether data_size_ is valid.
467 bool is_data_size_valid_ : 1;
468 // Whether offset_ is valid.
469 bool is_offset_valid_ : 1;
470 // Whether data size is fixed.
471 bool is_data_size_fixed_ : 1;
472 // Whether any dynamic relocs have been applied to this section.
473 bool has_dynamic_reloc_ : 1;
476 // Output the section headers.
478 class Output_section_headers : public Output_data
481 Output_section_headers(const Layout*,
482 const Layout::Segment_list*,
483 const Layout::Section_list*,
484 const Layout::Section_list*,
486 const Output_section*);
489 // Write the data to the file.
491 do_write(Output_file*);
493 // Return the required alignment.
496 { return Output_data::default_alignment(); }
498 // Write to a map file.
500 do_print_to_mapfile(Mapfile* mapfile) const
501 { mapfile->print_output_data(this, _("** section headers")); }
503 // Update the data size.
506 { this->set_data_size(this->do_size()); }
508 // Set final data size.
510 set_final_data_size()
511 { this->set_data_size(this->do_size()); }
514 // Write the data to the file with the right size and endianness.
515 template<int size, bool big_endian>
517 do_sized_write(Output_file*);
519 // Compute data size.
523 const Layout* layout_;
524 const Layout::Segment_list* segment_list_;
525 const Layout::Section_list* section_list_;
526 const Layout::Section_list* unattached_section_list_;
527 const Stringpool* secnamepool_;
528 const Output_section* shstrtab_section_;
531 // Output the segment headers.
533 class Output_segment_headers : public Output_data
536 Output_segment_headers(const Layout::Segment_list& segment_list);
539 // Write the data to the file.
541 do_write(Output_file*);
543 // Return the required alignment.
546 { return Output_data::default_alignment(); }
548 // Write to a map file.
550 do_print_to_mapfile(Mapfile* mapfile) const
551 { mapfile->print_output_data(this, _("** segment headers")); }
553 // Set final data size.
555 set_final_data_size()
556 { this->set_data_size(this->do_size()); }
559 // Write the data to the file with the right size and endianness.
560 template<int size, bool big_endian>
562 do_sized_write(Output_file*);
564 // Compute the current size.
568 const Layout::Segment_list& segment_list_;
571 // Output the ELF file header.
573 class Output_file_header : public Output_data
576 Output_file_header(Target*,
578 const Output_segment_headers*);
580 // Add information about the section headers. We lay out the ELF
581 // file header before we create the section headers.
582 void set_section_info(const Output_section_headers*,
583 const Output_section* shstrtab);
586 // Write the data to the file.
588 do_write(Output_file*);
590 // Return the required alignment.
593 { return Output_data::default_alignment(); }
595 // Write to a map file.
597 do_print_to_mapfile(Mapfile* mapfile) const
598 { mapfile->print_output_data(this, _("** file header")); }
600 // Set final data size.
602 set_final_data_size(void)
603 { this->set_data_size(this->do_size()); }
606 // Write the data to the file with the right size and endianness.
607 template<int size, bool big_endian>
609 do_sized_write(Output_file*);
611 // Return the value to use for the entry address.
613 typename elfcpp::Elf_types<size>::Elf_Addr
616 // Compute the current data size.
621 const Symbol_table* symtab_;
622 const Output_segment_headers* segment_header_;
623 const Output_section_headers* section_header_;
624 const Output_section* shstrtab_;
627 // Output sections are mainly comprised of input sections. However,
628 // there are cases where we have data to write out which is not in an
629 // input section. Output_section_data is used in such cases. This is
630 // an abstract base class.
632 class Output_section_data : public Output_data
635 Output_section_data(off_t data_size, uint64_t addralign,
636 bool is_data_size_fixed)
637 : Output_data(), output_section_(NULL), addralign_(addralign)
639 this->set_data_size(data_size);
640 if (is_data_size_fixed)
641 this->fix_data_size();
644 Output_section_data(uint64_t addralign)
645 : Output_data(), output_section_(NULL), addralign_(addralign)
648 // Return the output section.
651 { return this->output_section_; }
653 const Output_section*
654 output_section() const
655 { return this->output_section_; }
657 // Record the output section.
659 set_output_section(Output_section* os);
661 // Add an input section, for SHF_MERGE sections. This returns true
662 // if the section was handled.
664 add_input_section(Relobj* object, unsigned int shndx)
665 { return this->do_add_input_section(object, shndx); }
667 // Given an input OBJECT, an input section index SHNDX within that
668 // object, and an OFFSET relative to the start of that input
669 // section, return whether or not the corresponding offset within
670 // the output section is known. If this function returns true, it
671 // sets *POUTPUT to the output offset. The value -1 indicates that
672 // this input offset is being discarded.
674 output_offset(const Relobj* object, unsigned int shndx,
675 section_offset_type offset,
676 section_offset_type* poutput) const
677 { return this->do_output_offset(object, shndx, offset, poutput); }
679 // Return whether this is the merge section for the input section
680 // SHNDX in OBJECT. This should return true when output_offset
681 // would return true for some values of OFFSET.
683 is_merge_section_for(const Relobj* object, unsigned int shndx) const
684 { return this->do_is_merge_section_for(object, shndx); }
686 // Write the contents to a buffer. This is used for sections which
687 // require postprocessing, such as compression.
689 write_to_buffer(unsigned char* buffer)
690 { this->do_write_to_buffer(buffer); }
692 // Print merge stats to stderr. This should only be called for
693 // SHF_MERGE sections.
695 print_merge_stats(const char* section_name)
696 { this->do_print_merge_stats(section_name); }
699 // The child class must implement do_write.
701 // The child class may implement specific adjustments to the output
704 do_adjust_output_section(Output_section*)
707 // May be implemented by child class. Return true if the section
710 do_add_input_section(Relobj*, unsigned int)
711 { gold_unreachable(); }
713 // The child class may implement output_offset.
715 do_output_offset(const Relobj*, unsigned int, section_offset_type,
716 section_offset_type*) const
719 // The child class may implement is_merge_section_for.
721 do_is_merge_section_for(const Relobj*, unsigned int) const
724 // The child class may implement write_to_buffer. Most child
725 // classes can not appear in a compressed section, and they do not
728 do_write_to_buffer(unsigned char*)
729 { gold_unreachable(); }
731 // Print merge statistics.
733 do_print_merge_stats(const char*)
734 { gold_unreachable(); }
736 // Return the required alignment.
739 { return this->addralign_; }
741 // Return the output section.
744 { return this->output_section_; }
746 const Output_section*
747 do_output_section() const
748 { return this->output_section_; }
750 // Return the section index of the output section.
752 do_out_shndx() const;
754 // Set the alignment.
756 set_addralign(uint64_t addralign);
759 // The output section for this section.
760 Output_section* output_section_;
761 // The required alignment.
765 // Some Output_section_data classes build up their data step by step,
766 // rather than all at once. This class provides an interface for
769 class Output_section_data_build : public Output_section_data
772 Output_section_data_build(uint64_t addralign)
773 : Output_section_data(addralign)
776 Output_section_data_build(off_t data_size, uint64_t addralign)
777 : Output_section_data(data_size, addralign, false)
780 // Set the current data size.
782 set_current_data_size(off_t data_size)
783 { this->set_current_data_size_for_child(data_size); }
786 // Set the final data size.
788 set_final_data_size()
789 { this->set_data_size(this->current_data_size_for_child()); }
792 // A simple case of Output_data in which we have constant data to
795 class Output_data_const : public Output_section_data
798 Output_data_const(const std::string& data, uint64_t addralign)
799 : Output_section_data(data.size(), addralign, true), data_(data)
802 Output_data_const(const char* p, off_t len, uint64_t addralign)
803 : Output_section_data(len, addralign, true), data_(p, len)
806 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
807 : Output_section_data(len, addralign, true),
808 data_(reinterpret_cast<const char*>(p), len)
812 // Write the data to the output file.
814 do_write(Output_file*);
816 // Write the data to a buffer.
818 do_write_to_buffer(unsigned char* buffer)
819 { memcpy(buffer, this->data_.data(), this->data_.size()); }
821 // Write to a map file.
823 do_print_to_mapfile(Mapfile* mapfile) const
824 { mapfile->print_output_data(this, _("** fill")); }
830 // Another version of Output_data with constant data, in which the
831 // buffer is allocated by the caller.
833 class Output_data_const_buffer : public Output_section_data
836 Output_data_const_buffer(const unsigned char* p, off_t len,
837 uint64_t addralign, const char* map_name)
838 : Output_section_data(len, addralign, true),
839 p_(p), map_name_(map_name)
843 // Write the data the output file.
845 do_write(Output_file*);
847 // Write the data to a buffer.
849 do_write_to_buffer(unsigned char* buffer)
850 { memcpy(buffer, this->p_, this->data_size()); }
852 // Write to a map file.
854 do_print_to_mapfile(Mapfile* mapfile) const
855 { mapfile->print_output_data(this, _(this->map_name_)); }
858 // The data to output.
859 const unsigned char* p_;
860 // Name to use in a map file. Maps are a rarely used feature, but
861 // the space usage is minor as aren't very many of these objects.
862 const char* map_name_;
865 // A place holder for a fixed amount of data written out via some
868 class Output_data_fixed_space : public Output_section_data
871 Output_data_fixed_space(off_t data_size, uint64_t addralign,
872 const char* map_name)
873 : Output_section_data(data_size, addralign, true),
878 // Write out the data--the actual data must be written out
881 do_write(Output_file*)
884 // Write to a map file.
886 do_print_to_mapfile(Mapfile* mapfile) const
887 { mapfile->print_output_data(this, _(this->map_name_)); }
890 // Name to use in a map file. Maps are a rarely used feature, but
891 // the space usage is minor as aren't very many of these objects.
892 const char* map_name_;
895 // A place holder for variable sized data written out via some other
898 class Output_data_space : public Output_section_data_build
901 explicit Output_data_space(uint64_t addralign, const char* map_name)
902 : Output_section_data_build(addralign),
906 explicit Output_data_space(off_t data_size, uint64_t addralign,
907 const char* map_name)
908 : Output_section_data_build(data_size, addralign),
912 // Set the alignment.
914 set_space_alignment(uint64_t align)
915 { this->set_addralign(align); }
918 // Write out the data--the actual data must be written out
921 do_write(Output_file*)
924 // Write to a map file.
926 do_print_to_mapfile(Mapfile* mapfile) const
927 { mapfile->print_output_data(this, _(this->map_name_)); }
930 // Name to use in a map file. Maps are a rarely used feature, but
931 // the space usage is minor as aren't very many of these objects.
932 const char* map_name_;
935 // Fill fixed space with zeroes. This is just like
936 // Output_data_fixed_space, except that the map name is known.
938 class Output_data_zero_fill : public Output_section_data
941 Output_data_zero_fill(off_t data_size, uint64_t addralign)
942 : Output_section_data(data_size, addralign, true)
946 // There is no data to write out.
948 do_write(Output_file*)
951 // Write to a map file.
953 do_print_to_mapfile(Mapfile* mapfile) const
954 { mapfile->print_output_data(this, "** zero fill"); }
957 // A string table which goes into an output section.
959 class Output_data_strtab : public Output_section_data
962 Output_data_strtab(Stringpool* strtab)
963 : Output_section_data(1), strtab_(strtab)
967 // This is called to update the section size prior to assigning
968 // the address and file offset.
971 { this->set_final_data_size(); }
973 // This is called to set the address and file offset. Here we make
974 // sure that the Stringpool is finalized.
976 set_final_data_size();
978 // Write out the data.
980 do_write(Output_file*);
982 // Write the data to a buffer.
984 do_write_to_buffer(unsigned char* buffer)
985 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
987 // Write to a map file.
989 do_print_to_mapfile(Mapfile* mapfile) const
990 { mapfile->print_output_data(this, _("** string table")); }
996 // This POD class is used to represent a single reloc in the output
997 // file. This could be a private class within Output_data_reloc, but
998 // the templatization is complex enough that I broke it out into a
999 // separate class. The class is templatized on either elfcpp::SHT_REL
1000 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1001 // relocation or an ordinary relocation.
1003 // A relocation can be against a global symbol, a local symbol, a
1004 // local section symbol, an output section, or the undefined symbol at
1005 // index 0. We represent the latter by using a NULL global symbol.
1007 template<int sh_type, bool dynamic, int size, bool big_endian>
1010 template<bool dynamic, int size, bool big_endian>
1011 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1014 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1015 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1017 static const Address invalid_address = static_cast<Address>(0) - 1;
1019 // An uninitialized entry. We need this because we want to put
1020 // instances of this class into an STL container.
1022 : local_sym_index_(INVALID_CODE)
1025 // We have a bunch of different constructors. They come in pairs
1026 // depending on how the address of the relocation is specified. It
1027 // can either be an offset in an Output_data or an offset in an
1030 // A reloc against a global symbol.
1032 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1033 Address address, bool is_relative, bool is_symbolless,
1034 bool use_plt_offset);
1036 Output_reloc(Symbol* gsym, unsigned int type,
1037 Sized_relobj<size, big_endian>* relobj,
1038 unsigned int shndx, Address address, bool is_relative,
1039 bool is_symbolless, bool use_plt_offset);
1041 // A reloc against a local symbol or local section symbol.
1043 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1044 unsigned int local_sym_index, unsigned int type,
1045 Output_data* od, Address address, bool is_relative,
1046 bool is_symbolless, bool is_section_symbol,
1047 bool use_plt_offset);
1049 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1050 unsigned int local_sym_index, unsigned int type,
1051 unsigned int shndx, Address address, bool is_relative,
1052 bool is_symbolless, bool is_section_symbol,
1053 bool use_plt_offset);
1055 // A reloc against the STT_SECTION symbol of an output section.
1057 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1058 Address address, bool is_relative);
1060 Output_reloc(Output_section* os, unsigned int type,
1061 Sized_relobj<size, big_endian>* relobj, unsigned int shndx,
1062 Address address, bool is_relative);
1064 // An absolute or relative relocation with no symbol.
1066 Output_reloc(unsigned int type, Output_data* od, Address address,
1069 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1070 unsigned int shndx, Address address, bool is_relative);
1072 // A target specific relocation. The target will be called to get
1073 // the symbol index, passing ARG. The type and offset will be set
1074 // as for other relocation types.
1076 Output_reloc(unsigned int type, void* arg, Output_data* od,
1079 Output_reloc(unsigned int type, void* arg,
1080 Sized_relobj<size, big_endian>* relobj,
1081 unsigned int shndx, Address address);
1083 // Return the reloc type.
1086 { return this->type_; }
1088 // Return whether this is a RELATIVE relocation.
1091 { return this->is_relative_; }
1093 // Return whether this is a relocation which should not use
1094 // a symbol, but which obtains its addend from a symbol.
1096 is_symbolless() const
1097 { return this->is_symbolless_; }
1099 // Return whether this is against a local section symbol.
1101 is_local_section_symbol() const
1103 return (this->local_sym_index_ != GSYM_CODE
1104 && this->local_sym_index_ != SECTION_CODE
1105 && this->local_sym_index_ != INVALID_CODE
1106 && this->local_sym_index_ != TARGET_CODE
1107 && this->is_section_symbol_);
1110 // Return whether this is a target specific relocation.
1112 is_target_specific() const
1113 { return this->local_sym_index_ == TARGET_CODE; }
1115 // Return the argument to pass to the target for a target specific
1120 gold_assert(this->local_sym_index_ == TARGET_CODE);
1121 return this->u1_.arg;
1124 // For a local section symbol, return the offset of the input
1125 // section within the output section. ADDEND is the addend being
1126 // applied to the input section.
1128 local_section_offset(Addend addend) const;
1130 // Get the value of the symbol referred to by a Rel relocation when
1131 // we are adding the given ADDEND.
1133 symbol_value(Addend addend) const;
1135 // If this relocation is against an input section, return the
1136 // relocatable object containing the input section.
1137 Sized_relobj<size, big_endian>*
1140 if (this->shndx_ == INVALID_CODE)
1142 return this->u2_.relobj;
1145 // Write the reloc entry to an output view.
1147 write(unsigned char* pov) const;
1149 // Write the offset and info fields to Write_rel.
1150 template<typename Write_rel>
1151 void write_rel(Write_rel*) const;
1153 // This is used when sorting dynamic relocs. Return -1 to sort this
1154 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1156 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1159 // Return whether this reloc should be sorted before the argument
1160 // when sorting dynamic relocs.
1162 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1164 { return this->compare(r2) < 0; }
1167 // Record that we need a dynamic symbol index.
1169 set_needs_dynsym_index();
1171 // Return the symbol index.
1173 get_symbol_index() const;
1175 // Return the output address.
1177 get_address() const;
1179 // Codes for local_sym_index_.
1188 // Invalid uninitialized entry.
1194 // For a local symbol or local section symbol
1195 // (this->local_sym_index_ >= 0), the object. We will never
1196 // generate a relocation against a local symbol in a dynamic
1197 // object; that doesn't make sense. And our callers will always
1198 // be templatized, so we use Sized_relobj here.
1199 Sized_relobj<size, big_endian>* relobj;
1200 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1201 // symbol. If this is NULL, it indicates a relocation against the
1202 // undefined 0 symbol.
1204 // For a relocation against an output section
1205 // (this->local_sym_index_ == SECTION_CODE), the output section.
1207 // For a target specific relocation, an argument to pass to the
1213 // If this->shndx_ is not INVALID CODE, the object which holds the
1214 // input section being used to specify the reloc address.
1215 Sized_relobj<size, big_endian>* relobj;
1216 // If this->shndx_ is INVALID_CODE, the output data being used to
1217 // specify the reloc address. This may be NULL if the reloc
1218 // address is absolute.
1221 // The address offset within the input section or the Output_data.
1223 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1224 // relocation against an output section, or TARGET_CODE for a target
1225 // specific relocation, or INVALID_CODE for an uninitialized value.
1226 // Otherwise, for a local symbol (this->is_section_symbol_ is
1227 // false), the local symbol index. For a local section symbol
1228 // (this->is_section_symbol_ is true), the section index in the
1230 unsigned int local_sym_index_;
1231 // The reloc type--a processor specific code.
1232 unsigned int type_ : 28;
1233 // True if the relocation is a RELATIVE relocation.
1234 bool is_relative_ : 1;
1235 // True if the relocation is one which should not use
1236 // a symbol, but which obtains its addend from a symbol.
1237 bool is_symbolless_ : 1;
1238 // True if the relocation is against a section symbol.
1239 bool is_section_symbol_ : 1;
1240 // True if the addend should be the PLT offset.
1241 // (Used only for RELA, but stored here for space.)
1242 bool use_plt_offset_ : 1;
1243 // If the reloc address is an input section in an object, the
1244 // section index. This is INVALID_CODE if the reloc address is
1245 // specified in some other way.
1246 unsigned int shndx_;
1249 // The SHT_RELA version of Output_reloc<>. This is just derived from
1250 // the SHT_REL version of Output_reloc, but it adds an addend.
1252 template<bool dynamic, int size, bool big_endian>
1253 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1256 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1257 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1259 // An uninitialized entry.
1264 // A reloc against a global symbol.
1266 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1267 Address address, Addend addend, bool is_relative,
1268 bool is_symbolless, bool use_plt_offset)
1269 : rel_(gsym, type, od, address, is_relative, is_symbolless,
1274 Output_reloc(Symbol* gsym, unsigned int type,
1275 Sized_relobj<size, big_endian>* relobj,
1276 unsigned int shndx, Address address, Addend addend,
1277 bool is_relative, bool is_symbolless, bool use_plt_offset)
1278 : rel_(gsym, type, relobj, shndx, address, is_relative,
1279 is_symbolless, use_plt_offset), addend_(addend)
1282 // A reloc against a local symbol.
1284 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1285 unsigned int local_sym_index, unsigned int type,
1286 Output_data* od, Address address,
1287 Addend addend, bool is_relative,
1288 bool is_symbolless, bool is_section_symbol,
1289 bool use_plt_offset)
1290 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1291 is_symbolless, is_section_symbol, use_plt_offset),
1295 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1296 unsigned int local_sym_index, unsigned int type,
1297 unsigned int shndx, Address address,
1298 Addend addend, bool is_relative,
1299 bool is_symbolless, bool is_section_symbol,
1300 bool use_plt_offset)
1301 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1302 is_symbolless, is_section_symbol, use_plt_offset),
1306 // A reloc against the STT_SECTION symbol of an output section.
1308 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1309 Address address, Addend addend, bool is_relative)
1310 : rel_(os, type, od, address, is_relative), addend_(addend)
1313 Output_reloc(Output_section* os, unsigned int type,
1314 Sized_relobj<size, big_endian>* relobj,
1315 unsigned int shndx, Address address, Addend addend,
1317 : rel_(os, type, relobj, shndx, address, is_relative), addend_(addend)
1320 // An absolute or relative relocation with no symbol.
1322 Output_reloc(unsigned int type, Output_data* od, Address address,
1323 Addend addend, bool is_relative)
1324 : rel_(type, od, address, is_relative), addend_(addend)
1327 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1328 unsigned int shndx, Address address, Addend addend,
1330 : rel_(type, relobj, shndx, address, is_relative), addend_(addend)
1333 // A target specific relocation. The target will be called to get
1334 // the symbol index and the addend, passing ARG. The type and
1335 // offset will be set as for other relocation types.
1337 Output_reloc(unsigned int type, void* arg, Output_data* od,
1338 Address address, Addend addend)
1339 : rel_(type, arg, od, address), addend_(addend)
1342 Output_reloc(unsigned int type, void* arg,
1343 Sized_relobj<size, big_endian>* relobj,
1344 unsigned int shndx, Address address, Addend addend)
1345 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1348 // Return whether this is a RELATIVE relocation.
1351 { return this->rel_.is_relative(); }
1353 // Return whether this is a relocation which should not use
1354 // a symbol, but which obtains its addend from a symbol.
1356 is_symbolless() const
1357 { return this->rel_.is_symbolless(); }
1359 // If this relocation is against an input section, return the
1360 // relocatable object containing the input section.
1361 Sized_relobj<size, big_endian>*
1363 { return this->rel_.get_relobj(); }
1365 // Write the reloc entry to an output view.
1367 write(unsigned char* pov) const;
1369 // Return whether this reloc should be sorted before the argument
1370 // when sorting dynamic relocs.
1372 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1375 int i = this->rel_.compare(r2.rel_);
1381 return this->addend_ < r2.addend_;
1386 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1391 // Output_data_reloc_generic is a non-template base class for
1392 // Output_data_reloc_base. This gives the generic code a way to hold
1393 // a pointer to a reloc section.
1395 class Output_data_reloc_generic : public Output_section_data_build
1398 Output_data_reloc_generic(int size, bool sort_relocs)
1399 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1400 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1403 // Return the number of relative relocs in this section.
1405 relative_reloc_count() const
1406 { return this->relative_reloc_count_; }
1408 // Whether we should sort the relocs.
1411 { return this->sort_relocs_; }
1413 // Add a reloc of type TYPE against the global symbol GSYM. The
1414 // relocation applies to the data at offset ADDRESS within OD.
1416 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1417 uint64_t address, uint64_t addend) = 0;
1419 // Add a reloc of type TYPE against the global symbol GSYM. The
1420 // relocation applies to data at offset ADDRESS within section SHNDX
1421 // of object file RELOBJ. OD is the associated output section.
1423 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1424 Relobj* relobj, unsigned int shndx, uint64_t address,
1425 uint64_t addend) = 0;
1427 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1428 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1431 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1432 unsigned int type, Output_data* od, uint64_t address,
1433 uint64_t addend) = 0;
1435 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1436 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1437 // within section SHNDX of RELOBJ. OD is the associated output
1440 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1441 unsigned int type, Output_data* od, unsigned int shndx,
1442 uint64_t address, uint64_t addend) = 0;
1444 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1445 // output section OS. The relocation applies to the data at offset
1446 // ADDRESS within OD.
1448 add_output_section_generic(Output_section *os, unsigned int type,
1449 Output_data* od, uint64_t address,
1450 uint64_t addend) = 0;
1452 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1453 // output section OS. The relocation applies to the data at offset
1454 // ADDRESS within section SHNDX of RELOBJ. OD is the associated
1457 add_output_section_generic(Output_section* os, unsigned int type,
1458 Output_data* od, Relobj* relobj,
1459 unsigned int shndx, uint64_t address,
1460 uint64_t addend) = 0;
1463 // Note that we've added another relative reloc.
1465 bump_relative_reloc_count()
1466 { ++this->relative_reloc_count_; }
1469 // The number of relative relocs added to this section. This is to
1470 // support DT_RELCOUNT.
1471 size_t relative_reloc_count_;
1472 // Whether to sort the relocations when writing them out, to make
1473 // the dynamic linker more efficient.
1477 // Output_data_reloc is used to manage a section containing relocs.
1478 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1479 // indicates whether this is a dynamic relocation or a normal
1480 // relocation. Output_data_reloc_base is a base class.
1481 // Output_data_reloc is the real class, which we specialize based on
1484 template<int sh_type, bool dynamic, int size, bool big_endian>
1485 class Output_data_reloc_base : public Output_data_reloc_generic
1488 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1489 typedef typename Output_reloc_type::Address Address;
1490 static const int reloc_size =
1491 Reloc_types<sh_type, size, big_endian>::reloc_size;
1493 // Construct the section.
1494 Output_data_reloc_base(bool sort_relocs)
1495 : Output_data_reloc_generic(size, sort_relocs)
1499 // Write out the data.
1501 do_write(Output_file*);
1503 // Set the entry size and the link.
1505 do_adjust_output_section(Output_section* os);
1507 // Write to a map file.
1509 do_print_to_mapfile(Mapfile* mapfile) const
1511 mapfile->print_output_data(this,
1513 ? _("** dynamic relocs")
1517 // Add a relocation entry.
1519 add(Output_data* od, const Output_reloc_type& reloc)
1521 this->relocs_.push_back(reloc);
1522 this->set_current_data_size(this->relocs_.size() * reloc_size);
1524 od->add_dynamic_reloc();
1525 if (reloc.is_relative())
1526 this->bump_relative_reloc_count();
1527 Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1529 relobj->add_dyn_reloc(this->relocs_.size() - 1);
1533 typedef std::vector<Output_reloc_type> Relocs;
1535 // The class used to sort the relocations.
1536 struct Sort_relocs_comparison
1539 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1540 { return r1.sort_before(r2); }
1543 // The relocations in this section.
1547 // The class which callers actually create.
1549 template<int sh_type, bool dynamic, int size, bool big_endian>
1550 class Output_data_reloc;
1552 // The SHT_REL version of Output_data_reloc.
1554 template<bool dynamic, int size, bool big_endian>
1555 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1556 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1559 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1563 typedef typename Base::Output_reloc_type Output_reloc_type;
1564 typedef typename Output_reloc_type::Address Address;
1566 Output_data_reloc(bool sr)
1567 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1570 // Add a reloc against a global symbol.
1573 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1575 this->add(od, Output_reloc_type(gsym, type, od, address,
1576 false, false, false));
1580 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1581 Sized_relobj<size, big_endian>* relobj,
1582 unsigned int shndx, Address address)
1584 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1585 false, false, false));
1589 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1590 uint64_t address, uint64_t addend)
1592 gold_assert(addend == 0);
1593 this->add(od, Output_reloc_type(gsym, type, od,
1594 convert_types<Address, uint64_t>(address),
1595 false, false, false));
1599 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1600 Relobj* relobj, unsigned int shndx, uint64_t address,
1603 gold_assert(addend == 0);
1604 Sized_relobj<size, big_endian>* sized_relobj =
1605 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1606 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1607 convert_types<Address, uint64_t>(address),
1608 false, false, false));
1611 // Add a RELATIVE reloc against a global symbol. The final relocation
1612 // will not reference the symbol.
1615 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1618 this->add(od, Output_reloc_type(gsym, type, od, address, true, true,
1623 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1624 Sized_relobj<size, big_endian>* relobj,
1625 unsigned int shndx, Address address)
1627 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1628 true, true, false));
1631 // Add a global relocation which does not use a symbol for the relocation,
1632 // but which gets its addend from a symbol.
1635 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1636 Output_data* od, Address address)
1638 this->add(od, Output_reloc_type(gsym, type, od, address, false, true,
1643 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1645 Sized_relobj<size, big_endian>* relobj,
1646 unsigned int shndx, Address address)
1648 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1649 false, true, false));
1652 // Add a reloc against a local symbol.
1655 add_local(Sized_relobj<size, big_endian>* relobj,
1656 unsigned int local_sym_index, unsigned int type,
1657 Output_data* od, Address address)
1659 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1660 address, false, false, false, false));
1664 add_local(Sized_relobj<size, big_endian>* relobj,
1665 unsigned int local_sym_index, unsigned int type,
1666 Output_data* od, unsigned int shndx, Address address)
1668 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1669 address, false, false, false, false));
1673 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1674 unsigned int type, Output_data* od, uint64_t address,
1677 gold_assert(addend == 0);
1678 Sized_relobj<size, big_endian>* sized_relobj =
1679 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1680 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1681 convert_types<Address, uint64_t>(address),
1682 false, false, false, false));
1686 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1687 unsigned int type, Output_data* od, unsigned int shndx,
1688 uint64_t address, uint64_t addend)
1690 gold_assert(addend == 0);
1691 Sized_relobj<size, big_endian>* sized_relobj =
1692 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1693 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1694 convert_types<Address, uint64_t>(address),
1695 false, false, false, false));
1698 // Add a RELATIVE reloc against a local symbol.
1701 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1702 unsigned int local_sym_index, unsigned int type,
1703 Output_data* od, Address address)
1705 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1706 address, true, true, false, false));
1710 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1711 unsigned int local_sym_index, unsigned int type,
1712 Output_data* od, unsigned int shndx, Address address)
1714 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1715 address, true, true, false, false));
1718 // Add a local relocation which does not use a symbol for the relocation,
1719 // but which gets its addend from a symbol.
1722 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1723 unsigned int local_sym_index, unsigned int type,
1724 Output_data* od, Address address)
1726 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1727 address, false, true, false, false));
1731 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1732 unsigned int local_sym_index, unsigned int type,
1733 Output_data* od, unsigned int shndx,
1736 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1737 address, false, true, false, false));
1740 // Add a reloc against a local section symbol. This will be
1741 // converted into a reloc against the STT_SECTION symbol of the
1745 add_local_section(Sized_relobj<size, big_endian>* relobj,
1746 unsigned int input_shndx, unsigned int type,
1747 Output_data* od, Address address)
1749 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1750 address, false, false, true, false));
1754 add_local_section(Sized_relobj<size, big_endian>* relobj,
1755 unsigned int input_shndx, unsigned int type,
1756 Output_data* od, unsigned int shndx, Address address)
1758 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1759 address, false, false, true, false));
1762 // A reloc against the STT_SECTION symbol of an output section.
1763 // OS is the Output_section that the relocation refers to; OD is
1764 // the Output_data object being relocated.
1767 add_output_section(Output_section* os, unsigned int type,
1768 Output_data* od, Address address)
1769 { this->add(od, Output_reloc_type(os, type, od, address, false)); }
1772 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1773 Sized_relobj<size, big_endian>* relobj,
1774 unsigned int shndx, Address address)
1775 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, false)); }
1778 add_output_section_generic(Output_section* os, unsigned int type,
1779 Output_data* od, uint64_t address,
1782 gold_assert(addend == 0);
1783 this->add(od, Output_reloc_type(os, type, od,
1784 convert_types<Address, uint64_t>(address),
1789 add_output_section_generic(Output_section* os, unsigned int type,
1790 Output_data* od, Relobj* relobj,
1791 unsigned int shndx, uint64_t address,
1794 gold_assert(addend == 0);
1795 Sized_relobj<size, big_endian>* sized_relobj =
1796 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1797 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
1798 convert_types<Address, uint64_t>(address),
1802 // As above, but the reloc TYPE is relative
1805 add_output_section_relative(Output_section* os, unsigned int type,
1806 Output_data* od, Address address)
1807 { this->add(od, Output_reloc_type(os, type, od, address, true)); }
1810 add_output_section_relative(Output_section* os, unsigned int type,
1812 Sized_relobj<size, big_endian>* relobj,
1813 unsigned int shndx, Address address)
1814 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, true)); }
1816 // Add an absolute relocation.
1819 add_absolute(unsigned int type, Output_data* od, Address address)
1820 { this->add(od, Output_reloc_type(type, od, address, false)); }
1823 add_absolute(unsigned int type, Output_data* od,
1824 Sized_relobj<size, big_endian>* relobj,
1825 unsigned int shndx, Address address)
1826 { this->add(od, Output_reloc_type(type, relobj, shndx, address, false)); }
1828 // Add a relative relocation
1831 add_relative(unsigned int type, Output_data* od, Address address)
1832 { this->add(od, Output_reloc_type(type, od, address, true)); }
1835 add_relative(unsigned int type, Output_data* od,
1836 Sized_relobj<size, big_endian>* relobj,
1837 unsigned int shndx, Address address)
1838 { this->add(od, Output_reloc_type(type, relobj, shndx, address, true)); }
1840 // Add a target specific relocation. A target which calls this must
1841 // define the reloc_symbol_index and reloc_addend virtual functions.
1844 add_target_specific(unsigned int type, void* arg, Output_data* od,
1846 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1849 add_target_specific(unsigned int type, void* arg, Output_data* od,
1850 Sized_relobj<size, big_endian>* relobj,
1851 unsigned int shndx, Address address)
1852 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1855 // The SHT_RELA version of Output_data_reloc.
1857 template<bool dynamic, int size, bool big_endian>
1858 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1859 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1862 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1866 typedef typename Base::Output_reloc_type Output_reloc_type;
1867 typedef typename Output_reloc_type::Address Address;
1868 typedef typename Output_reloc_type::Addend Addend;
1870 Output_data_reloc(bool sr)
1871 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1874 // Add a reloc against a global symbol.
1877 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1878 Address address, Addend addend)
1880 this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1881 false, false, false));
1885 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1886 Sized_relobj<size, big_endian>* relobj,
1887 unsigned int shndx, Address address,
1890 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1891 addend, false, false, false));
1895 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1896 uint64_t address, uint64_t addend)
1898 this->add(od, Output_reloc_type(gsym, type, od,
1899 convert_types<Address, uint64_t>(address),
1900 convert_types<Addend, uint64_t>(addend),
1901 false, false, false));
1905 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1906 Relobj* relobj, unsigned int shndx, uint64_t address,
1909 Sized_relobj<size, big_endian>* sized_relobj =
1910 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1911 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1912 convert_types<Address, uint64_t>(address),
1913 convert_types<Addend, uint64_t>(addend),
1914 false, false, false));
1917 // Add a RELATIVE reloc against a global symbol. The final output
1918 // relocation will not reference the symbol, but we must keep the symbol
1919 // information long enough to set the addend of the relocation correctly
1920 // when it is written.
1923 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1924 Address address, Addend addend, bool use_plt_offset)
1926 this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1927 true, use_plt_offset));
1931 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1932 Sized_relobj<size, big_endian>* relobj,
1933 unsigned int shndx, Address address, Addend addend,
1934 bool use_plt_offset)
1936 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1937 addend, true, true, use_plt_offset));
1940 // Add a global relocation which does not use a symbol for the relocation,
1941 // but which gets its addend from a symbol.
1944 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1945 Address address, Addend addend)
1947 this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1948 false, true, false));
1952 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1954 Sized_relobj<size, big_endian>* relobj,
1955 unsigned int shndx, Address address,
1958 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1959 addend, false, true, false));
1962 // Add a reloc against a local symbol.
1965 add_local(Sized_relobj<size, big_endian>* relobj,
1966 unsigned int local_sym_index, unsigned int type,
1967 Output_data* od, Address address, Addend addend)
1969 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1970 addend, false, false, false, false));
1974 add_local(Sized_relobj<size, big_endian>* relobj,
1975 unsigned int local_sym_index, unsigned int type,
1976 Output_data* od, unsigned int shndx, Address address,
1979 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1980 address, addend, false, false, false,
1985 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1986 unsigned int type, Output_data* od, uint64_t address,
1989 Sized_relobj<size, big_endian>* sized_relobj =
1990 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1991 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1992 convert_types<Address, uint64_t>(address),
1993 convert_types<Addend, uint64_t>(addend),
1994 false, false, false, false));
1998 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1999 unsigned int type, Output_data* od, unsigned int shndx,
2000 uint64_t address, uint64_t addend)
2002 Sized_relobj<size, big_endian>* sized_relobj =
2003 static_cast<Sized_relobj<size, big_endian>*>(relobj);
2004 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
2005 convert_types<Address, uint64_t>(address),
2006 convert_types<Addend, uint64_t>(addend),
2007 false, false, false, false));
2010 // Add a RELATIVE reloc against a local symbol.
2013 add_local_relative(Sized_relobj<size, big_endian>* relobj,
2014 unsigned int local_sym_index, unsigned int type,
2015 Output_data* od, Address address, Addend addend,
2016 bool use_plt_offset)
2018 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2019 addend, true, true, false,
2024 add_local_relative(Sized_relobj<size, big_endian>* relobj,
2025 unsigned int local_sym_index, unsigned int type,
2026 Output_data* od, unsigned int shndx, Address address,
2027 Addend addend, bool use_plt_offset)
2029 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2030 address, addend, true, true, false,
2034 // Add a local relocation which does not use a symbol for the relocation,
2035 // but which gets it's addend from a symbol.
2038 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2039 unsigned int local_sym_index, unsigned int type,
2040 Output_data* od, Address address, Addend addend)
2042 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2043 addend, false, true, false, false));
2047 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2048 unsigned int local_sym_index, unsigned int type,
2049 Output_data* od, unsigned int shndx,
2050 Address address, Addend addend)
2052 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2053 address, addend, false, true, false,
2057 // Add a reloc against a local section symbol. This will be
2058 // converted into a reloc against the STT_SECTION symbol of the
2062 add_local_section(Sized_relobj<size, big_endian>* relobj,
2063 unsigned int input_shndx, unsigned int type,
2064 Output_data* od, Address address, Addend addend)
2066 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
2067 addend, false, false, true, false));
2071 add_local_section(Sized_relobj<size, big_endian>* relobj,
2072 unsigned int input_shndx, unsigned int type,
2073 Output_data* od, unsigned int shndx, Address address,
2076 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
2077 address, addend, false, false, true,
2081 // A reloc against the STT_SECTION symbol of an output section.
2084 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2085 Address address, Addend addend)
2086 { this->add(od, Output_reloc_type(os, type, od, address, addend, false)); }
2089 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2090 Sized_relobj<size, big_endian>* relobj,
2091 unsigned int shndx, Address address, Addend addend)
2093 this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
2098 add_output_section_generic(Output_section* os, unsigned int type,
2099 Output_data* od, uint64_t address,
2102 this->add(od, Output_reloc_type(os, type, od,
2103 convert_types<Address, uint64_t>(address),
2104 convert_types<Addend, uint64_t>(addend),
2109 add_output_section_generic(Output_section* os, unsigned int type,
2110 Output_data* od, Relobj* relobj,
2111 unsigned int shndx, uint64_t address,
2114 Sized_relobj<size, big_endian>* sized_relobj =
2115 static_cast<Sized_relobj<size, big_endian>*>(relobj);
2116 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
2117 convert_types<Address, uint64_t>(address),
2118 convert_types<Addend, uint64_t>(addend),
2122 // As above, but the reloc TYPE is relative
2125 add_output_section_relative(Output_section* os, unsigned int type,
2126 Output_data* od, Address address, Addend addend)
2127 { this->add(od, Output_reloc_type(os, type, od, address, addend, true)); }
2130 add_output_section_relative(Output_section* os, unsigned int type,
2132 Sized_relobj<size, big_endian>* relobj,
2133 unsigned int shndx, Address address,
2136 this->add(od, Output_reloc_type(os, type, relobj, shndx,
2137 address, addend, true));
2140 // Add an absolute relocation.
2143 add_absolute(unsigned int type, Output_data* od, Address address,
2145 { this->add(od, Output_reloc_type(type, od, address, addend, false)); }
2148 add_absolute(unsigned int type, Output_data* od,
2149 Sized_relobj<size, big_endian>* relobj,
2150 unsigned int shndx, Address address, Addend addend)
2152 this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2156 // Add a relative relocation
2159 add_relative(unsigned int type, Output_data* od, Address address,
2161 { this->add(od, Output_reloc_type(type, od, address, addend, true)); }
2164 add_relative(unsigned int type, Output_data* od,
2165 Sized_relobj<size, big_endian>* relobj,
2166 unsigned int shndx, Address address, Addend addend)
2168 this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2172 // Add a target specific relocation. A target which calls this must
2173 // define the reloc_symbol_index and reloc_addend virtual functions.
2176 add_target_specific(unsigned int type, void* arg, Output_data* od,
2177 Address address, Addend addend)
2178 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
2181 add_target_specific(unsigned int type, void* arg, Output_data* od,
2182 Sized_relobj<size, big_endian>* relobj,
2183 unsigned int shndx, Address address, Addend addend)
2185 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
2190 // Output_relocatable_relocs represents a relocation section in a
2191 // relocatable link. The actual data is written out in the target
2192 // hook relocate_relocs. This just saves space for it.
2194 template<int sh_type, int size, bool big_endian>
2195 class Output_relocatable_relocs : public Output_section_data
2198 Output_relocatable_relocs(Relocatable_relocs* rr)
2199 : Output_section_data(Output_data::default_alignment_for_size(size)),
2204 set_final_data_size();
2206 // Write out the data. There is nothing to do here.
2208 do_write(Output_file*)
2211 // Write to a map file.
2213 do_print_to_mapfile(Mapfile* mapfile) const
2214 { mapfile->print_output_data(this, _("** relocs")); }
2217 // The relocs associated with this input section.
2218 Relocatable_relocs* rr_;
2221 // Handle a GROUP section.
2223 template<int size, bool big_endian>
2224 class Output_data_group : public Output_section_data
2227 // The constructor clears *INPUT_SHNDXES.
2228 Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
2229 section_size_type entry_count,
2230 elfcpp::Elf_Word flags,
2231 std::vector<unsigned int>* input_shndxes);
2234 do_write(Output_file*);
2236 // Write to a map file.
2238 do_print_to_mapfile(Mapfile* mapfile) const
2239 { mapfile->print_output_data(this, _("** group")); }
2241 // Set final data size.
2243 set_final_data_size()
2244 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
2247 // The input object.
2248 Sized_relobj_file<size, big_endian>* relobj_;
2249 // The group flag word.
2250 elfcpp::Elf_Word flags_;
2251 // The section indexes of the input sections in this group.
2252 std::vector<unsigned int> input_shndxes_;
2255 // Output_data_got is used to manage a GOT. Each entry in the GOT is
2256 // for one symbol--either a global symbol or a local symbol in an
2257 // object. The target specific code adds entries to the GOT as
2258 // needed. The GOT_SIZE template parameter is the size in bits of a
2259 // GOT entry, typically 32 or 64.
2261 class Output_data_got_base : public Output_section_data_build
2264 Output_data_got_base(uint64_t align)
2265 : Output_section_data_build(align)
2268 Output_data_got_base(off_t data_size, uint64_t align)
2269 : Output_section_data_build(data_size, align)
2272 // Reserve the slot at index I in the GOT.
2274 reserve_slot(unsigned int i)
2275 { this->do_reserve_slot(i); }
2278 // Reserve the slot at index I in the GOT.
2280 do_reserve_slot(unsigned int i) = 0;
2283 template<int got_size, bool big_endian>
2284 class Output_data_got : public Output_data_got_base
2287 typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
2290 : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
2291 entries_(), free_list_()
2294 Output_data_got(off_t data_size)
2295 : Output_data_got_base(data_size,
2296 Output_data::default_alignment_for_size(got_size)),
2297 entries_(), free_list_()
2299 // For an incremental update, we have an existing GOT section.
2300 // Initialize the list of entries and the free list.
2301 this->entries_.resize(data_size / (got_size / 8));
2302 this->free_list_.init(data_size, false);
2305 // Add an entry for a global symbol to the GOT. Return true if this
2306 // is a new GOT entry, false if the symbol was already in the GOT.
2308 add_global(Symbol* gsym, unsigned int got_type);
2310 // Like add_global, but use the PLT offset of the global symbol if
2313 add_global_plt(Symbol* gsym, unsigned int got_type);
2315 // Like add_global, but for a TLS symbol where the value will be
2316 // offset using Target::tls_offset_for_global.
2318 add_global_tls(Symbol* gsym, unsigned int got_type)
2319 { return add_global_plt(gsym, got_type); }
2321 // Add an entry for a global symbol to the GOT, and add a dynamic
2322 // relocation of type R_TYPE for the GOT entry.
2324 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2325 Output_data_reloc_generic* rel_dyn, unsigned int r_type);
2327 // Add a pair of entries for a global symbol to the GOT, and add
2328 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2330 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2331 Output_data_reloc_generic* rel_dyn,
2332 unsigned int r_type_1, unsigned int r_type_2);
2334 // Add an entry for a local symbol to the GOT. This returns true if
2335 // this is a new GOT entry, false if the symbol already has a GOT
2338 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type);
2340 // Like add_local, but use the PLT offset of the local symbol if it
2343 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type);
2345 // Like add_local, but for a TLS symbol where the value will be
2346 // offset using Target::tls_offset_for_local.
2348 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2349 { return add_local_plt(object, sym_index, got_type); }
2351 // Add an entry for a local symbol to the GOT, and add a dynamic
2352 // relocation of type R_TYPE for the GOT entry.
2354 add_local_with_rel(Relobj* object, unsigned int sym_index,
2355 unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2356 unsigned int r_type);
2358 // Add a pair of entries for a local symbol to the GOT, and add
2359 // a dynamic relocation of type R_TYPE using the section symbol of
2360 // the output section to which input section SHNDX maps, on the first.
2361 // The first got entry will have a value of zero, the second the
2362 // value of the local symbol.
2364 add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2365 unsigned int shndx, unsigned int got_type,
2366 Output_data_reloc_generic* rel_dyn,
2367 unsigned int r_type);
2369 // Add a pair of entries for a local symbol to the GOT, and add
2370 // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
2371 // The first got entry will have a value of zero, the second the
2372 // value of the local symbol offset by Target::tls_offset_for_local.
2374 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2375 unsigned int got_type,
2376 Output_data_reloc_generic* rel_dyn,
2377 unsigned int r_type);
2379 // Add a constant to the GOT. This returns the offset of the new
2380 // entry from the start of the GOT.
2382 add_constant(Valtype constant)
2383 { return this->add_got_entry(Got_entry(constant)); }
2385 // Add a pair of constants to the GOT. This returns the offset of
2386 // the new entry from the start of the GOT.
2388 add_constant_pair(Valtype c1, Valtype c2)
2389 { return this->add_got_entry_pair(Got_entry(c1), Got_entry(c2)); }
2391 // Replace GOT entry I with a new constant.
2393 replace_constant(unsigned int i, Valtype constant)
2395 this->replace_got_entry(i, Got_entry(constant));
2398 // Reserve a slot in the GOT for a local symbol.
2400 reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2401 unsigned int got_type);
2403 // Reserve a slot in the GOT for a global symbol.
2405 reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2408 // Write out the GOT table.
2410 do_write(Output_file*);
2412 // Write to a map file.
2414 do_print_to_mapfile(Mapfile* mapfile) const
2415 { mapfile->print_output_data(this, _("** GOT")); }
2417 // Reserve the slot at index I in the GOT.
2419 do_reserve_slot(unsigned int i)
2420 { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2422 // Return the number of words in the GOT.
2424 num_entries () const
2425 { return this->entries_.size(); }
2427 // Return the offset into the GOT of GOT entry I.
2429 got_offset(unsigned int i) const
2430 { return i * (got_size / 8); }
2433 // This POD class holds a single GOT entry.
2437 // Create a zero entry.
2439 : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false)
2440 { this->u_.constant = 0; }
2442 // Create a global symbol entry.
2443 Got_entry(Symbol* gsym, bool use_plt_or_tls_offset)
2444 : local_sym_index_(GSYM_CODE),
2445 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2446 { this->u_.gsym = gsym; }
2448 // Create a local symbol entry.
2449 Got_entry(Relobj* object, unsigned int local_sym_index,
2450 bool use_plt_or_tls_offset)
2451 : local_sym_index_(local_sym_index),
2452 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2454 gold_assert(local_sym_index != GSYM_CODE
2455 && local_sym_index != CONSTANT_CODE
2456 && local_sym_index != RESERVED_CODE
2457 && local_sym_index == this->local_sym_index_);
2458 this->u_.object = object;
2461 // Create a constant entry. The constant is a host value--it will
2462 // be swapped, if necessary, when it is written out.
2463 explicit Got_entry(Valtype constant)
2464 : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
2465 { this->u_.constant = constant; }
2467 // Write the GOT entry to an output view.
2469 write(unsigned int got_indx, unsigned char* pov) const;
2474 GSYM_CODE = 0x7fffffff,
2475 CONSTANT_CODE = 0x7ffffffe,
2476 RESERVED_CODE = 0x7ffffffd
2481 // For a local symbol, the object.
2483 // For a global symbol, the symbol.
2485 // For a constant, the constant.
2488 // For a local symbol, the local symbol index. This is GSYM_CODE
2489 // for a global symbol, or CONSTANT_CODE for a constant.
2490 unsigned int local_sym_index_ : 31;
2491 // Whether to use the PLT offset of the symbol if it has one.
2492 // For TLS symbols, whether to offset the symbol value.
2493 bool use_plt_or_tls_offset_ : 1;
2496 typedef std::vector<Got_entry> Got_entries;
2498 // Create a new GOT entry and return its offset.
2500 add_got_entry(Got_entry got_entry);
2502 // Create a pair of new GOT entries and return the offset of the first.
2504 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2506 // Replace GOT entry I with a new value.
2508 replace_got_entry(unsigned int i, Got_entry got_entry);
2510 // Return the offset into the GOT of the last entry added.
2512 last_got_offset() const
2513 { return this->got_offset(this->num_entries() - 1); }
2515 // Set the size of the section.
2518 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2520 // The list of GOT entries.
2521 Got_entries entries_;
2523 // List of available regions within the section, for incremental
2525 Free_list free_list_;
2528 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2531 class Output_data_dynamic : public Output_section_data
2534 Output_data_dynamic(Stringpool* pool)
2535 : Output_section_data(Output_data::default_alignment()),
2536 entries_(), pool_(pool)
2539 // Add a new dynamic entry with a fixed numeric value.
2541 add_constant(elfcpp::DT tag, unsigned int val)
2542 { this->add_entry(Dynamic_entry(tag, val)); }
2544 // Add a new dynamic entry with the address of output data.
2546 add_section_address(elfcpp::DT tag, const Output_data* od)
2547 { this->add_entry(Dynamic_entry(tag, od, false)); }
2549 // Add a new dynamic entry with the address of output data
2550 // plus a constant offset.
2552 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2553 unsigned int offset)
2554 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2556 // Add a new dynamic entry with the size of output data.
2558 add_section_size(elfcpp::DT tag, const Output_data* od)
2559 { this->add_entry(Dynamic_entry(tag, od, true)); }
2561 // Add a new dynamic entry with the total size of two output datas.
2563 add_section_size(elfcpp::DT tag, const Output_data* od,
2564 const Output_data* od2)
2565 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2567 // Add a new dynamic entry with the address of a symbol.
2569 add_symbol(elfcpp::DT tag, const Symbol* sym)
2570 { this->add_entry(Dynamic_entry(tag, sym)); }
2572 // Add a new dynamic entry with a string.
2574 add_string(elfcpp::DT tag, const char* str)
2575 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2578 add_string(elfcpp::DT tag, const std::string& str)
2579 { this->add_string(tag, str.c_str()); }
2582 // Adjust the output section to set the entry size.
2584 do_adjust_output_section(Output_section*);
2586 // Set the final data size.
2588 set_final_data_size();
2590 // Write out the dynamic entries.
2592 do_write(Output_file*);
2594 // Write to a map file.
2596 do_print_to_mapfile(Mapfile* mapfile) const
2597 { mapfile->print_output_data(this, _("** dynamic")); }
2600 // This POD class holds a single dynamic entry.
2604 // Create an entry with a fixed numeric value.
2605 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2606 : tag_(tag), offset_(DYNAMIC_NUMBER)
2607 { this->u_.val = val; }
2609 // Create an entry with the size or address of a section.
2610 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2612 offset_(section_size
2613 ? DYNAMIC_SECTION_SIZE
2614 : DYNAMIC_SECTION_ADDRESS)
2620 // Create an entry with the size of two sections.
2621 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2623 offset_(DYNAMIC_SECTION_SIZE)
2629 // Create an entry with the address of a section plus a constant offset.
2630 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2633 { this->u_.od = od; }
2635 // Create an entry with the address of a symbol.
2636 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2637 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2638 { this->u_.sym = sym; }
2640 // Create an entry with a string.
2641 Dynamic_entry(elfcpp::DT tag, const char* str)
2642 : tag_(tag), offset_(DYNAMIC_STRING)
2643 { this->u_.str = str; }
2645 // Return the tag of this entry.
2648 { return this->tag_; }
2650 // Write the dynamic entry to an output view.
2651 template<int size, bool big_endian>
2653 write(unsigned char* pov, const Stringpool*) const;
2656 // Classification is encoded in the OFFSET field.
2660 DYNAMIC_SECTION_ADDRESS = 0,
2662 DYNAMIC_NUMBER = -1U,
2664 DYNAMIC_SECTION_SIZE = -2U,
2666 DYNAMIC_SYMBOL = -3U,
2668 DYNAMIC_STRING = -4U
2669 // Any other value indicates a section address plus OFFSET.
2674 // For DYNAMIC_NUMBER.
2676 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2677 const Output_data* od;
2678 // For DYNAMIC_SYMBOL.
2680 // For DYNAMIC_STRING.
2683 // For DYNAMIC_SYMBOL with two sections.
2684 const Output_data* od2;
2687 // The type of entry (Classification) or offset within a section.
2688 unsigned int offset_;
2691 // Add an entry to the list.
2693 add_entry(const Dynamic_entry& entry)
2694 { this->entries_.push_back(entry); }
2696 // Sized version of write function.
2697 template<int size, bool big_endian>
2699 sized_write(Output_file* of);
2701 // The type of the list of entries.
2702 typedef std::vector<Dynamic_entry> Dynamic_entries;
2705 Dynamic_entries entries_;
2706 // The pool used for strings.
2710 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2711 // which may be required if the object file has more than
2712 // SHN_LORESERVE sections.
2714 class Output_symtab_xindex : public Output_section_data
2717 Output_symtab_xindex(size_t symcount)
2718 : Output_section_data(symcount * 4, 4, true),
2722 // Add an entry: symbol number SYMNDX has section SHNDX.
2724 add(unsigned int symndx, unsigned int shndx)
2725 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2729 do_write(Output_file*);
2731 // Write to a map file.
2733 do_print_to_mapfile(Mapfile* mapfile) const
2734 { mapfile->print_output_data(this, _("** symtab xindex")); }
2737 template<bool big_endian>
2739 endian_do_write(unsigned char*);
2741 // It is likely that most symbols will not require entries. Rather
2742 // than keep a vector for all symbols, we keep pairs of symbol index
2743 // and section index.
2744 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2746 // The entries we need.
2747 Xindex_entries entries_;
2750 // A relaxed input section.
2751 class Output_relaxed_input_section : public Output_section_data_build
2754 // We would like to call relobj->section_addralign(shndx) to get the
2755 // alignment but we do not want the constructor to fail. So callers
2756 // are repsonsible for ensuring that.
2757 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2759 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2762 // Return the Relobj of this relaxed input section.
2765 { return this->relobj_; }
2767 // Return the section index of this relaxed input section.
2770 { return this->shndx_; }
2774 set_relobj(Relobj* relobj)
2775 { this->relobj_ = relobj; }
2778 set_shndx(unsigned int shndx)
2779 { this->shndx_ = shndx; }
2783 unsigned int shndx_;
2786 // This class describes properties of merge data sections. It is used
2787 // as a key type for maps.
2788 class Merge_section_properties
2791 Merge_section_properties(bool is_string, uint64_t entsize,
2793 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2796 // Whether this equals to another Merge_section_properties MSP.
2798 eq(const Merge_section_properties& msp) const
2800 return ((this->is_string_ == msp.is_string_)
2801 && (this->entsize_ == msp.entsize_)
2802 && (this->addralign_ == msp.addralign_));
2805 // Compute a hash value for this using 64-bit FNV-1a hash.
2809 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2810 uint64_t prime = 1099511628211ULL;
2811 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2812 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2813 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2817 // Functors for associative containers.
2821 operator()(const Merge_section_properties& msp1,
2822 const Merge_section_properties& msp2) const
2823 { return msp1.eq(msp2); }
2829 operator()(const Merge_section_properties& msp) const
2830 { return msp.hash_value(); }
2834 // Whether this merge data section is for strings.
2836 // Entsize of this merge data section.
2838 // Address alignment.
2839 uint64_t addralign_;
2842 // This class is used to speed up look up of special input sections in an
2845 class Output_section_lookup_maps
2848 Output_section_lookup_maps()
2849 : is_valid_(true), merge_sections_by_properties_(),
2850 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2853 // Whether the maps are valid.
2856 { return this->is_valid_; }
2858 // Invalidate the maps.
2861 { this->is_valid_ = false; }
2867 this->merge_sections_by_properties_.clear();
2868 this->merge_sections_by_id_.clear();
2869 this->relaxed_input_sections_by_id_.clear();
2870 // A cleared map is valid.
2871 this->is_valid_ = true;
2874 // Find a merge section by merge section properties. Return NULL if none
2877 find_merge_section(const Merge_section_properties& msp) const
2879 gold_assert(this->is_valid_);
2880 Merge_sections_by_properties::const_iterator p =
2881 this->merge_sections_by_properties_.find(msp);
2882 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2885 // Find a merge section by section ID of a merge input section. Return NULL
2886 // if none is found.
2888 find_merge_section(const Object* object, unsigned int shndx) const
2890 gold_assert(this->is_valid_);
2891 Merge_sections_by_id::const_iterator p =
2892 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2893 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2896 // Add a merge section pointed by POMB with properties MSP.
2898 add_merge_section(const Merge_section_properties& msp,
2899 Output_merge_base* pomb)
2901 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2902 std::pair<Merge_sections_by_properties::iterator, bool> result =
2903 this->merge_sections_by_properties_.insert(value);
2904 gold_assert(result.second);
2907 // Add a mapping from a merged input section in OBJECT with index SHNDX
2908 // to a merge output section pointed by POMB.
2910 add_merge_input_section(const Object* object, unsigned int shndx,
2911 Output_merge_base* pomb)
2913 Const_section_id csid(object, shndx);
2914 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2915 std::pair<Merge_sections_by_id::iterator, bool> result =
2916 this->merge_sections_by_id_.insert(value);
2917 gold_assert(result.second);
2920 // Find a relaxed input section of OBJECT with index SHNDX.
2921 Output_relaxed_input_section*
2922 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2924 gold_assert(this->is_valid_);
2925 Relaxed_input_sections_by_id::const_iterator p =
2926 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2927 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2930 // Add a relaxed input section pointed by POMB and whose original input
2931 // section is in OBJECT with index SHNDX.
2933 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2934 Output_relaxed_input_section* poris)
2936 Const_section_id csid(relobj, shndx);
2937 std::pair<Const_section_id, Output_relaxed_input_section*>
2939 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2940 this->relaxed_input_sections_by_id_.insert(value);
2941 gold_assert(result.second);
2945 typedef Unordered_map<Const_section_id, Output_merge_base*,
2946 Const_section_id_hash>
2947 Merge_sections_by_id;
2949 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2950 Merge_section_properties::hash,
2951 Merge_section_properties::equal_to>
2952 Merge_sections_by_properties;
2954 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2955 Const_section_id_hash>
2956 Relaxed_input_sections_by_id;
2958 // Whether this is valid
2960 // Merge sections by merge section properties.
2961 Merge_sections_by_properties merge_sections_by_properties_;
2962 // Merge sections by section IDs.
2963 Merge_sections_by_id merge_sections_by_id_;
2964 // Relaxed sections by section IDs.
2965 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2968 // This abstract base class defines the interface for the
2969 // types of methods used to fill free space left in an output
2970 // section during an incremental link. These methods are used
2971 // to insert dummy compilation units into debug info so that
2972 // debug info consumers can scan the debug info serially.
2978 : is_big_endian_(parameters->target().is_big_endian())
2985 // Return the smallest size chunk of free space that can be
2986 // filled with a dummy compilation unit.
2988 minimum_hole_size() const
2989 { return this->do_minimum_hole_size(); }
2991 // Write a fill pattern of length LEN at offset OFF in the file.
2993 write(Output_file* of, off_t off, size_t len) const
2994 { this->do_write(of, off, len); }
2998 do_minimum_hole_size() const = 0;
3001 do_write(Output_file* of, off_t off, size_t len) const = 0;
3004 is_big_endian() const
3005 { return this->is_big_endian_; }
3008 bool is_big_endian_;
3011 // Fill method that introduces a dummy compilation unit in
3012 // a .debug_info or .debug_types section.
3014 class Output_fill_debug_info : public Output_fill
3017 Output_fill_debug_info(bool is_debug_types)
3018 : is_debug_types_(is_debug_types)
3023 do_minimum_hole_size() const;
3026 do_write(Output_file* of, off_t off, size_t len) const;
3029 // Version of the header.
3030 static const int version = 4;
3031 // True if this is a .debug_types section.
3032 bool is_debug_types_;
3035 // Fill method that introduces a dummy compilation unit in
3036 // a .debug_line section.
3038 class Output_fill_debug_line : public Output_fill
3041 Output_fill_debug_line()
3046 do_minimum_hole_size() const;
3049 do_write(Output_file* of, off_t off, size_t len) const;
3052 // Version of the header. We write a DWARF-3 header because it's smaller
3053 // and many tools have not yet been updated to understand the DWARF-4 header.
3054 static const int version = 3;
3055 // Length of the portion of the header that follows the header_length
3056 // field. This includes the following fields:
3057 // minimum_instruction_length, default_is_stmt, line_base, line_range,
3058 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3059 // The standard_opcode_lengths array is 12 bytes long, and the
3060 // include_directories and filenames fields each contain only a single
3062 static const size_t header_length = 19;
3065 // An output section. We don't expect to have too many output
3066 // sections, so we don't bother to do a template on the size.
3068 class Output_section : public Output_data
3071 // Create an output section, giving the name, type, and flags.
3072 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
3073 virtual ~Output_section();
3075 // Add a new input section SHNDX, named NAME, with header SHDR, from
3076 // object OBJECT. RELOC_SHNDX is the index of a relocation section
3077 // which applies to this section, or 0 if none, or -1 if more than
3078 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3079 // in a linker script; in that case we need to keep track of input
3080 // sections associated with an output section. Return the offset
3081 // within the output section.
3082 template<int size, bool big_endian>
3084 add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
3085 unsigned int shndx, const char* name,
3086 const elfcpp::Shdr<size, big_endian>& shdr,
3087 unsigned int reloc_shndx, bool have_sections_script);
3089 // Add generated data POSD to this output section.
3091 add_output_section_data(Output_section_data* posd);
3093 // Add a relaxed input section PORIS called NAME to this output section
3096 add_relaxed_input_section(Layout* layout,
3097 Output_relaxed_input_section* poris,
3098 const std::string& name);
3100 // Return the section name.
3103 { return this->name_; }
3105 // Return the section type.
3108 { return this->type_; }
3110 // Return the section flags.
3113 { return this->flags_; }
3115 typedef std::map<Section_id, unsigned int> Section_layout_order;
3118 update_section_layout(const Section_layout_order* order_map);
3120 // Update the output section flags based on input section flags.
3122 update_flags_for_input_section(elfcpp::Elf_Xword flags);
3124 // Return the entsize field.
3127 { return this->entsize_; }
3129 // Set the entsize field.
3131 set_entsize(uint64_t v);
3133 // Set the load address.
3135 set_load_address(uint64_t load_address)
3137 this->load_address_ = load_address;
3138 this->has_load_address_ = true;
3141 // Set the link field to the output section index of a section.
3143 set_link_section(const Output_data* od)
3145 gold_assert(this->link_ == 0
3146 && !this->should_link_to_symtab_
3147 && !this->should_link_to_dynsym_);
3148 this->link_section_ = od;
3151 // Set the link field to a constant.
3153 set_link(unsigned int v)
3155 gold_assert(this->link_section_ == NULL
3156 && !this->should_link_to_symtab_
3157 && !this->should_link_to_dynsym_);
3161 // Record that this section should link to the normal symbol table.
3163 set_should_link_to_symtab()
3165 gold_assert(this->link_section_ == NULL
3167 && !this->should_link_to_dynsym_);
3168 this->should_link_to_symtab_ = true;
3171 // Record that this section should link to the dynamic symbol table.
3173 set_should_link_to_dynsym()
3175 gold_assert(this->link_section_ == NULL
3177 && !this->should_link_to_symtab_);
3178 this->should_link_to_dynsym_ = true;
3181 // Return the info field.
3185 gold_assert(this->info_section_ == NULL
3186 && this->info_symndx_ == NULL);
3190 // Set the info field to the output section index of a section.
3192 set_info_section(const Output_section* os)
3194 gold_assert((this->info_section_ == NULL
3195 || (this->info_section_ == os
3196 && this->info_uses_section_index_))
3197 && this->info_symndx_ == NULL
3198 && this->info_ == 0);
3199 this->info_section_ = os;
3200 this->info_uses_section_index_= true;
3203 // Set the info field to the symbol table index of a symbol.
3205 set_info_symndx(const Symbol* sym)
3207 gold_assert(this->info_section_ == NULL
3208 && (this->info_symndx_ == NULL
3209 || this->info_symndx_ == sym)
3210 && this->info_ == 0);
3211 this->info_symndx_ = sym;
3214 // Set the info field to the symbol table index of a section symbol.
3216 set_info_section_symndx(const Output_section* os)
3218 gold_assert((this->info_section_ == NULL
3219 || (this->info_section_ == os
3220 && !this->info_uses_section_index_))
3221 && this->info_symndx_ == NULL
3222 && this->info_ == 0);
3223 this->info_section_ = os;
3224 this->info_uses_section_index_ = false;
3227 // Set the info field to a constant.
3229 set_info(unsigned int v)
3231 gold_assert(this->info_section_ == NULL
3232 && this->info_symndx_ == NULL
3233 && (this->info_ == 0
3234 || this->info_ == v));
3238 // Set the addralign field.
3240 set_addralign(uint64_t v)
3241 { this->addralign_ = v; }
3244 checkpoint_set_addralign(uint64_t val)
3246 if (this->checkpoint_ != NULL)
3247 this->checkpoint_->set_addralign(val);
3250 // Whether the output section index has been set.
3252 has_out_shndx() const
3253 { return this->out_shndx_ != -1U; }
3255 // Indicate that we need a symtab index.
3257 set_needs_symtab_index()
3258 { this->needs_symtab_index_ = true; }
3260 // Return whether we need a symtab index.
3262 needs_symtab_index() const
3263 { return this->needs_symtab_index_; }
3265 // Get the symtab index.
3267 symtab_index() const
3269 gold_assert(this->symtab_index_ != 0);
3270 return this->symtab_index_;
3273 // Set the symtab index.
3275 set_symtab_index(unsigned int index)
3277 gold_assert(index != 0);
3278 this->symtab_index_ = index;
3281 // Indicate that we need a dynsym index.
3283 set_needs_dynsym_index()
3284 { this->needs_dynsym_index_ = true; }
3286 // Return whether we need a dynsym index.
3288 needs_dynsym_index() const
3289 { return this->needs_dynsym_index_; }
3291 // Get the dynsym index.
3293 dynsym_index() const
3295 gold_assert(this->dynsym_index_ != 0);
3296 return this->dynsym_index_;
3299 // Set the dynsym index.
3301 set_dynsym_index(unsigned int index)
3303 gold_assert(index != 0);
3304 this->dynsym_index_ = index;
3307 // Sort the attached input sections.
3309 sort_attached_input_sections();
3311 // Return whether the input sections sections attachd to this output
3312 // section may require sorting. This is used to handle constructor
3313 // priorities compatibly with GNU ld.
3315 may_sort_attached_input_sections() const
3316 { return this->may_sort_attached_input_sections_; }
3318 // Record that the input sections attached to this output section
3319 // may require sorting.
3321 set_may_sort_attached_input_sections()
3322 { this->may_sort_attached_input_sections_ = true; }
3324 // Returns true if input sections must be sorted according to the
3325 // order in which their name appear in the --section-ordering-file.
3327 input_section_order_specified()
3328 { return this->input_section_order_specified_; }
3330 // Record that input sections must be sorted as some of their names
3331 // match the patterns specified through --section-ordering-file.
3333 set_input_section_order_specified()
3334 { this->input_section_order_specified_ = true; }
3336 // Return whether the input sections attached to this output section
3337 // require sorting. This is used to handle constructor priorities
3338 // compatibly with GNU ld.
3340 must_sort_attached_input_sections() const
3341 { return this->must_sort_attached_input_sections_; }
3343 // Record that the input sections attached to this output section
3346 set_must_sort_attached_input_sections()
3347 { this->must_sort_attached_input_sections_ = true; }
3349 // Get the order in which this section appears in the PT_LOAD output
3351 Output_section_order
3353 { return this->order_; }
3355 // Set the order for this section.
3357 set_order(Output_section_order order)
3358 { this->order_ = order; }
3360 // Return whether this section holds relro data--data which has
3361 // dynamic relocations but which may be marked read-only after the
3362 // dynamic relocations have been completed.
3365 { return this->is_relro_; }
3367 // Record that this section holds relro data.
3370 { this->is_relro_ = true; }
3372 // Record that this section does not hold relro data.
3375 { this->is_relro_ = false; }
3377 // True if this is a small section: a section which holds small
3380 is_small_section() const
3381 { return this->is_small_section_; }
3383 // Record that this is a small section.
3385 set_is_small_section()
3386 { this->is_small_section_ = true; }
3388 // True if this is a large section: a section which holds large
3391 is_large_section() const
3392 { return this->is_large_section_; }
3394 // Record that this is a large section.
3396 set_is_large_section()
3397 { this->is_large_section_ = true; }
3399 // True if this is a large data (not BSS) section.
3401 is_large_data_section()
3402 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3404 // Return whether this section should be written after all the input
3405 // sections are complete.
3407 after_input_sections() const
3408 { return this->after_input_sections_; }
3410 // Record that this section should be written after all the input
3411 // sections are complete.
3413 set_after_input_sections()
3414 { this->after_input_sections_ = true; }
3416 // Return whether this section requires postprocessing after all
3417 // relocations have been applied.
3419 requires_postprocessing() const
3420 { return this->requires_postprocessing_; }
3423 is_unique_segment() const
3424 { return this->is_unique_segment_; }
3427 set_is_unique_segment()
3428 { this->is_unique_segment_ = true; }
3430 uint64_t extra_segment_flags() const
3431 { return this->extra_segment_flags_; }
3434 set_extra_segment_flags(uint64_t flags)
3435 { this->extra_segment_flags_ = flags; }
3437 uint64_t segment_alignment() const
3438 { return this->segment_alignment_; }
3441 set_segment_alignment(uint64_t align)
3442 { this->segment_alignment_ = align; }
3444 // If a section requires postprocessing, return the buffer to use.
3446 postprocessing_buffer() const
3448 gold_assert(this->postprocessing_buffer_ != NULL);
3449 return this->postprocessing_buffer_;
3452 // If a section requires postprocessing, create the buffer to use.
3454 create_postprocessing_buffer();
3456 // If a section requires postprocessing, this is the size of the
3457 // buffer to which relocations should be applied.
3459 postprocessing_buffer_size() const
3460 { return this->current_data_size_for_child(); }
3462 // Modify the section name. This is only permitted for an
3463 // unallocated section, and only before the size has been finalized.
3464 // Otherwise the name will not get into Layout::namepool_.
3466 set_name(const char* newname)
3468 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3469 gold_assert(!this->is_data_size_valid());
3470 this->name_ = newname;
3473 // Return whether the offset OFFSET in the input section SHNDX in
3474 // object OBJECT is being included in the link.
3476 is_input_address_mapped(const Relobj* object, unsigned int shndx,
3477 off_t offset) const;
3479 // Return the offset within the output section of OFFSET relative to
3480 // the start of input section SHNDX in object OBJECT.
3482 output_offset(const Relobj* object, unsigned int shndx,
3483 section_offset_type offset) const;
3485 // Return the output virtual address of OFFSET relative to the start
3486 // of input section SHNDX in object OBJECT.
3488 output_address(const Relobj* object, unsigned int shndx,
3489 off_t offset) const;
3491 // Look for the merged section for input section SHNDX in object
3492 // OBJECT. If found, return true, and set *ADDR to the address of
3493 // the start of the merged section. This is not necessary the
3494 // output offset corresponding to input offset 0 in the section,
3495 // since the section may be mapped arbitrarily.
3497 find_starting_output_address(const Relobj* object, unsigned int shndx,
3498 uint64_t* addr) const;
3500 // Record that this output section was found in the SECTIONS clause
3501 // of a linker script.
3503 set_found_in_sections_clause()
3504 { this->found_in_sections_clause_ = true; }
3506 // Return whether this output section was found in the SECTIONS
3507 // clause of a linker script.
3509 found_in_sections_clause() const
3510 { return this->found_in_sections_clause_; }
3512 // Write the section header into *OPHDR.
3513 template<int size, bool big_endian>
3515 write_header(const Layout*, const Stringpool*,
3516 elfcpp::Shdr_write<size, big_endian>*) const;
3518 // The next few calls are for linker script support.
3520 // In some cases we need to keep a list of the input sections
3521 // associated with this output section. We only need the list if we
3522 // might have to change the offsets of the input section within the
3523 // output section after we add the input section. The ordinary
3524 // input sections will be written out when we process the object
3525 // file, and as such we don't need to track them here. We do need
3526 // to track Output_section_data objects here. We store instances of
3527 // this structure in a std::vector, so it must be a POD. There can
3528 // be many instances of this structure, so we use a union to save
3534 : shndx_(0), p2align_(0)
3536 this->u1_.data_size = 0;
3537 this->u2_.object = NULL;
3540 // For an ordinary input section.
3541 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3544 p2align_(ffsll(static_cast<long long>(addralign))),
3545 section_order_index_(0)
3547 gold_assert(shndx != OUTPUT_SECTION_CODE
3548 && shndx != MERGE_DATA_SECTION_CODE
3549 && shndx != MERGE_STRING_SECTION_CODE
3550 && shndx != RELAXED_INPUT_SECTION_CODE);
3551 this->u1_.data_size = data_size;
3552 this->u2_.object = object;
3555 // For a non-merge output section.
3556 Input_section(Output_section_data* posd)
3557 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3558 section_order_index_(0)
3560 this->u1_.data_size = 0;
3561 this->u2_.posd = posd;
3564 // For a merge section.
3565 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3567 ? MERGE_STRING_SECTION_CODE
3568 : MERGE_DATA_SECTION_CODE),
3570 section_order_index_(0)
3572 this->u1_.entsize = entsize;
3573 this->u2_.posd = posd;
3576 // For a relaxed input section.
3577 Input_section(Output_relaxed_input_section* psection)
3578 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3579 section_order_index_(0)
3581 this->u1_.data_size = 0;
3582 this->u2_.poris = psection;
3586 section_order_index() const
3588 return this->section_order_index_;
3592 set_section_order_index(unsigned int number)
3594 this->section_order_index_ = number;
3597 // The required alignment.
3601 if (this->p2align_ != 0)
3602 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3603 else if (!this->is_input_section())
3604 return this->u2_.posd->addralign();
3609 // Set the required alignment, which must be either 0 or a power of 2.
3610 // For input sections that are sub-classes of Output_section_data, a
3611 // alignment of zero means asking the underlying object for alignment.
3613 set_addralign(uint64_t addralign)
3619 gold_assert((addralign & (addralign - 1)) == 0);
3620 this->p2align_ = ffsll(static_cast<long long>(addralign));
3624 // Return the current required size, without finalization.
3626 current_data_size() const;
3628 // Return the required size.
3632 // Whether this is an input section.
3634 is_input_section() const
3636 return (this->shndx_ != OUTPUT_SECTION_CODE
3637 && this->shndx_ != MERGE_DATA_SECTION_CODE
3638 && this->shndx_ != MERGE_STRING_SECTION_CODE
3639 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3642 // Return whether this is a merge section which matches the
3645 is_merge_section(bool is_string, uint64_t entsize,
3646 uint64_t addralign) const
3648 return (this->shndx_ == (is_string
3649 ? MERGE_STRING_SECTION_CODE
3650 : MERGE_DATA_SECTION_CODE)
3651 && this->u1_.entsize == entsize
3652 && this->addralign() == addralign);
3655 // Return whether this is a merge section for some input section.
3657 is_merge_section() const
3659 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3660 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3663 // Return whether this is a relaxed input section.
3665 is_relaxed_input_section() const
3666 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3668 // Return whether this is a generic Output_section_data.
3670 is_output_section_data() const
3672 return this->shndx_ == OUTPUT_SECTION_CODE;
3675 // Return the object for an input section.
3679 // Return the input section index for an input section.
3683 // For non-input-sections, return the associated Output_section_data
3685 Output_section_data*
3686 output_section_data() const
3688 gold_assert(!this->is_input_section());
3689 return this->u2_.posd;
3692 // For a merge section, return the Output_merge_base pointer.
3694 output_merge_base() const
3696 gold_assert(this->is_merge_section());
3697 return this->u2_.pomb;
3700 // Return the Output_relaxed_input_section object.
3701 Output_relaxed_input_section*
3702 relaxed_input_section() const
3704 gold_assert(this->is_relaxed_input_section());
3705 return this->u2_.poris;
3708 // Set the output section.
3710 set_output_section(Output_section* os)
3712 gold_assert(!this->is_input_section());
3713 Output_section_data* posd =
3714 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3715 posd->set_output_section(os);
3718 // Set the address and file offset. This is called during
3719 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3720 // the enclosing section.
3722 set_address_and_file_offset(uint64_t address, off_t file_offset,
3723 off_t section_file_offset);
3725 // Reset the address and file offset.
3727 reset_address_and_file_offset();
3729 // Finalize the data size.
3731 finalize_data_size();
3733 // Add an input section, for SHF_MERGE sections.
3735 add_input_section(Relobj* object, unsigned int shndx)
3737 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3738 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3739 return this->u2_.posd->add_input_section(object, shndx);
3742 // Given an input OBJECT, an input section index SHNDX within that
3743 // object, and an OFFSET relative to the start of that input
3744 // section, return whether or not the output offset is known. If
3745 // this function returns true, it sets *POUTPUT to the offset in
3746 // the output section, relative to the start of the input section
3747 // in the output section. *POUTPUT may be different from OFFSET
3748 // for a merged section.
3750 output_offset(const Relobj* object, unsigned int shndx,
3751 section_offset_type offset,
3752 section_offset_type* poutput) const;
3754 // Return whether this is the merge section for the input section
3757 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3759 // Write out the data. This does nothing for an input section.
3761 write(Output_file*);
3763 // Write the data to a buffer. This does nothing for an input
3766 write_to_buffer(unsigned char*);
3768 // Print to a map file.
3770 print_to_mapfile(Mapfile*) const;
3772 // Print statistics about merge sections to stderr.
3774 print_merge_stats(const char* section_name)
3776 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3777 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3778 this->u2_.posd->print_merge_stats(section_name);
3782 // Code values which appear in shndx_. If the value is not one of
3783 // these codes, it is the input section index in the object file.
3786 // An Output_section_data.
3787 OUTPUT_SECTION_CODE = -1U,
3788 // An Output_section_data for an SHF_MERGE section with
3789 // SHF_STRINGS not set.
3790 MERGE_DATA_SECTION_CODE = -2U,
3791 // An Output_section_data for an SHF_MERGE section with
3793 MERGE_STRING_SECTION_CODE = -3U,
3794 // An Output_section_data for a relaxed input section.
3795 RELAXED_INPUT_SECTION_CODE = -4U
3798 // For an ordinary input section, this is the section index in the
3799 // input file. For an Output_section_data, this is
3800 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3801 // MERGE_STRING_SECTION_CODE.
3802 unsigned int shndx_;
3803 // The required alignment, stored as a power of 2.
3804 unsigned int p2align_;
3807 // For an ordinary input section, the section size.
3809 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3810 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3816 // For an ordinary input section, the object which holds the
3819 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3820 // MERGE_STRING_SECTION_CODE, the data.
3821 Output_section_data* posd;
3822 Output_merge_base* pomb;
3823 // For RELAXED_INPUT_SECTION_CODE, the data.
3824 Output_relaxed_input_section* poris;
3826 // The line number of the pattern it matches in the --section-ordering-file
3827 // file. It is 0 if does not match any pattern.
3828 unsigned int section_order_index_;
3831 // Store the list of input sections for this Output_section into the
3832 // list passed in. This removes the input sections, leaving only
3833 // any Output_section_data elements. This returns the size of those
3834 // Output_section_data elements. ADDRESS is the address of this
3835 // output section. FILL is the fill value to use, in case there are
3836 // any spaces between the remaining Output_section_data elements.
3838 get_input_sections(uint64_t address, const std::string& fill,
3839 std::list<Input_section>*);
3841 // Add a script input section. A script input section can either be
3842 // a plain input section or a sub-class of Output_section_data.
3844 add_script_input_section(const Input_section& input_section);
3846 // Set the current size of the output section.
3848 set_current_data_size(off_t size)
3849 { this->set_current_data_size_for_child(size); }
3851 // End of linker script support.
3853 // Save states before doing section layout.
3854 // This is used for relaxation.
3858 // Restore states prior to section layout.
3866 // Convert existing input sections to relaxed input sections.
3868 convert_input_sections_to_relaxed_sections(
3869 const std::vector<Output_relaxed_input_section*>& sections);
3871 // Find a relaxed input section to an input section in OBJECT
3872 // with index SHNDX. Return NULL if none is found.
3873 const Output_relaxed_input_section*
3874 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3876 // Whether section offsets need adjustment due to relaxation.
3878 section_offsets_need_adjustment() const
3879 { return this->section_offsets_need_adjustment_; }
3881 // Set section_offsets_need_adjustment to be true.
3883 set_section_offsets_need_adjustment()
3884 { this->section_offsets_need_adjustment_ = true; }
3886 // Set section_offsets_need_adjustment to be false.
3888 clear_section_offsets_need_adjustment()
3889 { this->section_offsets_need_adjustment_ = false; }
3891 // Adjust section offsets of input sections in this. This is
3892 // requires if relaxation caused some input sections to change sizes.
3894 adjust_section_offsets();
3896 // Whether this is a NOLOAD section.
3899 { return this->is_noload_; }
3904 { this->is_noload_ = true; }
3906 // Print merge statistics to stderr.
3908 print_merge_stats();
3910 // Set a fixed layout for the section. Used for incremental update links.
3912 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3913 uint64_t sh_addralign);
3915 // Return TRUE if the section has a fixed layout.
3917 has_fixed_layout() const
3918 { return this->has_fixed_layout_; }
3920 // Set flag to allow patch space for this section. Used for full
3921 // incremental links.
3923 set_is_patch_space_allowed()
3924 { this->is_patch_space_allowed_ = true; }
3926 // Set a fill method to use for free space left in the output section
3927 // during incremental links.
3929 set_free_space_fill(Output_fill* free_space_fill)
3931 this->free_space_fill_ = free_space_fill;
3932 this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3935 // Reserve space within the fixed layout for the section. Used for
3936 // incremental update links.
3938 reserve(uint64_t sh_offset, uint64_t sh_size);
3940 // Allocate space from the free list for the section. Used for
3941 // incremental update links.
3943 allocate(off_t len, uint64_t addralign);
3945 typedef std::vector<Input_section> Input_section_list;
3947 // Allow access to the input sections.
3948 const Input_section_list&
3949 input_sections() const
3950 { return this->input_sections_; }
3954 { return this->input_sections_; }
3957 // Return the output section--i.e., the object itself.
3962 const Output_section*
3963 do_output_section() const
3966 // Return the section index in the output file.
3968 do_out_shndx() const
3970 gold_assert(this->out_shndx_ != -1U);
3971 return this->out_shndx_;
3974 // Set the output section index.
3976 do_set_out_shndx(unsigned int shndx)
3978 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3979 this->out_shndx_ = shndx;
3982 // Update the data size of the Output_section. For a typical
3983 // Output_section, there is nothing to do, but if there are any
3984 // Output_section_data objects we need to do a trial layout
3989 // Set the final data size of the Output_section. For a typical
3990 // Output_section, there is nothing to do, but if there are any
3991 // Output_section_data objects we need to set their final addresses
3994 set_final_data_size();
3996 // Reset the address and file offset.
3998 do_reset_address_and_file_offset();
4000 // Return true if address and file offset already have reset values. In
4001 // other words, calling reset_address_and_file_offset will not change them.
4003 do_address_and_file_offset_have_reset_values() const;
4005 // Write the data to the file. For a typical Output_section, this
4006 // does nothing: the data is written out by calling Object::Relocate
4007 // on each input object. But if there are any Output_section_data
4008 // objects we do need to write them out here.
4010 do_write(Output_file*);
4012 // Return the address alignment--function required by parent class.
4014 do_addralign() const
4015 { return this->addralign_; }
4017 // Return whether there is a load address.
4019 do_has_load_address() const
4020 { return this->has_load_address_; }
4022 // Return the load address.
4024 do_load_address() const
4026 gold_assert(this->has_load_address_);
4027 return this->load_address_;
4030 // Return whether this is an Output_section.
4032 do_is_section() const
4035 // Return whether this is a section of the specified type.
4037 do_is_section_type(elfcpp::Elf_Word type) const
4038 { return this->type_ == type; }
4040 // Return whether the specified section flag is set.
4042 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
4043 { return (this->flags_ & flag) != 0; }
4045 // Set the TLS offset. Called only for SHT_TLS sections.
4047 do_set_tls_offset(uint64_t tls_base);
4049 // Return the TLS offset, relative to the base of the TLS segment.
4050 // Valid only for SHT_TLS sections.
4052 do_tls_offset() const
4053 { return this->tls_offset_; }
4055 // This may be implemented by a child class.
4057 do_finalize_name(Layout*)
4060 // Print to the map file.
4062 do_print_to_mapfile(Mapfile*) const;
4064 // Record that this section requires postprocessing after all
4065 // relocations have been applied. This is called by a child class.
4067 set_requires_postprocessing()
4069 this->requires_postprocessing_ = true;
4070 this->after_input_sections_ = true;
4073 // Write all the data of an Output_section into the postprocessing
4076 write_to_postprocessing_buffer();
4078 // Whether this always keeps an input section list
4080 always_keeps_input_sections() const
4081 { return this->always_keeps_input_sections_; }
4083 // Always keep an input section list.
4085 set_always_keeps_input_sections()
4087 gold_assert(this->current_data_size_for_child() == 0);
4088 this->always_keeps_input_sections_ = true;
4092 // We only save enough information to undo the effects of section layout.
4093 class Checkpoint_output_section
4096 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
4097 const Input_section_list& input_sections,
4098 off_t first_input_offset,
4099 bool attached_input_sections_are_sorted)
4100 : addralign_(addralign), flags_(flags),
4101 input_sections_(input_sections),
4102 input_sections_size_(input_sections_.size()),
4103 input_sections_copy_(), first_input_offset_(first_input_offset),
4104 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
4108 ~Checkpoint_output_section()
4111 // Return the address alignment.
4114 { return this->addralign_; }
4117 set_addralign(uint64_t val)
4118 { this->addralign_ = val; }
4120 // Return the section flags.
4123 { return this->flags_; }
4125 // Return a reference to the input section list copy.
4128 { return &this->input_sections_copy_; }
4130 // Return the size of input_sections at the time when checkpoint is
4133 input_sections_size() const
4134 { return this->input_sections_size_; }
4136 // Whether input sections are copied.
4138 input_sections_saved() const
4139 { return this->input_sections_copy_.size() == this->input_sections_size_; }
4142 first_input_offset() const
4143 { return this->first_input_offset_; }
4146 attached_input_sections_are_sorted() const
4147 { return this->attached_input_sections_are_sorted_; }
4149 // Save input sections.
4151 save_input_sections()
4153 this->input_sections_copy_.reserve(this->input_sections_size_);
4154 this->input_sections_copy_.clear();
4155 Input_section_list::const_iterator p = this->input_sections_.begin();
4156 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
4157 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
4158 this->input_sections_copy_.push_back(*p);
4162 // The section alignment.
4163 uint64_t addralign_;
4164 // The section flags.
4165 elfcpp::Elf_Xword flags_;
4166 // Reference to the input sections to be checkpointed.
4167 const Input_section_list& input_sections_;
4168 // Size of the checkpointed portion of input_sections_;
4169 size_t input_sections_size_;
4170 // Copy of input sections.
4171 Input_section_list input_sections_copy_;
4172 // The offset of the first entry in input_sections_.
4173 off_t first_input_offset_;
4174 // True if the input sections attached to this output section have
4175 // already been sorted.
4176 bool attached_input_sections_are_sorted_;
4179 // This class is used to sort the input sections.
4180 class Input_section_sort_entry;
4182 // This is the sort comparison function for ctors and dtors.
4183 struct Input_section_sort_compare
4186 operator()(const Input_section_sort_entry&,
4187 const Input_section_sort_entry&) const;
4190 // This is the sort comparison function for .init_array and .fini_array.
4191 struct Input_section_sort_init_fini_compare
4194 operator()(const Input_section_sort_entry&,
4195 const Input_section_sort_entry&) const;
4198 // This is the sort comparison function when a section order is specified
4199 // from an input file.
4200 struct Input_section_sort_section_order_index_compare
4203 operator()(const Input_section_sort_entry&,
4204 const Input_section_sort_entry&) const;
4207 // This is the sort comparison function for .text to sort sections with
4208 // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4209 struct Input_section_sort_section_prefix_special_ordering_compare
4212 operator()(const Input_section_sort_entry&,
4213 const Input_section_sort_entry&) const;
4216 // This is the sort comparison function for sorting sections by name.
4217 struct Input_section_sort_section_name_compare
4220 operator()(const Input_section_sort_entry&,
4221 const Input_section_sort_entry&) const;
4224 // Fill data. This is used to fill in data between input sections.
4225 // It is also used for data statements (BYTE, WORD, etc.) in linker
4226 // scripts. When we have to keep track of the input sections, we
4227 // can use an Output_data_const, but we don't want to have to keep
4228 // track of input sections just to implement fills.
4232 Fill(off_t section_offset, off_t length)
4233 : section_offset_(section_offset),
4234 length_(convert_to_section_size_type(length))
4237 // Return section offset.
4239 section_offset() const
4240 { return this->section_offset_; }
4242 // Return fill length.
4245 { return this->length_; }
4248 // The offset within the output section.
4249 off_t section_offset_;
4250 // The length of the space to fill.
4251 section_size_type length_;
4254 typedef std::vector<Fill> Fill_list;
4256 // Map used during relaxation of existing sections. This map
4257 // a section id an input section list index. We assume that
4258 // Input_section_list is a vector.
4259 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4261 // Add a new output section by Input_section.
4263 add_output_section_data(Input_section*);
4265 // Add an SHF_MERGE input section. Returns true if the section was
4266 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4267 // stores information about the merged input sections.
4269 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4270 uint64_t entsize, uint64_t addralign,
4271 bool keeps_input_sections);
4273 // Add an output SHF_MERGE section POSD to this output section.
4274 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4275 // ENTSIZE is the entity size. This returns the entry added to
4278 add_output_merge_section(Output_section_data* posd, bool is_string,
4281 // Find the merge section into which an input section with index SHNDX in
4282 // OBJECT has been added. Return NULL if none found.
4283 Output_section_data*
4284 find_merge_section(const Relobj* object, unsigned int shndx) const;
4286 // Build a relaxation map.
4288 build_relaxation_map(
4289 const Input_section_list& input_sections,
4291 Relaxation_map* map) const;
4293 // Convert input sections in an input section list into relaxed sections.
4295 convert_input_sections_in_list_to_relaxed_sections(
4296 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4297 const Relaxation_map& map,
4298 Input_section_list* input_sections);
4300 // Build the lookup maps for merge and relaxed input sections.
4302 build_lookup_maps() const;
4304 // Most of these fields are only valid after layout.
4306 // The name of the section. This will point into a Stringpool.
4308 // The section address is in the parent class.
4309 // The section alignment.
4310 uint64_t addralign_;
4311 // The section entry size.
4313 // The load address. This is only used when using a linker script
4314 // with a SECTIONS clause. The has_load_address_ field indicates
4315 // whether this field is valid.
4316 uint64_t load_address_;
4317 // The file offset is in the parent class.
4318 // Set the section link field to the index of this section.
4319 const Output_data* link_section_;
4320 // If link_section_ is NULL, this is the link field.
4322 // Set the section info field to the index of this section.
4323 const Output_section* info_section_;
4324 // If info_section_ is NULL, set the info field to the symbol table
4325 // index of this symbol.
4326 const Symbol* info_symndx_;
4327 // If info_section_ and info_symndx_ are NULL, this is the section
4330 // The section type.
4331 const elfcpp::Elf_Word type_;
4332 // The section flags.
4333 elfcpp::Elf_Xword flags_;
4334 // The order of this section in the output segment.
4335 Output_section_order order_;
4336 // The section index.
4337 unsigned int out_shndx_;
4338 // If there is a STT_SECTION for this output section in the normal
4339 // symbol table, this is the symbol index. This starts out as zero.
4340 // It is initialized in Layout::finalize() to be the index, or -1U
4341 // if there isn't one.
4342 unsigned int symtab_index_;
4343 // If there is a STT_SECTION for this output section in the dynamic
4344 // symbol table, this is the symbol index. This starts out as zero.
4345 // It is initialized in Layout::finalize() to be the index, or -1U
4346 // if there isn't one.
4347 unsigned int dynsym_index_;
4348 // The input sections. This will be empty in cases where we don't
4349 // need to keep track of them.
4350 Input_section_list input_sections_;
4351 // The offset of the first entry in input_sections_.
4352 off_t first_input_offset_;
4353 // The fill data. This is separate from input_sections_ because we
4354 // often will need fill sections without needing to keep track of
4357 // If the section requires postprocessing, this buffer holds the
4358 // section contents during relocation.
4359 unsigned char* postprocessing_buffer_;
4360 // Whether this output section needs a STT_SECTION symbol in the
4361 // normal symbol table. This will be true if there is a relocation
4363 bool needs_symtab_index_ : 1;
4364 // Whether this output section needs a STT_SECTION symbol in the
4365 // dynamic symbol table. This will be true if there is a dynamic
4366 // relocation which needs it.
4367 bool needs_dynsym_index_ : 1;
4368 // Whether the link field of this output section should point to the
4369 // normal symbol table.
4370 bool should_link_to_symtab_ : 1;
4371 // Whether the link field of this output section should point to the
4372 // dynamic symbol table.
4373 bool should_link_to_dynsym_ : 1;
4374 // Whether this section should be written after all the input
4375 // sections are complete.
4376 bool after_input_sections_ : 1;
4377 // Whether this section requires post processing after all
4378 // relocations have been applied.
4379 bool requires_postprocessing_ : 1;
4380 // Whether an input section was mapped to this output section
4381 // because of a SECTIONS clause in a linker script.
4382 bool found_in_sections_clause_ : 1;
4383 // Whether this section has an explicitly specified load address.
4384 bool has_load_address_ : 1;
4385 // True if the info_section_ field means the section index of the
4386 // section, false if it means the symbol index of the corresponding
4388 bool info_uses_section_index_ : 1;
4389 // True if input sections attached to this output section have to be
4390 // sorted according to a specified order.
4391 bool input_section_order_specified_ : 1;
4392 // True if the input sections attached to this output section may
4394 bool may_sort_attached_input_sections_ : 1;
4395 // True if the input sections attached to this output section must
4397 bool must_sort_attached_input_sections_ : 1;
4398 // True if the input sections attached to this output section have
4399 // already been sorted.
4400 bool attached_input_sections_are_sorted_ : 1;
4401 // True if this section holds relro data.
4403 // True if this is a small section.
4404 bool is_small_section_ : 1;
4405 // True if this is a large section.
4406 bool is_large_section_ : 1;
4407 // Whether code-fills are generated at write.
4408 bool generate_code_fills_at_write_ : 1;
4409 // Whether the entry size field should be zero.
4410 bool is_entsize_zero_ : 1;
4411 // Whether section offsets need adjustment due to relaxation.
4412 bool section_offsets_need_adjustment_ : 1;
4413 // Whether this is a NOLOAD section.
4414 bool is_noload_ : 1;
4415 // Whether this always keeps input section.
4416 bool always_keeps_input_sections_ : 1;
4417 // Whether this section has a fixed layout, for incremental update links.
4418 bool has_fixed_layout_ : 1;
4419 // True if we can add patch space to this section.
4420 bool is_patch_space_allowed_ : 1;
4421 // True if this output section goes into a unique segment.
4422 bool is_unique_segment_ : 1;
4423 // For SHT_TLS sections, the offset of this section relative to the base
4424 // of the TLS segment.
4425 uint64_t tls_offset_;
4426 // Additional segment flags, specified via linker plugin, when mapping some
4427 // input sections to unique segments.
4428 uint64_t extra_segment_flags_;
4429 // Segment alignment specified via linker plugin, when mapping some
4430 // input sections to unique segments.
4431 uint64_t segment_alignment_;
4432 // Saved checkpoint.
4433 Checkpoint_output_section* checkpoint_;
4434 // Fast lookup maps for merged and relaxed input sections.
4435 Output_section_lookup_maps* lookup_maps_;
4436 // List of available regions within the section, for incremental
4438 Free_list free_list_;
4439 // Method for filling chunks of free space.
4440 Output_fill* free_space_fill_;
4441 // Amount added as patch space for incremental linking.
4445 // An output segment. PT_LOAD segments are built from collections of
4446 // output sections. Other segments typically point within PT_LOAD
4447 // segments, and are built directly as needed.
4449 // NOTE: We want to use the copy constructor for this class. During
4450 // relaxation, we may try built the segments multiple times. We do
4451 // that by copying the original segment list before lay-out, doing
4452 // a trial lay-out and roll-back to the saved copied if we need to
4453 // to the lay-out again.
4455 class Output_segment
4458 // Create an output segment, specifying the type and flags.
4459 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4461 // Return the virtual address.
4464 { return this->vaddr_; }
4466 // Return the physical address.
4469 { return this->paddr_; }
4471 // Return the segment type.
4474 { return this->type_; }
4476 // Return the segment flags.
4479 { return this->flags_; }
4481 // Return the memory size.
4484 { return this->memsz_; }
4486 // Return the file size.
4489 { return this->filesz_; }
4491 // Return the file offset.
4494 { return this->offset_; }
4496 // Whether this is a segment created to hold large data sections.
4498 is_large_data_segment() const
4499 { return this->is_large_data_segment_; }
4501 // Record that this is a segment created to hold large data
4504 set_is_large_data_segment()
4505 { this->is_large_data_segment_ = true; }
4508 is_unique_segment() const
4509 { return this->is_unique_segment_; }
4511 // Mark segment as unique, happens when linker plugins request that
4512 // certain input sections be mapped to unique segments.
4514 set_is_unique_segment()
4515 { this->is_unique_segment_ = true; }
4517 // Return the maximum alignment of the Output_data.
4519 maximum_alignment();
4521 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4522 // the segment flags to use.
4524 add_output_section_to_load(Layout* layout, Output_section* os,
4525 elfcpp::Elf_Word seg_flags);
4527 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4528 // is the segment flags to use.
4530 add_output_section_to_nonload(Output_section* os,
4531 elfcpp::Elf_Word seg_flags);
4533 // Remove an Output_section from this segment. It is an error if it
4536 remove_output_section(Output_section* os);
4538 // Add an Output_data (which need not be an Output_section) to the
4539 // start of this segment.
4541 add_initial_output_data(Output_data*);
4543 // Return true if this segment has any sections which hold actual
4544 // data, rather than being a BSS section.
4546 has_any_data_sections() const;
4548 // Whether this segment has a dynamic relocs.
4550 has_dynamic_reloc() const;
4552 // Return the first section.
4554 first_section() const;
4556 // Return the address of the first section.
4558 first_section_load_address() const
4560 const Output_section* os = this->first_section();
4561 return os->has_load_address() ? os->load_address() : os->address();
4564 // Return whether the addresses have been set already.
4566 are_addresses_set() const
4567 { return this->are_addresses_set_; }
4569 // Set the addresses.
4571 set_addresses(uint64_t vaddr, uint64_t paddr)
4573 this->vaddr_ = vaddr;
4574 this->paddr_ = paddr;
4575 this->are_addresses_set_ = true;
4578 // Update the flags for the flags of an output section added to this
4581 update_flags_for_output_section(elfcpp::Elf_Xword flags)
4583 // The ELF ABI specifies that a PT_TLS segment should always have
4584 // PF_R as the flags.
4585 if (this->type() != elfcpp::PT_TLS)
4586 this->flags_ |= flags;
4589 // Set the segment flags. This is only used if we have a PHDRS
4590 // clause which explicitly specifies the flags.
4592 set_flags(elfcpp::Elf_Word flags)
4593 { this->flags_ = flags; }
4595 // Set the address of the segment to ADDR and the offset to *POFF
4596 // and set the addresses and offsets of all contained output
4597 // sections accordingly. Set the section indexes of all contained
4598 // output sections starting with *PSHNDX. If RESET is true, first
4599 // reset the addresses of the contained sections. Return the
4600 // address of the immediately following segment. Update *POFF and
4601 // *PSHNDX. This should only be called for a PT_LOAD segment.
4603 set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
4604 unsigned int* increase_relro, bool* has_relro,
4605 off_t* poff, unsigned int* pshndx);
4607 // Set the minimum alignment of this segment. This may be adjusted
4608 // upward based on the section alignments.
4610 set_minimum_p_align(uint64_t align)
4612 if (align > this->min_p_align_)
4613 this->min_p_align_ = align;
4616 // Set the offset of this segment based on the section. This should
4617 // only be called for a non-PT_LOAD segment.
4619 set_offset(unsigned int increase);
4621 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4625 // Return the number of output sections.
4627 output_section_count() const;
4629 // Return the section attached to the list segment with the lowest
4630 // load address. This is used when handling a PHDRS clause in a
4633 section_with_lowest_load_address() const;
4635 // Write the segment header into *OPHDR.
4636 template<int size, bool big_endian>
4638 write_header(elfcpp::Phdr_write<size, big_endian>*);
4640 // Write the section headers of associated sections into V.
4641 template<int size, bool big_endian>
4643 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4644 unsigned int* pshndx) const;
4646 // Print the output sections in the map file.
4648 print_sections_to_mapfile(Mapfile*) const;
4651 typedef std::vector<Output_data*> Output_data_list;
4653 // Find the maximum alignment in an Output_data_list.
4655 maximum_alignment_list(const Output_data_list*);
4657 // Return whether the first data section is a relro section.
4659 is_first_section_relro() const;
4661 // Set the section addresses in an Output_data_list.
4663 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4664 uint64_t addr, off_t* poff, unsigned int* pshndx,
4667 // Return the number of Output_sections in an Output_data_list.
4669 output_section_count_list(const Output_data_list*) const;
4671 // Return whether an Output_data_list has a dynamic reloc.
4673 has_dynamic_reloc_list(const Output_data_list*) const;
4675 // Find the section with the lowest load address in an
4676 // Output_data_list.
4678 lowest_load_address_in_list(const Output_data_list* pdl,
4679 Output_section** found,
4680 uint64_t* found_lma) const;
4682 // Find the first and last entries by address.
4684 find_first_and_last_list(const Output_data_list* pdl,
4685 const Output_data** pfirst,
4686 const Output_data** plast) const;
4688 // Write the section headers in the list into V.
4689 template<int size, bool big_endian>
4691 write_section_headers_list(const Layout*, const Stringpool*,
4692 const Output_data_list*, unsigned char* v,
4693 unsigned int* pshdx) const;
4695 // Print a section list to the mapfile.
4697 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4699 // NOTE: We want to use the copy constructor. Currently, shallow copy
4700 // works for us so we do not need to write our own copy constructor.
4702 // The list of output data attached to this segment.
4703 Output_data_list output_lists_[ORDER_MAX];
4704 // The segment virtual address.
4706 // The segment physical address.
4708 // The size of the segment in memory.
4710 // The maximum section alignment. The is_max_align_known_ field
4711 // indicates whether this has been finalized.
4712 uint64_t max_align_;
4713 // The required minimum value for the p_align field. This is used
4714 // for PT_LOAD segments. Note that this does not mean that
4715 // addresses should be aligned to this value; it means the p_paddr
4716 // and p_vaddr fields must be congruent modulo this value. For
4717 // non-PT_LOAD segments, the dynamic linker works more efficiently
4718 // if the p_align field has the more conventional value, although it
4719 // can align as needed.
4720 uint64_t min_p_align_;
4721 // The offset of the segment data within the file.
4723 // The size of the segment data in the file.
4725 // The segment type;
4726 elfcpp::Elf_Word type_;
4727 // The segment flags.
4728 elfcpp::Elf_Word flags_;
4729 // Whether we have finalized max_align_.
4730 bool is_max_align_known_ : 1;
4731 // Whether vaddr and paddr were set by a linker script.
4732 bool are_addresses_set_ : 1;
4733 // Whether this segment holds large data sections.
4734 bool is_large_data_segment_ : 1;
4735 // Whether this was marked as a unique segment via a linker plugin.
4736 bool is_unique_segment_ : 1;
4739 // This class represents the output file.
4744 Output_file(const char* name);
4746 // Indicate that this is a temporary file which should not be
4750 { this->is_temporary_ = true; }
4752 // Try to open an existing file. Returns false if the file doesn't
4753 // exist, has a size of 0 or can't be mmaped. This method is
4754 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4755 // that file as the base for incremental linking.
4757 open_base_file(const char* base_name, bool writable);
4759 // Open the output file. FILE_SIZE is the final size of the file.
4760 // If the file already exists, it is deleted/truncated. This method
4761 // is thread-unsafe.
4763 open(off_t file_size);
4765 // Resize the output file. This method is thread-unsafe.
4767 resize(off_t file_size);
4769 // Close the output file (flushing all buffered data) and make sure
4770 // there are no errors. This method is thread-unsafe.
4774 // Return the size of this file.
4777 { return this->file_size_; }
4779 // Return the name of this file.
4782 { return this->name_; }
4784 // We currently always use mmap which makes the view handling quite
4785 // simple. In the future we may support other approaches.
4787 // Write data to the output file.
4789 write(off_t offset, const void* data, size_t len)
4790 { memcpy(this->base_ + offset, data, len); }
4792 // Get a buffer to use to write to the file, given the offset into
4793 // the file and the size.
4795 get_output_view(off_t start, size_t size)
4797 gold_assert(start >= 0
4798 && start + static_cast<off_t>(size) <= this->file_size_);
4799 return this->base_ + start;
4802 // VIEW must have been returned by get_output_view. Write the
4803 // buffer to the file, passing in the offset and the size.
4805 write_output_view(off_t, size_t, unsigned char*)
4808 // Get a read/write buffer. This is used when we want to write part
4809 // of the file, read it in, and write it again.
4811 get_input_output_view(off_t start, size_t size)
4812 { return this->get_output_view(start, size); }
4814 // Write a read/write buffer back to the file.
4816 write_input_output_view(off_t, size_t, unsigned char*)
4819 // Get a read buffer. This is used when we just want to read part
4820 // of the file back it in.
4821 const unsigned char*
4822 get_input_view(off_t start, size_t size)
4823 { return this->get_output_view(start, size); }
4825 // Release a read bfufer.
4827 free_input_view(off_t, size_t, const unsigned char*)
4831 // Map the file into memory or, if that fails, allocate anonymous
4836 // Allocate anonymous memory for the file.
4840 // Map the file into memory.
4842 map_no_anonymous(bool);
4844 // Unmap the file from memory (and flush to disk buffers).
4854 // Base of file mapped into memory.
4855 unsigned char* base_;
4856 // True iff base_ points to a memory buffer rather than an output file.
4857 bool map_is_anonymous_;
4858 // True if base_ was allocated using new rather than mmap.
4859 bool map_is_allocated_;
4860 // True if this is a temporary file which should not be output.
4864 } // End namespace gold.
4866 #endif // !defined(GOLD_OUTPUT_H)