bfd/
[binutils.git] / gold / output.h
blobb6527a34f72f57566425c54c8fab5c93abb2824d
1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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.
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
26 #include <list>
27 #include <vector>
29 #include "elfcpp.h"
30 #include "layout.h"
31 #include "reloc-types.h"
33 namespace gold
36 class General_options;
37 class Object;
38 class Symbol;
39 class Output_file;
40 class Output_section;
41 class Relocatable_relocs;
42 class Target;
43 template<int size, bool big_endian>
44 class Sized_target;
45 template<int size, bool big_endian>
46 class Sized_relobj;
48 // An abtract class for data which has to go into the output file.
50 class Output_data
52 public:
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
57 dynamic_reloc_count_(0)
58 { }
60 virtual
61 ~Output_data();
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
65 uint64_t
66 address() const
68 gold_assert(this->is_address_valid_);
69 return this->address_;
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
75 off_t
76 data_size() const
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
82 // Return the file offset. This is only valid after
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
85 off_t
86 offset() const
88 gold_assert(this->is_offset_valid_);
89 return this->offset_;
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
94 void
95 reset_address_and_file_offset()
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
103 // Return the required alignment.
104 uint64_t
105 addralign() const
106 { return this->do_addralign(); }
108 // Return whether this has a load address.
109 bool
110 has_load_address() const
111 { return this->do_has_load_address(); }
113 // Return the load address.
114 uint64_t
115 load_address() const
116 { return this->do_load_address(); }
118 // Return whether this is an Output_section.
119 bool
120 is_section() const
121 { return this->do_is_section(); }
123 // Return whether this is an Output_section of the specified type.
124 bool
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
128 // Return whether this is an Output_section with the specified flag
129 // set.
130 bool
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
134 // Return the output section that this goes in, if there is one.
135 Output_section*
136 output_section()
137 { return this->do_output_section(); }
139 // Return the output section index, if there is an output section.
140 unsigned int
141 out_shndx() const
142 { return this->do_out_shndx(); }
144 // Set the output section index, if this is an output section.
145 void
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
152 void
153 set_address_and_file_offset(uint64_t addr, off_t off)
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
160 // Set the address.
161 void
162 set_address(uint64_t addr)
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
169 // Set the file offset.
170 void
171 set_file_offset(off_t off)
173 gold_assert(!this->is_offset_valid_);
174 this->offset_ = off;
175 this->is_offset_valid_ = true;
178 // Finalize the data size.
179 void
180 finalize_data_size()
182 if (!this->is_data_size_valid_)
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
190 // Set the TLS offset. Called only for SHT_TLS sections.
191 void
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
197 uint64_t
198 tls_offset() const
199 { return this->do_tls_offset(); }
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
203 void
204 write(Output_file* file)
205 { this->do_write(file); }
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
209 static void
210 layout_complete()
211 { Output_data::allocated_sizes_are_fixed = true; }
213 // Used to check that layout has been done.
214 static bool
215 is_layout_complete()
216 { return Output_data::allocated_sizes_are_fixed; }
218 // Count the number of dynamic relocations applied to this section.
219 void
220 add_dynamic_reloc()
221 { ++this->dynamic_reloc_count_; }
223 // Return the number of dynamic relocations applied to this section.
224 unsigned int
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
228 // Whether the address is valid.
229 bool
230 is_address_valid() const
231 { return this->is_address_valid_; }
233 // Whether the file offset is valid.
234 bool
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
238 // Whether the data size is valid.
239 bool
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
243 protected:
244 // Functions that child classes may or in some cases must implement.
246 // Write the data to the output file.
247 virtual void
248 do_write(Output_file*) = 0;
250 // Return the required alignment.
251 virtual uint64_t
252 do_addralign() const = 0;
254 // Return whether this has a load address.
255 virtual bool
256 do_has_load_address() const
257 { return false; }
259 // Return the load address.
260 virtual uint64_t
261 do_load_address() const
262 { gold_unreachable(); }
264 // Return whether this is an Output_section.
265 virtual bool
266 do_is_section() const
267 { return false; }
269 // Return whether this is an Output_section of the specified type.
270 // This only needs to be implement by Output_section.
271 virtual bool
272 do_is_section_type(elfcpp::Elf_Word) const
273 { return false; }
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
277 virtual bool
278 do_is_section_flag_set(elfcpp::Elf_Xword) const
279 { return false; }
281 // Return the output section, if there is one.
282 virtual Output_section*
283 do_output_section()
284 { return NULL; }
286 // Return the output section index, if there is an output section.
287 virtual unsigned int
288 do_out_shndx() const
289 { gold_unreachable(); }
291 // Set the output section index, if this is an output section.
292 virtual void
293 do_set_out_shndx(unsigned int)
294 { gold_unreachable(); }
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
299 virtual void
300 set_final_data_size()
301 { gold_unreachable(); }
303 // A hook for resetting the address and file offset.
304 virtual void
305 do_reset_address_and_file_offset()
308 // Set the TLS offset. Called only for SHT_TLS sections.
309 virtual void
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
315 virtual uint64_t
316 do_tls_offset() const
317 { gold_unreachable(); }
319 // Functions that child classes may call.
321 // Set the size of the data.
322 void
323 set_data_size(off_t data_size)
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
332 off_t
333 current_data_size_for_child() const
334 { return this->data_size_; }
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
338 void
339 set_current_data_size_for_child(off_t data_size)
341 gold_assert(!this->is_data_size_valid_);
342 this->data_size_ = data_size;
345 // Return default alignment for the target size.
346 static uint64_t
347 default_alignment();
349 // Return default alignment for a specified size--32 or 64.
350 static uint64_t
351 default_alignment_for_size(int size);
353 private:
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
357 // This is used for verification, to make sure that we don't try to
358 // change any sizes of allocated sections after we set the section
359 // addresses.
360 static bool allocated_sizes_are_fixed;
362 // Memory address in output file.
363 uint64_t address_;
364 // Size of data in output file.
365 off_t data_size_;
366 // File offset of contents in output file.
367 off_t offset_;
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
378 // Output the section headers.
380 class Output_section_headers : public Output_data
382 public:
383 Output_section_headers(const Layout*,
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
386 const Layout::Section_list*,
387 const Stringpool*,
388 const Output_section*);
390 protected:
391 // Write the data to the file.
392 void
393 do_write(Output_file*);
395 // Return the required alignment.
396 uint64_t
397 do_addralign() const
398 { return Output_data::default_alignment(); }
400 private:
401 // Write the data to the file with the right size and endianness.
402 template<int size, bool big_endian>
403 void
404 do_sized_write(Output_file*);
406 const Layout* layout_;
407 const Layout::Segment_list* segment_list_;
408 const Layout::Section_list* section_list_;
409 const Layout::Section_list* unattached_section_list_;
410 const Stringpool* secnamepool_;
411 const Output_section* shstrtab_section_;
414 // Output the segment headers.
416 class Output_segment_headers : public Output_data
418 public:
419 Output_segment_headers(const Layout::Segment_list& segment_list);
421 protected:
422 // Write the data to the file.
423 void
424 do_write(Output_file*);
426 // Return the required alignment.
427 uint64_t
428 do_addralign() const
429 { return Output_data::default_alignment(); }
431 private:
432 // Write the data to the file with the right size and endianness.
433 template<int size, bool big_endian>
434 void
435 do_sized_write(Output_file*);
437 const Layout::Segment_list& segment_list_;
440 // Output the ELF file header.
442 class Output_file_header : public Output_data
444 public:
445 Output_file_header(const Target*,
446 const Symbol_table*,
447 const Output_segment_headers*,
448 const char* entry);
450 // Add information about the section headers. We lay out the ELF
451 // file header before we create the section headers.
452 void set_section_info(const Output_section_headers*,
453 const Output_section* shstrtab);
455 protected:
456 // Write the data to the file.
457 void
458 do_write(Output_file*);
460 // Return the required alignment.
461 uint64_t
462 do_addralign() const
463 { return Output_data::default_alignment(); }
465 private:
466 // Write the data to the file with the right size and endianness.
467 template<int size, bool big_endian>
468 void
469 do_sized_write(Output_file*);
471 // Return the value to use for the entry address.
472 template<int size>
473 typename elfcpp::Elf_types<size>::Elf_Addr
474 entry();
476 const Target* target_;
477 const Symbol_table* symtab_;
478 const Output_segment_headers* segment_header_;
479 const Output_section_headers* section_header_;
480 const Output_section* shstrtab_;
481 const char* entry_;
484 // Output sections are mainly comprised of input sections. However,
485 // there are cases where we have data to write out which is not in an
486 // input section. Output_section_data is used in such cases. This is
487 // an abstract base class.
489 class Output_section_data : public Output_data
491 public:
492 Output_section_data(off_t data_size, uint64_t addralign)
493 : Output_data(), output_section_(NULL), addralign_(addralign)
494 { this->set_data_size(data_size); }
496 Output_section_data(uint64_t addralign)
497 : Output_data(), output_section_(NULL), addralign_(addralign)
500 // Return the output section.
501 const Output_section*
502 output_section() const
503 { return this->output_section_; }
505 // Record the output section.
506 void
507 set_output_section(Output_section* os);
509 // Add an input section, for SHF_MERGE sections. This returns true
510 // if the section was handled.
511 bool
512 add_input_section(Relobj* object, unsigned int shndx)
513 { return this->do_add_input_section(object, shndx); }
515 // Given an input OBJECT, an input section index SHNDX within that
516 // object, and an OFFSET relative to the start of that input
517 // section, return whether or not the corresponding offset within
518 // the output section is known. If this function returns true, it
519 // sets *POUTPUT to the output offset. The value -1 indicates that
520 // this input offset is being discarded.
521 bool
522 output_offset(const Relobj* object, unsigned int shndx,
523 section_offset_type offset,
524 section_offset_type *poutput) const
525 { return this->do_output_offset(object, shndx, offset, poutput); }
527 // Return whether this is the merge section for the input section
528 // SHNDX in OBJECT. This should return true when output_offset
529 // would return true for some values of OFFSET.
530 bool
531 is_merge_section_for(const Relobj* object, unsigned int shndx) const
532 { return this->do_is_merge_section_for(object, shndx); }
534 // Write the contents to a buffer. This is used for sections which
535 // require postprocessing, such as compression.
536 void
537 write_to_buffer(unsigned char* buffer)
538 { this->do_write_to_buffer(buffer); }
540 // Print merge stats to stderr. This should only be called for
541 // SHF_MERGE sections.
542 void
543 print_merge_stats(const char* section_name)
544 { this->do_print_merge_stats(section_name); }
546 protected:
547 // The child class must implement do_write.
549 // The child class may implement specific adjustments to the output
550 // section.
551 virtual void
552 do_adjust_output_section(Output_section*)
555 // May be implemented by child class. Return true if the section
556 // was handled.
557 virtual bool
558 do_add_input_section(Relobj*, unsigned int)
559 { gold_unreachable(); }
561 // The child class may implement output_offset.
562 virtual bool
563 do_output_offset(const Relobj*, unsigned int, section_offset_type,
564 section_offset_type*) const
565 { return false; }
567 // The child class may implement is_merge_section_for.
568 virtual bool
569 do_is_merge_section_for(const Relobj*, unsigned int) const
570 { return false; }
572 // The child class may implement write_to_buffer. Most child
573 // classes can not appear in a compressed section, and they do not
574 // implement this.
575 virtual void
576 do_write_to_buffer(unsigned char*)
577 { gold_unreachable(); }
579 // Print merge statistics.
580 virtual void
581 do_print_merge_stats(const char*)
582 { gold_unreachable(); }
584 // Return the required alignment.
585 uint64_t
586 do_addralign() const
587 { return this->addralign_; }
589 // Return the output section.
590 Output_section*
591 do_output_section()
592 { return this->output_section_; }
594 // Return the section index of the output section.
595 unsigned int
596 do_out_shndx() const;
598 // Set the alignment.
599 void
600 set_addralign(uint64_t addralign);
602 private:
603 // The output section for this section.
604 Output_section* output_section_;
605 // The required alignment.
606 uint64_t addralign_;
609 // Some Output_section_data classes build up their data step by step,
610 // rather than all at once. This class provides an interface for
611 // them.
613 class Output_section_data_build : public Output_section_data
615 public:
616 Output_section_data_build(uint64_t addralign)
617 : Output_section_data(addralign)
620 // Get the current data size.
621 off_t
622 current_data_size() const
623 { return this->current_data_size_for_child(); }
625 // Set the current data size.
626 void
627 set_current_data_size(off_t data_size)
628 { this->set_current_data_size_for_child(data_size); }
630 protected:
631 // Set the final data size.
632 virtual void
633 set_final_data_size()
634 { this->set_data_size(this->current_data_size_for_child()); }
637 // A simple case of Output_data in which we have constant data to
638 // output.
640 class Output_data_const : public Output_section_data
642 public:
643 Output_data_const(const std::string& data, uint64_t addralign)
644 : Output_section_data(data.size(), addralign), data_(data)
647 Output_data_const(const char* p, off_t len, uint64_t addralign)
648 : Output_section_data(len, addralign), data_(p, len)
651 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
652 : Output_section_data(len, addralign),
653 data_(reinterpret_cast<const char*>(p), len)
656 protected:
657 // Write the data to the output file.
658 void
659 do_write(Output_file*);
661 // Write the data to a buffer.
662 void
663 do_write_to_buffer(unsigned char* buffer)
664 { memcpy(buffer, this->data_.data(), this->data_.size()); }
666 private:
667 std::string data_;
670 // Another version of Output_data with constant data, in which the
671 // buffer is allocated by the caller.
673 class Output_data_const_buffer : public Output_section_data
675 public:
676 Output_data_const_buffer(const unsigned char* p, off_t len,
677 uint64_t addralign)
678 : Output_section_data(len, addralign), p_(p)
681 protected:
682 // Write the data the output file.
683 void
684 do_write(Output_file*);
686 // Write the data to a buffer.
687 void
688 do_write_to_buffer(unsigned char* buffer)
689 { memcpy(buffer, this->p_, this->data_size()); }
691 private:
692 const unsigned char* p_;
695 // A place holder for a fixed amount of data written out via some
696 // other mechanism.
698 class Output_data_fixed_space : public Output_section_data
700 public:
701 Output_data_fixed_space(off_t data_size, uint64_t addralign)
702 : Output_section_data(data_size, addralign)
705 protected:
706 // Write out the data--the actual data must be written out
707 // elsewhere.
708 void
709 do_write(Output_file*)
713 // A place holder for variable sized data written out via some other
714 // mechanism.
716 class Output_data_space : public Output_section_data_build
718 public:
719 explicit Output_data_space(uint64_t addralign)
720 : Output_section_data_build(addralign)
723 // Set the alignment.
724 void
725 set_space_alignment(uint64_t align)
726 { this->set_addralign(align); }
728 protected:
729 // Write out the data--the actual data must be written out
730 // elsewhere.
731 void
732 do_write(Output_file*)
736 // A string table which goes into an output section.
738 class Output_data_strtab : public Output_section_data
740 public:
741 Output_data_strtab(Stringpool* strtab)
742 : Output_section_data(1), strtab_(strtab)
745 protected:
746 // This is called to set the address and file offset. Here we make
747 // sure that the Stringpool is finalized.
748 void
749 set_final_data_size();
751 // Write out the data.
752 void
753 do_write(Output_file*);
755 // Write the data to a buffer.
756 void
757 do_write_to_buffer(unsigned char* buffer)
758 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
760 private:
761 Stringpool* strtab_;
764 // This POD class is used to represent a single reloc in the output
765 // file. This could be a private class within Output_data_reloc, but
766 // the templatization is complex enough that I broke it out into a
767 // separate class. The class is templatized on either elfcpp::SHT_REL
768 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
769 // relocation or an ordinary relocation.
771 // A relocation can be against a global symbol, a local symbol, a
772 // local section symbol, an output section, or the undefined symbol at
773 // index 0. We represent the latter by using a NULL global symbol.
775 template<int sh_type, bool dynamic, int size, bool big_endian>
776 class Output_reloc;
778 template<bool dynamic, int size, bool big_endian>
779 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
781 public:
782 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
783 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
785 // An uninitialized entry. We need this because we want to put
786 // instances of this class into an STL container.
787 Output_reloc()
788 : local_sym_index_(INVALID_CODE)
791 // We have a bunch of different constructors. They come in pairs
792 // depending on how the address of the relocation is specified. It
793 // can either be an offset in an Output_data or an offset in an
794 // input section.
796 // A reloc against a global symbol.
798 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
799 Address address, bool is_relative);
801 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
802 unsigned int shndx, Address address, bool is_relative);
804 // A reloc against a local symbol or local section symbol.
806 Output_reloc(Sized_relobj<size, big_endian>* relobj,
807 unsigned int local_sym_index, unsigned int type,
808 Output_data* od, Address address, bool is_relative,
809 bool is_section_symbol);
811 Output_reloc(Sized_relobj<size, big_endian>* relobj,
812 unsigned int local_sym_index, unsigned int type,
813 unsigned int shndx, Address address, bool is_relative,
814 bool is_section_symbol);
816 // A reloc against the STT_SECTION symbol of an output section.
818 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
819 Address address);
821 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
822 unsigned int shndx, Address address);
824 // Return TRUE if this is a RELATIVE relocation.
825 bool
826 is_relative() const
827 { return this->is_relative_; }
829 // Return whether this is against a local section symbol.
830 bool
831 is_local_section_symbol() const
833 return (this->local_sym_index_ != GSYM_CODE
834 && this->local_sym_index_ != SECTION_CODE
835 && this->local_sym_index_ != INVALID_CODE
836 && this->is_section_symbol_);
839 // For a local section symbol, return the offset of the input
840 // section within the output section. ADDEND is the addend being
841 // applied to the input section.
842 section_offset_type
843 local_section_offset(Addend addend) const;
845 // Get the value of the symbol referred to by a Rel relocation when
846 // we are adding the given ADDEND.
847 Address
848 symbol_value(Addend addend) const;
850 // Write the reloc entry to an output view.
851 void
852 write(unsigned char* pov) const;
854 // Write the offset and info fields to Write_rel.
855 template<typename Write_rel>
856 void write_rel(Write_rel*) const;
858 private:
859 // Record that we need a dynamic symbol index.
860 void
861 set_needs_dynsym_index();
863 // Return the symbol index.
864 unsigned int
865 get_symbol_index() const;
867 // Codes for local_sym_index_.
868 enum
870 // Global symbol.
871 GSYM_CODE = -1U,
872 // Output section.
873 SECTION_CODE = -2U,
874 // Invalid uninitialized entry.
875 INVALID_CODE = -3U
878 union
880 // For a local symbol or local section symbol
881 // (this->local_sym_index_ >= 0), the object. We will never
882 // generate a relocation against a local symbol in a dynamic
883 // object; that doesn't make sense. And our callers will always
884 // be templatized, so we use Sized_relobj here.
885 Sized_relobj<size, big_endian>* relobj;
886 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
887 // symbol. If this is NULL, it indicates a relocation against the
888 // undefined 0 symbol.
889 Symbol* gsym;
890 // For a relocation against an output section
891 // (this->local_sym_index_ == SECTION_CODE), the output section.
892 Output_section* os;
893 } u1_;
894 union
896 // If this->shndx_ is not INVALID CODE, the object which holds the
897 // input section being used to specify the reloc address.
898 Relobj* relobj;
899 // If this->shndx_ is INVALID_CODE, the output data being used to
900 // specify the reloc address. This may be NULL if the reloc
901 // address is absolute.
902 Output_data* od;
903 } u2_;
904 // The address offset within the input section or the Output_data.
905 Address address_;
906 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
907 // relocation against an output section, or INVALID_CODE for an
908 // uninitialized value. Otherwise, for a local symbol
909 // (this->is_section_symbol_ is false), the local symbol index. For
910 // a local section symbol (this->is_section_symbol_ is true), the
911 // section index in the input file.
912 unsigned int local_sym_index_;
913 // The reloc type--a processor specific code.
914 unsigned int type_ : 30;
915 // True if the relocation is a RELATIVE relocation.
916 bool is_relative_ : 1;
917 // True if the relocation is against a section symbol.
918 bool is_section_symbol_ : 1;
919 // If the reloc address is an input section in an object, the
920 // section index. This is INVALID_CODE if the reloc address is
921 // specified in some other way.
922 unsigned int shndx_;
925 // The SHT_RELA version of Output_reloc<>. This is just derived from
926 // the SHT_REL version of Output_reloc, but it adds an addend.
928 template<bool dynamic, int size, bool big_endian>
929 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
931 public:
932 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
933 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
935 // An uninitialized entry.
936 Output_reloc()
937 : rel_()
940 // A reloc against a global symbol.
942 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
943 Address address, Addend addend, bool is_relative)
944 : rel_(gsym, type, od, address, is_relative), addend_(addend)
947 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
948 unsigned int shndx, Address address, Addend addend,
949 bool is_relative)
950 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
953 // A reloc against a local symbol.
955 Output_reloc(Sized_relobj<size, big_endian>* relobj,
956 unsigned int local_sym_index, unsigned int type,
957 Output_data* od, Address address,
958 Addend addend, bool is_relative, bool is_section_symbol)
959 : rel_(relobj, local_sym_index, type, od, address, is_relative,
960 is_section_symbol),
961 addend_(addend)
964 Output_reloc(Sized_relobj<size, big_endian>* relobj,
965 unsigned int local_sym_index, unsigned int type,
966 unsigned int shndx, Address address,
967 Addend addend, bool is_relative, bool is_section_symbol)
968 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
969 is_section_symbol),
970 addend_(addend)
973 // A reloc against the STT_SECTION symbol of an output section.
975 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
976 Address address, Addend addend)
977 : rel_(os, type, od, address), addend_(addend)
980 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
981 unsigned int shndx, Address address, Addend addend)
982 : rel_(os, type, relobj, shndx, address), addend_(addend)
985 // Write the reloc entry to an output view.
986 void
987 write(unsigned char* pov) const;
989 private:
990 // The basic reloc.
991 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
992 // The addend.
993 Addend addend_;
996 // Output_data_reloc is used to manage a section containing relocs.
997 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
998 // indicates whether this is a dynamic relocation or a normal
999 // relocation. Output_data_reloc_base is a base class.
1000 // Output_data_reloc is the real class, which we specialize based on
1001 // the reloc type.
1003 template<int sh_type, bool dynamic, int size, bool big_endian>
1004 class Output_data_reloc_base : public Output_section_data_build
1006 public:
1007 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1008 typedef typename Output_reloc_type::Address Address;
1009 static const int reloc_size =
1010 Reloc_types<sh_type, size, big_endian>::reloc_size;
1012 // Construct the section.
1013 Output_data_reloc_base()
1014 : Output_section_data_build(Output_data::default_alignment_for_size(size))
1017 protected:
1018 // Write out the data.
1019 void
1020 do_write(Output_file*);
1022 // Set the entry size and the link.
1023 void
1024 do_adjust_output_section(Output_section *os);
1026 // Add a relocation entry.
1027 void
1028 add(Output_data *od, const Output_reloc_type& reloc)
1030 this->relocs_.push_back(reloc);
1031 this->set_current_data_size(this->relocs_.size() * reloc_size);
1032 od->add_dynamic_reloc();
1035 private:
1036 typedef std::vector<Output_reloc_type> Relocs;
1038 Relocs relocs_;
1041 // The class which callers actually create.
1043 template<int sh_type, bool dynamic, int size, bool big_endian>
1044 class Output_data_reloc;
1046 // The SHT_REL version of Output_data_reloc.
1048 template<bool dynamic, int size, bool big_endian>
1049 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1050 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1052 private:
1053 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1054 big_endian> Base;
1056 public:
1057 typedef typename Base::Output_reloc_type Output_reloc_type;
1058 typedef typename Output_reloc_type::Address Address;
1060 Output_data_reloc()
1061 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1064 // Add a reloc against a global symbol.
1066 void
1067 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1068 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1070 void
1071 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1072 unsigned int shndx, Address address)
1073 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1074 false)); }
1076 // These are to simplify the Copy_relocs class.
1078 void
1079 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1080 Address addend)
1082 gold_assert(addend == 0);
1083 this->add_global(gsym, type, od, address);
1086 void
1087 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1088 unsigned int shndx, Address address, Address addend)
1090 gold_assert(addend == 0);
1091 this->add_global(gsym, type, od, relobj, shndx, address);
1094 // Add a RELATIVE reloc against a global symbol. The final relocation
1095 // will not reference the symbol.
1097 void
1098 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1099 Address address)
1100 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1102 void
1103 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1104 Relobj* relobj, unsigned int shndx, Address address)
1106 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1107 true));
1110 // Add a reloc against a local symbol.
1112 void
1113 add_local(Sized_relobj<size, big_endian>* relobj,
1114 unsigned int local_sym_index, unsigned int type,
1115 Output_data* od, Address address)
1117 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1118 address, false, false));
1121 void
1122 add_local(Sized_relobj<size, big_endian>* relobj,
1123 unsigned int local_sym_index, unsigned int type,
1124 Output_data* od, unsigned int shndx, Address address)
1126 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1127 address, false, false));
1130 // Add a RELATIVE reloc against a local symbol.
1132 void
1133 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1134 unsigned int local_sym_index, unsigned int type,
1135 Output_data* od, Address address)
1137 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1138 address, true, false));
1141 void
1142 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1143 unsigned int local_sym_index, unsigned int type,
1144 Output_data* od, unsigned int shndx, Address address)
1146 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1147 address, true, false));
1150 // Add a reloc against a local section symbol. This will be
1151 // converted into a reloc against the STT_SECTION symbol of the
1152 // output section.
1154 void
1155 add_local_section(Sized_relobj<size, big_endian>* relobj,
1156 unsigned int input_shndx, unsigned int type,
1157 Output_data* od, Address address)
1159 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1160 address, false, true));
1163 void
1164 add_local_section(Sized_relobj<size, big_endian>* relobj,
1165 unsigned int input_shndx, unsigned int type,
1166 Output_data* od, unsigned int shndx, Address address)
1168 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1169 address, false, true));
1172 // A reloc against the STT_SECTION symbol of an output section.
1173 // OS is the Output_section that the relocation refers to; OD is
1174 // the Output_data object being relocated.
1176 void
1177 add_output_section(Output_section* os, unsigned int type,
1178 Output_data* od, Address address)
1179 { this->add(od, Output_reloc_type(os, type, od, address)); }
1181 void
1182 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1183 Relobj* relobj, unsigned int shndx, Address address)
1184 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1187 // The SHT_RELA version of Output_data_reloc.
1189 template<bool dynamic, int size, bool big_endian>
1190 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1191 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1193 private:
1194 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1195 big_endian> Base;
1197 public:
1198 typedef typename Base::Output_reloc_type Output_reloc_type;
1199 typedef typename Output_reloc_type::Address Address;
1200 typedef typename Output_reloc_type::Addend Addend;
1202 Output_data_reloc()
1203 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1206 // Add a reloc against a global symbol.
1208 void
1209 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1210 Address address, Addend addend)
1211 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1212 false)); }
1214 void
1215 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1216 unsigned int shndx, Address address,
1217 Addend addend)
1218 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1219 addend, false)); }
1221 // Add a RELATIVE reloc against a global symbol. The final output
1222 // relocation will not reference the symbol, but we must keep the symbol
1223 // information long enough to set the addend of the relocation correctly
1224 // when it is written.
1226 void
1227 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1228 Address address, Addend addend)
1229 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1231 void
1232 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1233 Relobj* relobj, unsigned int shndx, Address address,
1234 Addend addend)
1235 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1236 addend, true)); }
1238 // Add a reloc against a local symbol.
1240 void
1241 add_local(Sized_relobj<size, big_endian>* relobj,
1242 unsigned int local_sym_index, unsigned int type,
1243 Output_data* od, Address address, Addend addend)
1245 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1246 addend, false, false));
1249 void
1250 add_local(Sized_relobj<size, big_endian>* relobj,
1251 unsigned int local_sym_index, unsigned int type,
1252 Output_data* od, unsigned int shndx, Address address,
1253 Addend addend)
1255 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1256 address, addend, false, false));
1259 // Add a RELATIVE reloc against a local symbol.
1261 void
1262 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1263 unsigned int local_sym_index, unsigned int type,
1264 Output_data* od, Address address, Addend addend)
1266 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1267 addend, true, false));
1270 void
1271 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1272 unsigned int local_sym_index, unsigned int type,
1273 Output_data* od, unsigned int shndx, Address address,
1274 Addend addend)
1276 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1277 address, addend, true, false));
1280 // Add a reloc against a local section symbol. This will be
1281 // converted into a reloc against the STT_SECTION symbol of the
1282 // output section.
1284 void
1285 add_local_section(Sized_relobj<size, big_endian>* relobj,
1286 unsigned int input_shndx, unsigned int type,
1287 Output_data* od, Address address, Addend addend)
1289 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1290 addend, false, true));
1293 void
1294 add_local_section(Sized_relobj<size, big_endian>* relobj,
1295 unsigned int input_shndx, unsigned int type,
1296 Output_data* od, unsigned int shndx, Address address,
1297 Addend addend)
1299 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1300 address, addend, false, true));
1303 // A reloc against the STT_SECTION symbol of an output section.
1305 void
1306 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1307 Address address, Addend addend)
1308 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1310 void
1311 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1312 unsigned int shndx, Address address, Addend addend)
1313 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1314 addend)); }
1317 // Output_relocatable_relocs represents a relocation section in a
1318 // relocatable link. The actual data is written out in the target
1319 // hook relocate_for_relocatable. This just saves space for it.
1321 template<int sh_type, int size, bool big_endian>
1322 class Output_relocatable_relocs : public Output_section_data
1324 public:
1325 Output_relocatable_relocs(Relocatable_relocs* rr)
1326 : Output_section_data(Output_data::default_alignment_for_size(size)),
1327 rr_(rr)
1330 void
1331 set_final_data_size();
1333 // Write out the data. There is nothing to do here.
1334 void
1335 do_write(Output_file*)
1338 private:
1339 // The relocs associated with this input section.
1340 Relocatable_relocs* rr_;
1343 // Handle a GROUP section.
1345 template<int size, bool big_endian>
1346 class Output_data_group : public Output_section_data
1348 public:
1349 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1350 section_size_type entry_count,
1351 const elfcpp::Elf_Word* contents);
1353 void
1354 do_write(Output_file*);
1356 private:
1357 // The input object.
1358 Sized_relobj<size, big_endian>* relobj_;
1359 // The group flag word.
1360 elfcpp::Elf_Word flags_;
1361 // The section indexes of the input sections in this group.
1362 std::vector<unsigned int> input_sections_;
1365 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1366 // for one symbol--either a global symbol or a local symbol in an
1367 // object. The target specific code adds entries to the GOT as
1368 // needed.
1370 template<int size, bool big_endian>
1371 class Output_data_got : public Output_section_data_build
1373 public:
1374 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1375 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1376 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1378 Output_data_got()
1379 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1380 entries_()
1383 // Add an entry for a global symbol to the GOT. Return true if this
1384 // is a new GOT entry, false if the symbol was already in the GOT.
1385 bool
1386 add_global(Symbol* gsym, unsigned int got_type);
1388 // Add an entry for a global symbol to the GOT, and add a dynamic
1389 // relocation of type R_TYPE for the GOT entry.
1390 void
1391 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1392 Rel_dyn* rel_dyn, unsigned int r_type);
1394 void
1395 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1396 Rela_dyn* rela_dyn, unsigned int r_type);
1398 // Add a pair of entries for a global symbol to the GOT, and add
1399 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1400 void
1401 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1402 Rel_dyn* rel_dyn, unsigned int r_type_1,
1403 unsigned int r_type_2);
1405 void
1406 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1407 Rela_dyn* rela_dyn, unsigned int r_type_1,
1408 unsigned int r_type_2);
1410 // Add an entry for a local symbol to the GOT. This returns true if
1411 // this is a new GOT entry, false if the symbol already has a GOT
1412 // entry.
1413 bool
1414 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1415 unsigned int got_type);
1417 // Add an entry for a local symbol to the GOT, and add a dynamic
1418 // relocation of type R_TYPE for the GOT entry.
1419 void
1420 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1421 unsigned int sym_index, unsigned int got_type,
1422 Rel_dyn* rel_dyn, unsigned int r_type);
1424 void
1425 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1426 unsigned int sym_index, unsigned int got_type,
1427 Rela_dyn* rela_dyn, unsigned int r_type);
1429 // Add a pair of entries for a local symbol to the GOT, and add
1430 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1431 void
1432 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1433 unsigned int sym_index, unsigned int shndx,
1434 unsigned int got_type, Rel_dyn* rel_dyn,
1435 unsigned int r_type_1, unsigned int r_type_2);
1437 void
1438 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1439 unsigned int sym_index, unsigned int shndx,
1440 unsigned int got_type, Rela_dyn* rela_dyn,
1441 unsigned int r_type_1, unsigned int r_type_2);
1443 // Add a constant to the GOT. This returns the offset of the new
1444 // entry from the start of the GOT.
1445 unsigned int
1446 add_constant(Valtype constant)
1448 this->entries_.push_back(Got_entry(constant));
1449 this->set_got_size();
1450 return this->last_got_offset();
1453 protected:
1454 // Write out the GOT table.
1455 void
1456 do_write(Output_file*);
1458 private:
1459 // This POD class holds a single GOT entry.
1460 class Got_entry
1462 public:
1463 // Create a zero entry.
1464 Got_entry()
1465 : local_sym_index_(CONSTANT_CODE)
1466 { this->u_.constant = 0; }
1468 // Create a global symbol entry.
1469 explicit Got_entry(Symbol* gsym)
1470 : local_sym_index_(GSYM_CODE)
1471 { this->u_.gsym = gsym; }
1473 // Create a local symbol entry.
1474 Got_entry(Sized_relobj<size, big_endian>* object,
1475 unsigned int local_sym_index)
1476 : local_sym_index_(local_sym_index)
1478 gold_assert(local_sym_index != GSYM_CODE
1479 && local_sym_index != CONSTANT_CODE);
1480 this->u_.object = object;
1483 // Create a constant entry. The constant is a host value--it will
1484 // be swapped, if necessary, when it is written out.
1485 explicit Got_entry(Valtype constant)
1486 : local_sym_index_(CONSTANT_CODE)
1487 { this->u_.constant = constant; }
1489 // Write the GOT entry to an output view.
1490 void
1491 write(unsigned char* pov) const;
1493 private:
1494 enum
1496 GSYM_CODE = -1U,
1497 CONSTANT_CODE = -2U
1500 union
1502 // For a local symbol, the object.
1503 Sized_relobj<size, big_endian>* object;
1504 // For a global symbol, the symbol.
1505 Symbol* gsym;
1506 // For a constant, the constant.
1507 Valtype constant;
1508 } u_;
1509 // For a local symbol, the local symbol index. This is GSYM_CODE
1510 // for a global symbol, or CONSTANT_CODE for a constant.
1511 unsigned int local_sym_index_;
1514 typedef std::vector<Got_entry> Got_entries;
1516 // Return the offset into the GOT of GOT entry I.
1517 unsigned int
1518 got_offset(unsigned int i) const
1519 { return i * (size / 8); }
1521 // Return the offset into the GOT of the last entry added.
1522 unsigned int
1523 last_got_offset() const
1524 { return this->got_offset(this->entries_.size() - 1); }
1526 // Set the size of the section.
1527 void
1528 set_got_size()
1529 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1531 // The list of GOT entries.
1532 Got_entries entries_;
1535 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1536 // section.
1538 class Output_data_dynamic : public Output_section_data
1540 public:
1541 Output_data_dynamic(Stringpool* pool)
1542 : Output_section_data(Output_data::default_alignment()),
1543 entries_(), pool_(pool)
1546 // Add a new dynamic entry with a fixed numeric value.
1547 void
1548 add_constant(elfcpp::DT tag, unsigned int val)
1549 { this->add_entry(Dynamic_entry(tag, val)); }
1551 // Add a new dynamic entry with the address of output data.
1552 void
1553 add_section_address(elfcpp::DT tag, const Output_data* od)
1554 { this->add_entry(Dynamic_entry(tag, od, false)); }
1556 // Add a new dynamic entry with the address of output data
1557 // plus a constant offset.
1558 void
1559 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1560 unsigned int offset)
1561 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1563 // Add a new dynamic entry with the size of output data.
1564 void
1565 add_section_size(elfcpp::DT tag, const Output_data* od)
1566 { this->add_entry(Dynamic_entry(tag, od, true)); }
1568 // Add a new dynamic entry with the address of a symbol.
1569 void
1570 add_symbol(elfcpp::DT tag, const Symbol* sym)
1571 { this->add_entry(Dynamic_entry(tag, sym)); }
1573 // Add a new dynamic entry with a string.
1574 void
1575 add_string(elfcpp::DT tag, const char* str)
1576 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1578 void
1579 add_string(elfcpp::DT tag, const std::string& str)
1580 { this->add_string(tag, str.c_str()); }
1582 protected:
1583 // Adjust the output section to set the entry size.
1584 void
1585 do_adjust_output_section(Output_section*);
1587 // Set the final data size.
1588 void
1589 set_final_data_size();
1591 // Write out the dynamic entries.
1592 void
1593 do_write(Output_file*);
1595 private:
1596 // This POD class holds a single dynamic entry.
1597 class Dynamic_entry
1599 public:
1600 // Create an entry with a fixed numeric value.
1601 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1602 : tag_(tag), offset_(DYNAMIC_NUMBER)
1603 { this->u_.val = val; }
1605 // Create an entry with the size or address of a section.
1606 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1607 : tag_(tag),
1608 offset_(section_size
1609 ? DYNAMIC_SECTION_SIZE
1610 : DYNAMIC_SECTION_ADDRESS)
1611 { this->u_.od = od; }
1613 // Create an entry with the address of a section plus a constant offset.
1614 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1615 : tag_(tag),
1616 offset_(offset)
1617 { this->u_.od = od; }
1619 // Create an entry with the address of a symbol.
1620 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1621 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1622 { this->u_.sym = sym; }
1624 // Create an entry with a string.
1625 Dynamic_entry(elfcpp::DT tag, const char* str)
1626 : tag_(tag), offset_(DYNAMIC_STRING)
1627 { this->u_.str = str; }
1629 // Write the dynamic entry to an output view.
1630 template<int size, bool big_endian>
1631 void
1632 write(unsigned char* pov, const Stringpool*) const;
1634 private:
1635 // Classification is encoded in the OFFSET field.
1636 enum Classification
1638 // Section address.
1639 DYNAMIC_SECTION_ADDRESS = 0,
1640 // Number.
1641 DYNAMIC_NUMBER = -1U,
1642 // Section size.
1643 DYNAMIC_SECTION_SIZE = -2U,
1644 // Symbol adress.
1645 DYNAMIC_SYMBOL = -3U,
1646 // String.
1647 DYNAMIC_STRING = -4U
1648 // Any other value indicates a section address plus OFFSET.
1651 union
1653 // For DYNAMIC_NUMBER.
1654 unsigned int val;
1655 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1656 const Output_data* od;
1657 // For DYNAMIC_SYMBOL.
1658 const Symbol* sym;
1659 // For DYNAMIC_STRING.
1660 const char* str;
1661 } u_;
1662 // The dynamic tag.
1663 elfcpp::DT tag_;
1664 // The type of entry (Classification) or offset within a section.
1665 unsigned int offset_;
1668 // Add an entry to the list.
1669 void
1670 add_entry(const Dynamic_entry& entry)
1671 { this->entries_.push_back(entry); }
1673 // Sized version of write function.
1674 template<int size, bool big_endian>
1675 void
1676 sized_write(Output_file* of);
1678 // The type of the list of entries.
1679 typedef std::vector<Dynamic_entry> Dynamic_entries;
1681 // The entries.
1682 Dynamic_entries entries_;
1683 // The pool used for strings.
1684 Stringpool* pool_;
1687 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1688 // which may be required if the object file has more than
1689 // SHN_LORESERVE sections.
1691 class Output_symtab_xindex : public Output_section_data
1693 public:
1694 Output_symtab_xindex(size_t symcount)
1695 : Output_section_data(symcount * 4, 4),
1696 entries_()
1699 // Add an entry: symbol number SYMNDX has section SHNDX.
1700 void
1701 add(unsigned int symndx, unsigned int shndx)
1702 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
1704 protected:
1705 void
1706 do_write(Output_file*);
1708 private:
1709 template<bool big_endian>
1710 void
1711 endian_do_write(unsigned char*);
1713 // It is likely that most symbols will not require entries. Rather
1714 // than keep a vector for all symbols, we keep pairs of symbol index
1715 // and section index.
1716 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
1718 // The entries we need.
1719 Xindex_entries entries_;
1722 // An output section. We don't expect to have too many output
1723 // sections, so we don't bother to do a template on the size.
1725 class Output_section : public Output_data
1727 public:
1728 // Create an output section, giving the name, type, and flags.
1729 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1730 virtual ~Output_section();
1732 // Add a new input section SHNDX, named NAME, with header SHDR, from
1733 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1734 // which applies to this section, or 0 if none, or -1U if more than
1735 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1736 // in a linker script; in that case we need to keep track of input
1737 // sections associated with an output section. Return the offset
1738 // within the output section.
1739 template<int size, bool big_endian>
1740 off_t
1741 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1742 const char *name,
1743 const elfcpp::Shdr<size, big_endian>& shdr,
1744 unsigned int reloc_shndx, bool have_sections_script);
1746 // Add generated data POSD to this output section.
1747 void
1748 add_output_section_data(Output_section_data* posd);
1750 // Return the section name.
1751 const char*
1752 name() const
1753 { return this->name_; }
1755 // Return the section type.
1756 elfcpp::Elf_Word
1757 type() const
1758 { return this->type_; }
1760 // Return the section flags.
1761 elfcpp::Elf_Xword
1762 flags() const
1763 { return this->flags_; }
1765 // Set the section flags. This may only be used with the Layout
1766 // code when it is prepared to move the section to a different
1767 // segment.
1768 void
1769 set_flags(elfcpp::Elf_Xword flags)
1770 { this->flags_ = flags; }
1772 // Update the output section flags based on input section flags.
1773 void
1774 update_flags_for_input_section(elfcpp::Elf_Xword flags)
1776 this->flags_ |= (flags
1777 & (elfcpp::SHF_WRITE
1778 | elfcpp::SHF_ALLOC
1779 | elfcpp::SHF_EXECINSTR));
1782 // Return the entsize field.
1783 uint64_t
1784 entsize() const
1785 { return this->entsize_; }
1787 // Set the entsize field.
1788 void
1789 set_entsize(uint64_t v);
1791 // Set the load address.
1792 void
1793 set_load_address(uint64_t load_address)
1795 this->load_address_ = load_address;
1796 this->has_load_address_ = true;
1799 // Set the link field to the output section index of a section.
1800 void
1801 set_link_section(const Output_data* od)
1803 gold_assert(this->link_ == 0
1804 && !this->should_link_to_symtab_
1805 && !this->should_link_to_dynsym_);
1806 this->link_section_ = od;
1809 // Set the link field to a constant.
1810 void
1811 set_link(unsigned int v)
1813 gold_assert(this->link_section_ == NULL
1814 && !this->should_link_to_symtab_
1815 && !this->should_link_to_dynsym_);
1816 this->link_ = v;
1819 // Record that this section should link to the normal symbol table.
1820 void
1821 set_should_link_to_symtab()
1823 gold_assert(this->link_section_ == NULL
1824 && this->link_ == 0
1825 && !this->should_link_to_dynsym_);
1826 this->should_link_to_symtab_ = true;
1829 // Record that this section should link to the dynamic symbol table.
1830 void
1831 set_should_link_to_dynsym()
1833 gold_assert(this->link_section_ == NULL
1834 && this->link_ == 0
1835 && !this->should_link_to_symtab_);
1836 this->should_link_to_dynsym_ = true;
1839 // Return the info field.
1840 unsigned int
1841 info() const
1843 gold_assert(this->info_section_ == NULL
1844 && this->info_symndx_ == NULL);
1845 return this->info_;
1848 // Set the info field to the output section index of a section.
1849 void
1850 set_info_section(const Output_section* os)
1852 gold_assert((this->info_section_ == NULL
1853 || (this->info_section_ == os
1854 && this->info_uses_section_index_))
1855 && this->info_symndx_ == NULL
1856 && this->info_ == 0);
1857 this->info_section_ = os;
1858 this->info_uses_section_index_= true;
1861 // Set the info field to the symbol table index of a symbol.
1862 void
1863 set_info_symndx(const Symbol* sym)
1865 gold_assert(this->info_section_ == NULL
1866 && (this->info_symndx_ == NULL
1867 || this->info_symndx_ == sym)
1868 && this->info_ == 0);
1869 this->info_symndx_ = sym;
1872 // Set the info field to the symbol table index of a section symbol.
1873 void
1874 set_info_section_symndx(const Output_section* os)
1876 gold_assert((this->info_section_ == NULL
1877 || (this->info_section_ == os
1878 && !this->info_uses_section_index_))
1879 && this->info_symndx_ == NULL
1880 && this->info_ == 0);
1881 this->info_section_ = os;
1882 this->info_uses_section_index_ = false;
1885 // Set the info field to a constant.
1886 void
1887 set_info(unsigned int v)
1889 gold_assert(this->info_section_ == NULL
1890 && this->info_symndx_ == NULL
1891 && (this->info_ == 0
1892 || this->info_ == v));
1893 this->info_ = v;
1896 // Set the addralign field.
1897 void
1898 set_addralign(uint64_t v)
1899 { this->addralign_ = v; }
1901 // Whether the output section index has been set.
1902 bool
1903 has_out_shndx() const
1904 { return this->out_shndx_ != -1U; }
1906 // Indicate that we need a symtab index.
1907 void
1908 set_needs_symtab_index()
1909 { this->needs_symtab_index_ = true; }
1911 // Return whether we need a symtab index.
1912 bool
1913 needs_symtab_index() const
1914 { return this->needs_symtab_index_; }
1916 // Get the symtab index.
1917 unsigned int
1918 symtab_index() const
1920 gold_assert(this->symtab_index_ != 0);
1921 return this->symtab_index_;
1924 // Set the symtab index.
1925 void
1926 set_symtab_index(unsigned int index)
1928 gold_assert(index != 0);
1929 this->symtab_index_ = index;
1932 // Indicate that we need a dynsym index.
1933 void
1934 set_needs_dynsym_index()
1935 { this->needs_dynsym_index_ = true; }
1937 // Return whether we need a dynsym index.
1938 bool
1939 needs_dynsym_index() const
1940 { return this->needs_dynsym_index_; }
1942 // Get the dynsym index.
1943 unsigned int
1944 dynsym_index() const
1946 gold_assert(this->dynsym_index_ != 0);
1947 return this->dynsym_index_;
1950 // Set the dynsym index.
1951 void
1952 set_dynsym_index(unsigned int index)
1954 gold_assert(index != 0);
1955 this->dynsym_index_ = index;
1958 // Return whether the input sections sections attachd to this output
1959 // section may require sorting. This is used to handle constructor
1960 // priorities compatibly with GNU ld.
1961 bool
1962 may_sort_attached_input_sections() const
1963 { return this->may_sort_attached_input_sections_; }
1965 // Record that the input sections attached to this output section
1966 // may require sorting.
1967 void
1968 set_may_sort_attached_input_sections()
1969 { this->may_sort_attached_input_sections_ = true; }
1971 // Return whether the input sections attached to this output section
1972 // require sorting. This is used to handle constructor priorities
1973 // compatibly with GNU ld.
1974 bool
1975 must_sort_attached_input_sections() const
1976 { return this->must_sort_attached_input_sections_; }
1978 // Record that the input sections attached to this output section
1979 // require sorting.
1980 void
1981 set_must_sort_attached_input_sections()
1982 { this->must_sort_attached_input_sections_ = true; }
1984 // Return whether this section should be written after all the input
1985 // sections are complete.
1986 bool
1987 after_input_sections() const
1988 { return this->after_input_sections_; }
1990 // Record that this section should be written after all the input
1991 // sections are complete.
1992 void
1993 set_after_input_sections()
1994 { this->after_input_sections_ = true; }
1996 // Return whether this section requires postprocessing after all
1997 // relocations have been applied.
1998 bool
1999 requires_postprocessing() const
2000 { return this->requires_postprocessing_; }
2002 // If a section requires postprocessing, return the buffer to use.
2003 unsigned char*
2004 postprocessing_buffer() const
2006 gold_assert(this->postprocessing_buffer_ != NULL);
2007 return this->postprocessing_buffer_;
2010 // If a section requires postprocessing, create the buffer to use.
2011 void
2012 create_postprocessing_buffer();
2014 // If a section requires postprocessing, this is the size of the
2015 // buffer to which relocations should be applied.
2016 off_t
2017 postprocessing_buffer_size() const
2018 { return this->current_data_size_for_child(); }
2020 // Modify the section name. This is only permitted for an
2021 // unallocated section, and only before the size has been finalized.
2022 // Otherwise the name will not get into Layout::namepool_.
2023 void
2024 set_name(const char* newname)
2026 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2027 gold_assert(!this->is_data_size_valid());
2028 this->name_ = newname;
2031 // Return whether the offset OFFSET in the input section SHNDX in
2032 // object OBJECT is being included in the link.
2033 bool
2034 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2035 off_t offset) const;
2037 // Return the offset within the output section of OFFSET relative to
2038 // the start of input section SHNDX in object OBJECT.
2039 section_offset_type
2040 output_offset(const Relobj* object, unsigned int shndx,
2041 section_offset_type offset) const;
2043 // Return the output virtual address of OFFSET relative to the start
2044 // of input section SHNDX in object OBJECT.
2045 uint64_t
2046 output_address(const Relobj* object, unsigned int shndx,
2047 off_t offset) const;
2049 // Return the output address of the start of the merged section for
2050 // input section SHNDX in object OBJECT. This is not necessarily
2051 // the offset corresponding to input offset 0 in the section, since
2052 // the section may be mapped arbitrarily.
2053 uint64_t
2054 starting_output_address(const Relobj* object, unsigned int shndx) const;
2056 // Record that this output section was found in the SECTIONS clause
2057 // of a linker script.
2058 void
2059 set_found_in_sections_clause()
2060 { this->found_in_sections_clause_ = true; }
2062 // Return whether this output section was found in the SECTIONS
2063 // clause of a linker script.
2064 bool
2065 found_in_sections_clause() const
2066 { return this->found_in_sections_clause_; }
2068 // Write the section header into *OPHDR.
2069 template<int size, bool big_endian>
2070 void
2071 write_header(const Layout*, const Stringpool*,
2072 elfcpp::Shdr_write<size, big_endian>*) const;
2074 // The next few calls are for linker script support.
2076 // Store the list of input sections for this Output_section into the
2077 // list passed in. This removes the input sections, leaving only
2078 // any Output_section_data elements. This returns the size of those
2079 // Output_section_data elements. ADDRESS is the address of this
2080 // output section. FILL is the fill value to use, in case there are
2081 // any spaces between the remaining Output_section_data elements.
2082 uint64_t
2083 get_input_sections(uint64_t address, const std::string& fill,
2084 std::list<std::pair<Relobj*, unsigned int > >*);
2086 // Add an input section from a script.
2087 void
2088 add_input_section_for_script(Relobj* object, unsigned int shndx,
2089 off_t data_size, uint64_t addralign);
2091 // Set the current size of the output section.
2092 void
2093 set_current_data_size(off_t size)
2094 { this->set_current_data_size_for_child(size); }
2096 // Get the current size of the output section.
2097 off_t
2098 current_data_size() const
2099 { return this->current_data_size_for_child(); }
2101 // End of linker script support.
2103 // Print merge statistics to stderr.
2104 void
2105 print_merge_stats();
2107 protected:
2108 // Return the output section--i.e., the object itself.
2109 Output_section*
2110 do_output_section()
2111 { return this; }
2113 // Return the section index in the output file.
2114 unsigned int
2115 do_out_shndx() const
2117 gold_assert(this->out_shndx_ != -1U);
2118 return this->out_shndx_;
2121 // Set the output section index.
2122 void
2123 do_set_out_shndx(unsigned int shndx)
2125 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2126 this->out_shndx_ = shndx;
2129 // Set the final data size of the Output_section. For a typical
2130 // Output_section, there is nothing to do, but if there are any
2131 // Output_section_data objects we need to set their final addresses
2132 // here.
2133 virtual void
2134 set_final_data_size();
2136 // Reset the address and file offset.
2137 void
2138 do_reset_address_and_file_offset();
2140 // Write the data to the file. For a typical Output_section, this
2141 // does nothing: the data is written out by calling Object::Relocate
2142 // on each input object. But if there are any Output_section_data
2143 // objects we do need to write them out here.
2144 virtual void
2145 do_write(Output_file*);
2147 // Return the address alignment--function required by parent class.
2148 uint64_t
2149 do_addralign() const
2150 { return this->addralign_; }
2152 // Return whether there is a load address.
2153 bool
2154 do_has_load_address() const
2155 { return this->has_load_address_; }
2157 // Return the load address.
2158 uint64_t
2159 do_load_address() const
2161 gold_assert(this->has_load_address_);
2162 return this->load_address_;
2165 // Return whether this is an Output_section.
2166 bool
2167 do_is_section() const
2168 { return true; }
2170 // Return whether this is a section of the specified type.
2171 bool
2172 do_is_section_type(elfcpp::Elf_Word type) const
2173 { return this->type_ == type; }
2175 // Return whether the specified section flag is set.
2176 bool
2177 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2178 { return (this->flags_ & flag) != 0; }
2180 // Set the TLS offset. Called only for SHT_TLS sections.
2181 void
2182 do_set_tls_offset(uint64_t tls_base);
2184 // Return the TLS offset, relative to the base of the TLS segment.
2185 // Valid only for SHT_TLS sections.
2186 uint64_t
2187 do_tls_offset() const
2188 { return this->tls_offset_; }
2190 // This may be implemented by a child class.
2191 virtual void
2192 do_finalize_name(Layout*)
2195 // Record that this section requires postprocessing after all
2196 // relocations have been applied. This is called by a child class.
2197 void
2198 set_requires_postprocessing()
2200 this->requires_postprocessing_ = true;
2201 this->after_input_sections_ = true;
2204 // Write all the data of an Output_section into the postprocessing
2205 // buffer.
2206 void
2207 write_to_postprocessing_buffer();
2209 private:
2210 // In some cases we need to keep a list of the input sections
2211 // associated with this output section. We only need the list if we
2212 // might have to change the offsets of the input section within the
2213 // output section after we add the input section. The ordinary
2214 // input sections will be written out when we process the object
2215 // file, and as such we don't need to track them here. We do need
2216 // to track Output_section_data objects here. We store instances of
2217 // this structure in a std::vector, so it must be a POD. There can
2218 // be many instances of this structure, so we use a union to save
2219 // some space.
2220 class Input_section
2222 public:
2223 Input_section()
2224 : shndx_(0), p2align_(0)
2226 this->u1_.data_size = 0;
2227 this->u2_.object = NULL;
2230 // For an ordinary input section.
2231 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2232 uint64_t addralign)
2233 : shndx_(shndx),
2234 p2align_(ffsll(static_cast<long long>(addralign)))
2236 gold_assert(shndx != OUTPUT_SECTION_CODE
2237 && shndx != MERGE_DATA_SECTION_CODE
2238 && shndx != MERGE_STRING_SECTION_CODE);
2239 this->u1_.data_size = data_size;
2240 this->u2_.object = object;
2243 // For a non-merge output section.
2244 Input_section(Output_section_data* posd)
2245 : shndx_(OUTPUT_SECTION_CODE),
2246 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2248 this->u1_.data_size = 0;
2249 this->u2_.posd = posd;
2252 // For a merge section.
2253 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2254 : shndx_(is_string
2255 ? MERGE_STRING_SECTION_CODE
2256 : MERGE_DATA_SECTION_CODE),
2257 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2259 this->u1_.entsize = entsize;
2260 this->u2_.posd = posd;
2263 // The required alignment.
2264 uint64_t
2265 addralign() const
2267 return (this->p2align_ == 0
2269 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2272 // Return the required size.
2273 off_t
2274 data_size() const;
2276 // Whether this is an input section.
2277 bool
2278 is_input_section() const
2280 return (this->shndx_ != OUTPUT_SECTION_CODE
2281 && this->shndx_ != MERGE_DATA_SECTION_CODE
2282 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2285 // Return whether this is a merge section which matches the
2286 // parameters.
2287 bool
2288 is_merge_section(bool is_string, uint64_t entsize,
2289 uint64_t addralign) const
2291 return (this->shndx_ == (is_string
2292 ? MERGE_STRING_SECTION_CODE
2293 : MERGE_DATA_SECTION_CODE)
2294 && this->u1_.entsize == entsize
2295 && this->addralign() == addralign);
2298 // Return the object for an input section.
2299 Relobj*
2300 relobj() const
2302 gold_assert(this->is_input_section());
2303 return this->u2_.object;
2306 // Return the input section index for an input section.
2307 unsigned int
2308 shndx() const
2310 gold_assert(this->is_input_section());
2311 return this->shndx_;
2314 // Set the output section.
2315 void
2316 set_output_section(Output_section* os)
2318 gold_assert(!this->is_input_section());
2319 this->u2_.posd->set_output_section(os);
2322 // Set the address and file offset. This is called during
2323 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2324 // the enclosing section.
2325 void
2326 set_address_and_file_offset(uint64_t address, off_t file_offset,
2327 off_t section_file_offset);
2329 // Reset the address and file offset.
2330 void
2331 reset_address_and_file_offset();
2333 // Finalize the data size.
2334 void
2335 finalize_data_size();
2337 // Add an input section, for SHF_MERGE sections.
2338 bool
2339 add_input_section(Relobj* object, unsigned int shndx)
2341 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2342 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2343 return this->u2_.posd->add_input_section(object, shndx);
2346 // Given an input OBJECT, an input section index SHNDX within that
2347 // object, and an OFFSET relative to the start of that input
2348 // section, return whether or not the output offset is known. If
2349 // this function returns true, it sets *POUTPUT to the offset in
2350 // the output section, relative to the start of the input section
2351 // in the output section. *POUTPUT may be different from OFFSET
2352 // for a merged section.
2353 bool
2354 output_offset(const Relobj* object, unsigned int shndx,
2355 section_offset_type offset,
2356 section_offset_type *poutput) const;
2358 // Return whether this is the merge section for the input section
2359 // SHNDX in OBJECT.
2360 bool
2361 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2363 // Write out the data. This does nothing for an input section.
2364 void
2365 write(Output_file*);
2367 // Write the data to a buffer. This does nothing for an input
2368 // section.
2369 void
2370 write_to_buffer(unsigned char*);
2372 // Print statistics about merge sections to stderr.
2373 void
2374 print_merge_stats(const char* section_name)
2376 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2377 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2378 this->u2_.posd->print_merge_stats(section_name);
2381 private:
2382 // Code values which appear in shndx_. If the value is not one of
2383 // these codes, it is the input section index in the object file.
2384 enum
2386 // An Output_section_data.
2387 OUTPUT_SECTION_CODE = -1U,
2388 // An Output_section_data for an SHF_MERGE section with
2389 // SHF_STRINGS not set.
2390 MERGE_DATA_SECTION_CODE = -2U,
2391 // An Output_section_data for an SHF_MERGE section with
2392 // SHF_STRINGS set.
2393 MERGE_STRING_SECTION_CODE = -3U
2396 // For an ordinary input section, this is the section index in the
2397 // input file. For an Output_section_data, this is
2398 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2399 // MERGE_STRING_SECTION_CODE.
2400 unsigned int shndx_;
2401 // The required alignment, stored as a power of 2.
2402 unsigned int p2align_;
2403 union
2405 // For an ordinary input section, the section size.
2406 off_t data_size;
2407 // For OUTPUT_SECTION_CODE, this is not used. For
2408 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2409 // entity size.
2410 uint64_t entsize;
2411 } u1_;
2412 union
2414 // For an ordinary input section, the object which holds the
2415 // input section.
2416 Relobj* object;
2417 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2418 // MERGE_STRING_SECTION_CODE, the data.
2419 Output_section_data* posd;
2420 } u2_;
2423 typedef std::vector<Input_section> Input_section_list;
2425 // This class is used to sort the input sections.
2426 class Input_section_sort_entry;
2428 // This is the sort comparison function.
2429 struct Input_section_sort_compare
2431 bool
2432 operator()(const Input_section_sort_entry&,
2433 const Input_section_sort_entry&) const;
2436 // Fill data. This is used to fill in data between input sections.
2437 // It is also used for data statements (BYTE, WORD, etc.) in linker
2438 // scripts. When we have to keep track of the input sections, we
2439 // can use an Output_data_const, but we don't want to have to keep
2440 // track of input sections just to implement fills.
2441 class Fill
2443 public:
2444 Fill(off_t section_offset, off_t length)
2445 : section_offset_(section_offset),
2446 length_(convert_to_section_size_type(length))
2449 // Return section offset.
2450 off_t
2451 section_offset() const
2452 { return this->section_offset_; }
2454 // Return fill length.
2455 section_size_type
2456 length() const
2457 { return this->length_; }
2459 private:
2460 // The offset within the output section.
2461 off_t section_offset_;
2462 // The length of the space to fill.
2463 section_size_type length_;
2466 typedef std::vector<Fill> Fill_list;
2468 // Add a new output section by Input_section.
2469 void
2470 add_output_section_data(Input_section*);
2472 // Add an SHF_MERGE input section. Returns true if the section was
2473 // handled.
2474 bool
2475 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2476 uint64_t entsize, uint64_t addralign);
2478 // Add an output SHF_MERGE section POSD to this output section.
2479 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2480 // ENTSIZE is the entity size. This returns the entry added to
2481 // input_sections_.
2482 void
2483 add_output_merge_section(Output_section_data* posd, bool is_string,
2484 uint64_t entsize);
2486 // Sort the attached input sections.
2487 void
2488 sort_attached_input_sections();
2490 // Most of these fields are only valid after layout.
2492 // The name of the section. This will point into a Stringpool.
2493 const char* name_;
2494 // The section address is in the parent class.
2495 // The section alignment.
2496 uint64_t addralign_;
2497 // The section entry size.
2498 uint64_t entsize_;
2499 // The load address. This is only used when using a linker script
2500 // with a SECTIONS clause. The has_load_address_ field indicates
2501 // whether this field is valid.
2502 uint64_t load_address_;
2503 // The file offset is in the parent class.
2504 // Set the section link field to the index of this section.
2505 const Output_data* link_section_;
2506 // If link_section_ is NULL, this is the link field.
2507 unsigned int link_;
2508 // Set the section info field to the index of this section.
2509 const Output_section* info_section_;
2510 // If info_section_ is NULL, set the info field to the symbol table
2511 // index of this symbol.
2512 const Symbol* info_symndx_;
2513 // If info_section_ and info_symndx_ are NULL, this is the section
2514 // info field.
2515 unsigned int info_;
2516 // The section type.
2517 const elfcpp::Elf_Word type_;
2518 // The section flags.
2519 elfcpp::Elf_Xword flags_;
2520 // The section index.
2521 unsigned int out_shndx_;
2522 // If there is a STT_SECTION for this output section in the normal
2523 // symbol table, this is the symbol index. This starts out as zero.
2524 // It is initialized in Layout::finalize() to be the index, or -1U
2525 // if there isn't one.
2526 unsigned int symtab_index_;
2527 // If there is a STT_SECTION for this output section in the dynamic
2528 // symbol table, this is the symbol index. This starts out as zero.
2529 // It is initialized in Layout::finalize() to be the index, or -1U
2530 // if there isn't one.
2531 unsigned int dynsym_index_;
2532 // The input sections. This will be empty in cases where we don't
2533 // need to keep track of them.
2534 Input_section_list input_sections_;
2535 // The offset of the first entry in input_sections_.
2536 off_t first_input_offset_;
2537 // The fill data. This is separate from input_sections_ because we
2538 // often will need fill sections without needing to keep track of
2539 // input sections.
2540 Fill_list fills_;
2541 // If the section requires postprocessing, this buffer holds the
2542 // section contents during relocation.
2543 unsigned char* postprocessing_buffer_;
2544 // Whether this output section needs a STT_SECTION symbol in the
2545 // normal symbol table. This will be true if there is a relocation
2546 // which needs it.
2547 bool needs_symtab_index_ : 1;
2548 // Whether this output section needs a STT_SECTION symbol in the
2549 // dynamic symbol table. This will be true if there is a dynamic
2550 // relocation which needs it.
2551 bool needs_dynsym_index_ : 1;
2552 // Whether the link field of this output section should point to the
2553 // normal symbol table.
2554 bool should_link_to_symtab_ : 1;
2555 // Whether the link field of this output section should point to the
2556 // dynamic symbol table.
2557 bool should_link_to_dynsym_ : 1;
2558 // Whether this section should be written after all the input
2559 // sections are complete.
2560 bool after_input_sections_ : 1;
2561 // Whether this section requires post processing after all
2562 // relocations have been applied.
2563 bool requires_postprocessing_ : 1;
2564 // Whether an input section was mapped to this output section
2565 // because of a SECTIONS clause in a linker script.
2566 bool found_in_sections_clause_ : 1;
2567 // Whether this section has an explicitly specified load address.
2568 bool has_load_address_ : 1;
2569 // True if the info_section_ field means the section index of the
2570 // section, false if it means the symbol index of the corresponding
2571 // section symbol.
2572 bool info_uses_section_index_ : 1;
2573 // True if the input sections attached to this output section may
2574 // need sorting.
2575 bool may_sort_attached_input_sections_ : 1;
2576 // True if the input sections attached to this output section must
2577 // be sorted.
2578 bool must_sort_attached_input_sections_ : 1;
2579 // True if the input sections attached to this output section have
2580 // already been sorted.
2581 bool attached_input_sections_are_sorted_ : 1;
2582 // For SHT_TLS sections, the offset of this section relative to the base
2583 // of the TLS segment.
2584 uint64_t tls_offset_;
2587 // An output segment. PT_LOAD segments are built from collections of
2588 // output sections. Other segments typically point within PT_LOAD
2589 // segments, and are built directly as needed.
2591 class Output_segment
2593 public:
2594 // Create an output segment, specifying the type and flags.
2595 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2597 // Return the virtual address.
2598 uint64_t
2599 vaddr() const
2600 { return this->vaddr_; }
2602 // Return the physical address.
2603 uint64_t
2604 paddr() const
2605 { return this->paddr_; }
2607 // Return the segment type.
2608 elfcpp::Elf_Word
2609 type() const
2610 { return this->type_; }
2612 // Return the segment flags.
2613 elfcpp::Elf_Word
2614 flags() const
2615 { return this->flags_; }
2617 // Return the memory size.
2618 uint64_t
2619 memsz() const
2620 { return this->memsz_; }
2622 // Return the file size.
2623 off_t
2624 filesz() const
2625 { return this->filesz_; }
2627 // Return the file offset.
2628 off_t
2629 offset() const
2630 { return this->offset_; }
2632 // Return the maximum alignment of the Output_data.
2633 uint64_t
2634 maximum_alignment();
2636 // Add an Output_section to this segment.
2637 void
2638 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2639 { this->add_output_section(os, seg_flags, false); }
2641 // Add an Output_section to the start of this segment.
2642 void
2643 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2644 { this->add_output_section(os, seg_flags, true); }
2646 // Remove an Output_section from this segment. It is an error if it
2647 // is not present.
2648 void
2649 remove_output_section(Output_section* os);
2651 // Add an Output_data (which is not an Output_section) to the start
2652 // of this segment.
2653 void
2654 add_initial_output_data(Output_data*);
2656 // Return true if this segment has any sections which hold actual
2657 // data, rather than being a BSS section.
2658 bool
2659 has_any_data_sections() const
2660 { return !this->output_data_.empty(); }
2662 // Return the number of dynamic relocations applied to this segment.
2663 unsigned int
2664 dynamic_reloc_count() const;
2666 // Return the address of the first section.
2667 uint64_t
2668 first_section_load_address() const;
2670 // Return whether the addresses have been set already.
2671 bool
2672 are_addresses_set() const
2673 { return this->are_addresses_set_; }
2675 // Set the addresses.
2676 void
2677 set_addresses(uint64_t vaddr, uint64_t paddr)
2679 this->vaddr_ = vaddr;
2680 this->paddr_ = paddr;
2681 this->are_addresses_set_ = true;
2684 // Set the segment flags. This is only used if we have a PHDRS
2685 // clause which explicitly specifies the flags.
2686 void
2687 set_flags(elfcpp::Elf_Word flags)
2688 { this->flags_ = flags; }
2690 // Set the address of the segment to ADDR and the offset to *POFF
2691 // and set the addresses and offsets of all contained output
2692 // sections accordingly. Set the section indexes of all contained
2693 // output sections starting with *PSHNDX. If RESET is true, first
2694 // reset the addresses of the contained sections. Return the
2695 // address of the immediately following segment. Update *POFF and
2696 // *PSHNDX. This should only be called for a PT_LOAD segment.
2697 uint64_t
2698 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
2699 unsigned int* pshndx);
2701 // Set the minimum alignment of this segment. This may be adjusted
2702 // upward based on the section alignments.
2703 void
2704 set_minimum_p_align(uint64_t align)
2705 { this->min_p_align_ = align; }
2707 // Set the offset of this segment based on the section. This should
2708 // only be called for a non-PT_LOAD segment.
2709 void
2710 set_offset();
2712 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2713 void
2714 set_tls_offsets();
2716 // Return the number of output sections.
2717 unsigned int
2718 output_section_count() const;
2720 // Return the section attached to the list segment with the lowest
2721 // load address. This is used when handling a PHDRS clause in a
2722 // linker script.
2723 Output_section*
2724 section_with_lowest_load_address() const;
2726 // Write the segment header into *OPHDR.
2727 template<int size, bool big_endian>
2728 void
2729 write_header(elfcpp::Phdr_write<size, big_endian>*);
2731 // Write the section headers of associated sections into V.
2732 template<int size, bool big_endian>
2733 unsigned char*
2734 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2735 unsigned int* pshndx) const;
2737 private:
2738 Output_segment(const Output_segment&);
2739 Output_segment& operator=(const Output_segment&);
2741 typedef std::list<Output_data*> Output_data_list;
2743 // Add an Output_section to this segment, specifying front or back.
2744 void
2745 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2746 bool front);
2748 // Find the maximum alignment in an Output_data_list.
2749 static uint64_t
2750 maximum_alignment_list(const Output_data_list*);
2752 // Set the section addresses in an Output_data_list.
2753 uint64_t
2754 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2755 uint64_t addr, off_t* poff, unsigned int* pshndx,
2756 bool* in_tls);
2758 // Return the number of Output_sections in an Output_data_list.
2759 unsigned int
2760 output_section_count_list(const Output_data_list*) const;
2762 // Return the number of dynamic relocs in an Output_data_list.
2763 unsigned int
2764 dynamic_reloc_count_list(const Output_data_list*) const;
2766 // Find the section with the lowest load address in an
2767 // Output_data_list.
2768 void
2769 lowest_load_address_in_list(const Output_data_list* pdl,
2770 Output_section** found,
2771 uint64_t* found_lma) const;
2773 // Write the section headers in the list into V.
2774 template<int size, bool big_endian>
2775 unsigned char*
2776 write_section_headers_list(const Layout*, const Stringpool*,
2777 const Output_data_list*, unsigned char* v,
2778 unsigned int* pshdx) const;
2780 // The list of output data with contents attached to this segment.
2781 Output_data_list output_data_;
2782 // The list of output data without contents attached to this segment.
2783 Output_data_list output_bss_;
2784 // The segment virtual address.
2785 uint64_t vaddr_;
2786 // The segment physical address.
2787 uint64_t paddr_;
2788 // The size of the segment in memory.
2789 uint64_t memsz_;
2790 // The maximum section alignment. The is_max_align_known_ field
2791 // indicates whether this has been finalized.
2792 uint64_t max_align_;
2793 // The required minimum value for the p_align field. This is used
2794 // for PT_LOAD segments. Note that this does not mean that
2795 // addresses should be aligned to this value; it means the p_paddr
2796 // and p_vaddr fields must be congruent modulo this value. For
2797 // non-PT_LOAD segments, the dynamic linker works more efficiently
2798 // if the p_align field has the more conventional value, although it
2799 // can align as needed.
2800 uint64_t min_p_align_;
2801 // The offset of the segment data within the file.
2802 off_t offset_;
2803 // The size of the segment data in the file.
2804 off_t filesz_;
2805 // The segment type;
2806 elfcpp::Elf_Word type_;
2807 // The segment flags.
2808 elfcpp::Elf_Word flags_;
2809 // Whether we have finalized max_align_.
2810 bool is_max_align_known_ : 1;
2811 // Whether vaddr and paddr were set by a linker script.
2812 bool are_addresses_set_ : 1;
2815 // This class represents the output file.
2817 class Output_file
2819 public:
2820 Output_file(const char* name);
2822 // Indicate that this is a temporary file which should not be
2823 // output.
2824 void
2825 set_is_temporary()
2826 { this->is_temporary_ = true; }
2828 // Open the output file. FILE_SIZE is the final size of the file.
2829 void
2830 open(off_t file_size);
2832 // Resize the output file.
2833 void
2834 resize(off_t file_size);
2836 // Close the output file (flushing all buffered data) and make sure
2837 // there are no errors.
2838 void
2839 close();
2841 // We currently always use mmap which makes the view handling quite
2842 // simple. In the future we may support other approaches.
2844 // Write data to the output file.
2845 void
2846 write(off_t offset, const void* data, size_t len)
2847 { memcpy(this->base_ + offset, data, len); }
2849 // Get a buffer to use to write to the file, given the offset into
2850 // the file and the size.
2851 unsigned char*
2852 get_output_view(off_t start, size_t size)
2854 gold_assert(start >= 0
2855 && start + static_cast<off_t>(size) <= this->file_size_);
2856 return this->base_ + start;
2859 // VIEW must have been returned by get_output_view. Write the
2860 // buffer to the file, passing in the offset and the size.
2861 void
2862 write_output_view(off_t, size_t, unsigned char*)
2865 // Get a read/write buffer. This is used when we want to write part
2866 // of the file, read it in, and write it again.
2867 unsigned char*
2868 get_input_output_view(off_t start, size_t size)
2869 { return this->get_output_view(start, size); }
2871 // Write a read/write buffer back to the file.
2872 void
2873 write_input_output_view(off_t, size_t, unsigned char*)
2876 // Get a read buffer. This is used when we just want to read part
2877 // of the file back it in.
2878 const unsigned char*
2879 get_input_view(off_t start, size_t size)
2880 { return this->get_output_view(start, size); }
2882 // Release a read bfufer.
2883 void
2884 free_input_view(off_t, size_t, const unsigned char*)
2887 private:
2888 // Map the file into memory and return a pointer to the map.
2889 void
2890 map();
2892 // Unmap the file from memory (and flush to disk buffers).
2893 void
2894 unmap();
2896 // File name.
2897 const char* name_;
2898 // File descriptor.
2899 int o_;
2900 // File size.
2901 off_t file_size_;
2902 // Base of file mapped into memory.
2903 unsigned char* base_;
2904 // True iff base_ points to a memory buffer rather than an output file.
2905 bool map_is_anonymous_;
2906 // True if this is a temporary file which should not be output.
2907 bool is_temporary_;
2910 } // End namespace gold.
2912 #endif // !defined(GOLD_OUTPUT_H)