1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "reloc-types.h"
37 class General_options
;
41 class Output_merge_base
;
43 class Relocatable_relocs
;
45 template<int size
, bool big_endian
>
47 template<int size
, bool big_endian
>
50 // This class specifies an input section. It is used as a key type
53 class Input_section_specifier
56 Input_section_specifier(const Relobj
* relobj
, unsigned int shndx
)
57 : relobj_(relobj
), shndx_(shndx
)
60 // Return Relobj of this.
63 { return this->relobj_
; }
65 // Return section index of this.
68 { return this->shndx_
; }
70 // Whether this equals to another specifier ISS.
72 eq(const Input_section_specifier
& iss
) const
73 { return this->relobj_
== iss
.relobj_
&& this->shndx_
== iss
.shndx_
; }
75 // Compute a hash value of this.
78 { return this->string_hash(this->relobj_
->name().c_str()) ^ this->shndx_
; }
80 // Functors for containers.
84 operator()(const Input_section_specifier
& iss1
,
85 const Input_section_specifier
& iss2
) const
86 { return iss1
.eq(iss2
); }
92 operator()(const Input_section_specifier
& iss
) const
93 { return iss
.hash_value(); }
97 // For portability, we use our own string hash function instead of assuming
98 // __gnu_cxx::hash or std::tr1::hash is available. This is the same hash
99 // function used in Stringpool_template::string_hash.
101 string_hash(const char* s
)
110 const Relobj
* relobj_
;
115 // An abtract class for data which has to go into the output file.
120 explicit Output_data()
121 : address_(0), data_size_(0), offset_(-1),
122 is_address_valid_(false), is_data_size_valid_(false),
123 is_offset_valid_(false), is_data_size_fixed_(false),
124 dynamic_reloc_count_(0)
130 // Return the address. For allocated sections, this is only valid
131 // after Layout::finalize is finished.
135 gold_assert(this->is_address_valid_
);
136 return this->address_
;
139 // Return the size of the data. For allocated sections, this must
140 // be valid after Layout::finalize calls set_address, but need not
141 // be valid before then.
145 gold_assert(this->is_data_size_valid_
);
146 return this->data_size_
;
149 // Return true if data size is fixed.
151 is_data_size_fixed() const
152 { return this->is_data_size_fixed_
; }
154 // Return the file offset. This is only valid after
155 // Layout::finalize is finished. For some non-allocated sections,
156 // it may not be valid until near the end of the link.
160 gold_assert(this->is_offset_valid_
);
161 return this->offset_
;
164 // Reset the address and file offset. This essentially disables the
165 // sanity testing about duplicate and unknown settings.
167 reset_address_and_file_offset()
169 this->is_address_valid_
= false;
170 this->is_offset_valid_
= false;
171 if (!this->is_data_size_fixed_
)
172 this->is_data_size_valid_
= false;
173 this->do_reset_address_and_file_offset();
176 // Return true if address and file offset already have reset values. In
177 // other words, calling reset_address_and_file_offset will not change them.
179 address_and_file_offset_have_reset_values() const
180 { return this->do_address_and_file_offset_have_reset_values(); }
182 // Return the required alignment.
185 { return this->do_addralign(); }
187 // Return whether this has a load address.
189 has_load_address() const
190 { return this->do_has_load_address(); }
192 // Return the load address.
195 { return this->do_load_address(); }
197 // Return whether this is an Output_section.
200 { return this->do_is_section(); }
202 // Return whether this is an Output_section of the specified type.
204 is_section_type(elfcpp::Elf_Word stt
) const
205 { return this->do_is_section_type(stt
); }
207 // Return whether this is an Output_section with the specified flag
210 is_section_flag_set(elfcpp::Elf_Xword shf
) const
211 { return this->do_is_section_flag_set(shf
); }
213 // Return the output section that this goes in, if there is one.
216 { return this->do_output_section(); }
218 // Return the output section index, if there is an output section.
221 { return this->do_out_shndx(); }
223 // Set the output section index, if this is an output section.
225 set_out_shndx(unsigned int shndx
)
226 { this->do_set_out_shndx(shndx
); }
228 // Set the address and file offset of this data, and finalize the
229 // size of the data. This is called during Layout::finalize for
230 // allocated sections.
232 set_address_and_file_offset(uint64_t addr
, off_t off
)
234 this->set_address(addr
);
235 this->set_file_offset(off
);
236 this->finalize_data_size();
241 set_address(uint64_t addr
)
243 gold_assert(!this->is_address_valid_
);
244 this->address_
= addr
;
245 this->is_address_valid_
= true;
248 // Set the file offset.
250 set_file_offset(off_t off
)
252 gold_assert(!this->is_offset_valid_
);
254 this->is_offset_valid_
= true;
257 // Finalize the data size.
261 if (!this->is_data_size_valid_
)
263 // Tell the child class to set the data size.
264 this->set_final_data_size();
265 gold_assert(this->is_data_size_valid_
);
269 // Set the TLS offset. Called only for SHT_TLS sections.
271 set_tls_offset(uint64_t tls_base
)
272 { this->do_set_tls_offset(tls_base
); }
274 // Return the TLS offset, relative to the base of the TLS segment.
275 // Valid only for SHT_TLS sections.
278 { return this->do_tls_offset(); }
280 // Write the data to the output file. This is called after
281 // Layout::finalize is complete.
283 write(Output_file
* file
)
284 { this->do_write(file
); }
286 // This is called by Layout::finalize to note that the sizes of
287 // allocated sections must now be fixed.
290 { Output_data::allocated_sizes_are_fixed
= true; }
292 // Used to check that layout has been done.
295 { return Output_data::allocated_sizes_are_fixed
; }
297 // Count the number of dynamic relocations applied to this section.
300 { ++this->dynamic_reloc_count_
; }
302 // Return the number of dynamic relocations applied to this section.
304 dynamic_reloc_count() const
305 { return this->dynamic_reloc_count_
; }
307 // Whether the address is valid.
309 is_address_valid() const
310 { return this->is_address_valid_
; }
312 // Whether the file offset is valid.
314 is_offset_valid() const
315 { return this->is_offset_valid_
; }
317 // Whether the data size is valid.
319 is_data_size_valid() const
320 { return this->is_data_size_valid_
; }
322 // Print information to the map file.
324 print_to_mapfile(Mapfile
* mapfile
) const
325 { return this->do_print_to_mapfile(mapfile
); }
328 // Functions that child classes may or in some cases must implement.
330 // Write the data to the output file.
332 do_write(Output_file
*) = 0;
334 // Return the required alignment.
336 do_addralign() const = 0;
338 // Return whether this has a load address.
340 do_has_load_address() const
343 // Return the load address.
345 do_load_address() const
346 { gold_unreachable(); }
348 // Return whether this is an Output_section.
350 do_is_section() const
353 // Return whether this is an Output_section of the specified type.
354 // This only needs to be implement by Output_section.
356 do_is_section_type(elfcpp::Elf_Word
) const
359 // Return whether this is an Output_section with the specific flag
360 // set. This only needs to be implemented by Output_section.
362 do_is_section_flag_set(elfcpp::Elf_Xword
) const
365 // Return the output section, if there is one.
366 virtual Output_section
*
370 // Return the output section index, if there is an output section.
373 { gold_unreachable(); }
375 // Set the output section index, if this is an output section.
377 do_set_out_shndx(unsigned int)
378 { gold_unreachable(); }
380 // This is a hook for derived classes to set the data size. This is
381 // called by finalize_data_size, normally called during
382 // Layout::finalize, when the section address is set.
384 set_final_data_size()
385 { gold_unreachable(); }
387 // A hook for resetting the address and file offset.
389 do_reset_address_and_file_offset()
392 // Return true if address and file offset already have reset values. In
393 // other words, calling reset_address_and_file_offset will not change them.
394 // A child class overriding do_reset_address_and_file_offset may need to
395 // also override this.
397 do_address_and_file_offset_have_reset_values() const
398 { return !this->is_address_valid_
&& !this->is_offset_valid_
; }
400 // Set the TLS offset. Called only for SHT_TLS sections.
402 do_set_tls_offset(uint64_t)
403 { gold_unreachable(); }
405 // Return the TLS offset, relative to the base of the TLS segment.
406 // Valid only for SHT_TLS sections.
408 do_tls_offset() const
409 { gold_unreachable(); }
411 // Print to the map file. This only needs to be implemented by
412 // classes which may appear in a PT_LOAD segment.
414 do_print_to_mapfile(Mapfile
*) const
415 { gold_unreachable(); }
417 // Functions that child classes may call.
419 // Reset the address. The Output_section class needs this when an
420 // SHF_ALLOC input section is added to an output section which was
421 // formerly not SHF_ALLOC.
423 mark_address_invalid()
424 { this->is_address_valid_
= false; }
426 // Set the size of the data.
428 set_data_size(off_t data_size
)
430 gold_assert(!this->is_data_size_valid_
431 && !this->is_data_size_fixed_
);
432 this->data_size_
= data_size
;
433 this->is_data_size_valid_
= true;
436 // Fix the data size. Once it is fixed, it cannot be changed
437 // and the data size remains always valid.
441 gold_assert(this->is_data_size_valid_
);
442 this->is_data_size_fixed_
= true;
445 // Get the current data size--this is for the convenience of
446 // sections which build up their size over time.
448 current_data_size_for_child() const
449 { return this->data_size_
; }
451 // Set the current data size--this is for the convenience of
452 // sections which build up their size over time.
454 set_current_data_size_for_child(off_t data_size
)
456 gold_assert(!this->is_data_size_valid_
);
457 this->data_size_
= data_size
;
460 // Return default alignment for the target size.
464 // Return default alignment for a specified size--32 or 64.
466 default_alignment_for_size(int size
);
469 Output_data(const Output_data
&);
470 Output_data
& operator=(const Output_data
&);
472 // This is used for verification, to make sure that we don't try to
473 // change any sizes of allocated sections after we set the section
475 static bool allocated_sizes_are_fixed
;
477 // Memory address in output file.
479 // Size of data in output file.
481 // File offset of contents in output file.
483 // Whether address_ is valid.
484 bool is_address_valid_
;
485 // Whether data_size_ is valid.
486 bool is_data_size_valid_
;
487 // Whether offset_ is valid.
488 bool is_offset_valid_
;
489 // Whether data size is fixed.
490 bool is_data_size_fixed_
;
491 // Count of dynamic relocations applied to this section.
492 unsigned int dynamic_reloc_count_
;
495 // Output the section headers.
497 class Output_section_headers
: public Output_data
500 Output_section_headers(const Layout
*,
501 const Layout::Segment_list
*,
502 const Layout::Section_list
*,
503 const Layout::Section_list
*,
505 const Output_section
*);
508 // Write the data to the file.
510 do_write(Output_file
*);
512 // Return the required alignment.
515 { return Output_data::default_alignment(); }
517 // Write to a map file.
519 do_print_to_mapfile(Mapfile
* mapfile
) const
520 { mapfile
->print_output_data(this, _("** section headers")); }
522 // Set final data size.
524 set_final_data_size()
525 { this->set_data_size(this->do_size()); }
528 // Write the data to the file with the right size and endianness.
529 template<int size
, bool big_endian
>
531 do_sized_write(Output_file
*);
533 // Compute data size.
537 const Layout
* layout_
;
538 const Layout::Segment_list
* segment_list_
;
539 const Layout::Section_list
* section_list_
;
540 const Layout::Section_list
* unattached_section_list_
;
541 const Stringpool
* secnamepool_
;
542 const Output_section
* shstrtab_section_
;
545 // Output the segment headers.
547 class Output_segment_headers
: public Output_data
550 Output_segment_headers(const Layout::Segment_list
& segment_list
);
553 // Write the data to the file.
555 do_write(Output_file
*);
557 // Return the required alignment.
560 { return Output_data::default_alignment(); }
562 // Write to a map file.
564 do_print_to_mapfile(Mapfile
* mapfile
) const
565 { mapfile
->print_output_data(this, _("** segment headers")); }
567 // Set final data size.
569 set_final_data_size()
570 { this->set_data_size(this->do_size()); }
573 // Write the data to the file with the right size and endianness.
574 template<int size
, bool big_endian
>
576 do_sized_write(Output_file
*);
578 // Compute the current size.
582 const Layout::Segment_list
& segment_list_
;
585 // Output the ELF file header.
587 class Output_file_header
: public Output_data
590 Output_file_header(const Target
*,
592 const Output_segment_headers
*,
595 // Add information about the section headers. We lay out the ELF
596 // file header before we create the section headers.
597 void set_section_info(const Output_section_headers
*,
598 const Output_section
* shstrtab
);
601 // Write the data to the file.
603 do_write(Output_file
*);
605 // Return the required alignment.
608 { return Output_data::default_alignment(); }
610 // Write to a map file.
612 do_print_to_mapfile(Mapfile
* mapfile
) const
613 { mapfile
->print_output_data(this, _("** file header")); }
615 // Set final data size.
617 set_final_data_size(void)
618 { this->set_data_size(this->do_size()); }
621 // Write the data to the file with the right size and endianness.
622 template<int size
, bool big_endian
>
624 do_sized_write(Output_file
*);
626 // Return the value to use for the entry address.
628 typename
elfcpp::Elf_types
<size
>::Elf_Addr
631 // Compute the current data size.
635 const Target
* target_
;
636 const Symbol_table
* symtab_
;
637 const Output_segment_headers
* segment_header_
;
638 const Output_section_headers
* section_header_
;
639 const Output_section
* shstrtab_
;
643 // Output sections are mainly comprised of input sections. However,
644 // there are cases where we have data to write out which is not in an
645 // input section. Output_section_data is used in such cases. This is
646 // an abstract base class.
648 class Output_section_data
: public Output_data
651 Output_section_data(off_t data_size
, uint64_t addralign
,
652 bool is_data_size_fixed
)
653 : Output_data(), output_section_(NULL
), addralign_(addralign
)
655 this->set_data_size(data_size
);
656 if (is_data_size_fixed
)
657 this->fix_data_size();
660 Output_section_data(uint64_t addralign
)
661 : Output_data(), output_section_(NULL
), addralign_(addralign
)
664 // Return the output section.
665 const Output_section
*
666 output_section() const
667 { return this->output_section_
; }
669 // Record the output section.
671 set_output_section(Output_section
* os
);
673 // Add an input section, for SHF_MERGE sections. This returns true
674 // if the section was handled.
676 add_input_section(Relobj
* object
, unsigned int shndx
)
677 { return this->do_add_input_section(object
, shndx
); }
679 // Given an input OBJECT, an input section index SHNDX within that
680 // object, and an OFFSET relative to the start of that input
681 // section, return whether or not the corresponding offset within
682 // the output section is known. If this function returns true, it
683 // sets *POUTPUT to the output offset. The value -1 indicates that
684 // this input offset is being discarded.
686 output_offset(const Relobj
* object
, unsigned int shndx
,
687 section_offset_type offset
,
688 section_offset_type
*poutput
) const
689 { return this->do_output_offset(object
, shndx
, offset
, poutput
); }
691 // Return whether this is the merge section for the input section
692 // SHNDX in OBJECT. This should return true when output_offset
693 // would return true for some values of OFFSET.
695 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const
696 { return this->do_is_merge_section_for(object
, shndx
); }
698 // Write the contents to a buffer. This is used for sections which
699 // require postprocessing, such as compression.
701 write_to_buffer(unsigned char* buffer
)
702 { this->do_write_to_buffer(buffer
); }
704 // Print merge stats to stderr. This should only be called for
705 // SHF_MERGE sections.
707 print_merge_stats(const char* section_name
)
708 { this->do_print_merge_stats(section_name
); }
711 // The child class must implement do_write.
713 // The child class may implement specific adjustments to the output
716 do_adjust_output_section(Output_section
*)
719 // May be implemented by child class. Return true if the section
722 do_add_input_section(Relobj
*, unsigned int)
723 { gold_unreachable(); }
725 // The child class may implement output_offset.
727 do_output_offset(const Relobj
*, unsigned int, section_offset_type
,
728 section_offset_type
*) const
731 // The child class may implement is_merge_section_for.
733 do_is_merge_section_for(const Relobj
*, unsigned int) const
736 // The child class may implement write_to_buffer. Most child
737 // classes can not appear in a compressed section, and they do not
740 do_write_to_buffer(unsigned char*)
741 { gold_unreachable(); }
743 // Print merge statistics.
745 do_print_merge_stats(const char*)
746 { gold_unreachable(); }
748 // Return the required alignment.
751 { return this->addralign_
; }
753 // Return the output section.
756 { return this->output_section_
; }
758 // Return the section index of the output section.
760 do_out_shndx() const;
762 // Set the alignment.
764 set_addralign(uint64_t addralign
);
767 // The output section for this section.
768 Output_section
* output_section_
;
769 // The required alignment.
773 // Some Output_section_data classes build up their data step by step,
774 // rather than all at once. This class provides an interface for
777 class Output_section_data_build
: public Output_section_data
780 Output_section_data_build(uint64_t addralign
)
781 : Output_section_data(addralign
)
784 // Get the current data size.
786 current_data_size() const
787 { return this->current_data_size_for_child(); }
789 // Set the current data size.
791 set_current_data_size(off_t data_size
)
792 { this->set_current_data_size_for_child(data_size
); }
795 // Set the final data size.
797 set_final_data_size()
798 { this->set_data_size(this->current_data_size_for_child()); }
801 // A simple case of Output_data in which we have constant data to
804 class Output_data_const
: public Output_section_data
807 Output_data_const(const std::string
& data
, uint64_t addralign
)
808 : Output_section_data(data
.size(), addralign
, true), data_(data
)
811 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
812 : Output_section_data(len
, addralign
, true), data_(p
, len
)
815 Output_data_const(const unsigned char* p
, off_t len
, uint64_t addralign
)
816 : Output_section_data(len
, addralign
, true),
817 data_(reinterpret_cast<const char*>(p
), len
)
821 // Write the data to the output file.
823 do_write(Output_file
*);
825 // Write the data to a buffer.
827 do_write_to_buffer(unsigned char* buffer
)
828 { memcpy(buffer
, this->data_
.data(), this->data_
.size()); }
830 // Write to a map file.
832 do_print_to_mapfile(Mapfile
* mapfile
) const
833 { mapfile
->print_output_data(this, _("** fill")); }
839 // Another version of Output_data with constant data, in which the
840 // buffer is allocated by the caller.
842 class Output_data_const_buffer
: public Output_section_data
845 Output_data_const_buffer(const unsigned char* p
, off_t len
,
846 uint64_t addralign
, const char* map_name
)
847 : Output_section_data(len
, addralign
, true),
848 p_(p
), map_name_(map_name
)
852 // Write the data the output file.
854 do_write(Output_file
*);
856 // Write the data to a buffer.
858 do_write_to_buffer(unsigned char* buffer
)
859 { memcpy(buffer
, this->p_
, this->data_size()); }
861 // Write to a map file.
863 do_print_to_mapfile(Mapfile
* mapfile
) const
864 { mapfile
->print_output_data(this, _(this->map_name_
)); }
867 // The data to output.
868 const unsigned char* p_
;
869 // Name to use in a map file. Maps are a rarely used feature, but
870 // the space usage is minor as aren't very many of these objects.
871 const char* map_name_
;
874 // A place holder for a fixed amount of data written out via some
877 class Output_data_fixed_space
: public Output_section_data
880 Output_data_fixed_space(off_t data_size
, uint64_t addralign
,
881 const char* map_name
)
882 : Output_section_data(data_size
, addralign
, true),
887 // Write out the data--the actual data must be written out
890 do_write(Output_file
*)
893 // Write to a map file.
895 do_print_to_mapfile(Mapfile
* mapfile
) const
896 { mapfile
->print_output_data(this, _(this->map_name_
)); }
899 // Name to use in a map file. Maps are a rarely used feature, but
900 // the space usage is minor as aren't very many of these objects.
901 const char* map_name_
;
904 // A place holder for variable sized data written out via some other
907 class Output_data_space
: public Output_section_data_build
910 explicit Output_data_space(uint64_t addralign
, const char* map_name
)
911 : Output_section_data_build(addralign
),
915 // Set the alignment.
917 set_space_alignment(uint64_t align
)
918 { this->set_addralign(align
); }
921 // Write out the data--the actual data must be written out
924 do_write(Output_file
*)
927 // Write to a map file.
929 do_print_to_mapfile(Mapfile
* mapfile
) const
930 { mapfile
->print_output_data(this, _(this->map_name_
)); }
933 // Name to use in a map file. Maps are a rarely used feature, but
934 // the space usage is minor as aren't very many of these objects.
935 const char* map_name_
;
938 // Fill fixed space with zeroes. This is just like
939 // Output_data_fixed_space, except that the map name is known.
941 class Output_data_zero_fill
: public Output_section_data
944 Output_data_zero_fill(off_t data_size
, uint64_t addralign
)
945 : Output_section_data(data_size
, addralign
, true)
949 // There is no data to write out.
951 do_write(Output_file
*)
954 // Write to a map file.
956 do_print_to_mapfile(Mapfile
* mapfile
) const
957 { mapfile
->print_output_data(this, "** zero fill"); }
960 // A string table which goes into an output section.
962 class Output_data_strtab
: public Output_section_data
965 Output_data_strtab(Stringpool
* strtab
)
966 : Output_section_data(1), strtab_(strtab
)
970 // This is called to set the address and file offset. Here we make
971 // sure that the Stringpool is finalized.
973 set_final_data_size();
975 // Write out the data.
977 do_write(Output_file
*);
979 // Write the data to a buffer.
981 do_write_to_buffer(unsigned char* buffer
)
982 { this->strtab_
->write_to_buffer(buffer
, this->data_size()); }
984 // Write to a map file.
986 do_print_to_mapfile(Mapfile
* mapfile
) const
987 { mapfile
->print_output_data(this, _("** string table")); }
993 // This POD class is used to represent a single reloc in the output
994 // file. This could be a private class within Output_data_reloc, but
995 // the templatization is complex enough that I broke it out into a
996 // separate class. The class is templatized on either elfcpp::SHT_REL
997 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
998 // relocation or an ordinary relocation.
1000 // A relocation can be against a global symbol, a local symbol, a
1001 // local section symbol, an output section, or the undefined symbol at
1002 // index 0. We represent the latter by using a NULL global symbol.
1004 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1007 template<bool dynamic
, int size
, bool big_endian
>
1008 class Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1011 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1012 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1014 static const Address invalid_address
= static_cast<Address
>(0) - 1;
1016 // An uninitialized entry. We need this because we want to put
1017 // instances of this class into an STL container.
1019 : local_sym_index_(INVALID_CODE
)
1022 // We have a bunch of different constructors. They come in pairs
1023 // depending on how the address of the relocation is specified. It
1024 // can either be an offset in an Output_data or an offset in an
1027 // A reloc against a global symbol.
1029 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1030 Address address
, bool is_relative
);
1032 Output_reloc(Symbol
* gsym
, unsigned int type
,
1033 Sized_relobj
<size
, big_endian
>* relobj
,
1034 unsigned int shndx
, Address address
, bool is_relative
);
1036 // A reloc against a local symbol or local section symbol.
1038 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1039 unsigned int local_sym_index
, unsigned int type
,
1040 Output_data
* od
, Address address
, bool is_relative
,
1041 bool is_section_symbol
);
1043 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1044 unsigned int local_sym_index
, unsigned int type
,
1045 unsigned int shndx
, Address address
, bool is_relative
,
1046 bool is_section_symbol
);
1048 // A reloc against the STT_SECTION symbol of an output section.
1050 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1053 Output_reloc(Output_section
* os
, unsigned int type
,
1054 Sized_relobj
<size
, big_endian
>* relobj
,
1055 unsigned int shndx
, Address address
);
1057 // Return TRUE if this is a RELATIVE relocation.
1060 { return this->is_relative_
; }
1062 // Return whether this is against a local section symbol.
1064 is_local_section_symbol() const
1066 return (this->local_sym_index_
!= GSYM_CODE
1067 && this->local_sym_index_
!= SECTION_CODE
1068 && this->local_sym_index_
!= INVALID_CODE
1069 && this->is_section_symbol_
);
1072 // For a local section symbol, return the offset of the input
1073 // section within the output section. ADDEND is the addend being
1074 // applied to the input section.
1076 local_section_offset(Addend addend
) const;
1078 // Get the value of the symbol referred to by a Rel relocation when
1079 // we are adding the given ADDEND.
1081 symbol_value(Addend addend
) const;
1083 // Write the reloc entry to an output view.
1085 write(unsigned char* pov
) const;
1087 // Write the offset and info fields to Write_rel.
1088 template<typename Write_rel
>
1089 void write_rel(Write_rel
*) const;
1091 // This is used when sorting dynamic relocs. Return -1 to sort this
1092 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1094 compare(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>& r2
)
1097 // Return whether this reloc should be sorted before the argument
1098 // when sorting dynamic relocs.
1100 sort_before(const Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>&
1102 { return this->compare(r2
) < 0; }
1105 // Record that we need a dynamic symbol index.
1107 set_needs_dynsym_index();
1109 // Return the symbol index.
1111 get_symbol_index() const;
1113 // Return the output address.
1115 get_address() const;
1117 // Codes for local_sym_index_.
1124 // Invalid uninitialized entry.
1130 // For a local symbol or local section symbol
1131 // (this->local_sym_index_ >= 0), the object. We will never
1132 // generate a relocation against a local symbol in a dynamic
1133 // object; that doesn't make sense. And our callers will always
1134 // be templatized, so we use Sized_relobj here.
1135 Sized_relobj
<size
, big_endian
>* relobj
;
1136 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1137 // symbol. If this is NULL, it indicates a relocation against the
1138 // undefined 0 symbol.
1140 // For a relocation against an output section
1141 // (this->local_sym_index_ == SECTION_CODE), the output section.
1146 // If this->shndx_ is not INVALID CODE, the object which holds the
1147 // input section being used to specify the reloc address.
1148 Sized_relobj
<size
, big_endian
>* relobj
;
1149 // If this->shndx_ is INVALID_CODE, the output data being used to
1150 // specify the reloc address. This may be NULL if the reloc
1151 // address is absolute.
1154 // The address offset within the input section or the Output_data.
1156 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1157 // relocation against an output section, or INVALID_CODE for an
1158 // uninitialized value. Otherwise, for a local symbol
1159 // (this->is_section_symbol_ is false), the local symbol index. For
1160 // a local section symbol (this->is_section_symbol_ is true), the
1161 // section index in the input file.
1162 unsigned int local_sym_index_
;
1163 // The reloc type--a processor specific code.
1164 unsigned int type_
: 30;
1165 // True if the relocation is a RELATIVE relocation.
1166 bool is_relative_
: 1;
1167 // True if the relocation is against a section symbol.
1168 bool is_section_symbol_
: 1;
1169 // If the reloc address is an input section in an object, the
1170 // section index. This is INVALID_CODE if the reloc address is
1171 // specified in some other way.
1172 unsigned int shndx_
;
1175 // The SHT_RELA version of Output_reloc<>. This is just derived from
1176 // the SHT_REL version of Output_reloc, but it adds an addend.
1178 template<bool dynamic
, int size
, bool big_endian
>
1179 class Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1182 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1183 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
1185 // An uninitialized entry.
1190 // A reloc against a global symbol.
1192 Output_reloc(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1193 Address address
, Addend addend
, bool is_relative
)
1194 : rel_(gsym
, type
, od
, address
, is_relative
), addend_(addend
)
1197 Output_reloc(Symbol
* gsym
, unsigned int type
,
1198 Sized_relobj
<size
, big_endian
>* relobj
,
1199 unsigned int shndx
, Address address
, Addend addend
,
1201 : rel_(gsym
, type
, relobj
, shndx
, address
, is_relative
), addend_(addend
)
1204 // A reloc against a local symbol.
1206 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1207 unsigned int local_sym_index
, unsigned int type
,
1208 Output_data
* od
, Address address
,
1209 Addend addend
, bool is_relative
, bool is_section_symbol
)
1210 : rel_(relobj
, local_sym_index
, type
, od
, address
, is_relative
,
1215 Output_reloc(Sized_relobj
<size
, big_endian
>* relobj
,
1216 unsigned int local_sym_index
, unsigned int type
,
1217 unsigned int shndx
, Address address
,
1218 Addend addend
, bool is_relative
, bool is_section_symbol
)
1219 : rel_(relobj
, local_sym_index
, type
, shndx
, address
, is_relative
,
1224 // A reloc against the STT_SECTION symbol of an output section.
1226 Output_reloc(Output_section
* os
, unsigned int type
, Output_data
* od
,
1227 Address address
, Addend addend
)
1228 : rel_(os
, type
, od
, address
), addend_(addend
)
1231 Output_reloc(Output_section
* os
, unsigned int type
,
1232 Sized_relobj
<size
, big_endian
>* relobj
,
1233 unsigned int shndx
, Address address
, Addend addend
)
1234 : rel_(os
, type
, relobj
, shndx
, address
), addend_(addend
)
1237 // Write the reloc entry to an output view.
1239 write(unsigned char* pov
) const;
1241 // Return whether this reloc should be sorted before the argument
1242 // when sorting dynamic relocs.
1244 sort_before(const Output_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>&
1247 int i
= this->rel_
.compare(r2
.rel_
);
1253 return this->addend_
< r2
.addend_
;
1258 Output_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
> rel_
;
1263 // Output_data_reloc is used to manage a section containing relocs.
1264 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1265 // indicates whether this is a dynamic relocation or a normal
1266 // relocation. Output_data_reloc_base is a base class.
1267 // Output_data_reloc is the real class, which we specialize based on
1270 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1271 class Output_data_reloc_base
: public Output_section_data_build
1274 typedef Output_reloc
<sh_type
, dynamic
, size
, big_endian
> Output_reloc_type
;
1275 typedef typename
Output_reloc_type::Address Address
;
1276 static const int reloc_size
=
1277 Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
1279 // Construct the section.
1280 Output_data_reloc_base(bool sort_relocs
)
1281 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1282 sort_relocs_(sort_relocs
)
1286 // Write out the data.
1288 do_write(Output_file
*);
1290 // Set the entry size and the link.
1292 do_adjust_output_section(Output_section
*os
);
1294 // Write to a map file.
1296 do_print_to_mapfile(Mapfile
* mapfile
) const
1298 mapfile
->print_output_data(this,
1300 ? _("** dynamic relocs")
1304 // Add a relocation entry.
1306 add(Output_data
*od
, const Output_reloc_type
& reloc
)
1308 this->relocs_
.push_back(reloc
);
1309 this->set_current_data_size(this->relocs_
.size() * reloc_size
);
1310 od
->add_dynamic_reloc();
1314 typedef std::vector
<Output_reloc_type
> Relocs
;
1316 // The class used to sort the relocations.
1317 struct Sort_relocs_comparison
1320 operator()(const Output_reloc_type
& r1
, const Output_reloc_type
& r2
) const
1321 { return r1
.sort_before(r2
); }
1324 // The relocations in this section.
1326 // Whether to sort the relocations when writing them out, to make
1327 // the dynamic linker more efficient.
1331 // The class which callers actually create.
1333 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
1334 class Output_data_reloc
;
1336 // The SHT_REL version of Output_data_reloc.
1338 template<bool dynamic
, int size
, bool big_endian
>
1339 class Output_data_reloc
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1340 : public Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>
1343 typedef Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
,
1347 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1348 typedef typename
Output_reloc_type::Address Address
;
1350 Output_data_reloc(bool sr
)
1351 : Output_data_reloc_base
<elfcpp::SHT_REL
, dynamic
, size
, big_endian
>(sr
)
1354 // Add a reloc against a global symbol.
1357 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
)
1358 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, false)); }
1361 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1362 Sized_relobj
<size
, big_endian
>* relobj
,
1363 unsigned int shndx
, Address address
)
1364 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1367 // These are to simplify the Copy_relocs class.
1370 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
, Address address
,
1373 gold_assert(addend
== 0);
1374 this->add_global(gsym
, type
, od
, address
);
1378 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1379 Sized_relobj
<size
, big_endian
>* relobj
,
1380 unsigned int shndx
, Address address
, Address addend
)
1382 gold_assert(addend
== 0);
1383 this->add_global(gsym
, type
, od
, relobj
, shndx
, address
);
1386 // Add a RELATIVE reloc against a global symbol. The final relocation
1387 // will not reference the symbol.
1390 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1392 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, true)); }
1395 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1396 Sized_relobj
<size
, big_endian
>* relobj
,
1397 unsigned int shndx
, Address address
)
1399 this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1403 // Add a reloc against a local symbol.
1406 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1407 unsigned int local_sym_index
, unsigned int type
,
1408 Output_data
* od
, Address address
)
1410 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1411 address
, false, false));
1415 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1416 unsigned int local_sym_index
, unsigned int type
,
1417 Output_data
* od
, unsigned int shndx
, Address address
)
1419 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1420 address
, false, false));
1423 // Add a RELATIVE reloc against a local symbol.
1426 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1427 unsigned int local_sym_index
, unsigned int type
,
1428 Output_data
* od
, Address address
)
1430 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
,
1431 address
, true, false));
1435 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1436 unsigned int local_sym_index
, unsigned int type
,
1437 Output_data
* od
, unsigned int shndx
, Address address
)
1439 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1440 address
, true, false));
1443 // Add a reloc against a local section symbol. This will be
1444 // converted into a reloc against the STT_SECTION symbol of the
1448 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1449 unsigned int input_shndx
, unsigned int type
,
1450 Output_data
* od
, Address address
)
1452 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
,
1453 address
, false, true));
1457 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1458 unsigned int input_shndx
, unsigned int type
,
1459 Output_data
* od
, unsigned int shndx
, Address address
)
1461 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1462 address
, false, true));
1465 // A reloc against the STT_SECTION symbol of an output section.
1466 // OS is the Output_section that the relocation refers to; OD is
1467 // the Output_data object being relocated.
1470 add_output_section(Output_section
* os
, unsigned int type
,
1471 Output_data
* od
, Address address
)
1472 { this->add(od
, Output_reloc_type(os
, type
, od
, address
)); }
1475 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1476 Sized_relobj
<size
, big_endian
>* relobj
,
1477 unsigned int shndx
, Address address
)
1478 { this->add(od
, Output_reloc_type(os
, type
, relobj
, shndx
, address
)); }
1481 // The SHT_RELA version of Output_data_reloc.
1483 template<bool dynamic
, int size
, bool big_endian
>
1484 class Output_data_reloc
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1485 : public Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>
1488 typedef Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
,
1492 typedef typename
Base::Output_reloc_type Output_reloc_type
;
1493 typedef typename
Output_reloc_type::Address Address
;
1494 typedef typename
Output_reloc_type::Addend Addend
;
1496 Output_data_reloc(bool sr
)
1497 : Output_data_reloc_base
<elfcpp::SHT_RELA
, dynamic
, size
, big_endian
>(sr
)
1500 // Add a reloc against a global symbol.
1503 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1504 Address address
, Addend addend
)
1505 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
,
1509 add_global(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1510 Sized_relobj
<size
, big_endian
>* relobj
,
1511 unsigned int shndx
, Address address
,
1513 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1516 // Add a RELATIVE reloc against a global symbol. The final output
1517 // relocation will not reference the symbol, but we must keep the symbol
1518 // information long enough to set the addend of the relocation correctly
1519 // when it is written.
1522 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1523 Address address
, Addend addend
)
1524 { this->add(od
, Output_reloc_type(gsym
, type
, od
, address
, addend
, true)); }
1527 add_global_relative(Symbol
* gsym
, unsigned int type
, Output_data
* od
,
1528 Sized_relobj
<size
, big_endian
>* relobj
,
1529 unsigned int shndx
, Address address
, Addend addend
)
1530 { this->add(od
, Output_reloc_type(gsym
, type
, relobj
, shndx
, address
,
1533 // Add a reloc against a local symbol.
1536 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1537 unsigned int local_sym_index
, unsigned int type
,
1538 Output_data
* od
, Address address
, Addend addend
)
1540 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1541 addend
, false, false));
1545 add_local(Sized_relobj
<size
, big_endian
>* relobj
,
1546 unsigned int local_sym_index
, unsigned int type
,
1547 Output_data
* od
, unsigned int shndx
, Address address
,
1550 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1551 address
, addend
, false, false));
1554 // Add a RELATIVE reloc against a local symbol.
1557 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1558 unsigned int local_sym_index
, unsigned int type
,
1559 Output_data
* od
, Address address
, Addend addend
)
1561 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, od
, address
,
1562 addend
, true, false));
1566 add_local_relative(Sized_relobj
<size
, big_endian
>* relobj
,
1567 unsigned int local_sym_index
, unsigned int type
,
1568 Output_data
* od
, unsigned int shndx
, Address address
,
1571 this->add(od
, Output_reloc_type(relobj
, local_sym_index
, type
, shndx
,
1572 address
, addend
, true, false));
1575 // Add a reloc against a local section symbol. This will be
1576 // converted into a reloc against the STT_SECTION symbol of the
1580 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1581 unsigned int input_shndx
, unsigned int type
,
1582 Output_data
* od
, Address address
, Addend addend
)
1584 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, od
, address
,
1585 addend
, false, true));
1589 add_local_section(Sized_relobj
<size
, big_endian
>* relobj
,
1590 unsigned int input_shndx
, unsigned int type
,
1591 Output_data
* od
, unsigned int shndx
, Address address
,
1594 this->add(od
, Output_reloc_type(relobj
, input_shndx
, type
, shndx
,
1595 address
, addend
, false, true));
1598 // A reloc against the STT_SECTION symbol of an output section.
1601 add_output_section(Output_section
* os
, unsigned int type
, Output_data
* od
,
1602 Address address
, Addend addend
)
1603 { this->add(os
, Output_reloc_type(os
, type
, od
, address
, addend
)); }
1606 add_output_section(Output_section
* os
, unsigned int type
,
1607 Sized_relobj
<size
, big_endian
>* relobj
,
1608 unsigned int shndx
, Address address
, Addend addend
)
1609 { this->add(os
, Output_reloc_type(os
, type
, relobj
, shndx
, address
,
1613 // Output_relocatable_relocs represents a relocation section in a
1614 // relocatable link. The actual data is written out in the target
1615 // hook relocate_for_relocatable. This just saves space for it.
1617 template<int sh_type
, int size
, bool big_endian
>
1618 class Output_relocatable_relocs
: public Output_section_data
1621 Output_relocatable_relocs(Relocatable_relocs
* rr
)
1622 : Output_section_data(Output_data::default_alignment_for_size(size
)),
1627 set_final_data_size();
1629 // Write out the data. There is nothing to do here.
1631 do_write(Output_file
*)
1634 // Write to a map file.
1636 do_print_to_mapfile(Mapfile
* mapfile
) const
1637 { mapfile
->print_output_data(this, _("** relocs")); }
1640 // The relocs associated with this input section.
1641 Relocatable_relocs
* rr_
;
1644 // Handle a GROUP section.
1646 template<int size
, bool big_endian
>
1647 class Output_data_group
: public Output_section_data
1650 // The constructor clears *INPUT_SHNDXES.
1651 Output_data_group(Sized_relobj
<size
, big_endian
>* relobj
,
1652 section_size_type entry_count
,
1653 elfcpp::Elf_Word flags
,
1654 std::vector
<unsigned int>* input_shndxes
);
1657 do_write(Output_file
*);
1659 // Write to a map file.
1661 do_print_to_mapfile(Mapfile
* mapfile
) const
1662 { mapfile
->print_output_data(this, _("** group")); }
1664 // Set final data size.
1666 set_final_data_size()
1667 { this->set_data_size((this->input_shndxes_
.size() + 1) * 4); }
1670 // The input object.
1671 Sized_relobj
<size
, big_endian
>* relobj_
;
1672 // The group flag word.
1673 elfcpp::Elf_Word flags_
;
1674 // The section indexes of the input sections in this group.
1675 std::vector
<unsigned int> input_shndxes_
;
1678 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1679 // for one symbol--either a global symbol or a local symbol in an
1680 // object. The target specific code adds entries to the GOT as
1683 template<int size
, bool big_endian
>
1684 class Output_data_got
: public Output_section_data_build
1687 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1688 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
> Rel_dyn
;
1689 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1692 : Output_section_data_build(Output_data::default_alignment_for_size(size
)),
1696 // Add an entry for a global symbol to the GOT. Return true if this
1697 // is a new GOT entry, false if the symbol was already in the GOT.
1699 add_global(Symbol
* gsym
, unsigned int got_type
);
1701 // Add an entry for a global symbol to the GOT, and add a dynamic
1702 // relocation of type R_TYPE for the GOT entry.
1704 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
1705 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1708 add_global_with_rela(Symbol
* gsym
, unsigned int got_type
,
1709 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1711 // Add a pair of entries for a global symbol to the GOT, and add
1712 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1714 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
1715 Rel_dyn
* rel_dyn
, unsigned int r_type_1
,
1716 unsigned int r_type_2
);
1719 add_global_pair_with_rela(Symbol
* gsym
, unsigned int got_type
,
1720 Rela_dyn
* rela_dyn
, unsigned int r_type_1
,
1721 unsigned int r_type_2
);
1723 // Add an entry for a local symbol to the GOT. This returns true if
1724 // this is a new GOT entry, false if the symbol already has a GOT
1727 add_local(Sized_relobj
<size
, big_endian
>* object
, unsigned int sym_index
,
1728 unsigned int got_type
);
1730 // Add an entry for a local symbol to the GOT, and add a dynamic
1731 // relocation of type R_TYPE for the GOT entry.
1733 add_local_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1734 unsigned int sym_index
, unsigned int got_type
,
1735 Rel_dyn
* rel_dyn
, unsigned int r_type
);
1738 add_local_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1739 unsigned int sym_index
, unsigned int got_type
,
1740 Rela_dyn
* rela_dyn
, unsigned int r_type
);
1742 // Add a pair of entries for a local symbol to the GOT, and add
1743 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1745 add_local_pair_with_rel(Sized_relobj
<size
, big_endian
>* object
,
1746 unsigned int sym_index
, unsigned int shndx
,
1747 unsigned int got_type
, Rel_dyn
* rel_dyn
,
1748 unsigned int r_type_1
, unsigned int r_type_2
);
1751 add_local_pair_with_rela(Sized_relobj
<size
, big_endian
>* object
,
1752 unsigned int sym_index
, unsigned int shndx
,
1753 unsigned int got_type
, Rela_dyn
* rela_dyn
,
1754 unsigned int r_type_1
, unsigned int r_type_2
);
1756 // Add a constant to the GOT. This returns the offset of the new
1757 // entry from the start of the GOT.
1759 add_constant(Valtype constant
)
1761 this->entries_
.push_back(Got_entry(constant
));
1762 this->set_got_size();
1763 return this->last_got_offset();
1767 // Write out the GOT table.
1769 do_write(Output_file
*);
1771 // Write to a map file.
1773 do_print_to_mapfile(Mapfile
* mapfile
) const
1774 { mapfile
->print_output_data(this, _("** GOT")); }
1777 // This POD class holds a single GOT entry.
1781 // Create a zero entry.
1783 : local_sym_index_(CONSTANT_CODE
)
1784 { this->u_
.constant
= 0; }
1786 // Create a global symbol entry.
1787 explicit Got_entry(Symbol
* gsym
)
1788 : local_sym_index_(GSYM_CODE
)
1789 { this->u_
.gsym
= gsym
; }
1791 // Create a local symbol entry.
1792 Got_entry(Sized_relobj
<size
, big_endian
>* object
,
1793 unsigned int local_sym_index
)
1794 : local_sym_index_(local_sym_index
)
1796 gold_assert(local_sym_index
!= GSYM_CODE
1797 && local_sym_index
!= CONSTANT_CODE
);
1798 this->u_
.object
= object
;
1801 // Create a constant entry. The constant is a host value--it will
1802 // be swapped, if necessary, when it is written out.
1803 explicit Got_entry(Valtype constant
)
1804 : local_sym_index_(CONSTANT_CODE
)
1805 { this->u_
.constant
= constant
; }
1807 // Write the GOT entry to an output view.
1809 write(unsigned char* pov
) const;
1820 // For a local symbol, the object.
1821 Sized_relobj
<size
, big_endian
>* object
;
1822 // For a global symbol, the symbol.
1824 // For a constant, the constant.
1827 // For a local symbol, the local symbol index. This is GSYM_CODE
1828 // for a global symbol, or CONSTANT_CODE for a constant.
1829 unsigned int local_sym_index_
;
1832 typedef std::vector
<Got_entry
> Got_entries
;
1834 // Return the offset into the GOT of GOT entry I.
1836 got_offset(unsigned int i
) const
1837 { return i
* (size
/ 8); }
1839 // Return the offset into the GOT of the last entry added.
1841 last_got_offset() const
1842 { return this->got_offset(this->entries_
.size() - 1); }
1844 // Set the size of the section.
1847 { this->set_current_data_size(this->got_offset(this->entries_
.size())); }
1849 // The list of GOT entries.
1850 Got_entries entries_
;
1853 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1856 class Output_data_dynamic
: public Output_section_data
1859 Output_data_dynamic(Stringpool
* pool
)
1860 : Output_section_data(Output_data::default_alignment()),
1861 entries_(), pool_(pool
)
1864 // Add a new dynamic entry with a fixed numeric value.
1866 add_constant(elfcpp::DT tag
, unsigned int val
)
1867 { this->add_entry(Dynamic_entry(tag
, val
)); }
1869 // Add a new dynamic entry with the address of output data.
1871 add_section_address(elfcpp::DT tag
, const Output_data
* od
)
1872 { this->add_entry(Dynamic_entry(tag
, od
, false)); }
1874 // Add a new dynamic entry with the address of output data
1875 // plus a constant offset.
1877 add_section_plus_offset(elfcpp::DT tag
, const Output_data
* od
,
1878 unsigned int offset
)
1879 { this->add_entry(Dynamic_entry(tag
, od
, offset
)); }
1881 // Add a new dynamic entry with the size of output data.
1883 add_section_size(elfcpp::DT tag
, const Output_data
* od
)
1884 { this->add_entry(Dynamic_entry(tag
, od
, true)); }
1886 // Add a new dynamic entry with the address of a symbol.
1888 add_symbol(elfcpp::DT tag
, const Symbol
* sym
)
1889 { this->add_entry(Dynamic_entry(tag
, sym
)); }
1891 // Add a new dynamic entry with a string.
1893 add_string(elfcpp::DT tag
, const char* str
)
1894 { this->add_entry(Dynamic_entry(tag
, this->pool_
->add(str
, true, NULL
))); }
1897 add_string(elfcpp::DT tag
, const std::string
& str
)
1898 { this->add_string(tag
, str
.c_str()); }
1901 // Adjust the output section to set the entry size.
1903 do_adjust_output_section(Output_section
*);
1905 // Set the final data size.
1907 set_final_data_size();
1909 // Write out the dynamic entries.
1911 do_write(Output_file
*);
1913 // Write to a map file.
1915 do_print_to_mapfile(Mapfile
* mapfile
) const
1916 { mapfile
->print_output_data(this, _("** dynamic")); }
1919 // This POD class holds a single dynamic entry.
1923 // Create an entry with a fixed numeric value.
1924 Dynamic_entry(elfcpp::DT tag
, unsigned int val
)
1925 : tag_(tag
), offset_(DYNAMIC_NUMBER
)
1926 { this->u_
.val
= val
; }
1928 // Create an entry with the size or address of a section.
1929 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, bool section_size
)
1931 offset_(section_size
1932 ? DYNAMIC_SECTION_SIZE
1933 : DYNAMIC_SECTION_ADDRESS
)
1934 { this->u_
.od
= od
; }
1936 // Create an entry with the address of a section plus a constant offset.
1937 Dynamic_entry(elfcpp::DT tag
, const Output_data
* od
, unsigned int offset
)
1940 { this->u_
.od
= od
; }
1942 // Create an entry with the address of a symbol.
1943 Dynamic_entry(elfcpp::DT tag
, const Symbol
* sym
)
1944 : tag_(tag
), offset_(DYNAMIC_SYMBOL
)
1945 { this->u_
.sym
= sym
; }
1947 // Create an entry with a string.
1948 Dynamic_entry(elfcpp::DT tag
, const char* str
)
1949 : tag_(tag
), offset_(DYNAMIC_STRING
)
1950 { this->u_
.str
= str
; }
1952 // Return the tag of this entry.
1955 { return this->tag_
; }
1957 // Write the dynamic entry to an output view.
1958 template<int size
, bool big_endian
>
1960 write(unsigned char* pov
, const Stringpool
*) const;
1963 // Classification is encoded in the OFFSET field.
1967 DYNAMIC_SECTION_ADDRESS
= 0,
1969 DYNAMIC_NUMBER
= -1U,
1971 DYNAMIC_SECTION_SIZE
= -2U,
1973 DYNAMIC_SYMBOL
= -3U,
1975 DYNAMIC_STRING
= -4U
1976 // Any other value indicates a section address plus OFFSET.
1981 // For DYNAMIC_NUMBER.
1983 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1984 const Output_data
* od
;
1985 // For DYNAMIC_SYMBOL.
1987 // For DYNAMIC_STRING.
1992 // The type of entry (Classification) or offset within a section.
1993 unsigned int offset_
;
1996 // Add an entry to the list.
1998 add_entry(const Dynamic_entry
& entry
)
1999 { this->entries_
.push_back(entry
); }
2001 // Sized version of write function.
2002 template<int size
, bool big_endian
>
2004 sized_write(Output_file
* of
);
2006 // The type of the list of entries.
2007 typedef std::vector
<Dynamic_entry
> Dynamic_entries
;
2010 Dynamic_entries entries_
;
2011 // The pool used for strings.
2015 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2016 // which may be required if the object file has more than
2017 // SHN_LORESERVE sections.
2019 class Output_symtab_xindex
: public Output_section_data
2022 Output_symtab_xindex(size_t symcount
)
2023 : Output_section_data(symcount
* 4, 4, true),
2027 // Add an entry: symbol number SYMNDX has section SHNDX.
2029 add(unsigned int symndx
, unsigned int shndx
)
2030 { this->entries_
.push_back(std::make_pair(symndx
, shndx
)); }
2034 do_write(Output_file
*);
2036 // Write to a map file.
2038 do_print_to_mapfile(Mapfile
* mapfile
) const
2039 { mapfile
->print_output_data(this, _("** symtab xindex")); }
2042 template<bool big_endian
>
2044 endian_do_write(unsigned char*);
2046 // It is likely that most symbols will not require entries. Rather
2047 // than keep a vector for all symbols, we keep pairs of symbol index
2048 // and section index.
2049 typedef std::vector
<std::pair
<unsigned int, unsigned int> > Xindex_entries
;
2051 // The entries we need.
2052 Xindex_entries entries_
;
2055 // A relaxed input section.
2056 class Output_relaxed_input_section
: public Output_section_data_build
2059 // We would like to call relobj->section_addralign(shndx) to get the
2060 // alignment but we do not want the constructor to fail. So callers
2061 // are repsonsible for ensuring that.
2062 Output_relaxed_input_section(Relobj
* relobj
, unsigned int shndx
,
2064 : Output_section_data_build(addralign
), relobj_(relobj
), shndx_(shndx
)
2067 // Return the Relobj of this relaxed input section.
2070 { return this->relobj_
; }
2072 // Return the section index of this relaxed input section.
2075 { return this->shndx_
; }
2079 unsigned int shndx_
;
2082 // An output section. We don't expect to have too many output
2083 // sections, so we don't bother to do a template on the size.
2085 class Output_section
: public Output_data
2088 // Create an output section, giving the name, type, and flags.
2089 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
2090 virtual ~Output_section();
2092 // Add a new input section SHNDX, named NAME, with header SHDR, from
2093 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2094 // which applies to this section, or 0 if none, or -1 if more than
2095 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2096 // in a linker script; in that case we need to keep track of input
2097 // sections associated with an output section. Return the offset
2098 // within the output section.
2099 template<int size
, bool big_endian
>
2101 add_input_section(Sized_relobj
<size
, big_endian
>* object
, unsigned int shndx
,
2103 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
2104 unsigned int reloc_shndx
, bool have_sections_script
);
2106 // Add generated data POSD to this output section.
2108 add_output_section_data(Output_section_data
* posd
);
2110 // Add a relaxed input section PORIS to this output section.
2112 add_relaxed_input_section(Output_relaxed_input_section
* poris
);
2114 // Return the section name.
2117 { return this->name_
; }
2119 // Return the section type.
2122 { return this->type_
; }
2124 // Return the section flags.
2127 { return this->flags_
; }
2129 // Update the output section flags based on input section flags.
2131 update_flags_for_input_section(elfcpp::Elf_Xword flags
);
2133 // Return the entsize field.
2136 { return this->entsize_
; }
2138 // Set the entsize field.
2140 set_entsize(uint64_t v
);
2142 // Set the load address.
2144 set_load_address(uint64_t load_address
)
2146 this->load_address_
= load_address
;
2147 this->has_load_address_
= true;
2150 // Set the link field to the output section index of a section.
2152 set_link_section(const Output_data
* od
)
2154 gold_assert(this->link_
== 0
2155 && !this->should_link_to_symtab_
2156 && !this->should_link_to_dynsym_
);
2157 this->link_section_
= od
;
2160 // Set the link field to a constant.
2162 set_link(unsigned int v
)
2164 gold_assert(this->link_section_
== NULL
2165 && !this->should_link_to_symtab_
2166 && !this->should_link_to_dynsym_
);
2170 // Record that this section should link to the normal symbol table.
2172 set_should_link_to_symtab()
2174 gold_assert(this->link_section_
== NULL
2176 && !this->should_link_to_dynsym_
);
2177 this->should_link_to_symtab_
= true;
2180 // Record that this section should link to the dynamic symbol table.
2182 set_should_link_to_dynsym()
2184 gold_assert(this->link_section_
== NULL
2186 && !this->should_link_to_symtab_
);
2187 this->should_link_to_dynsym_
= true;
2190 // Return the info field.
2194 gold_assert(this->info_section_
== NULL
2195 && this->info_symndx_
== NULL
);
2199 // Set the info field to the output section index of a section.
2201 set_info_section(const Output_section
* os
)
2203 gold_assert((this->info_section_
== NULL
2204 || (this->info_section_
== os
2205 && this->info_uses_section_index_
))
2206 && this->info_symndx_
== NULL
2207 && this->info_
== 0);
2208 this->info_section_
= os
;
2209 this->info_uses_section_index_
= true;
2212 // Set the info field to the symbol table index of a symbol.
2214 set_info_symndx(const Symbol
* sym
)
2216 gold_assert(this->info_section_
== NULL
2217 && (this->info_symndx_
== NULL
2218 || this->info_symndx_
== sym
)
2219 && this->info_
== 0);
2220 this->info_symndx_
= sym
;
2223 // Set the info field to the symbol table index of a section symbol.
2225 set_info_section_symndx(const Output_section
* os
)
2227 gold_assert((this->info_section_
== NULL
2228 || (this->info_section_
== os
2229 && !this->info_uses_section_index_
))
2230 && this->info_symndx_
== NULL
2231 && this->info_
== 0);
2232 this->info_section_
= os
;
2233 this->info_uses_section_index_
= false;
2236 // Set the info field to a constant.
2238 set_info(unsigned int v
)
2240 gold_assert(this->info_section_
== NULL
2241 && this->info_symndx_
== NULL
2242 && (this->info_
== 0
2243 || this->info_
== v
));
2247 // Set the addralign field.
2249 set_addralign(uint64_t v
)
2250 { this->addralign_
= v
; }
2252 // Whether the output section index has been set.
2254 has_out_shndx() const
2255 { return this->out_shndx_
!= -1U; }
2257 // Indicate that we need a symtab index.
2259 set_needs_symtab_index()
2260 { this->needs_symtab_index_
= true; }
2262 // Return whether we need a symtab index.
2264 needs_symtab_index() const
2265 { return this->needs_symtab_index_
; }
2267 // Get the symtab index.
2269 symtab_index() const
2271 gold_assert(this->symtab_index_
!= 0);
2272 return this->symtab_index_
;
2275 // Set the symtab index.
2277 set_symtab_index(unsigned int index
)
2279 gold_assert(index
!= 0);
2280 this->symtab_index_
= index
;
2283 // Indicate that we need a dynsym index.
2285 set_needs_dynsym_index()
2286 { this->needs_dynsym_index_
= true; }
2288 // Return whether we need a dynsym index.
2290 needs_dynsym_index() const
2291 { return this->needs_dynsym_index_
; }
2293 // Get the dynsym index.
2295 dynsym_index() const
2297 gold_assert(this->dynsym_index_
!= 0);
2298 return this->dynsym_index_
;
2301 // Set the dynsym index.
2303 set_dynsym_index(unsigned int index
)
2305 gold_assert(index
!= 0);
2306 this->dynsym_index_
= index
;
2309 // Return whether the input sections sections attachd to this output
2310 // section may require sorting. This is used to handle constructor
2311 // priorities compatibly with GNU ld.
2313 may_sort_attached_input_sections() const
2314 { return this->may_sort_attached_input_sections_
; }
2316 // Record that the input sections attached to this output section
2317 // may require sorting.
2319 set_may_sort_attached_input_sections()
2320 { this->may_sort_attached_input_sections_
= true; }
2322 // Return whether the input sections attached to this output section
2323 // require sorting. This is used to handle constructor priorities
2324 // compatibly with GNU ld.
2326 must_sort_attached_input_sections() const
2327 { return this->must_sort_attached_input_sections_
; }
2329 // Record that the input sections attached to this output section
2332 set_must_sort_attached_input_sections()
2333 { this->must_sort_attached_input_sections_
= true; }
2335 // Return whether this section holds relro data--data which has
2336 // dynamic relocations but which may be marked read-only after the
2337 // dynamic relocations have been completed.
2340 { return this->is_relro_
; }
2342 // Record that this section holds relro data.
2345 { this->is_relro_
= true; }
2347 // Record that this section does not hold relro data.
2350 { this->is_relro_
= false; }
2352 // True if this section holds relro local data--relro data for which
2353 // the dynamic relocations are all RELATIVE relocations.
2355 is_relro_local() const
2356 { return this->is_relro_local_
; }
2358 // Record that this section holds relro local data.
2360 set_is_relro_local()
2361 { this->is_relro_local_
= true; }
2363 // True if this is a small section: a section which holds small
2366 is_small_section() const
2367 { return this->is_small_section_
; }
2369 // Record that this is a small section.
2371 set_is_small_section()
2372 { this->is_small_section_
= true; }
2374 // True if this is a large section: a section which holds large
2377 is_large_section() const
2378 { return this->is_large_section_
; }
2380 // Record that this is a large section.
2382 set_is_large_section()
2383 { this->is_large_section_
= true; }
2385 // True if this is a large data (not BSS) section.
2387 is_large_data_section()
2388 { return this->is_large_section_
&& this->type_
!= elfcpp::SHT_NOBITS
; }
2390 // Return whether this section should be written after all the input
2391 // sections are complete.
2393 after_input_sections() const
2394 { return this->after_input_sections_
; }
2396 // Record that this section should be written after all the input
2397 // sections are complete.
2399 set_after_input_sections()
2400 { this->after_input_sections_
= true; }
2402 // Return whether this section requires postprocessing after all
2403 // relocations have been applied.
2405 requires_postprocessing() const
2406 { return this->requires_postprocessing_
; }
2408 // If a section requires postprocessing, return the buffer to use.
2410 postprocessing_buffer() const
2412 gold_assert(this->postprocessing_buffer_
!= NULL
);
2413 return this->postprocessing_buffer_
;
2416 // If a section requires postprocessing, create the buffer to use.
2418 create_postprocessing_buffer();
2420 // If a section requires postprocessing, this is the size of the
2421 // buffer to which relocations should be applied.
2423 postprocessing_buffer_size() const
2424 { return this->current_data_size_for_child(); }
2426 // Modify the section name. This is only permitted for an
2427 // unallocated section, and only before the size has been finalized.
2428 // Otherwise the name will not get into Layout::namepool_.
2430 set_name(const char* newname
)
2432 gold_assert((this->flags_
& elfcpp::SHF_ALLOC
) == 0);
2433 gold_assert(!this->is_data_size_valid());
2434 this->name_
= newname
;
2437 // Return whether the offset OFFSET in the input section SHNDX in
2438 // object OBJECT is being included in the link.
2440 is_input_address_mapped(const Relobj
* object
, unsigned int shndx
,
2441 off_t offset
) const;
2443 // Return the offset within the output section of OFFSET relative to
2444 // the start of input section SHNDX in object OBJECT.
2446 output_offset(const Relobj
* object
, unsigned int shndx
,
2447 section_offset_type offset
) const;
2449 // Return the output virtual address of OFFSET relative to the start
2450 // of input section SHNDX in object OBJECT.
2452 output_address(const Relobj
* object
, unsigned int shndx
,
2453 off_t offset
) const;
2455 // Look for the merged section for input section SHNDX in object
2456 // OBJECT. If found, return true, and set *ADDR to the address of
2457 // the start of the merged section. This is not necessary the
2458 // output offset corresponding to input offset 0 in the section,
2459 // since the section may be mapped arbitrarily.
2461 find_starting_output_address(const Relobj
* object
, unsigned int shndx
,
2462 uint64_t* addr
) const;
2464 // Record that this output section was found in the SECTIONS clause
2465 // of a linker script.
2467 set_found_in_sections_clause()
2468 { this->found_in_sections_clause_
= true; }
2470 // Return whether this output section was found in the SECTIONS
2471 // clause of a linker script.
2473 found_in_sections_clause() const
2474 { return this->found_in_sections_clause_
; }
2476 // Write the section header into *OPHDR.
2477 template<int size
, bool big_endian
>
2479 write_header(const Layout
*, const Stringpool
*,
2480 elfcpp::Shdr_write
<size
, big_endian
>*) const;
2482 // The next few calls are for linker script support.
2484 // We need to export the input sections to linker scripts. Previously
2485 // we export a pair of Relobj pointer and section index. We now need to
2486 // handle relaxed input sections as well. So we use this class.
2487 class Simple_input_section
2490 static const unsigned int invalid_shndx
= static_cast<unsigned int>(-1);
2493 Simple_input_section(Relobj
*relobj
, unsigned int shndx
)
2496 gold_assert(shndx
!= invalid_shndx
);
2497 this->u_
.relobj
= relobj
;
2500 Simple_input_section(Output_relaxed_input_section
* section
)
2501 : shndx_(invalid_shndx
)
2502 { this->u_
.relaxed_input_section
= section
; }
2504 // Whether this is a relaxed section.
2506 is_relaxed_input_section() const
2507 { return this->shndx_
== invalid_shndx
; }
2509 // Return object of an input section.
2513 return ((this->shndx_
!= invalid_shndx
)
2515 : this->u_
.relaxed_input_section
->relobj());
2518 // Return index of an input section.
2522 return ((this->shndx_
!= invalid_shndx
)
2524 : this->u_
.relaxed_input_section
->shndx());
2527 // Return the Output_relaxed_input_section object of a relaxed section.
2528 Output_relaxed_input_section
*
2529 relaxed_input_section() const
2531 gold_assert(this->shndx_
== invalid_shndx
);
2532 return this->u_
.relaxed_input_section
;
2536 // Pointer to either an Relobj or an Output_relaxed_input_section.
2540 Output_relaxed_input_section
* relaxed_input_section
;
2542 // Section index for an non-relaxed section or invalid_shndx for
2543 // a relaxed section.
2544 unsigned int shndx_
;
2547 // Store the list of input sections for this Output_section into the
2548 // list passed in. This removes the input sections, leaving only
2549 // any Output_section_data elements. This returns the size of those
2550 // Output_section_data elements. ADDRESS is the address of this
2551 // output section. FILL is the fill value to use, in case there are
2552 // any spaces between the remaining Output_section_data elements.
2554 get_input_sections(uint64_t address
, const std::string
& fill
,
2555 std::list
<Simple_input_section
>*);
2557 // Add an input section from a script.
2559 add_input_section_for_script(const Simple_input_section
& input_section
,
2560 off_t data_size
, uint64_t addralign
);
2562 // Set the current size of the output section.
2564 set_current_data_size(off_t size
)
2565 { this->set_current_data_size_for_child(size
); }
2567 // Get the current size of the output section.
2569 current_data_size() const
2570 { return this->current_data_size_for_child(); }
2572 // End of linker script support.
2574 // Save states before doing section layout.
2575 // This is used for relaxation.
2579 // Restore states prior to section layout.
2583 // Convert existing input sections to relaxed input sections.
2585 convert_input_sections_to_relaxed_sections(
2586 const std::vector
<Output_relaxed_input_section
*>& sections
);
2588 // Print merge statistics to stderr.
2590 print_merge_stats();
2593 // Return the output section--i.e., the object itself.
2598 // Return the section index in the output file.
2600 do_out_shndx() const
2602 gold_assert(this->out_shndx_
!= -1U);
2603 return this->out_shndx_
;
2606 // Set the output section index.
2608 do_set_out_shndx(unsigned int shndx
)
2610 gold_assert(this->out_shndx_
== -1U || this->out_shndx_
== shndx
);
2611 this->out_shndx_
= shndx
;
2614 // Set the final data size of the Output_section. For a typical
2615 // Output_section, there is nothing to do, but if there are any
2616 // Output_section_data objects we need to set their final addresses
2619 set_final_data_size();
2621 // Reset the address and file offset.
2623 do_reset_address_and_file_offset();
2625 // Return true if address and file offset already have reset values. In
2626 // other words, calling reset_address_and_file_offset will not change them.
2628 do_address_and_file_offset_have_reset_values() const;
2630 // Write the data to the file. For a typical Output_section, this
2631 // does nothing: the data is written out by calling Object::Relocate
2632 // on each input object. But if there are any Output_section_data
2633 // objects we do need to write them out here.
2635 do_write(Output_file
*);
2637 // Return the address alignment--function required by parent class.
2639 do_addralign() const
2640 { return this->addralign_
; }
2642 // Return whether there is a load address.
2644 do_has_load_address() const
2645 { return this->has_load_address_
; }
2647 // Return the load address.
2649 do_load_address() const
2651 gold_assert(this->has_load_address_
);
2652 return this->load_address_
;
2655 // Return whether this is an Output_section.
2657 do_is_section() const
2660 // Return whether this is a section of the specified type.
2662 do_is_section_type(elfcpp::Elf_Word type
) const
2663 { return this->type_
== type
; }
2665 // Return whether the specified section flag is set.
2667 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
2668 { return (this->flags_
& flag
) != 0; }
2670 // Set the TLS offset. Called only for SHT_TLS sections.
2672 do_set_tls_offset(uint64_t tls_base
);
2674 // Return the TLS offset, relative to the base of the TLS segment.
2675 // Valid only for SHT_TLS sections.
2677 do_tls_offset() const
2678 { return this->tls_offset_
; }
2680 // This may be implemented by a child class.
2682 do_finalize_name(Layout
*)
2685 // Print to the map file.
2687 do_print_to_mapfile(Mapfile
*) const;
2689 // Record that this section requires postprocessing after all
2690 // relocations have been applied. This is called by a child class.
2692 set_requires_postprocessing()
2694 this->requires_postprocessing_
= true;
2695 this->after_input_sections_
= true;
2698 // Write all the data of an Output_section into the postprocessing
2701 write_to_postprocessing_buffer();
2703 // In some cases we need to keep a list of the input sections
2704 // associated with this output section. We only need the list if we
2705 // might have to change the offsets of the input section within the
2706 // output section after we add the input section. The ordinary
2707 // input sections will be written out when we process the object
2708 // file, and as such we don't need to track them here. We do need
2709 // to track Output_section_data objects here. We store instances of
2710 // this structure in a std::vector, so it must be a POD. There can
2711 // be many instances of this structure, so we use a union to save
2717 : shndx_(0), p2align_(0)
2719 this->u1_
.data_size
= 0;
2720 this->u2_
.object
= NULL
;
2723 // For an ordinary input section.
2724 Input_section(Relobj
* object
, unsigned int shndx
, off_t data_size
,
2727 p2align_(ffsll(static_cast<long long>(addralign
)))
2729 gold_assert(shndx
!= OUTPUT_SECTION_CODE
2730 && shndx
!= MERGE_DATA_SECTION_CODE
2731 && shndx
!= MERGE_STRING_SECTION_CODE
2732 && shndx
!= RELAXED_INPUT_SECTION_CODE
);
2733 this->u1_
.data_size
= data_size
;
2734 this->u2_
.object
= object
;
2737 // For a non-merge output section.
2738 Input_section(Output_section_data
* posd
)
2739 : shndx_(OUTPUT_SECTION_CODE
), p2align_(0)
2741 this->u1_
.data_size
= 0;
2742 this->u2_
.posd
= posd
;
2745 // For a merge section.
2746 Input_section(Output_section_data
* posd
, bool is_string
, uint64_t entsize
)
2748 ? MERGE_STRING_SECTION_CODE
2749 : MERGE_DATA_SECTION_CODE
),
2752 this->u1_
.entsize
= entsize
;
2753 this->u2_
.posd
= posd
;
2756 // For a relaxed input section.
2757 Input_section(Output_relaxed_input_section
*psection
)
2758 : shndx_(RELAXED_INPUT_SECTION_CODE
), p2align_(0)
2760 this->u1_
.data_size
= 0;
2761 this->u2_
.poris
= psection
;
2764 // The required alignment.
2768 if (!this->is_input_section())
2769 return this->u2_
.posd
->addralign();
2770 return (this->p2align_
== 0
2772 : static_cast<uint64_t>(1) << (this->p2align_
- 1));
2775 // Return the required size.
2779 // Whether this is an input section.
2781 is_input_section() const
2783 return (this->shndx_
!= OUTPUT_SECTION_CODE
2784 && this->shndx_
!= MERGE_DATA_SECTION_CODE
2785 && this->shndx_
!= MERGE_STRING_SECTION_CODE
2786 && this->shndx_
!= RELAXED_INPUT_SECTION_CODE
);
2789 // Return whether this is a merge section which matches the
2792 is_merge_section(bool is_string
, uint64_t entsize
,
2793 uint64_t addralign
) const
2795 return (this->shndx_
== (is_string
2796 ? MERGE_STRING_SECTION_CODE
2797 : MERGE_DATA_SECTION_CODE
)
2798 && this->u1_
.entsize
== entsize
2799 && this->addralign() == addralign
);
2802 // Return whether this is a relaxed input section.
2804 is_relaxed_input_section() const
2805 { return this->shndx_
== RELAXED_INPUT_SECTION_CODE
; }
2807 // Return whether this is a generic Output_section_data.
2809 is_output_section_data() const
2811 return this->shndx_
== OUTPUT_SECTION_CODE
;
2814 // Return the object for an input section.
2818 if (this->is_input_section())
2819 return this->u2_
.object
;
2820 else if (this->is_relaxed_input_section())
2821 return this->u2_
.poris
->relobj();
2826 // Return the input section index for an input section.
2830 if (this->is_input_section())
2831 return this->shndx_
;
2832 else if (this->is_relaxed_input_section())
2833 return this->u2_
.poris
->shndx();
2838 // For non-input-sections, return the associated Output_section_data
2840 Output_section_data
*
2841 output_section_data() const
2843 gold_assert(!this->is_input_section());
2844 return this->u2_
.posd
;
2847 // Return the Output_relaxed_input_section object.
2848 Output_relaxed_input_section
*
2849 relaxed_input_section() const
2851 gold_assert(this->is_relaxed_input_section());
2852 return this->u2_
.poris
;
2855 // Set the output section.
2857 set_output_section(Output_section
* os
)
2859 gold_assert(!this->is_input_section());
2860 Output_section_data
*posd
=
2861 this->is_relaxed_input_section() ? this->u2_
.poris
: this->u2_
.posd
;
2862 posd
->set_output_section(os
);
2865 // Set the address and file offset. This is called during
2866 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2867 // the enclosing section.
2869 set_address_and_file_offset(uint64_t address
, off_t file_offset
,
2870 off_t section_file_offset
);
2872 // Reset the address and file offset.
2874 reset_address_and_file_offset();
2876 // Finalize the data size.
2878 finalize_data_size();
2880 // Add an input section, for SHF_MERGE sections.
2882 add_input_section(Relobj
* object
, unsigned int shndx
)
2884 gold_assert(this->shndx_
== MERGE_DATA_SECTION_CODE
2885 || this->shndx_
== MERGE_STRING_SECTION_CODE
);
2886 return this->u2_
.posd
->add_input_section(object
, shndx
);
2889 // Given an input OBJECT, an input section index SHNDX within that
2890 // object, and an OFFSET relative to the start of that input
2891 // section, return whether or not the output offset is known. If
2892 // this function returns true, it sets *POUTPUT to the offset in
2893 // the output section, relative to the start of the input section
2894 // in the output section. *POUTPUT may be different from OFFSET
2895 // for a merged section.
2897 output_offset(const Relobj
* object
, unsigned int shndx
,
2898 section_offset_type offset
,
2899 section_offset_type
*poutput
) const;
2901 // Return whether this is the merge section for the input section
2904 is_merge_section_for(const Relobj
* object
, unsigned int shndx
) const;
2906 // Write out the data. This does nothing for an input section.
2908 write(Output_file
*);
2910 // Write the data to a buffer. This does nothing for an input
2913 write_to_buffer(unsigned char*);
2915 // Print to a map file.
2917 print_to_mapfile(Mapfile
*) const;
2919 // Print statistics about merge sections to stderr.
2921 print_merge_stats(const char* section_name
)
2923 if (this->shndx_
== MERGE_DATA_SECTION_CODE
2924 || this->shndx_
== MERGE_STRING_SECTION_CODE
)
2925 this->u2_
.posd
->print_merge_stats(section_name
);
2929 // Code values which appear in shndx_. If the value is not one of
2930 // these codes, it is the input section index in the object file.
2933 // An Output_section_data.
2934 OUTPUT_SECTION_CODE
= -1U,
2935 // An Output_section_data for an SHF_MERGE section with
2936 // SHF_STRINGS not set.
2937 MERGE_DATA_SECTION_CODE
= -2U,
2938 // An Output_section_data for an SHF_MERGE section with
2940 MERGE_STRING_SECTION_CODE
= -3U,
2941 // An Output_section_data for a relaxed input section.
2942 RELAXED_INPUT_SECTION_CODE
= -4U
2945 // For an ordinary input section, this is the section index in the
2946 // input file. For an Output_section_data, this is
2947 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2948 // MERGE_STRING_SECTION_CODE.
2949 unsigned int shndx_
;
2950 // The required alignment, stored as a power of 2.
2951 unsigned int p2align_
;
2954 // For an ordinary input section, the section size.
2956 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2957 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2963 // For an ordinary input section, the object which holds the
2966 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2967 // MERGE_STRING_SECTION_CODE, the data.
2968 Output_section_data
* posd
;
2969 // For RELAXED_INPUT_SECTION_CODE, the data.
2970 Output_relaxed_input_section
* poris
;
2974 typedef std::vector
<Input_section
> Input_section_list
;
2976 // Allow a child class to access the input sections.
2977 const Input_section_list
&
2978 input_sections() const
2979 { return this->input_sections_
; }
2982 // We only save enough information to undo the effects of section layout.
2983 class Checkpoint_output_section
2986 Checkpoint_output_section(uint64_t addralign
, elfcpp::Elf_Xword flags
,
2987 const Input_section_list
& input_sections
,
2988 off_t first_input_offset
,
2989 bool attached_input_sections_are_sorted
)
2990 : addralign_(addralign
), flags_(flags
),
2991 input_sections_(input_sections
),
2992 input_sections_size_(input_sections_
.size()),
2993 input_sections_copy_(), first_input_offset_(first_input_offset
),
2994 attached_input_sections_are_sorted_(attached_input_sections_are_sorted
)
2998 ~Checkpoint_output_section()
3001 // Return the address alignment.
3004 { return this->addralign_
; }
3006 // Return the section flags.
3009 { return this->flags_
; }
3011 // Return a reference to the input section list copy.
3014 { return &this->input_sections_copy_
; }
3016 // Return the size of input_sections at the time when checkpoint is
3019 input_sections_size() const
3020 { return this->input_sections_size_
; }
3022 // Whether input sections are copied.
3024 input_sections_saved() const
3025 { return this->input_sections_copy_
.size() == this->input_sections_size_
; }
3028 first_input_offset() const
3029 { return this->first_input_offset_
; }
3032 attached_input_sections_are_sorted() const
3033 { return this->attached_input_sections_are_sorted_
; }
3035 // Save input sections.
3037 save_input_sections()
3039 this->input_sections_copy_
.reserve(this->input_sections_size_
);
3040 this->input_sections_copy_
.clear();
3041 Input_section_list::const_iterator p
= this->input_sections_
.begin();
3042 gold_assert(this->input_sections_size_
>= this->input_sections_
.size());
3043 for(size_t i
= 0; i
< this->input_sections_size_
; i
++, ++p
)
3044 this->input_sections_copy_
.push_back(*p
);
3048 // The section alignment.
3049 uint64_t addralign_
;
3050 // The section flags.
3051 elfcpp::Elf_Xword flags_
;
3052 // Reference to the input sections to be checkpointed.
3053 const Input_section_list
& input_sections_
;
3054 // Size of the checkpointed portion of input_sections_;
3055 size_t input_sections_size_
;
3056 // Copy of input sections.
3057 Input_section_list input_sections_copy_
;
3058 // The offset of the first entry in input_sections_.
3059 off_t first_input_offset_
;
3060 // True if the input sections attached to this output section have
3061 // already been sorted.
3062 bool attached_input_sections_are_sorted_
;
3065 // This class is used to sort the input sections.
3066 class Input_section_sort_entry
;
3068 // This is the sort comparison function.
3069 struct Input_section_sort_compare
3072 operator()(const Input_section_sort_entry
&,
3073 const Input_section_sort_entry
&) const;
3076 // Fill data. This is used to fill in data between input sections.
3077 // It is also used for data statements (BYTE, WORD, etc.) in linker
3078 // scripts. When we have to keep track of the input sections, we
3079 // can use an Output_data_const, but we don't want to have to keep
3080 // track of input sections just to implement fills.
3084 Fill(off_t section_offset
, off_t length
)
3085 : section_offset_(section_offset
),
3086 length_(convert_to_section_size_type(length
))
3089 // Return section offset.
3091 section_offset() const
3092 { return this->section_offset_
; }
3094 // Return fill length.
3097 { return this->length_
; }
3100 // The offset within the output section.
3101 off_t section_offset_
;
3102 // The length of the space to fill.
3103 section_size_type length_
;
3106 typedef std::vector
<Fill
> Fill_list
;
3108 // This class describes properties of merge data sections. It is used
3109 // as a key type for maps.
3110 class Merge_section_properties
3113 Merge_section_properties(bool is_string
, uint64_t entsize
,
3115 : is_string_(is_string
), entsize_(entsize
), addralign_(addralign
)
3118 // Whether this equals to another Merge_section_properties MSP.
3120 eq(const Merge_section_properties
& msp
) const
3122 return ((this->is_string_
== msp
.is_string_
)
3123 && (this->entsize_
== msp
.entsize_
)
3124 && (this->addralign_
== msp
.addralign_
));
3127 // Compute a hash value for this using 64-bit FNV-1a hash.
3131 uint64_t h
= 14695981039346656037ULL; // FNV offset basis.
3132 uint64_t prime
= 1099511628211ULL;
3133 h
= (h
^ static_cast<uint64_t>(this->is_string_
)) * prime
;
3134 h
= (h
^ static_cast<uint64_t>(this->entsize_
)) * prime
;
3135 h
= (h
^ static_cast<uint64_t>(this->addralign_
)) * prime
;
3139 // Functors for associative containers.
3143 operator()(const Merge_section_properties
& msp1
,
3144 const Merge_section_properties
& msp2
) const
3145 { return msp1
.eq(msp2
); }
3151 operator()(const Merge_section_properties
& msp
) const
3152 { return msp
.hash_value(); }
3156 // Whether this merge data section is for strings.
3158 // Entsize of this merge data section.
3160 // Address alignment.
3161 uint64_t addralign_
;
3164 // Map that link Merge_section_properties to Output_merge_base.
3165 typedef Unordered_map
<Merge_section_properties
, Output_merge_base
*,
3166 Merge_section_properties::hash
,
3167 Merge_section_properties::equal_to
>
3168 Merge_section_by_properties_map
;
3170 // Map that link Input_section_specifier to Output_section_data.
3171 typedef Unordered_map
<Input_section_specifier
, Output_section_data
*,
3172 Input_section_specifier::hash
,
3173 Input_section_specifier::equal_to
>
3174 Output_section_data_by_input_section_map
;
3176 // Map used during relaxation of existing sections. This map
3177 // an input section specifier to an input section list index.
3178 // We assume that Input_section_list is a vector.
3179 typedef Unordered_map
<Input_section_specifier
, size_t,
3180 Input_section_specifier::hash
,
3181 Input_section_specifier::equal_to
>
3184 // Add a new output section by Input_section.
3186 add_output_section_data(Input_section
*);
3188 // Add an SHF_MERGE input section. Returns true if the section was
3191 add_merge_input_section(Relobj
* object
, unsigned int shndx
, uint64_t flags
,
3192 uint64_t entsize
, uint64_t addralign
);
3194 // Add an output SHF_MERGE section POSD to this output section.
3195 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3196 // ENTSIZE is the entity size. This returns the entry added to
3199 add_output_merge_section(Output_section_data
* posd
, bool is_string
,
3202 // Sort the attached input sections.
3204 sort_attached_input_sections();
3206 // Find the merge section into which an input section with index SHNDX in
3207 // OBJECT has been added. Return NULL if none found.
3208 Output_section_data
*
3209 find_merge_section(const Relobj
* object
, unsigned int shndx
) const;
3211 // Find a relaxed input section to an input section in OBJECT
3212 // with index SHNDX. Return NULL if none is found.
3213 const Output_section_data
*
3214 find_relaxed_input_section(const Relobj
* object
, unsigned int shndx
) const;
3216 // Build a relaxation map.
3218 build_relaxation_map(
3219 const Input_section_list
& input_sections
,
3221 Relaxation_map
* map
) const;
3223 // Convert input sections in an input section list into relaxed sections.
3225 convert_input_sections_in_list_to_relaxed_sections(
3226 const std::vector
<Output_relaxed_input_section
*>& relaxed_sections
,
3227 const Relaxation_map
& map
,
3228 Input_section_list
* input_sections
);
3230 // Most of these fields are only valid after layout.
3232 // The name of the section. This will point into a Stringpool.
3234 // The section address is in the parent class.
3235 // The section alignment.
3236 uint64_t addralign_
;
3237 // The section entry size.
3239 // The load address. This is only used when using a linker script
3240 // with a SECTIONS clause. The has_load_address_ field indicates
3241 // whether this field is valid.
3242 uint64_t load_address_
;
3243 // The file offset is in the parent class.
3244 // Set the section link field to the index of this section.
3245 const Output_data
* link_section_
;
3246 // If link_section_ is NULL, this is the link field.
3248 // Set the section info field to the index of this section.
3249 const Output_section
* info_section_
;
3250 // If info_section_ is NULL, set the info field to the symbol table
3251 // index of this symbol.
3252 const Symbol
* info_symndx_
;
3253 // If info_section_ and info_symndx_ are NULL, this is the section
3256 // The section type.
3257 const elfcpp::Elf_Word type_
;
3258 // The section flags.
3259 elfcpp::Elf_Xword flags_
;
3260 // The section index.
3261 unsigned int out_shndx_
;
3262 // If there is a STT_SECTION for this output section in the normal
3263 // symbol table, this is the symbol index. This starts out as zero.
3264 // It is initialized in Layout::finalize() to be the index, or -1U
3265 // if there isn't one.
3266 unsigned int symtab_index_
;
3267 // If there is a STT_SECTION for this output section in the dynamic
3268 // symbol table, this is the symbol index. This starts out as zero.
3269 // It is initialized in Layout::finalize() to be the index, or -1U
3270 // if there isn't one.
3271 unsigned int dynsym_index_
;
3272 // The input sections. This will be empty in cases where we don't
3273 // need to keep track of them.
3274 Input_section_list input_sections_
;
3275 // The offset of the first entry in input_sections_.
3276 off_t first_input_offset_
;
3277 // The fill data. This is separate from input_sections_ because we
3278 // often will need fill sections without needing to keep track of
3281 // If the section requires postprocessing, this buffer holds the
3282 // section contents during relocation.
3283 unsigned char* postprocessing_buffer_
;
3284 // Whether this output section needs a STT_SECTION symbol in the
3285 // normal symbol table. This will be true if there is a relocation
3287 bool needs_symtab_index_
: 1;
3288 // Whether this output section needs a STT_SECTION symbol in the
3289 // dynamic symbol table. This will be true if there is a dynamic
3290 // relocation which needs it.
3291 bool needs_dynsym_index_
: 1;
3292 // Whether the link field of this output section should point to the
3293 // normal symbol table.
3294 bool should_link_to_symtab_
: 1;
3295 // Whether the link field of this output section should point to the
3296 // dynamic symbol table.
3297 bool should_link_to_dynsym_
: 1;
3298 // Whether this section should be written after all the input
3299 // sections are complete.
3300 bool after_input_sections_
: 1;
3301 // Whether this section requires post processing after all
3302 // relocations have been applied.
3303 bool requires_postprocessing_
: 1;
3304 // Whether an input section was mapped to this output section
3305 // because of a SECTIONS clause in a linker script.
3306 bool found_in_sections_clause_
: 1;
3307 // Whether this section has an explicitly specified load address.
3308 bool has_load_address_
: 1;
3309 // True if the info_section_ field means the section index of the
3310 // section, false if it means the symbol index of the corresponding
3312 bool info_uses_section_index_
: 1;
3313 // True if the input sections attached to this output section may
3315 bool may_sort_attached_input_sections_
: 1;
3316 // True if the input sections attached to this output section must
3318 bool must_sort_attached_input_sections_
: 1;
3319 // True if the input sections attached to this output section have
3320 // already been sorted.
3321 bool attached_input_sections_are_sorted_
: 1;
3322 // True if this section holds relro data.
3324 // True if this section holds relro local data.
3325 bool is_relro_local_
: 1;
3326 // True if this is a small section.
3327 bool is_small_section_
: 1;
3328 // True if this is a large section.
3329 bool is_large_section_
: 1;
3330 // For SHT_TLS sections, the offset of this section relative to the base
3331 // of the TLS segment.
3332 uint64_t tls_offset_
;
3333 // Saved checkpoint.
3334 Checkpoint_output_section
* checkpoint_
;
3335 // Map from input sections to merge sections.
3336 Output_section_data_by_input_section_map merge_section_map_
;
3337 // Map from merge section properties to merge_sections;
3338 Merge_section_by_properties_map merge_section_by_properties_map_
;
3339 // Map from input sections to relaxed input sections. This is mutable
3340 // beacause it is udpated lazily. We may need to update it in a
3341 // const qualified method.
3342 mutable Output_section_data_by_input_section_map relaxed_input_section_map_
;
3343 // Whether relaxed_input_section_map_ is valid.
3344 mutable bool is_relaxed_input_section_map_valid_
;
3345 // Whether code-fills are generated at write.
3346 bool generate_code_fills_at_write_
;
3349 // An output segment. PT_LOAD segments are built from collections of
3350 // output sections. Other segments typically point within PT_LOAD
3351 // segments, and are built directly as needed.
3353 // NOTE: We want to use the copy constructor for this class. During
3354 // relaxation, we may try built the segments multiple times. We do
3355 // that by copying the original segment list before lay-out, doing
3356 // a trial lay-out and roll-back to the saved copied if we need to
3357 // to the lay-out again.
3359 class Output_segment
3362 // Create an output segment, specifying the type and flags.
3363 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
3365 // Return the virtual address.
3368 { return this->vaddr_
; }
3370 // Return the physical address.
3373 { return this->paddr_
; }
3375 // Return the segment type.
3378 { return this->type_
; }
3380 // Return the segment flags.
3383 { return this->flags_
; }
3385 // Return the memory size.
3388 { return this->memsz_
; }
3390 // Return the file size.
3393 { return this->filesz_
; }
3395 // Return the file offset.
3398 { return this->offset_
; }
3400 // Whether this is a segment created to hold large data sections.
3402 is_large_data_segment() const
3403 { return this->is_large_data_segment_
; }
3405 // Record that this is a segment created to hold large data
3408 set_is_large_data_segment()
3409 { this->is_large_data_segment_
= true; }
3411 // Return the maximum alignment of the Output_data.
3413 maximum_alignment();
3415 // Add an Output_section to this segment.
3417 add_output_section(Output_section
* os
, elfcpp::Elf_Word seg_flags
);
3419 // Remove an Output_section from this segment. It is an error if it
3422 remove_output_section(Output_section
* os
);
3424 // Add an Output_data (which is not an Output_section) to the start
3427 add_initial_output_data(Output_data
*);
3429 // Return true if this segment has any sections which hold actual
3430 // data, rather than being a BSS section.
3432 has_any_data_sections() const
3433 { return !this->output_data_
.empty(); }
3435 // Return the number of dynamic relocations applied to this segment.
3437 dynamic_reloc_count() const;
3439 // Return the address of the first section.
3441 first_section_load_address() const;
3443 // Return whether the addresses have been set already.
3445 are_addresses_set() const
3446 { return this->are_addresses_set_
; }
3448 // Set the addresses.
3450 set_addresses(uint64_t vaddr
, uint64_t paddr
)
3452 this->vaddr_
= vaddr
;
3453 this->paddr_
= paddr
;
3454 this->are_addresses_set_
= true;
3457 // Set the segment flags. This is only used if we have a PHDRS
3458 // clause which explicitly specifies the flags.
3460 set_flags(elfcpp::Elf_Word flags
)
3461 { this->flags_
= flags
; }
3463 // Set the address of the segment to ADDR and the offset to *POFF
3464 // and set the addresses and offsets of all contained output
3465 // sections accordingly. Set the section indexes of all contained
3466 // output sections starting with *PSHNDX. If RESET is true, first
3467 // reset the addresses of the contained sections. Return the
3468 // address of the immediately following segment. Update *POFF and
3469 // *PSHNDX. This should only be called for a PT_LOAD segment.
3471 set_section_addresses(const Layout
*, bool reset
, uint64_t addr
, off_t
* poff
,
3472 unsigned int* pshndx
);
3474 // Set the minimum alignment of this segment. This may be adjusted
3475 // upward based on the section alignments.
3477 set_minimum_p_align(uint64_t align
)
3478 { this->min_p_align_
= align
; }
3480 // Set the offset of this segment based on the section. This should
3481 // only be called for a non-PT_LOAD segment.
3485 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3489 // Return the number of output sections.
3491 output_section_count() const;
3493 // Return the section attached to the list segment with the lowest
3494 // load address. This is used when handling a PHDRS clause in a
3497 section_with_lowest_load_address() const;
3499 // Write the segment header into *OPHDR.
3500 template<int size
, bool big_endian
>
3502 write_header(elfcpp::Phdr_write
<size
, big_endian
>*);
3504 // Write the section headers of associated sections into V.
3505 template<int size
, bool big_endian
>
3507 write_section_headers(const Layout
*, const Stringpool
*, unsigned char* v
,
3508 unsigned int* pshndx
) const;
3510 // Print the output sections in the map file.
3512 print_sections_to_mapfile(Mapfile
*) const;
3515 typedef std::list
<Output_data
*> Output_data_list
;
3517 // Find the maximum alignment in an Output_data_list.
3519 maximum_alignment_list(const Output_data_list
*);
3521 // Return whether the first data section is a relro section.
3523 is_first_section_relro() const;
3525 // Set the section addresses in an Output_data_list.
3527 set_section_list_addresses(const Layout
*, bool reset
, Output_data_list
*,
3528 uint64_t addr
, off_t
* poff
, unsigned int* pshndx
,
3529 bool* in_tls
, bool* in_relro
);
3531 // Return the number of Output_sections in an Output_data_list.
3533 output_section_count_list(const Output_data_list
*) const;
3535 // Return the number of dynamic relocs in an Output_data_list.
3537 dynamic_reloc_count_list(const Output_data_list
*) const;
3539 // Find the section with the lowest load address in an
3540 // Output_data_list.
3542 lowest_load_address_in_list(const Output_data_list
* pdl
,
3543 Output_section
** found
,
3544 uint64_t* found_lma
) const;
3546 // Write the section headers in the list into V.
3547 template<int size
, bool big_endian
>
3549 write_section_headers_list(const Layout
*, const Stringpool
*,
3550 const Output_data_list
*, unsigned char* v
,
3551 unsigned int* pshdx
) const;
3553 // Print a section list to the mapfile.
3555 print_section_list_to_mapfile(Mapfile
*, const Output_data_list
*) const;
3557 // NOTE: We want to use the copy constructor. Currently, shallow copy
3558 // works for us so we do not need to write our own copy constructor.
3560 // The list of output data with contents attached to this segment.
3561 Output_data_list output_data_
;
3562 // The list of output data without contents attached to this segment.
3563 Output_data_list output_bss_
;
3564 // The segment virtual address.
3566 // The segment physical address.
3568 // The size of the segment in memory.
3570 // The maximum section alignment. The is_max_align_known_ field
3571 // indicates whether this has been finalized.
3572 uint64_t max_align_
;
3573 // The required minimum value for the p_align field. This is used
3574 // for PT_LOAD segments. Note that this does not mean that
3575 // addresses should be aligned to this value; it means the p_paddr
3576 // and p_vaddr fields must be congruent modulo this value. For
3577 // non-PT_LOAD segments, the dynamic linker works more efficiently
3578 // if the p_align field has the more conventional value, although it
3579 // can align as needed.
3580 uint64_t min_p_align_
;
3581 // The offset of the segment data within the file.
3583 // The size of the segment data in the file.
3585 // The segment type;
3586 elfcpp::Elf_Word type_
;
3587 // The segment flags.
3588 elfcpp::Elf_Word flags_
;
3589 // Whether we have finalized max_align_.
3590 bool is_max_align_known_
: 1;
3591 // Whether vaddr and paddr were set by a linker script.
3592 bool are_addresses_set_
: 1;
3593 // Whether this segment holds large data sections.
3594 bool is_large_data_segment_
: 1;
3597 // This class represents the output file.
3602 Output_file(const char* name
);
3604 // Indicate that this is a temporary file which should not be
3608 { this->is_temporary_
= true; }
3610 // Try to open an existing file. Returns false if the file doesn't
3611 // exist, has a size of 0 or can't be mmaped. This method is
3614 open_for_modification();
3616 // Open the output file. FILE_SIZE is the final size of the file.
3617 // If the file already exists, it is deleted/truncated. This method
3618 // is thread-unsafe.
3620 open(off_t file_size
);
3622 // Resize the output file. This method is thread-unsafe.
3624 resize(off_t file_size
);
3626 // Close the output file (flushing all buffered data) and make sure
3627 // there are no errors. This method is thread-unsafe.
3631 // Return the size of this file.
3634 { return this->file_size_
; }
3636 // We currently always use mmap which makes the view handling quite
3637 // simple. In the future we may support other approaches.
3639 // Write data to the output file.
3641 write(off_t offset
, const void* data
, size_t len
)
3642 { memcpy(this->base_
+ offset
, data
, len
); }
3644 // Get a buffer to use to write to the file, given the offset into
3645 // the file and the size.
3647 get_output_view(off_t start
, size_t size
)
3649 gold_assert(start
>= 0
3650 && start
+ static_cast<off_t
>(size
) <= this->file_size_
);
3651 return this->base_
+ start
;
3654 // VIEW must have been returned by get_output_view. Write the
3655 // buffer to the file, passing in the offset and the size.
3657 write_output_view(off_t
, size_t, unsigned char*)
3660 // Get a read/write buffer. This is used when we want to write part
3661 // of the file, read it in, and write it again.
3663 get_input_output_view(off_t start
, size_t size
)
3664 { return this->get_output_view(start
, size
); }
3666 // Write a read/write buffer back to the file.
3668 write_input_output_view(off_t
, size_t, unsigned char*)
3671 // Get a read buffer. This is used when we just want to read part
3672 // of the file back it in.
3673 const unsigned char*
3674 get_input_view(off_t start
, size_t size
)
3675 { return this->get_output_view(start
, size
); }
3677 // Release a read bfufer.
3679 free_input_view(off_t
, size_t, const unsigned char*)
3683 // Map the file into memory or, if that fails, allocate anonymous
3688 // Allocate anonymous memory for the file.
3692 // Map the file into memory.
3696 // Unmap the file from memory (and flush to disk buffers).
3706 // Base of file mapped into memory.
3707 unsigned char* base_
;
3708 // True iff base_ points to a memory buffer rather than an output file.
3709 bool map_is_anonymous_
;
3710 // True if this is a temporary file which should not be output.
3714 } // End namespace gold.
3716 #endif // !defined(GOLD_OUTPUT_H)