1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "reloc-types.h"
37 class General_options
;
41 class Output_merge_base
;
43 class Relocatable_relocs
;
45 template<int size
, bool big_endian
>
47 template<int size
, bool big_endian
>
49 template<int size
, bool big_endian
>
50 class Sized_relobj_file
;
52 // An abtract class for data which has to go into the output file.
57 explicit Output_data()
58 : address_(0), data_size_(0), offset_(-1),
59 is_address_valid_(false), is_data_size_valid_(false),
60 is_offset_valid_(false), is_data_size_fixed_(false),
61 has_dynamic_reloc_(false)
67 // Return the address. For allocated sections, this is only valid
68 // after Layout::finalize is finished.
72 gold_assert(this->is_address_valid_
);
73 return this->address_
;
76 // Return the size of the data. For allocated sections, this must
77 // be valid after Layout::finalize calls set_address, but need not
78 // be valid before then.
82 gold_assert(this->is_data_size_valid_
);
83 return this->data_size_
;
86 // Get the current data size.
88 current_data_size() const
89 { return this->current_data_size_for_child(); }
91 // Return true if data size is fixed.
93 is_data_size_fixed() const
94 { return this->is_data_size_fixed_
; }
96 // Return the file offset. This is only valid after
97 // Layout::finalize is finished. For some non-allocated sections,
98 // it may not be valid until near the end of the link.
102 gold_assert(this->is_offset_valid_
);
103 return this->offset_
;
106 // Reset the address and file offset. This essentially disables the
107 // sanity testing about duplicate and unknown settings.
109 reset_address_and_file_offset()
111 this->is_address_valid_
= false;
112 this->is_offset_valid_
= false;
113 if (!this->is_data_size_fixed_
)
114 this->is_data_size_valid_
= false;
115 this->do_reset_address_and_file_offset();
118 // Return true if address and file offset already have reset values. In
119 // other words, calling reset_address_and_file_offset will not change them.
121 address_and_file_offset_have_reset_values() const
122 { return this->do_address_and_file_offset_have_reset_values(); }
124 // Return the required alignment.
127 { return this->do_addralign(); }
129 // Return whether this has a load address.
131 has_load_address() const
132 { return this->do_has_load_address(); }
134 // Return the load address.
137 { return this->do_load_address(); }
139 // Return whether this is an Output_section.
142 { return this->do_is_section(); }
144 // Return whether this is an Output_section of the specified type.
146 is_section_type(elfcpp::Elf_Word stt
) const
147 { return this->do_is_section_type(stt
); }
149 // Return whether this is an Output_section with the specified flag
152 is_section_flag_set(elfcpp::Elf_Xword shf
) const
153 { return this->do_is_section_flag_set(shf
); }
155 // Return the output section that this goes in, if there is one.
158 { return this->do_output_section(); }
160 const Output_section
*
161 output_section() const
162 { return this->do_output_section(); }
164 // Return the output section index, if there is an output section.
167 { return this->do_out_shndx(); }
169 // Set the output section index, if this is an output section.
171 set_out_shndx(unsigned int shndx
)
172 { this->do_set_out_shndx(shndx
); }
174 // Set the address and file offset of this data, and finalize the
175 // size of the data. This is called during Layout::finalize for
176 // allocated sections.
178 set_address_and_file_offset(uint64_t addr
, off_t off
)
180 this->set_address(addr
);
181 this->set_file_offset(off
);
182 this->finalize_data_size();
187 set_address(uint64_t addr
)
189 gold_assert(!this->is_address_valid_
);
190 this->address_
= addr
;
191 this->is_address_valid_
= true;
194 // Set the file offset.
196 set_file_offset(off_t off
)
198 gold_assert(!this->is_offset_valid_
);
200 this->is_offset_valid_
= true;
203 // Update the data size without finalizing it.
205 pre_finalize_data_size()
207 if (!this->is_data_size_valid_
)
209 // Tell the child class to update the data size.
210 this->update_data_size();
214 // Finalize the data size.
218 if (!this->is_data_size_valid_
)
220 // Tell the child class to set the data size.
221 this->set_final_data_size();
222 gold_assert(this->is_data_size_valid_
);
226 // Set the TLS offset. Called only for SHT_TLS sections.
228 set_tls_offset(uint64_t tls_base
)
229 { this->do_set_tls_offset(tls_base
); }
231 // Return the TLS offset, relative to the base of the TLS segment.
232 // Valid only for SHT_TLS sections.
235 { return this->do_tls_offset(); }
237 // Write the data to the output file. This is called after
238 // Layout::finalize is complete.
240 write(Output_file
* file
)
241 { this->do_write(file
); }
243 // This is called by Layout::finalize to note that the sizes of
244 // allocated sections must now be fixed.
247 { Output_data::allocated_sizes_are_fixed
= true; }
249 // Used to check that layout has been done.
252 { return Output_data::allocated_sizes_are_fixed
; }
254 // Note that a dynamic reloc has been applied to this data.
257 { this->has_dynamic_reloc_
= true; }
259 // Return whether a dynamic reloc has been applied.
261 has_dynamic_reloc() const
262 { return this->has_dynamic_reloc_
; }
264 // Whether the address is valid.
266 is_address_valid() const
267 { return this->is_address_valid_
; }
269 // Whether the file offset is valid.
271 is_offset_valid() const
272 { return this->is_offset_valid_
; }
274 // Whether the data size is valid.
276 is_data_size_valid() const
277 { return this->is_data_size_valid_
; }
279 // Print information to the map file.
281 print_to_mapfile(Mapfile
* mapfile
) const
282 { return this->do_print_to_mapfile(mapfile
); }
285 // Functions that child classes may or in some cases must implement.
287 // Write the data to the output file.
289 do_write(Output_file
*) = 0;
291 // Return the required alignment.
293 do_addralign() const = 0;
295 // Return whether this has a load address.
297 do_has_load_address() const
300 // Return the load address.
302 do_load_address() const
303 { gold_unreachable(); }
305 // Return whether this is an Output_section.
307 do_is_section() const
310 // Return whether this is an Output_section of the specified type.
311 // This only needs to be implement by Output_section.
313 do_is_section_type(elfcpp::Elf_Word
) const
316 // Return whether this is an Output_section with the specific flag
317 // set. This only needs to be implemented by Output_section.
319 do_is_section_flag_set(elfcpp::Elf_Xword
) const
322 // Return the output section, if there is one.
323 virtual Output_section
*
327 virtual const Output_section
*
328 do_output_section() const
331 // Return the output section index, if there is an output section.
334 { gold_unreachable(); }
336 // Set the output section index, if this is an output section.
338 do_set_out_shndx(unsigned int)
339 { gold_unreachable(); }
341 // This is a hook for derived classes to set the preliminary data size.
342 // This is called by pre_finalize_data_size, normally called during
343 // Layout::finalize, before the section address is set, and is used
344 // during an incremental update, when we need to know the size of a
345 // section before allocating space in the output file. For classes
346 // where the current data size is up to date, this default version of
347 // the method can be inherited.
352 // This is a hook for derived classes to set the data size. This is
353 // called by finalize_data_size, normally called during
354 // Layout::finalize, when the section address is set.
356 set_final_data_size()
357 { gold_unreachable(); }
359 // A hook for resetting the address and file offset.
361 do_reset_address_and_file_offset()
364 // Return true if address and file offset already have reset values. In
365 // other words, calling reset_address_and_file_offset will not change them.
366 // A child class overriding do_reset_address_and_file_offset may need to
367 // also override this.
369 do_address_and_file_offset_have_reset_values() const
370 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
372 // Set the TLS offset. Called only for SHT_TLS sections.
374 do_set_tls_offset(uint64_t)
375 { gold_unreachable(); }
377 // Return the TLS offset, relative to the base of the TLS segment.
378 // Valid only for SHT_TLS sections.
380 do_tls_offset() const
381 { gold_unreachable(); }
383 // Print to the map file. This only needs to be implemented by
384 // classes which may appear in a PT_LOAD segment.
386 do_print_to_mapfile(Mapfile
*) const
387 { gold_unreachable(); }
389 // Functions that child classes may call.
391 // Reset the address. The Output_section class needs this when an
392 // SHF_ALLOC input section is added to an output section which was
393 // formerly not SHF_ALLOC.
395 mark_address_invalid()
396 { this->is_address_valid_
= false; }
398 // Set the size of the data.
400 set_data_size(off_t data_size
)
402 gold_assert(!this->is_data_size_valid_
403 && !this->is_data_size_fixed_
);
404 this->data_size_
= data_size
;
405 this->is_data_size_valid_
= true;
408 // Fix the data size. Once it is fixed, it cannot be changed
409 // and the data size remains always valid.
413 gold_assert(this->is_data_size_valid_
);
414 this->is_data_size_fixed_
= true;
417 // Get the current data size--this is for the convenience of
418 // sections which build up their size over time.
420 current_data_size_for_child() const
421 { return this->data_size_
; }
423 // Set the current data size--this is for the convenience of
424 // sections which build up their size over time.
426 set_current_data_size_for_child(off_t data_size
)
428 gold_assert(!this->is_data_size_valid_
);
429 this->data_size_
= data_size
;
432 // Return default alignment for the target size.
436 // Return default alignment for a specified size--32 or 64.
438 default_alignment_for_size(int size
);
441 Output_data(const Output_data
&);
442 Output_data
& operator=(const Output_data
&);
444 // This is used for verification, to make sure that we don't try to
445 // change any sizes of allocated sections after we set the section
447 static bool allocated_sizes_are_fixed
;
449 // Memory address in output file.
451 // Size of data in output file.
453 // File offset of contents in output file.
455 // Whether address_ is valid.
456 bool is_address_valid_
: 1;
457 // Whether data_size_ is valid.
458 bool is_data_size_valid_
: 1;
459 // Whether offset_ is valid.
460 bool is_offset_valid_
: 1;
461 // Whether data size is fixed.
462 bool is_data_size_fixed_
: 1;
463 // Whether any dynamic relocs have been applied to this section.
464 bool has_dynamic_reloc_
: 1;
467 // Output the section headers.
469 class Output_section_headers
: public Output_data
472 Output_section_headers(const Layout
*,
473 const Layout::Segment_list
*,
474 const Layout::Section_list
*,
475 const Layout::Section_list
*,
477 const Output_section
*);
480 // Write the data to the file.
482 do_write(Output_file
*);
484 // Return the required alignment.
487 { return Output_data::default_alignment(); }
489 // Write to a map file.
491 do_print_to_mapfile(Mapfile
* mapfile
) const
492 { mapfile
->print_output_data(this, _("** section headers")); }
494 // Update the data size.
497 { this->set_data_size(this->do_size()); }
499 // Set final data size.
501 set_final_data_size()
502 { this->set_data_size(this->do_size()); }
505 // Write the data to the file with the right size and endianness.
506 template<int size
, bool big_endian
>
508 do_sized_write(Output_file
*);
510 // Compute data size.
514 const Layout
* layout_
;
515 const Layout::Segment_list
* segment_list_
;
516 const Layout::Section_list
* section_list_
;
517 const Layout::Section_list
* unattached_section_list_
;
518 const Stringpool
* secnamepool_
;
519 const Output_section
* shstrtab_section_
;
522 // Output the segment headers.
524 class Output_segment_headers
: public Output_data
527 Output_segment_headers(const Layout::Segment_list
& segment_list
);
530 // Write the data to the file.
532 do_write(Output_file
*);
534 // Return the required alignment.
537 { return Output_data::default_alignment(); }
539 // Write to a map file.
541 do_print_to_mapfile(Mapfile
* mapfile
) const
542 { mapfile
->print_output_data(this, _("** segment headers")); }
544 // Set final data size.
546 set_final_data_size()
547 { this->set_data_size(this->do_size()); }
550 // Write the data to the file with the right size and endianness.
551 template<int size
, bool big_endian
>
553 do_sized_write(Output_file
*);
555 // Compute the current size.
559 const Layout::Segment_list
& segment_list_
;
562 // Output the ELF file header.
564 class Output_file_header
: public Output_data
567 Output_file_header(const Target
*,
569 const Output_segment_headers
*);
571 // Add information about the section headers. We lay out the ELF
572 // file header before we create the section headers.
573 void set_section_info(const Output_section_headers
*,
574 const Output_section
* shstrtab
);
577 // Write the data to the file.
579 do_write(Output_file
*);
581 // Return the required alignment.
584 { return Output_data::default_alignment(); }
586 // Write to a map file.
588 do_print_to_mapfile(Mapfile
* mapfile
) const
589 { mapfile
->print_output_data(this, _("** file header")); }
591 // Set final data size.
593 set_final_data_size(void)
594 { this->set_data_size(this->do_size()); }
597 // Write the data to the file with the right size and endianness.
598 template<int size
, bool big_endian
>
600 do_sized_write(Output_file
*);
602 // Return the value to use for the entry address.
604 typename
elfcpp::Elf_types
<size
>::Elf_Addr
607 // Compute the current data size.
611 const Target
* target_
;
612 const Symbol_table
* symtab_
;
613 const Output_segment_headers
* segment_header_
;
614 const Output_section_headers
* section_header_
;
615 const Output_section
* shstrtab_
;
618 // Output sections are mainly comprised of input sections. However,
619 // there are cases where we have data to write out which is not in an
620 // input section. Output_section_data is used in such cases. This is
621 // an abstract base class.
623 class Output_section_data
: public Output_data
626 Output_section_data(off_t data_size
, uint64_t addralign
,
627 bool is_data_size_fixed
)
628 : Output_data(), output_section_(NULL
), addralign_(addralign
)
630 this->set_data_size(data_size
);
631 if (is_data_size_fixed
)
632 this->fix_data_size();
635 Output_section_data(uint64_t addralign
)
636 : Output_data(), output_section_(NULL
), addralign_(addralign
)
639 // Return the output section.
642 { return this->output_section_
; }
644 const Output_section
*
645 output_section() const
646 { return this->output_section_
; }
648 // Record the output section.
650 set_output_section(Output_section
* os
);
652 // Add an input section, for SHF_MERGE sections. This returns true
653 // if the section was handled.
655 add_input_section(Relobj
* object
, unsigned int shndx
)
656 { return this->do_add_input_section(object
, shndx
); }
658 // Given an input OBJECT, an input section index SHNDX within that
659 // object, and an OFFSET relative to the start of that input
660 // section, return whether or not the corresponding offset within
661 // the output section is known. If this function returns true, it
662 // sets *POUTPUT to the output offset. The value -1 indicates that
663 // this input offset is being discarded.
665 output_offset(const Relobj
* object
, unsigned int shndx
,
666 section_offset_type offset
,
667 section_offset_type
* poutput
) const
668 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
670 // Return whether this is the merge section for the input section
671 // SHNDX in OBJECT. This should return true when output_offset
672 // would return true for some values of OFFSET.
674 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
675 { return this->do_is_merge_section_for(object
, shndx
); }
677 // Write the contents to a buffer. This is used for sections which
678 // require postprocessing, such as compression.
680 write_to_buffer(unsigned char* buffer
)
681 { this->do_write_to_buffer(buffer
); }
683 // Print merge stats to stderr. This should only be called for
684 // SHF_MERGE sections.
686 print_merge_stats(const char* section_name
)
687 { this->do_print_merge_stats(section_name
); }
690 // The child class must implement do_write.
692 // The child class may implement specific adjustments to the output
695 do_adjust_output_section(Output_section
*)
698 // May be implemented by child class. Return true if the section
701 do_add_input_section(Relobj
*, unsigned int)
702 { gold_unreachable(); }
704 // The child class may implement output_offset.
706 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
707 section_offset_type
*) const
710 // The child class may implement is_merge_section_for.
712 do_is_merge_section_for(const Relobj
*, unsigned int) const
715 // The child class may implement write_to_buffer. Most child
716 // classes can not appear in a compressed section, and they do not
719 do_write_to_buffer(unsigned char*)
720 { gold_unreachable(); }
722 // Print merge statistics.
724 do_print_merge_stats(const char*)
725 { gold_unreachable(); }
727 // Return the required alignment.
730 { return this->addralign_
; }
732 // Return the output section.
735 { return this->output_section_
; }
737 const Output_section
*
738 do_output_section() const
739 { return this->output_section_
; }
741 // Return the section index of the output section.
743 do_out_shndx() const;
745 // Set the alignment.
747 set_addralign(uint64_t addralign
);
750 // The output section for this section.
751 Output_section
* output_section_
;
752 // The required alignment.
756 // Some Output_section_data classes build up their data step by step,
757 // rather than all at once. This class provides an interface for
760 class Output_section_data_build
: public Output_section_data
763 Output_section_data_build(uint64_t addralign
)
764 : Output_section_data(addralign
)
767 Output_section_data_build(off_t data_size
, uint64_t addralign
)
768 : Output_section_data(data_size
, addralign
, false)
771 // Set the current data size.
773 set_current_data_size(off_t data_size
)
774 { this->set_current_data_size_for_child(data_size
); }
777 // Set the final data size.
779 set_final_data_size()
780 { this->set_data_size(this->current_data_size_for_child()); }
783 // A simple case of Output_data in which we have constant data to
786 class Output_data_const
: public Output_section_data
789 Output_data_const(const std::string
& data
, uint64_t addralign
)
790 : Output_section_data(data
.size(), addralign
, true), data_(data
)
793 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
794 : Output_section_data(len
, addralign
, true), data_(p
, len
)
797 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
798 : Output_section_data(len
, addralign
, true),
799 data_(reinterpret_cast<const char*>(p
), len
)
803 // Write the data to the output file.
805 do_write(Output_file
*);
807 // Write the data to a buffer.
809 do_write_to_buffer(unsigned char* buffer
)
810 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
812 // Write to a map file.
814 do_print_to_mapfile(Mapfile
* mapfile
) const
815 { mapfile
->print_output_data(this, _("** fill")); }
821 // Another version of Output_data with constant data, in which the
822 // buffer is allocated by the caller.
824 class Output_data_const_buffer
: public Output_section_data
827 Output_data_const_buffer(const unsigned char* p
, off_t len
,
828 uint64_t addralign
, const char* map_name
)
829 : Output_section_data(len
, addralign
, true),
830 p_(p
), map_name_(map_name
)
834 // Write the data the output file.
836 do_write(Output_file
*);
838 // Write the data to a buffer.
840 do_write_to_buffer(unsigned char* buffer
)
841 { memcpy(buffer
, this->p_
, this->data_size()); }
843 // Write to a map file.
845 do_print_to_mapfile(Mapfile
* mapfile
) const
846 { mapfile
->print_output_data(this, _(this->map_name_
)); }
849 // The data to output.
850 const unsigned char* p_
;
851 // Name to use in a map file. Maps are a rarely used feature, but
852 // the space usage is minor as aren't very many of these objects.
853 const char* map_name_
;
856 // A place holder for a fixed amount of data written out via some
859 class Output_data_fixed_space
: public Output_section_data
862 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
863 const char* map_name
)
864 : Output_section_data(data_size
, addralign
, true),
869 // Write out the data--the actual data must be written out
872 do_write(Output_file
*)
875 // Write to a map file.
877 do_print_to_mapfile(Mapfile
* mapfile
) const
878 { mapfile
->print_output_data(this, _(this->map_name_
)); }
881 // Name to use in a map file. Maps are a rarely used feature, but
882 // the space usage is minor as aren't very many of these objects.
883 const char* map_name_
;
886 // A place holder for variable sized data written out via some other
889 class Output_data_space
: public Output_section_data_build
892 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
893 : Output_section_data_build(addralign
),
897 explicit Output_data_space(off_t data_size
, uint64_t addralign
,
898 const char* map_name
)
899 : Output_section_data_build(data_size
, addralign
),
903 // Set the alignment.
905 set_space_alignment(uint64_t align
)
906 { this->set_addralign(align
); }
909 // Write out the data--the actual data must be written out
912 do_write(Output_file
*)
915 // Write to a map file.
917 do_print_to_mapfile(Mapfile
* mapfile
) const
918 { mapfile
->print_output_data(this, _(this->map_name_
)); }
921 // Name to use in a map file. Maps are a rarely used feature, but
922 // the space usage is minor as aren't very many of these objects.
923 const char* map_name_
;
926 // Fill fixed space with zeroes. This is just like
927 // Output_data_fixed_space, except that the map name is known.
929 class Output_data_zero_fill
: public Output_section_data
932 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
933 : Output_section_data(data_size
, addralign
, true)
937 // There is no data to write out.
939 do_write(Output_file
*)
942 // Write to a map file.
944 do_print_to_mapfile(Mapfile
* mapfile
) const
945 { mapfile
->print_output_data(this, "** zero fill"); }
948 // A string table which goes into an output section.
950 class Output_data_strtab
: public Output_section_data
953 Output_data_strtab(Stringpool
* strtab
)
954 : Output_section_data(1), strtab_(strtab
)
958 // This is called to update the section size prior to assigning
959 // the address and file offset.
962 { this->set_final_data_size(); }
964 // This is called to set the address and file offset. Here we make
965 // sure that the Stringpool is finalized.
967 set_final_data_size();
969 // Write out the data.
971 do_write(Output_file
*);
973 // Write the data to a buffer.
975 do_write_to_buffer(unsigned char* buffer
)
976 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
978 // Write to a map file.
980 do_print_to_mapfile(Mapfile
* mapfile
) const
981 { mapfile
->print_output_data(this, _("** string table")); }
987 // This POD class is used to represent a single reloc in the output
988 // file. This could be a private class within Output_data_reloc, but
989 // the templatization is complex enough that I broke it out into a
990 // separate class. The class is templatized on either elfcpp::SHT_REL
991 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
992 // relocation or an ordinary relocation.
994 // A relocation can be against a global symbol, a local symbol, a
995 // local section symbol, an output section, or the undefined symbol at
996 // index 0. We represent the latter by using a NULL global symbol.
998 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1001 template<bool dynamic
, int size
, bool big_endian
>
1002 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1005 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1006 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1008 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1010 // An uninitialized entry. We need this because we want to put
1011 // instances of this class into an STL container.
1013 : local_sym_index_(INVALID_CODE
)
1016 // We have a bunch of different constructors. They come in pairs
1017 // depending on how the address of the relocation is specified. It
1018 // can either be an offset in an Output_data or an offset in an
1021 // A reloc against a global symbol.
1023 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1024 Address address
, bool is_relative
, bool is_symbolless
);
1026 Output_reloc(Symbol
* gsym
, unsigned int type
,
1027 Sized_relobj
<size
, big_endian
>* relobj
,
1028 unsigned int shndx
, Address address
, bool is_relative
,
1029 bool is_symbolless
);
1031 // A reloc against a local symbol or local section symbol.
1033 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1034 unsigned int local_sym_index
, unsigned int type
,
1035 Output_data
* od
, Address address
, bool is_relative
,
1036 bool is_symbolless
, bool is_section_symbol
,
1037 bool use_plt_offset
);
1039 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1040 unsigned int local_sym_index
, unsigned int type
,
1041 unsigned int shndx
, Address address
, bool is_relative
,
1042 bool is_symbolless
, bool is_section_symbol
,
1043 bool use_plt_offset
);
1045 // A reloc against the STT_SECTION symbol of an output section.
1047 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1050 Output_reloc(Output_section
* os
, unsigned int type
,
1051 Sized_relobj
<size
, big_endian
>* relobj
,
1052 unsigned int shndx
, Address address
);
1054 // An absolute relocation with no symbol.
1056 Output_reloc(unsigned int type
, Output_data
* od
, Address address
);
1058 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1059 unsigned int shndx
, Address address
);
1061 // A target specific relocation. The target will be called to get
1062 // the symbol index, passing ARG. The type and offset will be set
1063 // as for other relocation types.
1065 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1068 Output_reloc(unsigned int type
, void* arg
,
1069 Sized_relobj
<size
, big_endian
>* relobj
,
1070 unsigned int shndx
, Address address
);
1072 // Return the reloc type.
1075 { return this->type_
; }
1077 // Return whether this is a RELATIVE relocation.
1080 { return this->is_relative_
; }
1082 // Return whether this is a relocation which should not use
1083 // a symbol, but which obtains its addend from a symbol.
1085 is_symbolless() const
1086 { return this->is_symbolless_
; }
1088 // Return whether this is against a local section symbol.
1090 is_local_section_symbol() const
1092 return (this->local_sym_index_
!= GSYM_CODE
1093 && this->local_sym_index_
!= SECTION_CODE
1094 && this->local_sym_index_
!= INVALID_CODE
1095 && this->local_sym_index_
!= TARGET_CODE
1096 && this->is_section_symbol_
);
1099 // Return whether this is a target specific relocation.
1101 is_target_specific() const
1102 { return this->local_sym_index_
== TARGET_CODE
; }
1104 // Return the argument to pass to the target for a target specific
1109 gold_assert(this->local_sym_index_
== TARGET_CODE
);
1110 return this->u1_
.arg
;
1113 // For a local section symbol, return the offset of the input
1114 // section within the output section. ADDEND is the addend being
1115 // applied to the input section.
1117 local_section_offset(Addend addend
) const;
1119 // Get the value of the symbol referred to by a Rel relocation when
1120 // we are adding the given ADDEND.
1122 symbol_value(Addend addend
) const;
1124 // If this relocation is against an input section, return the
1125 // relocatable object containing the input section.
1126 Sized_relobj
<size
, big_endian
>*
1129 if (this->shndx_
== INVALID_CODE
)
1131 return this->u2_
.relobj
;
1134 // Write the reloc entry to an output view.
1136 write(unsigned char* pov
) const;
1138 // Write the offset and info fields to Write_rel.
1139 template<typename Write_rel
>
1140 void write_rel(Write_rel
*) const;
1142 // This is used when sorting dynamic relocs. Return -1 to sort this
1143 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1145 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1148 // Return whether this reloc should be sorted before the argument
1149 // when sorting dynamic relocs.
1151 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1153 { return this->compare(r2
) < 0; }
1156 // Record that we need a dynamic symbol index.
1158 set_needs_dynsym_index();
1160 // Return the symbol index.
1162 get_symbol_index() const;
1164 // Return the output address.
1166 get_address() const;
1168 // Codes for local_sym_index_.
1177 // Invalid uninitialized entry.
1183 // For a local symbol or local section symbol
1184 // (this->local_sym_index_ >= 0), the object. We will never
1185 // generate a relocation against a local symbol in a dynamic
1186 // object; that doesn't make sense. And our callers will always
1187 // be templatized, so we use Sized_relobj here.
1188 Sized_relobj
<size
, big_endian
>* relobj
;
1189 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1190 // symbol. If this is NULL, it indicates a relocation against the
1191 // undefined 0 symbol.
1193 // For a relocation against an output section
1194 // (this->local_sym_index_ == SECTION_CODE), the output section.
1196 // For a target specific relocation, an argument to pass to the
1202 // If this->shndx_ is not INVALID CODE, the object which holds the
1203 // input section being used to specify the reloc address.
1204 Sized_relobj
<size
, big_endian
>* relobj
;
1205 // If this->shndx_ is INVALID_CODE, the output data being used to
1206 // specify the reloc address. This may be NULL if the reloc
1207 // address is absolute.
1210 // The address offset within the input section or the Output_data.
1212 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1213 // relocation against an output section, or TARGET_CODE for a target
1214 // specific relocation, or INVALID_CODE for an uninitialized value.
1215 // Otherwise, for a local symbol (this->is_section_symbol_ is
1216 // false), the local symbol index. For a local section symbol
1217 // (this->is_section_symbol_ is true), the section index in the
1219 unsigned int local_sym_index_
;
1220 // The reloc type--a processor specific code.
1221 unsigned int type_
: 28;
1222 // True if the relocation is a RELATIVE relocation.
1223 bool is_relative_
: 1;
1224 // True if the relocation is one which should not use
1225 // a symbol, but which obtains its addend from a symbol.
1226 bool is_symbolless_
: 1;
1227 // True if the relocation is against a section symbol.
1228 bool is_section_symbol_
: 1;
1229 // True if the addend should be the PLT offset. This is used only
1230 // for RELATIVE relocations to local symbols.
1231 // (Used only for RELA, but stored here for space.)
1232 bool use_plt_offset_
: 1;
1233 // If the reloc address is an input section in an object, the
1234 // section index. This is INVALID_CODE if the reloc address is
1235 // specified in some other way.
1236 unsigned int shndx_
;
1239 // The SHT_RELA version of Output_reloc<>. This is just derived from
1240 // the SHT_REL version of Output_reloc, but it adds an addend.
1242 template<bool dynamic
, int size
, bool big_endian
>
1243 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1246 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1247 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1249 // An uninitialized entry.
1254 // A reloc against a global symbol.
1256 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1257 Address address
, Addend addend
, bool is_relative
,
1259 : rel_(gsym
, type
, od
, address
, is_relative
, is_symbolless
),
1263 Output_reloc(Symbol
* gsym
, unsigned int type
,
1264 Sized_relobj
<size
, big_endian
>* relobj
,
1265 unsigned int shndx
, Address address
, Addend addend
,
1266 bool is_relative
, bool is_symbolless
)
1267 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
,
1268 is_symbolless
), addend_(addend
)
1271 // A reloc against a local symbol.
1273 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1274 unsigned int local_sym_index
, unsigned int type
,
1275 Output_data
* od
, Address address
,
1276 Addend addend
, bool is_relative
,
1277 bool is_symbolless
, bool is_section_symbol
,
1278 bool use_plt_offset
)
1279 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1280 is_symbolless
, is_section_symbol
, use_plt_offset
),
1284 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1285 unsigned int local_sym_index
, unsigned int type
,
1286 unsigned int shndx
, Address address
,
1287 Addend addend
, bool is_relative
,
1288 bool is_symbolless
, bool is_section_symbol
,
1289 bool use_plt_offset
)
1290 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1291 is_symbolless
, is_section_symbol
, use_plt_offset
),
1295 // A reloc against the STT_SECTION symbol of an output section.
1297 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1298 Address address
, Addend addend
)
1299 : rel_(os
, type
, od
, address
), addend_(addend
)
1302 Output_reloc(Output_section
* os
, unsigned int type
,
1303 Sized_relobj
<size
, big_endian
>* relobj
,
1304 unsigned int shndx
, Address address
, Addend addend
)
1305 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1308 // An absolute relocation with no symbol.
1310 Output_reloc(unsigned int type
, Output_data
* od
, Address address
,
1312 : rel_(type
, od
, address
), addend_(addend
)
1315 Output_reloc(unsigned int type
, Sized_relobj
<size
, big_endian
>* relobj
,
1316 unsigned int shndx
, Address address
, Addend addend
)
1317 : rel_(type
, relobj
, shndx
, address
), addend_(addend
)
1320 // A target specific relocation. The target will be called to get
1321 // the symbol index and the addend, passing ARG. The type and
1322 // offset will be set as for other relocation types.
1324 Output_reloc(unsigned int type
, void* arg
, Output_data
* od
,
1325 Address address
, Addend addend
)
1326 : rel_(type
, arg
, od
, address
), addend_(addend
)
1329 Output_reloc(unsigned int type
, void* arg
,
1330 Sized_relobj
<size
, big_endian
>* relobj
,
1331 unsigned int shndx
, Address address
, Addend addend
)
1332 : rel_(type
, arg
, relobj
, shndx
, address
), addend_(addend
)
1335 // Return whether this is a RELATIVE relocation.
1338 { return this->rel_
.is_relative(); }
1340 // Return whether this is a relocation which should not use
1341 // a symbol, but which obtains its addend from a symbol.
1343 is_symbolless() const
1344 { return this->rel_
.is_symbolless(); }
1346 // If this relocation is against an input section, return the
1347 // relocatable object containing the input section.
1348 Sized_relobj
<size
, big_endian
>*
1350 { return this->rel_
.get_relobj(); }
1352 // Write the reloc entry to an output view.
1354 write(unsigned char* pov
) const;
1356 // Return whether this reloc should be sorted before the argument
1357 // when sorting dynamic relocs.
1359 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1362 int i
= this->rel_
.compare(r2
.rel_
);
1368 return this->addend_
< r2
.addend_
;
1373 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1378 // Output_data_reloc_generic is a non-template base class for
1379 // Output_data_reloc_base. This gives the generic code a way to hold
1380 // a pointer to a reloc section.
1382 class Output_data_reloc_generic
: public Output_section_data_build
1385 Output_data_reloc_generic(int size
, bool sort_relocs
)
1386 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1387 relative_reloc_count_(0), sort_relocs_(sort_relocs
)
1390 // Return the number of relative relocs in this section.
1392 relative_reloc_count() const
1393 { return this->relative_reloc_count_
; }
1395 // Whether we should sort the relocs.
1398 { return this->sort_relocs_
; }
1401 // Note that we've added another relative reloc.
1403 bump_relative_reloc_count()
1404 { ++this->relative_reloc_count_
; }
1407 // The number of relative relocs added to this section. This is to
1408 // support DT_RELCOUNT.
1409 size_t relative_reloc_count_
;
1410 // Whether to sort the relocations when writing them out, to make
1411 // the dynamic linker more efficient.
1415 // Output_data_reloc is used to manage a section containing relocs.
1416 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1417 // indicates whether this is a dynamic relocation or a normal
1418 // relocation. Output_data_reloc_base is a base class.
1419 // Output_data_reloc is the real class, which we specialize based on
1422 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1423 class Output_data_reloc_base
: public Output_data_reloc_generic
1426 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1427 typedef typename
Output_reloc_type::Address Address
;
1428 static const int reloc_size
=
1429 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1431 // Construct the section.
1432 Output_data_reloc_base(bool sort_relocs
)
1433 : Output_data_reloc_generic(size
, sort_relocs
)
1437 // Write out the data.
1439 do_write(Output_file
*);
1441 // Set the entry size and the link.
1443 do_adjust_output_section(Output_section
* os
);
1445 // Write to a map file.
1447 do_print_to_mapfile(Mapfile
* mapfile
) const
1449 mapfile
->print_output_data(this,
1451 ? _("** dynamic relocs")
1455 // Add a relocation entry.
1457 add(Output_data
* od
, const Output_reloc_type
& reloc
)
1459 this->relocs_
.push_back(reloc
);
1460 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1462 od
->add_dynamic_reloc();
1463 if (reloc
.is_relative())
1464 this->bump_relative_reloc_count();
1465 Sized_relobj
<size
, big_endian
>* relobj
= reloc
.get_relobj();
1467 relobj
->add_dyn_reloc(this->relocs_
.size() - 1);
1471 typedef std::vector
<Output_reloc_type
> Relocs
;
1473 // The class used to sort the relocations.
1474 struct Sort_relocs_comparison
1477 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1478 { return r1
.sort_before(r2
); }
1481 // The relocations in this section.
1485 // The class which callers actually create.
1487 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1488 class Output_data_reloc
;
1490 // The SHT_REL version of Output_data_reloc.
1492 template<bool dynamic
, int size
, bool big_endian
>
1493 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1494 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1497 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1501 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1502 typedef typename
Output_reloc_type::Address Address
;
1504 Output_data_reloc(bool sr
)
1505 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1508 // Add a reloc against a global symbol.
1511 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1512 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, false)); }
1515 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1516 Sized_relobj
<size
, big_endian
>* relobj
,
1517 unsigned int shndx
, Address address
)
1518 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1521 // These are to simplify the Copy_relocs class.
1524 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1527 gold_assert(addend
== 0);
1528 this->add_global(gsym
, type
, od
, address
);
1532 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1533 Sized_relobj
<size
, big_endian
>* relobj
,
1534 unsigned int shndx
, Address address
, Address addend
)
1536 gold_assert(addend
== 0);
1537 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1540 // Add a RELATIVE reloc against a global symbol. The final relocation
1541 // will not reference the symbol.
1544 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1546 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true, true)); }
1549 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1550 Sized_relobj
<size
, big_endian
>* relobj
,
1551 unsigned int shndx
, Address address
)
1553 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1557 // Add a global relocation which does not use a symbol for the relocation,
1558 // but which gets its addend from a symbol.
1561 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1562 Output_data
* od
, Address address
)
1563 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false, true)); }
1566 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1568 Sized_relobj
<size
, big_endian
>* relobj
,
1569 unsigned int shndx
, Address address
)
1571 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1575 // Add a reloc against a local symbol.
1578 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1579 unsigned int local_sym_index
, unsigned int type
,
1580 Output_data
* od
, Address address
)
1582 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1583 address
, false, false, false, false));
1587 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1588 unsigned int local_sym_index
, unsigned int type
,
1589 Output_data
* od
, unsigned int shndx
, Address address
)
1591 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1592 address
, false, false, false, false));
1595 // Add a RELATIVE reloc against a local symbol.
1598 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1599 unsigned int local_sym_index
, unsigned int type
,
1600 Output_data
* od
, Address address
)
1602 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1603 address
, true, true, false, false));
1607 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1608 unsigned int local_sym_index
, unsigned int type
,
1609 Output_data
* od
, unsigned int shndx
, Address address
)
1611 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1612 address
, true, true, false, false));
1615 // Add a local relocation which does not use a symbol for the relocation,
1616 // but which gets its addend from a symbol.
1619 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1620 unsigned int local_sym_index
, unsigned int type
,
1621 Output_data
* od
, Address address
)
1623 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1624 address
, false, true, false, false));
1628 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1629 unsigned int local_sym_index
, unsigned int type
,
1630 Output_data
* od
, unsigned int shndx
,
1633 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1634 address
, false, true, false, false));
1637 // Add a reloc against a local section symbol. This will be
1638 // converted into a reloc against the STT_SECTION symbol of the
1642 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1643 unsigned int input_shndx
, unsigned int type
,
1644 Output_data
* od
, Address address
)
1646 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1647 address
, false, false, true, false));
1651 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1652 unsigned int input_shndx
, unsigned int type
,
1653 Output_data
* od
, unsigned int shndx
, Address address
)
1655 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1656 address
, false, false, true, false));
1659 // A reloc against the STT_SECTION symbol of an output section.
1660 // OS is the Output_section that the relocation refers to; OD is
1661 // the Output_data object being relocated.
1664 add_output_section(Output_section
* os
, unsigned int type
,
1665 Output_data
* od
, Address address
)
1666 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1669 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1670 Sized_relobj
<size
, big_endian
>* relobj
,
1671 unsigned int shndx
, Address address
)
1672 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1674 // Add an absolute relocation.
1677 add_absolute(unsigned int type
, Output_data
* od
, Address address
)
1678 { this->add(od
, Output_reloc_type(type
, od
, address
)); }
1681 add_absolute(unsigned int type
, Output_data
* od
,
1682 Sized_relobj
<size
, big_endian
>* relobj
,
1683 unsigned int shndx
, Address address
)
1684 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
)); }
1686 // Add a target specific relocation. A target which calls this must
1687 // define the reloc_symbol_index and reloc_addend virtual functions.
1690 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1692 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
)); }
1695 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1696 Sized_relobj
<size
, big_endian
>* relobj
,
1697 unsigned int shndx
, Address address
)
1698 { this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
)); }
1701 // The SHT_RELA version of Output_data_reloc.
1703 template<bool dynamic
, int size
, bool big_endian
>
1704 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1705 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1708 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1712 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1713 typedef typename
Output_reloc_type::Address Address
;
1714 typedef typename
Output_reloc_type::Addend Addend
;
1716 Output_data_reloc(bool sr
)
1717 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1720 // Add a reloc against a global symbol.
1723 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1724 Address address
, Addend addend
)
1725 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1729 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1730 Sized_relobj
<size
, big_endian
>* relobj
,
1731 unsigned int shndx
, Address address
,
1733 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1734 addend
, false, false)); }
1736 // Add a RELATIVE reloc against a global symbol. The final output
1737 // relocation will not reference the symbol, but we must keep the symbol
1738 // information long enough to set the addend of the relocation correctly
1739 // when it is written.
1742 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1743 Address address
, Addend addend
)
1744 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true,
1748 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1749 Sized_relobj
<size
, big_endian
>* relobj
,
1750 unsigned int shndx
, Address address
, Addend addend
)
1751 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1752 addend
, true, true)); }
1754 // Add a global relocation which does not use a symbol for the relocation,
1755 // but which gets its addend from a symbol.
1758 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1759 Address address
, Addend addend
)
1760 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1764 add_symbolless_global_addend(Symbol
* gsym
, unsigned int type
,
1766 Sized_relobj
<size
, big_endian
>* relobj
,
1767 unsigned int shndx
, Address address
, Addend addend
)
1768 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1769 addend
, false, true)); }
1771 // Add a reloc against a local symbol.
1774 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1775 unsigned int local_sym_index
, unsigned int type
,
1776 Output_data
* od
, Address address
, Addend addend
)
1778 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1779 addend
, false, false, false, false));
1783 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1784 unsigned int local_sym_index
, unsigned int type
,
1785 Output_data
* od
, unsigned int shndx
, Address address
,
1788 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1789 address
, addend
, false, false, false,
1793 // Add a RELATIVE reloc against a local symbol.
1796 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1797 unsigned int local_sym_index
, unsigned int type
,
1798 Output_data
* od
, Address address
, Addend addend
,
1799 bool use_plt_offset
)
1801 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1802 addend
, true, true, false,
1807 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1808 unsigned int local_sym_index
, unsigned int type
,
1809 Output_data
* od
, unsigned int shndx
, Address address
,
1810 Addend addend
, bool use_plt_offset
)
1812 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1813 address
, addend
, true, true, false,
1817 // Add a local relocation which does not use a symbol for the relocation,
1818 // but which gets it's addend from a symbol.
1821 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1822 unsigned int local_sym_index
, unsigned int type
,
1823 Output_data
* od
, Address address
, Addend addend
)
1825 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1826 addend
, false, true, false, false));
1830 add_symbolless_local_addend(Sized_relobj
<size
, big_endian
>* relobj
,
1831 unsigned int local_sym_index
, unsigned int type
,
1832 Output_data
* od
, unsigned int shndx
,
1833 Address address
, Addend addend
)
1835 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1836 address
, addend
, false, true, false,
1840 // Add a reloc against a local section symbol. This will be
1841 // converted into a reloc against the STT_SECTION symbol of the
1845 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1846 unsigned int input_shndx
, unsigned int type
,
1847 Output_data
* od
, Address address
, Addend addend
)
1849 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1850 addend
, false, false, true, false));
1854 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1855 unsigned int input_shndx
, unsigned int type
,
1856 Output_data
* od
, unsigned int shndx
, Address address
,
1859 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1860 address
, addend
, false, false, true,
1864 // A reloc against the STT_SECTION symbol of an output section.
1867 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1868 Address address
, Addend addend
)
1869 { this->add(od
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1872 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1873 Sized_relobj
<size
, big_endian
>* relobj
,
1874 unsigned int shndx
, Address address
, Addend addend
)
1875 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1878 // Add an absolute relocation.
1881 add_absolute(unsigned int type
, Output_data
* od
, Address address
,
1883 { this->add(od
, Output_reloc_type(type
, od
, address
, addend
)); }
1886 add_absolute(unsigned int type
, Output_data
* od
,
1887 Sized_relobj
<size
, big_endian
>* relobj
,
1888 unsigned int shndx
, Address address
, Addend addend
)
1889 { this->add(od
, Output_reloc_type(type
, relobj
, shndx
, address
, addend
)); }
1891 // Add a target specific relocation. A target which calls this must
1892 // define the reloc_symbol_index and reloc_addend virtual functions.
1895 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1896 Address address
, Addend addend
)
1897 { this->add(od
, Output_reloc_type(type
, arg
, od
, address
, addend
)); }
1900 add_target_specific(unsigned int type
, void* arg
, Output_data
* od
,
1901 Sized_relobj
<size
, big_endian
>* relobj
,
1902 unsigned int shndx
, Address address
, Addend addend
)
1904 this->add(od
, Output_reloc_type(type
, arg
, relobj
, shndx
, address
,
1909 // Output_relocatable_relocs represents a relocation section in a
1910 // relocatable link. The actual data is written out in the target
1911 // hook relocate_for_relocatable. This just saves space for it.
1913 template<int sh_type
, int size
, bool big_endian
>
1914 class Output_relocatable_relocs
: public Output_section_data
1917 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1918 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1923 set_final_data_size();
1925 // Write out the data. There is nothing to do here.
1927 do_write(Output_file
*)
1930 // Write to a map file.
1932 do_print_to_mapfile(Mapfile
* mapfile
) const
1933 { mapfile
->print_output_data(this, _("** relocs")); }
1936 // The relocs associated with this input section.
1937 Relocatable_relocs
* rr_
;
1940 // Handle a GROUP section.
1942 template<int size
, bool big_endian
>
1943 class Output_data_group
: public Output_section_data
1946 // The constructor clears *INPUT_SHNDXES.
1947 Output_data_group(Sized_relobj_file
<size
, big_endian
>* relobj
,
1948 section_size_type entry_count
,
1949 elfcpp::Elf_Word flags
,
1950 std::vector
<unsigned int>* input_shndxes
);
1953 do_write(Output_file
*);
1955 // Write to a map file.
1957 do_print_to_mapfile(Mapfile
* mapfile
) const
1958 { mapfile
->print_output_data(this, _("** group")); }
1960 // Set final data size.
1962 set_final_data_size()
1963 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1966 // The input object.
1967 Sized_relobj_file
<size
, big_endian
>* relobj_
;
1968 // The group flag word.
1969 elfcpp::Elf_Word flags_
;
1970 // The section indexes of the input sections in this group.
1971 std::vector
<unsigned int> input_shndxes_
;
1974 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1975 // for one symbol--either a global symbol or a local symbol in an
1976 // object. The target specific code adds entries to the GOT as
1979 template<int size
, bool big_endian
>
1980 class Output_data_got
: public Output_section_data_build
1983 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1984 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1985 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1988 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1989 entries_(), free_list_()
1992 Output_data_got(off_t data_size
)
1993 : Output_section_data_build(data_size
,
1994 Output_data::default_alignment_for_size(size
)),
1995 entries_(), free_list_()
1997 // For an incremental update, we have an existing GOT section.
1998 // Initialize the list of entries and the free list.
1999 this->entries_
.resize(data_size
/ (size
/ 8));
2000 this->free_list_
.init(data_size
, false);
2003 // Add an entry for a global symbol to the GOT. Return true if this
2004 // is a new GOT entry, false if the symbol was already in the GOT.
2006 add_global(Symbol
* gsym
, unsigned int got_type
);
2008 // Like add_global, but use the PLT offset of the global symbol if
2011 add_global_plt(Symbol
* gsym
, unsigned int got_type
);
2013 // Add an entry for a global symbol to the GOT, and add a dynamic
2014 // relocation of type R_TYPE for the GOT entry.
2016 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2017 Rel_dyn
* rel_dyn
, unsigned int r_type
);
2020 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
2021 Rela_dyn
* rela_dyn
, unsigned int r_type
);
2023 // Add a pair of entries for a global symbol to the GOT, and add
2024 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2026 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2027 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
2028 unsigned int r_type_2
);
2031 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
2032 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
2033 unsigned int r_type_2
);
2035 // Add an entry for a local symbol to the GOT. This returns true if
2036 // this is a new GOT entry, false if the symbol already has a GOT
2039 add_local(Sized_relobj_file
<size
, big_endian
>* object
, unsigned int sym_index
,
2040 unsigned int got_type
);
2042 // Like add_local, but use the PLT offset of the local symbol if it
2045 add_local_plt(Sized_relobj_file
<size
, big_endian
>* object
,
2046 unsigned int sym_index
,
2047 unsigned int got_type
);
2049 // Add an entry for a local symbol to the GOT, and add a dynamic
2050 // relocation of type R_TYPE for the GOT entry.
2052 add_local_with_rel(Sized_relobj_file
<size
, big_endian
>* object
,
2053 unsigned int sym_index
, unsigned int got_type
,
2054 Rel_dyn
* rel_dyn
, unsigned int r_type
);
2057 add_local_with_rela(Sized_relobj_file
<size
, big_endian
>* object
,
2058 unsigned int sym_index
, unsigned int got_type
,
2059 Rela_dyn
* rela_dyn
, unsigned int r_type
);
2061 // Add a pair of entries for a local symbol to the GOT, and add
2062 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2064 add_local_pair_with_rel(Sized_relobj_file
<size
, big_endian
>* object
,
2065 unsigned int sym_index
, unsigned int shndx
,
2066 unsigned int got_type
, Rel_dyn
* rel_dyn
,
2067 unsigned int r_type_1
, unsigned int r_type_2
);
2070 add_local_pair_with_rela(Sized_relobj_file
<size
, big_endian
>* object
,
2071 unsigned int sym_index
, unsigned int shndx
,
2072 unsigned int got_type
, Rela_dyn
* rela_dyn
,
2073 unsigned int r_type_1
, unsigned int r_type_2
);
2075 // Add a constant to the GOT. This returns the offset of the new
2076 // entry from the start of the GOT.
2078 add_constant(Valtype constant
)
2080 unsigned int got_offset
= this->add_got_entry(Got_entry(constant
));
2084 // Reserve a slot in the GOT.
2086 reserve_slot(unsigned int i
)
2087 { this->free_list_
.remove(i
* size
/ 8, (i
+ 1) * size
/ 8); }
2089 // Reserve a slot in the GOT for a local symbol.
2091 reserve_local(unsigned int i
, Sized_relobj
<size
, big_endian
>* object
,
2092 unsigned int sym_index
, unsigned int got_type
);
2094 // Reserve a slot in the GOT for a global symbol.
2096 reserve_global(unsigned int i
, Symbol
* gsym
, unsigned int got_type
);
2099 // Write out the GOT table.
2101 do_write(Output_file
*);
2103 // Write to a map file.
2105 do_print_to_mapfile(Mapfile
* mapfile
) const
2106 { mapfile
->print_output_data(this, _("** GOT")); }
2109 // This POD class holds a single GOT entry.
2113 // Create a zero entry.
2115 : local_sym_index_(RESERVED_CODE
), use_plt_offset_(false)
2116 { this->u_
.constant
= 0; }
2118 // Create a global symbol entry.
2119 Got_entry(Symbol
* gsym
, bool use_plt_offset
)
2120 : local_sym_index_(GSYM_CODE
), use_plt_offset_(use_plt_offset
)
2121 { this->u_
.gsym
= gsym
; }
2123 // Create a local symbol entry.
2124 Got_entry(Sized_relobj_file
<size
, big_endian
>* object
,
2125 unsigned int local_sym_index
, bool use_plt_offset
)
2126 : local_sym_index_(local_sym_index
), use_plt_offset_(use_plt_offset
)
2128 gold_assert(local_sym_index
!= GSYM_CODE
2129 && local_sym_index
!= CONSTANT_CODE
2130 && local_sym_index
!= RESERVED_CODE
2131 && local_sym_index
== this->local_sym_index_
);
2132 this->u_
.object
= object
;
2135 // Create a constant entry. The constant is a host value--it will
2136 // be swapped, if necessary, when it is written out.
2137 explicit Got_entry(Valtype constant
)
2138 : local_sym_index_(CONSTANT_CODE
), use_plt_offset_(false)
2139 { this->u_
.constant
= constant
; }
2141 // Write the GOT entry to an output view.
2143 write(unsigned char* pov
) const;
2148 GSYM_CODE
= 0x7fffffff,
2149 CONSTANT_CODE
= 0x7ffffffe,
2150 RESERVED_CODE
= 0x7ffffffd
2155 // For a local symbol, the object.
2156 Sized_relobj_file
<size
, big_endian
>* object
;
2157 // For a global symbol, the symbol.
2159 // For a constant, the constant.
2162 // For a local symbol, the local symbol index. This is GSYM_CODE
2163 // for a global symbol, or CONSTANT_CODE for a constant.
2164 unsigned int local_sym_index_
: 31;
2165 // Whether to use the PLT offset of the symbol if it has one.
2166 bool use_plt_offset_
: 1;
2169 typedef std::vector
<Got_entry
> Got_entries
;
2171 // Create a new GOT entry and return its offset.
2173 add_got_entry(Got_entry got_entry
);
2175 // Create a pair of new GOT entries and return the offset of the first.
2177 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
);
2179 // Return the offset into the GOT of GOT entry I.
2181 got_offset(unsigned int i
) const
2182 { return i
* (size
/ 8); }
2184 // Return the offset into the GOT of the last entry added.
2186 last_got_offset() const
2187 { return this->got_offset(this->entries_
.size() - 1); }
2189 // Set the size of the section.
2192 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
2194 // The list of GOT entries.
2195 Got_entries entries_
;
2197 // List of available regions within the section, for incremental
2199 Free_list free_list_
;
2202 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2205 class Output_data_dynamic
: public Output_section_data
2208 Output_data_dynamic(Stringpool
* pool
)
2209 : Output_section_data(Output_data::default_alignment()),
2210 entries_(), pool_(pool
)
2213 // Add a new dynamic entry with a fixed numeric value.
2215 add_constant(elfcpp::DT tag
, unsigned int val
)
2216 { this->add_entry(Dynamic_entry(tag
, val
)); }
2218 // Add a new dynamic entry with the address of output data.
2220 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
2221 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
2223 // Add a new dynamic entry with the address of output data
2224 // plus a constant offset.
2226 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
2227 unsigned int offset
)
2228 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
2230 // Add a new dynamic entry with the size of output data.
2232 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
2233 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
2235 // Add a new dynamic entry with the total size of two output datas.
2237 add_section_size(elfcpp::DT tag
, const Output_data
* od
,
2238 const Output_data
* od2
)
2239 { this->add_entry(Dynamic_entry(tag
, od
, od2
)); }
2241 // Add a new dynamic entry with the address of a symbol.
2243 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
2244 { this->add_entry(Dynamic_entry(tag
, sym
)); }
2246 // Add a new dynamic entry with a string.
2248 add_string(elfcpp::DT tag
, const char* str
)
2249 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
2252 add_string(elfcpp::DT tag
, const std::string
& str
)
2253 { this->add_string(tag
, str
.c_str()); }
2256 // Adjust the output section to set the entry size.
2258 do_adjust_output_section(Output_section
*);
2260 // Set the final data size.
2262 set_final_data_size();
2264 // Write out the dynamic entries.
2266 do_write(Output_file
*);
2268 // Write to a map file.
2270 do_print_to_mapfile(Mapfile
* mapfile
) const
2271 { mapfile
->print_output_data(this, _("** dynamic")); }
2274 // This POD class holds a single dynamic entry.
2278 // Create an entry with a fixed numeric value.
2279 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
2280 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
2281 { this->u_
.val
= val
; }
2283 // Create an entry with the size or address of a section.
2284 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
2286 offset_(section_size
2287 ? DYNAMIC_SECTION_SIZE
2288 : DYNAMIC_SECTION_ADDRESS
)
2294 // Create an entry with the size of two sections.
2295 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, const Output_data
* od2
)
2297 offset_(DYNAMIC_SECTION_SIZE
)
2303 // Create an entry with the address of a section plus a constant offset.
2304 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
2307 { this->u_
.od
= od
; }
2309 // Create an entry with the address of a symbol.
2310 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
2311 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
2312 { this->u_
.sym
= sym
; }
2314 // Create an entry with a string.
2315 Dynamic_entry(elfcpp::DT tag
, const char* str
)
2316 : tag_(tag
), offset_(DYNAMIC_STRING
)
2317 { this->u_
.str
= str
; }
2319 // Return the tag of this entry.
2322 { return this->tag_
; }
2324 // Write the dynamic entry to an output view.
2325 template<int size
, bool big_endian
>
2327 write(unsigned char* pov
, const Stringpool
*) const;
2330 // Classification is encoded in the OFFSET field.
2334 DYNAMIC_SECTION_ADDRESS
= 0,
2336 DYNAMIC_NUMBER
= -1U,
2338 DYNAMIC_SECTION_SIZE
= -2U,
2340 DYNAMIC_SYMBOL
= -3U,
2342 DYNAMIC_STRING
= -4U
2343 // Any other value indicates a section address plus OFFSET.
2348 // For DYNAMIC_NUMBER.
2350 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2351 const Output_data
* od
;
2352 // For DYNAMIC_SYMBOL.
2354 // For DYNAMIC_STRING.
2357 // For DYNAMIC_SYMBOL with two sections.
2358 const Output_data
* od2
;
2361 // The type of entry (Classification) or offset within a section.
2362 unsigned int offset_
;
2365 // Add an entry to the list.
2367 add_entry(const Dynamic_entry
& entry
)
2368 { this->entries_
.push_back(entry
); }
2370 // Sized version of write function.
2371 template<int size
, bool big_endian
>
2373 sized_write(Output_file
* of
);
2375 // The type of the list of entries.
2376 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2379 Dynamic_entries entries_
;
2380 // The pool used for strings.
2384 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2385 // which may be required if the object file has more than
2386 // SHN_LORESERVE sections.
2388 class Output_symtab_xindex
: public Output_section_data
2391 Output_symtab_xindex(size_t symcount
)
2392 : Output_section_data(symcount
* 4, 4, true),
2396 // Add an entry: symbol number SYMNDX has section SHNDX.
2398 add(unsigned int symndx
, unsigned int shndx
)
2399 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2403 do_write(Output_file
*);
2405 // Write to a map file.
2407 do_print_to_mapfile(Mapfile
* mapfile
) const
2408 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2411 template<bool big_endian
>
2413 endian_do_write(unsigned char*);
2415 // It is likely that most symbols will not require entries. Rather
2416 // than keep a vector for all symbols, we keep pairs of symbol index
2417 // and section index.
2418 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2420 // The entries we need.
2421 Xindex_entries entries_
;
2424 // A relaxed input section.
2425 class Output_relaxed_input_section
: public Output_section_data_build
2428 // We would like to call relobj->section_addralign(shndx) to get the
2429 // alignment but we do not want the constructor to fail. So callers
2430 // are repsonsible for ensuring that.
2431 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2433 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2436 // Return the Relobj of this relaxed input section.
2439 { return this->relobj_
; }
2441 // Return the section index of this relaxed input section.
2444 { return this->shndx_
; }
2448 unsigned int shndx_
;
2451 // This class describes properties of merge data sections. It is used
2452 // as a key type for maps.
2453 class Merge_section_properties
2456 Merge_section_properties(bool is_string
, uint64_t entsize
,
2458 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
2461 // Whether this equals to another Merge_section_properties MSP.
2463 eq(const Merge_section_properties
& msp
) const
2465 return ((this->is_string_
== msp
.is_string_
)
2466 && (this->entsize_
== msp
.entsize_
)
2467 && (this->addralign_
== msp
.addralign_
));
2470 // Compute a hash value for this using 64-bit FNV-1a hash.
2474 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
2475 uint64_t prime
= 1099511628211ULL;
2476 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
2477 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
2478 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
2482 // Functors for associative containers.
2486 operator()(const Merge_section_properties
& msp1
,
2487 const Merge_section_properties
& msp2
) const
2488 { return msp1
.eq(msp2
); }
2494 operator()(const Merge_section_properties
& msp
) const
2495 { return msp
.hash_value(); }
2499 // Whether this merge data section is for strings.
2501 // Entsize of this merge data section.
2503 // Address alignment.
2504 uint64_t addralign_
;
2507 // This class is used to speed up look up of special input sections in an
2510 class Output_section_lookup_maps
2513 Output_section_lookup_maps()
2514 : is_valid_(true), merge_sections_by_properties_(),
2515 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2518 // Whether the maps are valid.
2521 { return this->is_valid_
; }
2523 // Invalidate the maps.
2526 { this->is_valid_
= false; }
2532 this->merge_sections_by_properties_
.clear();
2533 this->merge_sections_by_id_
.clear();
2534 this->relaxed_input_sections_by_id_
.clear();
2535 // A cleared map is valid.
2536 this->is_valid_
= true;
2539 // Find a merge section by merge section properties. Return NULL if none
2542 find_merge_section(const Merge_section_properties
& msp
) const
2544 gold_assert(this->is_valid_
);
2545 Merge_sections_by_properties::const_iterator p
=
2546 this->merge_sections_by_properties_
.find(msp
);
2547 return p
!= this->merge_sections_by_properties_
.end() ? p
->second
: NULL
;
2550 // Find a merge section by section ID of a merge input section. Return NULL
2551 // if none is found.
2553 find_merge_section(const Object
* object
, unsigned int shndx
) const
2555 gold_assert(this->is_valid_
);
2556 Merge_sections_by_id::const_iterator p
=
2557 this->merge_sections_by_id_
.find(Const_section_id(object
, shndx
));
2558 return p
!= this->merge_sections_by_id_
.end() ? p
->second
: NULL
;
2561 // Add a merge section pointed by POMB with properties MSP.
2563 add_merge_section(const Merge_section_properties
& msp
,
2564 Output_merge_base
* pomb
)
2566 std::pair
<Merge_section_properties
, Output_merge_base
*> value(msp
, pomb
);
2567 std::pair
<Merge_sections_by_properties::iterator
, bool> result
=
2568 this->merge_sections_by_properties_
.insert(value
);
2569 gold_assert(result
.second
);
2572 // Add a mapping from a merged input section in OBJECT with index SHNDX
2573 // to a merge output section pointed by POMB.
2575 add_merge_input_section(const Object
* object
, unsigned int shndx
,
2576 Output_merge_base
* pomb
)
2578 Const_section_id
csid(object
, shndx
);
2579 std::pair
<Const_section_id
, Output_merge_base
*> value(csid
, pomb
);
2580 std::pair
<Merge_sections_by_id::iterator
, bool> result
=
2581 this->merge_sections_by_id_
.insert(value
);
2582 gold_assert(result
.second
);
2585 // Find a relaxed input section of OBJECT with index SHNDX.
2586 Output_relaxed_input_section
*
2587 find_relaxed_input_section(const Object
* object
, unsigned int shndx
) const
2589 gold_assert(this->is_valid_
);
2590 Relaxed_input_sections_by_id::const_iterator p
=
2591 this->relaxed_input_sections_by_id_
.find(Const_section_id(object
, shndx
));
2592 return p
!= this->relaxed_input_sections_by_id_
.end() ? p
->second
: NULL
;
2595 // Add a relaxed input section pointed by POMB and whose original input
2596 // section is in OBJECT with index SHNDX.
2598 add_relaxed_input_section(const Relobj
* relobj
, unsigned int shndx
,
2599 Output_relaxed_input_section
* poris
)
2601 Const_section_id
csid(relobj
, shndx
);
2602 std::pair
<Const_section_id
, Output_relaxed_input_section
*>
2604 std::pair
<Relaxed_input_sections_by_id::iterator
, bool> result
=
2605 this->relaxed_input_sections_by_id_
.insert(value
);
2606 gold_assert(result
.second
);
2610 typedef Unordered_map
<Const_section_id
, Output_merge_base
*,
2611 Const_section_id_hash
>
2612 Merge_sections_by_id
;
2614 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
2615 Merge_section_properties::hash
,
2616 Merge_section_properties::equal_to
>
2617 Merge_sections_by_properties
;
2619 typedef Unordered_map
<Const_section_id
, Output_relaxed_input_section
*,
2620 Const_section_id_hash
>
2621 Relaxed_input_sections_by_id
;
2623 // Whether this is valid
2625 // Merge sections by merge section properties.
2626 Merge_sections_by_properties merge_sections_by_properties_
;
2627 // Merge sections by section IDs.
2628 Merge_sections_by_id merge_sections_by_id_
;
2629 // Relaxed sections by section IDs.
2630 Relaxed_input_sections_by_id relaxed_input_sections_by_id_
;
2633 // This abstract base class defines the interface for the
2634 // types of methods used to fill free space left in an output
2635 // section during an incremental link. These methods are used
2636 // to insert dummy compilation units into debug info so that
2637 // debug info consumers can scan the debug info serially.
2643 : is_big_endian_(parameters
->target().is_big_endian())
2646 // Return the smallest size chunk of free space that can be
2647 // filled with a dummy compilation unit.
2649 minimum_hole_size() const
2650 { return this->do_minimum_hole_size(); }
2652 // Write a fill pattern of length LEN at offset OFF in the file.
2654 write(Output_file
* of
, off_t off
, size_t len
) const
2655 { this->do_write(of
, off
, len
); }
2659 do_minimum_hole_size() const = 0;
2662 do_write(Output_file
* of
, off_t off
, size_t len
) const = 0;
2665 is_big_endian() const
2666 { return this->is_big_endian_
; }
2669 bool is_big_endian_
;
2672 // Fill method that introduces a dummy compilation unit in
2673 // a .debug_info or .debug_types section.
2675 class Output_fill_debug_info
: public Output_fill
2678 Output_fill_debug_info(bool is_debug_types
)
2679 : is_debug_types_(is_debug_types
)
2684 do_minimum_hole_size() const;
2687 do_write(Output_file
* of
, off_t off
, size_t len
) const;
2690 // Version of the header.
2691 static const int version
= 4;
2692 // True if this is a .debug_types section.
2693 bool is_debug_types_
;
2696 // Fill method that introduces a dummy compilation unit in
2697 // a .debug_line section.
2699 class Output_fill_debug_line
: public Output_fill
2702 Output_fill_debug_line()
2707 do_minimum_hole_size() const;
2710 do_write(Output_file
* of
, off_t off
, size_t len
) const;
2713 // Version of the header. We write a DWARF-3 header because it's smaller
2714 // and many tools have not yet been updated to understand the DWARF-4 header.
2715 static const int version
= 3;
2716 // Length of the portion of the header that follows the header_length
2717 // field. This includes the following fields:
2718 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2719 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2720 // The standard_opcode_lengths array is 12 bytes long, and the
2721 // include_directories and filenames fields each contain only a single
2723 static const size_t header_length
= 19;
2726 // An output section. We don't expect to have too many output
2727 // sections, so we don't bother to do a template on the size.
2729 class Output_section
: public Output_data
2732 // Create an output section, giving the name, type, and flags.
2733 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2734 virtual ~Output_section();
2736 // Add a new input section SHNDX, named NAME, with header SHDR, from
2737 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2738 // which applies to this section, or 0 if none, or -1 if more than
2739 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2740 // in a linker script; in that case we need to keep track of input
2741 // sections associated with an output section. Return the offset
2742 // within the output section.
2743 template<int size
, bool big_endian
>
2745 add_input_section(Layout
* layout
, Sized_relobj_file
<size
, big_endian
>* object
,
2746 unsigned int shndx
, const char* name
,
2747 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2748 unsigned int reloc_shndx
, bool have_sections_script
);
2750 // Add generated data POSD to this output section.
2752 add_output_section_data(Output_section_data
* posd
);
2754 // Add a relaxed input section PORIS called NAME to this output section
2757 add_relaxed_input_section(Layout
* layout
,
2758 Output_relaxed_input_section
* poris
,
2759 const std::string
& name
);
2761 // Return the section name.
2764 { return this->name_
; }
2766 // Return the section type.
2769 { return this->type_
; }
2771 // Return the section flags.
2774 { return this->flags_
; }
2776 typedef std::map
<Section_id
, unsigned int> Section_layout_order
;
2779 update_section_layout(const Section_layout_order
* order_map
);
2781 // Update the output section flags based on input section flags.
2783 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2785 // Return the entsize field.
2788 { return this->entsize_
; }
2790 // Set the entsize field.
2792 set_entsize(uint64_t v
);
2794 // Set the load address.
2796 set_load_address(uint64_t load_address
)
2798 this->load_address_
= load_address
;
2799 this->has_load_address_
= true;
2802 // Set the link field to the output section index of a section.
2804 set_link_section(const Output_data
* od
)
2806 gold_assert(this->link_
== 0
2807 && !this->should_link_to_symtab_
2808 && !this->should_link_to_dynsym_
);
2809 this->link_section_
= od
;
2812 // Set the link field to a constant.
2814 set_link(unsigned int v
)
2816 gold_assert(this->link_section_
== NULL
2817 && !this->should_link_to_symtab_
2818 && !this->should_link_to_dynsym_
);
2822 // Record that this section should link to the normal symbol table.
2824 set_should_link_to_symtab()
2826 gold_assert(this->link_section_
== NULL
2828 && !this->should_link_to_dynsym_
);
2829 this->should_link_to_symtab_
= true;
2832 // Record that this section should link to the dynamic symbol table.
2834 set_should_link_to_dynsym()
2836 gold_assert(this->link_section_
== NULL
2838 && !this->should_link_to_symtab_
);
2839 this->should_link_to_dynsym_
= true;
2842 // Return the info field.
2846 gold_assert(this->info_section_
== NULL
2847 && this->info_symndx_
== NULL
);
2851 // Set the info field to the output section index of a section.
2853 set_info_section(const Output_section
* os
)
2855 gold_assert((this->info_section_
== NULL
2856 || (this->info_section_
== os
2857 && this->info_uses_section_index_
))
2858 && this->info_symndx_
== NULL
2859 && this->info_
== 0);
2860 this->info_section_
= os
;
2861 this->info_uses_section_index_
= true;
2864 // Set the info field to the symbol table index of a symbol.
2866 set_info_symndx(const Symbol
* sym
)
2868 gold_assert(this->info_section_
== NULL
2869 && (this->info_symndx_
== NULL
2870 || this->info_symndx_
== sym
)
2871 && this->info_
== 0);
2872 this->info_symndx_
= sym
;
2875 // Set the info field to the symbol table index of a section symbol.
2877 set_info_section_symndx(const Output_section
* os
)
2879 gold_assert((this->info_section_
== NULL
2880 || (this->info_section_
== os
2881 && !this->info_uses_section_index_
))
2882 && this->info_symndx_
== NULL
2883 && this->info_
== 0);
2884 this->info_section_
= os
;
2885 this->info_uses_section_index_
= false;
2888 // Set the info field to a constant.
2890 set_info(unsigned int v
)
2892 gold_assert(this->info_section_
== NULL
2893 && this->info_symndx_
== NULL
2894 && (this->info_
== 0
2895 || this->info_
== v
));
2899 // Set the addralign field.
2901 set_addralign(uint64_t v
)
2902 { this->addralign_
= v
; }
2904 // Whether the output section index has been set.
2906 has_out_shndx() const
2907 { return this->out_shndx_
!= -1U; }
2909 // Indicate that we need a symtab index.
2911 set_needs_symtab_index()
2912 { this->needs_symtab_index_
= true; }
2914 // Return whether we need a symtab index.
2916 needs_symtab_index() const
2917 { return this->needs_symtab_index_
; }
2919 // Get the symtab index.
2921 symtab_index() const
2923 gold_assert(this->symtab_index_
!= 0);
2924 return this->symtab_index_
;
2927 // Set the symtab index.
2929 set_symtab_index(unsigned int index
)
2931 gold_assert(index
!= 0);
2932 this->symtab_index_
= index
;
2935 // Indicate that we need a dynsym index.
2937 set_needs_dynsym_index()
2938 { this->needs_dynsym_index_
= true; }
2940 // Return whether we need a dynsym index.
2942 needs_dynsym_index() const
2943 { return this->needs_dynsym_index_
; }
2945 // Get the dynsym index.
2947 dynsym_index() const
2949 gold_assert(this->dynsym_index_
!= 0);
2950 return this->dynsym_index_
;
2953 // Set the dynsym index.
2955 set_dynsym_index(unsigned int index
)
2957 gold_assert(index
!= 0);
2958 this->dynsym_index_
= index
;
2961 // Return whether the input sections sections attachd to this output
2962 // section may require sorting. This is used to handle constructor
2963 // priorities compatibly with GNU ld.
2965 may_sort_attached_input_sections() const
2966 { return this->may_sort_attached_input_sections_
; }
2968 // Record that the input sections attached to this output section
2969 // may require sorting.
2971 set_may_sort_attached_input_sections()
2972 { this->may_sort_attached_input_sections_
= true; }
2974 // Returns true if input sections must be sorted according to the
2975 // order in which their name appear in the --section-ordering-file.
2977 input_section_order_specified()
2978 { return this->input_section_order_specified_
; }
2980 // Record that input sections must be sorted as some of their names
2981 // match the patterns specified through --section-ordering-file.
2983 set_input_section_order_specified()
2984 { this->input_section_order_specified_
= true; }
2986 // Return whether the input sections attached to this output section
2987 // require sorting. This is used to handle constructor priorities
2988 // compatibly with GNU ld.
2990 must_sort_attached_input_sections() const
2991 { return this->must_sort_attached_input_sections_
; }
2993 // Record that the input sections attached to this output section
2996 set_must_sort_attached_input_sections()
2997 { this->must_sort_attached_input_sections_
= true; }
2999 // Get the order in which this section appears in the PT_LOAD output
3001 Output_section_order
3003 { return this->order_
; }
3005 // Set the order for this section.
3007 set_order(Output_section_order order
)
3008 { this->order_
= order
; }
3010 // Return whether this section holds relro data--data which has
3011 // dynamic relocations but which may be marked read-only after the
3012 // dynamic relocations have been completed.
3015 { return this->is_relro_
; }
3017 // Record that this section holds relro data.
3020 { this->is_relro_
= true; }
3022 // Record that this section does not hold relro data.
3025 { this->is_relro_
= false; }
3027 // True if this is a small section: a section which holds small
3030 is_small_section() const
3031 { return this->is_small_section_
; }
3033 // Record that this is a small section.
3035 set_is_small_section()
3036 { this->is_small_section_
= true; }
3038 // True if this is a large section: a section which holds large
3041 is_large_section() const
3042 { return this->is_large_section_
; }
3044 // Record that this is a large section.
3046 set_is_large_section()
3047 { this->is_large_section_
= true; }
3049 // True if this is a large data (not BSS) section.
3051 is_large_data_section()
3052 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
3054 // Return whether this section should be written after all the input
3055 // sections are complete.
3057 after_input_sections() const
3058 { return this->after_input_sections_
; }
3060 // Record that this section should be written after all the input
3061 // sections are complete.
3063 set_after_input_sections()
3064 { this->after_input_sections_
= true; }
3066 // Return whether this section requires postprocessing after all
3067 // relocations have been applied.
3069 requires_postprocessing() const
3070 { return this->requires_postprocessing_
; }
3072 // If a section requires postprocessing, return the buffer to use.
3074 postprocessing_buffer() const
3076 gold_assert(this->postprocessing_buffer_
!= NULL
);
3077 return this->postprocessing_buffer_
;
3080 // If a section requires postprocessing, create the buffer to use.
3082 create_postprocessing_buffer();
3084 // If a section requires postprocessing, this is the size of the
3085 // buffer to which relocations should be applied.
3087 postprocessing_buffer_size() const
3088 { return this->current_data_size_for_child(); }
3090 // Modify the section name. This is only permitted for an
3091 // unallocated section, and only before the size has been finalized.
3092 // Otherwise the name will not get into Layout::namepool_.
3094 set_name(const char* newname
)
3096 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
3097 gold_assert(!this->is_data_size_valid());
3098 this->name_
= newname
;
3101 // Return whether the offset OFFSET in the input section SHNDX in
3102 // object OBJECT is being included in the link.
3104 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
3105 off_t offset
) const;
3107 // Return the offset within the output section of OFFSET relative to
3108 // the start of input section SHNDX in object OBJECT.
3110 output_offset(const Relobj
* object
, unsigned int shndx
,
3111 section_offset_type offset
) const;
3113 // Return the output virtual address of OFFSET relative to the start
3114 // of input section SHNDX in object OBJECT.
3116 output_address(const Relobj
* object
, unsigned int shndx
,
3117 off_t offset
) const;
3119 // Look for the merged section for input section SHNDX in object
3120 // OBJECT. If found, return true, and set *ADDR to the address of
3121 // the start of the merged section. This is not necessary the
3122 // output offset corresponding to input offset 0 in the section,
3123 // since the section may be mapped arbitrarily.
3125 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
3126 uint64_t* addr
) const;
3128 // Record that this output section was found in the SECTIONS clause
3129 // of a linker script.
3131 set_found_in_sections_clause()
3132 { this->found_in_sections_clause_
= true; }
3134 // Return whether this output section was found in the SECTIONS
3135 // clause of a linker script.
3137 found_in_sections_clause() const
3138 { return this->found_in_sections_clause_
; }
3140 // Write the section header into *OPHDR.
3141 template<int size
, bool big_endian
>
3143 write_header(const Layout
*, const Stringpool
*,
3144 elfcpp::Shdr_write
<size
, big_endian
>*) const;
3146 // The next few calls are for linker script support.
3148 // In some cases we need to keep a list of the input sections
3149 // associated with this output section. We only need the list if we
3150 // might have to change the offsets of the input section within the
3151 // output section after we add the input section. The ordinary
3152 // input sections will be written out when we process the object
3153 // file, and as such we don't need to track them here. We do need
3154 // to track Output_section_data objects here. We store instances of
3155 // this structure in a std::vector, so it must be a POD. There can
3156 // be many instances of this structure, so we use a union to save
3162 : shndx_(0), p2align_(0)
3164 this->u1_
.data_size
= 0;
3165 this->u2_
.object
= NULL
;
3168 // For an ordinary input section.
3169 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
3172 p2align_(ffsll(static_cast<long long>(addralign
))),
3173 section_order_index_(0)
3175 gold_assert(shndx
!= OUTPUT_SECTION_CODE
3176 && shndx
!= MERGE_DATA_SECTION_CODE
3177 && shndx
!= MERGE_STRING_SECTION_CODE
3178 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
3179 this->u1_
.data_size
= data_size
;
3180 this->u2_
.object
= object
;
3183 // For a non-merge output section.
3184 Input_section(Output_section_data
* posd
)
3185 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0),
3186 section_order_index_(0)
3188 this->u1_
.data_size
= 0;
3189 this->u2_
.posd
= posd
;
3192 // For a merge section.
3193 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
3195 ? MERGE_STRING_SECTION_CODE
3196 : MERGE_DATA_SECTION_CODE
),
3198 section_order_index_(0)
3200 this->u1_
.entsize
= entsize
;
3201 this->u2_
.posd
= posd
;
3204 // For a relaxed input section.
3205 Input_section(Output_relaxed_input_section
* psection
)
3206 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0),
3207 section_order_index_(0)
3209 this->u1_
.data_size
= 0;
3210 this->u2_
.poris
= psection
;
3214 section_order_index() const
3216 return this->section_order_index_
;
3220 set_section_order_index(unsigned int number
)
3222 this->section_order_index_
= number
;
3225 // The required alignment.
3229 if (this->p2align_
!= 0)
3230 return static_cast<uint64_t>(1) << (this->p2align_
- 1);
3231 else if (!this->is_input_section())
3232 return this->u2_
.posd
->addralign();
3237 // Set the required alignment, which must be either 0 or a power of 2.
3238 // For input sections that are sub-classes of Output_section_data, a
3239 // alignment of zero means asking the underlying object for alignment.
3241 set_addralign(uint64_t addralign
)
3247 gold_assert((addralign
& (addralign
- 1)) == 0);
3248 this->p2align_
= ffsll(static_cast<long long>(addralign
));
3252 // Return the current required size, without finalization.
3254 current_data_size() const;
3256 // Return the required size.
3260 // Whether this is an input section.
3262 is_input_section() const
3264 return (this->shndx_
!= OUTPUT_SECTION_CODE
3265 && this->shndx_
!= MERGE_DATA_SECTION_CODE
3266 && this->shndx_
!= MERGE_STRING_SECTION_CODE
3267 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
3270 // Return whether this is a merge section which matches the
3273 is_merge_section(bool is_string
, uint64_t entsize
,
3274 uint64_t addralign
) const
3276 return (this->shndx_
== (is_string
3277 ? MERGE_STRING_SECTION_CODE
3278 : MERGE_DATA_SECTION_CODE
)
3279 && this->u1_
.entsize
== entsize
3280 && this->addralign() == addralign
);
3283 // Return whether this is a merge section for some input section.
3285 is_merge_section() const
3287 return (this->shndx_
== MERGE_DATA_SECTION_CODE
3288 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3291 // Return whether this is a relaxed input section.
3293 is_relaxed_input_section() const
3294 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
3296 // Return whether this is a generic Output_section_data.
3298 is_output_section_data() const
3300 return this->shndx_
== OUTPUT_SECTION_CODE
;
3303 // Return the object for an input section.
3307 // Return the input section index for an input section.
3311 // For non-input-sections, return the associated Output_section_data
3313 Output_section_data
*
3314 output_section_data() const
3316 gold_assert(!this->is_input_section());
3317 return this->u2_
.posd
;
3320 // For a merge section, return the Output_merge_base pointer.
3322 output_merge_base() const
3324 gold_assert(this->is_merge_section());
3325 return this->u2_
.pomb
;
3328 // Return the Output_relaxed_input_section object.
3329 Output_relaxed_input_section
*
3330 relaxed_input_section() const
3332 gold_assert(this->is_relaxed_input_section());
3333 return this->u2_
.poris
;
3336 // Set the output section.
3338 set_output_section(Output_section
* os
)
3340 gold_assert(!this->is_input_section());
3341 Output_section_data
* posd
=
3342 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
3343 posd
->set_output_section(os
);
3346 // Set the address and file offset. This is called during
3347 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3348 // the enclosing section.
3350 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
3351 off_t section_file_offset
);
3353 // Reset the address and file offset.
3355 reset_address_and_file_offset();
3357 // Finalize the data size.
3359 finalize_data_size();
3361 // Add an input section, for SHF_MERGE sections.
3363 add_input_section(Relobj
* object
, unsigned int shndx
)
3365 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
3366 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
3367 return this->u2_
.posd
->add_input_section(object
, shndx
);
3370 // Given an input OBJECT, an input section index SHNDX within that
3371 // object, and an OFFSET relative to the start of that input
3372 // section, return whether or not the output offset is known. If
3373 // this function returns true, it sets *POUTPUT to the offset in
3374 // the output section, relative to the start of the input section
3375 // in the output section. *POUTPUT may be different from OFFSET
3376 // for a merged section.
3378 output_offset(const Relobj
* object
, unsigned int shndx
,
3379 section_offset_type offset
,
3380 section_offset_type
* poutput
) const;
3382 // Return whether this is the merge section for the input section
3385 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
3387 // Write out the data. This does nothing for an input section.
3389 write(Output_file
*);
3391 // Write the data to a buffer. This does nothing for an input
3394 write_to_buffer(unsigned char*);
3396 // Print to a map file.
3398 print_to_mapfile(Mapfile
*) const;
3400 // Print statistics about merge sections to stderr.
3402 print_merge_stats(const char* section_name
)
3404 if (this->shndx_
== MERGE_DATA_SECTION_CODE
3405 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
3406 this->u2_
.posd
->print_merge_stats(section_name
);
3410 // Code values which appear in shndx_. If the value is not one of
3411 // these codes, it is the input section index in the object file.
3414 // An Output_section_data.
3415 OUTPUT_SECTION_CODE
= -1U,
3416 // An Output_section_data for an SHF_MERGE section with
3417 // SHF_STRINGS not set.
3418 MERGE_DATA_SECTION_CODE
= -2U,
3419 // An Output_section_data for an SHF_MERGE section with
3421 MERGE_STRING_SECTION_CODE
= -3U,
3422 // An Output_section_data for a relaxed input section.
3423 RELAXED_INPUT_SECTION_CODE
= -4U
3426 // For an ordinary input section, this is the section index in the
3427 // input file. For an Output_section_data, this is
3428 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3429 // MERGE_STRING_SECTION_CODE.
3430 unsigned int shndx_
;
3431 // The required alignment, stored as a power of 2.
3432 unsigned int p2align_
;
3435 // For an ordinary input section, the section size.
3437 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3438 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3444 // For an ordinary input section, the object which holds the
3447 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3448 // MERGE_STRING_SECTION_CODE, the data.
3449 Output_section_data
* posd
;
3450 Output_merge_base
* pomb
;
3451 // For RELAXED_INPUT_SECTION_CODE, the data.
3452 Output_relaxed_input_section
* poris
;
3454 // The line number of the pattern it matches in the --section-ordering-file
3455 // file. It is 0 if does not match any pattern.
3456 unsigned int section_order_index_
;
3459 // Store the list of input sections for this Output_section into the
3460 // list passed in. This removes the input sections, leaving only
3461 // any Output_section_data elements. This returns the size of those
3462 // Output_section_data elements. ADDRESS is the address of this
3463 // output section. FILL is the fill value to use, in case there are
3464 // any spaces between the remaining Output_section_data elements.
3466 get_input_sections(uint64_t address
, const std::string
& fill
,
3467 std::list
<Input_section
>*);
3469 // Add a script input section. A script input section can either be
3470 // a plain input section or a sub-class of Output_section_data.
3472 add_script_input_section(const Input_section
& input_section
);
3474 // Set the current size of the output section.
3476 set_current_data_size(off_t size
)
3477 { this->set_current_data_size_for_child(size
); }
3479 // End of linker script support.
3481 // Save states before doing section layout.
3482 // This is used for relaxation.
3486 // Restore states prior to section layout.
3494 // Convert existing input sections to relaxed input sections.
3496 convert_input_sections_to_relaxed_sections(
3497 const std::vector
<Output_relaxed_input_section
*>& sections
);
3499 // Find a relaxed input section to an input section in OBJECT
3500 // with index SHNDX. Return NULL if none is found.
3501 const Output_relaxed_input_section
*
3502 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
3504 // Whether section offsets need adjustment due to relaxation.
3506 section_offsets_need_adjustment() const
3507 { return this->section_offsets_need_adjustment_
; }
3509 // Set section_offsets_need_adjustment to be true.
3511 set_section_offsets_need_adjustment()
3512 { this->section_offsets_need_adjustment_
= true; }
3514 // Adjust section offsets of input sections in this. This is
3515 // requires if relaxation caused some input sections to change sizes.
3517 adjust_section_offsets();
3519 // Whether this is a NOLOAD section.
3522 { return this->is_noload_
; }
3527 { this->is_noload_
= true; }
3529 // Print merge statistics to stderr.
3531 print_merge_stats();
3533 // Set a fixed layout for the section. Used for incremental update links.
3535 set_fixed_layout(uint64_t sh_addr
, off_t sh_offset
, off_t sh_size
,
3536 uint64_t sh_addralign
);
3538 // Return TRUE if the section has a fixed layout.
3540 has_fixed_layout() const
3541 { return this->has_fixed_layout_
; }
3543 // Set flag to allow patch space for this section. Used for full
3544 // incremental links.
3546 set_is_patch_space_allowed()
3547 { this->is_patch_space_allowed_
= true; }
3549 // Set a fill method to use for free space left in the output section
3550 // during incremental links.
3552 set_free_space_fill(Output_fill
* free_space_fill
)
3554 this->free_space_fill_
= free_space_fill
;
3555 this->free_list_
.set_min_hole_size(free_space_fill
->minimum_hole_size());
3558 // Reserve space within the fixed layout for the section. Used for
3559 // incremental update links.
3561 reserve(uint64_t sh_offset
, uint64_t sh_size
);
3563 // Allocate space from the free list for the section. Used for
3564 // incremental update links.
3566 allocate(off_t len
, uint64_t addralign
);
3569 // Return the output section--i.e., the object itself.
3574 const Output_section
*
3575 do_output_section() const
3578 // Return the section index in the output file.
3580 do_out_shndx() const
3582 gold_assert(this->out_shndx_
!= -1U);
3583 return this->out_shndx_
;
3586 // Set the output section index.
3588 do_set_out_shndx(unsigned int shndx
)
3590 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
3591 this->out_shndx_
= shndx
;
3594 // Update the data size of the Output_section. For a typical
3595 // Output_section, there is nothing to do, but if there are any
3596 // Output_section_data objects we need to do a trial layout
3601 // Set the final data size of the Output_section. For a typical
3602 // Output_section, there is nothing to do, but if there are any
3603 // Output_section_data objects we need to set their final addresses
3606 set_final_data_size();
3608 // Reset the address and file offset.
3610 do_reset_address_and_file_offset();
3612 // Return true if address and file offset already have reset values. In
3613 // other words, calling reset_address_and_file_offset will not change them.
3615 do_address_and_file_offset_have_reset_values() const;
3617 // Write the data to the file. For a typical Output_section, this
3618 // does nothing: the data is written out by calling Object::Relocate
3619 // on each input object. But if there are any Output_section_data
3620 // objects we do need to write them out here.
3622 do_write(Output_file
*);
3624 // Return the address alignment--function required by parent class.
3626 do_addralign() const
3627 { return this->addralign_
; }
3629 // Return whether there is a load address.
3631 do_has_load_address() const
3632 { return this->has_load_address_
; }
3634 // Return the load address.
3636 do_load_address() const
3638 gold_assert(this->has_load_address_
);
3639 return this->load_address_
;
3642 // Return whether this is an Output_section.
3644 do_is_section() const
3647 // Return whether this is a section of the specified type.
3649 do_is_section_type(elfcpp::Elf_Word type
) const
3650 { return this->type_
== type
; }
3652 // Return whether the specified section flag is set.
3654 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
3655 { return (this->flags_
& flag
) != 0; }
3657 // Set the TLS offset. Called only for SHT_TLS sections.
3659 do_set_tls_offset(uint64_t tls_base
);
3661 // Return the TLS offset, relative to the base of the TLS segment.
3662 // Valid only for SHT_TLS sections.
3664 do_tls_offset() const
3665 { return this->tls_offset_
; }
3667 // This may be implemented by a child class.
3669 do_finalize_name(Layout
*)
3672 // Print to the map file.
3674 do_print_to_mapfile(Mapfile
*) const;
3676 // Record that this section requires postprocessing after all
3677 // relocations have been applied. This is called by a child class.
3679 set_requires_postprocessing()
3681 this->requires_postprocessing_
= true;
3682 this->after_input_sections_
= true;
3685 // Write all the data of an Output_section into the postprocessing
3688 write_to_postprocessing_buffer();
3690 typedef std::vector
<Input_section
> Input_section_list
;
3692 // Allow a child class to access the input sections.
3693 const Input_section_list
&
3694 input_sections() const
3695 { return this->input_sections_
; }
3697 // Whether this always keeps an input section list
3699 always_keeps_input_sections() const
3700 { return this->always_keeps_input_sections_
; }
3702 // Always keep an input section list.
3704 set_always_keeps_input_sections()
3706 gold_assert(this->current_data_size_for_child() == 0);
3707 this->always_keeps_input_sections_
= true;
3711 // We only save enough information to undo the effects of section layout.
3712 class Checkpoint_output_section
3715 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
3716 const Input_section_list
& input_sections
,
3717 off_t first_input_offset
,
3718 bool attached_input_sections_are_sorted
)
3719 : addralign_(addralign
), flags_(flags
),
3720 input_sections_(input_sections
),
3721 input_sections_size_(input_sections_
.size()),
3722 input_sections_copy_(), first_input_offset_(first_input_offset
),
3723 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
3727 ~Checkpoint_output_section()
3730 // Return the address alignment.
3733 { return this->addralign_
; }
3735 // Return the section flags.
3738 { return this->flags_
; }
3740 // Return a reference to the input section list copy.
3743 { return &this->input_sections_copy_
; }
3745 // Return the size of input_sections at the time when checkpoint is
3748 input_sections_size() const
3749 { return this->input_sections_size_
; }
3751 // Whether input sections are copied.
3753 input_sections_saved() const
3754 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3757 first_input_offset() const
3758 { return this->first_input_offset_
; }
3761 attached_input_sections_are_sorted() const
3762 { return this->attached_input_sections_are_sorted_
; }
3764 // Save input sections.
3766 save_input_sections()
3768 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3769 this->input_sections_copy_
.clear();
3770 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3771 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3772 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3773 this->input_sections_copy_
.push_back(*p
);
3777 // The section alignment.
3778 uint64_t addralign_
;
3779 // The section flags.
3780 elfcpp::Elf_Xword flags_
;
3781 // Reference to the input sections to be checkpointed.
3782 const Input_section_list
& input_sections_
;
3783 // Size of the checkpointed portion of input_sections_;
3784 size_t input_sections_size_
;
3785 // Copy of input sections.
3786 Input_section_list input_sections_copy_
;
3787 // The offset of the first entry in input_sections_.
3788 off_t first_input_offset_
;
3789 // True if the input sections attached to this output section have
3790 // already been sorted.
3791 bool attached_input_sections_are_sorted_
;
3794 // This class is used to sort the input sections.
3795 class Input_section_sort_entry
;
3797 // This is the sort comparison function for ctors and dtors.
3798 struct Input_section_sort_compare
3801 operator()(const Input_section_sort_entry
&,
3802 const Input_section_sort_entry
&) const;
3805 // This is the sort comparison function for .init_array and .fini_array.
3806 struct Input_section_sort_init_fini_compare
3809 operator()(const Input_section_sort_entry
&,
3810 const Input_section_sort_entry
&) const;
3813 // This is the sort comparison function when a section order is specified
3814 // from an input file.
3815 struct Input_section_sort_section_order_index_compare
3818 operator()(const Input_section_sort_entry
&,
3819 const Input_section_sort_entry
&) const;
3822 // Fill data. This is used to fill in data between input sections.
3823 // It is also used for data statements (BYTE, WORD, etc.) in linker
3824 // scripts. When we have to keep track of the input sections, we
3825 // can use an Output_data_const, but we don't want to have to keep
3826 // track of input sections just to implement fills.
3830 Fill(off_t section_offset
, off_t length
)
3831 : section_offset_(section_offset
),
3832 length_(convert_to_section_size_type(length
))
3835 // Return section offset.
3837 section_offset() const
3838 { return this->section_offset_
; }
3840 // Return fill length.
3843 { return this->length_
; }
3846 // The offset within the output section.
3847 off_t section_offset_
;
3848 // The length of the space to fill.
3849 section_size_type length_
;
3852 typedef std::vector
<Fill
> Fill_list
;
3854 // Map used during relaxation of existing sections. This map
3855 // a section id an input section list index. We assume that
3856 // Input_section_list is a vector.
3857 typedef Unordered_map
<Section_id
, size_t, Section_id_hash
> Relaxation_map
;
3859 // Add a new output section by Input_section.
3861 add_output_section_data(Input_section
*);
3863 // Add an SHF_MERGE input section. Returns true if the section was
3864 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3865 // stores information about the merged input sections.
3867 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3868 uint64_t entsize
, uint64_t addralign
,
3869 bool keeps_input_sections
);
3871 // Add an output SHF_MERGE section POSD to this output section.
3872 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3873 // ENTSIZE is the entity size. This returns the entry added to
3876 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3879 // Sort the attached input sections.
3881 sort_attached_input_sections();
3883 // Find the merge section into which an input section with index SHNDX in
3884 // OBJECT has been added. Return NULL if none found.
3885 Output_section_data
*
3886 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3888 // Build a relaxation map.
3890 build_relaxation_map(
3891 const Input_section_list
& input_sections
,
3893 Relaxation_map
* map
) const;
3895 // Convert input sections in an input section list into relaxed sections.
3897 convert_input_sections_in_list_to_relaxed_sections(
3898 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3899 const Relaxation_map
& map
,
3900 Input_section_list
* input_sections
);
3902 // Build the lookup maps for merge and relaxed input sections.
3904 build_lookup_maps() const;
3906 // Most of these fields are only valid after layout.
3908 // The name of the section. This will point into a Stringpool.
3910 // The section address is in the parent class.
3911 // The section alignment.
3912 uint64_t addralign_
;
3913 // The section entry size.
3915 // The load address. This is only used when using a linker script
3916 // with a SECTIONS clause. The has_load_address_ field indicates
3917 // whether this field is valid.
3918 uint64_t load_address_
;
3919 // The file offset is in the parent class.
3920 // Set the section link field to the index of this section.
3921 const Output_data
* link_section_
;
3922 // If link_section_ is NULL, this is the link field.
3924 // Set the section info field to the index of this section.
3925 const Output_section
* info_section_
;
3926 // If info_section_ is NULL, set the info field to the symbol table
3927 // index of this symbol.
3928 const Symbol
* info_symndx_
;
3929 // If info_section_ and info_symndx_ are NULL, this is the section
3932 // The section type.
3933 const elfcpp::Elf_Word type_
;
3934 // The section flags.
3935 elfcpp::Elf_Xword flags_
;
3936 // The order of this section in the output segment.
3937 Output_section_order order_
;
3938 // The section index.
3939 unsigned int out_shndx_
;
3940 // If there is a STT_SECTION for this output section in the normal
3941 // symbol table, this is the symbol index. This starts out as zero.
3942 // It is initialized in Layout::finalize() to be the index, or -1U
3943 // if there isn't one.
3944 unsigned int symtab_index_
;
3945 // If there is a STT_SECTION for this output section in the dynamic
3946 // symbol table, this is the symbol index. This starts out as zero.
3947 // It is initialized in Layout::finalize() to be the index, or -1U
3948 // if there isn't one.
3949 unsigned int dynsym_index_
;
3950 // The input sections. This will be empty in cases where we don't
3951 // need to keep track of them.
3952 Input_section_list input_sections_
;
3953 // The offset of the first entry in input_sections_.
3954 off_t first_input_offset_
;
3955 // The fill data. This is separate from input_sections_ because we
3956 // often will need fill sections without needing to keep track of
3959 // If the section requires postprocessing, this buffer holds the
3960 // section contents during relocation.
3961 unsigned char* postprocessing_buffer_
;
3962 // Whether this output section needs a STT_SECTION symbol in the
3963 // normal symbol table. This will be true if there is a relocation
3965 bool needs_symtab_index_
: 1;
3966 // Whether this output section needs a STT_SECTION symbol in the
3967 // dynamic symbol table. This will be true if there is a dynamic
3968 // relocation which needs it.
3969 bool needs_dynsym_index_
: 1;
3970 // Whether the link field of this output section should point to the
3971 // normal symbol table.
3972 bool should_link_to_symtab_
: 1;
3973 // Whether the link field of this output section should point to the
3974 // dynamic symbol table.
3975 bool should_link_to_dynsym_
: 1;
3976 // Whether this section should be written after all the input
3977 // sections are complete.
3978 bool after_input_sections_
: 1;
3979 // Whether this section requires post processing after all
3980 // relocations have been applied.
3981 bool requires_postprocessing_
: 1;
3982 // Whether an input section was mapped to this output section
3983 // because of a SECTIONS clause in a linker script.
3984 bool found_in_sections_clause_
: 1;
3985 // Whether this section has an explicitly specified load address.
3986 bool has_load_address_
: 1;
3987 // True if the info_section_ field means the section index of the
3988 // section, false if it means the symbol index of the corresponding
3990 bool info_uses_section_index_
: 1;
3991 // True if input sections attached to this output section have to be
3992 // sorted according to a specified order.
3993 bool input_section_order_specified_
: 1;
3994 // True if the input sections attached to this output section may
3996 bool may_sort_attached_input_sections_
: 1;
3997 // True if the input sections attached to this output section must
3999 bool must_sort_attached_input_sections_
: 1;
4000 // True if the input sections attached to this output section have
4001 // already been sorted.
4002 bool attached_input_sections_are_sorted_
: 1;
4003 // True if this section holds relro data.
4005 // True if this is a small section.
4006 bool is_small_section_
: 1;
4007 // True if this is a large section.
4008 bool is_large_section_
: 1;
4009 // Whether code-fills are generated at write.
4010 bool generate_code_fills_at_write_
: 1;
4011 // Whether the entry size field should be zero.
4012 bool is_entsize_zero_
: 1;
4013 // Whether section offsets need adjustment due to relaxation.
4014 bool section_offsets_need_adjustment_
: 1;
4015 // Whether this is a NOLOAD section.
4016 bool is_noload_
: 1;
4017 // Whether this always keeps input section.
4018 bool always_keeps_input_sections_
: 1;
4019 // Whether this section has a fixed layout, for incremental update links.
4020 bool has_fixed_layout_
: 1;
4021 // True if we can add patch space to this section.
4022 bool is_patch_space_allowed_
: 1;
4023 // For SHT_TLS sections, the offset of this section relative to the base
4024 // of the TLS segment.
4025 uint64_t tls_offset_
;
4026 // Saved checkpoint.
4027 Checkpoint_output_section
* checkpoint_
;
4028 // Fast lookup maps for merged and relaxed input sections.
4029 Output_section_lookup_maps
* lookup_maps_
;
4030 // List of available regions within the section, for incremental
4032 Free_list free_list_
;
4033 // Method for filling chunks of free space.
4034 Output_fill
* free_space_fill_
;
4035 // Amount added as patch space for incremental linking.
4039 // An output segment. PT_LOAD segments are built from collections of
4040 // output sections. Other segments typically point within PT_LOAD
4041 // segments, and are built directly as needed.
4043 // NOTE: We want to use the copy constructor for this class. During
4044 // relaxation, we may try built the segments multiple times. We do
4045 // that by copying the original segment list before lay-out, doing
4046 // a trial lay-out and roll-back to the saved copied if we need to
4047 // to the lay-out again.
4049 class Output_segment
4052 // Create an output segment, specifying the type and flags.
4053 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
4055 // Return the virtual address.
4058 { return this->vaddr_
; }
4060 // Return the physical address.
4063 { return this->paddr_
; }
4065 // Return the segment type.
4068 { return this->type_
; }
4070 // Return the segment flags.
4073 { return this->flags_
; }
4075 // Return the memory size.
4078 { return this->memsz_
; }
4080 // Return the file size.
4083 { return this->filesz_
; }
4085 // Return the file offset.
4088 { return this->offset_
; }
4090 // Whether this is a segment created to hold large data sections.
4092 is_large_data_segment() const
4093 { return this->is_large_data_segment_
; }
4095 // Record that this is a segment created to hold large data
4098 set_is_large_data_segment()
4099 { this->is_large_data_segment_
= true; }
4101 // Return the maximum alignment of the Output_data.
4103 maximum_alignment();
4105 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
4106 // the segment flags to use.
4108 add_output_section_to_load(Layout
* layout
, Output_section
* os
,
4109 elfcpp::Elf_Word seg_flags
);
4111 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
4112 // is the segment flags to use.
4114 add_output_section_to_nonload(Output_section
* os
,
4115 elfcpp::Elf_Word seg_flags
);
4117 // Remove an Output_section from this segment. It is an error if it
4120 remove_output_section(Output_section
* os
);
4122 // Add an Output_data (which need not be an Output_section) to the
4123 // start of this segment.
4125 add_initial_output_data(Output_data
*);
4127 // Return true if this segment has any sections which hold actual
4128 // data, rather than being a BSS section.
4130 has_any_data_sections() const;
4132 // Whether this segment has a dynamic relocs.
4134 has_dynamic_reloc() const;
4136 // Return the address of the first section.
4138 first_section_load_address() const;
4140 // Return whether the addresses have been set already.
4142 are_addresses_set() const
4143 { return this->are_addresses_set_
; }
4145 // Set the addresses.
4147 set_addresses(uint64_t vaddr
, uint64_t paddr
)
4149 this->vaddr_
= vaddr
;
4150 this->paddr_
= paddr
;
4151 this->are_addresses_set_
= true;
4154 // Update the flags for the flags of an output section added to this
4157 update_flags_for_output_section(elfcpp::Elf_Xword flags
)
4159 // The ELF ABI specifies that a PT_TLS segment should always have
4160 // PF_R as the flags.
4161 if (this->type() != elfcpp::PT_TLS
)
4162 this->flags_
|= flags
;
4165 // Set the segment flags. This is only used if we have a PHDRS
4166 // clause which explicitly specifies the flags.
4168 set_flags(elfcpp::Elf_Word flags
)
4169 { this->flags_
= flags
; }
4171 // Set the address of the segment to ADDR and the offset to *POFF
4172 // and set the addresses and offsets of all contained output
4173 // sections accordingly. Set the section indexes of all contained
4174 // output sections starting with *PSHNDX. If RESET is true, first
4175 // reset the addresses of the contained sections. Return the
4176 // address of the immediately following segment. Update *POFF and
4177 // *PSHNDX. This should only be called for a PT_LOAD segment.
4179 set_section_addresses(Layout
*, bool reset
, uint64_t addr
,
4180 unsigned int* increase_relro
, bool* has_relro
,
4181 off_t
* poff
, unsigned int* pshndx
);
4183 // Set the minimum alignment of this segment. This may be adjusted
4184 // upward based on the section alignments.
4186 set_minimum_p_align(uint64_t align
)
4188 if (align
> this->min_p_align_
)
4189 this->min_p_align_
= align
;
4192 // Set the offset of this segment based on the section. This should
4193 // only be called for a non-PT_LOAD segment.
4195 set_offset(unsigned int increase
);
4197 // Set the TLS offsets of the sections contained in the PT_TLS segment.
4201 // Return the number of output sections.
4203 output_section_count() const;
4205 // Return the section attached to the list segment with the lowest
4206 // load address. This is used when handling a PHDRS clause in a
4209 section_with_lowest_load_address() const;
4211 // Write the segment header into *OPHDR.
4212 template<int size
, bool big_endian
>
4214 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
4216 // Write the section headers of associated sections into V.
4217 template<int size
, bool big_endian
>
4219 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
4220 unsigned int* pshndx
) const;
4222 // Print the output sections in the map file.
4224 print_sections_to_mapfile(Mapfile
*) const;
4227 typedef std::vector
<Output_data
*> Output_data_list
;
4229 // Find the maximum alignment in an Output_data_list.
4231 maximum_alignment_list(const Output_data_list
*);
4233 // Return whether the first data section is a relro section.
4235 is_first_section_relro() const;
4237 // Set the section addresses in an Output_data_list.
4239 set_section_list_addresses(Layout
*, bool reset
, Output_data_list
*,
4240 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
4243 // Return the number of Output_sections in an Output_data_list.
4245 output_section_count_list(const Output_data_list
*) const;
4247 // Return whether an Output_data_list has a dynamic reloc.
4249 has_dynamic_reloc_list(const Output_data_list
*) const;
4251 // Find the section with the lowest load address in an
4252 // Output_data_list.
4254 lowest_load_address_in_list(const Output_data_list
* pdl
,
4255 Output_section
** found
,
4256 uint64_t* found_lma
) const;
4258 // Find the first and last entries by address.
4260 find_first_and_last_list(const Output_data_list
* pdl
,
4261 const Output_data
** pfirst
,
4262 const Output_data
** plast
) const;
4264 // Write the section headers in the list into V.
4265 template<int size
, bool big_endian
>
4267 write_section_headers_list(const Layout
*, const Stringpool
*,
4268 const Output_data_list
*, unsigned char* v
,
4269 unsigned int* pshdx
) const;
4271 // Print a section list to the mapfile.
4273 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
4275 // NOTE: We want to use the copy constructor. Currently, shallow copy
4276 // works for us so we do not need to write our own copy constructor.
4278 // The list of output data attached to this segment.
4279 Output_data_list output_lists_
[ORDER_MAX
];
4280 // The segment virtual address.
4282 // The segment physical address.
4284 // The size of the segment in memory.
4286 // The maximum section alignment. The is_max_align_known_ field
4287 // indicates whether this has been finalized.
4288 uint64_t max_align_
;
4289 // The required minimum value for the p_align field. This is used
4290 // for PT_LOAD segments. Note that this does not mean that
4291 // addresses should be aligned to this value; it means the p_paddr
4292 // and p_vaddr fields must be congruent modulo this value. For
4293 // non-PT_LOAD segments, the dynamic linker works more efficiently
4294 // if the p_align field has the more conventional value, although it
4295 // can align as needed.
4296 uint64_t min_p_align_
;
4297 // The offset of the segment data within the file.
4299 // The size of the segment data in the file.
4301 // The segment type;
4302 elfcpp::Elf_Word type_
;
4303 // The segment flags.
4304 elfcpp::Elf_Word flags_
;
4305 // Whether we have finalized max_align_.
4306 bool is_max_align_known_
: 1;
4307 // Whether vaddr and paddr were set by a linker script.
4308 bool are_addresses_set_
: 1;
4309 // Whether this segment holds large data sections.
4310 bool is_large_data_segment_
: 1;
4313 // This class represents the output file.
4318 Output_file(const char* name
);
4320 // Indicate that this is a temporary file which should not be
4324 { this->is_temporary_
= true; }
4326 // Try to open an existing file. Returns false if the file doesn't
4327 // exist, has a size of 0 or can't be mmaped. This method is
4328 // thread-unsafe. If BASE_NAME is not NULL, use the contents of
4329 // that file as the base for incremental linking.
4331 open_base_file(const char* base_name
, bool writable
);
4333 // Open the output file. FILE_SIZE is the final size of the file.
4334 // If the file already exists, it is deleted/truncated. This method
4335 // is thread-unsafe.
4337 open(off_t file_size
);
4339 // Resize the output file. This method is thread-unsafe.
4341 resize(off_t file_size
);
4343 // Close the output file (flushing all buffered data) and make sure
4344 // there are no errors. This method is thread-unsafe.
4348 // Return the size of this file.
4351 { return this->file_size_
; }
4353 // Return the name of this file.
4356 { return this->name_
; }
4358 // We currently always use mmap which makes the view handling quite
4359 // simple. In the future we may support other approaches.
4361 // Write data to the output file.
4363 write(off_t offset
, const void* data
, size_t len
)
4364 { memcpy(this->base_
+ offset
, data
, len
); }
4366 // Get a buffer to use to write to the file, given the offset into
4367 // the file and the size.
4369 get_output_view(off_t start
, size_t size
)
4371 gold_assert(start
>= 0
4372 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
4373 return this->base_
+ start
;
4376 // VIEW must have been returned by get_output_view. Write the
4377 // buffer to the file, passing in the offset and the size.
4379 write_output_view(off_t
, size_t, unsigned char*)
4382 // Get a read/write buffer. This is used when we want to write part
4383 // of the file, read it in, and write it again.
4385 get_input_output_view(off_t start
, size_t size
)
4386 { return this->get_output_view(start
, size
); }
4388 // Write a read/write buffer back to the file.
4390 write_input_output_view(off_t
, size_t, unsigned char*)
4393 // Get a read buffer. This is used when we just want to read part
4394 // of the file back it in.
4395 const unsigned char*
4396 get_input_view(off_t start
, size_t size
)
4397 { return this->get_output_view(start
, size
); }
4399 // Release a read bfufer.
4401 free_input_view(off_t
, size_t, const unsigned char*)
4405 // Map the file into memory or, if that fails, allocate anonymous
4410 // Allocate anonymous memory for the file.
4414 // Map the file into memory.
4416 map_no_anonymous(bool);
4418 // Unmap the file from memory (and flush to disk buffers).
4428 // Base of file mapped into memory.
4429 unsigned char* base_
;
4430 // True iff base_ points to a memory buffer rather than an output file.
4431 bool map_is_anonymous_
;
4432 // True if base_ was allocated using new rather than mmap.
4433 bool map_is_allocated_
;
4434 // True if this is a temporary file which should not be output.
4438 } // End namespace gold.
4440 #endif // !defined(GOLD_OUTPUT_H)