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 // True if this is the .interp section which goes into the PT_INTERP
2394 { return this->is_interp_; }
2396 // Record that this is the interp section.
2399 { this->is_interp_ = true; }
2401 // True if this is a section used by the dynamic linker.
2403 is_dynamic_linker_section() const
2404 { return this->is_dynamic_linker_section_; }
2406 // Record that this is a section used by the dynamic linker.
2408 set_is_dynamic_linker_section()
2409 { this->is_dynamic_linker_section_ = true; }
2411 // Return whether this section should be written after all the input
2412 // sections are complete.
2414 after_input_sections() const
2415 { return this->after_input_sections_; }
2417 // Record that this section should be written after all the input
2418 // sections are complete.
2420 set_after_input_sections()
2421 { this->after_input_sections_ = true; }
2423 // Return whether this section requires postprocessing after all
2424 // relocations have been applied.
2426 requires_postprocessing() const
2427 { return this->requires_postprocessing_; }
2429 // If a section requires postprocessing, return the buffer to use.
2431 postprocessing_buffer() const
2433 gold_assert(this->postprocessing_buffer_ != NULL);
2434 return this->postprocessing_buffer_;
2437 // If a section requires postprocessing, create the buffer to use.
2439 create_postprocessing_buffer();
2441 // If a section requires postprocessing, this is the size of the
2442 // buffer to which relocations should be applied.
2444 postprocessing_buffer_size() const
2445 { return this->current_data_size_for_child(); }
2447 // Modify the section name. This is only permitted for an
2448 // unallocated section, and only before the size has been finalized.
2449 // Otherwise the name will not get into Layout::namepool_.
2451 set_name(const char* newname)
2453 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2454 gold_assert(!this->is_data_size_valid());
2455 this->name_ = newname;
2458 // Return whether the offset OFFSET in the input section SHNDX in
2459 // object OBJECT is being included in the link.
2461 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2462 off_t offset) const;
2464 // Return the offset within the output section of OFFSET relative to
2465 // the start of input section SHNDX in object OBJECT.
2467 output_offset(const Relobj* object, unsigned int shndx,
2468 section_offset_type offset) const;
2470 // Return the output virtual address of OFFSET relative to the start
2471 // of input section SHNDX in object OBJECT.
2473 output_address(const Relobj* object, unsigned int shndx,
2474 off_t offset) const;
2476 // Look for the merged section for input section SHNDX in object
2477 // OBJECT. If found, return true, and set *ADDR to the address of
2478 // the start of the merged section. This is not necessary the
2479 // output offset corresponding to input offset 0 in the section,
2480 // since the section may be mapped arbitrarily.
2482 find_starting_output_address(const Relobj* object, unsigned int shndx,
2483 uint64_t* addr) const;
2485 // Record that this output section was found in the SECTIONS clause
2486 // of a linker script.
2488 set_found_in_sections_clause()
2489 { this->found_in_sections_clause_ = true; }
2491 // Return whether this output section was found in the SECTIONS
2492 // clause of a linker script.
2494 found_in_sections_clause() const
2495 { return this->found_in_sections_clause_; }
2497 // Write the section header into *OPHDR.
2498 template<int size, bool big_endian>
2500 write_header(const Layout*, const Stringpool*,
2501 elfcpp::Shdr_write<size, big_endian>*) const;
2503 // The next few calls are for linker script support.
2505 // We need to export the input sections to linker scripts. Previously
2506 // we export a pair of Relobj pointer and section index. We now need to
2507 // handle relaxed input sections as well. So we use this class.
2508 class Simple_input_section
2511 static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2514 Simple_input_section(Relobj *relobj, unsigned int shndx)
2517 gold_assert(shndx != invalid_shndx);
2518 this->u_.relobj = relobj;
2521 Simple_input_section(Output_relaxed_input_section* section)
2522 : shndx_(invalid_shndx)
2523 { this->u_.relaxed_input_section = section; }
2525 // Whether this is a relaxed section.
2527 is_relaxed_input_section() const
2528 { return this->shndx_ == invalid_shndx; }
2530 // Return object of an input section.
2534 return ((this->shndx_ != invalid_shndx)
2536 : this->u_.relaxed_input_section->relobj());
2539 // Return index of an input section.
2543 return ((this->shndx_ != invalid_shndx)
2545 : this->u_.relaxed_input_section->shndx());
2548 // Return the Output_relaxed_input_section object of a relaxed section.
2549 Output_relaxed_input_section*
2550 relaxed_input_section() const
2552 gold_assert(this->shndx_ == invalid_shndx);
2553 return this->u_.relaxed_input_section;
2557 // Pointer to either an Relobj or an Output_relaxed_input_section.
2561 Output_relaxed_input_section* relaxed_input_section;
2563 // Section index for an non-relaxed section or invalid_shndx for
2564 // a relaxed section.
2565 unsigned int shndx_;
2568 // Store the list of input sections for this Output_section into the
2569 // list passed in. This removes the input sections, leaving only
2570 // any Output_section_data elements. This returns the size of those
2571 // Output_section_data elements. ADDRESS is the address of this
2572 // output section. FILL is the fill value to use, in case there are
2573 // any spaces between the remaining Output_section_data elements.
2575 get_input_sections(uint64_t address, const std::string& fill,
2576 std::list<Simple_input_section>*);
2578 // Add an input section from a script.
2580 add_input_section_for_script(const Simple_input_section& input_section,
2581 off_t data_size, uint64_t addralign);
2583 // Set the current size of the output section.
2585 set_current_data_size(off_t size)
2586 { this->set_current_data_size_for_child(size); }
2588 // Get the current size of the output section.
2590 current_data_size() const
2591 { return this->current_data_size_for_child(); }
2593 // End of linker script support.
2595 // Save states before doing section layout.
2596 // This is used for relaxation.
2600 // Restore states prior to section layout.
2604 // Convert existing input sections to relaxed input sections.
2606 convert_input_sections_to_relaxed_sections(
2607 const std::vector<Output_relaxed_input_section*>& sections);
2609 // Print merge statistics to stderr.
2611 print_merge_stats();
2614 // Return the output section--i.e., the object itself.
2619 // Return the section index in the output file.
2621 do_out_shndx() const
2623 gold_assert(this->out_shndx_ != -1U);
2624 return this->out_shndx_;
2627 // Set the output section index.
2629 do_set_out_shndx(unsigned int shndx)
2631 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2632 this->out_shndx_ = shndx;
2635 // Set the final data size of the Output_section. For a typical
2636 // Output_section, there is nothing to do, but if there are any
2637 // Output_section_data objects we need to set their final addresses
2640 set_final_data_size();
2642 // Reset the address and file offset.
2644 do_reset_address_and_file_offset();
2646 // Return true if address and file offset already have reset values. In
2647 // other words, calling reset_address_and_file_offset will not change them.
2649 do_address_and_file_offset_have_reset_values() const;
2651 // Write the data to the file. For a typical Output_section, this
2652 // does nothing: the data is written out by calling Object::Relocate
2653 // on each input object. But if there are any Output_section_data
2654 // objects we do need to write them out here.
2656 do_write(Output_file*);
2658 // Return the address alignment--function required by parent class.
2660 do_addralign() const
2661 { return this->addralign_; }
2663 // Return whether there is a load address.
2665 do_has_load_address() const
2666 { return this->has_load_address_; }
2668 // Return the load address.
2670 do_load_address() const
2672 gold_assert(this->has_load_address_);
2673 return this->load_address_;
2676 // Return whether this is an Output_section.
2678 do_is_section() const
2681 // Return whether this is a section of the specified type.
2683 do_is_section_type(elfcpp::Elf_Word type) const
2684 { return this->type_ == type; }
2686 // Return whether the specified section flag is set.
2688 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2689 { return (this->flags_ & flag) != 0; }
2691 // Set the TLS offset. Called only for SHT_TLS sections.
2693 do_set_tls_offset(uint64_t tls_base);
2695 // Return the TLS offset, relative to the base of the TLS segment.
2696 // Valid only for SHT_TLS sections.
2698 do_tls_offset() const
2699 { return this->tls_offset_; }
2701 // This may be implemented by a child class.
2703 do_finalize_name(Layout*)
2706 // Print to the map file.
2708 do_print_to_mapfile(Mapfile*) const;
2710 // Record that this section requires postprocessing after all
2711 // relocations have been applied. This is called by a child class.
2713 set_requires_postprocessing()
2715 this->requires_postprocessing_ = true;
2716 this->after_input_sections_ = true;
2719 // Write all the data of an Output_section into the postprocessing
2722 write_to_postprocessing_buffer();
2724 // In some cases we need to keep a list of the input sections
2725 // associated with this output section. We only need the list if we
2726 // might have to change the offsets of the input section within the
2727 // output section after we add the input section. The ordinary
2728 // input sections will be written out when we process the object
2729 // file, and as such we don't need to track them here. We do need
2730 // to track Output_section_data objects here. We store instances of
2731 // this structure in a std::vector, so it must be a POD. There can
2732 // be many instances of this structure, so we use a union to save
2738 : shndx_(0), p2align_(0)
2740 this->u1_.data_size = 0;
2741 this->u2_.object = NULL;
2744 // For an ordinary input section.
2745 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2748 p2align_(ffsll(static_cast<long long>(addralign)))
2750 gold_assert(shndx != OUTPUT_SECTION_CODE
2751 && shndx != MERGE_DATA_SECTION_CODE
2752 && shndx != MERGE_STRING_SECTION_CODE
2753 && shndx != RELAXED_INPUT_SECTION_CODE);
2754 this->u1_.data_size = data_size;
2755 this->u2_.object = object;
2758 // For a non-merge output section.
2759 Input_section(Output_section_data* posd)
2760 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2762 this->u1_.data_size = 0;
2763 this->u2_.posd = posd;
2766 // For a merge section.
2767 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2769 ? MERGE_STRING_SECTION_CODE
2770 : MERGE_DATA_SECTION_CODE),
2773 this->u1_.entsize = entsize;
2774 this->u2_.posd = posd;
2777 // For a relaxed input section.
2778 Input_section(Output_relaxed_input_section *psection)
2779 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2781 this->u1_.data_size = 0;
2782 this->u2_.poris = psection;
2785 // The required alignment.
2789 if (!this->is_input_section())
2790 return this->u2_.posd->addralign();
2791 return (this->p2align_ == 0
2793 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2796 // Return the required size.
2800 // Whether this is an input section.
2802 is_input_section() const
2804 return (this->shndx_ != OUTPUT_SECTION_CODE
2805 && this->shndx_ != MERGE_DATA_SECTION_CODE
2806 && this->shndx_ != MERGE_STRING_SECTION_CODE
2807 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
2810 // Return whether this is a merge section which matches the
2813 is_merge_section(bool is_string, uint64_t entsize,
2814 uint64_t addralign) const
2816 return (this->shndx_ == (is_string
2817 ? MERGE_STRING_SECTION_CODE
2818 : MERGE_DATA_SECTION_CODE)
2819 && this->u1_.entsize == entsize
2820 && this->addralign() == addralign);
2823 // Return whether this is a relaxed input section.
2825 is_relaxed_input_section() const
2826 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2828 // Return whether this is a generic Output_section_data.
2830 is_output_section_data() const
2832 return this->shndx_ == OUTPUT_SECTION_CODE;
2835 // Return the object for an input section.
2839 if (this->is_input_section())
2840 return this->u2_.object;
2841 else if (this->is_relaxed_input_section())
2842 return this->u2_.poris->relobj();
2847 // Return the input section index for an input section.
2851 if (this->is_input_section())
2852 return this->shndx_;
2853 else if (this->is_relaxed_input_section())
2854 return this->u2_.poris->shndx();
2859 // For non-input-sections, return the associated Output_section_data
2861 Output_section_data*
2862 output_section_data() const
2864 gold_assert(!this->is_input_section());
2865 return this->u2_.posd;
2868 // Return the Output_relaxed_input_section object.
2869 Output_relaxed_input_section*
2870 relaxed_input_section() const
2872 gold_assert(this->is_relaxed_input_section());
2873 return this->u2_.poris;
2876 // Set the output section.
2878 set_output_section(Output_section* os)
2880 gold_assert(!this->is_input_section());
2881 Output_section_data *posd =
2882 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2883 posd->set_output_section(os);
2886 // Set the address and file offset. This is called during
2887 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2888 // the enclosing section.
2890 set_address_and_file_offset(uint64_t address, off_t file_offset,
2891 off_t section_file_offset);
2893 // Reset the address and file offset.
2895 reset_address_and_file_offset();
2897 // Finalize the data size.
2899 finalize_data_size();
2901 // Add an input section, for SHF_MERGE sections.
2903 add_input_section(Relobj* object, unsigned int shndx)
2905 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2906 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2907 return this->u2_.posd->add_input_section(object, shndx);
2910 // Given an input OBJECT, an input section index SHNDX within that
2911 // object, and an OFFSET relative to the start of that input
2912 // section, return whether or not the output offset is known. If
2913 // this function returns true, it sets *POUTPUT to the offset in
2914 // the output section, relative to the start of the input section
2915 // in the output section. *POUTPUT may be different from OFFSET
2916 // for a merged section.
2918 output_offset(const Relobj* object, unsigned int shndx,
2919 section_offset_type offset,
2920 section_offset_type *poutput) const;
2922 // Return whether this is the merge section for the input section
2925 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2927 // Write out the data. This does nothing for an input section.
2929 write(Output_file*);
2931 // Write the data to a buffer. This does nothing for an input
2934 write_to_buffer(unsigned char*);
2936 // Print to a map file.
2938 print_to_mapfile(Mapfile*) const;
2940 // Print statistics about merge sections to stderr.
2942 print_merge_stats(const char* section_name)
2944 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2945 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2946 this->u2_.posd->print_merge_stats(section_name);
2950 // Code values which appear in shndx_. If the value is not one of
2951 // these codes, it is the input section index in the object file.
2954 // An Output_section_data.
2955 OUTPUT_SECTION_CODE = -1U,
2956 // An Output_section_data for an SHF_MERGE section with
2957 // SHF_STRINGS not set.
2958 MERGE_DATA_SECTION_CODE = -2U,
2959 // An Output_section_data for an SHF_MERGE section with
2961 MERGE_STRING_SECTION_CODE = -3U,
2962 // An Output_section_data for a relaxed input section.
2963 RELAXED_INPUT_SECTION_CODE = -4U
2966 // For an ordinary input section, this is the section index in the
2967 // input file. For an Output_section_data, this is
2968 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2969 // MERGE_STRING_SECTION_CODE.
2970 unsigned int shndx_;
2971 // The required alignment, stored as a power of 2.
2972 unsigned int p2align_;
2975 // For an ordinary input section, the section size.
2977 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2978 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2984 // For an ordinary input section, the object which holds the
2987 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2988 // MERGE_STRING_SECTION_CODE, the data.
2989 Output_section_data* posd;
2990 // For RELAXED_INPUT_SECTION_CODE, the data.
2991 Output_relaxed_input_section* poris;
2995 typedef std::vector<Input_section> Input_section_list;
2997 // Allow a child class to access the input sections.
2998 const Input_section_list&
2999 input_sections() const
3000 { return this->input_sections_; }
3003 // We only save enough information to undo the effects of section layout.
3004 class Checkpoint_output_section
3007 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3008 const Input_section_list& input_sections,
3009 off_t first_input_offset,
3010 bool attached_input_sections_are_sorted)
3011 : addralign_(addralign), flags_(flags),
3012 input_sections_(input_sections),
3013 input_sections_size_(input_sections_.size()),
3014 input_sections_copy_(), first_input_offset_(first_input_offset),
3015 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3019 ~Checkpoint_output_section()
3022 // Return the address alignment.
3025 { return this->addralign_; }
3027 // Return the section flags.
3030 { return this->flags_; }
3032 // Return a reference to the input section list copy.
3035 { return &this->input_sections_copy_; }
3037 // Return the size of input_sections at the time when checkpoint is
3040 input_sections_size() const
3041 { return this->input_sections_size_; }
3043 // Whether input sections are copied.
3045 input_sections_saved() const
3046 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3049 first_input_offset() const
3050 { return this->first_input_offset_; }
3053 attached_input_sections_are_sorted() const
3054 { return this->attached_input_sections_are_sorted_; }
3056 // Save input sections.
3058 save_input_sections()
3060 this->input_sections_copy_.reserve(this->input_sections_size_);
3061 this->input_sections_copy_.clear();
3062 Input_section_list::const_iterator p = this->input_sections_.begin();
3063 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3064 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3065 this->input_sections_copy_.push_back(*p);
3069 // The section alignment.
3070 uint64_t addralign_;
3071 // The section flags.
3072 elfcpp::Elf_Xword flags_;
3073 // Reference to the input sections to be checkpointed.
3074 const Input_section_list& input_sections_;
3075 // Size of the checkpointed portion of input_sections_;
3076 size_t input_sections_size_;
3077 // Copy of input sections.
3078 Input_section_list input_sections_copy_;
3079 // The offset of the first entry in input_sections_.
3080 off_t first_input_offset_;
3081 // True if the input sections attached to this output section have
3082 // already been sorted.
3083 bool attached_input_sections_are_sorted_;
3086 // This class is used to sort the input sections.
3087 class Input_section_sort_entry;
3089 // This is the sort comparison function.
3090 struct Input_section_sort_compare
3093 operator()(const Input_section_sort_entry&,
3094 const Input_section_sort_entry&) const;
3097 // Fill data. This is used to fill in data between input sections.
3098 // It is also used for data statements (BYTE, WORD, etc.) in linker
3099 // scripts. When we have to keep track of the input sections, we
3100 // can use an Output_data_const, but we don't want to have to keep
3101 // track of input sections just to implement fills.
3105 Fill(off_t section_offset, off_t length)
3106 : section_offset_(section_offset),
3107 length_(convert_to_section_size_type(length))
3110 // Return section offset.
3112 section_offset() const
3113 { return this->section_offset_; }
3115 // Return fill length.
3118 { return this->length_; }
3121 // The offset within the output section.
3122 off_t section_offset_;
3123 // The length of the space to fill.
3124 section_size_type length_;
3127 typedef std::vector<Fill> Fill_list;
3129 // This class describes properties of merge data sections. It is used
3130 // as a key type for maps.
3131 class Merge_section_properties
3134 Merge_section_properties(bool is_string, uint64_t entsize,
3136 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
3139 // Whether this equals to another Merge_section_properties MSP.
3141 eq(const Merge_section_properties& msp) const
3143 return ((this->is_string_ == msp.is_string_)
3144 && (this->entsize_ == msp.entsize_)
3145 && (this->addralign_ == msp.addralign_));
3148 // Compute a hash value for this using 64-bit FNV-1a hash.
3152 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
3153 uint64_t prime = 1099511628211ULL;
3154 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
3155 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
3156 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3160 // Functors for associative containers.
3164 operator()(const Merge_section_properties& msp1,
3165 const Merge_section_properties& msp2) const
3166 { return msp1.eq(msp2); }
3172 operator()(const Merge_section_properties& msp) const
3173 { return msp.hash_value(); }
3177 // Whether this merge data section is for strings.
3179 // Entsize of this merge data section.
3181 // Address alignment.
3182 uint64_t addralign_;
3185 // Map that link Merge_section_properties to Output_merge_base.
3186 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3187 Merge_section_properties::hash,
3188 Merge_section_properties::equal_to>
3189 Merge_section_by_properties_map;
3191 // Map that link Input_section_specifier to Output_section_data.
3192 typedef Unordered_map<Input_section_specifier, Output_section_data*,
3193 Input_section_specifier::hash,
3194 Input_section_specifier::equal_to>
3195 Output_section_data_by_input_section_map;
3197 // Map used during relaxation of existing sections. This map
3198 // an input section specifier to an input section list index.
3199 // We assume that Input_section_list is a vector.
3200 typedef Unordered_map<Input_section_specifier, size_t,
3201 Input_section_specifier::hash,
3202 Input_section_specifier::equal_to>
3205 // Add a new output section by Input_section.
3207 add_output_section_data(Input_section*);
3209 // Add an SHF_MERGE input section. Returns true if the section was
3212 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3213 uint64_t entsize, uint64_t addralign);
3215 // Add an output SHF_MERGE section POSD to this output section.
3216 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3217 // ENTSIZE is the entity size. This returns the entry added to
3220 add_output_merge_section(Output_section_data* posd, bool is_string,
3223 // Sort the attached input sections.
3225 sort_attached_input_sections();
3227 // Find the merge section into which an input section with index SHNDX in
3228 // OBJECT has been added. Return NULL if none found.
3229 Output_section_data*
3230 find_merge_section(const Relobj* object, unsigned int shndx) const;
3232 // Find a relaxed input section to an input section in OBJECT
3233 // with index SHNDX. Return NULL if none is found.
3234 const Output_section_data*
3235 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3237 // Build a relaxation map.
3239 build_relaxation_map(
3240 const Input_section_list& input_sections,
3242 Relaxation_map* map) const;
3244 // Convert input sections in an input section list into relaxed sections.
3246 convert_input_sections_in_list_to_relaxed_sections(
3247 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3248 const Relaxation_map& map,
3249 Input_section_list* input_sections);
3251 // Most of these fields are only valid after layout.
3253 // The name of the section. This will point into a Stringpool.
3255 // The section address is in the parent class.
3256 // The section alignment.
3257 uint64_t addralign_;
3258 // The section entry size.
3260 // The load address. This is only used when using a linker script
3261 // with a SECTIONS clause. The has_load_address_ field indicates
3262 // whether this field is valid.
3263 uint64_t load_address_;
3264 // The file offset is in the parent class.
3265 // Set the section link field to the index of this section.
3266 const Output_data* link_section_;
3267 // If link_section_ is NULL, this is the link field.
3269 // Set the section info field to the index of this section.
3270 const Output_section* info_section_;
3271 // If info_section_ is NULL, set the info field to the symbol table
3272 // index of this symbol.
3273 const Symbol* info_symndx_;
3274 // If info_section_ and info_symndx_ are NULL, this is the section
3277 // The section type.
3278 const elfcpp::Elf_Word type_;
3279 // The section flags.
3280 elfcpp::Elf_Xword flags_;
3281 // The section index.
3282 unsigned int out_shndx_;
3283 // If there is a STT_SECTION for this output section in the normal
3284 // symbol table, this is the symbol index. This starts out as zero.
3285 // It is initialized in Layout::finalize() to be the index, or -1U
3286 // if there isn't one.
3287 unsigned int symtab_index_;
3288 // If there is a STT_SECTION for this output section in the dynamic
3289 // symbol table, this is the symbol index. This starts out as zero.
3290 // It is initialized in Layout::finalize() to be the index, or -1U
3291 // if there isn't one.
3292 unsigned int dynsym_index_;
3293 // The input sections. This will be empty in cases where we don't
3294 // need to keep track of them.
3295 Input_section_list input_sections_;
3296 // The offset of the first entry in input_sections_.
3297 off_t first_input_offset_;
3298 // The fill data. This is separate from input_sections_ because we
3299 // often will need fill sections without needing to keep track of
3302 // If the section requires postprocessing, this buffer holds the
3303 // section contents during relocation.
3304 unsigned char* postprocessing_buffer_;
3305 // Whether this output section needs a STT_SECTION symbol in the
3306 // normal symbol table. This will be true if there is a relocation
3308 bool needs_symtab_index_ : 1;
3309 // Whether this output section needs a STT_SECTION symbol in the
3310 // dynamic symbol table. This will be true if there is a dynamic
3311 // relocation which needs it.
3312 bool needs_dynsym_index_ : 1;
3313 // Whether the link field of this output section should point to the
3314 // normal symbol table.
3315 bool should_link_to_symtab_ : 1;
3316 // Whether the link field of this output section should point to the
3317 // dynamic symbol table.
3318 bool should_link_to_dynsym_ : 1;
3319 // Whether this section should be written after all the input
3320 // sections are complete.
3321 bool after_input_sections_ : 1;
3322 // Whether this section requires post processing after all
3323 // relocations have been applied.
3324 bool requires_postprocessing_ : 1;
3325 // Whether an input section was mapped to this output section
3326 // because of a SECTIONS clause in a linker script.
3327 bool found_in_sections_clause_ : 1;
3328 // Whether this section has an explicitly specified load address.
3329 bool has_load_address_ : 1;
3330 // True if the info_section_ field means the section index of the
3331 // section, false if it means the symbol index of the corresponding
3333 bool info_uses_section_index_ : 1;
3334 // True if the input sections attached to this output section may
3336 bool may_sort_attached_input_sections_ : 1;
3337 // True if the input sections attached to this output section must
3339 bool must_sort_attached_input_sections_ : 1;
3340 // True if the input sections attached to this output section have
3341 // already been sorted.
3342 bool attached_input_sections_are_sorted_ : 1;
3343 // True if this section holds relro data.
3345 // True if this section holds relro local data.
3346 bool is_relro_local_ : 1;
3347 // True if this is a small section.
3348 bool is_small_section_ : 1;
3349 // True if this is a large section.
3350 bool is_large_section_ : 1;
3351 // True if this is the .interp section going into the PT_INTERP
3353 bool is_interp_ : 1;
3354 // True if this is section is read by the dynamic linker.
3355 bool is_dynamic_linker_section_ : 1;
3356 // Whether code-fills are generated at write.
3357 bool generate_code_fills_at_write_ : 1;
3358 // For SHT_TLS sections, the offset of this section relative to the base
3359 // of the TLS segment.
3360 uint64_t tls_offset_;
3361 // Saved checkpoint.
3362 Checkpoint_output_section* checkpoint_;
3363 // Map from input sections to merge sections.
3364 Output_section_data_by_input_section_map merge_section_map_;
3365 // Map from merge section properties to merge_sections;
3366 Merge_section_by_properties_map merge_section_by_properties_map_;
3367 // Map from input sections to relaxed input sections. This is mutable
3368 // because it is updated lazily. We may need to update it in a
3369 // const qualified method.
3370 mutable Output_section_data_by_input_section_map relaxed_input_section_map_;
3371 // Whether relaxed_input_section_map_ is valid.
3372 mutable bool is_relaxed_input_section_map_valid_;
3375 // An output segment. PT_LOAD segments are built from collections of
3376 // output sections. Other segments typically point within PT_LOAD
3377 // segments, and are built directly as needed.
3379 // NOTE: We want to use the copy constructor for this class. During
3380 // relaxation, we may try built the segments multiple times. We do
3381 // that by copying the original segment list before lay-out, doing
3382 // a trial lay-out and roll-back to the saved copied if we need to
3383 // to the lay-out again.
3385 class Output_segment
3388 // Create an output segment, specifying the type and flags.
3389 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3391 // Return the virtual address.
3394 { return this->vaddr_; }
3396 // Return the physical address.
3399 { return this->paddr_; }
3401 // Return the segment type.
3404 { return this->type_; }
3406 // Return the segment flags.
3409 { return this->flags_; }
3411 // Return the memory size.
3414 { return this->memsz_; }
3416 // Return the file size.
3419 { return this->filesz_; }
3421 // Return the file offset.
3424 { return this->offset_; }
3426 // Whether this is a segment created to hold large data sections.
3428 is_large_data_segment() const
3429 { return this->is_large_data_segment_; }
3431 // Record that this is a segment created to hold large data
3434 set_is_large_data_segment()
3435 { this->is_large_data_segment_ = true; }
3437 // Return the maximum alignment of the Output_data.
3439 maximum_alignment();
3441 // Add the Output_section OS to this segment. SEG_FLAGS is the
3442 // segment flags to use. DO_SORT is true if we should sort the
3443 // placement of the input section for more efficient generated code.
3445 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags,
3448 // Remove an Output_section from this segment. It is an error if it
3451 remove_output_section(Output_section* os);
3453 // Add an Output_data (which is not an Output_section) to the start
3456 add_initial_output_data(Output_data*);
3458 // Return true if this segment has any sections which hold actual
3459 // data, rather than being a BSS section.
3461 has_any_data_sections() const
3462 { return !this->output_data_.empty(); }
3464 // Return the number of dynamic relocations applied to this segment.
3466 dynamic_reloc_count() const;
3468 // Return the address of the first section.
3470 first_section_load_address() const;
3472 // Return whether the addresses have been set already.
3474 are_addresses_set() const
3475 { return this->are_addresses_set_; }
3477 // Set the addresses.
3479 set_addresses(uint64_t vaddr, uint64_t paddr)
3481 this->vaddr_ = vaddr;
3482 this->paddr_ = paddr;
3483 this->are_addresses_set_ = true;
3486 // Set the segment flags. This is only used if we have a PHDRS
3487 // clause which explicitly specifies the flags.
3489 set_flags(elfcpp::Elf_Word flags)
3490 { this->flags_ = flags; }
3492 // Set the address of the segment to ADDR and the offset to *POFF
3493 // and set the addresses and offsets of all contained output
3494 // sections accordingly. Set the section indexes of all contained
3495 // output sections starting with *PSHNDX. If RESET is true, first
3496 // reset the addresses of the contained sections. Return the
3497 // address of the immediately following segment. Update *POFF and
3498 // *PSHNDX. This should only be called for a PT_LOAD segment.
3500 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
3501 unsigned int* pshndx);
3503 // Set the minimum alignment of this segment. This may be adjusted
3504 // upward based on the section alignments.
3506 set_minimum_p_align(uint64_t align)
3507 { this->min_p_align_ = align; }
3509 // Set the offset of this segment based on the section. This should
3510 // only be called for a non-PT_LOAD segment.
3514 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3518 // Return the number of output sections.
3520 output_section_count() const;
3522 // Return the section attached to the list segment with the lowest
3523 // load address. This is used when handling a PHDRS clause in a
3526 section_with_lowest_load_address() const;
3528 // Write the segment header into *OPHDR.
3529 template<int size, bool big_endian>
3531 write_header(elfcpp::Phdr_write<size, big_endian>*);
3533 // Write the section headers of associated sections into V.
3534 template<int size, bool big_endian>
3536 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3537 unsigned int* pshndx) const;
3539 // Print the output sections in the map file.
3541 print_sections_to_mapfile(Mapfile*) const;
3544 typedef std::list<Output_data*> Output_data_list;
3546 // Find the maximum alignment in an Output_data_list.
3548 maximum_alignment_list(const Output_data_list*);
3550 // Return whether the first data section is a relro section.
3552 is_first_section_relro() const;
3554 // Set the section addresses in an Output_data_list.
3556 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3557 uint64_t addr, off_t* poff, unsigned int* pshndx,
3558 bool* in_tls, bool* in_relro);
3560 // Return the number of Output_sections in an Output_data_list.
3562 output_section_count_list(const Output_data_list*) const;
3564 // Return the number of dynamic relocs in an Output_data_list.
3566 dynamic_reloc_count_list(const Output_data_list*) const;
3568 // Find the section with the lowest load address in an
3569 // Output_data_list.
3571 lowest_load_address_in_list(const Output_data_list* pdl,
3572 Output_section** found,
3573 uint64_t* found_lma) const;
3575 // Write the section headers in the list into V.
3576 template<int size, bool big_endian>
3578 write_section_headers_list(const Layout*, const Stringpool*,
3579 const Output_data_list*, unsigned char* v,
3580 unsigned int* pshdx) const;
3582 // Print a section list to the mapfile.
3584 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3586 // NOTE: We want to use the copy constructor. Currently, shallow copy
3587 // works for us so we do not need to write our own copy constructor.
3589 // The list of output data with contents attached to this segment.
3590 Output_data_list output_data_;
3591 // The list of output data without contents attached to this segment.
3592 Output_data_list output_bss_;
3593 // The segment virtual address.
3595 // The segment physical address.
3597 // The size of the segment in memory.
3599 // The maximum section alignment. The is_max_align_known_ field
3600 // indicates whether this has been finalized.
3601 uint64_t max_align_;
3602 // The required minimum value for the p_align field. This is used
3603 // for PT_LOAD segments. Note that this does not mean that
3604 // addresses should be aligned to this value; it means the p_paddr
3605 // and p_vaddr fields must be congruent modulo this value. For
3606 // non-PT_LOAD segments, the dynamic linker works more efficiently
3607 // if the p_align field has the more conventional value, although it
3608 // can align as needed.
3609 uint64_t min_p_align_;
3610 // The offset of the segment data within the file.
3612 // The size of the segment data in the file.
3614 // The segment type;
3615 elfcpp::Elf_Word type_;
3616 // The segment flags.
3617 elfcpp::Elf_Word flags_;
3618 // Whether we have finalized max_align_.
3619 bool is_max_align_known_ : 1;
3620 // Whether vaddr and paddr were set by a linker script.
3621 bool are_addresses_set_ : 1;
3622 // Whether this segment holds large data sections.
3623 bool is_large_data_segment_ : 1;
3626 // This class represents the output file.
3631 Output_file(const char* name);
3633 // Indicate that this is a temporary file which should not be
3637 { this->is_temporary_ = true; }
3639 // Try to open an existing file. Returns false if the file doesn't
3640 // exist, has a size of 0 or can't be mmaped. This method is
3643 open_for_modification();
3645 // Open the output file. FILE_SIZE is the final size of the file.
3646 // If the file already exists, it is deleted/truncated. This method
3647 // is thread-unsafe.
3649 open(off_t file_size);
3651 // Resize the output file. This method is thread-unsafe.
3653 resize(off_t file_size);
3655 // Close the output file (flushing all buffered data) and make sure
3656 // there are no errors. This method is thread-unsafe.
3660 // Return the size of this file.
3663 { return this->file_size_; }
3665 // We currently always use mmap which makes the view handling quite
3666 // simple. In the future we may support other approaches.
3668 // Write data to the output file.
3670 write(off_t offset, const void* data, size_t len)
3671 { memcpy(this->base_ + offset, data, len); }
3673 // Get a buffer to use to write to the file, given the offset into
3674 // the file and the size.
3676 get_output_view(off_t start, size_t size)
3678 gold_assert(start >= 0
3679 && start + static_cast<off_t>(size) <= this->file_size_);
3680 return this->base_ + start;
3683 // VIEW must have been returned by get_output_view. Write the
3684 // buffer to the file, passing in the offset and the size.
3686 write_output_view(off_t, size_t, unsigned char*)
3689 // Get a read/write buffer. This is used when we want to write part
3690 // of the file, read it in, and write it again.
3692 get_input_output_view(off_t start, size_t size)
3693 { return this->get_output_view(start, size); }
3695 // Write a read/write buffer back to the file.
3697 write_input_output_view(off_t, size_t, unsigned char*)
3700 // Get a read buffer. This is used when we just want to read part
3701 // of the file back it in.
3702 const unsigned char*
3703 get_input_view(off_t start, size_t size)
3704 { return this->get_output_view(start, size); }
3706 // Release a read bfufer.
3708 free_input_view(off_t, size_t, const unsigned char*)
3712 // Map the file into memory or, if that fails, allocate anonymous
3717 // Allocate anonymous memory for the file.
3721 // Map the file into memory.
3725 // Unmap the file from memory (and flush to disk buffers).
3735 // Base of file mapped into memory.
3736 unsigned char* base_;
3737 // True iff base_ points to a memory buffer rather than an output file.
3738 bool map_is_anonymous_;
3739 // True if this is a temporary file which should not be output.
3743 } // End namespace gold.
3745 #endif // !defined(GOLD_OUTPUT_H)