1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "reloc-types.h"
37 class General_options;
41 class Output_merge_base;
43 class Relocatable_relocs;
45 template<int size, bool big_endian>
47 template<int size, bool big_endian>
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
52 // An abtract class for data which has to go into the output file.
57 explicit Output_data()
58 : address_(0), data_size_(0), offset_(-1),
59 is_address_valid_(false), is_data_size_valid_(false),
60 is_offset_valid_(false), is_data_size_fixed_(false),
61 has_dynamic_reloc_(false)
67 // Return the address. For allocated sections, this is only valid
68 // after Layout::finalize is finished.
72 gold_assert(this->is_address_valid_);
73 return this->address_;
76 // Return the size of the data. For allocated sections, this must
77 // be valid after Layout::finalize calls set_address, but need not
78 // be valid before then.
82 gold_assert(this->is_data_size_valid_);
83 return this->data_size_;
86 // Get the current data size.
88 current_data_size() const
89 { return this->current_data_size_for_child(); }
91 // Return true if data size is fixed.
93 is_data_size_fixed() const
94 { return this->is_data_size_fixed_; }
96 // Return the file offset. This is only valid after
97 // Layout::finalize is finished. For some non-allocated sections,
98 // it may not be valid until near the end of the link.
102 gold_assert(this->is_offset_valid_);
103 return this->offset_;
106 // Reset the address and file offset. This essentially disables the
107 // sanity testing about duplicate and unknown settings.
109 reset_address_and_file_offset()
111 this->is_address_valid_ = false;
112 this->is_offset_valid_ = false;
113 if (!this->is_data_size_fixed_)
114 this->is_data_size_valid_ = false;
115 this->do_reset_address_and_file_offset();
118 // Return true if address and file offset already have reset values. In
119 // other words, calling reset_address_and_file_offset will not change them.
121 address_and_file_offset_have_reset_values() const
122 { return this->do_address_and_file_offset_have_reset_values(); }
124 // Return the required alignment.
127 { return this->do_addralign(); }
129 // Return whether this has a load address.
131 has_load_address() const
132 { return this->do_has_load_address(); }
134 // Return the load address.
137 { return this->do_load_address(); }
139 // Return whether this is an Output_section.
142 { return this->do_is_section(); }
144 // Return whether this is an Output_section of the specified type.
146 is_section_type(elfcpp::Elf_Word stt) const
147 { return this->do_is_section_type(stt); }
149 // Return whether this is an Output_section with the specified flag
152 is_section_flag_set(elfcpp::Elf_Xword shf) const
153 { return this->do_is_section_flag_set(shf); }
155 // Return the output section that this goes in, if there is one.
158 { return this->do_output_section(); }
160 const Output_section*
161 output_section() const
162 { return this->do_output_section(); }
164 // Return the output section index, if there is an output section.
167 { return this->do_out_shndx(); }
169 // Set the output section index, if this is an output section.
171 set_out_shndx(unsigned int shndx)
172 { this->do_set_out_shndx(shndx); }
174 // Set the address and file offset of this data, and finalize the
175 // size of the data. This is called during Layout::finalize for
176 // allocated sections.
178 set_address_and_file_offset(uint64_t addr, off_t off)
180 this->set_address(addr);
181 this->set_file_offset(off);
182 this->finalize_data_size();
187 set_address(uint64_t addr)
189 gold_assert(!this->is_address_valid_);
190 this->address_ = addr;
191 this->is_address_valid_ = true;
194 // Set the file offset.
196 set_file_offset(off_t off)
198 gold_assert(!this->is_offset_valid_);
200 this->is_offset_valid_ = true;
203 // Update the data size without finalizing it.
205 pre_finalize_data_size()
207 if (!this->is_data_size_valid_)
209 // Tell the child class to update the data size.
210 this->update_data_size();
214 // Finalize the data size.
218 if (!this->is_data_size_valid_)
220 // Tell the child class to set the data size.
221 this->set_final_data_size();
222 gold_assert(this->is_data_size_valid_);
226 // Set the TLS offset. Called only for SHT_TLS sections.
228 set_tls_offset(uint64_t tls_base)
229 { this->do_set_tls_offset(tls_base); }
231 // Return the TLS offset, relative to the base of the TLS segment.
232 // Valid only for SHT_TLS sections.
235 { return this->do_tls_offset(); }
237 // Write the data to the output file. This is called after
238 // Layout::finalize is complete.
240 write(Output_file* file)
241 { this->do_write(file); }
243 // This is called by Layout::finalize to note that the sizes of
244 // allocated sections must now be fixed.
247 { Output_data::allocated_sizes_are_fixed = true; }
249 // Used to check that layout has been done.
252 { return Output_data::allocated_sizes_are_fixed; }
254 // Note that a dynamic reloc has been applied to this data.
257 { this->has_dynamic_reloc_ = true; }
259 // Return whether a dynamic reloc has been applied.
261 has_dynamic_reloc() const
262 { return this->has_dynamic_reloc_; }
264 // Whether the address is valid.
266 is_address_valid() const
267 { return this->is_address_valid_; }
269 // Whether the file offset is valid.
271 is_offset_valid() const
272 { return this->is_offset_valid_; }
274 // Whether the data size is valid.
276 is_data_size_valid() const
277 { return this->is_data_size_valid_; }
279 // Print information to the map file.
281 print_to_mapfile(Mapfile* mapfile) const
282 { return this->do_print_to_mapfile(mapfile); }
285 // Functions that child classes may or in some cases must implement.
287 // Write the data to the output file.
289 do_write(Output_file*) = 0;
291 // Return the required alignment.
293 do_addralign() const = 0;
295 // Return whether this has a load address.
297 do_has_load_address() const
300 // Return the load address.
302 do_load_address() const
303 { gold_unreachable(); }
305 // Return whether this is an Output_section.
307 do_is_section() const
310 // Return whether this is an Output_section of the specified type.
311 // This only needs to be implement by Output_section.
313 do_is_section_type(elfcpp::Elf_Word) const
316 // Return whether this is an Output_section with the specific flag
317 // set. This only needs to be implemented by Output_section.
319 do_is_section_flag_set(elfcpp::Elf_Xword) const
322 // Return the output section, if there is one.
323 virtual Output_section*
327 virtual const Output_section*
328 do_output_section() const
331 // Return the output section index, if there is an output section.
334 { gold_unreachable(); }
336 // Set the output section index, if this is an output section.
338 do_set_out_shndx(unsigned int)
339 { gold_unreachable(); }
341 // This is a hook for derived classes to set the preliminary data size.
342 // This is called by pre_finalize_data_size, normally called during
343 // Layout::finalize, before the section address is set, and is used
344 // during an incremental update, when we need to know the size of a
345 // section before allocating space in the output file. For classes
346 // where the current data size is up to date, this default version of
347 // the method can be inherited.
352 // This is a hook for derived classes to set the data size. This is
353 // called by finalize_data_size, normally called during
354 // Layout::finalize, when the section address is set.
356 set_final_data_size()
357 { gold_unreachable(); }
359 // A hook for resetting the address and file offset.
361 do_reset_address_and_file_offset()
364 // Return true if address and file offset already have reset values. In
365 // other words, calling reset_address_and_file_offset will not change them.
366 // A child class overriding do_reset_address_and_file_offset may need to
367 // also override this.
369 do_address_and_file_offset_have_reset_values() const
370 { return !this->is_address_valid_ && !this->is_offset_valid_; }
372 // Set the TLS offset. Called only for SHT_TLS sections.
374 do_set_tls_offset(uint64_t)
375 { gold_unreachable(); }
377 // Return the TLS offset, relative to the base of the TLS segment.
378 // Valid only for SHT_TLS sections.
380 do_tls_offset() const
381 { gold_unreachable(); }
383 // Print to the map file. This only needs to be implemented by
384 // classes which may appear in a PT_LOAD segment.
386 do_print_to_mapfile(Mapfile*) const
387 { gold_unreachable(); }
389 // Functions that child classes may call.
391 // Reset the address. The Output_section class needs this when an
392 // SHF_ALLOC input section is added to an output section which was
393 // formerly not SHF_ALLOC.
395 mark_address_invalid()
396 { this->is_address_valid_ = false; }
398 // Set the size of the data.
400 set_data_size(off_t data_size)
402 gold_assert(!this->is_data_size_valid_
403 && !this->is_data_size_fixed_);
404 this->data_size_ = data_size;
405 this->is_data_size_valid_ = true;
408 // Fix the data size. Once it is fixed, it cannot be changed
409 // and the data size remains always valid.
413 gold_assert(this->is_data_size_valid_);
414 this->is_data_size_fixed_ = true;
417 // Get the current data size--this is for the convenience of
418 // sections which build up their size over time.
420 current_data_size_for_child() const
421 { return this->data_size_; }
423 // Set the current data size--this is for the convenience of
424 // sections which build up their size over time.
426 set_current_data_size_for_child(off_t data_size)
428 gold_assert(!this->is_data_size_valid_);
429 this->data_size_ = data_size;
432 // Return default alignment for the target size.
436 // Return default alignment for a specified size--32 or 64.
438 default_alignment_for_size(int size);
441 Output_data(const Output_data&);
442 Output_data& operator=(const Output_data&);
444 // This is used for verification, to make sure that we don't try to
445 // change any sizes of allocated sections after we set the section
447 static bool allocated_sizes_are_fixed;
449 // Memory address in output file.
451 // Size of data in output file.
453 // File offset of contents in output file.
455 // Whether address_ is valid.
456 bool is_address_valid_ : 1;
457 // Whether data_size_ is valid.
458 bool is_data_size_valid_ : 1;
459 // Whether offset_ is valid.
460 bool is_offset_valid_ : 1;
461 // Whether data size is fixed.
462 bool is_data_size_fixed_ : 1;
463 // Whether any dynamic relocs have been applied to this section.
464 bool has_dynamic_reloc_ : 1;
467 // Output the section headers.
469 class Output_section_headers : public Output_data
472 Output_section_headers(const Layout*,
473 const Layout::Segment_list*,
474 const Layout::Section_list*,
475 const Layout::Section_list*,
477 const Output_section*);
480 // Write the data to the file.
482 do_write(Output_file*);
484 // Return the required alignment.
487 { return Output_data::default_alignment(); }
489 // Write to a map file.
491 do_print_to_mapfile(Mapfile* mapfile) const
492 { mapfile->print_output_data(this, _("** section headers")); }
494 // Update the data size.
497 { this->set_data_size(this->do_size()); }
499 // Set final data size.
501 set_final_data_size()
502 { this->set_data_size(this->do_size()); }
505 // Write the data to the file with the right size and endianness.
506 template<int size, bool big_endian>
508 do_sized_write(Output_file*);
510 // Compute data size.
514 const Layout* layout_;
515 const Layout::Segment_list* segment_list_;
516 const Layout::Section_list* section_list_;
517 const Layout::Section_list* unattached_section_list_;
518 const Stringpool* secnamepool_;
519 const Output_section* shstrtab_section_;
522 // Output the segment headers.
524 class Output_segment_headers : public Output_data
527 Output_segment_headers(const Layout::Segment_list& segment_list);
530 // Write the data to the file.
532 do_write(Output_file*);
534 // Return the required alignment.
537 { return Output_data::default_alignment(); }
539 // Write to a map file.
541 do_print_to_mapfile(Mapfile* mapfile) const
542 { mapfile->print_output_data(this, _("** segment headers")); }
544 // Set final data size.
546 set_final_data_size()
547 { this->set_data_size(this->do_size()); }
550 // Write the data to the file with the right size and endianness.
551 template<int size, bool big_endian>
553 do_sized_write(Output_file*);
555 // Compute the current size.
559 const Layout::Segment_list& segment_list_;
562 // Output the ELF file header.
564 class Output_file_header : public Output_data
567 Output_file_header(const Target*,
569 const Output_segment_headers*);
571 // Add information about the section headers. We lay out the ELF
572 // file header before we create the section headers.
573 void set_section_info(const Output_section_headers*,
574 const Output_section* shstrtab);
577 // Write the data to the file.
579 do_write(Output_file*);
581 // Return the required alignment.
584 { return Output_data::default_alignment(); }
586 // Write to a map file.
588 do_print_to_mapfile(Mapfile* mapfile) const
589 { mapfile->print_output_data(this, _("** file header")); }
591 // Set final data size.
593 set_final_data_size(void)
594 { this->set_data_size(this->do_size()); }
597 // Write the data to the file with the right size and endianness.
598 template<int size, bool big_endian>
600 do_sized_write(Output_file*);
602 // Return the value to use for the entry address.
604 typename elfcpp::Elf_types<size>::Elf_Addr
607 // Compute the current data size.
611 const Target* target_;
612 const Symbol_table* symtab_;
613 const Output_segment_headers* segment_header_;
614 const Output_section_headers* section_header_;
615 const Output_section* shstrtab_;
618 // Output sections are mainly comprised of input sections. However,
619 // there are cases where we have data to write out which is not in an
620 // input section. Output_section_data is used in such cases. This is
621 // an abstract base class.
623 class Output_section_data : public Output_data
626 Output_section_data(off_t data_size, uint64_t addralign,
627 bool is_data_size_fixed)
628 : Output_data(), output_section_(NULL), addralign_(addralign)
630 this->set_data_size(data_size);
631 if (is_data_size_fixed)
632 this->fix_data_size();
635 Output_section_data(uint64_t addralign)
636 : Output_data(), output_section_(NULL), addralign_(addralign)
639 // Return the output section.
642 { return this->output_section_; }
644 const Output_section*
645 output_section() const
646 { return this->output_section_; }
648 // Record the output section.
650 set_output_section(Output_section* os);
652 // Add an input section, for SHF_MERGE sections. This returns true
653 // if the section was handled.
655 add_input_section(Relobj* object, unsigned int shndx)
656 { return this->do_add_input_section(object, shndx); }
658 // Given an input OBJECT, an input section index SHNDX within that
659 // object, and an OFFSET relative to the start of that input
660 // section, return whether or not the corresponding offset within
661 // the output section is known. If this function returns true, it
662 // sets *POUTPUT to the output offset. The value -1 indicates that
663 // this input offset is being discarded.
665 output_offset(const Relobj* object, unsigned int shndx,
666 section_offset_type offset,
667 section_offset_type* poutput) const
668 { return this->do_output_offset(object, shndx, offset, poutput); }
670 // Return whether this is the merge section for the input section
671 // SHNDX in OBJECT. This should return true when output_offset
672 // would return true for some values of OFFSET.
674 is_merge_section_for(const Relobj* object, unsigned int shndx) const
675 { return this->do_is_merge_section_for(object, shndx); }
677 // Write the contents to a buffer. This is used for sections which
678 // require postprocessing, such as compression.
680 write_to_buffer(unsigned char* buffer)
681 { this->do_write_to_buffer(buffer); }
683 // Print merge stats to stderr. This should only be called for
684 // SHF_MERGE sections.
686 print_merge_stats(const char* section_name)
687 { this->do_print_merge_stats(section_name); }
690 // The child class must implement do_write.
692 // The child class may implement specific adjustments to the output
695 do_adjust_output_section(Output_section*)
698 // May be implemented by child class. Return true if the section
701 do_add_input_section(Relobj*, unsigned int)
702 { gold_unreachable(); }
704 // The child class may implement output_offset.
706 do_output_offset(const Relobj*, unsigned int, section_offset_type,
707 section_offset_type*) const
710 // The child class may implement is_merge_section_for.
712 do_is_merge_section_for(const Relobj*, unsigned int) const
715 // The child class may implement write_to_buffer. Most child
716 // classes can not appear in a compressed section, and they do not
719 do_write_to_buffer(unsigned char*)
720 { gold_unreachable(); }
722 // Print merge statistics.
724 do_print_merge_stats(const char*)
725 { gold_unreachable(); }
727 // Return the required alignment.
730 { return this->addralign_; }
732 // Return the output section.
735 { return this->output_section_; }
737 const Output_section*
738 do_output_section() const
739 { return this->output_section_; }
741 // Return the section index of the output section.
743 do_out_shndx() const;
745 // Set the alignment.
747 set_addralign(uint64_t addralign);
750 // The output section for this section.
751 Output_section* output_section_;
752 // The required alignment.
756 // Some Output_section_data classes build up their data step by step,
757 // rather than all at once. This class provides an interface for
760 class Output_section_data_build : public Output_section_data
763 Output_section_data_build(uint64_t addralign)
764 : Output_section_data(addralign)
767 Output_section_data_build(off_t data_size, uint64_t addralign)
768 : Output_section_data(data_size, addralign, false)
771 // Set the current data size.
773 set_current_data_size(off_t data_size)
774 { this->set_current_data_size_for_child(data_size); }
777 // Set the final data size.
779 set_final_data_size()
780 { this->set_data_size(this->current_data_size_for_child()); }
783 // A simple case of Output_data in which we have constant data to
786 class Output_data_const : public Output_section_data
789 Output_data_const(const std::string& data, uint64_t addralign)
790 : Output_section_data(data.size(), addralign, true), data_(data)
793 Output_data_const(const char* p, off_t len, uint64_t addralign)
794 : Output_section_data(len, addralign, true), data_(p, len)
797 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
798 : Output_section_data(len, addralign, true),
799 data_(reinterpret_cast<const char*>(p), len)
803 // Write the data to the output file.
805 do_write(Output_file*);
807 // Write the data to a buffer.
809 do_write_to_buffer(unsigned char* buffer)
810 { memcpy(buffer, this->data_.data(), this->data_.size()); }
812 // Write to a map file.
814 do_print_to_mapfile(Mapfile* mapfile) const
815 { mapfile->print_output_data(this, _("** fill")); }
821 // Another version of Output_data with constant data, in which the
822 // buffer is allocated by the caller.
824 class Output_data_const_buffer : public Output_section_data
827 Output_data_const_buffer(const unsigned char* p, off_t len,
828 uint64_t addralign, const char* map_name)
829 : Output_section_data(len, addralign, true),
830 p_(p), map_name_(map_name)
834 // Write the data the output file.
836 do_write(Output_file*);
838 // Write the data to a buffer.
840 do_write_to_buffer(unsigned char* buffer)
841 { memcpy(buffer, this->p_, this->data_size()); }
843 // Write to a map file.
845 do_print_to_mapfile(Mapfile* mapfile) const
846 { mapfile->print_output_data(this, _(this->map_name_)); }
849 // The data to output.
850 const unsigned char* p_;
851 // Name to use in a map file. Maps are a rarely used feature, but
852 // the space usage is minor as aren't very many of these objects.
853 const char* map_name_;
856 // A place holder for a fixed amount of data written out via some
859 class Output_data_fixed_space : public Output_section_data
862 Output_data_fixed_space(off_t data_size, uint64_t addralign,
863 const char* map_name)
864 : Output_section_data(data_size, addralign, true),
869 // Write out the data--the actual data must be written out
872 do_write(Output_file*)
875 // Write to a map file.
877 do_print_to_mapfile(Mapfile* mapfile) const
878 { mapfile->print_output_data(this, _(this->map_name_)); }
881 // Name to use in a map file. Maps are a rarely used feature, but
882 // the space usage is minor as aren't very many of these objects.
883 const char* map_name_;
886 // A place holder for variable sized data written out via some other
889 class Output_data_space : public Output_section_data_build
892 explicit Output_data_space(uint64_t addralign, const char* map_name)
893 : Output_section_data_build(addralign),
897 explicit Output_data_space(off_t data_size, uint64_t addralign,
898 const char* map_name)
899 : Output_section_data_build(data_size, addralign),
903 // Set the alignment.
905 set_space_alignment(uint64_t align)
906 { this->set_addralign(align); }
909 // Write out the data--the actual data must be written out
912 do_write(Output_file*)
915 // Write to a map file.
917 do_print_to_mapfile(Mapfile* mapfile) const
918 { mapfile->print_output_data(this, _(this->map_name_)); }
921 // Name to use in a map file. Maps are a rarely used feature, but
922 // the space usage is minor as aren't very many of these objects.
923 const char* map_name_;
926 // Fill fixed space with zeroes. This is just like
927 // Output_data_fixed_space, except that the map name is known.
929 class Output_data_zero_fill : public Output_section_data
932 Output_data_zero_fill(off_t data_size, uint64_t addralign)
933 : Output_section_data(data_size, addralign, true)
937 // There is no data to write out.
939 do_write(Output_file*)
942 // Write to a map file.
944 do_print_to_mapfile(Mapfile* mapfile) const
945 { mapfile->print_output_data(this, "** zero fill"); }
948 // A string table which goes into an output section.
950 class Output_data_strtab : public Output_section_data
953 Output_data_strtab(Stringpool* strtab)
954 : Output_section_data(1), strtab_(strtab)
958 // This is called to update the section size prior to assigning
959 // the address and file offset.
962 { this->set_final_data_size(); }
964 // This is called to set the address and file offset. Here we make
965 // sure that the Stringpool is finalized.
967 set_final_data_size();
969 // Write out the data.
971 do_write(Output_file*);
973 // Write the data to a buffer.
975 do_write_to_buffer(unsigned char* buffer)
976 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
978 // Write to a map file.
980 do_print_to_mapfile(Mapfile* mapfile) const
981 { mapfile->print_output_data(this, _("** string table")); }
987 // This POD class is used to represent a single reloc in the output
988 // file. This could be a private class within Output_data_reloc, but
989 // the templatization is complex enough that I broke it out into a
990 // separate class. The class is templatized on either elfcpp::SHT_REL
991 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
992 // relocation or an ordinary relocation.
994 // A relocation can be against a global symbol, a local symbol, a
995 // local section symbol, an output section, or the undefined symbol at
996 // index 0. We represent the latter by using a NULL global symbol.
998 template<int sh_type, bool dynamic, int size, bool big_endian>
1001 template<bool dynamic, int size, bool big_endian>
1002 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1005 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1006 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1008 static const Address invalid_address = static_cast<Address>(0) - 1;
1010 // An uninitialized entry. We need this because we want to put
1011 // instances of this class into an STL container.
1013 : local_sym_index_(INVALID_CODE)
1016 // We have a bunch of different constructors. They come in pairs
1017 // depending on how the address of the relocation is specified. It
1018 // can either be an offset in an Output_data or an offset in an
1021 // A reloc against a global symbol.
1023 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1024 Address address, bool is_relative, bool is_symbolless,
1025 bool use_plt_offset);
1027 Output_reloc(Symbol* gsym, unsigned int type,
1028 Sized_relobj<size, big_endian>* relobj,
1029 unsigned int shndx, Address address, bool is_relative,
1030 bool is_symbolless, bool use_plt_offset);
1032 // A reloc against a local symbol or local section symbol.
1034 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1035 unsigned int local_sym_index, unsigned int type,
1036 Output_data* od, Address address, bool is_relative,
1037 bool is_symbolless, bool is_section_symbol,
1038 bool use_plt_offset);
1040 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1041 unsigned int local_sym_index, unsigned int type,
1042 unsigned int shndx, Address address, bool is_relative,
1043 bool is_symbolless, bool is_section_symbol,
1044 bool use_plt_offset);
1046 // A reloc against the STT_SECTION symbol of an output section.
1048 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1051 Output_reloc(Output_section* os, unsigned int type,
1052 Sized_relobj<size, big_endian>* relobj,
1053 unsigned int shndx, Address address);
1055 // An absolute relocation with no symbol.
1057 Output_reloc(unsigned int type, Output_data* od, Address address);
1059 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1060 unsigned int shndx, Address address);
1062 // A target specific relocation. The target will be called to get
1063 // the symbol index, passing ARG. The type and offset will be set
1064 // as for other relocation types.
1066 Output_reloc(unsigned int type, void* arg, Output_data* od,
1069 Output_reloc(unsigned int type, void* arg,
1070 Sized_relobj<size, big_endian>* relobj,
1071 unsigned int shndx, Address address);
1073 // Return the reloc type.
1076 { return this->type_; }
1078 // Return whether this is a RELATIVE relocation.
1081 { return this->is_relative_; }
1083 // Return whether this is a relocation which should not use
1084 // a symbol, but which obtains its addend from a symbol.
1086 is_symbolless() const
1087 { return this->is_symbolless_; }
1089 // Return whether this is against a local section symbol.
1091 is_local_section_symbol() const
1093 return (this->local_sym_index_ != GSYM_CODE
1094 && this->local_sym_index_ != SECTION_CODE
1095 && this->local_sym_index_ != INVALID_CODE
1096 && this->local_sym_index_ != TARGET_CODE
1097 && this->is_section_symbol_);
1100 // Return whether this is a target specific relocation.
1102 is_target_specific() const
1103 { return this->local_sym_index_ == TARGET_CODE; }
1105 // Return the argument to pass to the target for a target specific
1110 gold_assert(this->local_sym_index_ == TARGET_CODE);
1111 return this->u1_.arg;
1114 // For a local section symbol, return the offset of the input
1115 // section within the output section. ADDEND is the addend being
1116 // applied to the input section.
1118 local_section_offset(Addend addend) const;
1120 // Get the value of the symbol referred to by a Rel relocation when
1121 // we are adding the given ADDEND.
1123 symbol_value(Addend addend) const;
1125 // If this relocation is against an input section, return the
1126 // relocatable object containing the input section.
1127 Sized_relobj<size, big_endian>*
1130 if (this->shndx_ == INVALID_CODE)
1132 return this->u2_.relobj;
1135 // Write the reloc entry to an output view.
1137 write(unsigned char* pov) const;
1139 // Write the offset and info fields to Write_rel.
1140 template<typename Write_rel>
1141 void write_rel(Write_rel*) const;
1143 // This is used when sorting dynamic relocs. Return -1 to sort this
1144 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1146 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1149 // Return whether this reloc should be sorted before the argument
1150 // when sorting dynamic relocs.
1152 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1154 { return this->compare(r2) < 0; }
1157 // Record that we need a dynamic symbol index.
1159 set_needs_dynsym_index();
1161 // Return the symbol index.
1163 get_symbol_index() const;
1165 // Return the output address.
1167 get_address() const;
1169 // Codes for local_sym_index_.
1178 // Invalid uninitialized entry.
1184 // For a local symbol or local section symbol
1185 // (this->local_sym_index_ >= 0), the object. We will never
1186 // generate a relocation against a local symbol in a dynamic
1187 // object; that doesn't make sense. And our callers will always
1188 // be templatized, so we use Sized_relobj here.
1189 Sized_relobj<size, big_endian>* relobj;
1190 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1191 // symbol. If this is NULL, it indicates a relocation against the
1192 // undefined 0 symbol.
1194 // For a relocation against an output section
1195 // (this->local_sym_index_ == SECTION_CODE), the output section.
1197 // For a target specific relocation, an argument to pass to the
1203 // If this->shndx_ is not INVALID CODE, the object which holds the
1204 // input section being used to specify the reloc address.
1205 Sized_relobj<size, big_endian>* relobj;
1206 // If this->shndx_ is INVALID_CODE, the output data being used to
1207 // specify the reloc address. This may be NULL if the reloc
1208 // address is absolute.
1211 // The address offset within the input section or the Output_data.
1213 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1214 // relocation against an output section, or TARGET_CODE for a target
1215 // specific relocation, or INVALID_CODE for an uninitialized value.
1216 // Otherwise, for a local symbol (this->is_section_symbol_ is
1217 // false), the local symbol index. For a local section symbol
1218 // (this->is_section_symbol_ is true), the section index in the
1220 unsigned int local_sym_index_;
1221 // The reloc type--a processor specific code.
1222 unsigned int type_ : 28;
1223 // True if the relocation is a RELATIVE relocation.
1224 bool is_relative_ : 1;
1225 // True if the relocation is one which should not use
1226 // a symbol, but which obtains its addend from a symbol.
1227 bool is_symbolless_ : 1;
1228 // True if the relocation is against a section symbol.
1229 bool is_section_symbol_ : 1;
1230 // True if the addend should be the PLT offset.
1231 // (Used only for RELA, but stored here for space.)
1232 bool use_plt_offset_ : 1;
1233 // If the reloc address is an input section in an object, the
1234 // section index. This is INVALID_CODE if the reloc address is
1235 // specified in some other way.
1236 unsigned int shndx_;
1239 // The SHT_RELA version of Output_reloc<>. This is just derived from
1240 // the SHT_REL version of Output_reloc, but it adds an addend.
1242 template<bool dynamic, int size, bool big_endian>
1243 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1246 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1247 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1249 // An uninitialized entry.
1254 // A reloc against a global symbol.
1256 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1257 Address address, Addend addend, bool is_relative,
1258 bool is_symbolless, bool use_plt_offset)
1259 : rel_(gsym, type, od, address, is_relative, is_symbolless,
1264 Output_reloc(Symbol* gsym, unsigned int type,
1265 Sized_relobj<size, big_endian>* relobj,
1266 unsigned int shndx, Address address, Addend addend,
1267 bool is_relative, bool is_symbolless, bool use_plt_offset)
1268 : rel_(gsym, type, relobj, shndx, address, is_relative,
1269 is_symbolless, use_plt_offset), addend_(addend)
1272 // A reloc against a local symbol.
1274 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1275 unsigned int local_sym_index, unsigned int type,
1276 Output_data* od, Address address,
1277 Addend addend, bool is_relative,
1278 bool is_symbolless, bool is_section_symbol,
1279 bool use_plt_offset)
1280 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1281 is_symbolless, is_section_symbol, use_plt_offset),
1285 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1286 unsigned int local_sym_index, unsigned int type,
1287 unsigned int shndx, Address address,
1288 Addend addend, bool is_relative,
1289 bool is_symbolless, bool is_section_symbol,
1290 bool use_plt_offset)
1291 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1292 is_symbolless, is_section_symbol, use_plt_offset),
1296 // A reloc against the STT_SECTION symbol of an output section.
1298 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1299 Address address, Addend addend)
1300 : rel_(os, type, od, address), addend_(addend)
1303 Output_reloc(Output_section* os, unsigned int type,
1304 Sized_relobj<size, big_endian>* relobj,
1305 unsigned int shndx, Address address, Addend addend)
1306 : rel_(os, type, relobj, shndx, address), addend_(addend)
1309 // An absolute relocation with no symbol.
1311 Output_reloc(unsigned int type, Output_data* od, Address address,
1313 : rel_(type, od, address), addend_(addend)
1316 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1317 unsigned int shndx, Address address, Addend addend)
1318 : rel_(type, relobj, shndx, address), addend_(addend)
1321 // A target specific relocation. The target will be called to get
1322 // the symbol index and the addend, passing ARG. The type and
1323 // offset will be set as for other relocation types.
1325 Output_reloc(unsigned int type, void* arg, Output_data* od,
1326 Address address, Addend addend)
1327 : rel_(type, arg, od, address), addend_(addend)
1330 Output_reloc(unsigned int type, void* arg,
1331 Sized_relobj<size, big_endian>* relobj,
1332 unsigned int shndx, Address address, Addend addend)
1333 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1336 // Return whether this is a RELATIVE relocation.
1339 { return this->rel_.is_relative(); }
1341 // Return whether this is a relocation which should not use
1342 // a symbol, but which obtains its addend from a symbol.
1344 is_symbolless() const
1345 { return this->rel_.is_symbolless(); }
1347 // If this relocation is against an input section, return the
1348 // relocatable object containing the input section.
1349 Sized_relobj<size, big_endian>*
1351 { return this->rel_.get_relobj(); }
1353 // Write the reloc entry to an output view.
1355 write(unsigned char* pov) const;
1357 // Return whether this reloc should be sorted before the argument
1358 // when sorting dynamic relocs.
1360 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1363 int i = this->rel_.compare(r2.rel_);
1369 return this->addend_ < r2.addend_;
1374 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1379 // Output_data_reloc_generic is a non-template base class for
1380 // Output_data_reloc_base. This gives the generic code a way to hold
1381 // a pointer to a reloc section.
1383 class Output_data_reloc_generic : public Output_section_data_build
1386 Output_data_reloc_generic(int size, bool sort_relocs)
1387 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1388 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1391 // Return the number of relative relocs in this section.
1393 relative_reloc_count() const
1394 { return this->relative_reloc_count_; }
1396 // Whether we should sort the relocs.
1399 { return this->sort_relocs_; }
1401 // Add a reloc of type TYPE against the global symbol GSYM. The
1402 // relocation applies to the data at offset ADDRESS within OD.
1404 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1405 uint64_t address, uint64_t addend) = 0;
1407 // Add a reloc of type TYPE against the global symbol GSYM. The
1408 // relocation applies to data at offset ADDRESS within section SHNDX
1409 // of object file RELOBJ. OD is the associated output section.
1411 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1412 Relobj* relobj, unsigned int shndx, uint64_t address,
1413 uint64_t addend) = 0;
1415 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1416 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1419 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1420 unsigned int type, Output_data* od, uint64_t address,
1421 uint64_t addend) = 0;
1423 // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1424 // in RELOBJ. The relocation applies to the data at offset ADDRESS
1425 // within section SHNDX of RELOBJ. OD is the associated output
1428 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1429 unsigned int type, Output_data* od, unsigned int shndx,
1430 uint64_t address, uint64_t addend) = 0;
1432 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1433 // output section OS. The relocation applies to the data at offset
1434 // ADDRESS within OD.
1436 add_output_section_generic(Output_section *os, unsigned int type,
1437 Output_data* od, uint64_t address,
1438 uint64_t addend) = 0;
1440 // Add a reloc of type TYPE against the STT_SECTION symbol of the
1441 // output section OS. The relocation applies to the data at offset
1442 // ADDRESS within section SHNDX of RELOBJ. OD is the associated
1445 add_output_section_generic(Output_section* os, unsigned int type,
1446 Output_data* od, Relobj* relobj,
1447 unsigned int shndx, uint64_t address,
1448 uint64_t addend) = 0;
1451 // Note that we've added another relative reloc.
1453 bump_relative_reloc_count()
1454 { ++this->relative_reloc_count_; }
1457 // The number of relative relocs added to this section. This is to
1458 // support DT_RELCOUNT.
1459 size_t relative_reloc_count_;
1460 // Whether to sort the relocations when writing them out, to make
1461 // the dynamic linker more efficient.
1465 // Output_data_reloc is used to manage a section containing relocs.
1466 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1467 // indicates whether this is a dynamic relocation or a normal
1468 // relocation. Output_data_reloc_base is a base class.
1469 // Output_data_reloc is the real class, which we specialize based on
1472 template<int sh_type, bool dynamic, int size, bool big_endian>
1473 class Output_data_reloc_base : public Output_data_reloc_generic
1476 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1477 typedef typename Output_reloc_type::Address Address;
1478 static const int reloc_size =
1479 Reloc_types<sh_type, size, big_endian>::reloc_size;
1481 // Construct the section.
1482 Output_data_reloc_base(bool sort_relocs)
1483 : Output_data_reloc_generic(size, sort_relocs)
1487 // Write out the data.
1489 do_write(Output_file*);
1491 // Set the entry size and the link.
1493 do_adjust_output_section(Output_section* os);
1495 // Write to a map file.
1497 do_print_to_mapfile(Mapfile* mapfile) const
1499 mapfile->print_output_data(this,
1501 ? _("** dynamic relocs")
1505 // Add a relocation entry.
1507 add(Output_data* od, const Output_reloc_type& reloc)
1509 this->relocs_.push_back(reloc);
1510 this->set_current_data_size(this->relocs_.size() * reloc_size);
1512 od->add_dynamic_reloc();
1513 if (reloc.is_relative())
1514 this->bump_relative_reloc_count();
1515 Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1517 relobj->add_dyn_reloc(this->relocs_.size() - 1);
1521 typedef std::vector<Output_reloc_type> Relocs;
1523 // The class used to sort the relocations.
1524 struct Sort_relocs_comparison
1527 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1528 { return r1.sort_before(r2); }
1531 // The relocations in this section.
1535 // The class which callers actually create.
1537 template<int sh_type, bool dynamic, int size, bool big_endian>
1538 class Output_data_reloc;
1540 // The SHT_REL version of Output_data_reloc.
1542 template<bool dynamic, int size, bool big_endian>
1543 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1544 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1547 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1551 typedef typename Base::Output_reloc_type Output_reloc_type;
1552 typedef typename Output_reloc_type::Address Address;
1554 Output_data_reloc(bool sr)
1555 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1558 // Add a reloc against a global symbol.
1561 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1562 { this->add(od, Output_reloc_type(gsym, type, od, address, false, false, false)); }
1565 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1566 Sized_relobj<size, big_endian>* relobj,
1567 unsigned int shndx, Address address)
1568 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1569 false, false, false)); }
1572 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1573 uint64_t address, uint64_t addend)
1575 gold_assert(addend == 0);
1576 this->add(od, Output_reloc_type(gsym, type, od,
1577 convert_types<Address, uint64_t>(address),
1578 false, false, false));
1582 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1583 Relobj* relobj, unsigned int shndx, uint64_t address,
1586 gold_assert(addend == 0);
1587 Sized_relobj<size, big_endian>* sized_relobj =
1588 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1589 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1590 convert_types<Address, uint64_t>(address),
1591 false, false, false));
1594 // Add a RELATIVE reloc against a global symbol. The final relocation
1595 // will not reference the symbol.
1598 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1600 { this->add(od, Output_reloc_type(gsym, type, od, address, true, true,
1604 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1605 Sized_relobj<size, big_endian>* relobj,
1606 unsigned int shndx, Address address)
1608 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1609 true, true, false));
1612 // Add a global relocation which does not use a symbol for the relocation,
1613 // but which gets its addend from a symbol.
1616 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1617 Output_data* od, Address address)
1618 { this->add(od, Output_reloc_type(gsym, type, od, address, false, true,
1622 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
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 false, true, false));
1631 // Add a reloc against a local symbol.
1634 add_local(Sized_relobj<size, big_endian>* relobj,
1635 unsigned int local_sym_index, unsigned int type,
1636 Output_data* od, Address address)
1638 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1639 address, false, false, false, false));
1643 add_local(Sized_relobj<size, big_endian>* relobj,
1644 unsigned int local_sym_index, unsigned int type,
1645 Output_data* od, unsigned int shndx, Address address)
1647 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1648 address, false, false, false, false));
1652 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1653 unsigned int type, Output_data* od, uint64_t address,
1656 gold_assert(addend == 0);
1657 Sized_relobj<size, big_endian>* sized_relobj =
1658 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1659 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1660 convert_types<Address, uint64_t>(address),
1661 false, false, false, false));
1665 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1666 unsigned int type, Output_data* od, unsigned int shndx,
1667 uint64_t address, uint64_t addend)
1669 gold_assert(addend == 0);
1670 Sized_relobj<size, big_endian>* sized_relobj =
1671 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1672 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1673 convert_types<Address, uint64_t>(address),
1674 false, false, false, false));
1677 // Add a RELATIVE reloc against a local symbol.
1680 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1681 unsigned int local_sym_index, unsigned int type,
1682 Output_data* od, Address address)
1684 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1685 address, true, true, false, false));
1689 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1690 unsigned int local_sym_index, unsigned int type,
1691 Output_data* od, unsigned int shndx, Address address)
1693 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1694 address, true, true, false, false));
1697 // Add a local relocation which does not use a symbol for the relocation,
1698 // but which gets its addend from a symbol.
1701 add_symbolless_local_addend(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, false, true, false, false));
1710 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1711 unsigned int local_sym_index, unsigned int type,
1712 Output_data* od, unsigned int shndx,
1715 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1716 address, false, true, false, false));
1719 // Add a reloc against a local section symbol. This will be
1720 // converted into a reloc against the STT_SECTION symbol of the
1724 add_local_section(Sized_relobj<size, big_endian>* relobj,
1725 unsigned int input_shndx, unsigned int type,
1726 Output_data* od, Address address)
1728 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1729 address, false, false, true, false));
1733 add_local_section(Sized_relobj<size, big_endian>* relobj,
1734 unsigned int input_shndx, unsigned int type,
1735 Output_data* od, unsigned int shndx, Address address)
1737 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1738 address, false, false, true, false));
1741 // A reloc against the STT_SECTION symbol of an output section.
1742 // OS is the Output_section that the relocation refers to; OD is
1743 // the Output_data object being relocated.
1746 add_output_section(Output_section* os, unsigned int type,
1747 Output_data* od, Address address)
1748 { this->add(od, Output_reloc_type(os, type, od, address)); }
1751 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1752 Sized_relobj<size, big_endian>* relobj,
1753 unsigned int shndx, Address address)
1754 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1757 add_output_section_generic(Output_section* os, unsigned int type,
1758 Output_data* od, uint64_t address,
1761 gold_assert(addend == 0);
1762 this->add(od, Output_reloc_type(os, type, od,
1763 convert_types<Address, uint64_t>(address)));
1767 add_output_section_generic(Output_section* os, unsigned int type,
1768 Output_data* od, Relobj* relobj,
1769 unsigned int shndx, uint64_t address,
1772 gold_assert(addend == 0);
1773 Sized_relobj<size, big_endian>* sized_relobj =
1774 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1775 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
1776 convert_types<Address, uint64_t>(address)));
1779 // Add an absolute relocation.
1782 add_absolute(unsigned int type, Output_data* od, Address address)
1783 { this->add(od, Output_reloc_type(type, od, address)); }
1786 add_absolute(unsigned int type, Output_data* od,
1787 Sized_relobj<size, big_endian>* relobj,
1788 unsigned int shndx, Address address)
1789 { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1791 // Add a target specific relocation. A target which calls this must
1792 // define the reloc_symbol_index and reloc_addend virtual functions.
1795 add_target_specific(unsigned int type, void* arg, Output_data* od,
1797 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1800 add_target_specific(unsigned int type, void* arg, Output_data* od,
1801 Sized_relobj<size, big_endian>* relobj,
1802 unsigned int shndx, Address address)
1803 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1806 // The SHT_RELA version of Output_data_reloc.
1808 template<bool dynamic, int size, bool big_endian>
1809 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1810 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1813 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1817 typedef typename Base::Output_reloc_type Output_reloc_type;
1818 typedef typename Output_reloc_type::Address Address;
1819 typedef typename Output_reloc_type::Addend Addend;
1821 Output_data_reloc(bool sr)
1822 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1825 // Add a reloc against a global symbol.
1828 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1829 Address address, Addend addend)
1830 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1831 false, false, false)); }
1834 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1835 Sized_relobj<size, big_endian>* relobj,
1836 unsigned int shndx, Address address,
1838 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1839 addend, false, false, false)); }
1842 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1843 uint64_t address, uint64_t addend)
1845 this->add(od, Output_reloc_type(gsym, type, od,
1846 convert_types<Address, uint64_t>(address),
1847 convert_types<Addend, uint64_t>(addend),
1848 false, false, false));
1852 add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1853 Relobj* relobj, unsigned int shndx, uint64_t address,
1856 Sized_relobj<size, big_endian>* sized_relobj =
1857 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1858 this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1859 convert_types<Address, uint64_t>(address),
1860 convert_types<Addend, uint64_t>(addend),
1861 false, false, false));
1864 // Add a RELATIVE reloc against a global symbol. The final output
1865 // relocation will not reference the symbol, but we must keep the symbol
1866 // information long enough to set the addend of the relocation correctly
1867 // when it is written.
1870 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1871 Address address, Addend addend, bool use_plt_offset)
1872 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1873 true, use_plt_offset)); }
1876 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1877 Sized_relobj<size, big_endian>* relobj,
1878 unsigned int shndx, Address address, Addend addend,
1879 bool use_plt_offset)
1880 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1881 addend, true, true, use_plt_offset)); }
1883 // Add a global relocation which does not use a symbol for the relocation,
1884 // but which gets its addend from a symbol.
1887 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1888 Address address, Addend addend)
1889 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1890 false, true, false)); }
1893 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1895 Sized_relobj<size, big_endian>* relobj,
1896 unsigned int shndx, Address address, Addend addend)
1897 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1898 addend, false, true, false)); }
1900 // Add a reloc against a local symbol.
1903 add_local(Sized_relobj<size, big_endian>* relobj,
1904 unsigned int local_sym_index, unsigned int type,
1905 Output_data* od, Address address, Addend addend)
1907 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1908 addend, false, false, false, false));
1912 add_local(Sized_relobj<size, big_endian>* relobj,
1913 unsigned int local_sym_index, unsigned int type,
1914 Output_data* od, unsigned int shndx, Address address,
1917 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1918 address, addend, false, false, false,
1923 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1924 unsigned int type, Output_data* od, uint64_t address,
1927 Sized_relobj<size, big_endian>* sized_relobj =
1928 static_cast<Sized_relobj<size, big_endian> *>(relobj);
1929 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1930 convert_types<Address, uint64_t>(address),
1931 convert_types<Addend, uint64_t>(addend),
1932 false, false, false, false));
1936 add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1937 unsigned int type, Output_data* od, unsigned int shndx,
1938 uint64_t address, uint64_t addend)
1940 Sized_relobj<size, big_endian>* sized_relobj =
1941 static_cast<Sized_relobj<size, big_endian>*>(relobj);
1942 this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1943 convert_types<Address, uint64_t>(address),
1944 convert_types<Addend, uint64_t>(addend),
1945 false, false, false, false));
1948 // Add a RELATIVE reloc against a local symbol.
1951 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1952 unsigned int local_sym_index, unsigned int type,
1953 Output_data* od, Address address, Addend addend,
1954 bool use_plt_offset)
1956 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1957 addend, true, true, false,
1962 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1963 unsigned int local_sym_index, unsigned int type,
1964 Output_data* od, unsigned int shndx, Address address,
1965 Addend addend, bool use_plt_offset)
1967 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1968 address, addend, true, true, false,
1972 // Add a local relocation which does not use a symbol for the relocation,
1973 // but which gets it's addend from a symbol.
1976 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1977 unsigned int local_sym_index, unsigned int type,
1978 Output_data* od, Address address, Addend addend)
1980 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1981 addend, false, true, false, false));
1985 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1986 unsigned int local_sym_index, unsigned int type,
1987 Output_data* od, unsigned int shndx,
1988 Address address, Addend addend)
1990 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1991 address, addend, false, true, false,
1995 // Add a reloc against a local section symbol. This will be
1996 // converted into a reloc against the STT_SECTION symbol of the
2000 add_local_section(Sized_relobj<size, big_endian>* relobj,
2001 unsigned int input_shndx, unsigned int type,
2002 Output_data* od, Address address, Addend addend)
2004 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
2005 addend, false, false, true, false));
2009 add_local_section(Sized_relobj<size, big_endian>* relobj,
2010 unsigned int input_shndx, unsigned int type,
2011 Output_data* od, unsigned int shndx, Address address,
2014 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
2015 address, addend, false, false, true,
2019 // A reloc against the STT_SECTION symbol of an output section.
2022 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2023 Address address, Addend addend)
2024 { this->add(od, Output_reloc_type(os, type, od, address, addend)); }
2027 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2028 Sized_relobj<size, big_endian>* relobj,
2029 unsigned int shndx, Address address, Addend addend)
2030 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
2034 add_output_section_generic(Output_section* os, unsigned int type,
2035 Output_data* od, uint64_t address,
2038 this->add(od, Output_reloc_type(os, type, od,
2039 convert_types<Address, uint64_t>(address),
2040 convert_types<Addend, uint64_t>(addend)));
2044 add_output_section_generic(Output_section* os, unsigned int type,
2045 Output_data* od, Relobj* relobj,
2046 unsigned int shndx, uint64_t address,
2049 Sized_relobj<size, big_endian>* sized_relobj =
2050 static_cast<Sized_relobj<size, big_endian>*>(relobj);
2051 this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
2052 convert_types<Address, uint64_t>(address),
2053 convert_types<Addend, uint64_t>(addend)));
2056 // Add an absolute relocation.
2059 add_absolute(unsigned int type, Output_data* od, Address address,
2061 { this->add(od, Output_reloc_type(type, od, address, addend)); }
2064 add_absolute(unsigned int type, Output_data* od,
2065 Sized_relobj<size, big_endian>* relobj,
2066 unsigned int shndx, Address address, Addend addend)
2067 { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
2069 // Add a target specific relocation. A target which calls this must
2070 // define the reloc_symbol_index and reloc_addend virtual functions.
2073 add_target_specific(unsigned int type, void* arg, Output_data* od,
2074 Address address, Addend addend)
2075 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
2078 add_target_specific(unsigned int type, void* arg, Output_data* od,
2079 Sized_relobj<size, big_endian>* relobj,
2080 unsigned int shndx, Address address, Addend addend)
2082 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
2087 // Output_relocatable_relocs represents a relocation section in a
2088 // relocatable link. The actual data is written out in the target
2089 // hook relocate_for_relocatable. This just saves space for it.
2091 template<int sh_type, int size, bool big_endian>
2092 class Output_relocatable_relocs : public Output_section_data
2095 Output_relocatable_relocs(Relocatable_relocs* rr)
2096 : Output_section_data(Output_data::default_alignment_for_size(size)),
2101 set_final_data_size();
2103 // Write out the data. There is nothing to do here.
2105 do_write(Output_file*)
2108 // Write to a map file.
2110 do_print_to_mapfile(Mapfile* mapfile) const
2111 { mapfile->print_output_data(this, _("** relocs")); }
2114 // The relocs associated with this input section.
2115 Relocatable_relocs* rr_;
2118 // Handle a GROUP section.
2120 template<int size, bool big_endian>
2121 class Output_data_group : public Output_section_data
2124 // The constructor clears *INPUT_SHNDXES.
2125 Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
2126 section_size_type entry_count,
2127 elfcpp::Elf_Word flags,
2128 std::vector<unsigned int>* input_shndxes);
2131 do_write(Output_file*);
2133 // Write to a map file.
2135 do_print_to_mapfile(Mapfile* mapfile) const
2136 { mapfile->print_output_data(this, _("** group")); }
2138 // Set final data size.
2140 set_final_data_size()
2141 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
2144 // The input object.
2145 Sized_relobj_file<size, big_endian>* relobj_;
2146 // The group flag word.
2147 elfcpp::Elf_Word flags_;
2148 // The section indexes of the input sections in this group.
2149 std::vector<unsigned int> input_shndxes_;
2152 // Output_data_got is used to manage a GOT. Each entry in the GOT is
2153 // for one symbol--either a global symbol or a local symbol in an
2154 // object. The target specific code adds entries to the GOT as
2155 // needed. The GOT_SIZE template parameter is the size in bits of a
2156 // GOT entry, typically 32 or 64.
2158 class Output_data_got_base : public Output_section_data_build
2161 Output_data_got_base(uint64_t align)
2162 : Output_section_data_build(align)
2165 Output_data_got_base(off_t data_size, uint64_t align)
2166 : Output_section_data_build(data_size, align)
2169 // Reserve the slot at index I in the GOT.
2171 reserve_slot(unsigned int i)
2172 { this->do_reserve_slot(i); }
2175 // Reserve the slot at index I in the GOT.
2177 do_reserve_slot(unsigned int i) = 0;
2180 template<int got_size, bool big_endian>
2181 class Output_data_got : public Output_data_got_base
2184 typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
2187 : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
2188 entries_(), free_list_()
2191 Output_data_got(off_t data_size)
2192 : Output_data_got_base(data_size,
2193 Output_data::default_alignment_for_size(got_size)),
2194 entries_(), free_list_()
2196 // For an incremental update, we have an existing GOT section.
2197 // Initialize the list of entries and the free list.
2198 this->entries_.resize(data_size / (got_size / 8));
2199 this->free_list_.init(data_size, false);
2202 // Add an entry for a global symbol to the GOT. Return true if this
2203 // is a new GOT entry, false if the symbol was already in the GOT.
2205 add_global(Symbol* gsym, unsigned int got_type);
2207 // Like add_global, but use the PLT offset of the global symbol if
2210 add_global_plt(Symbol* gsym, unsigned int got_type);
2212 // Add an entry for a global symbol to the GOT, and add a dynamic
2213 // relocation of type R_TYPE for the GOT entry.
2215 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2216 Output_data_reloc_generic* rel_dyn, unsigned int r_type);
2218 // Add a pair of entries for a global symbol to the GOT, and add
2219 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2221 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2222 Output_data_reloc_generic* rel_dyn,
2223 unsigned int r_type_1, unsigned int r_type_2);
2225 // Add an entry for a local symbol to the GOT. This returns true if
2226 // this is a new GOT entry, false if the symbol already has a GOT
2229 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type);
2231 // Like add_local, but use the PLT offset of the local symbol if it
2234 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type);
2236 // Add an entry for a local symbol to the GOT, and add a dynamic
2237 // relocation of type R_TYPE for the GOT entry.
2239 add_local_with_rel(Relobj* object, unsigned int sym_index,
2240 unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2241 unsigned int r_type);
2243 // Add a pair of entries for a local symbol to the GOT, and add
2244 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2246 add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2247 unsigned int shndx, unsigned int got_type,
2248 Output_data_reloc_generic* rel_dyn,
2249 unsigned int r_type_1, unsigned int r_type_2);
2251 // Add a constant to the GOT. This returns the offset of the new
2252 // entry from the start of the GOT.
2254 add_constant(Valtype constant)
2256 unsigned int got_offset = this->add_got_entry(Got_entry(constant));
2260 // Replace GOT entry I with a new constant.
2262 replace_constant(unsigned int i, Valtype constant)
2264 this->replace_got_entry(i, Got_entry(constant));
2267 // Reserve a slot in the GOT for a local symbol.
2269 reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2270 unsigned int got_type);
2272 // Reserve a slot in the GOT for a global symbol.
2274 reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2277 // Write out the GOT table.
2279 do_write(Output_file*);
2281 // Write to a map file.
2283 do_print_to_mapfile(Mapfile* mapfile) const
2284 { mapfile->print_output_data(this, _("** GOT")); }
2286 // Reserve the slot at index I in the GOT.
2288 do_reserve_slot(unsigned int i)
2289 { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2291 // Return the number of words in the GOT.
2293 num_entries () const
2294 { return this->entries_.size(); }
2296 // Return the offset into the GOT of GOT entry I.
2298 got_offset(unsigned int i) const
2299 { return i * (got_size / 8); }
2302 // This POD class holds a single GOT entry.
2306 // Create a zero entry.
2308 : local_sym_index_(RESERVED_CODE), use_plt_offset_(false)
2309 { this->u_.constant = 0; }
2311 // Create a global symbol entry.
2312 Got_entry(Symbol* gsym, bool use_plt_offset)
2313 : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
2314 { this->u_.gsym = gsym; }
2316 // Create a local symbol entry.
2317 Got_entry(Relobj* object, unsigned int local_sym_index,
2318 bool use_plt_offset)
2319 : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
2321 gold_assert(local_sym_index != GSYM_CODE
2322 && local_sym_index != CONSTANT_CODE
2323 && local_sym_index != RESERVED_CODE
2324 && local_sym_index == this->local_sym_index_);
2325 this->u_.object = object;
2328 // Create a constant entry. The constant is a host value--it will
2329 // be swapped, if necessary, when it is written out.
2330 explicit Got_entry(Valtype constant)
2331 : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2332 { this->u_.constant = constant; }
2334 // Write the GOT entry to an output view.
2336 write(unsigned char* pov) const;
2341 GSYM_CODE = 0x7fffffff,
2342 CONSTANT_CODE = 0x7ffffffe,
2343 RESERVED_CODE = 0x7ffffffd
2348 // For a local symbol, the object.
2350 // For a global symbol, the symbol.
2352 // For a constant, the constant.
2355 // For a local symbol, the local symbol index. This is GSYM_CODE
2356 // for a global symbol, or CONSTANT_CODE for a constant.
2357 unsigned int local_sym_index_ : 31;
2358 // Whether to use the PLT offset of the symbol if it has one.
2359 bool use_plt_offset_ : 1;
2362 typedef std::vector<Got_entry> Got_entries;
2364 // Create a new GOT entry and return its offset.
2366 add_got_entry(Got_entry got_entry);
2368 // Create a pair of new GOT entries and return the offset of the first.
2370 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2372 // Replace GOT entry I with a new value.
2374 replace_got_entry(unsigned int i, Got_entry got_entry);
2376 // Return the offset into the GOT of the last entry added.
2378 last_got_offset() const
2379 { return this->got_offset(this->num_entries() - 1); }
2381 // Set the size of the section.
2384 { this->set_current_data_size(this->got_offset(this->num_entries())); }
2386 // The list of GOT entries.
2387 Got_entries entries_;
2389 // List of available regions within the section, for incremental
2391 Free_list free_list_;
2394 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2397 class Output_data_dynamic : public Output_section_data
2400 Output_data_dynamic(Stringpool* pool)
2401 : Output_section_data(Output_data::default_alignment()),
2402 entries_(), pool_(pool)
2405 // Add a new dynamic entry with a fixed numeric value.
2407 add_constant(elfcpp::DT tag, unsigned int val)
2408 { this->add_entry(Dynamic_entry(tag, val)); }
2410 // Add a new dynamic entry with the address of output data.
2412 add_section_address(elfcpp::DT tag, const Output_data* od)
2413 { this->add_entry(Dynamic_entry(tag, od, false)); }
2415 // Add a new dynamic entry with the address of output data
2416 // plus a constant offset.
2418 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2419 unsigned int offset)
2420 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2422 // Add a new dynamic entry with the size of output data.
2424 add_section_size(elfcpp::DT tag, const Output_data* od)
2425 { this->add_entry(Dynamic_entry(tag, od, true)); }
2427 // Add a new dynamic entry with the total size of two output datas.
2429 add_section_size(elfcpp::DT tag, const Output_data* od,
2430 const Output_data* od2)
2431 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2433 // Add a new dynamic entry with the address of a symbol.
2435 add_symbol(elfcpp::DT tag, const Symbol* sym)
2436 { this->add_entry(Dynamic_entry(tag, sym)); }
2438 // Add a new dynamic entry with a string.
2440 add_string(elfcpp::DT tag, const char* str)
2441 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2444 add_string(elfcpp::DT tag, const std::string& str)
2445 { this->add_string(tag, str.c_str()); }
2448 // Adjust the output section to set the entry size.
2450 do_adjust_output_section(Output_section*);
2452 // Set the final data size.
2454 set_final_data_size();
2456 // Write out the dynamic entries.
2458 do_write(Output_file*);
2460 // Write to a map file.
2462 do_print_to_mapfile(Mapfile* mapfile) const
2463 { mapfile->print_output_data(this, _("** dynamic")); }
2466 // This POD class holds a single dynamic entry.
2470 // Create an entry with a fixed numeric value.
2471 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2472 : tag_(tag), offset_(DYNAMIC_NUMBER)
2473 { this->u_.val = val; }
2475 // Create an entry with the size or address of a section.
2476 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2478 offset_(section_size
2479 ? DYNAMIC_SECTION_SIZE
2480 : DYNAMIC_SECTION_ADDRESS)
2486 // Create an entry with the size of two sections.
2487 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2489 offset_(DYNAMIC_SECTION_SIZE)
2495 // Create an entry with the address of a section plus a constant offset.
2496 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2499 { this->u_.od = od; }
2501 // Create an entry with the address of a symbol.
2502 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2503 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2504 { this->u_.sym = sym; }
2506 // Create an entry with a string.
2507 Dynamic_entry(elfcpp::DT tag, const char* str)
2508 : tag_(tag), offset_(DYNAMIC_STRING)
2509 { this->u_.str = str; }
2511 // Return the tag of this entry.
2514 { return this->tag_; }
2516 // Write the dynamic entry to an output view.
2517 template<int size, bool big_endian>
2519 write(unsigned char* pov, const Stringpool*) const;
2522 // Classification is encoded in the OFFSET field.
2526 DYNAMIC_SECTION_ADDRESS = 0,
2528 DYNAMIC_NUMBER = -1U,
2530 DYNAMIC_SECTION_SIZE = -2U,
2532 DYNAMIC_SYMBOL = -3U,
2534 DYNAMIC_STRING = -4U
2535 // Any other value indicates a section address plus OFFSET.
2540 // For DYNAMIC_NUMBER.
2542 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2543 const Output_data* od;
2544 // For DYNAMIC_SYMBOL.
2546 // For DYNAMIC_STRING.
2549 // For DYNAMIC_SYMBOL with two sections.
2550 const Output_data* od2;
2553 // The type of entry (Classification) or offset within a section.
2554 unsigned int offset_;
2557 // Add an entry to the list.
2559 add_entry(const Dynamic_entry& entry)
2560 { this->entries_.push_back(entry); }
2562 // Sized version of write function.
2563 template<int size, bool big_endian>
2565 sized_write(Output_file* of);
2567 // The type of the list of entries.
2568 typedef std::vector<Dynamic_entry> Dynamic_entries;
2571 Dynamic_entries entries_;
2572 // The pool used for strings.
2576 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2577 // which may be required if the object file has more than
2578 // SHN_LORESERVE sections.
2580 class Output_symtab_xindex : public Output_section_data
2583 Output_symtab_xindex(size_t symcount)
2584 : Output_section_data(symcount * 4, 4, true),
2588 // Add an entry: symbol number SYMNDX has section SHNDX.
2590 add(unsigned int symndx, unsigned int shndx)
2591 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2595 do_write(Output_file*);
2597 // Write to a map file.
2599 do_print_to_mapfile(Mapfile* mapfile) const
2600 { mapfile->print_output_data(this, _("** symtab xindex")); }
2603 template<bool big_endian>
2605 endian_do_write(unsigned char*);
2607 // It is likely that most symbols will not require entries. Rather
2608 // than keep a vector for all symbols, we keep pairs of symbol index
2609 // and section index.
2610 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2612 // The entries we need.
2613 Xindex_entries entries_;
2616 // A relaxed input section.
2617 class Output_relaxed_input_section : public Output_section_data_build
2620 // We would like to call relobj->section_addralign(shndx) to get the
2621 // alignment but we do not want the constructor to fail. So callers
2622 // are repsonsible for ensuring that.
2623 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2625 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2628 // Return the Relobj of this relaxed input section.
2631 { return this->relobj_; }
2633 // Return the section index of this relaxed input section.
2636 { return this->shndx_; }
2640 unsigned int shndx_;
2643 // This class describes properties of merge data sections. It is used
2644 // as a key type for maps.
2645 class Merge_section_properties
2648 Merge_section_properties(bool is_string, uint64_t entsize,
2650 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2653 // Whether this equals to another Merge_section_properties MSP.
2655 eq(const Merge_section_properties& msp) const
2657 return ((this->is_string_ == msp.is_string_)
2658 && (this->entsize_ == msp.entsize_)
2659 && (this->addralign_ == msp.addralign_));
2662 // Compute a hash value for this using 64-bit FNV-1a hash.
2666 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2667 uint64_t prime = 1099511628211ULL;
2668 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2669 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2670 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2674 // Functors for associative containers.
2678 operator()(const Merge_section_properties& msp1,
2679 const Merge_section_properties& msp2) const
2680 { return msp1.eq(msp2); }
2686 operator()(const Merge_section_properties& msp) const
2687 { return msp.hash_value(); }
2691 // Whether this merge data section is for strings.
2693 // Entsize of this merge data section.
2695 // Address alignment.
2696 uint64_t addralign_;
2699 // This class is used to speed up look up of special input sections in an
2702 class Output_section_lookup_maps
2705 Output_section_lookup_maps()
2706 : is_valid_(true), merge_sections_by_properties_(),
2707 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2710 // Whether the maps are valid.
2713 { return this->is_valid_; }
2715 // Invalidate the maps.
2718 { this->is_valid_ = false; }
2724 this->merge_sections_by_properties_.clear();
2725 this->merge_sections_by_id_.clear();
2726 this->relaxed_input_sections_by_id_.clear();
2727 // A cleared map is valid.
2728 this->is_valid_ = true;
2731 // Find a merge section by merge section properties. Return NULL if none
2734 find_merge_section(const Merge_section_properties& msp) const
2736 gold_assert(this->is_valid_);
2737 Merge_sections_by_properties::const_iterator p =
2738 this->merge_sections_by_properties_.find(msp);
2739 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2742 // Find a merge section by section ID of a merge input section. Return NULL
2743 // if none is found.
2745 find_merge_section(const Object* object, unsigned int shndx) const
2747 gold_assert(this->is_valid_);
2748 Merge_sections_by_id::const_iterator p =
2749 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2750 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2753 // Add a merge section pointed by POMB with properties MSP.
2755 add_merge_section(const Merge_section_properties& msp,
2756 Output_merge_base* pomb)
2758 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2759 std::pair<Merge_sections_by_properties::iterator, bool> result =
2760 this->merge_sections_by_properties_.insert(value);
2761 gold_assert(result.second);
2764 // Add a mapping from a merged input section in OBJECT with index SHNDX
2765 // to a merge output section pointed by POMB.
2767 add_merge_input_section(const Object* object, unsigned int shndx,
2768 Output_merge_base* pomb)
2770 Const_section_id csid(object, shndx);
2771 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2772 std::pair<Merge_sections_by_id::iterator, bool> result =
2773 this->merge_sections_by_id_.insert(value);
2774 gold_assert(result.second);
2777 // Find a relaxed input section of OBJECT with index SHNDX.
2778 Output_relaxed_input_section*
2779 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2781 gold_assert(this->is_valid_);
2782 Relaxed_input_sections_by_id::const_iterator p =
2783 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2784 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2787 // Add a relaxed input section pointed by POMB and whose original input
2788 // section is in OBJECT with index SHNDX.
2790 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2791 Output_relaxed_input_section* poris)
2793 Const_section_id csid(relobj, shndx);
2794 std::pair<Const_section_id, Output_relaxed_input_section*>
2796 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2797 this->relaxed_input_sections_by_id_.insert(value);
2798 gold_assert(result.second);
2802 typedef Unordered_map<Const_section_id, Output_merge_base*,
2803 Const_section_id_hash>
2804 Merge_sections_by_id;
2806 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2807 Merge_section_properties::hash,
2808 Merge_section_properties::equal_to>
2809 Merge_sections_by_properties;
2811 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2812 Const_section_id_hash>
2813 Relaxed_input_sections_by_id;
2815 // Whether this is valid
2817 // Merge sections by merge section properties.
2818 Merge_sections_by_properties merge_sections_by_properties_;
2819 // Merge sections by section IDs.
2820 Merge_sections_by_id merge_sections_by_id_;
2821 // Relaxed sections by section IDs.
2822 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2825 // This abstract base class defines the interface for the
2826 // types of methods used to fill free space left in an output
2827 // section during an incremental link. These methods are used
2828 // to insert dummy compilation units into debug info so that
2829 // debug info consumers can scan the debug info serially.
2835 : is_big_endian_(parameters->target().is_big_endian())
2842 // Return the smallest size chunk of free space that can be
2843 // filled with a dummy compilation unit.
2845 minimum_hole_size() const
2846 { return this->do_minimum_hole_size(); }
2848 // Write a fill pattern of length LEN at offset OFF in the file.
2850 write(Output_file* of, off_t off, size_t len) const
2851 { this->do_write(of, off, len); }
2855 do_minimum_hole_size() const = 0;
2858 do_write(Output_file* of, off_t off, size_t len) const = 0;
2861 is_big_endian() const
2862 { return this->is_big_endian_; }
2865 bool is_big_endian_;
2868 // Fill method that introduces a dummy compilation unit in
2869 // a .debug_info or .debug_types section.
2871 class Output_fill_debug_info : public Output_fill
2874 Output_fill_debug_info(bool is_debug_types)
2875 : is_debug_types_(is_debug_types)
2880 do_minimum_hole_size() const;
2883 do_write(Output_file* of, off_t off, size_t len) const;
2886 // Version of the header.
2887 static const int version = 4;
2888 // True if this is a .debug_types section.
2889 bool is_debug_types_;
2892 // Fill method that introduces a dummy compilation unit in
2893 // a .debug_line section.
2895 class Output_fill_debug_line : public Output_fill
2898 Output_fill_debug_line()
2903 do_minimum_hole_size() const;
2906 do_write(Output_file* of, off_t off, size_t len) const;
2909 // Version of the header. We write a DWARF-3 header because it's smaller
2910 // and many tools have not yet been updated to understand the DWARF-4 header.
2911 static const int version = 3;
2912 // Length of the portion of the header that follows the header_length
2913 // field. This includes the following fields:
2914 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2915 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2916 // The standard_opcode_lengths array is 12 bytes long, and the
2917 // include_directories and filenames fields each contain only a single
2919 static const size_t header_length = 19;
2922 // An output section. We don't expect to have too many output
2923 // sections, so we don't bother to do a template on the size.
2925 class Output_section : public Output_data
2928 // Create an output section, giving the name, type, and flags.
2929 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2930 virtual ~Output_section();
2932 // Add a new input section SHNDX, named NAME, with header SHDR, from
2933 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2934 // which applies to this section, or 0 if none, or -1 if more than
2935 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2936 // in a linker script; in that case we need to keep track of input
2937 // sections associated with an output section. Return the offset
2938 // within the output section.
2939 template<int size, bool big_endian>
2941 add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
2942 unsigned int shndx, const char* name,
2943 const elfcpp::Shdr<size, big_endian>& shdr,
2944 unsigned int reloc_shndx, bool have_sections_script);
2946 // Add generated data POSD to this output section.
2948 add_output_section_data(Output_section_data* posd);
2950 // Add a relaxed input section PORIS called NAME to this output section
2953 add_relaxed_input_section(Layout* layout,
2954 Output_relaxed_input_section* poris,
2955 const std::string& name);
2957 // Return the section name.
2960 { return this->name_; }
2962 // Return the section type.
2965 { return this->type_; }
2967 // Return the section flags.
2970 { return this->flags_; }
2972 typedef std::map<Section_id, unsigned int> Section_layout_order;
2975 update_section_layout(const Section_layout_order* order_map);
2977 // Update the output section flags based on input section flags.
2979 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2981 // Return the entsize field.
2984 { return this->entsize_; }
2986 // Set the entsize field.
2988 set_entsize(uint64_t v);
2990 // Set the load address.
2992 set_load_address(uint64_t load_address)
2994 this->load_address_ = load_address;
2995 this->has_load_address_ = true;
2998 // Set the link field to the output section index of a section.
3000 set_link_section(const Output_data* od)
3002 gold_assert(this->link_ == 0
3003 && !this->should_link_to_symtab_
3004 && !this->should_link_to_dynsym_);
3005 this->link_section_ = od;
3008 // Set the link field to a constant.
3010 set_link(unsigned int v)
3012 gold_assert(this->link_section_ == NULL
3013 && !this->should_link_to_symtab_
3014 && !this->should_link_to_dynsym_);
3018 // Record that this section should link to the normal symbol table.
3020 set_should_link_to_symtab()
3022 gold_assert(this->link_section_ == NULL
3024 && !this->should_link_to_dynsym_);
3025 this->should_link_to_symtab_ = true;
3028 // Record that this section should link to the dynamic symbol table.
3030 set_should_link_to_dynsym()
3032 gold_assert(this->link_section_ == NULL
3034 && !this->should_link_to_symtab_);
3035 this->should_link_to_dynsym_ = true;
3038 // Return the info field.
3042 gold_assert(this->info_section_ == NULL
3043 && this->info_symndx_ == NULL);
3047 // Set the info field to the output section index of a section.
3049 set_info_section(const Output_section* os)
3051 gold_assert((this->info_section_ == NULL
3052 || (this->info_section_ == os
3053 && this->info_uses_section_index_))
3054 && this->info_symndx_ == NULL
3055 && this->info_ == 0);
3056 this->info_section_ = os;
3057 this->info_uses_section_index_= true;
3060 // Set the info field to the symbol table index of a symbol.
3062 set_info_symndx(const Symbol* sym)
3064 gold_assert(this->info_section_ == NULL
3065 && (this->info_symndx_ == NULL
3066 || this->info_symndx_ == sym)
3067 && this->info_ == 0);
3068 this->info_symndx_ = sym;
3071 // Set the info field to the symbol table index of a section symbol.
3073 set_info_section_symndx(const Output_section* os)
3075 gold_assert((this->info_section_ == NULL
3076 || (this->info_section_ == os
3077 && !this->info_uses_section_index_))
3078 && this->info_symndx_ == NULL
3079 && this->info_ == 0);
3080 this->info_section_ = os;
3081 this->info_uses_section_index_ = false;
3084 // Set the info field to a constant.
3086 set_info(unsigned int v)
3088 gold_assert(this->info_section_ == NULL
3089 && this->info_symndx_ == NULL
3090 && (this->info_ == 0
3091 || this->info_ == v));
3095 // Set the addralign field.
3097 set_addralign(uint64_t v)
3098 { this->addralign_ = v; }
3100 // Whether the output section index has been set.
3102 has_out_shndx() const
3103 { return this->out_shndx_ != -1U; }
3105 // Indicate that we need a symtab index.
3107 set_needs_symtab_index()
3108 { this->needs_symtab_index_ = true; }
3110 // Return whether we need a symtab index.
3112 needs_symtab_index() const
3113 { return this->needs_symtab_index_; }
3115 // Get the symtab index.
3117 symtab_index() const
3119 gold_assert(this->symtab_index_ != 0);
3120 return this->symtab_index_;
3123 // Set the symtab index.
3125 set_symtab_index(unsigned int index)
3127 gold_assert(index != 0);
3128 this->symtab_index_ = index;
3131 // Indicate that we need a dynsym index.
3133 set_needs_dynsym_index()
3134 { this->needs_dynsym_index_ = true; }
3136 // Return whether we need a dynsym index.
3138 needs_dynsym_index() const
3139 { return this->needs_dynsym_index_; }
3141 // Get the dynsym index.
3143 dynsym_index() const
3145 gold_assert(this->dynsym_index_ != 0);
3146 return this->dynsym_index_;
3149 // Set the dynsym index.
3151 set_dynsym_index(unsigned int index)
3153 gold_assert(index != 0);
3154 this->dynsym_index_ = index;
3157 // Return whether the input sections sections attachd to this output
3158 // section may require sorting. This is used to handle constructor
3159 // priorities compatibly with GNU ld.
3161 may_sort_attached_input_sections() const
3162 { return this->may_sort_attached_input_sections_; }
3164 // Record that the input sections attached to this output section
3165 // may require sorting.
3167 set_may_sort_attached_input_sections()
3168 { this->may_sort_attached_input_sections_ = true; }
3170 // Returns true if input sections must be sorted according to the
3171 // order in which their name appear in the --section-ordering-file.
3173 input_section_order_specified()
3174 { return this->input_section_order_specified_; }
3176 // Record that input sections must be sorted as some of their names
3177 // match the patterns specified through --section-ordering-file.
3179 set_input_section_order_specified()
3180 { this->input_section_order_specified_ = true; }
3182 // Return whether the input sections attached to this output section
3183 // require sorting. This is used to handle constructor priorities
3184 // compatibly with GNU ld.
3186 must_sort_attached_input_sections() const
3187 { return this->must_sort_attached_input_sections_; }
3189 // Record that the input sections attached to this output section
3192 set_must_sort_attached_input_sections()
3193 { this->must_sort_attached_input_sections_ = true; }
3195 // Get the order in which this section appears in the PT_LOAD output
3197 Output_section_order
3199 { return this->order_; }
3201 // Set the order for this section.
3203 set_order(Output_section_order order)
3204 { this->order_ = order; }
3206 // Return whether this section holds relro data--data which has
3207 // dynamic relocations but which may be marked read-only after the
3208 // dynamic relocations have been completed.
3211 { return this->is_relro_; }
3213 // Record that this section holds relro data.
3216 { this->is_relro_ = true; }
3218 // Record that this section does not hold relro data.
3221 { this->is_relro_ = false; }
3223 // True if this is a small section: a section which holds small
3226 is_small_section() const
3227 { return this->is_small_section_; }
3229 // Record that this is a small section.
3231 set_is_small_section()
3232 { this->is_small_section_ = true; }
3234 // True if this is a large section: a section which holds large
3237 is_large_section() const
3238 { return this->is_large_section_; }
3240 // Record that this is a large section.
3242 set_is_large_section()
3243 { this->is_large_section_ = true; }
3245 // True if this is a large data (not BSS) section.
3247 is_large_data_section()
3248 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3250 // Return whether this section should be written after all the input
3251 // sections are complete.
3253 after_input_sections() const
3254 { return this->after_input_sections_; }
3256 // Record that this section should be written after all the input
3257 // sections are complete.
3259 set_after_input_sections()
3260 { this->after_input_sections_ = true; }
3262 // Return whether this section requires postprocessing after all
3263 // relocations have been applied.
3265 requires_postprocessing() const
3266 { return this->requires_postprocessing_; }
3269 is_unique_segment() const
3270 { return this->is_unique_segment_; }
3273 set_is_unique_segment()
3274 { this->is_unique_segment_ = true; }
3276 uint64_t extra_segment_flags() const
3277 { return this->extra_segment_flags_; }
3280 set_extra_segment_flags(uint64_t flags)
3281 { this->extra_segment_flags_ = flags; }
3283 uint64_t segment_alignment() const
3284 { return this->segment_alignment_; }
3287 set_segment_alignment(uint64_t align)
3288 { this->segment_alignment_ = align; }
3290 // If a section requires postprocessing, return the buffer to use.
3292 postprocessing_buffer() const
3294 gold_assert(this->postprocessing_buffer_ != NULL);
3295 return this->postprocessing_buffer_;
3298 // If a section requires postprocessing, create the buffer to use.
3300 create_postprocessing_buffer();
3302 // If a section requires postprocessing, this is the size of the
3303 // buffer to which relocations should be applied.
3305 postprocessing_buffer_size() const
3306 { return this->current_data_size_for_child(); }
3308 // Modify the section name. This is only permitted for an
3309 // unallocated section, and only before the size has been finalized.
3310 // Otherwise the name will not get into Layout::namepool_.
3312 set_name(const char* newname)
3314 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3315 gold_assert(!this->is_data_size_valid());
3316 this->name_ = newname;
3319 // Return whether the offset OFFSET in the input section SHNDX in
3320 // object OBJECT is being included in the link.
3322 is_input_address_mapped(const Relobj* object, unsigned int shndx,
3323 off_t offset) const;
3325 // Return the offset within the output section of OFFSET relative to
3326 // the start of input section SHNDX in object OBJECT.
3328 output_offset(const Relobj* object, unsigned int shndx,
3329 section_offset_type offset) const;
3331 // Return the output virtual address of OFFSET relative to the start
3332 // of input section SHNDX in object OBJECT.
3334 output_address(const Relobj* object, unsigned int shndx,
3335 off_t offset) const;
3337 // Look for the merged section for input section SHNDX in object
3338 // OBJECT. If found, return true, and set *ADDR to the address of
3339 // the start of the merged section. This is not necessary the
3340 // output offset corresponding to input offset 0 in the section,
3341 // since the section may be mapped arbitrarily.
3343 find_starting_output_address(const Relobj* object, unsigned int shndx,
3344 uint64_t* addr) const;
3346 // Record that this output section was found in the SECTIONS clause
3347 // of a linker script.
3349 set_found_in_sections_clause()
3350 { this->found_in_sections_clause_ = true; }
3352 // Return whether this output section was found in the SECTIONS
3353 // clause of a linker script.
3355 found_in_sections_clause() const
3356 { return this->found_in_sections_clause_; }
3358 // Write the section header into *OPHDR.
3359 template<int size, bool big_endian>
3361 write_header(const Layout*, const Stringpool*,
3362 elfcpp::Shdr_write<size, big_endian>*) const;
3364 // The next few calls are for linker script support.
3366 // In some cases we need to keep a list of the input sections
3367 // associated with this output section. We only need the list if we
3368 // might have to change the offsets of the input section within the
3369 // output section after we add the input section. The ordinary
3370 // input sections will be written out when we process the object
3371 // file, and as such we don't need to track them here. We do need
3372 // to track Output_section_data objects here. We store instances of
3373 // this structure in a std::vector, so it must be a POD. There can
3374 // be many instances of this structure, so we use a union to save
3380 : shndx_(0), p2align_(0)
3382 this->u1_.data_size = 0;
3383 this->u2_.object = NULL;
3386 // For an ordinary input section.
3387 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3390 p2align_(ffsll(static_cast<long long>(addralign))),
3391 section_order_index_(0)
3393 gold_assert(shndx != OUTPUT_SECTION_CODE
3394 && shndx != MERGE_DATA_SECTION_CODE
3395 && shndx != MERGE_STRING_SECTION_CODE
3396 && shndx != RELAXED_INPUT_SECTION_CODE);
3397 this->u1_.data_size = data_size;
3398 this->u2_.object = object;
3401 // For a non-merge output section.
3402 Input_section(Output_section_data* posd)
3403 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3404 section_order_index_(0)
3406 this->u1_.data_size = 0;
3407 this->u2_.posd = posd;
3410 // For a merge section.
3411 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3413 ? MERGE_STRING_SECTION_CODE
3414 : MERGE_DATA_SECTION_CODE),
3416 section_order_index_(0)
3418 this->u1_.entsize = entsize;
3419 this->u2_.posd = posd;
3422 // For a relaxed input section.
3423 Input_section(Output_relaxed_input_section* psection)
3424 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3425 section_order_index_(0)
3427 this->u1_.data_size = 0;
3428 this->u2_.poris = psection;
3432 section_order_index() const
3434 return this->section_order_index_;
3438 set_section_order_index(unsigned int number)
3440 this->section_order_index_ = number;
3443 // The required alignment.
3447 if (this->p2align_ != 0)
3448 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3449 else if (!this->is_input_section())
3450 return this->u2_.posd->addralign();
3455 // Set the required alignment, which must be either 0 or a power of 2.
3456 // For input sections that are sub-classes of Output_section_data, a
3457 // alignment of zero means asking the underlying object for alignment.
3459 set_addralign(uint64_t addralign)
3465 gold_assert((addralign & (addralign - 1)) == 0);
3466 this->p2align_ = ffsll(static_cast<long long>(addralign));
3470 // Return the current required size, without finalization.
3472 current_data_size() const;
3474 // Return the required size.
3478 // Whether this is an input section.
3480 is_input_section() const
3482 return (this->shndx_ != OUTPUT_SECTION_CODE
3483 && this->shndx_ != MERGE_DATA_SECTION_CODE
3484 && this->shndx_ != MERGE_STRING_SECTION_CODE
3485 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3488 // Return whether this is a merge section which matches the
3491 is_merge_section(bool is_string, uint64_t entsize,
3492 uint64_t addralign) const
3494 return (this->shndx_ == (is_string
3495 ? MERGE_STRING_SECTION_CODE
3496 : MERGE_DATA_SECTION_CODE)
3497 && this->u1_.entsize == entsize
3498 && this->addralign() == addralign);
3501 // Return whether this is a merge section for some input section.
3503 is_merge_section() const
3505 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3506 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3509 // Return whether this is a relaxed input section.
3511 is_relaxed_input_section() const
3512 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3514 // Return whether this is a generic Output_section_data.
3516 is_output_section_data() const
3518 return this->shndx_ == OUTPUT_SECTION_CODE;
3521 // Return the object for an input section.
3525 // Return the input section index for an input section.
3529 // For non-input-sections, return the associated Output_section_data
3531 Output_section_data*
3532 output_section_data() const
3534 gold_assert(!this->is_input_section());
3535 return this->u2_.posd;
3538 // For a merge section, return the Output_merge_base pointer.
3540 output_merge_base() const
3542 gold_assert(this->is_merge_section());
3543 return this->u2_.pomb;
3546 // Return the Output_relaxed_input_section object.
3547 Output_relaxed_input_section*
3548 relaxed_input_section() const
3550 gold_assert(this->is_relaxed_input_section());
3551 return this->u2_.poris;
3554 // Set the output section.
3556 set_output_section(Output_section* os)
3558 gold_assert(!this->is_input_section());
3559 Output_section_data* posd =
3560 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3561 posd->set_output_section(os);
3564 // Set the address and file offset. This is called during
3565 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3566 // the enclosing section.
3568 set_address_and_file_offset(uint64_t address, off_t file_offset,
3569 off_t section_file_offset);
3571 // Reset the address and file offset.
3573 reset_address_and_file_offset();
3575 // Finalize the data size.
3577 finalize_data_size();
3579 // Add an input section, for SHF_MERGE sections.
3581 add_input_section(Relobj* object, unsigned int shndx)
3583 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3584 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3585 return this->u2_.posd->add_input_section(object, shndx);
3588 // Given an input OBJECT, an input section index SHNDX within that
3589 // object, and an OFFSET relative to the start of that input
3590 // section, return whether or not the output offset is known. If
3591 // this function returns true, it sets *POUTPUT to the offset in
3592 // the output section, relative to the start of the input section
3593 // in the output section. *POUTPUT may be different from OFFSET
3594 // for a merged section.
3596 output_offset(const Relobj* object, unsigned int shndx,
3597 section_offset_type offset,
3598 section_offset_type* poutput) const;
3600 // Return whether this is the merge section for the input section
3603 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3605 // Write out the data. This does nothing for an input section.
3607 write(Output_file*);
3609 // Write the data to a buffer. This does nothing for an input
3612 write_to_buffer(unsigned char*);
3614 // Print to a map file.
3616 print_to_mapfile(Mapfile*) const;
3618 // Print statistics about merge sections to stderr.
3620 print_merge_stats(const char* section_name)
3622 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3623 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3624 this->u2_.posd->print_merge_stats(section_name);
3628 // Code values which appear in shndx_. If the value is not one of
3629 // these codes, it is the input section index in the object file.
3632 // An Output_section_data.
3633 OUTPUT_SECTION_CODE = -1U,
3634 // An Output_section_data for an SHF_MERGE section with
3635 // SHF_STRINGS not set.
3636 MERGE_DATA_SECTION_CODE = -2U,
3637 // An Output_section_data for an SHF_MERGE section with
3639 MERGE_STRING_SECTION_CODE = -3U,
3640 // An Output_section_data for a relaxed input section.
3641 RELAXED_INPUT_SECTION_CODE = -4U
3644 // For an ordinary input section, this is the section index in the
3645 // input file. For an Output_section_data, this is
3646 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3647 // MERGE_STRING_SECTION_CODE.
3648 unsigned int shndx_;
3649 // The required alignment, stored as a power of 2.
3650 unsigned int p2align_;
3653 // For an ordinary input section, the section size.
3655 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3656 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3662 // For an ordinary input section, the object which holds the
3665 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3666 // MERGE_STRING_SECTION_CODE, the data.
3667 Output_section_data* posd;
3668 Output_merge_base* pomb;
3669 // For RELAXED_INPUT_SECTION_CODE, the data.
3670 Output_relaxed_input_section* poris;
3672 // The line number of the pattern it matches in the --section-ordering-file
3673 // file. It is 0 if does not match any pattern.
3674 unsigned int section_order_index_;
3677 // Store the list of input sections for this Output_section into the
3678 // list passed in. This removes the input sections, leaving only
3679 // any Output_section_data elements. This returns the size of those
3680 // Output_section_data elements. ADDRESS is the address of this
3681 // output section. FILL is the fill value to use, in case there are
3682 // any spaces between the remaining Output_section_data elements.
3684 get_input_sections(uint64_t address, const std::string& fill,
3685 std::list<Input_section>*);
3687 // Add a script input section. A script input section can either be
3688 // a plain input section or a sub-class of Output_section_data.
3690 add_script_input_section(const Input_section& input_section);
3692 // Set the current size of the output section.
3694 set_current_data_size(off_t size)
3695 { this->set_current_data_size_for_child(size); }
3697 // End of linker script support.
3699 // Save states before doing section layout.
3700 // This is used for relaxation.
3704 // Restore states prior to section layout.
3712 // Convert existing input sections to relaxed input sections.
3714 convert_input_sections_to_relaxed_sections(
3715 const std::vector<Output_relaxed_input_section*>& sections);
3717 // Find a relaxed input section to an input section in OBJECT
3718 // with index SHNDX. Return NULL if none is found.
3719 const Output_relaxed_input_section*
3720 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3722 // Whether section offsets need adjustment due to relaxation.
3724 section_offsets_need_adjustment() const
3725 { return this->section_offsets_need_adjustment_; }
3727 // Set section_offsets_need_adjustment to be true.
3729 set_section_offsets_need_adjustment()
3730 { this->section_offsets_need_adjustment_ = true; }
3732 // Adjust section offsets of input sections in this. This is
3733 // requires if relaxation caused some input sections to change sizes.
3735 adjust_section_offsets();
3737 // Whether this is a NOLOAD section.
3740 { return this->is_noload_; }
3745 { this->is_noload_ = true; }
3747 // Print merge statistics to stderr.
3749 print_merge_stats();
3751 // Set a fixed layout for the section. Used for incremental update links.
3753 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3754 uint64_t sh_addralign);
3756 // Return TRUE if the section has a fixed layout.
3758 has_fixed_layout() const
3759 { return this->has_fixed_layout_; }
3761 // Set flag to allow patch space for this section. Used for full
3762 // incremental links.
3764 set_is_patch_space_allowed()
3765 { this->is_patch_space_allowed_ = true; }
3767 // Set a fill method to use for free space left in the output section
3768 // during incremental links.
3770 set_free_space_fill(Output_fill* free_space_fill)
3772 this->free_space_fill_ = free_space_fill;
3773 this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3776 // Reserve space within the fixed layout for the section. Used for
3777 // incremental update links.
3779 reserve(uint64_t sh_offset, uint64_t sh_size);
3781 // Allocate space from the free list for the section. Used for
3782 // incremental update links.
3784 allocate(off_t len, uint64_t addralign);
3787 // Return the output section--i.e., the object itself.
3792 const Output_section*
3793 do_output_section() const
3796 // Return the section index in the output file.
3798 do_out_shndx() const
3800 gold_assert(this->out_shndx_ != -1U);
3801 return this->out_shndx_;
3804 // Set the output section index.
3806 do_set_out_shndx(unsigned int shndx)
3808 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3809 this->out_shndx_ = shndx;
3812 // Update the data size of the Output_section. For a typical
3813 // Output_section, there is nothing to do, but if there are any
3814 // Output_section_data objects we need to do a trial layout
3819 // Set the final data size of the Output_section. For a typical
3820 // Output_section, there is nothing to do, but if there are any
3821 // Output_section_data objects we need to set their final addresses
3824 set_final_data_size();
3826 // Reset the address and file offset.
3828 do_reset_address_and_file_offset();
3830 // Return true if address and file offset already have reset values. In
3831 // other words, calling reset_address_and_file_offset will not change them.
3833 do_address_and_file_offset_have_reset_values() const;
3835 // Write the data to the file. For a typical Output_section, this
3836 // does nothing: the data is written out by calling Object::Relocate
3837 // on each input object. But if there are any Output_section_data
3838 // objects we do need to write them out here.
3840 do_write(Output_file*);
3842 // Return the address alignment--function required by parent class.
3844 do_addralign() const
3845 { return this->addralign_; }
3847 // Return whether there is a load address.
3849 do_has_load_address() const
3850 { return this->has_load_address_; }
3852 // Return the load address.
3854 do_load_address() const
3856 gold_assert(this->has_load_address_);
3857 return this->load_address_;
3860 // Return whether this is an Output_section.
3862 do_is_section() const
3865 // Return whether this is a section of the specified type.
3867 do_is_section_type(elfcpp::Elf_Word type) const
3868 { return this->type_ == type; }
3870 // Return whether the specified section flag is set.
3872 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3873 { return (this->flags_ & flag) != 0; }
3875 // Set the TLS offset. Called only for SHT_TLS sections.
3877 do_set_tls_offset(uint64_t tls_base);
3879 // Return the TLS offset, relative to the base of the TLS segment.
3880 // Valid only for SHT_TLS sections.
3882 do_tls_offset() const
3883 { return this->tls_offset_; }
3885 // This may be implemented by a child class.
3887 do_finalize_name(Layout*)
3890 // Print to the map file.
3892 do_print_to_mapfile(Mapfile*) const;
3894 // Record that this section requires postprocessing after all
3895 // relocations have been applied. This is called by a child class.
3897 set_requires_postprocessing()
3899 this->requires_postprocessing_ = true;
3900 this->after_input_sections_ = true;
3903 // Write all the data of an Output_section into the postprocessing
3906 write_to_postprocessing_buffer();
3908 typedef std::vector<Input_section> Input_section_list;
3910 // Allow a child class to access the input sections.
3911 const Input_section_list&
3912 input_sections() const
3913 { return this->input_sections_; }
3915 // Whether this always keeps an input section list
3917 always_keeps_input_sections() const
3918 { return this->always_keeps_input_sections_; }
3920 // Always keep an input section list.
3922 set_always_keeps_input_sections()
3924 gold_assert(this->current_data_size_for_child() == 0);
3925 this->always_keeps_input_sections_ = true;
3929 // We only save enough information to undo the effects of section layout.
3930 class Checkpoint_output_section
3933 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3934 const Input_section_list& input_sections,
3935 off_t first_input_offset,
3936 bool attached_input_sections_are_sorted)
3937 : addralign_(addralign), flags_(flags),
3938 input_sections_(input_sections),
3939 input_sections_size_(input_sections_.size()),
3940 input_sections_copy_(), first_input_offset_(first_input_offset),
3941 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3945 ~Checkpoint_output_section()
3948 // Return the address alignment.
3951 { return this->addralign_; }
3953 // Return the section flags.
3956 { return this->flags_; }
3958 // Return a reference to the input section list copy.
3961 { return &this->input_sections_copy_; }
3963 // Return the size of input_sections at the time when checkpoint is
3966 input_sections_size() const
3967 { return this->input_sections_size_; }
3969 // Whether input sections are copied.
3971 input_sections_saved() const
3972 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3975 first_input_offset() const
3976 { return this->first_input_offset_; }
3979 attached_input_sections_are_sorted() const
3980 { return this->attached_input_sections_are_sorted_; }
3982 // Save input sections.
3984 save_input_sections()
3986 this->input_sections_copy_.reserve(this->input_sections_size_);
3987 this->input_sections_copy_.clear();
3988 Input_section_list::const_iterator p = this->input_sections_.begin();
3989 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3990 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3991 this->input_sections_copy_.push_back(*p);
3995 // The section alignment.
3996 uint64_t addralign_;
3997 // The section flags.
3998 elfcpp::Elf_Xword flags_;
3999 // Reference to the input sections to be checkpointed.
4000 const Input_section_list& input_sections_;
4001 // Size of the checkpointed portion of input_sections_;
4002 size_t input_sections_size_;
4003 // Copy of input sections.
4004 Input_section_list input_sections_copy_;
4005 // The offset of the first entry in input_sections_.
4006 off_t first_input_offset_;
4007 // True if the input sections attached to this output section have
4008 // already been sorted.
4009 bool attached_input_sections_are_sorted_;
4012 // This class is used to sort the input sections.
4013 class Input_section_sort_entry;
4015 // This is the sort comparison function for ctors and dtors.
4016 struct Input_section_sort_compare
4019 operator()(const Input_section_sort_entry&,
4020 const Input_section_sort_entry&) const;
4023 // This is the sort comparison function for .init_array and .fini_array.
4024 struct Input_section_sort_init_fini_compare
4027 operator()(const Input_section_sort_entry&,
4028 const Input_section_sort_entry&) const;
4031 // This is the sort comparison function when a section order is specified
4032 // from an input file.
4033 struct Input_section_sort_section_order_index_compare
4036 operator()(const Input_section_sort_entry&,
4037 const Input_section_sort_entry&) const;
4040 // Fill data. This is used to fill in data between input sections.
4041 // It is also used for data statements (BYTE, WORD, etc.) in linker
4042 // scripts. When we have to keep track of the input sections, we
4043 // can use an Output_data_const, but we don't want to have to keep
4044 // track of input sections just to implement fills.
4048 Fill(off_t section_offset, off_t length)
4049 : section_offset_(section_offset),
4050 length_(convert_to_section_size_type(length))
4053 // Return section offset.
4055 section_offset() const
4056 { return this->section_offset_; }
4058 // Return fill length.
4061 { return this->length_; }
4064 // The offset within the output section.
4065 off_t section_offset_;
4066 // The length of the space to fill.
4067 section_size_type length_;
4070 typedef std::vector<Fill> Fill_list;
4072 // Map used during relaxation of existing sections. This map
4073 // a section id an input section list index. We assume that
4074 // Input_section_list is a vector.
4075 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4077 // Add a new output section by Input_section.
4079 add_output_section_data(Input_section*);
4081 // Add an SHF_MERGE input section. Returns true if the section was
4082 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
4083 // stores information about the merged input sections.
4085 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4086 uint64_t entsize, uint64_t addralign,
4087 bool keeps_input_sections);
4089 // Add an output SHF_MERGE section POSD to this output section.
4090 // IS_STRING indicates whether it is a SHF_STRINGS section, and
4091 // ENTSIZE is the entity size. This returns the entry added to
4094 add_output_merge_section(Output_section_data* posd, bool is_string,
4097 // Sort the attached input sections.
4099 sort_attached_input_sections();
4101 // Find the merge section into which an input section with index SHNDX in
4102 // OBJECT has been added. Return NULL if none found.
4103 Output_section_data*
4104 find_merge_section(const Relobj* object, unsigned int shndx) const;
4106 // Build a relaxation map.
4108 build_relaxation_map(
4109 const Input_section_list& input_sections,
4111 Relaxation_map* map) const;
4113 // Convert input sections in an input section list into relaxed sections.
4115 convert_input_sections_in_list_to_relaxed_sections(
4116 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4117 const Relaxation_map& map,
4118 Input_section_list* input_sections);
4120 // Build the lookup maps for merge and relaxed input sections.
4122 build_lookup_maps() const;
4124 // Most of these fields are only valid after layout.
4126 // The name of the section. This will point into a Stringpool.
4128 // The section address is in the parent class.
4129 // The section alignment.
4130 uint64_t addralign_;
4131 // The section entry size.
4133 // The load address. This is only used when using a linker script
4134 // with a SECTIONS clause. The has_load_address_ field indicates
4135 // whether this field is valid.
4136 uint64_t load_address_;
4137 // The file offset is in the parent class.
4138 // Set the section link field to the index of this section.
4139 const Output_data* link_section_;
4140 // If link_section_ is NULL, this is the link field.
4142 // Set the section info field to the index of this section.
4143 const Output_section* info_section_;
4144 // If info_section_ is NULL, set the info field to the symbol table
4145 // index of this symbol.
4146 const Symbol* info_symndx_;
4147 // If info_section_ and info_symndx_ are NULL, this is the section
4150 // The section type.
4151 const elfcpp::Elf_Word type_;
4152 // The section flags.
4153 elfcpp::Elf_Xword flags_;
4154 // The order of this section in the output segment.
4155 Output_section_order order_;
4156 // The section index.
4157 unsigned int out_shndx_;
4158 // If there is a STT_SECTION for this output section in the normal
4159 // symbol table, this is the symbol index. This starts out as zero.
4160 // It is initialized in Layout::finalize() to be the index, or -1U
4161 // if there isn't one.
4162 unsigned int symtab_index_;
4163 // If there is a STT_SECTION for this output section in the dynamic
4164 // symbol table, this is the symbol index. This starts out as zero.
4165 // It is initialized in Layout::finalize() to be the index, or -1U
4166 // if there isn't one.
4167 unsigned int dynsym_index_;
4168 // The input sections. This will be empty in cases where we don't
4169 // need to keep track of them.
4170 Input_section_list input_sections_;
4171 // The offset of the first entry in input_sections_.
4172 off_t first_input_offset_;
4173 // The fill data. This is separate from input_sections_ because we
4174 // often will need fill sections without needing to keep track of
4177 // If the section requires postprocessing, this buffer holds the
4178 // section contents during relocation.
4179 unsigned char* postprocessing_buffer_;
4180 // Whether this output section needs a STT_SECTION symbol in the
4181 // normal symbol table. This will be true if there is a relocation
4183 bool needs_symtab_index_ : 1;
4184 // Whether this output section needs a STT_SECTION symbol in the
4185 // dynamic symbol table. This will be true if there is a dynamic
4186 // relocation which needs it.
4187 bool needs_dynsym_index_ : 1;
4188 // Whether the link field of this output section should point to the
4189 // normal symbol table.
4190 bool should_link_to_symtab_ : 1;
4191 // Whether the link field of this output section should point to the
4192 // dynamic symbol table.
4193 bool should_link_to_dynsym_ : 1;
4194 // Whether this section should be written after all the input
4195 // sections are complete.
4196 bool after_input_sections_ : 1;
4197 // Whether this section requires post processing after all
4198 // relocations have been applied.
4199 bool requires_postprocessing_ : 1;
4200 // Whether an input section was mapped to this output section
4201 // because of a SECTIONS clause in a linker script.
4202 bool found_in_sections_clause_ : 1;
4203 // Whether this section has an explicitly specified load address.
4204 bool has_load_address_ : 1;
4205 // True if the info_section_ field means the section index of the
4206 // section, false if it means the symbol index of the corresponding
4208 bool info_uses_section_index_ : 1;
4209 // True if input sections attached to this output section have to be
4210 // sorted according to a specified order.
4211 bool input_section_order_specified_ : 1;
4212 // True if the input sections attached to this output section may
4214 bool may_sort_attached_input_sections_ : 1;
4215 // True if the input sections attached to this output section must
4217 bool must_sort_attached_input_sections_ : 1;
4218 // True if the input sections attached to this output section have
4219 // already been sorted.
4220 bool attached_input_sections_are_sorted_ : 1;
4221 // True if this section holds relro data.
4223 // True if this is a small section.
4224 bool is_small_section_ : 1;
4225 // True if this is a large section.
4226 bool is_large_section_ : 1;
4227 // Whether code-fills are generated at write.
4228 bool generate_code_fills_at_write_ : 1;
4229 // Whether the entry size field should be zero.
4230 bool is_entsize_zero_ : 1;
4231 // Whether section offsets need adjustment due to relaxation.
4232 bool section_offsets_need_adjustment_ : 1;
4233 // Whether this is a NOLOAD section.
4234 bool is_noload_ : 1;
4235 // Whether this always keeps input section.
4236 bool always_keeps_input_sections_ : 1;
4237 // Whether this section has a fixed layout, for incremental update links.
4238 bool has_fixed_layout_ : 1;
4239 // True if we can add patch space to this section.
4240 bool is_patch_space_allowed_ : 1;
4241 // True if this output section goes into a unique segment.
4242 bool is_unique_segment_ : 1;
4243 // For SHT_TLS sections, the offset of this section relative to the base
4244 // of the TLS segment.
4245 uint64_t tls_offset_;
4246 // Additional segment flags, specified via linker plugin, when mapping some
4247 // input sections to unique segments.
4248 uint64_t extra_segment_flags_;
4249 // Segment alignment specified via linker plugin, when mapping some
4250 // input sections to unique segments.
4251 uint64_t segment_alignment_;
4252 // Saved checkpoint.
4253 Checkpoint_output_section* checkpoint_;
4254 // Fast lookup maps for merged and relaxed input sections.
4255 Output_section_lookup_maps* lookup_maps_;
4256 // List of available regions within the section, for incremental
4258 Free_list free_list_;
4259 // Method for filling chunks of free space.
4260 Output_fill* free_space_fill_;
4261 // Amount added as patch space for incremental linking.
4265 // An output segment. PT_LOAD segments are built from collections of
4266 // output sections. Other segments typically point within PT_LOAD
4267 // segments, and are built directly as needed.
4269 // NOTE: We want to use the copy constructor for this class. During
4270 // relaxation, we may try built the segments multiple times. We do
4271 // that by copying the original segment list before lay-out, doing
4272 // a trial lay-out and roll-back to the saved copied if we need to
4273 // to the lay-out again.
4275 class Output_segment
4278 // Create an output segment, specifying the type and flags.
4279 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4281 // Return the virtual address.
4284 { return this->vaddr_; }
4286 // Return the physical address.
4289 { return this->paddr_; }
4291 // Return the segment type.
4294 { return this->type_; }
4296 // Return the segment flags.
4299 { return this->flags_; }
4301 // Return the memory size.
4304 { return this->memsz_; }
4306 // Return the file size.
4309 { return this->filesz_; }
4311 // Return the file offset.
4314 { return this->offset_; }
4316 // Whether this is a segment created to hold large data sections.
4318 is_large_data_segment() const
4319 { return this->is_large_data_segment_; }
4321 // Record that this is a segment created to hold large data
4324 set_is_large_data_segment()
4325 { this->is_large_data_segment_ = true; }
4328 is_unique_segment() const
4329 { return this->is_unique_segment_; }
4331 // Mark segment as unique, happens when linker plugins request that
4332 // certain input sections be mapped to unique segments.
4334 set_is_unique_segment()
4335 { this->is_unique_segment_ = true; }
4337 // Return the maximum alignment of the Output_data.
4339 maximum_alignment();
4341 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4342 // the segment flags to use.
4344 add_output_section_to_load(Layout* layout, Output_section* os,
4345 elfcpp::Elf_Word seg_flags);
4347 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4348 // is the segment flags to use.
4350 add_output_section_to_nonload(Output_section* os,
4351 elfcpp::Elf_Word seg_flags);
4353 // Remove an Output_section from this segment. It is an error if it
4356 remove_output_section(Output_section* os);
4358 // Add an Output_data (which need not be an Output_section) to the
4359 // start of this segment.
4361 add_initial_output_data(Output_data*);
4363 // Return true if this segment has any sections which hold actual
4364 // data, rather than being a BSS section.
4366 has_any_data_sections() const;
4368 // Whether this segment has a dynamic relocs.
4370 has_dynamic_reloc() const;
4372 // Return the address of the first section.
4374 first_section_load_address() const;
4376 // Return whether the addresses have been set already.
4378 are_addresses_set() const
4379 { return this->are_addresses_set_; }
4381 // Set the addresses.
4383 set_addresses(uint64_t vaddr, uint64_t paddr)
4385 this->vaddr_ = vaddr;
4386 this->paddr_ = paddr;
4387 this->are_addresses_set_ = true;
4390 // Update the flags for the flags of an output section added to this
4393 update_flags_for_output_section(elfcpp::Elf_Xword flags)
4395 // The ELF ABI specifies that a PT_TLS segment should always have
4396 // PF_R as the flags.
4397 if (this->type() != elfcpp::PT_TLS)
4398 this->flags_ |= flags;
4401 // Set the segment flags. This is only used if we have a PHDRS
4402 // clause which explicitly specifies the flags.
4404 set_flags(elfcpp::Elf_Word flags)
4405 { this->flags_ = flags; }
4407 // Set the address of the segment to ADDR and the offset to *POFF
4408 // and set the addresses and offsets of all contained output
4409 // sections accordingly. Set the section indexes of all contained
4410 // output sections starting with *PSHNDX. If RESET is true, first
4411 // reset the addresses of the contained sections. Return the
4412 // address of the immediately following segment. Update *POFF and
4413 // *PSHNDX. This should only be called for a PT_LOAD segment.
4415 set_section_addresses(Layout*, bool reset, uint64_t addr,
4416 unsigned int* increase_relro, bool* has_relro,
4417 off_t* poff, unsigned int* pshndx);
4419 // Set the minimum alignment of this segment. This may be adjusted
4420 // upward based on the section alignments.
4422 set_minimum_p_align(uint64_t align)
4424 if (align > this->min_p_align_)
4425 this->min_p_align_ = align;
4428 // Set the offset of this segment based on the section. This should
4429 // only be called for a non-PT_LOAD segment.
4431 set_offset(unsigned int increase);
4433 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4437 // Return the number of output sections.
4439 output_section_count() const;
4441 // Return the section attached to the list segment with the lowest
4442 // load address. This is used when handling a PHDRS clause in a
4445 section_with_lowest_load_address() const;
4447 // Write the segment header into *OPHDR.
4448 template<int size, bool big_endian>
4450 write_header(elfcpp::Phdr_write<size, big_endian>*);
4452 // Write the section headers of associated sections into V.
4453 template<int size, bool big_endian>
4455 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4456 unsigned int* pshndx) const;
4458 // Print the output sections in the map file.
4460 print_sections_to_mapfile(Mapfile*) const;
4463 typedef std::vector<Output_data*> Output_data_list;
4465 // Find the maximum alignment in an Output_data_list.
4467 maximum_alignment_list(const Output_data_list*);
4469 // Return whether the first data section is a relro section.
4471 is_first_section_relro() const;
4473 // Set the section addresses in an Output_data_list.
4475 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4476 uint64_t addr, off_t* poff, unsigned int* pshndx,
4479 // Return the number of Output_sections in an Output_data_list.
4481 output_section_count_list(const Output_data_list*) const;
4483 // Return whether an Output_data_list has a dynamic reloc.
4485 has_dynamic_reloc_list(const Output_data_list*) const;
4487 // Find the section with the lowest load address in an
4488 // Output_data_list.
4490 lowest_load_address_in_list(const Output_data_list* pdl,
4491 Output_section** found,
4492 uint64_t* found_lma) const;
4494 // Find the first and last entries by address.
4496 find_first_and_last_list(const Output_data_list* pdl,
4497 const Output_data** pfirst,
4498 const Output_data** plast) const;
4500 // Write the section headers in the list into V.
4501 template<int size, bool big_endian>
4503 write_section_headers_list(const Layout*, const Stringpool*,
4504 const Output_data_list*, unsigned char* v,
4505 unsigned int* pshdx) const;
4507 // Print a section list to the mapfile.
4509 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4511 // NOTE: We want to use the copy constructor. Currently, shallow copy
4512 // works for us so we do not need to write our own copy constructor.
4514 // The list of output data attached to this segment.
4515 Output_data_list output_lists_[ORDER_MAX];
4516 // The segment virtual address.
4518 // The segment physical address.
4520 // The size of the segment in memory.
4522 // The maximum section alignment. The is_max_align_known_ field
4523 // indicates whether this has been finalized.
4524 uint64_t max_align_;
4525 // The required minimum value for the p_align field. This is used
4526 // for PT_LOAD segments. Note that this does not mean that
4527 // addresses should be aligned to this value; it means the p_paddr
4528 // and p_vaddr fields must be congruent modulo this value. For
4529 // non-PT_LOAD segments, the dynamic linker works more efficiently
4530 // if the p_align field has the more conventional value, although it
4531 // can align as needed.
4532 uint64_t min_p_align_;
4533 // The offset of the segment data within the file.
4535 // The size of the segment data in the file.
4537 // The segment type;
4538 elfcpp::Elf_Word type_;
4539 // The segment flags.
4540 elfcpp::Elf_Word flags_;
4541 // Whether we have finalized max_align_.
4542 bool is_max_align_known_ : 1;
4543 // Whether vaddr and paddr were set by a linker script.
4544 bool are_addresses_set_ : 1;
4545 // Whether this segment holds large data sections.
4546 bool is_large_data_segment_ : 1;
4547 // Whether this was marked as a unique segment via a linker plugin.
4548 bool is_unique_segment_ : 1;
4551 // This class represents the output file.
4556 Output_file(const char* name);
4558 // Indicate that this is a temporary file which should not be
4562 { this->is_temporary_ = true; }
4564 // Try to open an existing file. Returns false if the file doesn't
4565 // exist, has a size of 0 or can't be mmaped. This method is
4566 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4567 // that file as the base for incremental linking.
4569 open_base_file(const char* base_name, bool writable);
4571 // Open the output file. FILE_SIZE is the final size of the file.
4572 // If the file already exists, it is deleted/truncated. This method
4573 // is thread-unsafe.
4575 open(off_t file_size);
4577 // Resize the output file. This method is thread-unsafe.
4579 resize(off_t file_size);
4581 // Close the output file (flushing all buffered data) and make sure
4582 // there are no errors. This method is thread-unsafe.
4586 // Return the size of this file.
4589 { return this->file_size_; }
4591 // Return the name of this file.
4594 { return this->name_; }
4596 // We currently always use mmap which makes the view handling quite
4597 // simple. In the future we may support other approaches.
4599 // Write data to the output file.
4601 write(off_t offset, const void* data, size_t len)
4602 { memcpy(this->base_ + offset, data, len); }
4604 // Get a buffer to use to write to the file, given the offset into
4605 // the file and the size.
4607 get_output_view(off_t start, size_t size)
4609 gold_assert(start >= 0
4610 && start + static_cast<off_t>(size) <= this->file_size_);
4611 return this->base_ + start;
4614 // VIEW must have been returned by get_output_view. Write the
4615 // buffer to the file, passing in the offset and the size.
4617 write_output_view(off_t, size_t, unsigned char*)
4620 // Get a read/write buffer. This is used when we want to write part
4621 // of the file, read it in, and write it again.
4623 get_input_output_view(off_t start, size_t size)
4624 { return this->get_output_view(start, size); }
4626 // Write a read/write buffer back to the file.
4628 write_input_output_view(off_t, size_t, unsigned char*)
4631 // Get a read buffer. This is used when we just want to read part
4632 // of the file back it in.
4633 const unsigned char*
4634 get_input_view(off_t start, size_t size)
4635 { return this->get_output_view(start, size); }
4637 // Release a read bfufer.
4639 free_input_view(off_t, size_t, const unsigned char*)
4643 // Map the file into memory or, if that fails, allocate anonymous
4648 // Allocate anonymous memory for the file.
4652 // Map the file into memory.
4654 map_no_anonymous(bool);
4656 // Unmap the file from memory (and flush to disk buffers).
4666 // Base of file mapped into memory.
4667 unsigned char* base_;
4668 // True iff base_ points to a memory buffer rather than an output file.
4669 bool map_is_anonymous_;
4670 // True if base_ was allocated using new rather than mmap.
4671 bool map_is_allocated_;
4672 // True if this is a temporary file which should not be output.
4676 } // End namespace gold.
4678 #endif // !defined(GOLD_OUTPUT_H)