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(const 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.
620 const Target* target_;
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)
2384 unsigned int got_offset = this->add_got_entry(Got_entry(constant));
2388 // Replace GOT entry I with a new constant.
2390 replace_constant(unsigned int i, Valtype constant)
2392 this->replace_got_entry(i, Got_entry(constant));
2395 // Reserve a slot in the GOT for a local symbol.
2397 reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2398 unsigned int got_type);
2400 // Reserve a slot in the GOT for a global symbol.
2402 reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2405 // Write out the GOT table.
2407 do_write(Output_file*);
2409 // Write to a map file.
2411 do_print_to_mapfile(Mapfile* mapfile) const
2412 { mapfile->print_output_data(this, _("** GOT")); }
2414 // Reserve the slot at index I in the GOT.
2416 do_reserve_slot(unsigned int i)
2417 { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2419 // Return the number of words in the GOT.
2421 num_entries () const
2422 { return this->entries_.size(); }
2424 // Return the offset into the GOT of GOT entry I.
2426 got_offset(unsigned int i) const
2427 { return i * (got_size / 8); }
2430 // This POD class holds a single GOT entry.
2434 // Create a zero entry.
2436 : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false)
2437 { this->u_.constant = 0; }
2439 // Create a global symbol entry.
2440 Got_entry(Symbol* gsym, bool use_plt_or_tls_offset)
2441 : local_sym_index_(GSYM_CODE),
2442 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2443 { this->u_.gsym = gsym; }
2445 // Create a local symbol entry.
2446 Got_entry(Relobj* object, unsigned int local_sym_index,
2447 bool use_plt_or_tls_offset)
2448 : local_sym_index_(local_sym_index),
2449 use_plt_or_tls_offset_(use_plt_or_tls_offset)
2451 gold_assert(local_sym_index != GSYM_CODE
2452 && local_sym_index != CONSTANT_CODE
2453 && local_sym_index != RESERVED_CODE
2454 && local_sym_index == this->local_sym_index_);
2455 this->u_.object = object;
2458 // Create a constant entry. The constant is a host value--it will
2459 // be swapped, if necessary, when it is written out.
2460 explicit Got_entry(Valtype constant)
2461 : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
2462 { this->u_.constant = constant; }
2464 // Write the GOT entry to an output view.
2466 write(unsigned int got_indx, unsigned char* pov) const;
2471 GSYM_CODE = 0x7fffffff,
2472 CONSTANT_CODE = 0x7ffffffe,
2473 RESERVED_CODE = 0x7ffffffd
2478 // For a local symbol, the object.
2480 // For a global symbol, the symbol.
2482 // For a constant, the constant.
2485 // For a local symbol, the local symbol index. This is GSYM_CODE
2486 // for a global symbol, or CONSTANT_CODE for a constant.
2487 unsigned int local_sym_index_ : 31;
2488 // Whether to use the PLT offset of the symbol if it has one.
2489 // For TLS symbols, whether to offset the symbol value.
2490 bool use_plt_or_tls_offset_ : 1;
2493 typedef std::vector<Got_entry> Got_entries;
2495 // Create a new GOT entry and return its offset.
2497 add_got_entry(Got_entry got_entry);
2499 // Create a pair of new GOT entries and return the offset of the first.
2501 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2503 // Replace GOT entry I with a new value.
2505 replace_got_entry(unsigned int i, Got_entry got_entry);
2507 // Return the offset into the GOT of the last entry added.
2509 last_got_offset() const
2510 { return this->got_offset(this->num_entries() - 1); }
2512 // Set the size of the section.
2515 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2517 // The list of GOT entries.
2518 Got_entries entries_;
2520 // List of available regions within the section, for incremental
2522 Free_list free_list_;
2525 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2528 class Output_data_dynamic : public Output_section_data
2531 Output_data_dynamic(Stringpool* pool)
2532 : Output_section_data(Output_data::default_alignment()),
2533 entries_(), pool_(pool)
2536 // Add a new dynamic entry with a fixed numeric value.
2538 add_constant(elfcpp::DT tag, unsigned int val)
2539 { this->add_entry(Dynamic_entry(tag, val)); }
2541 // Add a new dynamic entry with the address of output data.
2543 add_section_address(elfcpp::DT tag, const Output_data* od)
2544 { this->add_entry(Dynamic_entry(tag, od, false)); }
2546 // Add a new dynamic entry with the address of output data
2547 // plus a constant offset.
2549 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2550 unsigned int offset)
2551 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2553 // Add a new dynamic entry with the size of output data.
2555 add_section_size(elfcpp::DT tag, const Output_data* od)
2556 { this->add_entry(Dynamic_entry(tag, od, true)); }
2558 // Add a new dynamic entry with the total size of two output datas.
2560 add_section_size(elfcpp::DT tag, const Output_data* od,
2561 const Output_data* od2)
2562 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2564 // Add a new dynamic entry with the address of a symbol.
2566 add_symbol(elfcpp::DT tag, const Symbol* sym)
2567 { this->add_entry(Dynamic_entry(tag, sym)); }
2569 // Add a new dynamic entry with a string.
2571 add_string(elfcpp::DT tag, const char* str)
2572 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2575 add_string(elfcpp::DT tag, const std::string& str)
2576 { this->add_string(tag, str.c_str()); }
2579 // Adjust the output section to set the entry size.
2581 do_adjust_output_section(Output_section*);
2583 // Set the final data size.
2585 set_final_data_size();
2587 // Write out the dynamic entries.
2589 do_write(Output_file*);
2591 // Write to a map file.
2593 do_print_to_mapfile(Mapfile* mapfile) const
2594 { mapfile->print_output_data(this, _("** dynamic")); }
2597 // This POD class holds a single dynamic entry.
2601 // Create an entry with a fixed numeric value.
2602 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2603 : tag_(tag), offset_(DYNAMIC_NUMBER)
2604 { this->u_.val = val; }
2606 // Create an entry with the size or address of a section.
2607 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2609 offset_(section_size
2610 ? DYNAMIC_SECTION_SIZE
2611 : DYNAMIC_SECTION_ADDRESS)
2617 // Create an entry with the size of two sections.
2618 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2620 offset_(DYNAMIC_SECTION_SIZE)
2626 // Create an entry with the address of a section plus a constant offset.
2627 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2630 { this->u_.od = od; }
2632 // Create an entry with the address of a symbol.
2633 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2634 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2635 { this->u_.sym = sym; }
2637 // Create an entry with a string.
2638 Dynamic_entry(elfcpp::DT tag, const char* str)
2639 : tag_(tag), offset_(DYNAMIC_STRING)
2640 { this->u_.str = str; }
2642 // Return the tag of this entry.
2645 { return this->tag_; }
2647 // Write the dynamic entry to an output view.
2648 template<int size, bool big_endian>
2650 write(unsigned char* pov, const Stringpool*) const;
2653 // Classification is encoded in the OFFSET field.
2657 DYNAMIC_SECTION_ADDRESS = 0,
2659 DYNAMIC_NUMBER = -1U,
2661 DYNAMIC_SECTION_SIZE = -2U,
2663 DYNAMIC_SYMBOL = -3U,
2665 DYNAMIC_STRING = -4U
2666 // Any other value indicates a section address plus OFFSET.
2671 // For DYNAMIC_NUMBER.
2673 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2674 const Output_data* od;
2675 // For DYNAMIC_SYMBOL.
2677 // For DYNAMIC_STRING.
2680 // For DYNAMIC_SYMBOL with two sections.
2681 const Output_data* od2;
2684 // The type of entry (Classification) or offset within a section.
2685 unsigned int offset_;
2688 // Add an entry to the list.
2690 add_entry(const Dynamic_entry& entry)
2691 { this->entries_.push_back(entry); }
2693 // Sized version of write function.
2694 template<int size, bool big_endian>
2696 sized_write(Output_file* of);
2698 // The type of the list of entries.
2699 typedef std::vector<Dynamic_entry> Dynamic_entries;
2702 Dynamic_entries entries_;
2703 // The pool used for strings.
2707 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2708 // which may be required if the object file has more than
2709 // SHN_LORESERVE sections.
2711 class Output_symtab_xindex : public Output_section_data
2714 Output_symtab_xindex(size_t symcount)
2715 : Output_section_data(symcount * 4, 4, true),
2719 // Add an entry: symbol number SYMNDX has section SHNDX.
2721 add(unsigned int symndx, unsigned int shndx)
2722 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2726 do_write(Output_file*);
2728 // Write to a map file.
2730 do_print_to_mapfile(Mapfile* mapfile) const
2731 { mapfile->print_output_data(this, _("** symtab xindex")); }
2734 template<bool big_endian>
2736 endian_do_write(unsigned char*);
2738 // It is likely that most symbols will not require entries. Rather
2739 // than keep a vector for all symbols, we keep pairs of symbol index
2740 // and section index.
2741 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2743 // The entries we need.
2744 Xindex_entries entries_;
2747 // A relaxed input section.
2748 class Output_relaxed_input_section : public Output_section_data_build
2751 // We would like to call relobj->section_addralign(shndx) to get the
2752 // alignment but we do not want the constructor to fail. So callers
2753 // are repsonsible for ensuring that.
2754 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2756 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2759 // Return the Relobj of this relaxed input section.
2762 { return this->relobj_; }
2764 // Return the section index of this relaxed input section.
2767 { return this->shndx_; }
2771 set_relobj(Relobj* relobj)
2772 { this->relobj_ = relobj; }
2775 set_shndx(unsigned int shndx)
2776 { this->shndx_ = shndx; }
2780 unsigned int shndx_;
2783 // This class describes properties of merge data sections. It is used
2784 // as a key type for maps.
2785 class Merge_section_properties
2788 Merge_section_properties(bool is_string, uint64_t entsize,
2790 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2793 // Whether this equals to another Merge_section_properties MSP.
2795 eq(const Merge_section_properties& msp) const
2797 return ((this->is_string_ == msp.is_string_)
2798 && (this->entsize_ == msp.entsize_)
2799 && (this->addralign_ == msp.addralign_));
2802 // Compute a hash value for this using 64-bit FNV-1a hash.
2806 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2807 uint64_t prime = 1099511628211ULL;
2808 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2809 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2810 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2814 // Functors for associative containers.
2818 operator()(const Merge_section_properties& msp1,
2819 const Merge_section_properties& msp2) const
2820 { return msp1.eq(msp2); }
2826 operator()(const Merge_section_properties& msp) const
2827 { return msp.hash_value(); }
2831 // Whether this merge data section is for strings.
2833 // Entsize of this merge data section.
2835 // Address alignment.
2836 uint64_t addralign_;
2839 // This class is used to speed up look up of special input sections in an
2842 class Output_section_lookup_maps
2845 Output_section_lookup_maps()
2846 : is_valid_(true), merge_sections_by_properties_(),
2847 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2850 // Whether the maps are valid.
2853 { return this->is_valid_; }
2855 // Invalidate the maps.
2858 { this->is_valid_ = false; }
2864 this->merge_sections_by_properties_.clear();
2865 this->merge_sections_by_id_.clear();
2866 this->relaxed_input_sections_by_id_.clear();
2867 // A cleared map is valid.
2868 this->is_valid_ = true;
2871 // Find a merge section by merge section properties. Return NULL if none
2874 find_merge_section(const Merge_section_properties& msp) const
2876 gold_assert(this->is_valid_);
2877 Merge_sections_by_properties::const_iterator p =
2878 this->merge_sections_by_properties_.find(msp);
2879 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2882 // Find a merge section by section ID of a merge input section. Return NULL
2883 // if none is found.
2885 find_merge_section(const Object* object, unsigned int shndx) const
2887 gold_assert(this->is_valid_);
2888 Merge_sections_by_id::const_iterator p =
2889 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2890 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2893 // Add a merge section pointed by POMB with properties MSP.
2895 add_merge_section(const Merge_section_properties& msp,
2896 Output_merge_base* pomb)
2898 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2899 std::pair<Merge_sections_by_properties::iterator, bool> result =
2900 this->merge_sections_by_properties_.insert(value);
2901 gold_assert(result.second);
2904 // Add a mapping from a merged input section in OBJECT with index SHNDX
2905 // to a merge output section pointed by POMB.
2907 add_merge_input_section(const Object* object, unsigned int shndx,
2908 Output_merge_base* pomb)
2910 Const_section_id csid(object, shndx);
2911 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2912 std::pair<Merge_sections_by_id::iterator, bool> result =
2913 this->merge_sections_by_id_.insert(value);
2914 gold_assert(result.second);
2917 // Find a relaxed input section of OBJECT with index SHNDX.
2918 Output_relaxed_input_section*
2919 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2921 gold_assert(this->is_valid_);
2922 Relaxed_input_sections_by_id::const_iterator p =
2923 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2924 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2927 // Add a relaxed input section pointed by POMB and whose original input
2928 // section is in OBJECT with index SHNDX.
2930 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2931 Output_relaxed_input_section* poris)
2933 Const_section_id csid(relobj, shndx);
2934 std::pair<Const_section_id, Output_relaxed_input_section*>
2936 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2937 this->relaxed_input_sections_by_id_.insert(value);
2938 gold_assert(result.second);
2942 typedef Unordered_map<Const_section_id, Output_merge_base*,
2943 Const_section_id_hash>
2944 Merge_sections_by_id;
2946 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2947 Merge_section_properties::hash,
2948 Merge_section_properties::equal_to>
2949 Merge_sections_by_properties;
2951 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2952 Const_section_id_hash>
2953 Relaxed_input_sections_by_id;
2955 // Whether this is valid
2957 // Merge sections by merge section properties.
2958 Merge_sections_by_properties merge_sections_by_properties_;
2959 // Merge sections by section IDs.
2960 Merge_sections_by_id merge_sections_by_id_;
2961 // Relaxed sections by section IDs.
2962 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2965 // This abstract base class defines the interface for the
2966 // types of methods used to fill free space left in an output
2967 // section during an incremental link. These methods are used
2968 // to insert dummy compilation units into debug info so that
2969 // debug info consumers can scan the debug info serially.
2975 : is_big_endian_(parameters->target().is_big_endian())
2982 // Return the smallest size chunk of free space that can be
2983 // filled with a dummy compilation unit.
2985 minimum_hole_size() const
2986 { return this->do_minimum_hole_size(); }
2988 // Write a fill pattern of length LEN at offset OFF in the file.
2990 write(Output_file* of, off_t off, size_t len) const
2991 { this->do_write(of, off, len); }
2995 do_minimum_hole_size() const = 0;
2998 do_write(Output_file* of, off_t off, size_t len) const = 0;
3001 is_big_endian() const
3002 { return this->is_big_endian_; }
3005 bool is_big_endian_;
3008 // Fill method that introduces a dummy compilation unit in
3009 // a .debug_info or .debug_types section.
3011 class Output_fill_debug_info : public Output_fill
3014 Output_fill_debug_info(bool is_debug_types)
3015 : is_debug_types_(is_debug_types)
3020 do_minimum_hole_size() const;
3023 do_write(Output_file* of, off_t off, size_t len) const;
3026 // Version of the header.
3027 static const int version = 4;
3028 // True if this is a .debug_types section.
3029 bool is_debug_types_;
3032 // Fill method that introduces a dummy compilation unit in
3033 // a .debug_line section.
3035 class Output_fill_debug_line : public Output_fill
3038 Output_fill_debug_line()
3043 do_minimum_hole_size() const;
3046 do_write(Output_file* of, off_t off, size_t len) const;
3049 // Version of the header. We write a DWARF-3 header because it's smaller
3050 // and many tools have not yet been updated to understand the DWARF-4 header.
3051 static const int version = 3;
3052 // Length of the portion of the header that follows the header_length
3053 // field. This includes the following fields:
3054 // minimum_instruction_length, default_is_stmt, line_base, line_range,
3055 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3056 // The standard_opcode_lengths array is 12 bytes long, and the
3057 // include_directories and filenames fields each contain only a single
3059 static const size_t header_length = 19;
3062 // An output section. We don't expect to have too many output
3063 // sections, so we don't bother to do a template on the size.
3065 class Output_section : public Output_data
3068 // Create an output section, giving the name, type, and flags.
3069 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
3070 virtual ~Output_section();
3072 // Add a new input section SHNDX, named NAME, with header SHDR, from
3073 // object OBJECT. RELOC_SHNDX is the index of a relocation section
3074 // which applies to this section, or 0 if none, or -1 if more than
3075 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3076 // in a linker script; in that case we need to keep track of input
3077 // sections associated with an output section. Return the offset
3078 // within the output section.
3079 template<int size, bool big_endian>
3081 add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
3082 unsigned int shndx, const char* name,
3083 const elfcpp::Shdr<size, big_endian>& shdr,
3084 unsigned int reloc_shndx, bool have_sections_script);
3086 // Add generated data POSD to this output section.
3088 add_output_section_data(Output_section_data* posd);
3090 // Add a relaxed input section PORIS called NAME to this output section
3093 add_relaxed_input_section(Layout* layout,
3094 Output_relaxed_input_section* poris,
3095 const std::string& name);
3097 // Return the section name.
3100 { return this->name_; }
3102 // Return the section type.
3105 { return this->type_; }
3107 // Return the section flags.
3110 { return this->flags_; }
3112 typedef std::map<Section_id, unsigned int> Section_layout_order;
3115 update_section_layout(const Section_layout_order* order_map);
3117 // Update the output section flags based on input section flags.
3119 update_flags_for_input_section(elfcpp::Elf_Xword flags);
3121 // Return the entsize field.
3124 { return this->entsize_; }
3126 // Set the entsize field.
3128 set_entsize(uint64_t v);
3130 // Set the load address.
3132 set_load_address(uint64_t load_address)
3134 this->load_address_ = load_address;
3135 this->has_load_address_ = true;
3138 // Set the link field to the output section index of a section.
3140 set_link_section(const Output_data* od)
3142 gold_assert(this->link_ == 0
3143 && !this->should_link_to_symtab_
3144 && !this->should_link_to_dynsym_);
3145 this->link_section_ = od;
3148 // Set the link field to a constant.
3150 set_link(unsigned int v)
3152 gold_assert(this->link_section_ == NULL
3153 && !this->should_link_to_symtab_
3154 && !this->should_link_to_dynsym_);
3158 // Record that this section should link to the normal symbol table.
3160 set_should_link_to_symtab()
3162 gold_assert(this->link_section_ == NULL
3164 && !this->should_link_to_dynsym_);
3165 this->should_link_to_symtab_ = true;
3168 // Record that this section should link to the dynamic symbol table.
3170 set_should_link_to_dynsym()
3172 gold_assert(this->link_section_ == NULL
3174 && !this->should_link_to_symtab_);
3175 this->should_link_to_dynsym_ = true;
3178 // Return the info field.
3182 gold_assert(this->info_section_ == NULL
3183 && this->info_symndx_ == NULL);
3187 // Set the info field to the output section index of a section.
3189 set_info_section(const Output_section* os)
3191 gold_assert((this->info_section_ == NULL
3192 || (this->info_section_ == os
3193 && this->info_uses_section_index_))
3194 && this->info_symndx_ == NULL
3195 && this->info_ == 0);
3196 this->info_section_ = os;
3197 this->info_uses_section_index_= true;
3200 // Set the info field to the symbol table index of a symbol.
3202 set_info_symndx(const Symbol* sym)
3204 gold_assert(this->info_section_ == NULL
3205 && (this->info_symndx_ == NULL
3206 || this->info_symndx_ == sym)
3207 && this->info_ == 0);
3208 this->info_symndx_ = sym;
3211 // Set the info field to the symbol table index of a section symbol.
3213 set_info_section_symndx(const Output_section* os)
3215 gold_assert((this->info_section_ == NULL
3216 || (this->info_section_ == os
3217 && !this->info_uses_section_index_))
3218 && this->info_symndx_ == NULL
3219 && this->info_ == 0);
3220 this->info_section_ = os;
3221 this->info_uses_section_index_ = false;
3224 // Set the info field to a constant.
3226 set_info(unsigned int v)
3228 gold_assert(this->info_section_ == NULL
3229 && this->info_symndx_ == NULL
3230 && (this->info_ == 0
3231 || this->info_ == v));
3235 // Set the addralign field.
3237 set_addralign(uint64_t v)
3238 { this->addralign_ = v; }
3241 checkpoint_set_addralign(uint64_t val)
3243 if (this->checkpoint_ != NULL)
3244 this->checkpoint_->set_addralign(val);
3247 // Whether the output section index has been set.
3249 has_out_shndx() const
3250 { return this->out_shndx_ != -1U; }
3252 // Indicate that we need a symtab index.
3254 set_needs_symtab_index()
3255 { this->needs_symtab_index_ = true; }
3257 // Return whether we need a symtab index.
3259 needs_symtab_index() const
3260 { return this->needs_symtab_index_; }
3262 // Get the symtab index.
3264 symtab_index() const
3266 gold_assert(this->symtab_index_ != 0);
3267 return this->symtab_index_;
3270 // Set the symtab index.
3272 set_symtab_index(unsigned int index)
3274 gold_assert(index != 0);
3275 this->symtab_index_ = index;
3278 // Indicate that we need a dynsym index.
3280 set_needs_dynsym_index()
3281 { this->needs_dynsym_index_ = true; }
3283 // Return whether we need a dynsym index.
3285 needs_dynsym_index() const
3286 { return this->needs_dynsym_index_; }
3288 // Get the dynsym index.
3290 dynsym_index() const
3292 gold_assert(this->dynsym_index_ != 0);
3293 return this->dynsym_index_;
3296 // Set the dynsym index.
3298 set_dynsym_index(unsigned int index)
3300 gold_assert(index != 0);
3301 this->dynsym_index_ = index;
3304 // Sort the attached input sections.
3306 sort_attached_input_sections();
3308 // Return whether the input sections sections attachd to this output
3309 // section may require sorting. This is used to handle constructor
3310 // priorities compatibly with GNU ld.
3312 may_sort_attached_input_sections() const
3313 { return this->may_sort_attached_input_sections_; }
3315 // Record that the input sections attached to this output section
3316 // may require sorting.
3318 set_may_sort_attached_input_sections()
3319 { this->may_sort_attached_input_sections_ = true; }
3321 // Returns true if input sections must be sorted according to the
3322 // order in which their name appear in the --section-ordering-file.
3324 input_section_order_specified()
3325 { return this->input_section_order_specified_; }
3327 // Record that input sections must be sorted as some of their names
3328 // match the patterns specified through --section-ordering-file.
3330 set_input_section_order_specified()
3331 { this->input_section_order_specified_ = true; }
3333 // Return whether the input sections attached to this output section
3334 // require sorting. This is used to handle constructor priorities
3335 // compatibly with GNU ld.
3337 must_sort_attached_input_sections() const
3338 { return this->must_sort_attached_input_sections_; }
3340 // Record that the input sections attached to this output section
3343 set_must_sort_attached_input_sections()
3344 { this->must_sort_attached_input_sections_ = true; }
3346 // Get the order in which this section appears in the PT_LOAD output
3348 Output_section_order
3350 { return this->order_; }
3352 // Set the order for this section.
3354 set_order(Output_section_order order)
3355 { this->order_ = order; }
3357 // Return whether this section holds relro data--data which has
3358 // dynamic relocations but which may be marked read-only after the
3359 // dynamic relocations have been completed.
3362 { return this->is_relro_; }
3364 // Record that this section holds relro data.
3367 { this->is_relro_ = true; }
3369 // Record that this section does not hold relro data.
3372 { this->is_relro_ = false; }
3374 // True if this is a small section: a section which holds small
3377 is_small_section() const
3378 { return this->is_small_section_; }
3380 // Record that this is a small section.
3382 set_is_small_section()
3383 { this->is_small_section_ = true; }
3385 // True if this is a large section: a section which holds large
3388 is_large_section() const
3389 { return this->is_large_section_; }
3391 // Record that this is a large section.
3393 set_is_large_section()
3394 { this->is_large_section_ = true; }
3396 // True if this is a large data (not BSS) section.
3398 is_large_data_section()
3399 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3401 // Return whether this section should be written after all the input
3402 // sections are complete.
3404 after_input_sections() const
3405 { return this->after_input_sections_; }
3407 // Record that this section should be written after all the input
3408 // sections are complete.
3410 set_after_input_sections()
3411 { this->after_input_sections_ = true; }
3413 // Return whether this section requires postprocessing after all
3414 // relocations have been applied.
3416 requires_postprocessing() const
3417 { return this->requires_postprocessing_; }
3420 is_unique_segment() const
3421 { return this->is_unique_segment_; }
3424 set_is_unique_segment()
3425 { this->is_unique_segment_ = true; }
3427 uint64_t extra_segment_flags() const
3428 { return this->extra_segment_flags_; }
3431 set_extra_segment_flags(uint64_t flags)
3432 { this->extra_segment_flags_ = flags; }
3434 uint64_t segment_alignment() const
3435 { return this->segment_alignment_; }
3438 set_segment_alignment(uint64_t align)
3439 { this->segment_alignment_ = align; }
3441 // If a section requires postprocessing, return the buffer to use.
3443 postprocessing_buffer() const
3445 gold_assert(this->postprocessing_buffer_ != NULL);
3446 return this->postprocessing_buffer_;
3449 // If a section requires postprocessing, create the buffer to use.
3451 create_postprocessing_buffer();
3453 // If a section requires postprocessing, this is the size of the
3454 // buffer to which relocations should be applied.
3456 postprocessing_buffer_size() const
3457 { return this->current_data_size_for_child(); }
3459 // Modify the section name. This is only permitted for an
3460 // unallocated section, and only before the size has been finalized.
3461 // Otherwise the name will not get into Layout::namepool_.
3463 set_name(const char* newname)
3465 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3466 gold_assert(!this->is_data_size_valid());
3467 this->name_ = newname;
3470 // Return whether the offset OFFSET in the input section SHNDX in
3471 // object OBJECT is being included in the link.
3473 is_input_address_mapped(const Relobj* object, unsigned int shndx,
3474 off_t offset) const;
3476 // Return the offset within the output section of OFFSET relative to
3477 // the start of input section SHNDX in object OBJECT.
3479 output_offset(const Relobj* object, unsigned int shndx,
3480 section_offset_type offset) const;
3482 // Return the output virtual address of OFFSET relative to the start
3483 // of input section SHNDX in object OBJECT.
3485 output_address(const Relobj* object, unsigned int shndx,
3486 off_t offset) const;
3488 // Look for the merged section for input section SHNDX in object
3489 // OBJECT. If found, return true, and set *ADDR to the address of
3490 // the start of the merged section. This is not necessary the
3491 // output offset corresponding to input offset 0 in the section,
3492 // since the section may be mapped arbitrarily.
3494 find_starting_output_address(const Relobj* object, unsigned int shndx,
3495 uint64_t* addr) const;
3497 // Record that this output section was found in the SECTIONS clause
3498 // of a linker script.
3500 set_found_in_sections_clause()
3501 { this->found_in_sections_clause_ = true; }
3503 // Return whether this output section was found in the SECTIONS
3504 // clause of a linker script.
3506 found_in_sections_clause() const
3507 { return this->found_in_sections_clause_; }
3509 // Write the section header into *OPHDR.
3510 template<int size, bool big_endian>
3512 write_header(const Layout*, const Stringpool*,
3513 elfcpp::Shdr_write<size, big_endian>*) const;
3515 // The next few calls are for linker script support.
3517 // In some cases we need to keep a list of the input sections
3518 // associated with this output section. We only need the list if we
3519 // might have to change the offsets of the input section within the
3520 // output section after we add the input section. The ordinary
3521 // input sections will be written out when we process the object
3522 // file, and as such we don't need to track them here. We do need
3523 // to track Output_section_data objects here. We store instances of
3524 // this structure in a std::vector, so it must be a POD. There can
3525 // be many instances of this structure, so we use a union to save
3531 : shndx_(0), p2align_(0)
3533 this->u1_.data_size = 0;
3534 this->u2_.object = NULL;
3537 // For an ordinary input section.
3538 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3541 p2align_(ffsll(static_cast<long long>(addralign))),
3542 section_order_index_(0)
3544 gold_assert(shndx != OUTPUT_SECTION_CODE
3545 && shndx != MERGE_DATA_SECTION_CODE
3546 && shndx != MERGE_STRING_SECTION_CODE
3547 && shndx != RELAXED_INPUT_SECTION_CODE);
3548 this->u1_.data_size = data_size;
3549 this->u2_.object = object;
3552 // For a non-merge output section.
3553 Input_section(Output_section_data* posd)
3554 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3555 section_order_index_(0)
3557 this->u1_.data_size = 0;
3558 this->u2_.posd = posd;
3561 // For a merge section.
3562 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3564 ? MERGE_STRING_SECTION_CODE
3565 : MERGE_DATA_SECTION_CODE),
3567 section_order_index_(0)
3569 this->u1_.entsize = entsize;
3570 this->u2_.posd = posd;
3573 // For a relaxed input section.
3574 Input_section(Output_relaxed_input_section* psection)
3575 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3576 section_order_index_(0)
3578 this->u1_.data_size = 0;
3579 this->u2_.poris = psection;
3583 section_order_index() const
3585 return this->section_order_index_;
3589 set_section_order_index(unsigned int number)
3591 this->section_order_index_ = number;
3594 // The required alignment.
3598 if (this->p2align_ != 0)
3599 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3600 else if (!this->is_input_section())
3601 return this->u2_.posd->addralign();
3606 // Set the required alignment, which must be either 0 or a power of 2.
3607 // For input sections that are sub-classes of Output_section_data, a
3608 // alignment of zero means asking the underlying object for alignment.
3610 set_addralign(uint64_t addralign)
3616 gold_assert((addralign & (addralign - 1)) == 0);
3617 this->p2align_ = ffsll(static_cast<long long>(addralign));
3621 // Return the current required size, without finalization.
3623 current_data_size() const;
3625 // Return the required size.
3629 // Whether this is an input section.
3631 is_input_section() const
3633 return (this->shndx_ != OUTPUT_SECTION_CODE
3634 && this->shndx_ != MERGE_DATA_SECTION_CODE
3635 && this->shndx_ != MERGE_STRING_SECTION_CODE
3636 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3639 // Return whether this is a merge section which matches the
3642 is_merge_section(bool is_string, uint64_t entsize,
3643 uint64_t addralign) const
3645 return (this->shndx_ == (is_string
3646 ? MERGE_STRING_SECTION_CODE
3647 : MERGE_DATA_SECTION_CODE)
3648 && this->u1_.entsize == entsize
3649 && this->addralign() == addralign);
3652 // Return whether this is a merge section for some input section.
3654 is_merge_section() const
3656 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3657 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3660 // Return whether this is a relaxed input section.
3662 is_relaxed_input_section() const
3663 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3665 // Return whether this is a generic Output_section_data.
3667 is_output_section_data() const
3669 return this->shndx_ == OUTPUT_SECTION_CODE;
3672 // Return the object for an input section.
3676 // Return the input section index for an input section.
3680 // For non-input-sections, return the associated Output_section_data
3682 Output_section_data*
3683 output_section_data() const
3685 gold_assert(!this->is_input_section());
3686 return this->u2_.posd;
3689 // For a merge section, return the Output_merge_base pointer.
3691 output_merge_base() const
3693 gold_assert(this->is_merge_section());
3694 return this->u2_.pomb;
3697 // Return the Output_relaxed_input_section object.
3698 Output_relaxed_input_section*
3699 relaxed_input_section() const
3701 gold_assert(this->is_relaxed_input_section());
3702 return this->u2_.poris;
3705 // Set the output section.
3707 set_output_section(Output_section* os)
3709 gold_assert(!this->is_input_section());
3710 Output_section_data* posd =
3711 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3712 posd->set_output_section(os);
3715 // Set the address and file offset. This is called during
3716 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3717 // the enclosing section.
3719 set_address_and_file_offset(uint64_t address, off_t file_offset,
3720 off_t section_file_offset);
3722 // Reset the address and file offset.
3724 reset_address_and_file_offset();
3726 // Finalize the data size.
3728 finalize_data_size();
3730 // Add an input section, for SHF_MERGE sections.
3732 add_input_section(Relobj* object, unsigned int shndx)
3734 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3735 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3736 return this->u2_.posd->add_input_section(object, shndx);
3739 // Given an input OBJECT, an input section index SHNDX within that
3740 // object, and an OFFSET relative to the start of that input
3741 // section, return whether or not the output offset is known. If
3742 // this function returns true, it sets *POUTPUT to the offset in
3743 // the output section, relative to the start of the input section
3744 // in the output section. *POUTPUT may be different from OFFSET
3745 // for a merged section.
3747 output_offset(const Relobj* object, unsigned int shndx,
3748 section_offset_type offset,
3749 section_offset_type* poutput) const;
3751 // Return whether this is the merge section for the input section
3754 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3756 // Write out the data. This does nothing for an input section.
3758 write(Output_file*);
3760 // Write the data to a buffer. This does nothing for an input
3763 write_to_buffer(unsigned char*);
3765 // Print to a map file.
3767 print_to_mapfile(Mapfile*) const;
3769 // Print statistics about merge sections to stderr.
3771 print_merge_stats(const char* section_name)
3773 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3774 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3775 this->u2_.posd->print_merge_stats(section_name);
3779 // Code values which appear in shndx_. If the value is not one of
3780 // these codes, it is the input section index in the object file.
3783 // An Output_section_data.
3784 OUTPUT_SECTION_CODE = -1U,
3785 // An Output_section_data for an SHF_MERGE section with
3786 // SHF_STRINGS not set.
3787 MERGE_DATA_SECTION_CODE = -2U,
3788 // An Output_section_data for an SHF_MERGE section with
3790 MERGE_STRING_SECTION_CODE = -3U,
3791 // An Output_section_data for a relaxed input section.
3792 RELAXED_INPUT_SECTION_CODE = -4U
3795 // For an ordinary input section, this is the section index in the
3796 // input file. For an Output_section_data, this is
3797 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3798 // MERGE_STRING_SECTION_CODE.
3799 unsigned int shndx_;
3800 // The required alignment, stored as a power of 2.
3801 unsigned int p2align_;
3804 // For an ordinary input section, the section size.
3806 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3807 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3813 // For an ordinary input section, the object which holds the
3816 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3817 // MERGE_STRING_SECTION_CODE, the data.
3818 Output_section_data* posd;
3819 Output_merge_base* pomb;
3820 // For RELAXED_INPUT_SECTION_CODE, the data.
3821 Output_relaxed_input_section* poris;
3823 // The line number of the pattern it matches in the --section-ordering-file
3824 // file. It is 0 if does not match any pattern.
3825 unsigned int section_order_index_;
3828 // Store the list of input sections for this Output_section into the
3829 // list passed in. This removes the input sections, leaving only
3830 // any Output_section_data elements. This returns the size of those
3831 // Output_section_data elements. ADDRESS is the address of this
3832 // output section. FILL is the fill value to use, in case there are
3833 // any spaces between the remaining Output_section_data elements.
3835 get_input_sections(uint64_t address, const std::string& fill,
3836 std::list<Input_section>*);
3838 // Add a script input section. A script input section can either be
3839 // a plain input section or a sub-class of Output_section_data.
3841 add_script_input_section(const Input_section& input_section);
3843 // Set the current size of the output section.
3845 set_current_data_size(off_t size)
3846 { this->set_current_data_size_for_child(size); }
3848 // End of linker script support.
3850 // Save states before doing section layout.
3851 // This is used for relaxation.
3855 // Restore states prior to section layout.
3863 // Convert existing input sections to relaxed input sections.
3865 convert_input_sections_to_relaxed_sections(
3866 const std::vector<Output_relaxed_input_section*>& sections);
3868 // Find a relaxed input section to an input section in OBJECT
3869 // with index SHNDX. Return NULL if none is found.
3870 const Output_relaxed_input_section*
3871 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3873 // Whether section offsets need adjustment due to relaxation.
3875 section_offsets_need_adjustment() const
3876 { return this->section_offsets_need_adjustment_; }
3878 // Set section_offsets_need_adjustment to be true.
3880 set_section_offsets_need_adjustment()
3881 { this->section_offsets_need_adjustment_ = true; }
3883 // Set section_offsets_need_adjustment to be false.
3885 clear_section_offsets_need_adjustment()
3886 { this->section_offsets_need_adjustment_ = false; }
3888 // Adjust section offsets of input sections in this. This is
3889 // requires if relaxation caused some input sections to change sizes.
3891 adjust_section_offsets();
3893 // Whether this is a NOLOAD section.
3896 { return this->is_noload_; }
3901 { this->is_noload_ = true; }
3903 // Print merge statistics to stderr.
3905 print_merge_stats();
3907 // Set a fixed layout for the section. Used for incremental update links.
3909 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3910 uint64_t sh_addralign);
3912 // Return TRUE if the section has a fixed layout.
3914 has_fixed_layout() const
3915 { return this->has_fixed_layout_; }
3917 // Set flag to allow patch space for this section. Used for full
3918 // incremental links.
3920 set_is_patch_space_allowed()
3921 { this->is_patch_space_allowed_ = true; }
3923 // Set a fill method to use for free space left in the output section
3924 // during incremental links.
3926 set_free_space_fill(Output_fill* free_space_fill)
3928 this->free_space_fill_ = free_space_fill;
3929 this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3932 // Reserve space within the fixed layout for the section. Used for
3933 // incremental update links.
3935 reserve(uint64_t sh_offset, uint64_t sh_size);
3937 // Allocate space from the free list for the section. Used for
3938 // incremental update links.
3940 allocate(off_t len, uint64_t addralign);
3942 typedef std::vector<Input_section> Input_section_list;
3944 // Allow access to the input sections.
3945 const Input_section_list&
3946 input_sections() const
3947 { return this->input_sections_; }
3951 { return this->input_sections_; }
3954 // Return the output section--i.e., the object itself.
3959 const Output_section*
3960 do_output_section() const
3963 // Return the section index in the output file.
3965 do_out_shndx() const
3967 gold_assert(this->out_shndx_ != -1U);
3968 return this->out_shndx_;
3971 // Set the output section index.
3973 do_set_out_shndx(unsigned int shndx)
3975 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3976 this->out_shndx_ = shndx;
3979 // Update the data size of the Output_section. For a typical
3980 // Output_section, there is nothing to do, but if there are any
3981 // Output_section_data objects we need to do a trial layout
3986 // Set the final data size of the Output_section. For a typical
3987 // Output_section, there is nothing to do, but if there are any
3988 // Output_section_data objects we need to set their final addresses
3991 set_final_data_size();
3993 // Reset the address and file offset.
3995 do_reset_address_and_file_offset();
3997 // Return true if address and file offset already have reset values. In
3998 // other words, calling reset_address_and_file_offset will not change them.
4000 do_address_and_file_offset_have_reset_values() const;
4002 // Write the data to the file. For a typical Output_section, this
4003 // does nothing: the data is written out by calling Object::Relocate
4004 // on each input object. But if there are any Output_section_data
4005 // objects we do need to write them out here.
4007 do_write(Output_file*);
4009 // Return the address alignment--function required by parent class.
4011 do_addralign() const
4012 { return this->addralign_; }
4014 // Return whether there is a load address.
4016 do_has_load_address() const
4017 { return this->has_load_address_; }
4019 // Return the load address.
4021 do_load_address() const
4023 gold_assert(this->has_load_address_);
4024 return this->load_address_;
4027 // Return whether this is an Output_section.
4029 do_is_section() const
4032 // Return whether this is a section of the specified type.
4034 do_is_section_type(elfcpp::Elf_Word type) const
4035 { return this->type_ == type; }
4037 // Return whether the specified section flag is set.
4039 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
4040 { return (this->flags_ & flag) != 0; }
4042 // Set the TLS offset. Called only for SHT_TLS sections.
4044 do_set_tls_offset(uint64_t tls_base);
4046 // Return the TLS offset, relative to the base of the TLS segment.
4047 // Valid only for SHT_TLS sections.
4049 do_tls_offset() const
4050 { return this->tls_offset_; }
4052 // This may be implemented by a child class.
4054 do_finalize_name(Layout*)
4057 // Print to the map file.
4059 do_print_to_mapfile(Mapfile*) const;
4061 // Record that this section requires postprocessing after all
4062 // relocations have been applied. This is called by a child class.
4064 set_requires_postprocessing()
4066 this->requires_postprocessing_ = true;
4067 this->after_input_sections_ = true;
4070 // Write all the data of an Output_section into the postprocessing
4073 write_to_postprocessing_buffer();
4075 // Whether this always keeps an input section list
4077 always_keeps_input_sections() const
4078 { return this->always_keeps_input_sections_; }
4080 // Always keep an input section list.
4082 set_always_keeps_input_sections()
4084 gold_assert(this->current_data_size_for_child() == 0);
4085 this->always_keeps_input_sections_ = true;
4089 // We only save enough information to undo the effects of section layout.
4090 class Checkpoint_output_section
4093 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
4094 const Input_section_list& input_sections,
4095 off_t first_input_offset,
4096 bool attached_input_sections_are_sorted)
4097 : addralign_(addralign), flags_(flags),
4098 input_sections_(input_sections),
4099 input_sections_size_(input_sections_.size()),
4100 input_sections_copy_(), first_input_offset_(first_input_offset),
4101 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
4105 ~Checkpoint_output_section()
4108 // Return the address alignment.
4111 { return this->addralign_; }
4114 set_addralign(uint64_t val)
4115 { this->addralign_ = val; }
4117 // Return the section flags.
4120 { return this->flags_; }
4122 // Return a reference to the input section list copy.
4125 { return &this->input_sections_copy_; }
4127 // Return the size of input_sections at the time when checkpoint is
4130 input_sections_size() const
4131 { return this->input_sections_size_; }
4133 // Whether input sections are copied.
4135 input_sections_saved() const
4136 { return this->input_sections_copy_.size() == this->input_sections_size_; }
4139 first_input_offset() const
4140 { return this->first_input_offset_; }
4143 attached_input_sections_are_sorted() const
4144 { return this->attached_input_sections_are_sorted_; }
4146 // Save input sections.
4148 save_input_sections()
4150 this->input_sections_copy_.reserve(this->input_sections_size_);
4151 this->input_sections_copy_.clear();
4152 Input_section_list::const_iterator p = this->input_sections_.begin();
4153 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
4154 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
4155 this->input_sections_copy_.push_back(*p);
4159 // The section alignment.
4160 uint64_t addralign_;
4161 // The section flags.
4162 elfcpp::Elf_Xword flags_;
4163 // Reference to the input sections to be checkpointed.
4164 const Input_section_list& input_sections_;
4165 // Size of the checkpointed portion of input_sections_;
4166 size_t input_sections_size_;
4167 // Copy of input sections.
4168 Input_section_list input_sections_copy_;
4169 // The offset of the first entry in input_sections_.
4170 off_t first_input_offset_;
4171 // True if the input sections attached to this output section have
4172 // already been sorted.
4173 bool attached_input_sections_are_sorted_;
4176 // This class is used to sort the input sections.
4177 class Input_section_sort_entry;
4179 // This is the sort comparison function for ctors and dtors.
4180 struct Input_section_sort_compare
4183 operator()(const Input_section_sort_entry&,
4184 const Input_section_sort_entry&) const;
4187 // This is the sort comparison function for .init_array and .fini_array.
4188 struct Input_section_sort_init_fini_compare
4191 operator()(const Input_section_sort_entry&,
4192 const Input_section_sort_entry&) const;
4195 // This is the sort comparison function when a section order is specified
4196 // from an input file.
4197 struct Input_section_sort_section_order_index_compare
4200 operator()(const Input_section_sort_entry&,
4201 const Input_section_sort_entry&) const;
4204 // This is the sort comparison function for .text to sort sections with
4205 // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4206 struct Input_section_sort_section_prefix_special_ordering_compare
4209 operator()(const Input_section_sort_entry&,
4210 const Input_section_sort_entry&) const;
4213 // This is the sort comparison function for sorting sections by name.
4214 struct Input_section_sort_section_name_compare
4217 operator()(const Input_section_sort_entry&,
4218 const Input_section_sort_entry&) const;
4221 // Fill data. This is used to fill in data between input sections.
4222 // It is also used for data statements (BYTE, WORD, etc.) in linker
4223 // scripts. When we have to keep track of the input sections, we
4224 // can use an Output_data_const, but we don't want to have to keep
4225 // track of input sections just to implement fills.
4229 Fill(off_t section_offset, off_t length)
4230 : section_offset_(section_offset),
4231 length_(convert_to_section_size_type(length))
4234 // Return section offset.
4236 section_offset() const
4237 { return this->section_offset_; }
4239 // Return fill length.
4242 { return this->length_; }
4245 // The offset within the output section.
4246 off_t section_offset_;
4247 // The length of the space to fill.
4248 section_size_type length_;
4251 typedef std::vector<Fill> Fill_list;
4253 // Map used during relaxation of existing sections. This map
4254 // a section id an input section list index. We assume that
4255 // Input_section_list is a vector.
4256 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4258 // Add a new output section by Input_section.
4260 add_output_section_data(Input_section*);
4262 // Add an SHF_MERGE input section. Returns true if the section was
4263 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4264 // stores information about the merged input sections.
4266 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4267 uint64_t entsize, uint64_t addralign,
4268 bool keeps_input_sections);
4270 // Add an output SHF_MERGE section POSD to this output section.
4271 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4272 // ENTSIZE is the entity size. This returns the entry added to
4275 add_output_merge_section(Output_section_data* posd, bool is_string,
4278 // Find the merge section into which an input section with index SHNDX in
4279 // OBJECT has been added. Return NULL if none found.
4280 Output_section_data*
4281 find_merge_section(const Relobj* object, unsigned int shndx) const;
4283 // Build a relaxation map.
4285 build_relaxation_map(
4286 const Input_section_list& input_sections,
4288 Relaxation_map* map) const;
4290 // Convert input sections in an input section list into relaxed sections.
4292 convert_input_sections_in_list_to_relaxed_sections(
4293 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4294 const Relaxation_map& map,
4295 Input_section_list* input_sections);
4297 // Build the lookup maps for merge and relaxed input sections.
4299 build_lookup_maps() const;
4301 // Most of these fields are only valid after layout.
4303 // The name of the section. This will point into a Stringpool.
4305 // The section address is in the parent class.
4306 // The section alignment.
4307 uint64_t addralign_;
4308 // The section entry size.
4310 // The load address. This is only used when using a linker script
4311 // with a SECTIONS clause. The has_load_address_ field indicates
4312 // whether this field is valid.
4313 uint64_t load_address_;
4314 // The file offset is in the parent class.
4315 // Set the section link field to the index of this section.
4316 const Output_data* link_section_;
4317 // If link_section_ is NULL, this is the link field.
4319 // Set the section info field to the index of this section.
4320 const Output_section* info_section_;
4321 // If info_section_ is NULL, set the info field to the symbol table
4322 // index of this symbol.
4323 const Symbol* info_symndx_;
4324 // If info_section_ and info_symndx_ are NULL, this is the section
4327 // The section type.
4328 const elfcpp::Elf_Word type_;
4329 // The section flags.
4330 elfcpp::Elf_Xword flags_;
4331 // The order of this section in the output segment.
4332 Output_section_order order_;
4333 // The section index.
4334 unsigned int out_shndx_;
4335 // If there is a STT_SECTION for this output section in the normal
4336 // symbol table, this is the symbol index. This starts out as zero.
4337 // It is initialized in Layout::finalize() to be the index, or -1U
4338 // if there isn't one.
4339 unsigned int symtab_index_;
4340 // If there is a STT_SECTION for this output section in the dynamic
4341 // symbol table, this is the symbol index. This starts out as zero.
4342 // It is initialized in Layout::finalize() to be the index, or -1U
4343 // if there isn't one.
4344 unsigned int dynsym_index_;
4345 // The input sections. This will be empty in cases where we don't
4346 // need to keep track of them.
4347 Input_section_list input_sections_;
4348 // The offset of the first entry in input_sections_.
4349 off_t first_input_offset_;
4350 // The fill data. This is separate from input_sections_ because we
4351 // often will need fill sections without needing to keep track of
4354 // If the section requires postprocessing, this buffer holds the
4355 // section contents during relocation.
4356 unsigned char* postprocessing_buffer_;
4357 // Whether this output section needs a STT_SECTION symbol in the
4358 // normal symbol table. This will be true if there is a relocation
4360 bool needs_symtab_index_ : 1;
4361 // Whether this output section needs a STT_SECTION symbol in the
4362 // dynamic symbol table. This will be true if there is a dynamic
4363 // relocation which needs it.
4364 bool needs_dynsym_index_ : 1;
4365 // Whether the link field of this output section should point to the
4366 // normal symbol table.
4367 bool should_link_to_symtab_ : 1;
4368 // Whether the link field of this output section should point to the
4369 // dynamic symbol table.
4370 bool should_link_to_dynsym_ : 1;
4371 // Whether this section should be written after all the input
4372 // sections are complete.
4373 bool after_input_sections_ : 1;
4374 // Whether this section requires post processing after all
4375 // relocations have been applied.
4376 bool requires_postprocessing_ : 1;
4377 // Whether an input section was mapped to this output section
4378 // because of a SECTIONS clause in a linker script.
4379 bool found_in_sections_clause_ : 1;
4380 // Whether this section has an explicitly specified load address.
4381 bool has_load_address_ : 1;
4382 // True if the info_section_ field means the section index of the
4383 // section, false if it means the symbol index of the corresponding
4385 bool info_uses_section_index_ : 1;
4386 // True if input sections attached to this output section have to be
4387 // sorted according to a specified order.
4388 bool input_section_order_specified_ : 1;
4389 // True if the input sections attached to this output section may
4391 bool may_sort_attached_input_sections_ : 1;
4392 // True if the input sections attached to this output section must
4394 bool must_sort_attached_input_sections_ : 1;
4395 // True if the input sections attached to this output section have
4396 // already been sorted.
4397 bool attached_input_sections_are_sorted_ : 1;
4398 // True if this section holds relro data.
4400 // True if this is a small section.
4401 bool is_small_section_ : 1;
4402 // True if this is a large section.
4403 bool is_large_section_ : 1;
4404 // Whether code-fills are generated at write.
4405 bool generate_code_fills_at_write_ : 1;
4406 // Whether the entry size field should be zero.
4407 bool is_entsize_zero_ : 1;
4408 // Whether section offsets need adjustment due to relaxation.
4409 bool section_offsets_need_adjustment_ : 1;
4410 // Whether this is a NOLOAD section.
4411 bool is_noload_ : 1;
4412 // Whether this always keeps input section.
4413 bool always_keeps_input_sections_ : 1;
4414 // Whether this section has a fixed layout, for incremental update links.
4415 bool has_fixed_layout_ : 1;
4416 // True if we can add patch space to this section.
4417 bool is_patch_space_allowed_ : 1;
4418 // True if this output section goes into a unique segment.
4419 bool is_unique_segment_ : 1;
4420 // For SHT_TLS sections, the offset of this section relative to the base
4421 // of the TLS segment.
4422 uint64_t tls_offset_;
4423 // Additional segment flags, specified via linker plugin, when mapping some
4424 // input sections to unique segments.
4425 uint64_t extra_segment_flags_;
4426 // Segment alignment specified via linker plugin, when mapping some
4427 // input sections to unique segments.
4428 uint64_t segment_alignment_;
4429 // Saved checkpoint.
4430 Checkpoint_output_section* checkpoint_;
4431 // Fast lookup maps for merged and relaxed input sections.
4432 Output_section_lookup_maps* lookup_maps_;
4433 // List of available regions within the section, for incremental
4435 Free_list free_list_;
4436 // Method for filling chunks of free space.
4437 Output_fill* free_space_fill_;
4438 // Amount added as patch space for incremental linking.
4442 // An output segment. PT_LOAD segments are built from collections of
4443 // output sections. Other segments typically point within PT_LOAD
4444 // segments, and are built directly as needed.
4446 // NOTE: We want to use the copy constructor for this class. During
4447 // relaxation, we may try built the segments multiple times. We do
4448 // that by copying the original segment list before lay-out, doing
4449 // a trial lay-out and roll-back to the saved copied if we need to
4450 // to the lay-out again.
4452 class Output_segment
4455 // Create an output segment, specifying the type and flags.
4456 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4458 // Return the virtual address.
4461 { return this->vaddr_; }
4463 // Return the physical address.
4466 { return this->paddr_; }
4468 // Return the segment type.
4471 { return this->type_; }
4473 // Return the segment flags.
4476 { return this->flags_; }
4478 // Return the memory size.
4481 { return this->memsz_; }
4483 // Return the file size.
4486 { return this->filesz_; }
4488 // Return the file offset.
4491 { return this->offset_; }
4493 // Whether this is a segment created to hold large data sections.
4495 is_large_data_segment() const
4496 { return this->is_large_data_segment_; }
4498 // Record that this is a segment created to hold large data
4501 set_is_large_data_segment()
4502 { this->is_large_data_segment_ = true; }
4505 is_unique_segment() const
4506 { return this->is_unique_segment_; }
4508 // Mark segment as unique, happens when linker plugins request that
4509 // certain input sections be mapped to unique segments.
4511 set_is_unique_segment()
4512 { this->is_unique_segment_ = true; }
4514 // Return the maximum alignment of the Output_data.
4516 maximum_alignment();
4518 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4519 // the segment flags to use.
4521 add_output_section_to_load(Layout* layout, Output_section* os,
4522 elfcpp::Elf_Word seg_flags);
4524 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4525 // is the segment flags to use.
4527 add_output_section_to_nonload(Output_section* os,
4528 elfcpp::Elf_Word seg_flags);
4530 // Remove an Output_section from this segment. It is an error if it
4533 remove_output_section(Output_section* os);
4535 // Add an Output_data (which need not be an Output_section) to the
4536 // start of this segment.
4538 add_initial_output_data(Output_data*);
4540 // Return true if this segment has any sections which hold actual
4541 // data, rather than being a BSS section.
4543 has_any_data_sections() const;
4545 // Whether this segment has a dynamic relocs.
4547 has_dynamic_reloc() const;
4549 // Return the first section.
4551 first_section() const;
4553 // Return the address of the first section.
4555 first_section_load_address() const
4557 const Output_section* os = this->first_section();
4558 return os->has_load_address() ? os->load_address() : os->address();
4561 // Return whether the addresses have been set already.
4563 are_addresses_set() const
4564 { return this->are_addresses_set_; }
4566 // Set the addresses.
4568 set_addresses(uint64_t vaddr, uint64_t paddr)
4570 this->vaddr_ = vaddr;
4571 this->paddr_ = paddr;
4572 this->are_addresses_set_ = true;
4575 // Update the flags for the flags of an output section added to this
4578 update_flags_for_output_section(elfcpp::Elf_Xword flags)
4580 // The ELF ABI specifies that a PT_TLS segment should always have
4581 // PF_R as the flags.
4582 if (this->type() != elfcpp::PT_TLS)
4583 this->flags_ |= flags;
4586 // Set the segment flags. This is only used if we have a PHDRS
4587 // clause which explicitly specifies the flags.
4589 set_flags(elfcpp::Elf_Word flags)
4590 { this->flags_ = flags; }
4592 // Set the address of the segment to ADDR and the offset to *POFF
4593 // and set the addresses and offsets of all contained output
4594 // sections accordingly. Set the section indexes of all contained
4595 // output sections starting with *PSHNDX. If RESET is true, first
4596 // reset the addresses of the contained sections. Return the
4597 // address of the immediately following segment. Update *POFF and
4598 // *PSHNDX. This should only be called for a PT_LOAD segment.
4600 set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
4601 unsigned int* increase_relro, bool* has_relro,
4602 off_t* poff, unsigned int* pshndx);
4604 // Set the minimum alignment of this segment. This may be adjusted
4605 // upward based on the section alignments.
4607 set_minimum_p_align(uint64_t align)
4609 if (align > this->min_p_align_)
4610 this->min_p_align_ = align;
4613 // Set the offset of this segment based on the section. This should
4614 // only be called for a non-PT_LOAD segment.
4616 set_offset(unsigned int increase);
4618 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4622 // Return the number of output sections.
4624 output_section_count() const;
4626 // Return the section attached to the list segment with the lowest
4627 // load address. This is used when handling a PHDRS clause in a
4630 section_with_lowest_load_address() const;
4632 // Write the segment header into *OPHDR.
4633 template<int size, bool big_endian>
4635 write_header(elfcpp::Phdr_write<size, big_endian>*);
4637 // Write the section headers of associated sections into V.
4638 template<int size, bool big_endian>
4640 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4641 unsigned int* pshndx) const;
4643 // Print the output sections in the map file.
4645 print_sections_to_mapfile(Mapfile*) const;
4648 typedef std::vector<Output_data*> Output_data_list;
4650 // Find the maximum alignment in an Output_data_list.
4652 maximum_alignment_list(const Output_data_list*);
4654 // Return whether the first data section is a relro section.
4656 is_first_section_relro() const;
4658 // Set the section addresses in an Output_data_list.
4660 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4661 uint64_t addr, off_t* poff, unsigned int* pshndx,
4664 // Return the number of Output_sections in an Output_data_list.
4666 output_section_count_list(const Output_data_list*) const;
4668 // Return whether an Output_data_list has a dynamic reloc.
4670 has_dynamic_reloc_list(const Output_data_list*) const;
4672 // Find the section with the lowest load address in an
4673 // Output_data_list.
4675 lowest_load_address_in_list(const Output_data_list* pdl,
4676 Output_section** found,
4677 uint64_t* found_lma) const;
4679 // Find the first and last entries by address.
4681 find_first_and_last_list(const Output_data_list* pdl,
4682 const Output_data** pfirst,
4683 const Output_data** plast) const;
4685 // Write the section headers in the list into V.
4686 template<int size, bool big_endian>
4688 write_section_headers_list(const Layout*, const Stringpool*,
4689 const Output_data_list*, unsigned char* v,
4690 unsigned int* pshdx) const;
4692 // Print a section list to the mapfile.
4694 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4696 // NOTE: We want to use the copy constructor. Currently, shallow copy
4697 // works for us so we do not need to write our own copy constructor.
4699 // The list of output data attached to this segment.
4700 Output_data_list output_lists_[ORDER_MAX];
4701 // The segment virtual address.
4703 // The segment physical address.
4705 // The size of the segment in memory.
4707 // The maximum section alignment. The is_max_align_known_ field
4708 // indicates whether this has been finalized.
4709 uint64_t max_align_;
4710 // The required minimum value for the p_align field. This is used
4711 // for PT_LOAD segments. Note that this does not mean that
4712 // addresses should be aligned to this value; it means the p_paddr
4713 // and p_vaddr fields must be congruent modulo this value. For
4714 // non-PT_LOAD segments, the dynamic linker works more efficiently
4715 // if the p_align field has the more conventional value, although it
4716 // can align as needed.
4717 uint64_t min_p_align_;
4718 // The offset of the segment data within the file.
4720 // The size of the segment data in the file.
4722 // The segment type;
4723 elfcpp::Elf_Word type_;
4724 // The segment flags.
4725 elfcpp::Elf_Word flags_;
4726 // Whether we have finalized max_align_.
4727 bool is_max_align_known_ : 1;
4728 // Whether vaddr and paddr were set by a linker script.
4729 bool are_addresses_set_ : 1;
4730 // Whether this segment holds large data sections.
4731 bool is_large_data_segment_ : 1;
4732 // Whether this was marked as a unique segment via a linker plugin.
4733 bool is_unique_segment_ : 1;
4736 // This class represents the output file.
4741 Output_file(const char* name);
4743 // Indicate that this is a temporary file which should not be
4747 { this->is_temporary_ = true; }
4749 // Try to open an existing file. Returns false if the file doesn't
4750 // exist, has a size of 0 or can't be mmaped. This method is
4751 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4752 // that file as the base for incremental linking.
4754 open_base_file(const char* base_name, bool writable);
4756 // Open the output file. FILE_SIZE is the final size of the file.
4757 // If the file already exists, it is deleted/truncated. This method
4758 // is thread-unsafe.
4760 open(off_t file_size);
4762 // Resize the output file. This method is thread-unsafe.
4764 resize(off_t file_size);
4766 // Close the output file (flushing all buffered data) and make sure
4767 // there are no errors. This method is thread-unsafe.
4771 // Return the size of this file.
4774 { return this->file_size_; }
4776 // Return the name of this file.
4779 { return this->name_; }
4781 // We currently always use mmap which makes the view handling quite
4782 // simple. In the future we may support other approaches.
4784 // Write data to the output file.
4786 write(off_t offset, const void* data, size_t len)
4787 { memcpy(this->base_ + offset, data, len); }
4789 // Get a buffer to use to write to the file, given the offset into
4790 // the file and the size.
4792 get_output_view(off_t start, size_t size)
4794 gold_assert(start >= 0
4795 && start + static_cast<off_t>(size) <= this->file_size_);
4796 return this->base_ + start;
4799 // VIEW must have been returned by get_output_view. Write the
4800 // buffer to the file, passing in the offset and the size.
4802 write_output_view(off_t, size_t, unsigned char*)
4805 // Get a read/write buffer. This is used when we want to write part
4806 // of the file, read it in, and write it again.
4808 get_input_output_view(off_t start, size_t size)
4809 { return this->get_output_view(start, size); }
4811 // Write a read/write buffer back to the file.
4813 write_input_output_view(off_t, size_t, unsigned char*)
4816 // Get a read buffer. This is used when we just want to read part
4817 // of the file back it in.
4818 const unsigned char*
4819 get_input_view(off_t start, size_t size)
4820 { return this->get_output_view(start, size); }
4822 // Release a read bfufer.
4824 free_input_view(off_t, size_t, const unsigned char*)
4828 // Map the file into memory or, if that fails, allocate anonymous
4833 // Allocate anonymous memory for the file.
4837 // Map the file into memory.
4839 map_no_anonymous(bool);
4841 // Unmap the file from memory (and flush to disk buffers).
4851 // Base of file mapped into memory.
4852 unsigned char* base_;
4853 // True iff base_ points to a memory buffer rather than an output file.
4854 bool map_is_anonymous_;
4855 // True if base_ was allocated using new rather than mmap.
4856 bool map_is_allocated_;
4857 // True if this is a temporary file which should not be output.
4861 } // End namespace gold.
4863 #endif // !defined(GOLD_OUTPUT_H)