1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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>
50 // This class specifies an input section. It is used as a key type
53 class Input_section_specifier
56 Input_section_specifier(const Relobj* relobj, unsigned int shndx)
57 : relobj_(relobj), shndx_(shndx)
60 // Return Relobj of this.
63 { return this->relobj_; }
65 // Return section index of this.
68 { return this->shndx_; }
70 // Whether this equals to another specifier ISS.
72 eq(const Input_section_specifier& iss) const
73 { return this->relobj_ == iss.relobj_ && this->shndx_ == iss.shndx_; }
75 // Compute a hash value of this.
79 return (gold::string_hash<char>(this->relobj_->name().c_str())
83 // Functors for containers.
87 operator()(const Input_section_specifier& iss1,
88 const Input_section_specifier& iss2) const
89 { return iss1.eq(iss2); }
95 operator()(const Input_section_specifier& iss) const
96 { return iss.hash_value(); }
101 const Relobj* relobj_;
106 // An abtract class for data which has to go into the output file.
111 explicit Output_data()
112 : address_(0), data_size_(0), offset_(-1),
113 is_address_valid_(false), is_data_size_valid_(false),
114 is_offset_valid_(false), is_data_size_fixed_(false),
115 dynamic_reloc_count_(0)
121 // Return the address. For allocated sections, this is only valid
122 // after Layout::finalize is finished.
126 gold_assert(this->is_address_valid_);
127 return this->address_;
130 // Return the size of the data. For allocated sections, this must
131 // be valid after Layout::finalize calls set_address, but need not
132 // be valid before then.
136 gold_assert(this->is_data_size_valid_);
137 return this->data_size_;
140 // Return true if data size is fixed.
142 is_data_size_fixed() const
143 { return this->is_data_size_fixed_; }
145 // Return the file offset. This is only valid after
146 // Layout::finalize is finished. For some non-allocated sections,
147 // it may not be valid until near the end of the link.
151 gold_assert(this->is_offset_valid_);
152 return this->offset_;
155 // Reset the address and file offset. This essentially disables the
156 // sanity testing about duplicate and unknown settings.
158 reset_address_and_file_offset()
160 this->is_address_valid_ = false;
161 this->is_offset_valid_ = false;
162 if (!this->is_data_size_fixed_)
163 this->is_data_size_valid_ = false;
164 this->do_reset_address_and_file_offset();
167 // Return true if address and file offset already have reset values. In
168 // other words, calling reset_address_and_file_offset will not change them.
170 address_and_file_offset_have_reset_values() const
171 { return this->do_address_and_file_offset_have_reset_values(); }
173 // Return the required alignment.
176 { return this->do_addralign(); }
178 // Return whether this has a load address.
180 has_load_address() const
181 { return this->do_has_load_address(); }
183 // Return the load address.
186 { return this->do_load_address(); }
188 // Return whether this is an Output_section.
191 { return this->do_is_section(); }
193 // Return whether this is an Output_section of the specified type.
195 is_section_type(elfcpp::Elf_Word stt) const
196 { return this->do_is_section_type(stt); }
198 // Return whether this is an Output_section with the specified flag
201 is_section_flag_set(elfcpp::Elf_Xword shf) const
202 { return this->do_is_section_flag_set(shf); }
204 // Return the output section that this goes in, if there is one.
207 { return this->do_output_section(); }
209 const Output_section*
210 output_section() const
211 { return this->do_output_section(); }
213 // Return the output section index, if there is an output section.
216 { return this->do_out_shndx(); }
218 // Set the output section index, if this is an output section.
220 set_out_shndx(unsigned int shndx)
221 { this->do_set_out_shndx(shndx); }
223 // Set the address and file offset of this data, and finalize the
224 // size of the data. This is called during Layout::finalize for
225 // allocated sections.
227 set_address_and_file_offset(uint64_t addr, off_t off)
229 this->set_address(addr);
230 this->set_file_offset(off);
231 this->finalize_data_size();
236 set_address(uint64_t addr)
238 gold_assert(!this->is_address_valid_);
239 this->address_ = addr;
240 this->is_address_valid_ = true;
243 // Set the file offset.
245 set_file_offset(off_t off)
247 gold_assert(!this->is_offset_valid_);
249 this->is_offset_valid_ = true;
252 // Finalize the data size.
256 if (!this->is_data_size_valid_)
258 // Tell the child class to set the data size.
259 this->set_final_data_size();
260 gold_assert(this->is_data_size_valid_);
264 // Set the TLS offset. Called only for SHT_TLS sections.
266 set_tls_offset(uint64_t tls_base)
267 { this->do_set_tls_offset(tls_base); }
269 // Return the TLS offset, relative to the base of the TLS segment.
270 // Valid only for SHT_TLS sections.
273 { return this->do_tls_offset(); }
275 // Write the data to the output file. This is called after
276 // Layout::finalize is complete.
278 write(Output_file* file)
279 { this->do_write(file); }
281 // This is called by Layout::finalize to note that the sizes of
282 // allocated sections must now be fixed.
285 { Output_data::allocated_sizes_are_fixed = true; }
287 // Used to check that layout has been done.
290 { return Output_data::allocated_sizes_are_fixed; }
292 // Count the number of dynamic relocations applied to this section.
295 { ++this->dynamic_reloc_count_; }
297 // Return the number of dynamic relocations applied to this section.
299 dynamic_reloc_count() const
300 { return this->dynamic_reloc_count_; }
302 // Whether the address is valid.
304 is_address_valid() const
305 { return this->is_address_valid_; }
307 // Whether the file offset is valid.
309 is_offset_valid() const
310 { return this->is_offset_valid_; }
312 // Whether the data size is valid.
314 is_data_size_valid() const
315 { return this->is_data_size_valid_; }
317 // Print information to the map file.
319 print_to_mapfile(Mapfile* mapfile) const
320 { return this->do_print_to_mapfile(mapfile); }
323 // Functions that child classes may or in some cases must implement.
325 // Write the data to the output file.
327 do_write(Output_file*) = 0;
329 // Return the required alignment.
331 do_addralign() const = 0;
333 // Return whether this has a load address.
335 do_has_load_address() const
338 // Return the load address.
340 do_load_address() const
341 { gold_unreachable(); }
343 // Return whether this is an Output_section.
345 do_is_section() const
348 // Return whether this is an Output_section of the specified type.
349 // This only needs to be implement by Output_section.
351 do_is_section_type(elfcpp::Elf_Word) const
354 // Return whether this is an Output_section with the specific flag
355 // set. This only needs to be implemented by Output_section.
357 do_is_section_flag_set(elfcpp::Elf_Xword) const
360 // Return the output section, if there is one.
361 virtual Output_section*
365 virtual const Output_section*
366 do_output_section() const
369 // Return the output section index, if there is an output section.
372 { gold_unreachable(); }
374 // Set the output section index, if this is an output section.
376 do_set_out_shndx(unsigned int)
377 { gold_unreachable(); }
379 // This is a hook for derived classes to set the data size. This is
380 // called by finalize_data_size, normally called during
381 // Layout::finalize, when the section address is set.
383 set_final_data_size()
384 { gold_unreachable(); }
386 // A hook for resetting the address and file offset.
388 do_reset_address_and_file_offset()
391 // Return true if address and file offset already have reset values. In
392 // other words, calling reset_address_and_file_offset will not change them.
393 // A child class overriding do_reset_address_and_file_offset may need to
394 // also override this.
396 do_address_and_file_offset_have_reset_values() const
397 { return !this->is_address_valid_ && !this->is_offset_valid_; }
399 // Set the TLS offset. Called only for SHT_TLS sections.
401 do_set_tls_offset(uint64_t)
402 { gold_unreachable(); }
404 // Return the TLS offset, relative to the base of the TLS segment.
405 // Valid only for SHT_TLS sections.
407 do_tls_offset() const
408 { gold_unreachable(); }
410 // Print to the map file. This only needs to be implemented by
411 // classes which may appear in a PT_LOAD segment.
413 do_print_to_mapfile(Mapfile*) const
414 { gold_unreachable(); }
416 // Functions that child classes may call.
418 // Reset the address. The Output_section class needs this when an
419 // SHF_ALLOC input section is added to an output section which was
420 // formerly not SHF_ALLOC.
422 mark_address_invalid()
423 { this->is_address_valid_ = false; }
425 // Set the size of the data.
427 set_data_size(off_t data_size)
429 gold_assert(!this->is_data_size_valid_
430 && !this->is_data_size_fixed_);
431 this->data_size_ = data_size;
432 this->is_data_size_valid_ = true;
435 // Fix the data size. Once it is fixed, it cannot be changed
436 // and the data size remains always valid.
440 gold_assert(this->is_data_size_valid_);
441 this->is_data_size_fixed_ = true;
444 // Get the current data size--this is for the convenience of
445 // sections which build up their size over time.
447 current_data_size_for_child() const
448 { return this->data_size_; }
450 // Set the current data size--this is for the convenience of
451 // sections which build up their size over time.
453 set_current_data_size_for_child(off_t data_size)
455 gold_assert(!this->is_data_size_valid_);
456 this->data_size_ = data_size;
459 // Return default alignment for the target size.
463 // Return default alignment for a specified size--32 or 64.
465 default_alignment_for_size(int size);
468 Output_data(const Output_data&);
469 Output_data& operator=(const Output_data&);
471 // This is used for verification, to make sure that we don't try to
472 // change any sizes of allocated sections after we set the section
474 static bool allocated_sizes_are_fixed;
476 // Memory address in output file.
478 // Size of data in output file.
480 // File offset of contents in output file.
482 // Whether address_ is valid.
483 bool is_address_valid_;
484 // Whether data_size_ is valid.
485 bool is_data_size_valid_;
486 // Whether offset_ is valid.
487 bool is_offset_valid_;
488 // Whether data size is fixed.
489 bool is_data_size_fixed_;
490 // Count of dynamic relocations applied to this section.
491 unsigned int dynamic_reloc_count_;
494 // Output the section headers.
496 class Output_section_headers : public Output_data
499 Output_section_headers(const Layout*,
500 const Layout::Segment_list*,
501 const Layout::Section_list*,
502 const Layout::Section_list*,
504 const Output_section*);
507 // Write the data to the file.
509 do_write(Output_file*);
511 // Return the required alignment.
514 { return Output_data::default_alignment(); }
516 // Write to a map file.
518 do_print_to_mapfile(Mapfile* mapfile) const
519 { mapfile->print_output_data(this, _("** section headers")); }
521 // Set final data size.
523 set_final_data_size()
524 { this->set_data_size(this->do_size()); }
527 // Write the data to the file with the right size and endianness.
528 template<int size, bool big_endian>
530 do_sized_write(Output_file*);
532 // Compute data size.
536 const Layout* layout_;
537 const Layout::Segment_list* segment_list_;
538 const Layout::Section_list* section_list_;
539 const Layout::Section_list* unattached_section_list_;
540 const Stringpool* secnamepool_;
541 const Output_section* shstrtab_section_;
544 // Output the segment headers.
546 class Output_segment_headers : public Output_data
549 Output_segment_headers(const Layout::Segment_list& segment_list);
552 // Write the data to the file.
554 do_write(Output_file*);
556 // Return the required alignment.
559 { return Output_data::default_alignment(); }
561 // Write to a map file.
563 do_print_to_mapfile(Mapfile* mapfile) const
564 { mapfile->print_output_data(this, _("** segment headers")); }
566 // Set final data size.
568 set_final_data_size()
569 { this->set_data_size(this->do_size()); }
572 // Write the data to the file with the right size and endianness.
573 template<int size, bool big_endian>
575 do_sized_write(Output_file*);
577 // Compute the current size.
581 const Layout::Segment_list& segment_list_;
584 // Output the ELF file header.
586 class Output_file_header : public Output_data
589 Output_file_header(const Target*,
591 const Output_segment_headers*,
594 // Add information about the section headers. We lay out the ELF
595 // file header before we create the section headers.
596 void set_section_info(const Output_section_headers*,
597 const Output_section* shstrtab);
600 // Write the data to the file.
602 do_write(Output_file*);
604 // Return the required alignment.
607 { return Output_data::default_alignment(); }
609 // Write to a map file.
611 do_print_to_mapfile(Mapfile* mapfile) const
612 { mapfile->print_output_data(this, _("** file header")); }
614 // Set final data size.
616 set_final_data_size(void)
617 { this->set_data_size(this->do_size()); }
620 // Write the data to the file with the right size and endianness.
621 template<int size, bool big_endian>
623 do_sized_write(Output_file*);
625 // Return the value to use for the entry address.
627 typename elfcpp::Elf_types<size>::Elf_Addr
630 // Compute the current data size.
634 const Target* target_;
635 const Symbol_table* symtab_;
636 const Output_segment_headers* segment_header_;
637 const Output_section_headers* section_header_;
638 const Output_section* shstrtab_;
642 // Output sections are mainly comprised of input sections. However,
643 // there are cases where we have data to write out which is not in an
644 // input section. Output_section_data is used in such cases. This is
645 // an abstract base class.
647 class Output_section_data : public Output_data
650 Output_section_data(off_t data_size, uint64_t addralign,
651 bool is_data_size_fixed)
652 : Output_data(), output_section_(NULL), addralign_(addralign)
654 this->set_data_size(data_size);
655 if (is_data_size_fixed)
656 this->fix_data_size();
659 Output_section_data(uint64_t addralign)
660 : Output_data(), output_section_(NULL), addralign_(addralign)
663 // Return the output section.
664 const Output_section*
665 output_section() const
666 { return this->output_section_; }
668 // Record the output section.
670 set_output_section(Output_section* os);
672 // Add an input section, for SHF_MERGE sections. This returns true
673 // if the section was handled.
675 add_input_section(Relobj* object, unsigned int shndx)
676 { return this->do_add_input_section(object, shndx); }
678 // Given an input OBJECT, an input section index SHNDX within that
679 // object, and an OFFSET relative to the start of that input
680 // section, return whether or not the corresponding offset within
681 // the output section is known. If this function returns true, it
682 // sets *POUTPUT to the output offset. The value -1 indicates that
683 // this input offset is being discarded.
685 output_offset(const Relobj* object, unsigned int shndx,
686 section_offset_type offset,
687 section_offset_type *poutput) const
688 { return this->do_output_offset(object, shndx, offset, poutput); }
690 // Return whether this is the merge section for the input section
691 // SHNDX in OBJECT. This should return true when output_offset
692 // would return true for some values of OFFSET.
694 is_merge_section_for(const Relobj* object, unsigned int shndx) const
695 { return this->do_is_merge_section_for(object, shndx); }
697 // Write the contents to a buffer. This is used for sections which
698 // require postprocessing, such as compression.
700 write_to_buffer(unsigned char* buffer)
701 { this->do_write_to_buffer(buffer); }
703 // Print merge stats to stderr. This should only be called for
704 // SHF_MERGE sections.
706 print_merge_stats(const char* section_name)
707 { this->do_print_merge_stats(section_name); }
710 // The child class must implement do_write.
712 // The child class may implement specific adjustments to the output
715 do_adjust_output_section(Output_section*)
718 // May be implemented by child class. Return true if the section
721 do_add_input_section(Relobj*, unsigned int)
722 { gold_unreachable(); }
724 // The child class may implement output_offset.
726 do_output_offset(const Relobj*, unsigned int, section_offset_type,
727 section_offset_type*) const
730 // The child class may implement is_merge_section_for.
732 do_is_merge_section_for(const Relobj*, unsigned int) const
735 // The child class may implement write_to_buffer. Most child
736 // classes can not appear in a compressed section, and they do not
739 do_write_to_buffer(unsigned char*)
740 { gold_unreachable(); }
742 // Print merge statistics.
744 do_print_merge_stats(const char*)
745 { gold_unreachable(); }
747 // Return the required alignment.
750 { return this->addralign_; }
752 // Return the output section.
755 { return this->output_section_; }
757 const Output_section*
758 do_output_section() const
759 { return this->output_section_; }
761 // Return the section index of the output section.
763 do_out_shndx() const;
765 // Set the alignment.
767 set_addralign(uint64_t addralign);
770 // The output section for this section.
771 Output_section* output_section_;
772 // The required alignment.
776 // Some Output_section_data classes build up their data step by step,
777 // rather than all at once. This class provides an interface for
780 class Output_section_data_build : public Output_section_data
783 Output_section_data_build(uint64_t addralign)
784 : Output_section_data(addralign)
787 // Get the current data size.
789 current_data_size() const
790 { return this->current_data_size_for_child(); }
792 // Set the current data size.
794 set_current_data_size(off_t data_size)
795 { this->set_current_data_size_for_child(data_size); }
798 // Set the final data size.
800 set_final_data_size()
801 { this->set_data_size(this->current_data_size_for_child()); }
804 // A simple case of Output_data in which we have constant data to
807 class Output_data_const : public Output_section_data
810 Output_data_const(const std::string& data, uint64_t addralign)
811 : Output_section_data(data.size(), addralign, true), data_(data)
814 Output_data_const(const char* p, off_t len, uint64_t addralign)
815 : Output_section_data(len, addralign, true), data_(p, len)
818 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
819 : Output_section_data(len, addralign, true),
820 data_(reinterpret_cast<const char*>(p), len)
824 // Write the data to the output file.
826 do_write(Output_file*);
828 // Write the data to a buffer.
830 do_write_to_buffer(unsigned char* buffer)
831 { memcpy(buffer, this->data_.data(), this->data_.size()); }
833 // Write to a map file.
835 do_print_to_mapfile(Mapfile* mapfile) const
836 { mapfile->print_output_data(this, _("** fill")); }
842 // Another version of Output_data with constant data, in which the
843 // buffer is allocated by the caller.
845 class Output_data_const_buffer : public Output_section_data
848 Output_data_const_buffer(const unsigned char* p, off_t len,
849 uint64_t addralign, const char* map_name)
850 : Output_section_data(len, addralign, true),
851 p_(p), map_name_(map_name)
855 // Write the data the output file.
857 do_write(Output_file*);
859 // Write the data to a buffer.
861 do_write_to_buffer(unsigned char* buffer)
862 { memcpy(buffer, this->p_, this->data_size()); }
864 // Write to a map file.
866 do_print_to_mapfile(Mapfile* mapfile) const
867 { mapfile->print_output_data(this, _(this->map_name_)); }
870 // The data to output.
871 const unsigned char* p_;
872 // Name to use in a map file. Maps are a rarely used feature, but
873 // the space usage is minor as aren't very many of these objects.
874 const char* map_name_;
877 // A place holder for a fixed amount of data written out via some
880 class Output_data_fixed_space : public Output_section_data
883 Output_data_fixed_space(off_t data_size, uint64_t addralign,
884 const char* map_name)
885 : Output_section_data(data_size, addralign, true),
890 // Write out the data--the actual data must be written out
893 do_write(Output_file*)
896 // Write to a map file.
898 do_print_to_mapfile(Mapfile* mapfile) const
899 { mapfile->print_output_data(this, _(this->map_name_)); }
902 // Name to use in a map file. Maps are a rarely used feature, but
903 // the space usage is minor as aren't very many of these objects.
904 const char* map_name_;
907 // A place holder for variable sized data written out via some other
910 class Output_data_space : public Output_section_data_build
913 explicit Output_data_space(uint64_t addralign, const char* map_name)
914 : Output_section_data_build(addralign),
918 // Set the alignment.
920 set_space_alignment(uint64_t align)
921 { this->set_addralign(align); }
924 // Write out the data--the actual data must be written out
927 do_write(Output_file*)
930 // Write to a map file.
932 do_print_to_mapfile(Mapfile* mapfile) const
933 { mapfile->print_output_data(this, _(this->map_name_)); }
936 // Name to use in a map file. Maps are a rarely used feature, but
937 // the space usage is minor as aren't very many of these objects.
938 const char* map_name_;
941 // Fill fixed space with zeroes. This is just like
942 // Output_data_fixed_space, except that the map name is known.
944 class Output_data_zero_fill : public Output_section_data
947 Output_data_zero_fill(off_t data_size, uint64_t addralign)
948 : Output_section_data(data_size, addralign, true)
952 // There is no data to write out.
954 do_write(Output_file*)
957 // Write to a map file.
959 do_print_to_mapfile(Mapfile* mapfile) const
960 { mapfile->print_output_data(this, "** zero fill"); }
963 // A string table which goes into an output section.
965 class Output_data_strtab : public Output_section_data
968 Output_data_strtab(Stringpool* strtab)
969 : Output_section_data(1), strtab_(strtab)
973 // This is called to set the address and file offset. Here we make
974 // sure that the Stringpool is finalized.
976 set_final_data_size();
978 // Write out the data.
980 do_write(Output_file*);
982 // Write the data to a buffer.
984 do_write_to_buffer(unsigned char* buffer)
985 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
987 // Write to a map file.
989 do_print_to_mapfile(Mapfile* mapfile) const
990 { mapfile->print_output_data(this, _("** string table")); }
996 // This POD class is used to represent a single reloc in the output
997 // file. This could be a private class within Output_data_reloc, but
998 // the templatization is complex enough that I broke it out into a
999 // separate class. The class is templatized on either elfcpp::SHT_REL
1000 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1001 // relocation or an ordinary relocation.
1003 // A relocation can be against a global symbol, a local symbol, a
1004 // local section symbol, an output section, or the undefined symbol at
1005 // index 0. We represent the latter by using a NULL global symbol.
1007 template<int sh_type, bool dynamic, int size, bool big_endian>
1010 template<bool dynamic, int size, bool big_endian>
1011 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1014 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1015 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1017 static const Address invalid_address = static_cast<Address>(0) - 1;
1019 // An uninitialized entry. We need this because we want to put
1020 // instances of this class into an STL container.
1022 : local_sym_index_(INVALID_CODE)
1025 // We have a bunch of different constructors. They come in pairs
1026 // depending on how the address of the relocation is specified. It
1027 // can either be an offset in an Output_data or an offset in an
1030 // A reloc against a global symbol.
1032 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1033 Address address, bool is_relative);
1035 Output_reloc(Symbol* gsym, unsigned int type,
1036 Sized_relobj<size, big_endian>* relobj,
1037 unsigned int shndx, Address address, bool is_relative);
1039 // A reloc against a local symbol or local section symbol.
1041 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1042 unsigned int local_sym_index, unsigned int type,
1043 Output_data* od, Address address, bool is_relative,
1044 bool is_section_symbol);
1046 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1047 unsigned int local_sym_index, unsigned int type,
1048 unsigned int shndx, Address address, bool is_relative,
1049 bool is_section_symbol);
1051 // A reloc against the STT_SECTION symbol of an output section.
1053 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1056 Output_reloc(Output_section* os, unsigned int type,
1057 Sized_relobj<size, big_endian>* relobj,
1058 unsigned int shndx, Address address);
1060 // Return TRUE if this is a RELATIVE relocation.
1063 { return this->is_relative_; }
1065 // Return whether this is against a local section symbol.
1067 is_local_section_symbol() const
1069 return (this->local_sym_index_ != GSYM_CODE
1070 && this->local_sym_index_ != SECTION_CODE
1071 && this->local_sym_index_ != INVALID_CODE
1072 && this->is_section_symbol_);
1075 // For a local section symbol, return the offset of the input
1076 // section within the output section. ADDEND is the addend being
1077 // applied to the input section.
1079 local_section_offset(Addend addend) const;
1081 // Get the value of the symbol referred to by a Rel relocation when
1082 // we are adding the given ADDEND.
1084 symbol_value(Addend addend) const;
1086 // Write the reloc entry to an output view.
1088 write(unsigned char* pov) const;
1090 // Write the offset and info fields to Write_rel.
1091 template<typename Write_rel>
1092 void write_rel(Write_rel*) const;
1094 // This is used when sorting dynamic relocs. Return -1 to sort this
1095 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1097 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1100 // Return whether this reloc should be sorted before the argument
1101 // when sorting dynamic relocs.
1103 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1105 { return this->compare(r2) < 0; }
1108 // Record that we need a dynamic symbol index.
1110 set_needs_dynsym_index();
1112 // Return the symbol index.
1114 get_symbol_index() const;
1116 // Return the output address.
1118 get_address() const;
1120 // Codes for local_sym_index_.
1127 // Invalid uninitialized entry.
1133 // For a local symbol or local section symbol
1134 // (this->local_sym_index_ >= 0), the object. We will never
1135 // generate a relocation against a local symbol in a dynamic
1136 // object; that doesn't make sense. And our callers will always
1137 // be templatized, so we use Sized_relobj here.
1138 Sized_relobj<size, big_endian>* relobj;
1139 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1140 // symbol. If this is NULL, it indicates a relocation against the
1141 // undefined 0 symbol.
1143 // For a relocation against an output section
1144 // (this->local_sym_index_ == SECTION_CODE), the output section.
1149 // If this->shndx_ is not INVALID CODE, the object which holds the
1150 // input section being used to specify the reloc address.
1151 Sized_relobj<size, big_endian>* relobj;
1152 // If this->shndx_ is INVALID_CODE, the output data being used to
1153 // specify the reloc address. This may be NULL if the reloc
1154 // address is absolute.
1157 // The address offset within the input section or the Output_data.
1159 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1160 // relocation against an output section, or INVALID_CODE for an
1161 // uninitialized value. Otherwise, for a local symbol
1162 // (this->is_section_symbol_ is false), the local symbol index. For
1163 // a local section symbol (this->is_section_symbol_ is true), the
1164 // section index in the input file.
1165 unsigned int local_sym_index_;
1166 // The reloc type--a processor specific code.
1167 unsigned int type_ : 30;
1168 // True if the relocation is a RELATIVE relocation.
1169 bool is_relative_ : 1;
1170 // True if the relocation is against a section symbol.
1171 bool is_section_symbol_ : 1;
1172 // If the reloc address is an input section in an object, the
1173 // section index. This is INVALID_CODE if the reloc address is
1174 // specified in some other way.
1175 unsigned int shndx_;
1178 // The SHT_RELA version of Output_reloc<>. This is just derived from
1179 // the SHT_REL version of Output_reloc, but it adds an addend.
1181 template<bool dynamic, int size, bool big_endian>
1182 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1185 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1186 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1188 // An uninitialized entry.
1193 // A reloc against a global symbol.
1195 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1196 Address address, Addend addend, bool is_relative)
1197 : rel_(gsym, type, od, address, is_relative), addend_(addend)
1200 Output_reloc(Symbol* gsym, unsigned int type,
1201 Sized_relobj<size, big_endian>* relobj,
1202 unsigned int shndx, Address address, Addend addend,
1204 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1207 // A reloc against a local symbol.
1209 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1210 unsigned int local_sym_index, unsigned int type,
1211 Output_data* od, Address address,
1212 Addend addend, bool is_relative, bool is_section_symbol)
1213 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1218 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1219 unsigned int local_sym_index, unsigned int type,
1220 unsigned int shndx, Address address,
1221 Addend addend, bool is_relative, bool is_section_symbol)
1222 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1227 // A reloc against the STT_SECTION symbol of an output section.
1229 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1230 Address address, Addend addend)
1231 : rel_(os, type, od, address), addend_(addend)
1234 Output_reloc(Output_section* os, unsigned int type,
1235 Sized_relobj<size, big_endian>* relobj,
1236 unsigned int shndx, Address address, Addend addend)
1237 : rel_(os, type, relobj, shndx, address), addend_(addend)
1240 // Write the reloc entry to an output view.
1242 write(unsigned char* pov) const;
1244 // Return whether this reloc should be sorted before the argument
1245 // when sorting dynamic relocs.
1247 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1250 int i = this->rel_.compare(r2.rel_);
1256 return this->addend_ < r2.addend_;
1261 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1266 // Output_data_reloc is used to manage a section containing relocs.
1267 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1268 // indicates whether this is a dynamic relocation or a normal
1269 // relocation. Output_data_reloc_base is a base class.
1270 // Output_data_reloc is the real class, which we specialize based on
1273 template<int sh_type, bool dynamic, int size, bool big_endian>
1274 class Output_data_reloc_base : public Output_section_data_build
1277 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1278 typedef typename Output_reloc_type::Address Address;
1279 static const int reloc_size =
1280 Reloc_types<sh_type, size, big_endian>::reloc_size;
1282 // Construct the section.
1283 Output_data_reloc_base(bool sort_relocs)
1284 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1285 sort_relocs_(sort_relocs)
1289 // Write out the data.
1291 do_write(Output_file*);
1293 // Set the entry size and the link.
1295 do_adjust_output_section(Output_section *os);
1297 // Write to a map file.
1299 do_print_to_mapfile(Mapfile* mapfile) const
1301 mapfile->print_output_data(this,
1303 ? _("** dynamic relocs")
1307 // Add a relocation entry.
1309 add(Output_data *od, const Output_reloc_type& reloc)
1311 this->relocs_.push_back(reloc);
1312 this->set_current_data_size(this->relocs_.size() * reloc_size);
1313 od->add_dynamic_reloc();
1317 typedef std::vector<Output_reloc_type> Relocs;
1319 // The class used to sort the relocations.
1320 struct Sort_relocs_comparison
1323 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1324 { return r1.sort_before(r2); }
1327 // The relocations in this section.
1329 // Whether to sort the relocations when writing them out, to make
1330 // the dynamic linker more efficient.
1334 // The class which callers actually create.
1336 template<int sh_type, bool dynamic, int size, bool big_endian>
1337 class Output_data_reloc;
1339 // The SHT_REL version of Output_data_reloc.
1341 template<bool dynamic, int size, bool big_endian>
1342 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1343 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1346 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1350 typedef typename Base::Output_reloc_type Output_reloc_type;
1351 typedef typename Output_reloc_type::Address Address;
1353 Output_data_reloc(bool sr)
1354 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1357 // Add a reloc against a global symbol.
1360 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1361 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1364 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1365 Sized_relobj<size, big_endian>* relobj,
1366 unsigned int shndx, Address address)
1367 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1370 // These are to simplify the Copy_relocs class.
1373 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1376 gold_assert(addend == 0);
1377 this->add_global(gsym, type, od, address);
1381 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1382 Sized_relobj<size, big_endian>* relobj,
1383 unsigned int shndx, Address address, Address addend)
1385 gold_assert(addend == 0);
1386 this->add_global(gsym, type, od, relobj, shndx, address);
1389 // Add a RELATIVE reloc against a global symbol. The final relocation
1390 // will not reference the symbol.
1393 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1395 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1398 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1399 Sized_relobj<size, big_endian>* relobj,
1400 unsigned int shndx, Address address)
1402 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1406 // Add a reloc against a local symbol.
1409 add_local(Sized_relobj<size, big_endian>* relobj,
1410 unsigned int local_sym_index, unsigned int type,
1411 Output_data* od, Address address)
1413 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1414 address, false, false));
1418 add_local(Sized_relobj<size, big_endian>* relobj,
1419 unsigned int local_sym_index, unsigned int type,
1420 Output_data* od, unsigned int shndx, Address address)
1422 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1423 address, false, false));
1426 // Add a RELATIVE reloc against a local symbol.
1429 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1430 unsigned int local_sym_index, unsigned int type,
1431 Output_data* od, Address address)
1433 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1434 address, true, false));
1438 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1439 unsigned int local_sym_index, unsigned int type,
1440 Output_data* od, unsigned int shndx, Address address)
1442 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1443 address, true, false));
1446 // Add a reloc against a local section symbol. This will be
1447 // converted into a reloc against the STT_SECTION symbol of the
1451 add_local_section(Sized_relobj<size, big_endian>* relobj,
1452 unsigned int input_shndx, unsigned int type,
1453 Output_data* od, Address address)
1455 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1456 address, false, true));
1460 add_local_section(Sized_relobj<size, big_endian>* relobj,
1461 unsigned int input_shndx, unsigned int type,
1462 Output_data* od, unsigned int shndx, Address address)
1464 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1465 address, false, true));
1468 // A reloc against the STT_SECTION symbol of an output section.
1469 // OS is the Output_section that the relocation refers to; OD is
1470 // the Output_data object being relocated.
1473 add_output_section(Output_section* os, unsigned int type,
1474 Output_data* od, Address address)
1475 { this->add(od, Output_reloc_type(os, type, od, address)); }
1478 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1479 Sized_relobj<size, big_endian>* relobj,
1480 unsigned int shndx, Address address)
1481 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1484 // The SHT_RELA version of Output_data_reloc.
1486 template<bool dynamic, int size, bool big_endian>
1487 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1488 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1491 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1495 typedef typename Base::Output_reloc_type Output_reloc_type;
1496 typedef typename Output_reloc_type::Address Address;
1497 typedef typename Output_reloc_type::Addend Addend;
1499 Output_data_reloc(bool sr)
1500 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1503 // Add a reloc against a global symbol.
1506 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1507 Address address, Addend addend)
1508 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1512 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1513 Sized_relobj<size, big_endian>* relobj,
1514 unsigned int shndx, Address address,
1516 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1519 // Add a RELATIVE reloc against a global symbol. The final output
1520 // relocation will not reference the symbol, but we must keep the symbol
1521 // information long enough to set the addend of the relocation correctly
1522 // when it is written.
1525 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1526 Address address, Addend addend)
1527 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1530 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1531 Sized_relobj<size, big_endian>* relobj,
1532 unsigned int shndx, Address address, Addend addend)
1533 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1536 // Add a reloc against a local symbol.
1539 add_local(Sized_relobj<size, big_endian>* relobj,
1540 unsigned int local_sym_index, unsigned int type,
1541 Output_data* od, Address address, Addend addend)
1543 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1544 addend, false, false));
1548 add_local(Sized_relobj<size, big_endian>* relobj,
1549 unsigned int local_sym_index, unsigned int type,
1550 Output_data* od, unsigned int shndx, Address address,
1553 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1554 address, addend, false, false));
1557 // Add a RELATIVE reloc against a local symbol.
1560 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1561 unsigned int local_sym_index, unsigned int type,
1562 Output_data* od, Address address, Addend addend)
1564 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1565 addend, true, false));
1569 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1570 unsigned int local_sym_index, unsigned int type,
1571 Output_data* od, unsigned int shndx, Address address,
1574 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1575 address, addend, true, false));
1578 // Add a reloc against a local section symbol. This will be
1579 // converted into a reloc against the STT_SECTION symbol of the
1583 add_local_section(Sized_relobj<size, big_endian>* relobj,
1584 unsigned int input_shndx, unsigned int type,
1585 Output_data* od, Address address, Addend addend)
1587 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1588 addend, false, true));
1592 add_local_section(Sized_relobj<size, big_endian>* relobj,
1593 unsigned int input_shndx, unsigned int type,
1594 Output_data* od, unsigned int shndx, Address address,
1597 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1598 address, addend, false, true));
1601 // A reloc against the STT_SECTION symbol of an output section.
1604 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1605 Address address, Addend addend)
1606 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1609 add_output_section(Output_section* os, unsigned int type,
1610 Sized_relobj<size, big_endian>* relobj,
1611 unsigned int shndx, Address address, Addend addend)
1612 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1616 // Output_relocatable_relocs represents a relocation section in a
1617 // relocatable link. The actual data is written out in the target
1618 // hook relocate_for_relocatable. This just saves space for it.
1620 template<int sh_type, int size, bool big_endian>
1621 class Output_relocatable_relocs : public Output_section_data
1624 Output_relocatable_relocs(Relocatable_relocs* rr)
1625 : Output_section_data(Output_data::default_alignment_for_size(size)),
1630 set_final_data_size();
1632 // Write out the data. There is nothing to do here.
1634 do_write(Output_file*)
1637 // Write to a map file.
1639 do_print_to_mapfile(Mapfile* mapfile) const
1640 { mapfile->print_output_data(this, _("** relocs")); }
1643 // The relocs associated with this input section.
1644 Relocatable_relocs* rr_;
1647 // Handle a GROUP section.
1649 template<int size, bool big_endian>
1650 class Output_data_group : public Output_section_data
1653 // The constructor clears *INPUT_SHNDXES.
1654 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1655 section_size_type entry_count,
1656 elfcpp::Elf_Word flags,
1657 std::vector<unsigned int>* input_shndxes);
1660 do_write(Output_file*);
1662 // Write to a map file.
1664 do_print_to_mapfile(Mapfile* mapfile) const
1665 { mapfile->print_output_data(this, _("** group")); }
1667 // Set final data size.
1669 set_final_data_size()
1670 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1673 // The input object.
1674 Sized_relobj<size, big_endian>* relobj_;
1675 // The group flag word.
1676 elfcpp::Elf_Word flags_;
1677 // The section indexes of the input sections in this group.
1678 std::vector<unsigned int> input_shndxes_;
1681 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1682 // for one symbol--either a global symbol or a local symbol in an
1683 // object. The target specific code adds entries to the GOT as
1686 template<int size, bool big_endian>
1687 class Output_data_got : public Output_section_data_build
1690 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1691 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1692 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1695 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1699 // Add an entry for a global symbol to the GOT. Return true if this
1700 // is a new GOT entry, false if the symbol was already in the GOT.
1702 add_global(Symbol* gsym, unsigned int got_type);
1704 // Add an entry for a global symbol to the GOT, and add a dynamic
1705 // relocation of type R_TYPE for the GOT entry.
1707 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1708 Rel_dyn* rel_dyn, unsigned int r_type);
1711 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1712 Rela_dyn* rela_dyn, unsigned int r_type);
1714 // Add a pair of entries for a global symbol to the GOT, and add
1715 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1717 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1718 Rel_dyn* rel_dyn, unsigned int r_type_1,
1719 unsigned int r_type_2);
1722 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1723 Rela_dyn* rela_dyn, unsigned int r_type_1,
1724 unsigned int r_type_2);
1726 // Add an entry for a local symbol to the GOT. This returns true if
1727 // this is a new GOT entry, false if the symbol already has a GOT
1730 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1731 unsigned int got_type);
1733 // Add an entry for a local symbol to the GOT, and add a dynamic
1734 // relocation of type R_TYPE for the GOT entry.
1736 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1737 unsigned int sym_index, unsigned int got_type,
1738 Rel_dyn* rel_dyn, unsigned int r_type);
1741 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1742 unsigned int sym_index, unsigned int got_type,
1743 Rela_dyn* rela_dyn, unsigned int r_type);
1745 // Add a pair of entries for a local symbol to the GOT, and add
1746 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1748 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1749 unsigned int sym_index, unsigned int shndx,
1750 unsigned int got_type, Rel_dyn* rel_dyn,
1751 unsigned int r_type_1, unsigned int r_type_2);
1754 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1755 unsigned int sym_index, unsigned int shndx,
1756 unsigned int got_type, Rela_dyn* rela_dyn,
1757 unsigned int r_type_1, unsigned int r_type_2);
1759 // Add a constant to the GOT. This returns the offset of the new
1760 // entry from the start of the GOT.
1762 add_constant(Valtype constant)
1764 this->entries_.push_back(Got_entry(constant));
1765 this->set_got_size();
1766 return this->last_got_offset();
1770 // Write out the GOT table.
1772 do_write(Output_file*);
1774 // Write to a map file.
1776 do_print_to_mapfile(Mapfile* mapfile) const
1777 { mapfile->print_output_data(this, _("** GOT")); }
1780 // This POD class holds a single GOT entry.
1784 // Create a zero entry.
1786 : local_sym_index_(CONSTANT_CODE)
1787 { this->u_.constant = 0; }
1789 // Create a global symbol entry.
1790 explicit Got_entry(Symbol* gsym)
1791 : local_sym_index_(GSYM_CODE)
1792 { this->u_.gsym = gsym; }
1794 // Create a local symbol entry.
1795 Got_entry(Sized_relobj<size, big_endian>* object,
1796 unsigned int local_sym_index)
1797 : local_sym_index_(local_sym_index)
1799 gold_assert(local_sym_index != GSYM_CODE
1800 && local_sym_index != CONSTANT_CODE);
1801 this->u_.object = object;
1804 // Create a constant entry. The constant is a host value--it will
1805 // be swapped, if necessary, when it is written out.
1806 explicit Got_entry(Valtype constant)
1807 : local_sym_index_(CONSTANT_CODE)
1808 { this->u_.constant = constant; }
1810 // Write the GOT entry to an output view.
1812 write(unsigned char* pov) const;
1823 // For a local symbol, the object.
1824 Sized_relobj<size, big_endian>* object;
1825 // For a global symbol, the symbol.
1827 // For a constant, the constant.
1830 // For a local symbol, the local symbol index. This is GSYM_CODE
1831 // for a global symbol, or CONSTANT_CODE for a constant.
1832 unsigned int local_sym_index_;
1835 typedef std::vector<Got_entry> Got_entries;
1837 // Return the offset into the GOT of GOT entry I.
1839 got_offset(unsigned int i) const
1840 { return i * (size / 8); }
1842 // Return the offset into the GOT of the last entry added.
1844 last_got_offset() const
1845 { return this->got_offset(this->entries_.size() - 1); }
1847 // Set the size of the section.
1850 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1852 // The list of GOT entries.
1853 Got_entries entries_;
1856 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1859 class Output_data_dynamic : public Output_section_data
1862 Output_data_dynamic(Stringpool* pool)
1863 : Output_section_data(Output_data::default_alignment()),
1864 entries_(), pool_(pool)
1867 // Add a new dynamic entry with a fixed numeric value.
1869 add_constant(elfcpp::DT tag, unsigned int val)
1870 { this->add_entry(Dynamic_entry(tag, val)); }
1872 // Add a new dynamic entry with the address of output data.
1874 add_section_address(elfcpp::DT tag, const Output_data* od)
1875 { this->add_entry(Dynamic_entry(tag, od, false)); }
1877 // Add a new dynamic entry with the address of output data
1878 // plus a constant offset.
1880 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1881 unsigned int offset)
1882 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1884 // Add a new dynamic entry with the size of output data.
1886 add_section_size(elfcpp::DT tag, const Output_data* od)
1887 { this->add_entry(Dynamic_entry(tag, od, true)); }
1889 // Add a new dynamic entry with the address of a symbol.
1891 add_symbol(elfcpp::DT tag, const Symbol* sym)
1892 { this->add_entry(Dynamic_entry(tag, sym)); }
1894 // Add a new dynamic entry with a string.
1896 add_string(elfcpp::DT tag, const char* str)
1897 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1900 add_string(elfcpp::DT tag, const std::string& str)
1901 { this->add_string(tag, str.c_str()); }
1904 // Adjust the output section to set the entry size.
1906 do_adjust_output_section(Output_section*);
1908 // Set the final data size.
1910 set_final_data_size();
1912 // Write out the dynamic entries.
1914 do_write(Output_file*);
1916 // Write to a map file.
1918 do_print_to_mapfile(Mapfile* mapfile) const
1919 { mapfile->print_output_data(this, _("** dynamic")); }
1922 // This POD class holds a single dynamic entry.
1926 // Create an entry with a fixed numeric value.
1927 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1928 : tag_(tag), offset_(DYNAMIC_NUMBER)
1929 { this->u_.val = val; }
1931 // Create an entry with the size or address of a section.
1932 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1934 offset_(section_size
1935 ? DYNAMIC_SECTION_SIZE
1936 : DYNAMIC_SECTION_ADDRESS)
1937 { this->u_.od = od; }
1939 // Create an entry with the address of a section plus a constant offset.
1940 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1943 { this->u_.od = od; }
1945 // Create an entry with the address of a symbol.
1946 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1947 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1948 { this->u_.sym = sym; }
1950 // Create an entry with a string.
1951 Dynamic_entry(elfcpp::DT tag, const char* str)
1952 : tag_(tag), offset_(DYNAMIC_STRING)
1953 { this->u_.str = str; }
1955 // Return the tag of this entry.
1958 { return this->tag_; }
1960 // Write the dynamic entry to an output view.
1961 template<int size, bool big_endian>
1963 write(unsigned char* pov, const Stringpool*) const;
1966 // Classification is encoded in the OFFSET field.
1970 DYNAMIC_SECTION_ADDRESS = 0,
1972 DYNAMIC_NUMBER = -1U,
1974 DYNAMIC_SECTION_SIZE = -2U,
1976 DYNAMIC_SYMBOL = -3U,
1978 DYNAMIC_STRING = -4U
1979 // Any other value indicates a section address plus OFFSET.
1984 // For DYNAMIC_NUMBER.
1986 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1987 const Output_data* od;
1988 // For DYNAMIC_SYMBOL.
1990 // For DYNAMIC_STRING.
1995 // The type of entry (Classification) or offset within a section.
1996 unsigned int offset_;
1999 // Add an entry to the list.
2001 add_entry(const Dynamic_entry& entry)
2002 { this->entries_.push_back(entry); }
2004 // Sized version of write function.
2005 template<int size, bool big_endian>
2007 sized_write(Output_file* of);
2009 // The type of the list of entries.
2010 typedef std::vector<Dynamic_entry> Dynamic_entries;
2013 Dynamic_entries entries_;
2014 // The pool used for strings.
2018 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2019 // which may be required if the object file has more than
2020 // SHN_LORESERVE sections.
2022 class Output_symtab_xindex : public Output_section_data
2025 Output_symtab_xindex(size_t symcount)
2026 : Output_section_data(symcount * 4, 4, true),
2030 // Add an entry: symbol number SYMNDX has section SHNDX.
2032 add(unsigned int symndx, unsigned int shndx)
2033 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2037 do_write(Output_file*);
2039 // Write to a map file.
2041 do_print_to_mapfile(Mapfile* mapfile) const
2042 { mapfile->print_output_data(this, _("** symtab xindex")); }
2045 template<bool big_endian>
2047 endian_do_write(unsigned char*);
2049 // It is likely that most symbols will not require entries. Rather
2050 // than keep a vector for all symbols, we keep pairs of symbol index
2051 // and section index.
2052 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2054 // The entries we need.
2055 Xindex_entries entries_;
2058 // A relaxed input section.
2059 class Output_relaxed_input_section : public Output_section_data_build
2062 // We would like to call relobj->section_addralign(shndx) to get the
2063 // alignment but we do not want the constructor to fail. So callers
2064 // are repsonsible for ensuring that.
2065 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2067 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2070 // Return the Relobj of this relaxed input section.
2073 { return this->relobj_; }
2075 // Return the section index of this relaxed input section.
2078 { return this->shndx_; }
2082 unsigned int shndx_;
2085 // An output section. We don't expect to have too many output
2086 // sections, so we don't bother to do a template on the size.
2088 class Output_section : public Output_data
2091 // Create an output section, giving the name, type, and flags.
2092 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2093 virtual ~Output_section();
2095 // Add a new input section SHNDX, named NAME, with header SHDR, from
2096 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2097 // which applies to this section, or 0 if none, or -1 if more than
2098 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2099 // in a linker script; in that case we need to keep track of input
2100 // sections associated with an output section. Return the offset
2101 // within the output section.
2102 template<int size, bool big_endian>
2104 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2106 const elfcpp::Shdr<size, big_endian>& shdr,
2107 unsigned int reloc_shndx, bool have_sections_script);
2109 // Add generated data POSD to this output section.
2111 add_output_section_data(Output_section_data* posd);
2113 // Add a relaxed input section PORIS to this output section.
2115 add_relaxed_input_section(Output_relaxed_input_section* poris);
2117 // Return the section name.
2120 { return this->name_; }
2122 // Return the section type.
2125 { return this->type_; }
2127 // Return the section flags.
2130 { return this->flags_; }
2132 // Update the output section flags based on input section flags.
2134 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2136 // Return the entsize field.
2139 { return this->entsize_; }
2141 // Set the entsize field.
2143 set_entsize(uint64_t v);
2145 // Set the load address.
2147 set_load_address(uint64_t load_address)
2149 this->load_address_ = load_address;
2150 this->has_load_address_ = true;
2153 // Set the link field to the output section index of a section.
2155 set_link_section(const Output_data* od)
2157 gold_assert(this->link_ == 0
2158 && !this->should_link_to_symtab_
2159 && !this->should_link_to_dynsym_);
2160 this->link_section_ = od;
2163 // Set the link field to a constant.
2165 set_link(unsigned int v)
2167 gold_assert(this->link_section_ == NULL
2168 && !this->should_link_to_symtab_
2169 && !this->should_link_to_dynsym_);
2173 // Record that this section should link to the normal symbol table.
2175 set_should_link_to_symtab()
2177 gold_assert(this->link_section_ == NULL
2179 && !this->should_link_to_dynsym_);
2180 this->should_link_to_symtab_ = true;
2183 // Record that this section should link to the dynamic symbol table.
2185 set_should_link_to_dynsym()
2187 gold_assert(this->link_section_ == NULL
2189 && !this->should_link_to_symtab_);
2190 this->should_link_to_dynsym_ = true;
2193 // Return the info field.
2197 gold_assert(this->info_section_ == NULL
2198 && this->info_symndx_ == NULL);
2202 // Set the info field to the output section index of a section.
2204 set_info_section(const Output_section* os)
2206 gold_assert((this->info_section_ == NULL
2207 || (this->info_section_ == os
2208 && this->info_uses_section_index_))
2209 && this->info_symndx_ == NULL
2210 && this->info_ == 0);
2211 this->info_section_ = os;
2212 this->info_uses_section_index_= true;
2215 // Set the info field to the symbol table index of a symbol.
2217 set_info_symndx(const Symbol* sym)
2219 gold_assert(this->info_section_ == NULL
2220 && (this->info_symndx_ == NULL
2221 || this->info_symndx_ == sym)
2222 && this->info_ == 0);
2223 this->info_symndx_ = sym;
2226 // Set the info field to the symbol table index of a section symbol.
2228 set_info_section_symndx(const Output_section* os)
2230 gold_assert((this->info_section_ == NULL
2231 || (this->info_section_ == os
2232 && !this->info_uses_section_index_))
2233 && this->info_symndx_ == NULL
2234 && this->info_ == 0);
2235 this->info_section_ = os;
2236 this->info_uses_section_index_ = false;
2239 // Set the info field to a constant.
2241 set_info(unsigned int v)
2243 gold_assert(this->info_section_ == NULL
2244 && this->info_symndx_ == NULL
2245 && (this->info_ == 0
2246 || this->info_ == v));
2250 // Set the addralign field.
2252 set_addralign(uint64_t v)
2253 { this->addralign_ = v; }
2255 // Whether the output section index has been set.
2257 has_out_shndx() const
2258 { return this->out_shndx_ != -1U; }
2260 // Indicate that we need a symtab index.
2262 set_needs_symtab_index()
2263 { this->needs_symtab_index_ = true; }
2265 // Return whether we need a symtab index.
2267 needs_symtab_index() const
2268 { return this->needs_symtab_index_; }
2270 // Get the symtab index.
2272 symtab_index() const
2274 gold_assert(this->symtab_index_ != 0);
2275 return this->symtab_index_;
2278 // Set the symtab index.
2280 set_symtab_index(unsigned int index)
2282 gold_assert(index != 0);
2283 this->symtab_index_ = index;
2286 // Indicate that we need a dynsym index.
2288 set_needs_dynsym_index()
2289 { this->needs_dynsym_index_ = true; }
2291 // Return whether we need a dynsym index.
2293 needs_dynsym_index() const
2294 { return this->needs_dynsym_index_; }
2296 // Get the dynsym index.
2298 dynsym_index() const
2300 gold_assert(this->dynsym_index_ != 0);
2301 return this->dynsym_index_;
2304 // Set the dynsym index.
2306 set_dynsym_index(unsigned int index)
2308 gold_assert(index != 0);
2309 this->dynsym_index_ = index;
2312 // Return whether the input sections sections attachd to this output
2313 // section may require sorting. This is used to handle constructor
2314 // priorities compatibly with GNU ld.
2316 may_sort_attached_input_sections() const
2317 { return this->may_sort_attached_input_sections_; }
2319 // Record that the input sections attached to this output section
2320 // may require sorting.
2322 set_may_sort_attached_input_sections()
2323 { this->may_sort_attached_input_sections_ = true; }
2325 // Return whether the input sections attached to this output section
2326 // require sorting. This is used to handle constructor priorities
2327 // compatibly with GNU ld.
2329 must_sort_attached_input_sections() const
2330 { return this->must_sort_attached_input_sections_; }
2332 // Record that the input sections attached to this output section
2335 set_must_sort_attached_input_sections()
2336 { this->must_sort_attached_input_sections_ = true; }
2338 // Return whether this section holds relro data--data which has
2339 // dynamic relocations but which may be marked read-only after the
2340 // dynamic relocations have been completed.
2343 { return this->is_relro_; }
2345 // Record that this section holds relro data.
2348 { this->is_relro_ = true; }
2350 // Record that this section does not hold relro data.
2353 { this->is_relro_ = false; }
2355 // True if this section holds relro local data--relro data for which
2356 // the dynamic relocations are all RELATIVE relocations.
2358 is_relro_local() const
2359 { return this->is_relro_local_; }
2361 // Record that this section holds relro local data.
2363 set_is_relro_local()
2364 { this->is_relro_local_ = true; }
2366 // True if this must be the last relro section.
2368 is_last_relro() const
2369 { return this->is_last_relro_; }
2371 // Record that this must be the last relro section.
2375 gold_assert(this->is_relro_);
2376 this->is_last_relro_ = true;
2379 // True if this must be the first section following the relro sections.
2381 is_first_non_relro() const
2383 gold_assert(!this->is_relro_);
2384 return this->is_first_non_relro_;
2387 // Record that this must be the first non-relro section.
2389 set_is_first_non_relro()
2391 gold_assert(!this->is_relro_);
2392 this->is_first_non_relro_ = true;
2395 // True if this is a small section: a section which holds small
2398 is_small_section() const
2399 { return this->is_small_section_; }
2401 // Record that this is a small section.
2403 set_is_small_section()
2404 { this->is_small_section_ = true; }
2406 // True if this is a large section: a section which holds large
2409 is_large_section() const
2410 { return this->is_large_section_; }
2412 // Record that this is a large section.
2414 set_is_large_section()
2415 { this->is_large_section_ = true; }
2417 // True if this is a large data (not BSS) section.
2419 is_large_data_section()
2420 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2422 // True if this is the .interp section which goes into the PT_INTERP
2426 { return this->is_interp_; }
2428 // Record that this is the interp section.
2431 { this->is_interp_ = true; }
2433 // True if this is a section used by the dynamic linker.
2435 is_dynamic_linker_section() const
2436 { return this->is_dynamic_linker_section_; }
2438 // Record that this is a section used by the dynamic linker.
2440 set_is_dynamic_linker_section()
2441 { this->is_dynamic_linker_section_ = true; }
2443 // Return whether this section should be written after all the input
2444 // sections are complete.
2446 after_input_sections() const
2447 { return this->after_input_sections_; }
2449 // Record that this section should be written after all the input
2450 // sections are complete.
2452 set_after_input_sections()
2453 { this->after_input_sections_ = true; }
2455 // Return whether this section requires postprocessing after all
2456 // relocations have been applied.
2458 requires_postprocessing() const
2459 { return this->requires_postprocessing_; }
2461 // If a section requires postprocessing, return the buffer to use.
2463 postprocessing_buffer() const
2465 gold_assert(this->postprocessing_buffer_ != NULL);
2466 return this->postprocessing_buffer_;
2469 // If a section requires postprocessing, create the buffer to use.
2471 create_postprocessing_buffer();
2473 // If a section requires postprocessing, this is the size of the
2474 // buffer to which relocations should be applied.
2476 postprocessing_buffer_size() const
2477 { return this->current_data_size_for_child(); }
2479 // Modify the section name. This is only permitted for an
2480 // unallocated section, and only before the size has been finalized.
2481 // Otherwise the name will not get into Layout::namepool_.
2483 set_name(const char* newname)
2485 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2486 gold_assert(!this->is_data_size_valid());
2487 this->name_ = newname;
2490 // Return whether the offset OFFSET in the input section SHNDX in
2491 // object OBJECT is being included in the link.
2493 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2494 off_t offset) const;
2496 // Return the offset within the output section of OFFSET relative to
2497 // the start of input section SHNDX in object OBJECT.
2499 output_offset(const Relobj* object, unsigned int shndx,
2500 section_offset_type offset) const;
2502 // Return the output virtual address of OFFSET relative to the start
2503 // of input section SHNDX in object OBJECT.
2505 output_address(const Relobj* object, unsigned int shndx,
2506 off_t offset) const;
2508 // Look for the merged section for input section SHNDX in object
2509 // OBJECT. If found, return true, and set *ADDR to the address of
2510 // the start of the merged section. This is not necessary the
2511 // output offset corresponding to input offset 0 in the section,
2512 // since the section may be mapped arbitrarily.
2514 find_starting_output_address(const Relobj* object, unsigned int shndx,
2515 uint64_t* addr) const;
2517 // Record that this output section was found in the SECTIONS clause
2518 // of a linker script.
2520 set_found_in_sections_clause()
2521 { this->found_in_sections_clause_ = true; }
2523 // Return whether this output section was found in the SECTIONS
2524 // clause of a linker script.
2526 found_in_sections_clause() const
2527 { return this->found_in_sections_clause_; }
2529 // Write the section header into *OPHDR.
2530 template<int size, bool big_endian>
2532 write_header(const Layout*, const Stringpool*,
2533 elfcpp::Shdr_write<size, big_endian>*) const;
2535 // The next few calls are for linker script support.
2537 // We need to export the input sections to linker scripts. Previously
2538 // we export a pair of Relobj pointer and section index. We now need to
2539 // handle relaxed input sections as well. So we use this class.
2540 class Simple_input_section
2543 static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2546 Simple_input_section(Relobj *relobj, unsigned int shndx)
2549 gold_assert(shndx != invalid_shndx);
2550 this->u_.relobj = relobj;
2553 Simple_input_section(Output_relaxed_input_section* section)
2554 : shndx_(invalid_shndx)
2555 { this->u_.relaxed_input_section = section; }
2557 // Whether this is a relaxed section.
2559 is_relaxed_input_section() const
2560 { return this->shndx_ == invalid_shndx; }
2562 // Return object of an input section.
2566 return ((this->shndx_ != invalid_shndx)
2568 : this->u_.relaxed_input_section->relobj());
2571 // Return index of an input section.
2575 return ((this->shndx_ != invalid_shndx)
2577 : this->u_.relaxed_input_section->shndx());
2580 // Return the Output_relaxed_input_section object of a relaxed section.
2581 Output_relaxed_input_section*
2582 relaxed_input_section() const
2584 gold_assert(this->shndx_ == invalid_shndx);
2585 return this->u_.relaxed_input_section;
2589 // Pointer to either an Relobj or an Output_relaxed_input_section.
2593 Output_relaxed_input_section* relaxed_input_section;
2595 // Section index for an non-relaxed section or invalid_shndx for
2596 // a relaxed section.
2597 unsigned int shndx_;
2600 // Store the list of input sections for this Output_section into the
2601 // list passed in. This removes the input sections, leaving only
2602 // any Output_section_data elements. This returns the size of those
2603 // Output_section_data elements. ADDRESS is the address of this
2604 // output section. FILL is the fill value to use, in case there are
2605 // any spaces between the remaining Output_section_data elements.
2607 get_input_sections(uint64_t address, const std::string& fill,
2608 std::list<Simple_input_section>*);
2610 // Add an input section from a script.
2612 add_input_section_for_script(const Simple_input_section& input_section,
2613 off_t data_size, uint64_t addralign);
2615 // Set the current size of the output section.
2617 set_current_data_size(off_t size)
2618 { this->set_current_data_size_for_child(size); }
2620 // Get the current size of the output section.
2622 current_data_size() const
2623 { return this->current_data_size_for_child(); }
2625 // End of linker script support.
2627 // Save states before doing section layout.
2628 // This is used for relaxation.
2632 // Restore states prior to section layout.
2636 // Convert existing input sections to relaxed input sections.
2638 convert_input_sections_to_relaxed_sections(
2639 const std::vector<Output_relaxed_input_section*>& sections);
2641 // Find a relaxed input section to an input section in OBJECT
2642 // with index SHNDX. Return NULL if none is found.
2643 const Output_relaxed_input_section*
2644 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
2646 // Print merge statistics to stderr.
2648 print_merge_stats();
2651 // Return the output section--i.e., the object itself.
2656 const Output_section*
2657 do_output_section() const
2660 // Return the section index in the output file.
2662 do_out_shndx() const
2664 gold_assert(this->out_shndx_ != -1U);
2665 return this->out_shndx_;
2668 // Set the output section index.
2670 do_set_out_shndx(unsigned int shndx)
2672 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2673 this->out_shndx_ = shndx;
2676 // Set the final data size of the Output_section. For a typical
2677 // Output_section, there is nothing to do, but if there are any
2678 // Output_section_data objects we need to set their final addresses
2681 set_final_data_size();
2683 // Reset the address and file offset.
2685 do_reset_address_and_file_offset();
2687 // Return true if address and file offset already have reset values. In
2688 // other words, calling reset_address_and_file_offset will not change them.
2690 do_address_and_file_offset_have_reset_values() const;
2692 // Write the data to the file. For a typical Output_section, this
2693 // does nothing: the data is written out by calling Object::Relocate
2694 // on each input object. But if there are any Output_section_data
2695 // objects we do need to write them out here.
2697 do_write(Output_file*);
2699 // Return the address alignment--function required by parent class.
2701 do_addralign() const
2702 { return this->addralign_; }
2704 // Return whether there is a load address.
2706 do_has_load_address() const
2707 { return this->has_load_address_; }
2709 // Return the load address.
2711 do_load_address() const
2713 gold_assert(this->has_load_address_);
2714 return this->load_address_;
2717 // Return whether this is an Output_section.
2719 do_is_section() const
2722 // Return whether this is a section of the specified type.
2724 do_is_section_type(elfcpp::Elf_Word type) const
2725 { return this->type_ == type; }
2727 // Return whether the specified section flag is set.
2729 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2730 { return (this->flags_ & flag) != 0; }
2732 // Set the TLS offset. Called only for SHT_TLS sections.
2734 do_set_tls_offset(uint64_t tls_base);
2736 // Return the TLS offset, relative to the base of the TLS segment.
2737 // Valid only for SHT_TLS sections.
2739 do_tls_offset() const
2740 { return this->tls_offset_; }
2742 // This may be implemented by a child class.
2744 do_finalize_name(Layout*)
2747 // Print to the map file.
2749 do_print_to_mapfile(Mapfile*) const;
2751 // Record that this section requires postprocessing after all
2752 // relocations have been applied. This is called by a child class.
2754 set_requires_postprocessing()
2756 this->requires_postprocessing_ = true;
2757 this->after_input_sections_ = true;
2760 // Write all the data of an Output_section into the postprocessing
2763 write_to_postprocessing_buffer();
2765 // In some cases we need to keep a list of the input sections
2766 // associated with this output section. We only need the list if we
2767 // might have to change the offsets of the input section within the
2768 // output section after we add the input section. The ordinary
2769 // input sections will be written out when we process the object
2770 // file, and as such we don't need to track them here. We do need
2771 // to track Output_section_data objects here. We store instances of
2772 // this structure in a std::vector, so it must be a POD. There can
2773 // be many instances of this structure, so we use a union to save
2779 : shndx_(0), p2align_(0)
2781 this->u1_.data_size = 0;
2782 this->u2_.object = NULL;
2785 // For an ordinary input section.
2786 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2789 p2align_(ffsll(static_cast<long long>(addralign)))
2791 gold_assert(shndx != OUTPUT_SECTION_CODE
2792 && shndx != MERGE_DATA_SECTION_CODE
2793 && shndx != MERGE_STRING_SECTION_CODE
2794 && shndx != RELAXED_INPUT_SECTION_CODE);
2795 this->u1_.data_size = data_size;
2796 this->u2_.object = object;
2799 // For a non-merge output section.
2800 Input_section(Output_section_data* posd)
2801 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2803 this->u1_.data_size = 0;
2804 this->u2_.posd = posd;
2807 // For a merge section.
2808 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2810 ? MERGE_STRING_SECTION_CODE
2811 : MERGE_DATA_SECTION_CODE),
2814 this->u1_.entsize = entsize;
2815 this->u2_.posd = posd;
2818 // For a relaxed input section.
2819 Input_section(Output_relaxed_input_section *psection)
2820 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2822 this->u1_.data_size = 0;
2823 this->u2_.poris = psection;
2826 // The required alignment.
2830 if (!this->is_input_section())
2831 return this->u2_.posd->addralign();
2832 return (this->p2align_ == 0
2834 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2837 // Return the required size.
2841 // Whether this is an input section.
2843 is_input_section() const
2845 return (this->shndx_ != OUTPUT_SECTION_CODE
2846 && this->shndx_ != MERGE_DATA_SECTION_CODE
2847 && this->shndx_ != MERGE_STRING_SECTION_CODE
2848 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
2851 // Return whether this is a merge section which matches the
2854 is_merge_section(bool is_string, uint64_t entsize,
2855 uint64_t addralign) const
2857 return (this->shndx_ == (is_string
2858 ? MERGE_STRING_SECTION_CODE
2859 : MERGE_DATA_SECTION_CODE)
2860 && this->u1_.entsize == entsize
2861 && this->addralign() == addralign);
2864 // Return whether this is a relaxed input section.
2866 is_relaxed_input_section() const
2867 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2869 // Return whether this is a generic Output_section_data.
2871 is_output_section_data() const
2873 return this->shndx_ == OUTPUT_SECTION_CODE;
2876 // Return the object for an input section.
2880 if (this->is_input_section())
2881 return this->u2_.object;
2882 else if (this->is_relaxed_input_section())
2883 return this->u2_.poris->relobj();
2888 // Return the input section index for an input section.
2892 if (this->is_input_section())
2893 return this->shndx_;
2894 else if (this->is_relaxed_input_section())
2895 return this->u2_.poris->shndx();
2900 // For non-input-sections, return the associated Output_section_data
2902 Output_section_data*
2903 output_section_data() const
2905 gold_assert(!this->is_input_section());
2906 return this->u2_.posd;
2909 // Return the Output_relaxed_input_section object.
2910 Output_relaxed_input_section*
2911 relaxed_input_section() const
2913 gold_assert(this->is_relaxed_input_section());
2914 return this->u2_.poris;
2917 // Set the output section.
2919 set_output_section(Output_section* os)
2921 gold_assert(!this->is_input_section());
2922 Output_section_data *posd =
2923 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2924 posd->set_output_section(os);
2927 // Set the address and file offset. This is called during
2928 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2929 // the enclosing section.
2931 set_address_and_file_offset(uint64_t address, off_t file_offset,
2932 off_t section_file_offset);
2934 // Reset the address and file offset.
2936 reset_address_and_file_offset();
2938 // Finalize the data size.
2940 finalize_data_size();
2942 // Add an input section, for SHF_MERGE sections.
2944 add_input_section(Relobj* object, unsigned int shndx)
2946 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2947 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2948 return this->u2_.posd->add_input_section(object, shndx);
2951 // Given an input OBJECT, an input section index SHNDX within that
2952 // object, and an OFFSET relative to the start of that input
2953 // section, return whether or not the output offset is known. If
2954 // this function returns true, it sets *POUTPUT to the offset in
2955 // the output section, relative to the start of the input section
2956 // in the output section. *POUTPUT may be different from OFFSET
2957 // for a merged section.
2959 output_offset(const Relobj* object, unsigned int shndx,
2960 section_offset_type offset,
2961 section_offset_type *poutput) const;
2963 // Return whether this is the merge section for the input section
2966 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2968 // Write out the data. This does nothing for an input section.
2970 write(Output_file*);
2972 // Write the data to a buffer. This does nothing for an input
2975 write_to_buffer(unsigned char*);
2977 // Print to a map file.
2979 print_to_mapfile(Mapfile*) const;
2981 // Print statistics about merge sections to stderr.
2983 print_merge_stats(const char* section_name)
2985 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2986 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2987 this->u2_.posd->print_merge_stats(section_name);
2991 // Code values which appear in shndx_. If the value is not one of
2992 // these codes, it is the input section index in the object file.
2995 // An Output_section_data.
2996 OUTPUT_SECTION_CODE = -1U,
2997 // An Output_section_data for an SHF_MERGE section with
2998 // SHF_STRINGS not set.
2999 MERGE_DATA_SECTION_CODE = -2U,
3000 // An Output_section_data for an SHF_MERGE section with
3002 MERGE_STRING_SECTION_CODE = -3U,
3003 // An Output_section_data for a relaxed input section.
3004 RELAXED_INPUT_SECTION_CODE = -4U
3007 // For an ordinary input section, this is the section index in the
3008 // input file. For an Output_section_data, this is
3009 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3010 // MERGE_STRING_SECTION_CODE.
3011 unsigned int shndx_;
3012 // The required alignment, stored as a power of 2.
3013 unsigned int p2align_;
3016 // For an ordinary input section, the section size.
3018 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3019 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3025 // For an ordinary input section, the object which holds the
3028 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3029 // MERGE_STRING_SECTION_CODE, the data.
3030 Output_section_data* posd;
3031 // For RELAXED_INPUT_SECTION_CODE, the data.
3032 Output_relaxed_input_section* poris;
3036 typedef std::vector<Input_section> Input_section_list;
3038 // Allow a child class to access the input sections.
3039 const Input_section_list&
3040 input_sections() const
3041 { return this->input_sections_; }
3044 // We only save enough information to undo the effects of section layout.
3045 class Checkpoint_output_section
3048 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3049 const Input_section_list& input_sections,
3050 off_t first_input_offset,
3051 bool attached_input_sections_are_sorted)
3052 : addralign_(addralign), flags_(flags),
3053 input_sections_(input_sections),
3054 input_sections_size_(input_sections_.size()),
3055 input_sections_copy_(), first_input_offset_(first_input_offset),
3056 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3060 ~Checkpoint_output_section()
3063 // Return the address alignment.
3066 { return this->addralign_; }
3068 // Return the section flags.
3071 { return this->flags_; }
3073 // Return a reference to the input section list copy.
3076 { return &this->input_sections_copy_; }
3078 // Return the size of input_sections at the time when checkpoint is
3081 input_sections_size() const
3082 { return this->input_sections_size_; }
3084 // Whether input sections are copied.
3086 input_sections_saved() const
3087 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3090 first_input_offset() const
3091 { return this->first_input_offset_; }
3094 attached_input_sections_are_sorted() const
3095 { return this->attached_input_sections_are_sorted_; }
3097 // Save input sections.
3099 save_input_sections()
3101 this->input_sections_copy_.reserve(this->input_sections_size_);
3102 this->input_sections_copy_.clear();
3103 Input_section_list::const_iterator p = this->input_sections_.begin();
3104 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3105 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3106 this->input_sections_copy_.push_back(*p);
3110 // The section alignment.
3111 uint64_t addralign_;
3112 // The section flags.
3113 elfcpp::Elf_Xword flags_;
3114 // Reference to the input sections to be checkpointed.
3115 const Input_section_list& input_sections_;
3116 // Size of the checkpointed portion of input_sections_;
3117 size_t input_sections_size_;
3118 // Copy of input sections.
3119 Input_section_list input_sections_copy_;
3120 // The offset of the first entry in input_sections_.
3121 off_t first_input_offset_;
3122 // True if the input sections attached to this output section have
3123 // already been sorted.
3124 bool attached_input_sections_are_sorted_;
3127 // This class is used to sort the input sections.
3128 class Input_section_sort_entry;
3130 // This is the sort comparison function.
3131 struct Input_section_sort_compare
3134 operator()(const Input_section_sort_entry&,
3135 const Input_section_sort_entry&) const;
3138 // Fill data. This is used to fill in data between input sections.
3139 // It is also used for data statements (BYTE, WORD, etc.) in linker
3140 // scripts. When we have to keep track of the input sections, we
3141 // can use an Output_data_const, but we don't want to have to keep
3142 // track of input sections just to implement fills.
3146 Fill(off_t section_offset, off_t length)
3147 : section_offset_(section_offset),
3148 length_(convert_to_section_size_type(length))
3151 // Return section offset.
3153 section_offset() const
3154 { return this->section_offset_; }
3156 // Return fill length.
3159 { return this->length_; }
3162 // The offset within the output section.
3163 off_t section_offset_;
3164 // The length of the space to fill.
3165 section_size_type length_;
3168 typedef std::vector<Fill> Fill_list;
3170 // This class describes properties of merge data sections. It is used
3171 // as a key type for maps.
3172 class Merge_section_properties
3175 Merge_section_properties(bool is_string, uint64_t entsize,
3177 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
3180 // Whether this equals to another Merge_section_properties MSP.
3182 eq(const Merge_section_properties& msp) const
3184 return ((this->is_string_ == msp.is_string_)
3185 && (this->entsize_ == msp.entsize_)
3186 && (this->addralign_ == msp.addralign_));
3189 // Compute a hash value for this using 64-bit FNV-1a hash.
3193 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
3194 uint64_t prime = 1099511628211ULL;
3195 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
3196 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
3197 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3201 // Functors for associative containers.
3205 operator()(const Merge_section_properties& msp1,
3206 const Merge_section_properties& msp2) const
3207 { return msp1.eq(msp2); }
3213 operator()(const Merge_section_properties& msp) const
3214 { return msp.hash_value(); }
3218 // Whether this merge data section is for strings.
3220 // Entsize of this merge data section.
3222 // Address alignment.
3223 uint64_t addralign_;
3226 // Map that link Merge_section_properties to Output_merge_base.
3227 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3228 Merge_section_properties::hash,
3229 Merge_section_properties::equal_to>
3230 Merge_section_by_properties_map;
3232 // Map that link Input_section_specifier to Output_section_data.
3233 typedef Unordered_map<Input_section_specifier, Output_section_data*,
3234 Input_section_specifier::hash,
3235 Input_section_specifier::equal_to>
3236 Output_section_data_by_input_section_map;
3238 // Map that link Input_section_specifier to Output_relaxed_input_section.
3239 typedef Unordered_map<Input_section_specifier, Output_relaxed_input_section*,
3240 Input_section_specifier::hash,
3241 Input_section_specifier::equal_to>
3242 Output_relaxed_input_section_by_input_section_map;
3244 // Map used during relaxation of existing sections. This map
3245 // an input section specifier to an input section list index.
3246 // We assume that Input_section_list is a vector.
3247 typedef Unordered_map<Input_section_specifier, size_t,
3248 Input_section_specifier::hash,
3249 Input_section_specifier::equal_to>
3252 // Add a new output section by Input_section.
3254 add_output_section_data(Input_section*);
3256 // Add an SHF_MERGE input section. Returns true if the section was
3259 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3260 uint64_t entsize, uint64_t addralign);
3262 // Add an output SHF_MERGE section POSD to this output section.
3263 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3264 // ENTSIZE is the entity size. This returns the entry added to
3267 add_output_merge_section(Output_section_data* posd, bool is_string,
3270 // Sort the attached input sections.
3272 sort_attached_input_sections();
3274 // Find the merge section into which an input section with index SHNDX in
3275 // OBJECT has been added. Return NULL if none found.
3276 Output_section_data*
3277 find_merge_section(const Relobj* object, unsigned int shndx) const;
3279 // Build a relaxation map.
3281 build_relaxation_map(
3282 const Input_section_list& input_sections,
3284 Relaxation_map* map) const;
3286 // Convert input sections in an input section list into relaxed sections.
3288 convert_input_sections_in_list_to_relaxed_sections(
3289 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3290 const Relaxation_map& map,
3291 Input_section_list* input_sections);
3293 // Most of these fields are only valid after layout.
3295 // The name of the section. This will point into a Stringpool.
3297 // The section address is in the parent class.
3298 // The section alignment.
3299 uint64_t addralign_;
3300 // The section entry size.
3302 // The load address. This is only used when using a linker script
3303 // with a SECTIONS clause. The has_load_address_ field indicates
3304 // whether this field is valid.
3305 uint64_t load_address_;
3306 // The file offset is in the parent class.
3307 // Set the section link field to the index of this section.
3308 const Output_data* link_section_;
3309 // If link_section_ is NULL, this is the link field.
3311 // Set the section info field to the index of this section.
3312 const Output_section* info_section_;
3313 // If info_section_ is NULL, set the info field to the symbol table
3314 // index of this symbol.
3315 const Symbol* info_symndx_;
3316 // If info_section_ and info_symndx_ are NULL, this is the section
3319 // The section type.
3320 const elfcpp::Elf_Word type_;
3321 // The section flags.
3322 elfcpp::Elf_Xword flags_;
3323 // The section index.
3324 unsigned int out_shndx_;
3325 // If there is a STT_SECTION for this output section in the normal
3326 // symbol table, this is the symbol index. This starts out as zero.
3327 // It is initialized in Layout::finalize() to be the index, or -1U
3328 // if there isn't one.
3329 unsigned int symtab_index_;
3330 // If there is a STT_SECTION for this output section in the dynamic
3331 // symbol table, this is the symbol index. This starts out as zero.
3332 // It is initialized in Layout::finalize() to be the index, or -1U
3333 // if there isn't one.
3334 unsigned int dynsym_index_;
3335 // The input sections. This will be empty in cases where we don't
3336 // need to keep track of them.
3337 Input_section_list input_sections_;
3338 // The offset of the first entry in input_sections_.
3339 off_t first_input_offset_;
3340 // The fill data. This is separate from input_sections_ because we
3341 // often will need fill sections without needing to keep track of
3344 // If the section requires postprocessing, this buffer holds the
3345 // section contents during relocation.
3346 unsigned char* postprocessing_buffer_;
3347 // Whether this output section needs a STT_SECTION symbol in the
3348 // normal symbol table. This will be true if there is a relocation
3350 bool needs_symtab_index_ : 1;
3351 // Whether this output section needs a STT_SECTION symbol in the
3352 // dynamic symbol table. This will be true if there is a dynamic
3353 // relocation which needs it.
3354 bool needs_dynsym_index_ : 1;
3355 // Whether the link field of this output section should point to the
3356 // normal symbol table.
3357 bool should_link_to_symtab_ : 1;
3358 // Whether the link field of this output section should point to the
3359 // dynamic symbol table.
3360 bool should_link_to_dynsym_ : 1;
3361 // Whether this section should be written after all the input
3362 // sections are complete.
3363 bool after_input_sections_ : 1;
3364 // Whether this section requires post processing after all
3365 // relocations have been applied.
3366 bool requires_postprocessing_ : 1;
3367 // Whether an input section was mapped to this output section
3368 // because of a SECTIONS clause in a linker script.
3369 bool found_in_sections_clause_ : 1;
3370 // Whether this section has an explicitly specified load address.
3371 bool has_load_address_ : 1;
3372 // True if the info_section_ field means the section index of the
3373 // section, false if it means the symbol index of the corresponding
3375 bool info_uses_section_index_ : 1;
3376 // True if the input sections attached to this output section may
3378 bool may_sort_attached_input_sections_ : 1;
3379 // True if the input sections attached to this output section must
3381 bool must_sort_attached_input_sections_ : 1;
3382 // True if the input sections attached to this output section have
3383 // already been sorted.
3384 bool attached_input_sections_are_sorted_ : 1;
3385 // True if this section holds relro data.
3387 // True if this section holds relro local data.
3388 bool is_relro_local_ : 1;
3389 // True if this must be the last relro section.
3390 bool is_last_relro_ : 1;
3391 // True if this must be the first section after the relro sections.
3392 bool is_first_non_relro_ : 1;
3393 // True if this is a small section.
3394 bool is_small_section_ : 1;
3395 // True if this is a large section.
3396 bool is_large_section_ : 1;
3397 // True if this is the .interp section going into the PT_INTERP
3399 bool is_interp_ : 1;
3400 // True if this is section is read by the dynamic linker.
3401 bool is_dynamic_linker_section_ : 1;
3402 // Whether code-fills are generated at write.
3403 bool generate_code_fills_at_write_ : 1;
3404 // Whether the entry size field should be zero.
3405 bool is_entsize_zero_ : 1;
3406 // For SHT_TLS sections, the offset of this section relative to the base
3407 // of the TLS segment.
3408 uint64_t tls_offset_;
3409 // Saved checkpoint.
3410 Checkpoint_output_section* checkpoint_;
3411 // Map from input sections to merge sections.
3412 Output_section_data_by_input_section_map merge_section_map_;
3413 // Map from merge section properties to merge_sections;
3414 Merge_section_by_properties_map merge_section_by_properties_map_;
3415 // Map from input sections to relaxed input sections. This is mutable
3416 // because it is updated lazily. We may need to update it in a
3417 // const qualified method.
3418 mutable Output_relaxed_input_section_by_input_section_map
3419 relaxed_input_section_map_;
3420 // Whether relaxed_input_section_map_ is valid.
3421 mutable bool is_relaxed_input_section_map_valid_;
3424 // An output segment. PT_LOAD segments are built from collections of
3425 // output sections. Other segments typically point within PT_LOAD
3426 // segments, and are built directly as needed.
3428 // NOTE: We want to use the copy constructor for this class. During
3429 // relaxation, we may try built the segments multiple times. We do
3430 // that by copying the original segment list before lay-out, doing
3431 // a trial lay-out and roll-back to the saved copied if we need to
3432 // to the lay-out again.
3434 class Output_segment
3437 // Create an output segment, specifying the type and flags.
3438 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3440 // Return the virtual address.
3443 { return this->vaddr_; }
3445 // Return the physical address.
3448 { return this->paddr_; }
3450 // Return the segment type.
3453 { return this->type_; }
3455 // Return the segment flags.
3458 { return this->flags_; }
3460 // Return the memory size.
3463 { return this->memsz_; }
3465 // Return the file size.
3468 { return this->filesz_; }
3470 // Return the file offset.
3473 { return this->offset_; }
3475 // Whether this is a segment created to hold large data sections.
3477 is_large_data_segment() const
3478 { return this->is_large_data_segment_; }
3480 // Record that this is a segment created to hold large data
3483 set_is_large_data_segment()
3484 { this->is_large_data_segment_ = true; }
3486 // Return the maximum alignment of the Output_data.
3488 maximum_alignment();
3490 // Add the Output_section OS to this segment. SEG_FLAGS is the
3491 // segment flags to use. DO_SORT is true if we should sort the
3492 // placement of the input section for more efficient generated code.
3494 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags,
3497 // Remove an Output_section from this segment. It is an error if it
3500 remove_output_section(Output_section* os);
3502 // Add an Output_data (which need not be an Output_section) to the
3503 // start of this segment.
3505 add_initial_output_data(Output_data*);
3507 // Return true if this segment has any sections which hold actual
3508 // data, rather than being a BSS section.
3510 has_any_data_sections() const
3511 { return !this->output_data_.empty(); }
3513 // Return the number of dynamic relocations applied to this segment.
3515 dynamic_reloc_count() const;
3517 // Return the address of the first section.
3519 first_section_load_address() const;
3521 // Return whether the addresses have been set already.
3523 are_addresses_set() const
3524 { return this->are_addresses_set_; }
3526 // Set the addresses.
3528 set_addresses(uint64_t vaddr, uint64_t paddr)
3530 this->vaddr_ = vaddr;
3531 this->paddr_ = paddr;
3532 this->are_addresses_set_ = true;
3535 // Update the flags for the flags of an output section added to this
3538 update_flags_for_output_section(elfcpp::Elf_Xword flags)
3540 // The ELF ABI specifies that a PT_TLS segment should always have
3541 // PF_R as the flags.
3542 if (this->type() != elfcpp::PT_TLS)
3543 this->flags_ |= flags;
3546 // Set the segment flags. This is only used if we have a PHDRS
3547 // clause which explicitly specifies the flags.
3549 set_flags(elfcpp::Elf_Word flags)
3550 { this->flags_ = flags; }
3552 // Set the address of the segment to ADDR and the offset to *POFF
3553 // and set the addresses and offsets of all contained output
3554 // sections accordingly. Set the section indexes of all contained
3555 // output sections starting with *PSHNDX. If RESET is true, first
3556 // reset the addresses of the contained sections. Return the
3557 // address of the immediately following segment. Update *POFF and
3558 // *PSHNDX. This should only be called for a PT_LOAD segment.
3560 set_section_addresses(const Layout*, bool reset, uint64_t addr,
3561 unsigned int increase_relro, off_t* poff,
3562 unsigned int* pshndx);
3564 // Set the minimum alignment of this segment. This may be adjusted
3565 // upward based on the section alignments.
3567 set_minimum_p_align(uint64_t align)
3568 { this->min_p_align_ = align; }
3570 // Set the offset of this segment based on the section. This should
3571 // only be called for a non-PT_LOAD segment.
3573 set_offset(unsigned int increase);
3575 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3579 // Return the number of output sections.
3581 output_section_count() const;
3583 // Return the section attached to the list segment with the lowest
3584 // load address. This is used when handling a PHDRS clause in a
3587 section_with_lowest_load_address() const;
3589 // Write the segment header into *OPHDR.
3590 template<int size, bool big_endian>
3592 write_header(elfcpp::Phdr_write<size, big_endian>*);
3594 // Write the section headers of associated sections into V.
3595 template<int size, bool big_endian>
3597 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3598 unsigned int* pshndx) const;
3600 // Print the output sections in the map file.
3602 print_sections_to_mapfile(Mapfile*) const;
3605 typedef std::list<Output_data*> Output_data_list;
3607 // Find the maximum alignment in an Output_data_list.
3609 maximum_alignment_list(const Output_data_list*);
3611 // Return whether the first data section is a relro section.
3613 is_first_section_relro() const;
3615 // Set the section addresses in an Output_data_list.
3617 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3618 uint64_t addr, off_t* poff, unsigned int* pshndx,
3621 // Return the number of Output_sections in an Output_data_list.
3623 output_section_count_list(const Output_data_list*) const;
3625 // Return the number of dynamic relocs in an Output_data_list.
3627 dynamic_reloc_count_list(const Output_data_list*) const;
3629 // Find the section with the lowest load address in an
3630 // Output_data_list.
3632 lowest_load_address_in_list(const Output_data_list* pdl,
3633 Output_section** found,
3634 uint64_t* found_lma) const;
3636 // Write the section headers in the list into V.
3637 template<int size, bool big_endian>
3639 write_section_headers_list(const Layout*, const Stringpool*,
3640 const Output_data_list*, unsigned char* v,
3641 unsigned int* pshdx) const;
3643 // Print a section list to the mapfile.
3645 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3647 // NOTE: We want to use the copy constructor. Currently, shallow copy
3648 // works for us so we do not need to write our own copy constructor.
3650 // The list of output data with contents attached to this segment.
3651 Output_data_list output_data_;
3652 // The list of output data without contents attached to this segment.
3653 Output_data_list output_bss_;
3654 // The segment virtual address.
3656 // The segment physical address.
3658 // The size of the segment in memory.
3660 // The maximum section alignment. The is_max_align_known_ field
3661 // indicates whether this has been finalized.
3662 uint64_t max_align_;
3663 // The required minimum value for the p_align field. This is used
3664 // for PT_LOAD segments. Note that this does not mean that
3665 // addresses should be aligned to this value; it means the p_paddr
3666 // and p_vaddr fields must be congruent modulo this value. For
3667 // non-PT_LOAD segments, the dynamic linker works more efficiently
3668 // if the p_align field has the more conventional value, although it
3669 // can align as needed.
3670 uint64_t min_p_align_;
3671 // The offset of the segment data within the file.
3673 // The size of the segment data in the file.
3675 // The segment type;
3676 elfcpp::Elf_Word type_;
3677 // The segment flags.
3678 elfcpp::Elf_Word flags_;
3679 // Whether we have finalized max_align_.
3680 bool is_max_align_known_ : 1;
3681 // Whether vaddr and paddr were set by a linker script.
3682 bool are_addresses_set_ : 1;
3683 // Whether this segment holds large data sections.
3684 bool is_large_data_segment_ : 1;
3687 // This class represents the output file.
3692 Output_file(const char* name);
3694 // Indicate that this is a temporary file which should not be
3698 { this->is_temporary_ = true; }
3700 // Try to open an existing file. Returns false if the file doesn't
3701 // exist, has a size of 0 or can't be mmaped. This method is
3704 open_for_modification();
3706 // Open the output file. FILE_SIZE is the final size of the file.
3707 // If the file already exists, it is deleted/truncated. This method
3708 // is thread-unsafe.
3710 open(off_t file_size);
3712 // Resize the output file. This method is thread-unsafe.
3714 resize(off_t file_size);
3716 // Close the output file (flushing all buffered data) and make sure
3717 // there are no errors. This method is thread-unsafe.
3721 // Return the size of this file.
3724 { return this->file_size_; }
3726 // Return the name of this file.
3729 { return this->name_; }
3731 // We currently always use mmap which makes the view handling quite
3732 // simple. In the future we may support other approaches.
3734 // Write data to the output file.
3736 write(off_t offset, const void* data, size_t len)
3737 { memcpy(this->base_ + offset, data, len); }
3739 // Get a buffer to use to write to the file, given the offset into
3740 // the file and the size.
3742 get_output_view(off_t start, size_t size)
3744 gold_assert(start >= 0
3745 && start + static_cast<off_t>(size) <= this->file_size_);
3746 return this->base_ + start;
3749 // VIEW must have been returned by get_output_view. Write the
3750 // buffer to the file, passing in the offset and the size.
3752 write_output_view(off_t, size_t, unsigned char*)
3755 // Get a read/write buffer. This is used when we want to write part
3756 // of the file, read it in, and write it again.
3758 get_input_output_view(off_t start, size_t size)
3759 { return this->get_output_view(start, size); }
3761 // Write a read/write buffer back to the file.
3763 write_input_output_view(off_t, size_t, unsigned char*)
3766 // Get a read buffer. This is used when we just want to read part
3767 // of the file back it in.
3768 const unsigned char*
3769 get_input_view(off_t start, size_t size)
3770 { return this->get_output_view(start, size); }
3772 // Release a read bfufer.
3774 free_input_view(off_t, size_t, const unsigned char*)
3778 // Map the file into memory or, if that fails, allocate anonymous
3783 // Allocate anonymous memory for the file.
3787 // Map the file into memory.
3791 // Unmap the file from memory (and flush to disk buffers).
3801 // Base of file mapped into memory.
3802 unsigned char* base_;
3803 // True iff base_ points to a memory buffer rather than an output file.
3804 bool map_is_anonymous_;
3805 // True if this is a temporary file which should not be output.
3809 } // End namespace gold.
3811 #endif // !defined(GOLD_OUTPUT_H)